]> code.delx.au - gnu-emacs/blob - test/lisp/emacs-lisp/seq-tests.el
Merge from origin/emacs-25
[gnu-emacs] / test / lisp / emacs-lisp / seq-tests.el
1 ;;; seq-tests.el --- Tests for sequences.el
2
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
4
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Maintainer: emacs-devel@gnu.org
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; Tests for sequences.el
26
27 ;;; Code:
28
29 (require 'ert)
30 (require 'seq)
31
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.
35
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)))))
44
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 '())))
48
49 (defun test-sequences-evenp (integer)
50 "Return t if INTEGER is even."
51 (eq (logand integer 1) 0))
52
53 (defun test-sequences-oddp (integer)
54 "Return t if INTEGER is odd."
55 (not (test-sequences-evenp integer)))
56
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)))))
61
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)))))
72
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))))
80
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)))))
90
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)))))
99
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)))))
109
110 (ert-deftest test-seq-filter ()
111 (with-test-sequences (seq '(6 7 8 9 10))
112 (should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10)))
113 (should (equal (seq-filter #'test-sequences-oddp seq) '(7 9)))
114 (should (equal (seq-filter (lambda (elt) nil) seq) '())))
115 (with-test-sequences (seq '())
116 (should (equal (seq-filter #'test-sequences-evenp seq) '()))))
117
118 (ert-deftest test-seq-remove ()
119 (with-test-sequences (seq '(6 7 8 9 10))
120 (should (equal (seq-remove #'test-sequences-evenp seq) '(7 9)))
121 (should (equal (seq-remove #'test-sequences-oddp seq) '(6 8 10)))
122 (should (same-contents-p (seq-remove (lambda (elt) nil) seq) seq)))
123 (with-test-sequences (seq '())
124 (should (equal (seq-remove #'test-sequences-evenp seq) '()))))
125
126 (ert-deftest test-seq-count ()
127 (with-test-sequences (seq '(6 7 8 9 10))
128 (should (equal (seq-count #'test-sequences-evenp seq) 3))
129 (should (equal (seq-count #'test-sequences-oddp seq) 2))
130 (should (equal (seq-count (lambda (elt) nil) seq) 0)))
131 (with-test-sequences (seq '())
132 (should (equal (seq-count #'test-sequences-evenp seq) 0))))
133
134 (ert-deftest test-seq-reduce ()
135 (with-test-sequences (seq '(1 2 3 4))
136 (should (= (seq-reduce #'+ seq 0) 10))
137 (should (= (seq-reduce #'+ seq 5) 15)))
138 (with-test-sequences (seq '())
139 (should (eq (seq-reduce #'+ seq 0) 0))
140 (should (eq (seq-reduce #'+ seq 7) 7))))
141
142 (ert-deftest test-seq-some ()
143 (with-test-sequences (seq '(4 3 2 1))
144 (should (seq-some #'test-sequences-evenp seq))
145 (should (seq-some #'test-sequences-oddp seq))
146 (should-not (seq-some (lambda (elt) (> elt 10)) seq)))
147 (with-test-sequences (seq '())
148 (should-not (seq-some #'test-sequences-oddp seq)))
149 (should (seq-some #'null '(1 nil 2))))
150
151 (ert-deftest test-seq-find ()
152 (with-test-sequences (seq '(4 3 2 1))
153 (should (= 4 (seq-find #'test-sequences-evenp seq)))
154 (should (= 3 (seq-find #'test-sequences-oddp seq)))
155 (should-not (seq-find (lambda (elt) (> elt 10)) seq)))
156 (should-not (seq-find #'null '(1 nil 2)))
157 (should-not (seq-find #'null '(1 nil 2) t))
158 (should-not (seq-find #'null '(1 2 3)))
159 (should (seq-find #'null '(1 2 3) 'sentinel)))
160
161 (ert-deftest test-seq-contains ()
162 (with-test-sequences (seq '(3 4 5 6))
163 (should (seq-contains seq 3))
164 (should-not (seq-contains seq 7)))
165 (with-test-sequences (seq '())
166 (should-not (seq-contains seq 3))
167 (should-not (seq-contains seq nil))))
168
169 (ert-deftest test-seq-every-p ()
170 (with-test-sequences (seq '(43 54 22 1))
171 (should (seq-every-p (lambda (elt) t) seq))
172 (should-not (seq-every-p #'test-sequences-oddp seq))
173 (should-not (seq-every-p #'test-sequences-evenp seq)))
174 (with-test-sequences (seq '(42 54 22 2))
175 (should (seq-every-p #'test-sequences-evenp seq))
176 (should-not (seq-every-p #'test-sequences-oddp seq)))
177 (with-test-sequences (seq '())
178 (should (seq-every-p #'identity seq))
179 (should (seq-every-p #'test-sequences-evenp seq))))
180
181 (ert-deftest test-seq-empty-p ()
182 (with-test-sequences (seq '(0))
183 (should-not (seq-empty-p seq)))
184 (with-test-sequences (seq '(0 1 2))
185 (should-not (seq-empty-p seq)))
186 (with-test-sequences (seq '())
187 (should (seq-empty-p seq))))
188
189 (ert-deftest test-seq-sort ()
190 (should (equal (seq-sort #'< "cbaf") "abcf"))
191 (should (equal (seq-sort #'< '(2 1 9 4)) '(1 2 4 9)))
192 (should (equal (seq-sort #'< [2 1 9 4]) [1 2 4 9]))
193 (should (equal (seq-sort #'< "") "")))
194
195 (ert-deftest test-seq-uniq ()
196 (with-test-sequences (seq '(2 4 6 8 6 4 3))
197 (should (equal (seq-uniq seq) '(2 4 6 8 3))))
198 (with-test-sequences (seq '(3 3 3 3 3))
199 (should (equal (seq-uniq seq) '(3))))
200 (with-test-sequences (seq '())
201 (should (equal (seq-uniq seq) '()))))
202
203 (ert-deftest test-seq-subseq ()
204 (with-test-sequences (seq '(2 3 4 5))
205 (should (equal (seq-subseq seq 0 4) seq))
206 (should (same-contents-p (seq-subseq seq 2 4) '(4 5)))
207 (should (same-contents-p (seq-subseq seq 1 3) '(3 4)))
208 (should (same-contents-p (seq-subseq seq 1 -1) '(3 4))))
209 (should (vectorp (seq-subseq [2 3 4 5] 2)))
210 (should (stringp (seq-subseq "foo" 2 3)))
211 (should (listp (seq-subseq '(2 3 4 4) 2 3)))
212 (should-error (seq-subseq '(1 2 3) 4))
213 (should-not (seq-subseq '(1 2 3) 3))
214 (should (seq-subseq '(1 2 3) -3))
215 (should-error (seq-subseq '(1 2 3) 1 4))
216 (should (seq-subseq '(1 2 3) 1 3))
217 (should-error (seq-subseq '() -1))
218 (should-error (seq-subseq [] -1))
219 (should-error (seq-subseq "" -1))
220 (should-not (seq-subseq '() 0))
221 (should-error (seq-subseq '() 0 -1)))
222
223 (ert-deftest test-seq-concatenate ()
224 (with-test-sequences (seq '(2 4 6))
225 (should (equal (seq-concatenate 'string seq [8]) (string 2 4 6 8)))
226 (should (equal (seq-concatenate 'list seq '(8 10)) '(2 4 6 8 10)))
227 (should (equal (seq-concatenate 'vector seq '(8 10)) [2 4 6 8 10]))
228 (should (equal (seq-concatenate 'vector nil '(8 10)) [8 10]))
229 (should (equal (seq-concatenate 'vector seq nil) [2 4 6]))))
230
231 (ert-deftest test-seq-mapcat ()
232 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
233 '(1 2 3 4 5 6)))
234 (should (equal (seq-mapcat #'seq-reverse '[(3 2 1) (6 5 4)])
235 '(1 2 3 4 5 6)))
236 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)) 'vector)
237 '[1 2 3 4 5 6])))
238
239 (ert-deftest test-seq-partition ()
240 (should (same-contents-p (seq-partition '(0 1 2 3 4 5 6 7) 3)
241 '((0 1 2) (3 4 5) (6 7))))
242 (should (same-contents-p (seq-partition '[0 1 2 3 4 5 6 7] 3)
243 '([0 1 2] [3 4 5] [6 7])))
244 (should (same-contents-p (seq-partition "Hello world" 2)
245 '("He" "ll" "o " "wo" "rl" "d")))
246 (should (equal (seq-partition '() 2) '()))
247 (should (equal (seq-partition '(1 2 3) -1) '())))
248
249 (ert-deftest test-seq-group-by ()
250 (with-test-sequences (seq '(1 2 3 4))
251 (should (equal (seq-group-by #'test-sequences-oddp seq)
252 '((t 1 3) (nil 2 4)))))
253 (should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2)))
254 '((b (b 3)) (c (c 4)) (a (a 1) (a 2))))))
255
256 (ert-deftest test-seq-reverse ()
257 (with-test-sequences (seq '(1 2 3 4))
258 (should (same-contents-p (seq-reverse seq) '(4 3 2 1)))
259 (should (equal (type-of (seq-reverse seq))
260 (type-of seq)))))
261
262 (ert-deftest test-seq-into ()
263 (let* ((vector [1 2 3])
264 (list (seq-into vector 'list)))
265 (should (same-contents-p vector list))
266 (should (listp list)))
267 (let* ((list '(hello world))
268 (vector (seq-into list 'vector)))
269 (should (same-contents-p vector list))
270 (should (vectorp vector)))
271 (let* ((string "hello")
272 (list (seq-into string 'list)))
273 (should (same-contents-p string list))
274 (should (stringp string)))
275 (let* ((string "hello")
276 (vector (seq-into string 'vector)))
277 (should (same-contents-p string vector))
278 (should (stringp string)))
279 (let* ((list nil)
280 (vector (seq-into list 'vector)))
281 (should (same-contents-p list vector))
282 (should (vectorp vector))))
283
284 (ert-deftest test-seq-intersection ()
285 (let ((v1 [2 3 4 5])
286 (v2 [1 3 5 6 7]))
287 (should (same-contents-p (seq-intersection v1 v2)
288 '(3 5))))
289 (let ((l1 '(2 3 4 5))
290 (l2 '(1 3 5 6 7)))
291 (should (same-contents-p (seq-intersection l1 l2)
292 '(3 5))))
293 (let ((v1 [2 4 6])
294 (v2 [1 3 5]))
295 (should (seq-empty-p (seq-intersection v1 v2)))))
296
297 (ert-deftest test-seq-difference ()
298 (let ((v1 [2 3 4 5])
299 (v2 [1 3 5 6 7]))
300 (should (same-contents-p (seq-difference v1 v2)
301 '(2 4))))
302 (let ((l1 '(2 3 4 5))
303 (l2 '(1 3 5 6 7)))
304 (should (same-contents-p (seq-difference l1 l2)
305 '(2 4))))
306 (let ((v1 [2 4 6])
307 (v2 [2 4 6]))
308 (should (seq-empty-p (seq-difference v1 v2)))))
309
310 (ert-deftest test-seq-let ()
311 (with-test-sequences (seq '(1 2 3 4))
312 (seq-let (a b c d e) seq
313 (should (= a 1))
314 (should (= b 2))
315 (should (= c 3))
316 (should (= d 4))
317 (should (null e)))
318 (seq-let (a b &rest others) seq
319 (should (= a 1))
320 (should (= b 2))
321 (should (same-contents-p others (seq-drop seq 2)))))
322 (let ((seq '(1 (2 (3 (4))))))
323 (seq-let (_ (_ (_ (a)))) seq
324 (should (= a 4))))
325 (let (seq)
326 (seq-let (a b c) seq
327 (should (null a))
328 (should (null b))
329 (should (null c)))))
330
331 (ert-deftest test-seq-min-max ()
332 (with-test-sequences (seq '(4 5 3 2 0 4))
333 (should (= (seq-min seq) 0))
334 (should (= (seq-max seq) 5))))
335
336 (ert-deftest test-seq-into-sequence ()
337 (with-test-sequences (seq '(1 2 3))
338 (should (eq seq (seq-into-sequence seq)))
339 (should-error (seq-into-sequence 2))))
340
341 (ert-deftest test-seq-position ()
342 (with-test-sequences (seq '(2 4 6))
343 (should (null (seq-position seq 1)))
344 (should (= (seq-position seq 4) 1)))
345 (let ((seq '(a b c)))
346 (should (null (seq-position seq 'd #'eq)))
347 (should (= (seq-position seq 'a #'eq) 0))
348 (should (null (seq-position seq (make-symbol "a") #'eq)))))
349
350 (ert-deftest test-seq-sort-by ()
351 (let ((seq ["x" "xx" "xxx"]))
352 (should (equal (seq-sort-by #'seq-length #'> seq)
353 ["xxx" "xx" "x"]))))
354
355 (provide 'seq-tests)
356 ;;; seq-tests.el ends here