diff options
author | Mattias EngdegÄrd <mattiase@acm.org> | 2022-07-18 11:32:22 +0200 |
---|---|---|
committer | Mattias EngdegÄrd <mattiase@acm.org> | 2022-07-18 12:49:29 +0200 |
commit | 6f7941272b112f0412479ffc315352d7928e0fdf (patch) | |
tree | 2a97d3b6d5d61d5a893f91a6c46bae46fcb656db /test | |
parent | 5ad8f3e5709a3823371ea6aa039b5e7e50feca1f (diff) | |
download | emacs-6f7941272b112f0412479ffc315352d7928e0fdf.tar.gz |
Speed up `seq-subseq` for lists (bug#56521)
* lisp/emacs-lisp/seq.el (seq-subseq):
Make faster by using `take` instead of a lisp loop,
and more importantly by not front-loading the error text formatting.
* test/lisp/emacs-lisp/seq-tests.el (seq-tests--list-subseq-ref)
(test-seq-subseq): Test `seq-subseq` for lists more thoroughly.
Diffstat (limited to 'test')
-rw-r--r-- | test/lisp/emacs-lisp/seq-tests.el | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index d979604910e..3b22e42df24 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el @@ -257,6 +257,19 @@ Evaluate BODY for each created sequence. (with-test-sequences (seq '()) (should (equal (seq-uniq seq) '())))) +(defun seq-tests--list-subseq-ref (list start &optional end) + "Reference implementation of `seq-subseq' for lists." + (let ((len (length list))) + (when (< start 0) + (setq start (+ start len))) + (unless end + (setq end len)) + (when (< end 0) + (setq end (+ end len))) + (if (<= 0 start end len) + (take (- end start) (nthcdr start list)) + (error "bad args")))) + (ert-deftest test-seq-subseq () (with-test-sequences (seq '(2 3 4 5)) (should (equal (seq-subseq seq 0 4) seq)) @@ -275,7 +288,21 @@ Evaluate BODY for each created sequence. (should-error (seq-subseq [] -1)) (should-error (seq-subseq "" -1)) (should-not (seq-subseq '() 0)) - (should-error (seq-subseq '() 0 -1))) + (should-error (seq-subseq '() 0 -1)) + + (dolist (list '(() (a b c d))) + (ert-info ((prin1-to-string list) :prefix "list: ") + (let ((len (length list))) + (dolist (start (number-sequence (- -2 len) (+ 2 len))) + (ert-info ((prin1-to-string start) :prefix "start: ") + (dolist (end (cons nil (number-sequence (- -2 len) (+ 2 len)))) + (ert-info ((prin1-to-string end) :prefix "end: ") + (condition-case res + (seq-tests--list-subseq-ref list start end) + (error + (should-error (seq-subseq list start end))) + (:success + (should (equal (seq-subseq list start end) res)))))))))))) (ert-deftest test-seq-concatenate () (with-test-sequences (seq '(2 4 6)) |