1 ;;; cl-lib.el --- tests for emacs-lisp/cl-lib.el -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
22 ;; Extracted from ert-tests.el, back when ert used to reimplement some
30 (ert-deftest cl-lib-test-remprop
()
31 (let ((x (cl-gensym)))
32 (should (equal (symbol-plist x
) '()))
33 ;; Remove nonexistent property on empty plist.
35 (should (equal (symbol-plist x
) '()))
37 (should (equal (symbol-plist x
) '(a 1)))
38 ;; Remove nonexistent property on nonempty plist.
40 (should (equal (symbol-plist x
) '(a 1)))
44 (should (equal (symbol-plist x
) '(a 1 b
2 c
3 d
4)))
45 ;; Remove property that is neither first nor last.
47 (should (equal (symbol-plist x
) '(a 1 b
2 d
4)))
48 ;; Remove last property from a plist of length >1.
50 (should (equal (symbol-plist x
) '(a 1 b
2)))
51 ;; Remove first property from a plist of length >1.
53 (should (equal (symbol-plist x
) '(b 2)))
54 ;; Remove property when there is only one.
56 (should (equal (symbol-plist x
) '()))))
58 (ert-deftest cl-lib-test-remove-if-not
()
59 (let ((list (list 'a
'b
'c
'd
))
61 (let ((result (cl-remove-if-not (lambda (x)
62 (should (eql x
(nth i list
)))
67 (should (equal result
'(b c
)))
68 (should (equal list
'(a b c d
)))))
70 (cl-remove-if-not (lambda (_x) (should nil
)) '()))))
72 (ert-deftest cl-lib-test-remove
()
73 (let ((list (list 'a
'b
'c
'd
))
79 (should (eql x
(nth key-index list
)))
86 (should (equal b
(list test-index
87 (nth test-index list
))))
89 (member test-index
'(2 3))))))
90 (should (equal key-index
4))
91 (should (equal test-index
4))
92 (should (equal result
'(a d
)))
93 (should (equal list
'(a b c d
)))))
94 (let ((x (cons nil nil
))
96 (should (equal (cl-remove x
(list x y
))
97 ;; or (list x), since we use `equal' -- the
98 ;; important thing is that only one element got
99 ;; removed, this proves that the default test is
100 ;; `eql', not `equal'
104 (ert-deftest cl-lib-test-set-functions
()
105 (let ((c1 (cons nil nil
))
107 (sym (make-symbol "a")))
109 (a (list 'a
'b sym nil
"" "x" c1 c2
))
110 (b (list c1
'y
'b sym
'x
)))
111 (should (equal (cl-set-difference e e
) e
))
112 (should (equal (cl-set-difference a e
) a
))
113 (should (equal (cl-set-difference e a
) e
))
114 (should (equal (cl-set-difference a a
) e
))
115 (should (equal (cl-set-difference b e
) b
))
116 (should (equal (cl-set-difference e b
) e
))
117 (should (equal (cl-set-difference b b
) e
))
118 ;; Note: this test (and others) is sensitive to the order of the
119 ;; result, which is not documented.
120 (should (equal (cl-set-difference a b
) (list 'a nil
"" "x" c2
)))
121 (should (equal (cl-set-difference b a
) (list 'y
'x
)))
123 ;; We aren't testing whether this is really using `eq' rather than `eql'.
124 (should (equal (cl-set-difference e e
:test
'eq
) e
))
125 (should (equal (cl-set-difference a e
:test
'eq
) a
))
126 (should (equal (cl-set-difference e a
:test
'eq
) e
))
127 (should (equal (cl-set-difference a a
:test
'eq
) e
))
128 (should (equal (cl-set-difference b e
:test
'eq
) b
))
129 (should (equal (cl-set-difference e b
:test
'eq
) e
))
130 (should (equal (cl-set-difference b b
:test
'eq
) e
))
131 (should (equal (cl-set-difference a b
:test
'eq
) (list 'a nil
"" "x" c2
)))
132 (should (equal (cl-set-difference b a
:test
'eq
) (list 'y
'x
)))
134 (should (equal (cl-union e e
) e
))
135 (should (equal (cl-union a e
) a
))
136 (should (equal (cl-union e a
) a
))
137 (should (equal (cl-union a a
) a
))
138 (should (equal (cl-union b e
) b
))
139 (should (equal (cl-union e b
) b
))
140 (should (equal (cl-union b b
) b
))
141 (should (equal (cl-union a b
) (list 'x
'y
'a
'b sym nil
"" "x" c1 c2
)))
143 (should (equal (cl-union b a
) (list 'x
'y
'a
'b sym nil
"" "x" c1 c2
)))
145 (should (equal (cl-intersection e e
) e
))
146 (should (equal (cl-intersection a e
) e
))
147 (should (equal (cl-intersection e a
) e
))
148 (should (equal (cl-intersection a a
) a
))
149 (should (equal (cl-intersection b e
) e
))
150 (should (equal (cl-intersection e b
) e
))
151 (should (equal (cl-intersection b b
) b
))
152 (should (equal (cl-intersection a b
) (list sym
'b c1
)))
153 (should (equal (cl-intersection b a
) (list sym
'b c1
))))))
155 (ert-deftest cl-lib-test-gensym
()
156 ;; Since the expansion of `should' calls `cl-gensym' and thus has a
157 ;; side-effect on `cl--gensym-counter', we have to make sure all
158 ;; macros in our test body are expanded before we rebind
159 ;; `cl--gensym-counter' and run the body. Otherwise, the test would
160 ;; fail if run interpreted.
161 (let ((body (byte-compile
163 (should (equal (symbol-name (cl-gensym)) "G0"))
164 (should (equal (symbol-name (cl-gensym)) "G1"))
165 (should (equal (symbol-name (cl-gensym)) "G2"))
166 (should (equal (symbol-name (cl-gensym "foo")) "foo3"))
167 (should (equal (symbol-name (cl-gensym "bar")) "bar4"))
168 (should (equal cl--gensym-counter
5))))))
169 (let ((cl--gensym-counter 0))
172 (ert-deftest cl-lib-test-coerce-to-vector
()
177 (should (eql (cl-coerce a
'vector
) a
))
178 (should (eql (cl-coerce b
'vector
) b
))
179 (should (equal (cl-coerce c
'vector
) (vector)))
180 (should (equal (cl-coerce d
'vector
) (vector b a
)))))
182 (ert-deftest cl-lib-test-string-position
()
183 (should (eql (cl-position ?x
"") nil
))
184 (should (eql (cl-position ?a
"abc") 0))
185 (should (eql (cl-position ?b
"abc") 1))
186 (should (eql (cl-position ?c
"abc") 2))
187 (should (eql (cl-position ?d
"abc") nil
))
188 (should (eql (cl-position ?A
"abc") nil
)))
190 (ert-deftest cl-lib-test-mismatch
()
191 (should (eql (cl-mismatch "" "") nil
))
192 (should (eql (cl-mismatch "" "a") 0))
193 (should (eql (cl-mismatch "a" "a") nil
))
194 (should (eql (cl-mismatch "ab" "a") 1))
195 (should (eql (cl-mismatch "Aa" "aA") 0))
196 (should (eql (cl-mismatch '(a b c
) '(a b d
)) 2)))
198 (ert-deftest cl-lib-test-loop
()
199 (should (eql (cl-loop with
(a b c
) = '(1 2 3) return
(+ a b c
)) 6)))
201 (ert-deftest cl-lib-keyword-names-versus-values
()
203 (funcall (cl-function (lambda (&key a b
) (list a b
)))
207 (cl-defstruct (mystruct
208 (:constructor cl-lib--con-1
(&aux
(abc 1)))
209 (:constructor cl-lib--con-2
(&optional def
) "Constructor docstring."))
211 (abc 5 :readonly t
) (def nil
))
212 (ert-deftest cl-lib-struct-accessors
()
213 (let ((x (make-mystruct :abc
1 :def
2)))
214 (should (eql (cl-struct-slot-value 'mystruct
'abc x
) 1))
215 (should (eql (cl-struct-slot-value 'mystruct
'def x
) 2))
216 (setf (cl-struct-slot-value 'mystruct
'def x
) -
1)
217 (should (eql (cl-struct-slot-value 'mystruct
'def x
) -
1))
218 (should (eql (cl-struct-slot-offset 'mystruct
'abc
) 1))
219 (should-error (cl-struct-slot-offset 'mystruct
'marypoppins
))
220 (should (pcase (cl-struct-slot-info 'mystruct
)
221 (`((cl-tag-slot) (abc 5 :readonly t
)
222 (def .
,(or `nil
`(nil))))
224 (ert-deftest cl-lib-struct-constructors
()
225 (should (string-match "\\`Constructor docstring."
226 (documentation 'cl-lib--con-2 t
)))
227 (should (mystruct-p (cl-lib--con-1)))
228 (should (mystruct-p (cl-lib--con-2))))
230 (ert-deftest cl-lib-arglist-performance
()
231 ;; An `&aux' should not cause lambda's arglist to be turned into an &rest
232 ;; that's parsed by hand.
233 (should (equal () (help-function-arglist 'cl-lib--con-1
)))
234 (should (pcase (help-function-arglist 'cl-lib--con-2
)
235 (`(&optional
,_
) t
))))
237 (ert-deftest cl-the
()
238 (should (eql (cl-the integer
42) 42))
239 (should-error (cl-the integer
"abc"))
240 (let ((side-effect 0))
241 (should (= (cl-the integer
(cl-incf side-effect
)) 1))
242 (should (= side-effect
1))))
244 (ert-deftest cl-lib-test-plusp
()
245 (should-not (cl-plusp -
1.0e
+INF
))
246 (should-not (cl-plusp -
1.5e2
))
247 (should-not (cl-plusp -
3.14))
248 (should-not (cl-plusp -
1))
249 (should-not (cl-plusp -
0.0))
250 (should-not (cl-plusp 0))
251 (should-not (cl-plusp 0.0))
252 (should-not (cl-plusp -
0.0e
+NaN
))
253 (should-not (cl-plusp 0.0e
+NaN
))
254 (should (cl-plusp 1))
255 (should (cl-plusp 3.14))
256 (should (cl-plusp 1.5e2
))
257 (should (cl-plusp 1.0e
+INF
))
258 (should-error (cl-plusp "42") :type
'wrong-type-argument
))
260 (ert-deftest cl-lib-test-minusp
()
261 (should (cl-minusp -
1.0e
+INF
))
262 (should (cl-minusp -
1.5e2
))
263 (should (cl-minusp -
3.14))
264 (should (cl-minusp -
1))
265 (should-not (cl-minusp -
0.0))
266 (should-not (cl-minusp 0))
267 (should-not (cl-minusp 0.0))
268 (should-not (cl-minusp -
0.0e
+NaN
))
269 (should-not (cl-minusp 0.0e
+NaN
))
270 (should-not (cl-minusp 1))
271 (should-not (cl-minusp 3.14))
272 (should-not (cl-minusp 1.5e2
))
273 (should-not (cl-minusp 1.0e
+INF
))
274 (should-error (cl-minusp "-42") :type
'wrong-type-argument
))
276 (ert-deftest cl-lib-test-oddp
()
277 (should (cl-oddp -
3))
279 (should-not (cl-oddp -
2))
280 (should-not (cl-oddp 0))
281 (should-not (cl-oddp 2))
282 (should-error (cl-oddp 3.0e
+NaN
) :type
'wrong-type-argument
)
283 (should-error (cl-oddp 3.0) :type
'wrong-type-argument
)
284 (should-error (cl-oddp "3") :type
'wrong-type-argument
))
286 (ert-deftest cl-lib-test-evenp
()
287 (should (cl-evenp -
2))
288 (should (cl-evenp 0))
289 (should (cl-evenp 2))
290 (should-not (cl-evenp -
3))
291 (should-not (cl-evenp 3))
292 (should-error (cl-evenp 2.0e
+NaN
) :type
'wrong-type-argument
)
293 (should-error (cl-evenp 2.0) :type
'wrong-type-argument
)
294 (should-error (cl-evenp "2") :type
'wrong-type-argument
))
296 (ert-deftest cl-digit-char-p
()
297 (should (eql 3 (cl-digit-char-p ?
3)))
298 (should (eql 10 (cl-digit-char-p ?a
11)))
299 (should (eql 10 (cl-digit-char-p ?A
11)))
300 (should-not (cl-digit-char-p ?a
))
301 (should (eql 32 (cl-digit-char-p ?w
36)))
302 (should-error (cl-digit-char-p ?a
37) :type
'args-out-of-range
)
303 (should-error (cl-digit-char-p ?a
1) :type
'args-out-of-range
))
305 (ert-deftest cl-lib-test-first
()
306 (should (null (cl-first '())))
307 (should (= 4 (cl-first '(4))))
308 (should (= 4 (cl-first '(4 2))))
309 (should-error (cl-first "42") :type
'wrong-type-argument
))
311 (ert-deftest cl-lib-test-second
()
312 (should (null (cl-second '())))
313 (should (null (cl-second '(4))))
314 (should (= 2 (cl-second '(1 2))))
315 (should (= 2 (cl-second '(1 2 3))))
316 (should-error (cl-second "1 2 3") :type
'wrong-type-argument
))
318 (ert-deftest cl-lib-test-third
()
319 (should (null (cl-third '())))
320 (should (null (cl-third '(1 2))))
321 (should (= 3 (cl-third '(1 2 3))))
322 (should (= 3 (cl-third '(1 2 3 4))))
323 (should-error (cl-third "123") :type
'wrong-type-argument
))
325 (ert-deftest cl-lib-test-fourth
()
326 (should (null (cl-fourth '())))
327 (should (null (cl-fourth '(1 2 3))))
328 (should (= 4 (cl-fourth '(1 2 3 4))))
329 (should (= 4 (cl-fourth '(1 2 3 4 5))))
330 (should-error (cl-fourth "1234") :type
'wrong-type-argument
))
332 (ert-deftest cl-lib-test-fifth
()
333 (should (null (cl-fifth '())))
334 (should (null (cl-fifth '(1 2 3 4))))
335 (should (= 5 (cl-fifth '(1 2 3 4 5))))
336 (should (= 5 (cl-fifth '(1 2 3 4 5 6))))
337 (should-error (cl-fifth "12345") :type
'wrong-type-argument
))
339 (ert-deftest cl-lib-test-fifth
()
340 (should (null (cl-fifth '())))
341 (should (null (cl-fifth '(1 2 3 4))))
342 (should (= 5 (cl-fifth '(1 2 3 4 5))))
343 (should (= 5 (cl-fifth '(1 2 3 4 5 6))))
344 (should-error (cl-fifth "12345") :type
'wrong-type-argument
))
346 (ert-deftest cl-lib-test-sixth
()
347 (should (null (cl-sixth '())))
348 (should (null (cl-sixth '(1 2 3 4 5))))
349 (should (= 6 (cl-sixth '(1 2 3 4 5 6))))
350 (should (= 6 (cl-sixth '(1 2 3 4 5 6 7))))
351 (should-error (cl-sixth "123456") :type
'wrong-type-argument
))
353 (ert-deftest cl-lib-test-seventh
()
354 (should (null (cl-seventh '())))
355 (should (null (cl-seventh '(1 2 3 4 5 6))))
356 (should (= 7 (cl-seventh '(1 2 3 4 5 6 7))))
357 (should (= 7 (cl-seventh '(1 2 3 4 5 6 7 8))))
358 (should-error (cl-seventh "1234567") :type
'wrong-type-argument
))
360 (ert-deftest cl-lib-test-eighth
()
361 (should (null (cl-eighth '())))
362 (should (null (cl-eighth '(1 2 3 4 5 6 7))))
363 (should (= 8 (cl-eighth '(1 2 3 4 5 6 7 8))))
364 (should (= 8 (cl-eighth '(1 2 3 4 5 6 7 8 9))))
365 (should-error (cl-eighth "12345678") :type
'wrong-type-argument
))
367 (ert-deftest cl-lib-test-ninth
()
368 (should (null (cl-ninth '())))
369 (should (null (cl-ninth '(1 2 3 4 5 6 7 8))))
370 (should (= 9 (cl-ninth '(1 2 3 4 5 6 7 8 9))))
371 (should (= 9 (cl-ninth '(1 2 3 4 5 6 7 8 9 10))))
372 (should-error (cl-ninth "123456789") :type
'wrong-type-argument
))
374 (ert-deftest cl-lib-test-tenth
()
375 (should (null (cl-tenth '())))
376 (should (null (cl-tenth '(1 2 3 4 5 6 7 8 9))))
377 (should (= 10 (cl-tenth '(1 2 3 4 5 6 7 8 9 10))))
378 (should (= 10 (cl-tenth '(1 2 3 4 5 6 7 8 9 10 11))))
379 (should-error (cl-tenth "1234567890") :type
'wrong-type-argument
))
381 (ert-deftest cl-lib-test-endp
()
382 (should (cl-endp '()))
383 (should-not (cl-endp '(1)))
384 (should-error (cl-endp 1) :type
'wrong-type-argument
)
385 (should-error (cl-endp [1]) :type 'wrong-type-argument))
387 (ert-deftest cl-lib-test-nth-value ()
388 (let ((vals (cl-values 2 3)))
389 (should (= (cl-nth-value 0 vals) 2))
390 (should (= (cl-nth-value 1 vals) 3))
391 (should (null (cl-nth-value 2 vals)))
392 (should-error (cl-nth-value 0.0 vals) :type 'wrong-type-argument)))
394 (ert-deftest cl-lib-nth-value-test-multiple-values ()
395 "While CL multiple values are an alias to list, these won't work."
396 :expected-result :failed
397 (should (eq (cl-nth-value 0 '(2 3)) '(2 3)))
398 (should (= (cl-nth-value 0 1) 1))
399 (should (null (cl-nth-value 1 1)))
400 (should-error (cl-nth-value -1 (cl-values 2 3)) :type 'args-out-of-range)
401 (should (string= (cl-nth-value 0 "only lists") "only lists")))
403 (ert-deftest cl-test-caaar ()
404 (should (null (cl-caaar '())))
405 (should (null (cl-caaar '(() (2)))))
406 (should (null (cl-caaar '((() (2)) (a b)))))
407 (should-error (cl-caaar '(1 2)) :type 'wrong-type-argument)
408 (should-error (cl-caaar '((1 2))) :type 'wrong-type-argument)
409 (should (= 1 (cl-caaar '(((1 2) (3 4))))))
410 (should (null (cl-caaar '((() (3 4)))))))
412 (ert-deftest cl-test-caadr ()
413 (should (null (cl-caadr '())))
414 (should (null (cl-caadr '(1))))
415 (should-error (cl-caadr '(1 2)) :type 'wrong-type-argument)
416 (should (= 2 (cl-caadr '(1 (2 3)))))
417 (should (equal '((2) (3)) (cl-caadr '((1) (((2) (3))) (4))))))
419 (ert-deftest cl-test-ldiff ()
421 (should (null (cl-ldiff '() '())))
422 (should (null (cl-ldiff '() l)))
423 (should (null (cl-ldiff l l)))
424 (should (equal l (cl-ldiff l '())))
425 ;; must be part of the list
426 (should (equal l (cl-ldiff l '(2 3))))
427 (should (equal '(1) (cl-ldiff l (nthcdr 1 l))))
428 ;; should return a copy
429 (should-not (eq (cl-ldiff l '()) l))))
431 (ert-deftest cl-lib-adjoin-test ()
434 ;; add non-existing item to the front
435 (should (equal '(3 1 2) (cl-adjoin 3 nums)))
436 ;; just add - don't copy rest
437 (should (eq nums (cdr (cl-adjoin 3 nums))))
438 ;; add only when not already there
439 (should (eq nums (cl-adjoin 2 nums)))
440 (should (equal '(2 1 (2)) (cl-adjoin 2 '(1 (2)))))
441 ;; default test function is eql
442 (should (equal '(1.0 1 2) (cl-adjoin 1.0 nums)))
443 ;; own :test function - returns true if match
444 (should (equal '(1.0 1 2) (cl-adjoin 1.0 nums :test nil))) ;defaults to eql
445 (should (eq nums (cl-adjoin 2 nums :test myfn-p))) ;match
446 (should (equal '(3 1 2) (cl-adjoin 3 nums :test myfn-p))) ;no match
447 ;; own :test-not function - returns false if match
448 (should (equal '(1.0 1 2) (cl-adjoin 1.0 nums :test-not nil))) ;defaults to eql
449 (should (equal '(2 2) (cl-adjoin 2 '(2) :test-not myfn-p))) ; no match
450 (should (eq nums (cl-adjoin 2 nums :test-not myfn-p))) ; 1 matches
451 (should (eq nums (cl-adjoin 3 nums :test-not myfn-p))) ; 1 and 2 matches
453 ;; according to CLtL2 passing both :test and :test-not should signal error
454 ;;(should-error (cl-adjoin 3 nums :test 'myfn-p :test-not myfn-p))
457 (should (eq nums (cl-adjoin 3 nums :key (lambda (x) (if (cl-evenp x) (1+ x) x)))))
458 (should (equal '(3 1 2) (cl-adjoin 3 nums :key (lambda (x) (if (cl-evenp x) (+ 2 x) x)))))
460 ;; convert using :key, then compare with :test
461 (should (eq nums (cl-adjoin 1 nums :key 'int-to-string :test 'string=)))
462 (should (equal '(3 1 2) (cl-adjoin 3 nums :key 'int-to-string :test 'string=)))
463 (should-error (cl-adjoin 3 nums :key 'int-to-string :test myfn-p)
464 :type 'wrong-type-argument)
466 ;; convert using :key, then compare with :test-not
467 (should (eq nums (cl-adjoin 3 nums :key 'int-to-string :test-not 'string=)))
468 (should (equal '(1 1) (cl-adjoin 1 '(1) :key 'int-to-string :test-not 'string=)))
469 (should-error (cl-adjoin 1 nums :key 'int-to-string :test-not myfn-p)
470 :type 'wrong-type-argument)))
472 (ert-deftest cl-parse-integer ()
473 (should-error (cl-parse-integer "abc"))
474 (should (null (cl-parse-integer "abc" :junk-allowed t)))
475 (should (null (cl-parse-integer "" :junk-allowed t)))
476 (should (= 342391 (cl-parse-integer "0123456789" :radix 8 :junk-allowed t)))
477 (should-error (cl-parse-integer "0123456789" :radix 8))
478 (should (= -239 (cl-parse-integer "-efz" :radix 16 :junk-allowed t)))
479 (should-error (cl-parse-integer "efz" :radix 16))
480 (should (= 239 (cl-parse-integer "zzef" :radix 16 :start 2)))
481 (should (= -123 (cl-parse-integer " -123 "))))
483 (ert-deftest cl-loop-destructuring-with ()
484 (should (equal (cl-loop with (a b c) = '(1 2 3) return (+ a b c)) 6)))
486 (ert-deftest cl-flet-test ()
487 (should (equal (cl-flet ((f1 (x) x)) (let ((x #'f1)) (funcall x 5))) 5)))
489 (ert-deftest cl-lib-test-typep ()
490 (cl-deftype cl-lib-test-type (&optional x) `(member ,x))
491 ;; Make sure we correctly implement the rule that deftype's optional args
492 ;; default to `*' rather than to nil.
493 (should (cl-typep '* 'cl-lib-test-type))
494 (should-not (cl-typep 1 'cl-lib-test-type)))
496 ;;; cl-lib.el ends here