Type checking for `define-widget'
[emacs.git] / test / automated / cl-lib-tests.el
blob1c36e7d7abff729050888797854a5d7e2198eaf6
1 ;;; cl-lib.el --- tests for emacs-lisp/cl-lib.el
3 ;; Copyright (C) 2013-2015 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/'.
20 ;;; Commentary:
22 ;; Extracted from ert-tests.el, back when ert used to reimplement some
23 ;; cl functions.
25 ;;; Code:
27 (require 'cl-lib)
28 (require 'ert)
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.
34 (cl-remprop x 'b)
35 (should (equal (symbol-plist x) '()))
36 (put x 'a 1)
37 (should (equal (symbol-plist x) '(a 1)))
38 ;; Remove nonexistent property on nonempty plist.
39 (cl-remprop x 'b)
40 (should (equal (symbol-plist x) '(a 1)))
41 (put x 'b 2)
42 (put x 'c 3)
43 (put x 'd 4)
44 (should (equal (symbol-plist x) '(a 1 b 2 c 3 d 4)))
45 ;; Remove property that is neither first nor last.
46 (cl-remprop x 'c)
47 (should (equal (symbol-plist x) '(a 1 b 2 d 4)))
48 ;; Remove last property from a plist of length >1.
49 (cl-remprop x 'd)
50 (should (equal (symbol-plist x) '(a 1 b 2)))
51 ;; Remove first property from a plist of length >1.
52 (cl-remprop x 'a)
53 (should (equal (symbol-plist x) '(b 2)))
54 ;; Remove property when there is only one.
55 (cl-remprop x 'b)
56 (should (equal (symbol-plist x) '()))))
58 (ert-deftest cl-lib-test-remove-if-not ()
59 (let ((list (list 'a 'b 'c 'd))
60 (i 0))
61 (let ((result (cl-remove-if-not (lambda (x)
62 (should (eql x (nth i list)))
63 (cl-incf i)
64 (member i '(2 3)))
65 list)))
66 (should (equal i 4))
67 (should (equal result '(b c)))
68 (should (equal list '(a b c d)))))
69 (should (equal '()
70 (cl-remove-if-not (lambda (_x) (should nil)) '()))))
72 (ert-deftest cl-lib-test-remove ()
73 (let ((list (list 'a 'b 'c 'd))
74 (key-index 0)
75 (test-index 0))
76 (let ((result
77 (cl-remove 'foo list
78 :key (lambda (x)
79 (should (eql x (nth key-index list)))
80 (prog1
81 (list key-index x)
82 (cl-incf key-index)))
83 :test
84 (lambda (a b)
85 (should (eql a 'foo))
86 (should (equal b (list test-index
87 (nth test-index list))))
88 (cl-incf test-index)
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))
95 (y (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'
101 (list y)))))
104 (ert-deftest cl-lib-test-set-functions ()
105 (let ((c1 (cons nil nil))
106 (c2 (cons nil nil))
107 (sym (make-symbol "a")))
108 (let ((e '())
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 c2 "x" "" nil 'a)))
121 (should (equal (cl-set-difference b a) (list 'x 'y)))
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 c2 "x" "" nil 'a)))
132 (should (equal (cl-set-difference b a :test 'eq) (list 'x 'y)))
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
162 '(lambda ()
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))
170 (funcall body))))
172 (ert-deftest cl-lib-test-coerce-to-vector ()
173 (let* ((a (vector))
174 (b (vector 1 a 3))
175 (c (list))
176 (d (list b a)))
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 ()
202 (should (equal
203 (funcall (cl-function (lambda (&key a b) (list a b)))
204 :b :a :a 42)
205 '(42 :a))))
207 (cl-defstruct mystruct (abc :readonly t) def)
208 (ert-deftest cl-lib-struct-accessors ()
209 (let ((x (make-mystruct :abc 1 :def 2)))
210 (should (eql (cl-struct-slot-value 'mystruct 'abc x) 1))
211 (should (eql (cl-struct-slot-value 'mystruct 'def x) 2))
212 (setf (cl-struct-slot-value 'mystruct 'def x) -1)
213 (should (eql (cl-struct-slot-value 'mystruct 'def x) -1))
214 (should (eql (cl-struct-slot-offset 'mystruct 'abc) 1))
215 (should-error (cl-struct-slot-offset 'mystruct 'marypoppins))
216 (should (equal (cl-struct-slot-info 'mystruct)
217 '((cl-tag-slot) (abc :readonly t) (def))))))
219 (ert-deftest cl-the ()
220 (should (eql (cl-the integer 42) 42))
221 (should-error (cl-the integer "abc"))
222 (let ((side-effect 0))
223 (should (= (cl-the integer (cl-incf side-effect)) 1))
224 (should (= side-effect 1))))
226 (ert-deftest cl-lib-test-plusp ()
227 (should-not (cl-plusp -1.0e+INF))
228 (should-not (cl-plusp -1.5e2))
229 (should-not (cl-plusp -3.14))
230 (should-not (cl-plusp -1))
231 (should-not (cl-plusp -0.0))
232 (should-not (cl-plusp 0))
233 (should-not (cl-plusp 0.0))
234 (should-not (cl-plusp -0.0e+NaN))
235 (should-not (cl-plusp 0.0e+NaN))
236 (should (cl-plusp 1))
237 (should (cl-plusp 3.14))
238 (should (cl-plusp 1.5e2))
239 (should (cl-plusp 1.0e+INF))
240 (should-error (cl-plusp "42") :type 'wrong-type-argument))
242 (ert-deftest cl-lib-test-minusp ()
243 (should (cl-minusp -1.0e+INF))
244 (should (cl-minusp -1.5e2))
245 (should (cl-minusp -3.14))
246 (should (cl-minusp -1))
247 (should-not (cl-minusp -0.0))
248 (should-not (cl-minusp 0))
249 (should-not (cl-minusp 0.0))
250 (should-not (cl-minusp -0.0e+NaN))
251 (should-not (cl-minusp 0.0e+NaN))
252 (should-not (cl-minusp 1))
253 (should-not (cl-minusp 3.14))
254 (should-not (cl-minusp 1.5e2))
255 (should-not (cl-minusp 1.0e+INF))
256 (should-error (cl-minusp "-42") :type 'wrong-type-argument))
258 (ert-deftest cl-lib-test-oddp ()
259 (should (cl-oddp -3))
260 (should (cl-oddp 3))
261 (should-not (cl-oddp -2))
262 (should-not (cl-oddp 0))
263 (should-not (cl-oddp 2))
264 (should-error (cl-oddp 3.0e+NaN) :type 'wrong-type-argument)
265 (should-error (cl-oddp 3.0) :type 'wrong-type-argument)
266 (should-error (cl-oddp "3") :type 'wrong-type-argument))
268 (ert-deftest cl-lib-test-evenp ()
269 (should (cl-evenp -2))
270 (should (cl-evenp 0))
271 (should (cl-evenp 2))
272 (should-not (cl-evenp -3))
273 (should-not (cl-evenp 3))
274 (should-error (cl-evenp 2.0e+NaN) :type 'wrong-type-argument)
275 (should-error (cl-evenp 2.0) :type 'wrong-type-argument)
276 (should-error (cl-evenp "2") :type 'wrong-type-argument))
278 (ert-deftest cl-digit-char-p ()
279 (should (eql 3 (cl-digit-char-p ?3)))
280 (should (eql 10 (cl-digit-char-p ?a 11)))
281 (should (eql 10 (cl-digit-char-p ?A 11)))
282 (should-not (cl-digit-char-p ?a))
283 (should (eql 32 (cl-digit-char-p ?w 36)))
284 (should-error (cl-digit-char-p ?a 37) :type 'args-out-of-range)
285 (should-error (cl-digit-char-p ?a 1) :type 'args-out-of-range))
287 (ert-deftest cl-lib-test-first ()
288 (should (null (cl-first '())))
289 (should (= 4 (cl-first '(4))))
290 (should (= 4 (cl-first '(4 2))))
291 (should-error (cl-first "42") :type 'wrong-type-argument))
293 (ert-deftest cl-lib-test-second ()
294 (should (null (cl-second '())))
295 (should (null (cl-second '(4))))
296 (should (= 2 (cl-second '(1 2))))
297 (should (= 2 (cl-second '(1 2 3))))
298 (should-error (cl-second "1 2 3") :type 'wrong-type-argument))
300 (ert-deftest cl-lib-test-third ()
301 (should (null (cl-third '())))
302 (should (null (cl-third '(1 2))))
303 (should (= 3 (cl-third '(1 2 3))))
304 (should (= 3 (cl-third '(1 2 3 4))))
305 (should-error (cl-third "123") :type 'wrong-type-argument))
307 (ert-deftest cl-lib-test-fourth ()
308 (should (null (cl-fourth '())))
309 (should (null (cl-fourth '(1 2 3))))
310 (should (= 4 (cl-fourth '(1 2 3 4))))
311 (should (= 4 (cl-fourth '(1 2 3 4 5))))
312 (should-error (cl-fourth "1234") :type 'wrong-type-argument))
314 (ert-deftest cl-lib-test-fifth ()
315 (should (null (cl-fifth '())))
316 (should (null (cl-fifth '(1 2 3 4))))
317 (should (= 5 (cl-fifth '(1 2 3 4 5))))
318 (should (= 5 (cl-fifth '(1 2 3 4 5 6))))
319 (should-error (cl-fifth "12345") :type 'wrong-type-argument))
321 (ert-deftest cl-lib-test-fifth ()
322 (should (null (cl-fifth '())))
323 (should (null (cl-fifth '(1 2 3 4))))
324 (should (= 5 (cl-fifth '(1 2 3 4 5))))
325 (should (= 5 (cl-fifth '(1 2 3 4 5 6))))
326 (should-error (cl-fifth "12345") :type 'wrong-type-argument))
328 (ert-deftest cl-lib-test-sixth ()
329 (should (null (cl-sixth '())))
330 (should (null (cl-sixth '(1 2 3 4 5))))
331 (should (= 6 (cl-sixth '(1 2 3 4 5 6))))
332 (should (= 6 (cl-sixth '(1 2 3 4 5 6 7))))
333 (should-error (cl-sixth "123456") :type 'wrong-type-argument))
335 (ert-deftest cl-lib-test-seventh ()
336 (should (null (cl-seventh '())))
337 (should (null (cl-seventh '(1 2 3 4 5 6))))
338 (should (= 7 (cl-seventh '(1 2 3 4 5 6 7))))
339 (should (= 7 (cl-seventh '(1 2 3 4 5 6 7 8))))
340 (should-error (cl-seventh "1234567") :type 'wrong-type-argument))
342 (ert-deftest cl-lib-test-eighth ()
343 (should (null (cl-eighth '())))
344 (should (null (cl-eighth '(1 2 3 4 5 6 7))))
345 (should (= 8 (cl-eighth '(1 2 3 4 5 6 7 8))))
346 (should (= 8 (cl-eighth '(1 2 3 4 5 6 7 8 9))))
347 (should-error (cl-eighth "12345678") :type 'wrong-type-argument))
349 (ert-deftest cl-lib-test-ninth ()
350 (should (null (cl-ninth '())))
351 (should (null (cl-ninth '(1 2 3 4 5 6 7 8))))
352 (should (= 9 (cl-ninth '(1 2 3 4 5 6 7 8 9))))
353 (should (= 9 (cl-ninth '(1 2 3 4 5 6 7 8 9 10))))
354 (should-error (cl-ninth "123456789") :type 'wrong-type-argument))
356 (ert-deftest cl-lib-test-tenth ()
357 (should (null (cl-tenth '())))
358 (should (null (cl-tenth '(1 2 3 4 5 6 7 8 9))))
359 (should (= 10 (cl-tenth '(1 2 3 4 5 6 7 8 9 10))))
360 (should (= 10 (cl-tenth '(1 2 3 4 5 6 7 8 9 10 11))))
361 (should-error (cl-tenth "1234567890") :type 'wrong-type-argument))
363 (ert-deftest cl-lib-test-endp ()
364 (should (cl-endp '()))
365 (should-not (cl-endp '(1)))
366 (should-error (cl-endp 1) :type 'wrong-type-argument)
367 (should-error (cl-endp [1]) :type 'wrong-type-argument))
369 (ert-deftest cl-lib-test-nth-value ()
370 (let ((vals (cl-values 2 3)))
371 (should (= (cl-nth-value 0 vals) 2))
372 (should (= (cl-nth-value 1 vals) 3))
373 (should (null (cl-nth-value 2 vals)))
374 (should-error (cl-nth-value 0.0 vals) :type 'wrong-type-argument)))
376 (ert-deftest cl-lib-nth-value-test-multiple-values ()
377 "While CL multiple values are an alias to list, these won't work."
378 :expected-result :failed
379 (should (eq (cl-nth-value 0 '(2 3)) '(2 3)))
380 (should (= (cl-nth-value 0 1) 1))
381 (should (null (cl-nth-value 1 1)))
382 (should-error (cl-nth-value -1 (cl-values 2 3)) :type 'args-out-of-range)
383 (should (string= (cl-nth-value 0 "only lists") "only lists")))
385 (ert-deftest cl-test-caaar ()
386 (should (null (cl-caaar '())))
387 (should (null (cl-caaar '(() (2)))))
388 (should (null (cl-caaar '((() (2)) (a b)))))
389 (should-error (cl-caaar '(1 2)) :type 'wrong-type-argument)
390 (should-error (cl-caaar '((1 2))) :type 'wrong-type-argument)
391 (should (= 1 (cl-caaar '(((1 2) (3 4))))))
392 (should (null (cl-caaar '((() (3 4)))))))
394 (ert-deftest cl-test-caadr ()
395 (should (null (cl-caadr '())))
396 (should (null (cl-caadr '(1))))
397 (should-error (cl-caadr '(1 2)) :type 'wrong-type-argument)
398 (should (= 2 (cl-caadr '(1 (2 3)))))
399 (should (equal '((2) (3)) (cl-caadr '((1) (((2) (3))) (4))))))
401 (ert-deftest cl-test-ldiff ()
402 (let ((l '(1 2 3)))
403 (should (null (cl-ldiff '() '())))
404 (should (null (cl-ldiff '() l)))
405 (should (null (cl-ldiff l l)))
406 (should (equal l (cl-ldiff l '())))
407 ;; must be part of the list
408 (should (equal l (cl-ldiff l '(2 3))))
409 (should (equal '(1) (cl-ldiff l (nthcdr 1 l))))
410 ;; should return a copy
411 (should-not (eq (cl-ldiff l '()) l))))
413 (ert-deftest cl-parse-integer ()
414 (should-error (cl-parse-integer "abc"))
415 (should (null (cl-parse-integer "abc" :junk-allowed t)))
416 (should (null (cl-parse-integer "" :junk-allowed t)))
417 (should (= 342391 (cl-parse-integer "0123456789" :radix 8 :junk-allowed t)))
418 (should-error (cl-parse-integer "0123456789" :radix 8))
419 (should (= -239 (cl-parse-integer "-efz" :radix 16 :junk-allowed t)))
420 (should-error (cl-parse-integer "efz" :radix 16))
421 (should (= 239 (cl-parse-integer "zzef" :radix 16 :start 2)))
422 (should (= -123 (cl-parse-integer " -123 "))))
424 (ert-deftest cl-loop-destructuring-with ()
425 (should (equal (cl-loop with (a b c) = '(1 2 3) return (+ a b c)) 6)))
427 (ert-deftest cl-flet-test ()
428 (should (equal (cl-flet ((f1 (x) x)) (let ((x #'f1)) (funcall x 5))) 5)))
430 ;;; cl-lib.el ends here