1 ;;; seq-tests.el --- Tests for sequences.el
3 ;; Copyright (C) 2014-2016 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/>.
25 ;; Tests for sequences.el
33 (defmacro with-test-sequences
(spec &rest body
)
34 "Successively bind VAR to a list, vector, and string built from SEQ.
35 Evaluate BODY for each created sequence.
38 (declare (indent 1) (debug ((symbolp form
) body
)))
39 (let ((initial-seq (make-symbol "initial-seq")))
40 `(let ((,initial-seq
,(cadr spec
)))
42 `(let ((,(car spec
) (apply (function ,s
) ,initial-seq
)))
44 '(list vector string
)))))
46 (defun same-contents-p (seq1 seq2
)
47 "Return t if SEQ1 and SEQ2 have the same contents, nil otherwise."
48 (equal (append seq1
'()) (append seq2
'())))
50 (defun test-sequences-evenp (integer)
51 "Return t if INTEGER is even."
52 (eq (logand integer
1) 0))
54 (defun test-sequences-oddp (integer)
55 "Return t if INTEGER is odd."
56 (not (test-sequences-evenp integer
)))
58 (ert-deftest test-setf-seq-elt
()
59 (with-test-sequences (seq '(1 2 3))
60 (setf (seq-elt seq
1) 4)
61 (should (= 4 (seq-elt seq
1)))))
63 (ert-deftest test-seq-drop
()
64 (with-test-sequences (seq '(1 2 3 4))
65 (should (equal (seq-drop seq
0) seq
))
66 (should (equal (seq-drop seq
1) (seq-subseq seq
1)))
67 (should (equal (seq-drop seq
2) (seq-subseq seq
2)))
68 (should (seq-empty-p (seq-drop seq
4)))
69 (should (seq-empty-p (seq-drop seq
10))))
70 (with-test-sequences (seq '())
71 (should (seq-empty-p (seq-drop seq
0)))
72 (should (seq-empty-p (seq-drop seq
1)))))
74 (ert-deftest test-seq-take
()
75 (with-test-sequences (seq '(2 3 4 5))
76 (should (seq-empty-p (seq-take seq
0)))
77 (should (= (seq-length (seq-take seq
1)) 1))
78 (should (= (seq-elt (seq-take seq
1) 0) 2))
79 (should (same-contents-p (seq-take seq
3) '(2 3 4)))
80 (should (equal (seq-take seq
10) seq
))))
82 (ert-deftest test-seq-drop-while
()
83 (with-test-sequences (seq '(1 3 2 4))
84 (should (equal (seq-drop-while #'test-sequences-oddp seq
)
86 (should (equal (seq-drop-while #'test-sequences-evenp seq
)
88 (should (seq-empty-p (seq-drop-while #'numberp seq
))))
89 (with-test-sequences (seq '())
90 (should (seq-empty-p (seq-drop-while #'test-sequences-oddp seq
)))))
92 (ert-deftest test-seq-take-while
()
93 (with-test-sequences (seq '(1 3 2 4))
94 (should (equal (seq-take-while #'test-sequences-oddp seq
)
96 (should (seq-empty-p (seq-take-while #'test-sequences-evenp seq
)))
97 (should (equal (seq-take-while #'numberp seq
) seq
)))
98 (with-test-sequences (seq '())
99 (should (seq-empty-p (seq-take-while #'test-sequences-oddp seq
)))))
101 (ert-deftest test-seq-map-indexed
()
102 (should (equal (seq-map-indexed (lambda (elt i
)
106 (should (equal (seq-map-indexed (lambda (elt i
)
109 '((a 0) (b 1) (c 2) (d 3)))))
111 (ert-deftest test-seq-do-indexed
()
113 (seq-do-indexed (lambda (elt i
)
114 (add-to-list 'result
(list elt i
)))
116 (should (equal result nil
)))
117 (with-test-sequences (seq '(4 5 6))
119 (seq-do-indexed (lambda (elt i
)
120 (add-to-list 'result
(list elt i
)))
122 (should (equal (seq-elt result
0) '(6 2)))
123 (should (equal (seq-elt result
1) '(5 1)))
124 (should (equal (seq-elt result
2) '(4 0))))))
126 (ert-deftest test-seq-filter
()
127 (with-test-sequences (seq '(6 7 8 9 10))
128 (should (equal (seq-filter #'test-sequences-evenp seq
) '(6 8 10)))
129 (should (equal (seq-filter #'test-sequences-oddp seq
) '(7 9)))
130 (should (equal (seq-filter (lambda (elt) nil
) seq
) '())))
131 (with-test-sequences (seq '())
132 (should (equal (seq-filter #'test-sequences-evenp seq
) '()))))
134 (ert-deftest test-seq-remove
()
135 (with-test-sequences (seq '(6 7 8 9 10))
136 (should (equal (seq-remove #'test-sequences-evenp seq
) '(7 9)))
137 (should (equal (seq-remove #'test-sequences-oddp seq
) '(6 8 10)))
138 (should (same-contents-p (seq-remove (lambda (elt) nil
) seq
) seq
)))
139 (with-test-sequences (seq '())
140 (should (equal (seq-remove #'test-sequences-evenp seq
) '()))))
142 (ert-deftest test-seq-count
()
143 (with-test-sequences (seq '(6 7 8 9 10))
144 (should (equal (seq-count #'test-sequences-evenp seq
) 3))
145 (should (equal (seq-count #'test-sequences-oddp seq
) 2))
146 (should (equal (seq-count (lambda (elt) nil
) seq
) 0)))
147 (with-test-sequences (seq '())
148 (should (equal (seq-count #'test-sequences-evenp seq
) 0))))
150 (ert-deftest test-seq-reduce
()
151 (with-test-sequences (seq '(1 2 3 4))
152 (should (= (seq-reduce #'+ seq
0) 10))
153 (should (= (seq-reduce #'+ seq
5) 15)))
154 (with-test-sequences (seq '())
155 (should (eq (seq-reduce #'+ seq
0) 0))
156 (should (eq (seq-reduce #'+ seq
7) 7))))
158 (ert-deftest test-seq-some
()
159 (with-test-sequences (seq '(4 3 2 1))
160 (should (seq-some #'test-sequences-evenp seq
))
161 (should (seq-some #'test-sequences-oddp seq
))
162 (should-not (seq-some (lambda (elt) (> elt
10)) seq
)))
163 (with-test-sequences (seq '())
164 (should-not (seq-some #'test-sequences-oddp seq
)))
165 (should (seq-some #'null
'(1 nil
2))))
167 (ert-deftest test-seq-find
()
168 (with-test-sequences (seq '(4 3 2 1))
169 (should (= 4 (seq-find #'test-sequences-evenp seq
)))
170 (should (= 3 (seq-find #'test-sequences-oddp seq
)))
171 (should-not (seq-find (lambda (elt) (> elt
10)) seq
)))
172 (should-not (seq-find #'null
'(1 nil
2)))
173 (should-not (seq-find #'null
'(1 nil
2) t
))
174 (should-not (seq-find #'null
'(1 2 3)))
175 (should (seq-find #'null
'(1 2 3) 'sentinel
)))
177 (ert-deftest test-seq-contains
()
178 (with-test-sequences (seq '(3 4 5 6))
179 (should (seq-contains seq
3))
180 (should-not (seq-contains seq
7)))
181 (with-test-sequences (seq '())
182 (should-not (seq-contains seq
3))
183 (should-not (seq-contains seq nil
))))
185 (ert-deftest test-seq-contains-should-return-the-elt
()
186 (with-test-sequences (seq '(3 4 5 6))
187 (should (= 5 (seq-contains seq
5)))))
189 (ert-deftest test-seq-every-p
()
190 (with-test-sequences (seq '(43 54 22 1))
191 (should (seq-every-p (lambda (elt) t
) seq
))
192 (should-not (seq-every-p #'test-sequences-oddp seq
))
193 (should-not (seq-every-p #'test-sequences-evenp seq
)))
194 (with-test-sequences (seq '(42 54 22 2))
195 (should (seq-every-p #'test-sequences-evenp seq
))
196 (should-not (seq-every-p #'test-sequences-oddp seq
)))
197 (with-test-sequences (seq '())
198 (should (seq-every-p #'identity seq
))
199 (should (seq-every-p #'test-sequences-evenp seq
))))
201 (ert-deftest test-seq-empty-p
()
202 (with-test-sequences (seq '(0))
203 (should-not (seq-empty-p seq
)))
204 (with-test-sequences (seq '(0 1 2))
205 (should-not (seq-empty-p seq
)))
206 (with-test-sequences (seq '())
207 (should (seq-empty-p seq
))))
209 (ert-deftest test-seq-sort
()
210 (should (equal (seq-sort #'< "cbaf") "abcf"))
211 (should (equal (seq-sort #'< '(2 1 9 4)) '(1 2 4 9)))
212 (should (equal (seq-sort #'< [2 1 9 4]) [1 2 4 9]))
213 (should (equal (seq-sort #'< "") "")))
215 (ert-deftest test-seq-uniq
()
216 (with-test-sequences (seq '(2 4 6 8 6 4 3))
217 (should (equal (seq-uniq seq
) '(2 4 6 8 3))))
218 (with-test-sequences (seq '(3 3 3 3 3))
219 (should (equal (seq-uniq seq
) '(3))))
220 (with-test-sequences (seq '())
221 (should (equal (seq-uniq seq
) '()))))
223 (ert-deftest test-seq-subseq
()
224 (with-test-sequences (seq '(2 3 4 5))
225 (should (equal (seq-subseq seq
0 4) seq
))
226 (should (same-contents-p (seq-subseq seq
2 4) '(4 5)))
227 (should (same-contents-p (seq-subseq seq
1 3) '(3 4)))
228 (should (same-contents-p (seq-subseq seq
1 -
1) '(3 4))))
229 (should (vectorp (seq-subseq [2 3 4 5] 2)))
230 (should (stringp (seq-subseq "foo" 2 3)))
231 (should (listp (seq-subseq '(2 3 4 4) 2 3)))
232 (should-error (seq-subseq '(1 2 3) 4))
233 (should-not (seq-subseq '(1 2 3) 3))
234 (should (seq-subseq '(1 2 3) -
3))
235 (should-error (seq-subseq '(1 2 3) 1 4))
236 (should (seq-subseq '(1 2 3) 1 3))
237 (should-error (seq-subseq '() -
1))
238 (should-error (seq-subseq [] -
1))
239 (should-error (seq-subseq "" -
1))
240 (should-not (seq-subseq '() 0))
241 (should-error (seq-subseq '() 0 -
1)))
243 (ert-deftest test-seq-concatenate
()
244 (with-test-sequences (seq '(2 4 6))
245 (should (equal (seq-concatenate 'string seq
[8]) (string 2 4 6 8)))
246 (should (equal (seq-concatenate 'list seq '(8 10)) '(2 4 6 8 10)))
247 (should (equal (seq-concatenate 'vector seq '(8 10)) [2 4 6 8 10]))
248 (should (equal (seq-concatenate 'vector nil '(8 10)) [8 10]))
249 (should (equal (seq-concatenate 'vector seq nil) [2 4 6]))))
251 (ert-deftest test-seq-mapcat ()
252 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
254 (should (equal (seq-mapcat #'seq-reverse '[(3 2 1) (6 5 4)])
256 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)) 'vector)
259 (ert-deftest test-seq-partition ()
260 (should (same-contents-p (seq-partition '(0 1 2 3 4 5 6 7) 3)
261 '((0 1 2) (3 4 5) (6 7))))
262 (should (same-contents-p (seq-partition '[0 1 2 3 4 5 6 7] 3)
263 '([0 1 2] [3 4 5] [6 7])))
264 (should (same-contents-p (seq-partition "Hello world" 2)
265 '("He" "ll" "o " "wo" "rl" "d")))
266 (should (equal (seq-partition '() 2) '()))
267 (should (equal (seq-partition '(1 2 3) -1) '())))
269 (ert-deftest test-seq-group-by ()
270 (with-test-sequences (seq '(1 2 3 4))
271 (should (equal (seq-group-by #'test-sequences-oddp seq)
272 '((t 1 3) (nil 2 4)))))
273 (should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2)))
274 '((b (b 3)) (c (c 4)) (a (a 1) (a 2))))))
276 (ert-deftest test-seq-reverse ()
277 (with-test-sequences (seq '(1 2 3 4))
278 (should (same-contents-p (seq-reverse seq) '(4 3 2 1)))
279 (should (equal (type-of (seq-reverse seq))
282 (ert-deftest test-seq-into ()
283 (let* ((vector [1 2 3])
284 (list (seq-into vector 'list)))
285 (should (same-contents-p vector list))
286 (should (listp list)))
287 (let* ((list '(hello world))
288 (vector (seq-into list 'vector)))
289 (should (same-contents-p vector list))
290 (should (vectorp vector)))
291 (let* ((string "hello")
292 (list (seq-into string 'list)))
293 (should (same-contents-p string list))
294 (should (stringp string)))
295 (let* ((string "hello")
296 (vector (seq-into string 'vector)))
297 (should (same-contents-p string vector))
298 (should (stringp string)))
300 (vector (seq-into list 'vector)))
301 (should (same-contents-p list vector))
302 (should (vectorp vector))))
304 (ert-deftest test-seq-intersection ()
307 (should (same-contents-p (seq-intersection v1 v2)
309 (let ((l1 '(2 3 4 5))
311 (should (same-contents-p (seq-intersection l1 l2)
315 (should (seq-empty-p (seq-intersection v1 v2)))))
317 (ert-deftest test-seq-difference ()
320 (should (same-contents-p (seq-difference v1 v2)
322 (let ((l1 '(2 3 4 5))
324 (should (same-contents-p (seq-difference l1 l2)
328 (should (seq-empty-p (seq-difference v1 v2)))))
330 (ert-deftest test-seq-let ()
331 (with-test-sequences (seq '(1 2 3 4))
332 (seq-let (a b c d e) seq
338 (seq-let (a b &rest others) seq
341 (should (same-contents-p others (seq-drop seq 2)))))
342 (let ((seq '(1 (2 (3 (4))))))
343 (seq-let (_ (_ (_ (a)))) seq
351 (ert-deftest test-seq-min-max ()
352 (with-test-sequences (seq '(4 5 3 2 0 4))
353 (should (= (seq-min seq) 0))
354 (should (= (seq-max seq) 5))))
356 (ert-deftest test-seq-into-sequence ()
357 (with-test-sequences (seq '(1 2 3))
358 (should (eq seq (seq-into-sequence seq)))
359 (should-error (seq-into-sequence 2))))
361 (ert-deftest test-seq-position ()
362 (with-test-sequences (seq '(2 4 6))
363 (should (null (seq-position seq 1)))
364 (should (= (seq-position seq 4) 1)))
365 (let ((seq '(a b c)))
366 (should (null (seq-position seq 'd #'eq)))
367 (should (= (seq-position seq 'a #'eq) 0))
368 (should (null (seq-position seq (make-symbol "a") #'eq)))))
370 (ert-deftest test-seq-sort-by ()
371 (let ((seq ["x" "xx" "xxx"]))
372 (should (equal (seq-sort-by #'seq-length #'> seq)
375 (ert-deftest test-seq-random-elt-take-all ()
376 (let ((seq '(a b c d e))
378 (should (= 0 (map-length count)))
380 (let ((random-elt (seq-random-elt seq)))
383 (map-elt count random-elt 0))))
384 (should (= 5 (map-length count)))))
386 (ert-deftest test-seq-random-elt-signal-on-empty ()
387 (should-error (seq-random-elt nil))
388 (should-error (seq-random-elt []))
389 (should-error (seq-random-elt "")))
392 ;;; seq-tests.el ends here