PROPER-LIST-LENGTH and related changes
[alexandria.git] / tests.lisp
blob47297d24eee9df3449c41f5cba6c0284a9dc1636
1 (in-package :cl-user)
3 (eval-when (:compile-toplevel :load-toplevel)
4 (require :sb-rt))
6 (require :alexandria)
8 (defpackage :alexandria-test
9 (:use :cl :alexandria :sb-rt))
11 (in-package :alexandria-test)
13 ;;;; Arrays
15 (deftest copy-array.1
16 (let* ((orig (vector 1 2 3))
17 (copy (copy-array orig)))
18 (values (eq orig copy) (equalp orig copy)))
19 nil t)
21 (deftest copy-array.2
22 (let ((orig (make-array 1024 :fill-pointer 0)))
23 (vector-push-extend 1 orig)
24 (vector-push-extend 2 orig)
25 (vector-push-extend 3 orig)
26 (let ((copy (copy-array orig)))
27 (values (eq orig copy) (equalp orig copy)
28 (array-has-fill-pointer-p copy)
29 (eql (fill-pointer orig) (fill-pointer copy)))))
30 nil t t t)
32 (deftest array-index.1
33 (typep 0 'array-index)
36 ;;;; Conditions
38 (deftest unwind-protect-case.1
39 (let (result)
40 (unwind-protect-case ()
41 (random 10)
42 (:normal (push :normal result))
43 (:abort (push :abort result))
44 (:always (push :always result)))
45 result)
46 (:always :normal))
48 (deftest unwind-protect-case.2
49 (let (result)
50 (unwind-protect-case ()
51 (random 10)
52 (:always (push :always result))
53 (:normal (push :normal result))
54 (:abort (push :abort result)))
55 result)
56 (:normal :always))
58 (deftest unwind-protect-case.3
59 (let (result1 result2 result3)
60 (ignore-errors
61 (unwind-protect-case ()
62 (error "FOOF!")
63 (:normal (push :normal result1))
64 (:abort (push :abort result1))
65 (:always (push :always result1))))
66 (catch 'foof
67 (unwind-protect-case ()
68 (throw 'foof 42)
69 (:normal (push :normal result2))
70 (:abort (push :abort result2))
71 (:always (push :always result2))))
72 (block foof
73 (unwind-protect-case ()
74 (return-from foof 42)
75 (:normal (push :normal result3))
76 (:abort (push :abort result3))
77 (:always (push :always result3))))
78 (values result1 result2 result3))
79 (:always :abort)
80 (:always :abort)
81 (:always :abort))
83 (deftest unwind-protect-case.4
84 (let (result)
85 (unwind-protect-case (aborted-p)
86 (random 42)
87 (:always (setq result aborted-p)))
88 result)
89 nil)
91 (deftest unwind-protect-case.5
92 (let (result)
93 (block foof
94 (unwind-protect-case (aborted-p)
95 (return-from foof)
96 (:always (setq result aborted-p))))
97 result)
100 ;;;; Control flow
102 (deftest switch.1
103 (switch (13 :test =)
104 (12 :oops)
105 (13.0 :yay))
106 :yay)
108 (deftest switch.2
109 (switch (13)
110 ((+ 12 2) :oops)
111 ((- 13 1) :oops2)
112 (t :yay))
113 :yay)
115 (deftest eswitch.1
116 (let ((x 13))
117 (eswitch (x :test =)
118 (12 :oops)
119 (13.0 :yay)))
120 :yay)
122 (deftest eswitch.2
123 (let ((x 13))
124 (eswitch (x :key 1+)
125 (11 :oops)
126 (14 :yay)))
127 :yay)
129 (deftest cswitch.1
130 (cswitch (13 :test =)
131 (12 :oops)
132 (13.0 :yay))
133 :yay)
135 (deftest cswitch.2
136 (cswitch (13 :key 1-)
137 (12 :yay)
138 (13.0 :oops))
139 :yay)
141 (deftest whichever.1
142 (let ((x (whichever 1 2 3)))
143 (and (member x '(1 2 3)) t))
146 (deftest whichever.2
147 (let* ((a 1)
148 (b 2)
149 (c 3)
150 (x (whichever a b c)))
151 (and (member x '(1 2 3)) t))
154 (deftest xor.1
155 (xor nil nil 1 nil)
159 ;;;; Definitions
161 (deftest define-constant.1
162 (let ((name (gensym)))
163 (eval `(define-constant ,name "FOO" :test 'equal))
164 (eval `(define-constant ,name "FOO" :test 'equal))
165 (values (equal "FOO" (symbol-value name))
166 (constantp name)))
170 (deftest define-constant.2
171 (let ((name (gensym)))
172 (eval `(define-constant ,name 13))
173 (eval `(define-constant ,name 13))
174 (values (eql 13 (symbol-value name))
175 (constantp name)))
179 ;;;; Errors
181 (deftest required-argument.1
182 (multiple-value-bind (res err)
183 (ignore-errors (required-argument))
184 (typep err 'error))
187 ;;;; Hash tables
189 (deftest ensure-hash-table.1
190 (let ((table (make-hash-table))
191 (x (list 1)))
192 (multiple-value-bind (value already-there)
193 (ensure-gethash x table 42)
194 (and (= value 42)
195 (not already-there)
196 (= 42 (gethash x table))
197 (multiple-value-bind (value2 already-there2)
198 (ensure-gethash x table 13)
199 (and (= value2 42)
200 already-there2
201 (= 42 (gethash x table)))))))
204 (deftest copy-hash-table.1
205 (let ((orig (make-hash-table :test 'eq :size 123))
206 (foo "foo"))
207 (setf (gethash orig orig) t
208 (gethash foo orig) t)
209 (let ((eq-copy (copy-hash-table orig))
210 (eql-copy (copy-hash-table orig :test 'eql))
211 (equal-copy (copy-hash-table orig :test 'equal))
212 (equalp-copy (copy-hash-table orig :test 'equalp)))
213 (list (hash-table-size eq-copy)
214 (hash-table-count eql-copy)
215 (gethash orig eq-copy)
216 (gethash (copy-seq foo) eql-copy)
217 (gethash foo eql-copy)
218 (gethash (copy-seq foo) equal-copy)
219 (gethash "FOO" equal-copy)
220 (gethash "FOO" equalp-copy))))
221 (123 2 t nil t t nil t))
223 (deftest copy-hash-table.2
224 (let ((ht (make-hash-table))
225 (list (list :list (vector :A :B :C))))
226 (setf (gethash 'list ht) list)
227 (let* ((shallow-copy (copy-hash-table ht))
228 (deep1-copy (copy-hash-table ht :key 'copy-list))
229 (list (gethash 'list ht))
230 (shallow-list (gethash 'list shallow-copy))
231 (deep1-list (gethash 'list deep1-copy)))
232 (list (eq ht shallow-copy)
233 (eq ht deep1-copy)
234 (eq list shallow-list)
235 (eq list deep1-list) ; outer list was copied.
236 (eq (second list) (second shallow-list))
237 (eq (second list) (second deep1-list)) ; inner vector wasn't copied.
239 (nil nil t nil t t))
241 (deftest maphash-keys.1
242 (let ((keys nil)
243 (table (make-hash-table)))
244 (declare (notinline maphash-keys))
245 (dotimes (i 10)
246 (setf (gethash i table) t))
247 (maphash-keys (lambda (k) (push k keys)) table)
248 (set-equal keys '(0 1 2 3 4 5 6 7 8 9)))
251 (deftest maphash-values.1
252 (let ((vals nil)
253 (table (make-hash-table)))
254 (declare (notinline maphash-values))
255 (dotimes (i 10)
256 (setf (gethash i table) (- i)))
257 (maphash-values (lambda (v) (push v vals)) table)
258 (set-equal vals '(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)))
261 (deftest hash-table-keys.1
262 (let ((table (make-hash-table)))
263 (dotimes (i 10)
264 (setf (gethash i table) t))
265 (set-equal (hash-table-keys table) '(0 1 2 3 4 5 6 7 8 9)))
268 (deftest hash-table-values.1
269 (let ((table (make-hash-table)))
270 (dotimes (i 10)
271 (setf (gethash (gensym) table) i))
272 (set-equal (hash-table-values table) '(0 1 2 3 4 5 6 7 8 9)))
275 (deftest hash-table-alist.1
276 (let ((table (make-hash-table)))
277 (dotimes (i 10)
278 (setf (gethash i table) (- i)))
279 (let ((alist (hash-table-alist table)))
280 (list (length alist)
281 (assoc 0 alist)
282 (assoc 3 alist)
283 (assoc 9 alist)
284 (assoc nil alist))))
285 (10 (0 . 0) (3 . -3) (9 . -9) nil))
287 (deftest hash-table-plist.1
288 (let ((table (make-hash-table)))
289 (dotimes (i 10)
290 (setf (gethash i table) (- i)))
291 (let ((plist (hash-table-plist table)))
292 (list (length plist)
293 (getf plist 0)
294 (getf plist 2)
295 (getf plist 7)
296 (getf plist nil))))
297 (20 0 -2 -7 nil))
299 (deftest alist-hash-table.1
300 (let* ((alist '((0 a) (1 b) (2 c)))
301 (table (alist-hash-table alist)))
302 (list (hash-table-count table)
303 (gethash 0 table)
304 (gethash 1 table)
305 (gethash 2 table)
306 (hash-table-test table)))
307 (3 (a) (b) (c) eql))
309 (deftest plist-hash-table.1
310 (let* ((plist '(:a 1 :b 2 :c 3))
311 (table (plist-hash-table plist :test 'eq)))
312 (list (hash-table-count table)
313 (gethash :a table)
314 (gethash :b table)
315 (gethash :c table)
316 (gethash 2 table)
317 (gethash nil table)
318 (hash-table-test table)))
319 (3 1 2 3 nil nil eq))
321 ;;;; Functions
323 (deftest disjoin.1
324 (let ((disjunction (disjoin (lambda (x)
325 (and (consp x) :cons))
326 (lambda (x)
327 (and (stringp x) :string)))))
328 (list (funcall disjunction 'zot)
329 (funcall disjunction '(foo bar))
330 (funcall disjunction "test")))
331 (nil :cons :string))
333 (deftest conjoin.1
334 (let ((conjunction (conjoin #'consp
335 (lambda (x)
336 (stringp (car x)))
337 (lambda (x)
338 (char (car x) 0)))))
339 (list (funcall conjunction 'zot)
340 (funcall conjunction '(foo))
341 (funcall conjunction '("foo"))))
342 (nil nil #\f))
344 (deftest compose.1
345 (let ((composite (compose '1+
346 (lambda (x)
347 (* x 2))
348 #'read-from-string)))
349 (funcall composite "1"))
352 (deftest compose.2
353 (let ((composite
354 (locally (declare (notinline compose))
355 (compose '1+
356 (lambda (x)
357 (* x 2))
358 #'read-from-string))))
359 (funcall composite "2"))
362 (deftest compose.3
363 (let ((compose-form (funcall (compiler-macro-function 'compose)
364 '(compose '1+
365 (lambda (x)
366 (* x 2))
367 #'read-from-string)
368 nil)))
369 (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
370 (funcall fun "3")))
373 (deftest multiple-value-compose.1
374 (let ((composite (multiple-value-compose
375 #'truncate
376 (lambda (x y)
377 (values y x))
378 (lambda (x)
379 (with-input-from-string (s x)
380 (values (read s) (read s)))))))
381 (multiple-value-list (funcall composite "2 7")))
382 (3 1))
384 (deftest multiple-value-compose.2
385 (let ((composite (locally (declare (notinline multiple-value-compose))
386 (multiple-value-compose
387 #'truncate
388 (lambda (x y)
389 (values y x))
390 (lambda (x)
391 (with-input-from-string (s x)
392 (values (read s) (read s))))))))
393 (multiple-value-list (funcall composite "2 11")))
394 (5 1))
396 (deftest multiple-value-compose.3
397 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose)
398 '(multiple-value-compose
399 #'truncate
400 (lambda (x y)
401 (values y x))
402 (lambda (x)
403 (with-input-from-string (s x)
404 (values (read s) (read s)))))
405 nil)))
406 (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
407 (multiple-value-list (funcall fun "2 9"))))
408 (4 1))
410 (deftest curry.1
411 (let ((curried (curry '+ 3)))
412 (funcall curried 1 5))
415 (deftest curry.2
416 (let ((curried (locally (declare (notinline curry))
417 (curry '* 2 3))))
418 (funcall curried 7))
421 (deftest curry.3
422 (let ((curried-form (funcall (compiler-macro-function 'curry)
423 '(curry '/ 8)
424 nil)))
425 (let ((fun (funcall (compile nil `(lambda () ,curried-form)))))
426 (funcall fun 2)))
429 (deftest rcurry.1
430 (let ((r (rcurry '/ 2)))
431 (funcall r 8))
434 (deftest named-lambda.1
435 (let ((fac (named-lambda fac (x)
436 (if (> x 1)
437 (* x (fac (- x 1)))
438 x))))
439 (funcall fac 5))
440 120)
442 (deftest named-lambda.2
443 (let ((fac (named-lambda fac (&key x)
444 (if (> x 1)
445 (* x (fac :x (- x 1)))
446 x))))
447 (funcall fac :x 5))
448 120)
450 ;;;; Lists
452 (deftest alist-plist.1
453 (alist-plist '((a . 1) (b . 2) (c . 3)))
454 (a 1 b 2 c 3))
456 (deftest plist-alist.1
457 (plist-alist '(a 1 b 2 c 3))
458 ((a . 1) (b . 2) (c . 3)))
460 (deftest unionf.1
461 (let* ((list (list 1 2 3))
462 (orig list))
463 (unionf list (list 1 2 4))
464 (values (equal orig (list 1 2 3))
465 (eql (length list) 4)
466 (set-difference list (list 1 2 3 4))
467 (set-difference (list 1 2 3 4) list)))
471 nil)
473 (deftest nunionf.1
474 (let ((list (list 1 2 3)))
475 (nunionf list (list 1 2 4))
476 (values (eql (length list) 4)
477 (set-difference (list 1 2 3 4) list)
478 (set-difference list (list 1 2 3 4))))
481 nil)
483 (deftest appendf.1
484 (let* ((list (list 1 2 3))
485 (orig list))
486 (appendf list '(4 5 6) '(7 8))
487 (list list (eq list orig)))
488 ((1 2 3 4 5 6 7 8) nil))
490 (deftest nconcf.1
491 (let ((list1 (list 1 2 3))
492 (list2 (list 4 5 6)))
493 (nconcf list1 list2 (list 7 8 9))
494 list1)
495 (1 2 3 4 5 6 7 8 9))
497 (deftest circular-list.1
498 (let ((circle (circular-list 1 2 3)))
499 (list (first circle)
500 (second circle)
501 (third circle)
502 (fourth circle)
503 (eq circle (nthcdr 3 circle))))
504 (1 2 3 1 t))
506 (deftest circular-list-p.1
507 (let* ((circle (circular-list 1 2 3 4))
508 (tree (list circle circle))
509 (dotted (cons circle t))
510 (proper (list 1 2 3 circle))
511 (tailcirc (list* 1 2 3 circle)))
512 (list (circular-list-p circle)
513 (circular-list-p tree)
514 (circular-list-p dotted)
515 (circular-list-p proper)
516 (circular-list-p tailcirc)))
517 (t nil nil nil t))
519 (deftest circular-list-p.2
520 (circular-list-p 'foo)
521 nil)
523 (deftest circular-tree-p.1
524 (let* ((circle (circular-list 1 2 3 4))
525 (tree1 (list circle circle))
526 (tree2 (let* ((level2 (list 1 nil 2))
527 (level1 (list level2)))
528 (setf (second level2) level1)
529 level1))
530 (dotted (cons circle t))
531 (proper (list 1 2 3 circle))
532 (tailcirc (list* 1 2 3 circle))
533 (quite-proper (list 1 2 3))
534 (quite-dotted (list 1 (cons 2 3))))
535 (list (circular-tree-p circle)
536 (circular-tree-p tree1)
537 (circular-tree-p tree2)
538 (circular-tree-p dotted)
539 (circular-tree-p proper)
540 (circular-tree-p tailcirc)
541 (circular-tree-p quite-proper)
542 (circular-tree-p quite-dotted)))
543 (t t t t t t nil nil))
545 (deftest proper-list-p.1
546 (let ((l1 (list 1))
547 (l2 (list 1 2))
548 (l3 (cons 1 2))
549 (l4 (list (cons 1 2) 3))
550 (l5 (circular-list 1 2)))
551 (list (proper-list-p l1)
552 (proper-list-p l2)
553 (proper-list-p l3)
554 (proper-list-p l4)
555 (proper-list-p l5)))
556 (t t nil t nil))
558 (deftest proper-list-p.2
559 (proper-list-p '(1 2 . 3))
560 nil)
562 (deftest proper-list.type.1
563 (let ((l1 (list 1))
564 (l2 (list 1 2))
565 (l3 (cons 1 2))
566 (l4 (list (cons 1 2) 3))
567 (l5 (circular-list 1 2)))
568 (list (typep l1 'proper-list)
569 (typep l2 'proper-list)
570 (typep l3 'proper-list)
571 (typep l4 'proper-list)
572 (typep l5 'proper-list)))
573 (t t nil t nil))
575 (deftest proper-list-length.1
576 (values
577 (proper-list-length nil)
578 (proper-list-length (list 1))
579 (proper-list-length (list 2 2))
580 (proper-list-length (list 3 3 3))
581 (proper-list-length (list 4 4 4 4))
582 (proper-list-length (list 5 5 5 5 5))
583 (proper-list-length (list 6 6 6 6 6 6))
584 (proper-list-length (list 7 7 7 7 7 7 7))
585 (proper-list-length (list 8 8 8 8 8 8 8 8))
586 (proper-list-length (list 9 9 9 9 9 9 9 9 9)))
587 0 1 2 3 4 5 6 7 8 9)
589 (deftest proper-list-length.2
590 (flet ((plength (x)
591 (handler-case
592 (proper-list-length x)
593 (type-error ()
594 :ok))))
595 (values
596 (plength (list* 1))
597 (plength (list* 2 2))
598 (plength (list* 3 3 3))
599 (plength (list* 4 4 4 4))
600 (plength (list* 5 5 5 5 5))
601 (plength (list* 6 6 6 6 6 6))
602 (plength (list* 7 7 7 7 7 7 7))
603 (plength (list* 8 8 8 8 8 8 8 8))
604 (plength (list* 9 9 9 9 9 9 9 9 9))))
605 :ok :ok :ok
606 :ok :ok :ok
607 :ok :ok :ok)
609 (deftest lastcar.1
610 (let ((l1 (list 1))
611 (l2 (list 1 2)))
612 (list (lastcar l1)
613 (lastcar l2)))
614 (1 2))
616 (deftest lastcar.error.2
617 (handler-case
618 (progn
619 (lastcar (circular-list 1 2 3))
620 nil)
621 (error ()
625 (deftest setf-lastcar.1
626 (let ((l (list 1 2 3 4)))
627 (values (lastcar l)
628 (progn
629 (setf (lastcar l) 42)
630 (lastcar l))))
634 (deftest setf-lastcar.2
635 (let ((l (circular-list 1 2 3)))
636 (multiple-value-bind (res err)
637 (ignore-errors (setf (lastcar l) 4))
638 (typep err 'type-error)))
641 (deftest make-circular-list.1
642 (let ((l (make-circular-list 3 :initial-element :x)))
643 (setf (car l) :y)
644 (list (eq l (nthcdr 3 l))
645 (first l)
646 (second l)
647 (third l)
648 (fourth l)))
649 (t :y :x :x :y))
651 (deftest circular-list.type.1
652 (let* ((l1 (list 1 2 3))
653 (l2 (circular-list 1 2 3))
654 (l3 (list* 1 2 3 l2)))
655 (list (typep l1 'circular-list)
656 (typep l2 'circular-list)
657 (typep l3 'circular-list)))
658 (nil t t))
660 (deftest ensure-list.1
661 (let ((x (list 1))
662 (y 2))
663 (list (ensure-list x)
664 (ensure-list y)))
665 ((1) (2)))
667 (deftest ensure-cons.1
668 (let ((x (cons 1 2))
669 (y nil)
670 (z "foo"))
671 (values (ensure-cons x)
672 (ensure-cons y)
673 (ensure-cons z)))
674 (1 . 2)
675 (nil)
676 ("foo"))
678 (deftest setp.1
679 (setp '(1))
682 (deftest setp.2
683 (setp nil)
686 (deftest setp.3
687 (setp "foo")
688 nil)
690 (deftest setp.4
691 (setp '(1 2 3 1))
692 nil)
694 (deftest setp.5
695 (setp '(1 2 3))
698 (deftest setp.6
699 (setp '(a :a))
702 (deftest setp.7
703 (setp '(a :a) :key 'character)
704 nil)
706 (deftest setp.8
707 (setp '(a :a) :key 'character :test (constantly nil))
710 (deftest set-equal.1
711 (set-equal '(1 2 3) '(3 1 2))
714 (deftest set-equal.2
715 (set-equal '("Xa") '("Xb")
716 :test (lambda (a b) (eql (char a 0) (char b 0))))
719 (deftest set-equal.3
720 (set-equal '(1 2) '(4 2))
721 nil)
723 (deftest set-equal.4
724 (set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal)
727 (deftest set-equal.5
728 (set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal)
729 nil)
731 (deftest set-equal.6
732 (set-equal '(a b c) '(a b c d))
733 nil)
735 (deftest map-product.1
736 (map-product 'cons '(2 3) '(1 4))
737 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
739 (deftest map-product.2
740 (map-product #'cons '(2 3) '(1 4))
741 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
743 (deftest flatten.1
744 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
745 (1 2 3 4 5 6 7))
747 (deftest remove-from-plist.1
748 (let ((orig '(a 1 b 2 c 3 d 4)))
749 (list (remove-from-plist orig 'a 'c)
750 (remove-from-plist orig 'b 'd)
751 (remove-from-plist orig 'b)
752 (remove-from-plist orig 'a)
753 (remove-from-plist orig 'd 42 "zot")
754 (remove-from-plist orig 'a 'b 'c 'd)
755 (remove-from-plist orig 'a 'b 'c 'd 'x)
756 (equal orig '(a 1 b 2 c 3 d 4))))
757 ((b 2 d 4)
758 (a 1 c 3)
759 (a 1 c 3 d 4)
760 (b 2 c 3 d 4)
761 (a 1 b 2 c 3)
766 (deftest mappend.1
767 (mappend (compose 'list '*) '(1 2 3) '(1 2 3))
768 (1 4 9))
770 ;;;; Numbers
772 (deftest clamp.1
773 (list (clamp 1.5 1 2)
774 (clamp 2.0 1 2)
775 (clamp 1.0 1 2)
776 (clamp 3 1 2)
777 (clamp 0 1 2))
778 (1.5 2.0 1.0 2 1))
780 (deftest gaussian-random.1
781 (let ((min -0.2)
782 (max +0.2))
783 (multiple-value-bind (g1 g2)
784 (gaussian-random min max)
785 (values (<= min g1 max)
786 (<= min g2 max)
787 (/= g1 g2) ;uh
793 (deftest iota.1
794 (iota 3)
795 (0 1 2))
797 (deftest iota.2
798 (iota 3 :start 0.0d0)
799 (0.0d0 1.0d0 2.0d0))
801 (deftest iota.3
802 (iota 3 :start 2 :step 3.0)
803 (2.0 5.0 8.0))
805 (deftest map-iota.1
806 (let (all)
807 (declare (notinline map-iota))
808 (values (map-iota (lambda (x) (push x all))
810 :start 2
811 :step 1.1d0)
812 all))
814 (4.2d0 3.1d0 2.0d0))
816 (deftest lerp.1
817 (lerp 0.5 1 2)
818 1.5)
820 (deftest lerp.2
821 (lerp 0.1 1 2)
822 1.1)
824 (deftest mean.1
825 (mean '(1 2 3))
828 (deftest mean.2
829 (mean '(1 2 3 4))
830 5/2)
832 (deftest mean.3
833 (mean '(1 2 10))
834 13/3)
836 (deftest median.1
837 (median '(100 0 99 1 98 2 97))
840 (deftest median.2
841 (median '(100 0 99 1 98 2 97 96))
842 195/2)
844 (deftest variance.1
845 (variance (list 1 2 3))
846 2/3)
848 (deftest standard-deviation.1
849 (< 0 (standard-deviation (list 1 2 3)) 1)
852 (deftest maxf.1
853 (let ((x 1))
854 (maxf x 2)
858 (deftest maxf.2
859 (let ((x 1))
860 (maxf x 0)
864 (deftest maxf.3
865 (let ((x 1)
866 (c 0))
867 (maxf x (incf c))
868 (list x c))
869 (1 1))
871 (deftest maxf.4
872 (let ((xv (vector 0 0 0))
873 (p 0))
874 (maxf (svref xv (incf p)) (incf p))
875 (list p xv))
876 (2 #(0 2 0)))
878 (deftest minf.1
879 (let ((y 1))
880 (minf y 0)
884 (deftest minf.2
885 (let ((xv (vector 10 10 10))
886 (p 0))
887 (minf (svref xv (incf p)) (incf p))
888 (list p xv))
889 (2 #(10 2 10)))
891 ;;;; Arrays
893 #+nil
894 (deftest array-index.type)
896 #+nil
897 (deftest copy-array)
899 ;;;; Sequences
901 (deftest rotate.1
902 (list (rotate (list 1 2 3) 0)
903 (rotate (list 1 2 3) 1)
904 (rotate (list 1 2 3) 2)
905 (rotate (list 1 2 3) 3)
906 (rotate (list 1 2 3) 4))
907 ((1 2 3)
908 (3 1 2)
909 (2 3 1)
910 (1 2 3)
911 (3 1 2)))
913 (deftest rotate.2
914 (list (rotate (vector 1 2 3 4) 0)
915 (rotate (vector 1 2 3 4))
916 (rotate (vector 1 2 3 4) 2)
917 (rotate (vector 1 2 3 4) 3)
918 (rotate (vector 1 2 3 4) 4)
919 (rotate (vector 1 2 3 4) 5))
920 (#(1 2 3 4)
921 #(4 1 2 3)
922 #(3 4 1 2)
923 #(2 3 4 1)
924 #(1 2 3 4)
925 #(4 1 2 3)))
927 (deftest rotate.3
928 (list (rotate (list 1 2 3) 0)
929 (rotate (list 1 2 3) -1)
930 (rotate (list 1 2 3) -2)
931 (rotate (list 1 2 3) -3)
932 (rotate (list 1 2 3) -4))
933 ((1 2 3)
934 (2 3 1)
935 (3 1 2)
936 (1 2 3)
937 (2 3 1)))
939 (deftest rotate.4
940 (list (rotate (vector 1 2 3 4) 0)
941 (rotate (vector 1 2 3 4) -1)
942 (rotate (vector 1 2 3 4) -2)
943 (rotate (vector 1 2 3 4) -3)
944 (rotate (vector 1 2 3 4) -4)
945 (rotate (vector 1 2 3 4) -5))
946 (#(1 2 3 4)
947 #(2 3 4 1)
948 #(3 4 1 2)
949 #(4 1 2 3)
950 #(1 2 3 4)
951 #(2 3 4 1)))
953 (deftest rotate.5
954 (values (rotate (list 1) 17)
955 (rotate (list 1) -5))
957 (1))
959 (deftest shuffle.1
960 (let ((s (shuffle (iota 100))))
961 (list (equal s (iota 100))
962 (every (lambda (x)
963 (member x s))
964 (iota 100))
965 (every (lambda (x)
966 (typep x '(integer 0 99)))
967 s)))
968 (nil t t))
970 (deftest shuffle.2
971 (let ((s (shuffle (coerce (iota 100) 'vector))))
972 (list (equal s (coerce (iota 100) 'vector))
973 (every (lambda (x)
974 (find x s))
975 (iota 100))
976 (every (lambda (x)
977 (typep x '(integer 0 99)))
978 s)))
979 (nil t t))
981 (deftest random-elt.1
982 (let ((s1 #(1 2 3 4))
983 (s2 '(1 2 3 4)))
984 (list (dotimes (i 1000 nil)
985 (unless (member (random-elt s1) s2)
986 (return nil))
987 (when (/= (random-elt s1) (random-elt s1))
988 (return t)))
989 (dotimes (i 1000 nil)
990 (unless (member (random-elt s2) s2)
991 (return nil))
992 (when (/= (random-elt s2) (random-elt s2))
993 (return t)))))
994 (t t))
996 (deftest removef.1
997 (let* ((x '(1 2 3))
998 (x* x)
999 (y #(1 2 3))
1000 (y* y))
1001 (removef x 1)
1002 (removef y 3)
1003 (list x x* y y*))
1004 ((2 3)
1005 (1 2 3)
1006 #(1 2)
1007 #(1 2 3)))
1009 (deftest deletef.1
1010 (let* ((x (list 1 2 3))
1011 (x* x)
1012 (y (vector 1 2 3)))
1013 (deletef x 2)
1014 (deletef y 1)
1015 (list x x* y))
1016 ((1 3)
1017 (1 3)
1018 #(2 3)))
1020 (deftest map-permutations.1
1021 (let ((seq (list 1 2 3))
1022 (seen nil)
1023 (ok t))
1024 (map-permutations (lambda (s)
1025 (unless (set-equal s seq)
1026 (setf ok nil))
1027 (when (member s seen :test 'equal)
1028 (setf ok nil))
1029 (push s seen))
1031 :copy t)
1032 (values ok (length seen)))
1036 (deftest proper-sequence.type.1
1037 (mapcar (lambda (x)
1038 (typep x 'proper-sequence))
1039 (list (list 1 2 3)
1040 (vector 1 2 3)
1041 #2a((1 2) (3 4))
1042 (circular-list 1 2 3 4)))
1043 (t t nil nil))
1045 (deftest emptyp.1
1046 (mapcar #'emptyp
1047 (list (list 1)
1048 (circular-list 1)
1050 (vector)
1051 (vector 1)))
1052 (nil nil t t nil))
1054 (deftest sequence-of-length-p.1
1055 (mapcar #'sequence-of-length-p
1056 (list nil
1058 (list 1)
1059 (vector 1)
1060 (list 1 2)
1061 (vector 1 2)
1062 (list 1 2)
1063 (vector 1 2)
1064 (list 1 2)
1065 (vector 1 2))
1066 (list 0
1076 (t t t t t t nil nil nil nil))
1078 (deftest length=.1
1079 (mapcar #'length=
1080 (list nil
1082 (list 1)
1083 (vector 1)
1084 (list 1 2)
1085 (vector 1 2)
1086 (list 1 2)
1087 (vector 1 2)
1088 (list 1 2)
1089 (vector 1 2))
1090 (list 0
1100 (t t t t t t nil nil nil nil))
1102 (deftest length=.2
1103 ;; test the compiler macro
1104 (macrolet ((x (&rest args)
1105 (funcall
1106 (compile nil
1107 `(lambda ()
1108 (length= ,@args))))))
1109 (list (x 2 '(1 2))
1110 (x '(1 2) '(3 4))
1111 (x '(1 2) 2)
1112 (x '(1 2) 2 '(3 4))
1113 (x 1 2 3)))
1114 (t t t t nil))
1116 (deftest copy-sequence.1
1117 (let ((l (list 1 2 3))
1118 (v (vector #\a #\b #\c)))
1119 (declare (notinline copy-sequence))
1120 (let ((l.list (copy-sequence 'list l))
1121 (l.vector (copy-sequence 'vector l))
1122 (l.spec-v (copy-sequence '(vector fixnum) l))
1123 (v.vector (copy-sequence 'vector v))
1124 (v.list (copy-sequence 'list v))
1125 (v.string (copy-sequence 'string v)))
1126 (list (member l (list l.list l.vector l.spec-v))
1127 (member v (list v.vector v.list v.string))
1128 (equal l.list l)
1129 (equalp l.vector #(1 2 3))
1130 (eq 'fixnum (array-element-type l.spec-v))
1131 (equalp v.vector v)
1132 (equal v.list '(#\a #\b #\c))
1133 (equal "abc" v.string))))
1134 (nil nil t t t t t t))
1136 (deftest first-elt.1
1137 (mapcar #'first-elt
1138 (list (list 1 2 3)
1139 "abc"
1140 (vector :a :b :c)))
1141 (1 #\a :a))
1143 (deftest first-elt.error.1
1144 (mapcar (lambda (x)
1145 (handler-case
1146 (first-elt x)
1147 (type-error ()
1148 :type-error)))
1149 (list nil
1152 :zot))
1153 (:type-error
1154 :type-error
1155 :type-error
1156 :type-error))
1158 (deftest setf-first-elt.1
1159 (let ((l (list 1 2 3))
1160 (s (copy-seq "foobar"))
1161 (v (vector :a :b :c)))
1162 (setf (first-elt l) -1
1163 (first-elt s) #\x
1164 (first-elt v) 'zot)
1165 (values l s v))
1166 (-1 2 3)
1167 "xoobar"
1168 #(zot :b :c))
1170 (deftest setf-first-elt.error.1
1171 (let ((l 'foo))
1172 (multiple-value-bind (res err)
1173 (ignore-errors (setf (first-elt l) 4))
1174 (typep err 'type-error)))
1177 (deftest last-elt.1
1178 (mapcar #'last-elt
1179 (list (list 1 2 3)
1180 (vector :a :b :c)
1181 "FOOBAR"
1182 #*001
1183 #*010))
1184 (3 :c #\R 1 0))
1186 (deftest last-elt.error.1
1187 (mapcar (lambda (x)
1188 (handler-case
1189 (last-elt x)
1190 (type-error ()
1191 :type-error)))
1192 (list nil
1195 :zot
1196 (circular-list 1 2 3)
1197 (list* 1 2 3 (circular-list 4 5))))
1198 (:type-error
1199 :type-error
1200 :type-error
1201 :type-error
1202 :type-error
1203 :type-error))
1205 (deftest setf-last-elt.1
1206 (let ((l (list 1 2 3))
1207 (s (copy-seq "foobar"))
1208 (b (copy-seq #*010101001)))
1209 (setf (last-elt l) '???
1210 (last-elt s) #\?
1211 (last-elt b) 0)
1212 (values l s b))
1213 (1 2 ???)
1214 "fooba?"
1215 #*010101000)
1217 (deftest setf-last-elt.error.1
1218 (handler-case
1219 (setf (last-elt 'foo) 13)
1220 (type-error ()
1221 :type-error))
1222 :type-error)
1224 (deftest starts-with.1
1225 (list (starts-with 1 '(1 2 3))
1226 (starts-with 1 #(1 2 3))
1227 (starts-with #\x "xyz")
1228 (starts-with 2 '(1 2 3))
1229 (starts-with 3 #(1 2 3))
1230 (starts-with 1 1)
1231 (starts-with nil nil))
1232 (t t t nil nil nil nil))
1234 (deftest starts-with.2
1235 (values (starts-with 1 '(-1 2 3) :key '-)
1236 (starts-with "foo" '("foo" "bar") :test 'equal)
1237 (starts-with "f" '(#\f) :key 'string :test 'equal)
1238 (starts-with -1 '(0 1 2) :key #'1+)
1239 (starts-with "zot" '("ZOT") :test 'equal))
1244 nil)
1246 (deftest ends-with.1
1247 (list (ends-with 3 '(1 2 3))
1248 (ends-with 3 #(1 2 3))
1249 (ends-with #\z "xyz")
1250 (ends-with 2 '(1 2 3))
1251 (ends-with 1 #(1 2 3))
1252 (ends-with 1 1)
1253 (ends-with nil nil))
1254 (t t t nil nil nil nil))
1256 (deftest ends-with.2
1257 (values (ends-with 2 '(0 13 1) :key '1+)
1258 (ends-with "foo" (vector "bar" "foo") :test 'equal)
1259 (ends-with "X" (vector 1 2 #\X) :key 'string :test 'equal)
1260 (ends-with "foo" "foo" :test 'equal))
1264 nil)
1266 (deftest ends-with.error.1
1267 (handler-case
1268 (ends-with 3 (circular-list 3 3 3 1 3 3))
1269 (type-error ()
1270 :type-error))
1271 :type-error)
1273 (deftest sequences.passing-improper-lists
1274 (macrolet ((signals-error-p (form)
1275 `(handler-case
1276 (progn ,form nil)
1277 (type-error (e)
1278 t)))
1279 (cut (fn &rest args)
1280 (with-gensyms (arg)
1281 (print`(lambda (,arg)
1282 (apply ,fn (list ,@(substitute arg '_ args))))))))
1283 (let ((circular-list (make-circular-list 5 :initial-element :foo))
1284 (dotted-list (list* 'a 'b 'c 'd)))
1285 (loop for nth from 0
1286 for fn in (list
1287 (cut #'lastcar _)
1288 (cut #'rotate _ 3)
1289 (cut #'rotate _ -3)
1290 (cut #'shuffle _)
1291 (cut #'random-elt _)
1292 (cut #'last-elt _)
1293 (cut #'ends-with :foo _))
1294 nconcing
1295 (let ((on-circular-p (signals-error-p (funcall fn circular-list)))
1296 (on-dotted-p (signals-error-p (funcall fn dotted-list))))
1297 (when (or (not on-circular-p) (not on-dotted-p))
1298 (append
1299 (unless on-circular-p
1300 (let ((*print-circle* t))
1301 (list
1302 (format nil
1303 "No appropriate error signalled when passing ~S to ~Ath entry."
1304 circular-list nth))))
1305 (unless on-dotted-p
1306 (list
1307 (format nil
1308 "No appropriate error signalled when passing ~S to ~Ath entry."
1309 dotted-list nth)))))))))
1310 nil)
1312 (deftest with-unique-names.1
1313 (let ((*gensym-counter* 0))
1314 (let ((syms (with-unique-names (foo bar quux)
1315 (list foo bar quux))))
1316 (list (find-if #'symbol-package syms)
1317 (equal '("FOO0" "BAR1" "QUUX2")
1318 (mapcar #'symbol-name syms)))))
1319 (nil t))
1321 (deftest with-unique-names.2
1322 (let ((*gensym-counter* 0))
1323 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-) (quux #\q))
1324 (list foo bar quux))))
1325 (list (find-if #'symbol-package syms)
1326 (equal '("_foo_0" "-BAR-1" "q2")
1327 (mapcar #'symbol-name syms)))))
1328 (nil t))
1330 (deftest with-unique-names.3
1331 (let ((*gensym-counter* 0))
1332 (multiple-value-bind (res err)
1333 (ignore-errors
1334 (eval
1335 '(let ((syms
1336 (with-unique-names ((foo "_foo_") (bar -bar-) (quux 42))
1337 (list foo bar quux))))
1338 (list (find-if #'symbol-package syms)
1339 (equal '("_foo_0" "-BAR-1" "q2")
1340 (mapcar #'symbol-name syms))))))
1341 (typep err 'error)))
1344 (deftest once-only.1
1345 (macrolet ((cons1.good (x)
1346 (once-only (x)
1347 `(cons ,x ,x)))
1348 (cons1.bad (x)
1349 `(cons ,x ,x)))
1350 (let ((y 0))
1351 (list (cons1.good (incf y))
1353 (cons1.bad (incf y))
1354 y)))
1355 ((1 . 1) 1 (2 . 3) 3))
1357 (deftest once-only.2
1358 (macrolet ((cons1 (x)
1359 (once-only ((y x))
1360 `(cons ,y ,y))))
1361 (let ((z 0))
1362 (list (cons1 (incf z))
1364 (cons1 (incf z)))))
1365 ((1 . 1) 1 (2 . 2)))
1367 (deftest parse-body.1
1368 (parse-body '("doc" "body") :documentation t)
1369 ("body")
1371 "doc")
1373 (deftest parse-body.2
1374 (parse-body '("body") :documentation t)
1375 ("body")
1377 nil)
1379 (deftest parse-body.3
1380 (parse-body '("doc" "body"))
1381 ("doc" "body")
1383 nil)
1385 (deftest parse-body.4
1386 (parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t)
1387 (body)
1388 ((declare (foo)) (declare (bar)))
1389 "doc")
1391 (deftest parse-body.5
1392 (parse-body '((declare (foo)) "doc" (declare (bar)) body))
1393 ("doc" (declare (bar)) body)
1394 ((declare (foo)))
1395 nil)
1397 (deftest parse-body.6
1398 (multiple-value-bind (res err)
1399 (ignore-errors
1400 (parse-body '("foo" "bar" "quux")
1401 :documentation t))
1402 (typep err 'error))
1405 ;;;; Symbols
1407 (deftest ensure-symbol.1
1408 (ensure-symbol :cons :cl)
1409 cons
1410 :external)
1412 (deftest ensure-symbol.2
1413 (ensure-symbol "CONS" :alexandria)
1414 cons
1415 :inherited)
1417 (deftest ensure-symbol.3
1418 (ensure-symbol 'foo :keyword)
1419 :foo
1420 :external)
1422 (deftest ensure-symbol.4
1423 (ensure-symbol #\* :alexandria)
1425 :inherited)
1427 (deftest format-symbol.1
1428 (let ((s (format-symbol nil "X-~D" 13)))
1429 (list (symbol-package s)
1430 (symbol-name s)))
1431 (nil "X-13"))
1433 (deftest format-symbol.2
1434 (format-symbol :keyword "SYM-~A" :bolic)
1435 :sym-bolic)
1437 (deftest format-symbol.3
1438 (let ((*package* (find-package :cl)))
1439 (format-symbol t "FIND-~A" 'package))
1440 find-package)
1442 (deftest make-keyword.1
1443 (list (make-keyword 'zot)
1444 (make-keyword "FOO")
1445 (make-keyword #\Q))
1446 (:zot :foo :q))
1448 (deftest make-gensym-list.1
1449 (let ((*gensym-counter* 0))
1450 (let ((syms (make-gensym-list 3 "FOO")))
1451 (list (find-if 'symbol-package syms)
1452 (equal '("FOO0" "FOO1" "FOO2")
1453 (mapcar 'symbol-name syms)))))
1454 (nil t))
1456 (deftest make-gensym-list.2
1457 (let ((*gensym-counter* 0))
1458 (let ((syms (make-gensym-list 3)))
1459 (list (find-if 'symbol-package syms)
1460 (equal '("G0" "G1" "G2")
1461 (mapcar 'symbol-name syms)))))
1462 (nil t))
1464 ;;;; Type-system
1466 (deftest of-type.1
1467 (locally
1468 (declare (notinline of-type))
1469 (let ((f (of-type 'string)))
1470 (list (funcall f "foo")
1471 (funcall f 'bar))))
1472 (t nil))
1474 (deftest type=.1
1475 (type= 'string 'string)
1479 (deftest type=.2
1480 (type= 'list '(or null cons))
1484 (deftest type=.3
1485 (type= 'null '(and symbol list))
1489 (deftest type=.4
1490 (type= 'string '(satisfies emptyp))
1492 nil)
1494 (deftest type=.5
1495 (type= 'string 'list)
1499 (macrolet
1500 ((test (type numbers)
1501 `(deftest ,(format-symbol t "CDR5.~A" type)
1502 (let ((numbers ,numbers))
1503 (values (mapcar (of-type ',(format-symbol t "NEGATIVE-~A" type)) numbers)
1504 (mapcar (of-type ',(format-symbol t "NON-POSITIVE-~A" type)) numbers)
1505 (mapcar (of-type ',(format-symbol t "NON-NEGATIVE-~A" type)) numbers)
1506 (mapcar (of-type ',(format-symbol t "POSITIVE-~A" type)) numbers)))
1507 (t t t nil nil nil nil)
1508 (t t t t nil nil nil)
1509 (nil nil nil t t t t)
1510 (nil nil nil nil t t t))))
1511 (test fixnum (list most-negative-fixnum -42 -1 0 1 42 most-positive-fixnum))
1512 (test integer (list (1- most-negative-fixnum) -42 -1 0 1 42 (1+ most-positive-fixnum)))
1513 (test rational (list (1- most-negative-fixnum) -42/13 -1 0 1 42/13 (1+ most-positive-fixnum)))
1514 (test real (list most-negative-long-float -42/13 -1 0 1 42/13 most-positive-long-float))
1515 (test float (list most-negative-short-float -42.02 -1.0 0.0 1.0 42.02 most-positive-short-float))
1516 (test short-float (list most-negative-short-float -42.02s0 -1.0s0 0.0s0 1.0s0 42.02s0 most-positive-short-float))
1517 (test single-float (list most-negative-single-float -42.02f0 -1.0f0 0.0f0 1.0f0 42.02f0 most-positive-single-float))
1518 (test double-float (list most-negative-double-float -42.02d0 -1.0d0 0.0d0 1.0d0 42.02d0 most-positive-double-float))
1519 (test long-float (list most-negative-long-float -42.02l0 -1.0l0 0.0l0 1.0l0 42.02l0 most-positive-long-float)))
1521 ;;;; Bindings
1523 (declaim (notinline opaque))
1524 (defun opaque (x)
1527 (deftest if-let.1
1528 (if-let (x (opaque :ok))
1530 :bad)
1531 :ok)
1533 (deftest if-let.2
1534 (if-let (x (opaque nil))
1535 :bad
1536 (and (not x) :ok))
1537 :ok)
1539 (deftest if-let.3
1540 (let ((x 1))
1541 (if-let ((x 2)
1542 (y x))
1543 (+ x y)
1544 :oops))
1547 (deftest if-let.4
1548 (if-let ((x 1)
1549 (y nil))
1550 :oops
1551 (and (not y) x))
1554 (deftest if-let.5
1555 (if-let (x)
1556 :oops
1557 (not x))
1560 (deftest if-let.error.1
1561 (handler-case
1562 (eval '(if-let x
1563 :oops
1564 :oops))
1565 (type-error ()
1566 :type-error))
1567 :type-error)
1569 (deftest when-let.1
1570 (when-let (x (opaque :ok))
1571 (setf x (cons x x))
1573 (:ok . :ok))
1575 (deftest when-let.2
1576 (when-let ((x 1)
1577 (y nil)
1578 (z 3))
1579 :oops)
1580 nil)
1582 (deftest when-let.3
1583 (let ((x 1))
1584 (when-let ((x 2)
1585 (y x))
1586 (+ x y)))
1589 (deftest when-let.error.1
1590 (handler-case
1591 (eval '(when-let x :oops))
1592 (type-error ()
1593 :type-error))
1594 :type-error)
1596 (deftest when-let*.1
1597 (let ((x 1))
1598 (when-let* ((x 2)
1599 (y x))
1600 (+ x y)))
1603 (deftest when-let*.2
1604 (let ((y 1))
1605 (when-let* (x y)
1606 (1+ x)))
1609 (deftest when-let*.3
1610 (when-let* ((x t)
1611 (y (consp x))
1612 (z (error "OOPS")))
1614 nil)
1616 (deftest when-let*.error.1
1617 (handler-case
1618 (eval '(when-let* x :oops))
1619 (type-error ()
1620 :type-error))
1621 :type-error)
1623 (deftest nth-value-or.1
1624 (multiple-value-bind (a b c)
1625 (nth-value-or 1
1626 (values 1 nil 1)
1627 (values 2 2 2))
1628 (= a b c 2))
1631 (deftest doplist.1
1632 (let (keys values)
1633 (doplist (k v '(a 1 b 2 c 3) (values t (reverse keys) (reverse values) k v))
1634 (push k keys)
1635 (push v values)))
1637 (a b c)
1638 (1 2 3)
1640 nil)