Added length
[alexandria.git] / tests.lisp
blob66a2cdcbac5c7a038b20c683ea721fa62f71f3cf
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 lastcar.1
576 (let ((l1 (list 1))
577 (l2 (list 1 2)))
578 (list (lastcar l1)
579 (lastcar l2)))
580 (1 2))
582 (deftest lastcar.error.2
583 (handler-case
584 (progn
585 (lastcar (circular-list 1 2 3))
586 nil)
587 (error ()
591 (deftest setf-lastcar.1
592 (let ((l (list 1 2 3 4)))
593 (values (lastcar l)
594 (progn
595 (setf (lastcar l) 42)
596 (lastcar l))))
600 (deftest setf-lastcar.2
601 (let ((l (circular-list 1 2 3)))
602 (multiple-value-bind (res err)
603 (ignore-errors (setf (lastcar l) 4))
604 (typep err 'type-error)))
607 (deftest make-circular-list.1
608 (let ((l (make-circular-list 3 :initial-element :x)))
609 (setf (car l) :y)
610 (list (eq l (nthcdr 3 l))
611 (first l)
612 (second l)
613 (third l)
614 (fourth l)))
615 (t :y :x :x :y))
617 (deftest circular-list.type.1
618 (let* ((l1 (list 1 2 3))
619 (l2 (circular-list 1 2 3))
620 (l3 (list* 1 2 3 l2)))
621 (list (typep l1 'circular-list)
622 (typep l2 'circular-list)
623 (typep l3 'circular-list)))
624 (nil t t))
626 (deftest ensure-list.1
627 (let ((x (list 1))
628 (y 2))
629 (list (ensure-list x)
630 (ensure-list y)))
631 ((1) (2)))
633 (deftest ensure-cons.1
634 (let ((x (cons 1 2))
635 (y nil)
636 (z "foo"))
637 (values (ensure-cons x)
638 (ensure-cons y)
639 (ensure-cons z)))
640 (1 . 2)
641 (nil)
642 ("foo"))
644 (deftest setp.1
645 (setp '(1))
648 (deftest setp.2
649 (setp nil)
652 (deftest setp.3
653 (setp "foo")
654 nil)
656 (deftest setp.4
657 (setp '(1 2 3 1))
658 nil)
660 (deftest setp.5
661 (setp '(1 2 3))
664 (deftest setp.6
665 (setp '(a :a))
668 (deftest setp.7
669 (setp '(a :a) :key 'character)
670 nil)
672 (deftest setp.8
673 (setp '(a :a) :key 'character :test (constantly nil))
676 (deftest set-equal.1
677 (set-equal '(1 2 3) '(3 1 2))
680 (deftest set-equal.2
681 (set-equal '("Xa") '("Xb")
682 :test (lambda (a b) (eql (char a 0) (char b 0))))
685 (deftest set-equal.3
686 (set-equal '(1 2) '(4 2))
687 nil)
689 (deftest set-equal.4
690 (set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal)
693 (deftest set-equal.5
694 (set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal)
695 nil)
697 (deftest set-equal.6
698 (set-equal '(a b c) '(a b c d))
699 nil)
701 (deftest map-product.1
702 (map-product 'cons '(2 3) '(1 4))
703 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
705 (deftest map-product.2
706 (map-product #'cons '(2 3) '(1 4))
707 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
709 (deftest flatten.1
710 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
711 (1 2 3 4 5 6 7))
713 (deftest remove-from-plist.1
714 (let ((orig '(a 1 b 2 c 3 d 4)))
715 (list (remove-from-plist orig 'a 'c)
716 (remove-from-plist orig 'b 'd)
717 (remove-from-plist orig 'b)
718 (remove-from-plist orig 'a)
719 (remove-from-plist orig 'd 42 "zot")
720 (remove-from-plist orig 'a 'b 'c 'd)
721 (remove-from-plist orig 'a 'b 'c 'd 'x)
722 (equal orig '(a 1 b 2 c 3 d 4))))
723 ((b 2 d 4)
724 (a 1 c 3)
725 (a 1 c 3 d 4)
726 (b 2 c 3 d 4)
727 (a 1 b 2 c 3)
732 (deftest mappend.1
733 (mappend (compose 'list '*) '(1 2 3) '(1 2 3))
734 (1 4 9))
736 ;;;; Numbers
738 (deftest clamp.1
739 (list (clamp 1.5 1 2)
740 (clamp 2.0 1 2)
741 (clamp 1.0 1 2)
742 (clamp 3 1 2)
743 (clamp 0 1 2))
744 (1.5 2.0 1.0 2 1))
746 (deftest gaussian-random.1
747 (let ((min -0.2)
748 (max +0.2))
749 (multiple-value-bind (g1 g2)
750 (gaussian-random min max)
751 (values (<= min g1 max)
752 (<= min g2 max)
753 (/= g1 g2) ;uh
759 (deftest iota.1
760 (iota 3)
761 (0 1 2))
763 (deftest iota.2
764 (iota 3 :start 0.0d0)
765 (0.0d0 1.0d0 2.0d0))
767 (deftest iota.3
768 (iota 3 :start 2 :step 3.0)
769 (2.0 5.0 8.0))
771 (deftest map-iota.1
772 (let (all)
773 (declare (notinline map-iota))
774 (values (map-iota (lambda (x) (push x all))
776 :start 2
777 :step 1.1d0)
778 all))
780 (4.2d0 3.1d0 2.0d0))
782 (deftest lerp.1
783 (lerp 0.5 1 2)
784 1.5)
786 (deftest lerp.2
787 (lerp 0.1 1 2)
788 1.1)
790 (deftest mean.1
791 (mean '(1 2 3))
794 (deftest mean.2
795 (mean '(1 2 3 4))
796 5/2)
798 (deftest mean.3
799 (mean '(1 2 10))
800 13/3)
802 (deftest median.1
803 (median '(100 0 99 1 98 2 97))
806 (deftest median.2
807 (median '(100 0 99 1 98 2 97 96))
808 195/2)
810 (deftest variance.1
811 (variance (list 1 2 3))
812 2/3)
814 (deftest standard-deviation.1
815 (< 0 (standard-deviation (list 1 2 3)) 1)
818 (deftest maxf.1
819 (let ((x 1))
820 (maxf x 2)
824 (deftest maxf.2
825 (let ((x 1))
826 (maxf x 0)
830 (deftest maxf.3
831 (let ((x 1)
832 (c 0))
833 (maxf x (incf c))
834 (list x c))
835 (1 1))
837 (deftest maxf.4
838 (let ((xv (vector 0 0 0))
839 (p 0))
840 (maxf (svref xv (incf p)) (incf p))
841 (list p xv))
842 (2 #(0 2 0)))
844 (deftest minf.1
845 (let ((y 1))
846 (minf y 0)
850 (deftest minf.2
851 (let ((xv (vector 10 10 10))
852 (p 0))
853 (minf (svref xv (incf p)) (incf p))
854 (list p xv))
855 (2 #(10 2 10)))
857 ;;;; Arrays
859 #+nil
860 (deftest array-index.type)
862 #+nil
863 (deftest copy-array)
865 ;;;; Sequences
867 (deftest rotate.1
868 (list (rotate (list 1 2 3) 0)
869 (rotate (list 1 2 3) 1)
870 (rotate (list 1 2 3) 2)
871 (rotate (list 1 2 3) 3)
872 (rotate (list 1 2 3) 4))
873 ((1 2 3)
874 (3 1 2)
875 (2 3 1)
876 (1 2 3)
877 (3 1 2)))
879 (deftest rotate.2
880 (list (rotate (vector 1 2 3 4) 0)
881 (rotate (vector 1 2 3 4))
882 (rotate (vector 1 2 3 4) 2)
883 (rotate (vector 1 2 3 4) 3)
884 (rotate (vector 1 2 3 4) 4)
885 (rotate (vector 1 2 3 4) 5))
886 (#(1 2 3 4)
887 #(4 1 2 3)
888 #(3 4 1 2)
889 #(2 3 4 1)
890 #(1 2 3 4)
891 #(4 1 2 3)))
893 (deftest rotate.3
894 (list (rotate (list 1 2 3) 0)
895 (rotate (list 1 2 3) -1)
896 (rotate (list 1 2 3) -2)
897 (rotate (list 1 2 3) -3)
898 (rotate (list 1 2 3) -4))
899 ((1 2 3)
900 (2 3 1)
901 (3 1 2)
902 (1 2 3)
903 (2 3 1)))
905 (deftest rotate.4
906 (list (rotate (vector 1 2 3 4) 0)
907 (rotate (vector 1 2 3 4) -1)
908 (rotate (vector 1 2 3 4) -2)
909 (rotate (vector 1 2 3 4) -3)
910 (rotate (vector 1 2 3 4) -4)
911 (rotate (vector 1 2 3 4) -5))
912 (#(1 2 3 4)
913 #(2 3 4 1)
914 #(3 4 1 2)
915 #(4 1 2 3)
916 #(1 2 3 4)
917 #(2 3 4 1)))
919 (deftest rotate.5
920 (values (rotate (list 1) 17)
921 (rotate (list 1) -5))
923 (1))
925 (deftest shuffle.1
926 (let ((s (shuffle (iota 100))))
927 (list (equal s (iota 100))
928 (every (lambda (x)
929 (member x s))
930 (iota 100))
931 (every (lambda (x)
932 (typep x '(integer 0 99)))
933 s)))
934 (nil t t))
936 (deftest shuffle.2
937 (let ((s (shuffle (coerce (iota 100) 'vector))))
938 (list (equal s (coerce (iota 100) 'vector))
939 (every (lambda (x)
940 (find x s))
941 (iota 100))
942 (every (lambda (x)
943 (typep x '(integer 0 99)))
944 s)))
945 (nil t t))
947 (deftest random-elt.1
948 (let ((s1 #(1 2 3 4))
949 (s2 '(1 2 3 4)))
950 (list (dotimes (i 1000 nil)
951 (unless (member (random-elt s1) s2)
952 (return nil))
953 (when (/= (random-elt s1) (random-elt s1))
954 (return t)))
955 (dotimes (i 1000 nil)
956 (unless (member (random-elt s2) s2)
957 (return nil))
958 (when (/= (random-elt s2) (random-elt s2))
959 (return t)))))
960 (t t))
962 (deftest removef.1
963 (let* ((x '(1 2 3))
964 (x* x)
965 (y #(1 2 3))
966 (y* y))
967 (removef x 1)
968 (removef y 3)
969 (list x x* y y*))
970 ((2 3)
971 (1 2 3)
972 #(1 2)
973 #(1 2 3)))
975 (deftest deletef.1
976 (let* ((x (list 1 2 3))
977 (x* x)
978 (y (vector 1 2 3)))
979 (deletef x 2)
980 (deletef y 1)
981 (list x x* y))
982 ((1 3)
983 (1 3)
984 #(2 3)))
986 (deftest proper-sequence.type.1
987 (mapcar (lambda (x)
988 (typep x 'proper-sequence))
989 (list (list 1 2 3)
990 (vector 1 2 3)
991 #2a((1 2) (3 4))
992 (circular-list 1 2 3 4)))
993 (t t nil nil))
995 (deftest emptyp.1
996 (mapcar #'emptyp
997 (list (list 1)
998 (circular-list 1)
1000 (vector)
1001 (vector 1)))
1002 (nil nil t t nil))
1004 (deftest sequence-of-length-p.1
1005 (mapcar #'sequence-of-length-p
1006 (list nil
1008 (list 1)
1009 (vector 1)
1010 (list 1 2)
1011 (vector 1 2)
1012 (list 1 2)
1013 (vector 1 2)
1014 (list 1 2)
1015 (vector 1 2))
1016 (list 0
1026 (t t t t t t nil nil nil nil))
1028 (deftest length=.1
1029 (mapcar #'length=
1030 (list nil
1032 (list 1)
1033 (vector 1)
1034 (list 1 2)
1035 (vector 1 2)
1036 (list 1 2)
1037 (vector 1 2)
1038 (list 1 2)
1039 (vector 1 2))
1040 (list 0
1050 (t t t t t t nil nil nil nil))
1052 (deftest length=.2
1053 ;; test the compiler macro
1054 (macrolet ((x (&rest args)
1055 (funcall
1056 (compile nil
1057 `(lambda ()
1058 (length= ,@args))))))
1059 (list (x 2 '(1 2))
1060 (x '(1 2) '(3 4))
1061 (x '(1 2) 2)
1062 (x '(1 2) 2 '(3 4))
1063 (x 1 2 3)))
1064 (t t t t nil))
1066 (deftest copy-sequence.1
1067 (let ((l (list 1 2 3))
1068 (v (vector #\a #\b #\c)))
1069 (declare (notinline copy-sequence))
1070 (let ((l.list (copy-sequence 'list l))
1071 (l.vector (copy-sequence 'vector l))
1072 (l.spec-v (copy-sequence '(vector fixnum) l))
1073 (v.vector (copy-sequence 'vector v))
1074 (v.list (copy-sequence 'list v))
1075 (v.string (copy-sequence 'string v)))
1076 (list (member l (list l.list l.vector l.spec-v))
1077 (member v (list v.vector v.list v.string))
1078 (equal l.list l)
1079 (equalp l.vector #(1 2 3))
1080 (eq 'fixnum (array-element-type l.spec-v))
1081 (equalp v.vector v)
1082 (equal v.list '(#\a #\b #\c))
1083 (equal "abc" v.string))))
1084 (nil nil t t t t t t))
1086 (deftest first-elt.1
1087 (mapcar #'first-elt
1088 (list (list 1 2 3)
1089 "abc"
1090 (vector :a :b :c)))
1091 (1 #\a :a))
1093 (deftest first-elt.error.1
1094 (mapcar (lambda (x)
1095 (handler-case
1096 (first-elt x)
1097 (type-error ()
1098 :type-error)))
1099 (list nil
1102 :zot))
1103 (:type-error
1104 :type-error
1105 :type-error
1106 :type-error))
1108 (deftest setf-first-elt.1
1109 (let ((l (list 1 2 3))
1110 (s (copy-seq "foobar"))
1111 (v (vector :a :b :c)))
1112 (setf (first-elt l) -1
1113 (first-elt s) #\x
1114 (first-elt v) 'zot)
1115 (values l s v))
1116 (-1 2 3)
1117 "xoobar"
1118 #(zot :b :c))
1120 (deftest setf-first-elt.error.1
1121 (let ((l 'foo))
1122 (multiple-value-bind (res err)
1123 (ignore-errors (setf (first-elt l) 4))
1124 (typep err 'type-error)))
1127 (deftest last-elt.1
1128 (mapcar #'last-elt
1129 (list (list 1 2 3)
1130 (vector :a :b :c)
1131 "FOOBAR"
1132 #*001
1133 #*010))
1134 (3 :c #\R 1 0))
1136 (deftest last-elt.error.1
1137 (mapcar (lambda (x)
1138 (handler-case
1139 (last-elt x)
1140 (type-error ()
1141 :type-error)))
1142 (list nil
1145 :zot
1146 (circular-list 1 2 3)
1147 (list* 1 2 3 (circular-list 4 5))))
1148 (:type-error
1149 :type-error
1150 :type-error
1151 :type-error
1152 :type-error
1153 :type-error))
1155 (deftest setf-last-elt.1
1156 (let ((l (list 1 2 3))
1157 (s (copy-seq "foobar"))
1158 (b (copy-seq #*010101001)))
1159 (setf (last-elt l) '???
1160 (last-elt s) #\?
1161 (last-elt b) 0)
1162 (values l s b))
1163 (1 2 ???)
1164 "fooba?"
1165 #*010101000)
1167 (deftest setf-last-elt.error.1
1168 (handler-case
1169 (setf (last-elt 'foo) 13)
1170 (type-error ()
1171 :type-error))
1172 :type-error)
1174 (deftest starts-with.1
1175 (list (starts-with 1 '(1 2 3))
1176 (starts-with 1 #(1 2 3))
1177 (starts-with #\x "xyz")
1178 (starts-with 2 '(1 2 3))
1179 (starts-with 3 #(1 2 3))
1180 (starts-with 1 1)
1181 (starts-with nil nil))
1182 (t t t nil nil nil nil))
1184 (deftest starts-with.2
1185 (values (starts-with 1 '(-1 2 3) :key '-)
1186 (starts-with "foo" '("foo" "bar") :test 'equal)
1187 (starts-with "f" '(#\f) :key 'string :test 'equal)
1188 (starts-with -1 '(0 1 2) :key #'1+)
1189 (starts-with "zot" '("ZOT") :test 'equal))
1194 nil)
1196 (deftest ends-with.1
1197 (list (ends-with 3 '(1 2 3))
1198 (ends-with 3 #(1 2 3))
1199 (ends-with #\z "xyz")
1200 (ends-with 2 '(1 2 3))
1201 (ends-with 1 #(1 2 3))
1202 (ends-with 1 1)
1203 (ends-with nil nil))
1204 (t t t nil nil nil nil))
1206 (deftest ends-with.2
1207 (values (ends-with 2 '(0 13 1) :key '1+)
1208 (ends-with "foo" (vector "bar" "foo") :test 'equal)
1209 (ends-with "X" (vector 1 2 #\X) :key 'string :test 'equal)
1210 (ends-with "foo" "foo" :test 'equal))
1214 nil)
1216 (deftest ends-with.error.1
1217 (handler-case
1218 (ends-with 3 (circular-list 3 3 3 1 3 3))
1219 (type-error ()
1220 :type-error))
1221 :type-error)
1223 (deftest with-unique-names.1
1224 (let ((*gensym-counter* 0))
1225 (let ((syms (with-unique-names (foo bar quux)
1226 (list foo bar quux))))
1227 (list (find-if #'symbol-package syms)
1228 (equal '("FOO0" "BAR1" "QUUX2")
1229 (mapcar #'symbol-name syms)))))
1230 (nil t))
1232 (deftest with-unique-names.2
1233 (let ((*gensym-counter* 0))
1234 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-) (quux #\q))
1235 (list foo bar quux))))
1236 (list (find-if #'symbol-package syms)
1237 (equal '("_foo_0" "-BAR-1" "q2")
1238 (mapcar #'symbol-name syms)))))
1239 (nil t))
1241 (deftest with-unique-names.3
1242 (let ((*gensym-counter* 0))
1243 (multiple-value-bind (res err)
1244 (ignore-errors
1245 (eval
1246 '(let ((syms
1247 (with-unique-names ((foo "_foo_") (bar -bar-) (quux 42))
1248 (list foo bar quux))))
1249 (list (find-if #'symbol-package syms)
1250 (equal '("_foo_0" "-BAR-1" "q2")
1251 (mapcar #'symbol-name syms))))))
1252 (typep err 'error)))
1255 (deftest once-only.1
1256 (macrolet ((cons1.good (x)
1257 (once-only (x)
1258 `(cons ,x ,x)))
1259 (cons1.bad (x)
1260 `(cons ,x ,x)))
1261 (let ((y 0))
1262 (list (cons1.good (incf y))
1264 (cons1.bad (incf y))
1265 y)))
1266 ((1 . 1) 1 (2 . 3) 3))
1268 (deftest once-only.2
1269 (macrolet ((cons1 (x)
1270 (once-only ((y x))
1271 `(cons ,y ,y))))
1272 (let ((z 0))
1273 (list (cons1 (incf z))
1275 (cons1 (incf z)))))
1276 ((1 . 1) 1 (2 . 2)))
1278 (deftest parse-body.1
1279 (parse-body '("doc" "body") :documentation t)
1280 ("body")
1282 "doc")
1284 (deftest parse-body.2
1285 (parse-body '("body") :documentation t)
1286 ("body")
1288 nil)
1290 (deftest parse-body.3
1291 (parse-body '("doc" "body"))
1292 ("doc" "body")
1294 nil)
1296 (deftest parse-body.4
1297 (parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t)
1298 (body)
1299 ((declare (foo)) (declare (bar)))
1300 "doc")
1302 (deftest parse-body.5
1303 (parse-body '((declare (foo)) "doc" (declare (bar)) body))
1304 ("doc" (declare (bar)) body)
1305 ((declare (foo)))
1306 nil)
1308 (deftest parse-body.6
1309 (multiple-value-bind (res err)
1310 (ignore-errors
1311 (parse-body '("foo" "bar" "quux")
1312 :documentation t))
1313 (typep err 'error))
1316 ;;;; Symbols
1318 (deftest ensure-symbol.1
1319 (ensure-symbol :cons :cl)
1320 cons
1321 :external)
1323 (deftest ensure-symbol.2
1324 (ensure-symbol "CONS" :alexandria)
1325 cons
1326 :inherited)
1328 (deftest ensure-symbol.3
1329 (ensure-symbol 'foo :keyword)
1330 :foo
1331 :external)
1333 (deftest ensure-symbol.4
1334 (ensure-symbol #\* :alexandria)
1336 :inherited)
1338 (deftest format-symbol.1
1339 (let ((s (format-symbol nil "X-~D" 13)))
1340 (list (symbol-package s)
1341 (symbol-name s)))
1342 (nil "X-13"))
1344 (deftest format-symbol.2
1345 (format-symbol :keyword "SYM-~A" :bolic)
1346 :sym-bolic)
1348 (deftest format-symbol.3
1349 (let ((*package* (find-package :cl)))
1350 (format-symbol t "FIND-~A" 'package))
1351 find-package)
1353 (deftest make-keyword.1
1354 (list (make-keyword 'zot)
1355 (make-keyword "FOO")
1356 (make-keyword #\Q))
1357 (:zot :foo :q))
1359 (deftest make-gensym-list.1
1360 (let ((*gensym-counter* 0))
1361 (let ((syms (make-gensym-list 3 "FOO")))
1362 (list (find-if 'symbol-package syms)
1363 (equal '("FOO0" "FOO1" "FOO2")
1364 (mapcar 'symbol-name syms)))))
1365 (nil t))
1367 (deftest make-gensym-list.2
1368 (let ((*gensym-counter* 0))
1369 (let ((syms (make-gensym-list 3)))
1370 (list (find-if 'symbol-package syms)
1371 (equal '("G0" "G1" "G2")
1372 (mapcar 'symbol-name syms)))))
1373 (nil t))
1375 ;;;; Type-system
1377 (deftest of-type.1
1378 (locally
1379 (declare (notinline of-type))
1380 (let ((f (of-type 'string)))
1381 (list (funcall f "foo")
1382 (funcall f 'bar))))
1383 (t nil))
1385 (deftest type=.1
1386 (type= 'string 'string)
1390 (deftest type=.2
1391 (type= 'list '(or null cons))
1395 (deftest type=.3
1396 (type= 'null '(and symbol list))
1400 (deftest type=.4
1401 (type= 'string '(satisfies emptyp))
1403 nil)
1405 (deftest type=.5
1406 (type= 'string 'list)
1410 (macrolet
1411 ((test (type numbers)
1412 `(deftest ,(format-symbol t "CDR5.~A" type)
1413 (let ((numbers ,numbers))
1414 (values (mapcar (of-type ',(format-symbol t "NEGATIVE-~A" type)) numbers)
1415 (mapcar (of-type ',(format-symbol t "NON-POSITIVE-~A" type)) numbers)
1416 (mapcar (of-type ',(format-symbol t "NON-NEGATIVE-~A" type)) numbers)
1417 (mapcar (of-type ',(format-symbol t "POSITIVE-~A" type)) numbers)))
1418 (t t t nil nil nil nil)
1419 (t t t t nil nil nil)
1420 (nil nil nil t t t t)
1421 (nil nil nil nil t t t))))
1422 (test fixnum (list most-negative-fixnum -42 -1 0 1 42 most-positive-fixnum))
1423 (test integer (list (1- most-negative-fixnum) -42 -1 0 1 42 (1+ most-positive-fixnum)))
1424 (test rational (list (1- most-negative-fixnum) -42/13 -1 0 1 42/13 (1+ most-positive-fixnum)))
1425 (test real (list most-negative-long-float -42/13 -1 0 1 42/13 most-positive-long-float))
1426 (test float (list most-negative-short-float -42.02 -1.0 0.0 1.0 42.02 most-positive-short-float))
1427 (test short-float (list most-negative-short-float -42.02s0 -1.0s0 0.0s0 1.0s0 42.02s0 most-positive-short-float))
1428 (test single-float (list most-negative-single-float -42.02f0 -1.0f0 0.0f0 1.0f0 42.02f0 most-positive-single-float))
1429 (test double-float (list most-negative-double-float -42.02d0 -1.0d0 0.0d0 1.0d0 42.02d0 most-positive-double-float))
1430 (test long-float (list most-negative-long-float -42.02l0 -1.0l0 0.0l0 1.0l0 42.02l0 most-positive-long-float)))
1432 ;;;; Bindings
1434 (declaim (notinline opaque))
1435 (defun opaque (x)
1438 (deftest if-let.1
1439 (if-let (x (opaque :ok))
1441 :bad)
1442 :ok)
1444 (deftest if-let.2
1445 (if-let (x (opaque nil))
1446 :bad
1447 (and (not x) :ok))
1448 :ok)
1450 (deftest if-let.3
1451 (let ((x 1))
1452 (if-let ((x 2)
1453 (y x))
1454 (+ x y)
1455 :oops))
1458 (deftest if-let.4
1459 (if-let ((x 1)
1460 (y nil))
1461 :oops
1462 (and (not y) x))
1465 (deftest if-let.5
1466 (if-let (x)
1467 :oops
1468 (not x))
1471 (deftest if-let.error.1
1472 (handler-case
1473 (eval '(if-let x
1474 :oops
1475 :oops))
1476 (type-error ()
1477 :type-error))
1478 :type-error)
1480 (deftest if-let*.1
1481 (let ((x 1))
1482 (if-let* ((x 2)
1483 (y x))
1484 (+ x y)
1485 :oops))
1488 (deftest if-let*.2
1489 (if-let* ((x 2)
1490 (y (prog1 x (setf x nil))))
1491 :oops
1492 (and (not x) y))
1495 (deftest if-let*.3
1496 (if-let* (x 1)
1498 :oops)
1501 (deftest if-let*.error.1
1502 (handler-case
1503 (eval '(if-let* x :oops :oops))
1504 (type-error ()
1505 :type-error))
1506 :type-error)
1508 (deftest when-let.1
1509 (when-let (x (opaque :ok))
1510 (setf x (cons x x))
1512 (:ok . :ok))
1514 (deftest when-let.2
1515 (when-let ((x 1)
1516 (y nil)
1517 (z 3))
1518 :oops)
1519 nil)
1521 (deftest when-let.3
1522 (let ((x 1))
1523 (when-let ((x 2)
1524 (y x))
1525 (+ x y)))
1528 (deftest when-let.error.1
1529 (handler-case
1530 (eval '(when-let x :oops))
1531 (type-error ()
1532 :type-error))
1533 :type-error)
1535 (deftest when-let*.1
1536 (let ((x 1))
1537 (when-let* ((x 2)
1538 (y x))
1539 (+ x y)))
1542 (deftest when-let*.2
1543 (let ((y 1))
1544 (when-let* (x y)
1545 (1+ x)))
1548 (deftest when-let*.error.1
1549 (handler-case
1550 (eval '(when-let* x :oops))
1551 (type-error ()
1552 :type-error))
1553 :type-error)
1555 (deftest nth-value-or.1
1556 (multiple-value-bind (a b c)
1557 (nth-value-or 1
1558 (values 1 nil 1)
1559 (values 2 2 2))
1560 (= a b c 2))
1563 (deftest doplist.1
1564 (let (keys values)
1565 (doplist (k v '(a 1 b 2 c 3) (values t (reverse keys) (reverse values) k v))
1566 (push k keys)
1567 (push v values)))
1569 (a b c)
1570 (1 2 3)
1572 nil)