Merge from origin/emacs-25
[emacs.git] / test / lisp / emacs-lisp / seq-tests.el
blob788524bedb558335840a04050e7e5f1cd5d621f8
1 ;;; seq-tests.el --- Tests for sequences.el
3 ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Maintainer: emacs-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Tests for sequences.el
27 ;;; Code:
29 (require 'ert)
30 (require 'seq)
32 (defmacro with-test-sequences (spec &rest body)
33 "Successively bind VAR to a list, vector, and string built from SEQ.
34 Evaluate BODY for each created sequence.
36 \(fn (var seq) body)"
37 (declare (indent 1) (debug ((symbolp form) body)))
38 (let ((initial-seq (make-symbol "initial-seq")))
39 `(let ((,initial-seq ,(cadr spec)))
40 ,@(mapcar (lambda (s)
41 `(let ((,(car spec) (apply (function ,s) ,initial-seq)))
42 ,@body))
43 '(list vector string)))))
45 (defun same-contents-p (seq1 seq2)
46 "Return t if SEQ1 and SEQ2 have the same contents, nil otherwise."
47 (equal (append seq1 '()) (append seq2 '())))
49 (defun test-sequences-evenp (integer)
50 "Return t if INTEGER is even."
51 (eq (logand integer 1) 0))
53 (defun test-sequences-oddp (integer)
54 "Return t if INTEGER is odd."
55 (not (test-sequences-evenp integer)))
57 (ert-deftest test-setf-seq-elt ()
58 (with-test-sequences (seq '(1 2 3))
59 (setf (seq-elt seq 1) 4)
60 (should (= 4 (seq-elt seq 1)))))
62 (ert-deftest test-seq-drop ()
63 (with-test-sequences (seq '(1 2 3 4))
64 (should (equal (seq-drop seq 0) seq))
65 (should (equal (seq-drop seq 1) (seq-subseq seq 1)))
66 (should (equal (seq-drop seq 2) (seq-subseq seq 2)))
67 (should (seq-empty-p (seq-drop seq 4)))
68 (should (seq-empty-p (seq-drop seq 10))))
69 (with-test-sequences (seq '())
70 (should (seq-empty-p (seq-drop seq 0)))
71 (should (seq-empty-p (seq-drop seq 1)))))
73 (ert-deftest test-seq-take ()
74 (with-test-sequences (seq '(2 3 4 5))
75 (should (seq-empty-p (seq-take seq 0)))
76 (should (= (seq-length (seq-take seq 1)) 1))
77 (should (= (seq-elt (seq-take seq 1) 0) 2))
78 (should (same-contents-p (seq-take seq 3) '(2 3 4)))
79 (should (equal (seq-take seq 10) seq))))
81 (ert-deftest test-seq-drop-while ()
82 (with-test-sequences (seq '(1 3 2 4))
83 (should (equal (seq-drop-while #'test-sequences-oddp seq)
84 (seq-drop seq 2)))
85 (should (equal (seq-drop-while #'test-sequences-evenp seq)
86 seq))
87 (should (seq-empty-p (seq-drop-while #'numberp seq))))
88 (with-test-sequences (seq '())
89 (should (seq-empty-p (seq-drop-while #'test-sequences-oddp seq)))))
91 (ert-deftest test-seq-take-while ()
92 (with-test-sequences (seq '(1 3 2 4))
93 (should (equal (seq-take-while #'test-sequences-oddp seq)
94 (seq-take seq 2)))
95 (should (seq-empty-p (seq-take-while #'test-sequences-evenp seq)))
96 (should (equal (seq-take-while #'numberp seq) seq)))
97 (with-test-sequences (seq '())
98 (should (seq-empty-p (seq-take-while #'test-sequences-oddp seq)))))
100 (ert-deftest test-seq-map-indexed ()
101 (should (equal (seq-map-indexed (lambda (elt i)
102 (list elt i))
103 nil)
104 nil))
105 (should (equal (seq-map-indexed (lambda (elt i)
106 (list elt i))
107 '(a b c d))
108 '((a 0) (b 1) (c 2) (d 3)))))
110 (ert-deftest test-seq-do-indexed ()
111 (let ((result nil))
112 (seq-do-indexed (lambda (elt i)
113 (add-to-list 'result (list elt i)))
114 nil)
115 (should (equal result nil)))
116 (with-test-sequences (seq '(4 5 6))
117 (let ((result nil))
118 (seq-do-indexed (lambda (elt i)
119 (add-to-list 'result (list elt i)))
120 seq)
121 (should (equal (seq-elt result 0) '(6 2)))
122 (should (equal (seq-elt result 1) '(5 1)))
123 (should (equal (seq-elt result 2) '(4 0))))))
125 (ert-deftest test-seq-filter ()
126 (with-test-sequences (seq '(6 7 8 9 10))
127 (should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10)))
128 (should (equal (seq-filter #'test-sequences-oddp seq) '(7 9)))
129 (should (equal (seq-filter (lambda (elt) nil) seq) '())))
130 (with-test-sequences (seq '())
131 (should (equal (seq-filter #'test-sequences-evenp seq) '()))))
133 (ert-deftest test-seq-remove ()
134 (with-test-sequences (seq '(6 7 8 9 10))
135 (should (equal (seq-remove #'test-sequences-evenp seq) '(7 9)))
136 (should (equal (seq-remove #'test-sequences-oddp seq) '(6 8 10)))
137 (should (same-contents-p (seq-remove (lambda (elt) nil) seq) seq)))
138 (with-test-sequences (seq '())
139 (should (equal (seq-remove #'test-sequences-evenp seq) '()))))
141 (ert-deftest test-seq-count ()
142 (with-test-sequences (seq '(6 7 8 9 10))
143 (should (equal (seq-count #'test-sequences-evenp seq) 3))
144 (should (equal (seq-count #'test-sequences-oddp seq) 2))
145 (should (equal (seq-count (lambda (elt) nil) seq) 0)))
146 (with-test-sequences (seq '())
147 (should (equal (seq-count #'test-sequences-evenp seq) 0))))
149 (ert-deftest test-seq-reduce ()
150 (with-test-sequences (seq '(1 2 3 4))
151 (should (= (seq-reduce #'+ seq 0) 10))
152 (should (= (seq-reduce #'+ seq 5) 15)))
153 (with-test-sequences (seq '())
154 (should (eq (seq-reduce #'+ seq 0) 0))
155 (should (eq (seq-reduce #'+ seq 7) 7))))
157 (ert-deftest test-seq-some ()
158 (with-test-sequences (seq '(4 3 2 1))
159 (should (seq-some #'test-sequences-evenp seq))
160 (should (seq-some #'test-sequences-oddp seq))
161 (should-not (seq-some (lambda (elt) (> elt 10)) seq)))
162 (with-test-sequences (seq '())
163 (should-not (seq-some #'test-sequences-oddp seq)))
164 (should (seq-some #'null '(1 nil 2))))
166 (ert-deftest test-seq-find ()
167 (with-test-sequences (seq '(4 3 2 1))
168 (should (= 4 (seq-find #'test-sequences-evenp seq)))
169 (should (= 3 (seq-find #'test-sequences-oddp seq)))
170 (should-not (seq-find (lambda (elt) (> elt 10)) seq)))
171 (should-not (seq-find #'null '(1 nil 2)))
172 (should-not (seq-find #'null '(1 nil 2) t))
173 (should-not (seq-find #'null '(1 2 3)))
174 (should (seq-find #'null '(1 2 3) 'sentinel)))
176 (ert-deftest test-seq-contains ()
177 (with-test-sequences (seq '(3 4 5 6))
178 (should (seq-contains seq 3))
179 (should-not (seq-contains seq 7)))
180 (with-test-sequences (seq '())
181 (should-not (seq-contains seq 3))
182 (should-not (seq-contains seq nil))))
184 (ert-deftest test-seq-contains-should-return-the-elt ()
185 (with-test-sequences (seq '(3 4 5 6))
186 (should (= 5 (seq-contains seq 5)))))
188 (ert-deftest test-seq-every-p ()
189 (with-test-sequences (seq '(43 54 22 1))
190 (should (seq-every-p (lambda (elt) t) seq))
191 (should-not (seq-every-p #'test-sequences-oddp seq))
192 (should-not (seq-every-p #'test-sequences-evenp seq)))
193 (with-test-sequences (seq '(42 54 22 2))
194 (should (seq-every-p #'test-sequences-evenp seq))
195 (should-not (seq-every-p #'test-sequences-oddp seq)))
196 (with-test-sequences (seq '())
197 (should (seq-every-p #'identity seq))
198 (should (seq-every-p #'test-sequences-evenp seq))))
200 (ert-deftest test-seq-empty-p ()
201 (with-test-sequences (seq '(0))
202 (should-not (seq-empty-p seq)))
203 (with-test-sequences (seq '(0 1 2))
204 (should-not (seq-empty-p seq)))
205 (with-test-sequences (seq '())
206 (should (seq-empty-p seq))))
208 (ert-deftest test-seq-sort ()
209 (should (equal (seq-sort #'< "cbaf") "abcf"))
210 (should (equal (seq-sort #'< '(2 1 9 4)) '(1 2 4 9)))
211 (should (equal (seq-sort #'< [2 1 9 4]) [1 2 4 9]))
212 (should (equal (seq-sort #'< "") "")))
214 (ert-deftest test-seq-uniq ()
215 (with-test-sequences (seq '(2 4 6 8 6 4 3))
216 (should (equal (seq-uniq seq) '(2 4 6 8 3))))
217 (with-test-sequences (seq '(3 3 3 3 3))
218 (should (equal (seq-uniq seq) '(3))))
219 (with-test-sequences (seq '())
220 (should (equal (seq-uniq seq) '()))))
222 (ert-deftest test-seq-subseq ()
223 (with-test-sequences (seq '(2 3 4 5))
224 (should (equal (seq-subseq seq 0 4) seq))
225 (should (same-contents-p (seq-subseq seq 2 4) '(4 5)))
226 (should (same-contents-p (seq-subseq seq 1 3) '(3 4)))
227 (should (same-contents-p (seq-subseq seq 1 -1) '(3 4))))
228 (should (vectorp (seq-subseq [2 3 4 5] 2)))
229 (should (stringp (seq-subseq "foo" 2 3)))
230 (should (listp (seq-subseq '(2 3 4 4) 2 3)))
231 (should-error (seq-subseq '(1 2 3) 4))
232 (should-not (seq-subseq '(1 2 3) 3))
233 (should (seq-subseq '(1 2 3) -3))
234 (should-error (seq-subseq '(1 2 3) 1 4))
235 (should (seq-subseq '(1 2 3) 1 3))
236 (should-error (seq-subseq '() -1))
237 (should-error (seq-subseq [] -1))
238 (should-error (seq-subseq "" -1))
239 (should-not (seq-subseq '() 0))
240 (should-error (seq-subseq '() 0 -1)))
242 (ert-deftest test-seq-concatenate ()
243 (with-test-sequences (seq '(2 4 6))
244 (should (equal (seq-concatenate 'string seq [8]) (string 2 4 6 8)))
245 (should (equal (seq-concatenate 'list seq '(8 10)) '(2 4 6 8 10)))
246 (should (equal (seq-concatenate 'vector seq '(8 10)) [2 4 6 8 10]))
247 (should (equal (seq-concatenate 'vector nil '(8 10)) [8 10]))
248 (should (equal (seq-concatenate 'vector seq nil) [2 4 6]))))
250 (ert-deftest test-seq-mapcat ()
251 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
252 '(1 2 3 4 5 6)))
253 (should (equal (seq-mapcat #'seq-reverse '[(3 2 1) (6 5 4)])
254 '(1 2 3 4 5 6)))
255 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)) 'vector)
256 '[1 2 3 4 5 6])))
258 (ert-deftest test-seq-partition ()
259 (should (same-contents-p (seq-partition '(0 1 2 3 4 5 6 7) 3)
260 '((0 1 2) (3 4 5) (6 7))))
261 (should (same-contents-p (seq-partition '[0 1 2 3 4 5 6 7] 3)
262 '([0 1 2] [3 4 5] [6 7])))
263 (should (same-contents-p (seq-partition "Hello world" 2)
264 '("He" "ll" "o " "wo" "rl" "d")))
265 (should (equal (seq-partition '() 2) '()))
266 (should (equal (seq-partition '(1 2 3) -1) '())))
268 (ert-deftest test-seq-group-by ()
269 (with-test-sequences (seq '(1 2 3 4))
270 (should (equal (seq-group-by #'test-sequences-oddp seq)
271 '((t 1 3) (nil 2 4)))))
272 (should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2)))
273 '((b (b 3)) (c (c 4)) (a (a 1) (a 2))))))
275 (ert-deftest test-seq-reverse ()
276 (with-test-sequences (seq '(1 2 3 4))
277 (should (same-contents-p (seq-reverse seq) '(4 3 2 1)))
278 (should (equal (type-of (seq-reverse seq))
279 (type-of seq)))))
281 (ert-deftest test-seq-into ()
282 (let* ((vector [1 2 3])
283 (list (seq-into vector 'list)))
284 (should (same-contents-p vector list))
285 (should (listp list)))
286 (let* ((list '(hello world))
287 (vector (seq-into list 'vector)))
288 (should (same-contents-p vector list))
289 (should (vectorp vector)))
290 (let* ((string "hello")
291 (list (seq-into string 'list)))
292 (should (same-contents-p string list))
293 (should (stringp string)))
294 (let* ((string "hello")
295 (vector (seq-into string 'vector)))
296 (should (same-contents-p string vector))
297 (should (stringp string)))
298 (let* ((list nil)
299 (vector (seq-into list 'vector)))
300 (should (same-contents-p list vector))
301 (should (vectorp vector))))
303 (ert-deftest test-seq-intersection ()
304 (let ((v1 [2 3 4 5])
305 (v2 [1 3 5 6 7]))
306 (should (same-contents-p (seq-intersection v1 v2)
307 '(3 5))))
308 (let ((l1 '(2 3 4 5))
309 (l2 '(1 3 5 6 7)))
310 (should (same-contents-p (seq-intersection l1 l2)
311 '(3 5))))
312 (let ((v1 [2 4 6])
313 (v2 [1 3 5]))
314 (should (seq-empty-p (seq-intersection v1 v2)))))
316 (ert-deftest test-seq-difference ()
317 (let ((v1 [2 3 4 5])
318 (v2 [1 3 5 6 7]))
319 (should (same-contents-p (seq-difference v1 v2)
320 '(2 4))))
321 (let ((l1 '(2 3 4 5))
322 (l2 '(1 3 5 6 7)))
323 (should (same-contents-p (seq-difference l1 l2)
324 '(2 4))))
325 (let ((v1 [2 4 6])
326 (v2 [2 4 6]))
327 (should (seq-empty-p (seq-difference v1 v2)))))
329 (ert-deftest test-seq-let ()
330 (with-test-sequences (seq '(1 2 3 4))
331 (seq-let (a b c d e) seq
332 (should (= a 1))
333 (should (= b 2))
334 (should (= c 3))
335 (should (= d 4))
336 (should (null e)))
337 (seq-let (a b &rest others) seq
338 (should (= a 1))
339 (should (= b 2))
340 (should (same-contents-p others (seq-drop seq 2)))))
341 (let ((seq '(1 (2 (3 (4))))))
342 (seq-let (_ (_ (_ (a)))) seq
343 (should (= a 4))))
344 (let (seq)
345 (seq-let (a b c) seq
346 (should (null a))
347 (should (null b))
348 (should (null c)))))
350 (ert-deftest test-seq-min-max ()
351 (with-test-sequences (seq '(4 5 3 2 0 4))
352 (should (= (seq-min seq) 0))
353 (should (= (seq-max seq) 5))))
355 (ert-deftest test-seq-into-sequence ()
356 (with-test-sequences (seq '(1 2 3))
357 (should (eq seq (seq-into-sequence seq)))
358 (should-error (seq-into-sequence 2))))
360 (ert-deftest test-seq-position ()
361 (with-test-sequences (seq '(2 4 6))
362 (should (null (seq-position seq 1)))
363 (should (= (seq-position seq 4) 1)))
364 (let ((seq '(a b c)))
365 (should (null (seq-position seq 'd #'eq)))
366 (should (= (seq-position seq 'a #'eq) 0))
367 (should (null (seq-position seq (make-symbol "a") #'eq)))))
369 (ert-deftest test-seq-sort-by ()
370 (let ((seq ["x" "xx" "xxx"]))
371 (should (equal (seq-sort-by #'seq-length #'> seq)
372 ["xxx" "xx" "x"]))))
374 (ert-deftest test-seq-random-elt-take-all ()
375 (let ((seq '(a b c d e))
376 (elts '()))
377 (should (= 0 (length elts)))
378 (dotimes (_ 1000)
379 (let ((random-elt (seq-random-elt seq)))
380 (add-to-list 'elts
381 random-elt)))
382 (should (= 5 (length elts)))))
384 (ert-deftest test-seq-random-elt-signal-on-empty ()
385 (should-error (seq-random-elt nil))
386 (should-error (seq-random-elt []))
387 (should-error (seq-random-elt "")))
389 (ert-deftest test-seq-mapn-circular-lists ()
390 (let ((l1 '#1=(1 . #1#)))
391 (should (equal (seq-mapn #'+ '(3 4 5 7) l1)
392 '(4 5 6 8)))))
394 (ert-deftest test-seq-into-and-identity ()
395 (let ((lst '(1 2 3))
396 (vec [1 2 3])
397 (str "foo bar"))
398 (should (eq (seq-into lst 'list) lst))
399 (should (eq (seq-into vec 'vector) vec))
400 (should (eq (seq-into str 'string) str))))
402 (provide 'seq-tests)
403 ;;; seq-tests.el ends here