Fix autodoc argument list of remove-from-plistf and delete-from-plistf.
[alexandria.git] / tests.lisp
blob383d966985adf444ddb0544bb3c3e7f701f66e9a
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 ;;;; Control flow
38 (deftest switch.1
39 (switch (13 :test =)
40 (12 :oops)
41 (13.0 :yay))
42 :yay)
44 (deftest switch.2
45 (switch (13)
46 ((+ 12 2) :oops)
47 ((- 13 1) :oops2)
48 (t :yay))
49 :yay)
51 (deftest eswitch.1
52 (let ((x 13))
53 (eswitch (x :test =)
54 (12 :oops)
55 (13.0 :yay)))
56 :yay)
58 (deftest eswitch.2
59 (let ((x 13))
60 (eswitch (x :key 1+)
61 (11 :oops)
62 (14 :yay)))
63 :yay)
65 (deftest cswitch.1
66 (cswitch (13 :test =)
67 (12 :oops)
68 (13.0 :yay))
69 :yay)
71 (deftest cswitch.2
72 (cswitch (13 :key 1-)
73 (12 :yay)
74 (13.0 :oops))
75 :yay)
77 (deftest whichever.1
78 (let ((x (whichever 1 2 3)))
79 (and (member x '(1 2 3)) t))
82 (deftest whichever.2
83 (let* ((a 1)
84 (b 2)
85 (c 3)
86 (x (whichever a b c)))
87 (and (member x '(1 2 3)) t))
90 (deftest xor.1
91 (xor nil nil 1 nil)
95 ;;;; Definitions
97 (deftest define-constant.1
98 (let ((name (gensym)))
99 (eval `(define-constant ,name "FOO" :test equal))
100 (eval `(define-constant ,name "FOO" :test equal))
101 (values (equal "FOO" (symbol-value name))
102 (constantp name)))
106 (deftest define-constant.2
107 (let ((name (gensym)))
108 (eval `(define-constant ,name 13))
109 (eval `(define-constant ,name 13))
110 (values (eql 13 (symbol-value name))
111 (constantp name)))
115 ;;;; Errors
117 (deftest required-argument.1
118 (multiple-value-bind (res err)
119 (ignore-errors (required-argument))
120 (typep err 'error))
123 ;;;; Hash tables
125 (deftest ensure-hash-table.1
126 (let ((table (make-hash-table))
127 (x (list 1)))
128 (multiple-value-bind (value already-there)
129 (ensure-gethash x table 42)
130 (and (= value 42)
131 (not already-there)
132 (= 42 (gethash x table))
133 (multiple-value-bind (value2 already-there2)
134 (ensure-gethash x table 13)
135 (and (= value2 42)
136 already-there2
137 (= 42 (gethash x table)))))))
140 (deftest copy-hash-table.1
141 (let ((orig (make-hash-table :test 'eq :size 123))
142 (foo "foo"))
143 (setf (gethash orig orig) t
144 (gethash foo orig) t)
145 (let ((eq-copy (copy-hash-table orig))
146 (eql-copy (copy-hash-table orig :test 'eql))
147 (equal-copy (copy-hash-table orig :test 'equal))
148 (equalp-copy (copy-hash-table orig :test 'equalp)))
149 (list (hash-table-size eq-copy)
150 (hash-table-count eql-copy)
151 (gethash orig eq-copy)
152 (gethash (copy-seq foo) eql-copy)
153 (gethash foo eql-copy)
154 (gethash (copy-seq foo) equal-copy)
155 (gethash "FOO" equal-copy)
156 (gethash "FOO" equalp-copy))))
157 (123 2 t nil t t nil t))
159 (deftest maphash-keys.1
160 (let ((keys nil)
161 (table (make-hash-table)))
162 (declare (notinline maphash-keys))
163 (dotimes (i 10)
164 (setf (gethash i table) t))
165 (maphash-keys (lambda (k) (push k keys)) table)
166 (set-equal keys '(0 1 2 3 4 5 6 7 8 9)))
169 (deftest maphash-values.1
170 (let ((vals nil)
171 (table (make-hash-table)))
172 (declare (notinline maphash-values))
173 (dotimes (i 10)
174 (setf (gethash i table) (- i)))
175 (maphash-values (lambda (v) (push v vals)) table)
176 (set-equal vals '(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)))
179 (deftest hash-table-keys.1
180 (let ((table (make-hash-table)))
181 (dotimes (i 10)
182 (setf (gethash i table) t))
183 (set-equal (hash-table-keys table) '(0 1 2 3 4 5 6 7 8 9)))
186 (deftest hash-table-values.1
187 (let ((table (make-hash-table)))
188 (dotimes (i 10)
189 (setf (gethash (gensym) table) i))
190 (set-equal (hash-table-values table) '(0 1 2 3 4 5 6 7 8 9)))
193 (deftest hash-table-alist.1
194 (let ((table (make-hash-table)))
195 (dotimes (i 10)
196 (setf (gethash i table) (- i)))
197 (let ((alist (hash-table-alist table)))
198 (list (length alist)
199 (assoc 0 alist)
200 (assoc 3 alist)
201 (assoc 9 alist)
202 (assoc nil alist))))
203 (10 (0 . 0) (3 . -3) (9 . -9) nil))
205 (deftest hash-table-plist.1
206 (let ((table (make-hash-table)))
207 (dotimes (i 10)
208 (setf (gethash i table) (- i)))
209 (let ((plist (hash-table-plist table)))
210 (list (length plist)
211 (getf plist 0)
212 (getf plist 2)
213 (getf plist 7)
214 (getf plist nil))))
215 (20 0 -2 -7 nil))
217 (deftest alist-hash-table.1
218 (let* ((alist '((0 a) (1 b) (2 c)))
219 (table (alist-hash-table alist)))
220 (list (hash-table-count table)
221 (gethash 0 table)
222 (gethash 1 table)
223 (gethash 2 table)
224 (hash-table-test table)))
225 (3 (a) (b) (c) eql))
227 (deftest plist-hash-table.1
228 (let* ((plist '(:a 1 :b 2 :c 3))
229 (table (plist-hash-table plist :test 'eq)))
230 (list (hash-table-count table)
231 (gethash :a table)
232 (gethash :b table)
233 (gethash :c table)
234 (gethash 2 table)
235 (gethash nil table)
236 (hash-table-test table)))
237 (3 1 2 3 nil nil eq))
239 ;;;; Functions
241 (deftest disjoin.1
242 (let ((disjunction (disjoin (lambda (x)
243 (and (consp x) :cons))
244 (lambda (x)
245 (and (stringp x) :string)))))
246 (list (funcall disjunction 'zot)
247 (funcall disjunction '(foo bar))
248 (funcall disjunction "test")))
249 (nil :cons :string))
251 (deftest conjoin.1
252 (let ((conjunction (conjoin #'consp
253 (lambda (x)
254 (stringp (car x)))
255 (lambda (x)
256 (char (car x) 0)))))
257 (list (funcall conjunction 'zot)
258 (funcall conjunction '(foo))
259 (funcall conjunction '("foo"))))
260 (nil nil #\f))
262 (deftest compose.1
263 (let ((composite (compose '1+
264 (lambda (x)
265 (* x 2))
266 #'read-from-string)))
267 (funcall composite "1"))
270 (deftest compose.2
271 (let ((composite
272 (locally (declare (notinline compose))
273 (compose '1+
274 (lambda (x)
275 (* x 2))
276 #'read-from-string))))
277 (funcall composite "2"))
280 (deftest compose.3
281 (let ((compose-form (funcall (compiler-macro-function 'compose)
282 '(compose '1+
283 (lambda (x)
284 (* x 2))
285 #'read-from-string)
286 nil)))
287 (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
288 (funcall fun "3")))
291 (deftest multiple-value-compose.1
292 (let ((composite (multiple-value-compose
293 #'truncate
294 (lambda (x y)
295 (values y x))
296 (lambda (x)
297 (with-input-from-string (s x)
298 (values (read s) (read s)))))))
299 (multiple-value-list (funcall composite "2 7")))
300 (3 1))
302 (deftest multiple-value-compose.2
303 (let ((composite (locally (declare (notinline multiple-value-compose))
304 (multiple-value-compose
305 #'truncate
306 (lambda (x y)
307 (values y x))
308 (lambda (x)
309 (with-input-from-string (s x)
310 (values (read s) (read s))))))))
311 (multiple-value-list (funcall composite "2 11")))
312 (5 1))
314 (deftest multiple-value-compose.3
315 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose)
316 '(multiple-value-compose
317 #'truncate
318 (lambda (x y)
319 (values y x))
320 (lambda (x)
321 (with-input-from-string (s x)
322 (values (read s) (read s)))))
323 nil)))
324 (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
325 (multiple-value-list (funcall fun "2 9"))))
326 (4 1))
328 (deftest curry.1
329 (let ((curried (curry '+ 3)))
330 (funcall curried 1 5))
333 (deftest curry.2
334 (let ((curried (locally (declare (notinline curry))
335 (curry '* 2 3))))
336 (funcall curried 7))
339 (deftest curry.3
340 (let ((curried-form (funcall (compiler-macro-function 'curry)
341 '(curry '/ 8)
342 nil)))
343 (let ((fun (funcall (compile nil `(lambda () ,curried-form)))))
344 (funcall fun 2)))
347 (deftest rcurry.1
348 (let ((r (rcurry '/ 2)))
349 (funcall r 8))
352 (deftest named-lambda.1
353 (let ((fac (named-lambda fac (x)
354 (if (> x 1)
355 (* x (fac (- x 1)))
356 x))))
357 (funcall fac 5))
358 120)
360 (deftest named-lambda.2
361 (let ((fac (named-lambda fac (&key x)
362 (if (> x 1)
363 (* x (fac :x (- x 1)))
364 x))))
365 (funcall fac :x 5))
366 120)
368 ;;;; Lists
370 (deftest alist-plist.1
371 (alist-plist '((a . 1) (b . 2) (c . 3)))
372 (a 1 b 2 c 3))
374 (deftest plist-alist.1
375 (plist-alist '(a 1 b 2 c 3))
376 ((a . 1) (b . 2) (c . 3)))
378 (deftest unionf.1
379 (let* ((list (list 1 2 3))
380 (orig list))
381 (unionf list (list 1 2 4))
382 (values (equal orig (list 1 2 3))
383 (eql (length list) 4)
384 (set-difference list (list 1 2 3 4))
385 (set-difference (list 1 2 3 4) list)))
389 nil)
391 (deftest nunionf.1
392 (let ((list (list 1 2 3)))
393 (nunionf list (list 1 2 4))
394 (values (eql (length list) 4)
395 (set-difference (list 1 2 3 4) list)
396 (set-difference list (list 1 2 3 4))))
399 nil)
401 (deftest appendf.1
402 (let* ((list (list 1 2 3))
403 (orig list))
404 (appendf list '(4 5 6) '(7 8))
405 (list list (eq list orig)))
406 ((1 2 3 4 5 6 7 8) nil))
408 (deftest nconcf.1
409 (let ((list1 (list 1 2 3))
410 (list2 (list 4 5 6)))
411 (nconcf list1 list2 (list 7 8 9))
412 list1)
413 (1 2 3 4 5 6 7 8 9))
415 (deftest circular-list.1
416 (let ((circle (circular-list 1 2 3)))
417 (list (first circle)
418 (second circle)
419 (third circle)
420 (fourth circle)
421 (eq circle (nthcdr 3 circle))))
422 (1 2 3 1 t))
424 (deftest circular-list-p.1
425 (let* ((circle (circular-list 1 2 3 4))
426 (tree (list circle circle))
427 (dotted (cons circle t))
428 (proper (list 1 2 3 circle))
429 (tailcirc (list* 1 2 3 circle)))
430 (list (circular-list-p circle)
431 (circular-list-p tree)
432 (circular-list-p dotted)
433 (circular-list-p proper)
434 (circular-list-p tailcirc)))
435 (t nil nil nil t))
437 (deftest circular-list-p.2
438 (circular-list-p 'foo)
439 nil)
441 (deftest circular-tree-p.1
442 (let* ((circle (circular-list 1 2 3 4))
443 (tree1 (list circle circle))
444 (tree2 (let* ((level2 (list 1 nil 2))
445 (level1 (list level2)))
446 (setf (second level2) level1)
447 level1))
448 (dotted (cons circle t))
449 (proper (list 1 2 3 circle))
450 (tailcirc (list* 1 2 3 circle))
451 (quite-proper (list 1 2 3))
452 (quite-dotted (list 1 (cons 2 3))))
453 (list (circular-tree-p circle)
454 (circular-tree-p tree1)
455 (circular-tree-p tree2)
456 (circular-tree-p dotted)
457 (circular-tree-p proper)
458 (circular-tree-p tailcirc)
459 (circular-tree-p quite-proper)
460 (circular-tree-p quite-dotted)))
461 (t t t t t t nil nil))
463 (deftest proper-list-p.1
464 (let ((l1 (list 1))
465 (l2 (list 1 2))
466 (l3 (cons 1 2))
467 (l4 (list (cons 1 2) 3))
468 (l5 (circular-list 1 2)))
469 (list (proper-list-p l1)
470 (proper-list-p l2)
471 (proper-list-p l3)
472 (proper-list-p l4)
473 (proper-list-p l5)))
474 (t t nil t nil))
476 (deftest proper-list-p.2
477 (proper-list-p '(1 2 . 3))
478 nil)
480 (deftest proper-list.type.1
481 (let ((l1 (list 1))
482 (l2 (list 1 2))
483 (l3 (cons 1 2))
484 (l4 (list (cons 1 2) 3))
485 (l5 (circular-list 1 2)))
486 (list (typep l1 'proper-list)
487 (typep l2 'proper-list)
488 (typep l3 'proper-list)
489 (typep l4 'proper-list)
490 (typep l5 'proper-list)))
491 (t t nil t nil))
493 (deftest lastcar.1
494 (let ((l1 (list 1))
495 (l2 (list 1 2)))
496 (list (lastcar l1)
497 (lastcar l2)))
498 (1 2))
500 (deftest lastcar.error.2
501 (handler-case
502 (progn
503 (lastcar (circular-list 1 2 3))
504 nil)
505 (error ()
509 (deftest setf-lastcar.1
510 (let ((l (list 1 2 3 4)))
511 (values (lastcar l)
512 (progn
513 (setf (lastcar l) 42)
514 (lastcar l))))
518 (deftest setf-lastcar.2
519 (let ((l (circular-list 1 2 3)))
520 (multiple-value-bind (res err)
521 (ignore-errors (setf (lastcar l) 4))
522 (typep err 'type-error)))
525 (deftest make-circular-list.1
526 (let ((l (make-circular-list 3 :initial-element :x)))
527 (setf (car l) :y)
528 (list (eq l (nthcdr 3 l))
529 (first l)
530 (second l)
531 (third l)
532 (fourth l)))
533 (t :y :x :x :y))
535 (deftest circular-list.type.1
536 (let* ((l1 (list 1 2 3))
537 (l2 (circular-list 1 2 3))
538 (l3 (list* 1 2 3 l2)))
539 (list (typep l1 'circular-list)
540 (typep l2 'circular-list)
541 (typep l3 'circular-list)))
542 (nil t t))
544 (deftest ensure-list.1
545 (let ((x (list 1))
546 (y 2))
547 (list (ensure-list x)
548 (ensure-list y)))
549 ((1) (2)))
551 (deftest ensure-cons.1
552 (let ((x (cons 1 2))
553 (y nil)
554 (z "foo"))
555 (values (ensure-cons x)
556 (ensure-cons y)
557 (ensure-cons z)))
558 (1 . 2)
559 (nil)
560 ("foo"))
562 (deftest setp.1
563 (setp '(1))
566 (deftest setp.2
567 (setp nil)
570 (deftest setp.3
571 (setp "foo")
572 nil)
574 (deftest setp.4
575 (setp '(1 2 3 1))
576 nil)
578 (deftest setp.5
579 (setp '(1 2 3))
582 (deftest setp.6
583 (setp '(a :a))
586 (deftest setp.7
587 (setp '(a :a) :key 'character)
588 nil)
590 (deftest setp.8
591 (setp '(a :a) :key 'character :test (constantly nil))
594 (deftest set-equal.1
595 (set-equal '(1 2 3) '(3 1 2))
598 (deftest set-equal.2
599 (set-equal '("Xa") '("Xb")
600 :test (lambda (a b) (eql (char a 0) (char b 0))))
603 (deftest set-equal.3
604 (set-equal '(1 2) '(4 2))
605 nil)
607 (deftest set-equal.4
608 (set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal)
611 (deftest set-equal.5
612 (set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal)
613 nil)
615 (deftest set-equal.6
616 (set-equal '(a b c) '(a b c d))
617 nil)
619 (deftest map-product.1
620 (map-product 'cons '(2 3) '(1 4))
621 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
623 (deftest map-product.2
624 (map-product #'cons '(2 3) '(1 4))
625 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
627 (deftest flatten.1
628 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
629 (1 2 3 4 5 6 7))
631 (deftest remove-from-plist.1
632 (let ((orig '(a 1 b 2 c 3 d 4)))
633 (list (remove-from-plist orig 'a 'c)
634 (remove-from-plist orig 'b 'd)
635 (remove-from-plist orig 'b)
636 (remove-from-plist orig 'a)
637 (remove-from-plist orig 'd 42 "zot")
638 (remove-from-plist orig 'a 'b 'c 'd)
639 (remove-from-plist orig 'a 'b 'c 'd 'x)
640 (equal orig '(a 1 b 2 c 3 d 4))))
641 ((b 2 d 4)
642 (a 1 c 3)
643 (a 1 c 3 d 4)
644 (b 2 c 3 d 4)
645 (a 1 b 2 c 3)
650 (deftest mappend.1
651 (mappend (compose 'list '*) '(1 2 3) '(1 2 3))
652 (1 4 9))
654 ;;;; Numbers
656 (deftest clamp.1
657 (list (clamp 1.5 1 2)
658 (clamp 2.0 1 2)
659 (clamp 1.0 1 2)
660 (clamp 3 1 2)
661 (clamp 0 1 2))
662 (1.5 2.0 1.0 2 1))
664 (deftest gaussian-random.1
665 (let ((min -0.2)
666 (max +0.2))
667 (multiple-value-bind (g1 g2)
668 (gaussian-random min max)
669 (values (<= min g1 max)
670 (<= min g2 max)
671 (/= g1 g2) ;uh
677 (deftest iota.1
678 (iota 3)
679 (0 1 2))
681 (deftest iota.2
682 (iota 3 :start 0.0d0)
683 (0.0d0 1.0d0 2.0d0))
685 (deftest iota.3
686 (iota 3 :start 2 :step 3.0)
687 (2.0 5.0 8.0))
689 (deftest map-iota.1
690 (let (all)
691 (declare (notinline map-iota))
692 (values (map-iota (lambda (x) (push x all))
694 :start 2
695 :step 1.1d0)
696 all))
698 (4.2d0 3.1d0 2.0d0))
700 (deftest lerp.1
701 (lerp 0.5 1 2)
702 1.5)
704 (deftest lerp.2
705 (lerp 0.1 1 2)
706 1.1)
708 (deftest mean.1
709 (mean '(1 2 3))
712 (deftest mean.2
713 (mean '(1 2 3 4))
714 5/2)
716 (deftest mean.3
717 (mean '(1 2 10))
718 13/3)
720 (deftest median.1
721 (median '(100 0 99 1 98 2 97))
724 (deftest median.2
725 (median '(100 0 99 1 98 2 97 96))
726 195/2)
728 (deftest variance.1
729 (variance (list 1 2 3))
730 2/3)
732 (deftest standard-deviation.1
733 (< 0 (standard-deviation (list 1 2 3)) 1)
736 (deftest maxf.1
737 (let ((x 1))
738 (maxf x 2)
742 (deftest maxf.2
743 (let ((x 1))
744 (maxf x 0)
748 (deftest maxf.3
749 (let ((x 1)
750 (c 0))
751 (maxf x (incf c))
752 (list x c))
753 (1 1))
755 (deftest maxf.4
756 (let ((xv (vector 0 0 0))
757 (p 0))
758 (maxf (svref xv (incf p)) (incf p))
759 (list p xv))
760 (2 #(0 2 0)))
762 (deftest minf.1
763 (let ((y 1))
764 (minf y 0)
768 (deftest minf.2
769 (let ((xv (vector 10 10 10))
770 (p 0))
771 (minf (svref xv (incf p)) (incf p))
772 (list p xv))
773 (2 #(10 2 10)))
775 ;;;; Arrays
777 #+nil
778 (deftest array-index.type)
780 #+nil
781 (deftest copy-array)
783 ;;;; Sequences
785 (deftest rotate.1
786 (list (rotate (list 1 2 3) 0)
787 (rotate (list 1 2 3) 1)
788 (rotate (list 1 2 3) 2)
789 (rotate (list 1 2 3) 3)
790 (rotate (list 1 2 3) 4))
791 ((1 2 3)
792 (3 1 2)
793 (2 3 1)
794 (1 2 3)
795 (3 1 2)))
797 (deftest rotate.2
798 (list (rotate (vector 1 2 3 4) 0)
799 (rotate (vector 1 2 3 4))
800 (rotate (vector 1 2 3 4) 2)
801 (rotate (vector 1 2 3 4) 3)
802 (rotate (vector 1 2 3 4) 4)
803 (rotate (vector 1 2 3 4) 5))
804 (#(1 2 3 4)
805 #(4 1 2 3)
806 #(3 4 1 2)
807 #(2 3 4 1)
808 #(1 2 3 4)
809 #(4 1 2 3)))
811 (deftest rotate.3
812 (list (rotate (list 1 2 3) 0)
813 (rotate (list 1 2 3) -1)
814 (rotate (list 1 2 3) -2)
815 (rotate (list 1 2 3) -3)
816 (rotate (list 1 2 3) -4))
817 ((1 2 3)
818 (2 3 1)
819 (3 1 2)
820 (1 2 3)
821 (2 3 1)))
823 (deftest rotate.4
824 (list (rotate (vector 1 2 3 4) 0)
825 (rotate (vector 1 2 3 4) -1)
826 (rotate (vector 1 2 3 4) -2)
827 (rotate (vector 1 2 3 4) -3)
828 (rotate (vector 1 2 3 4) -4)
829 (rotate (vector 1 2 3 4) -5))
830 (#(1 2 3 4)
831 #(2 3 4 1)
832 #(3 4 1 2)
833 #(4 1 2 3)
834 #(1 2 3 4)
835 #(2 3 4 1)))
837 (deftest rotate.5
838 (values (rotate (list 1) 17)
839 (rotate (list 1) -5))
841 (1))
843 (deftest shuffle.1
844 (let ((s (shuffle (iota 100))))
845 (list (equal s (iota 100))
846 (every (lambda (x)
847 (member x s))
848 (iota 100))
849 (every (lambda (x)
850 (typep x '(integer 0 99)))
851 s)))
852 (nil t t))
854 (deftest shuffle.2
855 (let ((s (shuffle (coerce (iota 100) 'vector))))
856 (list (equal s (coerce (iota 100) 'vector))
857 (every (lambda (x)
858 (find x s))
859 (iota 100))
860 (every (lambda (x)
861 (typep x '(integer 0 99)))
862 s)))
863 (nil t t))
865 (deftest random-elt.1
866 (let ((s1 #(1 2 3 4))
867 (s2 '(1 2 3 4)))
868 (list (dotimes (i 1000 nil)
869 (unless (member (random-elt s1) s2)
870 (return nil))
871 (when (/= (random-elt s1) (random-elt s1))
872 (return t)))
873 (dotimes (i 1000 nil)
874 (unless (member (random-elt s2) s2)
875 (return nil))
876 (when (/= (random-elt s2) (random-elt s2))
877 (return t)))))
878 (t t))
880 (deftest removef.1
881 (let* ((x '(1 2 3))
882 (x* x)
883 (y #(1 2 3))
884 (y* y))
885 (removef x 1)
886 (removef y 3)
887 (list x x* y y*))
888 ((2 3)
889 (1 2 3)
890 #(1 2)
891 #(1 2 3)))
893 (deftest deletef.1
894 (let* ((x (list 1 2 3))
895 (x* x)
896 (y (vector 1 2 3)))
897 (deletef x 2)
898 (deletef y 1)
899 (list x x* y))
900 ((1 3)
901 (1 3)
902 #(2 3)))
904 (deftest proper-sequence.type.1
905 (mapcar (lambda (x)
906 (typep x 'proper-sequence))
907 (list (list 1 2 3)
908 (vector 1 2 3)
909 #2a((1 2) (3 4))
910 (circular-list 1 2 3 4)))
911 (t t nil nil))
913 (deftest emptyp.1
914 (mapcar #'emptyp
915 (list (list 1)
916 (circular-list 1)
918 (vector)
919 (vector 1)))
920 (nil nil t t nil))
922 (deftest sequence-of-length-p.1
923 (mapcar #'sequence-of-length-p
924 (list nil
926 (list 1)
927 (vector 1)
928 (list 1 2)
929 (vector 1 2)
930 (list 1 2)
931 (vector 1 2)
932 (list 1 2)
933 (vector 1 2))
934 (list 0
944 (t t t t t t nil nil nil nil))
946 (deftest copy-sequence.1
947 (let ((l (list 1 2 3))
948 (v (vector #\a #\b #\c)))
949 (declare (notinline copy-sequence))
950 (let ((l.list (copy-sequence 'list l))
951 (l.vector (copy-sequence 'vector l))
952 (l.spec-v (copy-sequence '(vector fixnum) l))
953 (v.vector (copy-sequence 'vector v))
954 (v.list (copy-sequence 'list v))
955 (v.string (copy-sequence 'string v)))
956 (list (member l (list l.list l.vector l.spec-v))
957 (member v (list v.vector v.list v.string))
958 (equal l.list l)
959 (equalp l.vector #(1 2 3))
960 (eq 'fixnum (array-element-type l.spec-v))
961 (equalp v.vector v)
962 (equal v.list '(#\a #\b #\c))
963 (equal "abc" v.string))))
964 (nil nil t t t t t t))
966 (deftest first-elt.1
967 (mapcar #'first-elt
968 (list (list 1 2 3)
969 "abc"
970 (vector :a :b :c)))
971 (1 #\a :a))
973 (deftest first-elt.error.1
974 (mapcar (lambda (x)
975 (handler-case
976 (first-elt x)
977 (type-error ()
978 :type-error)))
979 (list nil
982 :zot))
983 (:type-error
984 :type-error
985 :type-error
986 :type-error))
988 (deftest setf-first-elt.1
989 (let ((l (list 1 2 3))
990 (s (copy-seq "foobar"))
991 (v (vector :a :b :c)))
992 (setf (first-elt l) -1
993 (first-elt s) #\x
994 (first-elt v) 'zot)
995 (values l s v))
996 (-1 2 3)
997 "xoobar"
998 #(zot :b :c))
1000 (deftest setf-first-elt.error.1
1001 (let ((l 'foo))
1002 (multiple-value-bind (res err)
1003 (ignore-errors (setf (first-elt l) 4))
1004 (typep err 'type-error)))
1007 (deftest last-elt.1
1008 (mapcar #'last-elt
1009 (list (list 1 2 3)
1010 (vector :a :b :c)
1011 "FOOBAR"
1012 #*001
1013 #*010))
1014 (3 :c #\R 1 0))
1016 (deftest last-elt.error.1
1017 (mapcar (lambda (x)
1018 (handler-case
1019 (last-elt x)
1020 (type-error ()
1021 :type-error)))
1022 (list nil
1025 :zot
1026 (circular-list 1 2 3)
1027 (list* 1 2 3 (circular-list 4 5))))
1028 (:type-error
1029 :type-error
1030 :type-error
1031 :type-error
1032 :type-error
1033 :type-error))
1035 (deftest setf-last-elt.1
1036 (let ((l (list 1 2 3))
1037 (s (copy-seq "foobar"))
1038 (b (copy-seq #*010101001)))
1039 (setf (last-elt l) '???
1040 (last-elt s) #\?
1041 (last-elt b) 0)
1042 (values l s b))
1043 (1 2 ???)
1044 "fooba?"
1045 #*010101000)
1047 (deftest setf-last-elt.error.1
1048 (handler-case
1049 (setf (last-elt 'foo) 13)
1050 (type-error ()
1051 :type-error))
1052 :type-error)
1054 (deftest starts-with.1
1055 (list (starts-with 1 '(1 2 3))
1056 (starts-with 1 #(1 2 3))
1057 (starts-with #\x "xyz")
1058 (starts-with 2 '(1 2 3))
1059 (starts-with 3 #(1 2 3))
1060 (starts-with 1 1)
1061 (starts-with nil nil))
1062 (t t t nil nil nil nil))
1064 (deftest starts-with.2
1065 (values (starts-with 1 '(-1 2 3) :key '-)
1066 (starts-with "foo" '("foo" "bar") :test 'equal)
1067 (starts-with "f" '(#\f) :key 'string :test 'equal)
1068 (starts-with -1 '(0 1 2) :key #'1+)
1069 (starts-with "zot" '("ZOT") :test 'equal))
1074 nil)
1076 (deftest ends-with.1
1077 (list (ends-with 3 '(1 2 3))
1078 (ends-with 3 #(1 2 3))
1079 (ends-with #\z "xyz")
1080 (ends-with 2 '(1 2 3))
1081 (ends-with 1 #(1 2 3))
1082 (ends-with 1 1)
1083 (ends-with nil nil))
1084 (t t t nil nil nil nil))
1086 (deftest ends-with.2
1087 (values (ends-with 2 '(0 13 1) :key '1+)
1088 (ends-with "foo" (vector "bar" "foo") :test 'equal)
1089 (ends-with "X" (vector 1 2 #\X) :key 'string :test 'equal)
1090 (ends-with "foo" "foo" :test 'equal))
1094 nil)
1096 (deftest ends-with.error.1
1097 (handler-case
1098 (ends-with 3 (circular-list 3 3 3 1 3 3))
1099 (type-error ()
1100 :type-error))
1101 :type-error)
1103 (deftest with-unique-names.1
1104 (let ((*gensym-counter* 0))
1105 (let ((syms (with-unique-names (foo bar quux)
1106 (list foo bar quux))))
1107 (list (find-if #'symbol-package syms)
1108 (equal '("FOO0" "BAR1" "QUUX2")
1109 (mapcar #'symbol-name syms)))))
1110 (nil t))
1112 (deftest with-unique-names.2
1113 (let ((*gensym-counter* 0))
1114 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-) (quux #\q))
1115 (list foo bar quux))))
1116 (list (find-if #'symbol-package syms)
1117 (equal '("_foo_0" "-BAR-1" "q2")
1118 (mapcar #'symbol-name syms)))))
1119 (nil t))
1121 (deftest with-unique-names.3
1122 (let ((*gensym-counter* 0))
1123 (multiple-value-bind (res err)
1124 (ignore-errors
1125 (eval
1126 '(let ((syms
1127 (with-unique-names ((foo "_foo_") (bar -bar-) (quux 42))
1128 (list foo bar quux))))
1129 (list (find-if #'symbol-package syms)
1130 (equal '("_foo_0" "-BAR-1" "q2")
1131 (mapcar #'symbol-name syms))))))
1132 (typep err 'error)))
1135 (deftest once-only.1
1136 (macrolet ((cons1.good (x)
1137 (once-only (x)
1138 `(cons ,x ,x)))
1139 (cons1.bad (x)
1140 `(cons ,x ,x)))
1141 (let ((y 0))
1142 (list (cons1.good (incf y))
1144 (cons1.bad (incf y))
1145 y)))
1146 ((1 . 1) 1 (2 . 3) 3))
1148 (deftest once-only.2
1149 (macrolet ((cons1 (x)
1150 (once-only ((y x))
1151 `(cons ,y ,y))))
1152 (let ((z 0))
1153 (list (cons1 (incf z))
1155 (cons1 (incf z)))))
1156 ((1 . 1) 1 (2 . 2)))
1158 (deftest parse-body.1
1159 (parse-body '("doc" "body") :documentation t)
1160 ("body")
1162 "doc")
1164 (deftest parse-body.2
1165 (parse-body '("body") :documentation t)
1166 ("body")
1168 nil)
1170 (deftest parse-body.3
1171 (parse-body '("doc" "body"))
1172 ("doc" "body")
1174 nil)
1176 (deftest parse-body.4
1177 (parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t)
1178 (body)
1179 ((declare (foo)) (declare (bar)))
1180 "doc")
1182 (deftest parse-body.5
1183 (parse-body '((declare (foo)) "doc" (declare (bar)) body))
1184 ("doc" (declare (bar)) body)
1185 ((declare (foo)))
1186 nil)
1188 (deftest parse-body.6
1189 (multiple-value-bind (res err)
1190 (ignore-errors
1191 (parse-body '("foo" "bar" "quux")
1192 :documentation t))
1193 (typep err 'error))
1196 ;;;; Symbols
1198 (deftest ensure-symbol.1
1199 (ensure-symbol :cons :cl)
1200 cons
1201 :external)
1203 (deftest ensure-symbol.2
1204 (ensure-symbol "CONS" :alexandria)
1205 cons
1206 :inherited)
1208 (deftest ensure-symbol.3
1209 (ensure-symbol 'foo :keyword)
1210 :foo
1211 :external)
1213 (deftest ensure-symbol.4
1214 (ensure-symbol #\* :alexandria)
1216 :inherited)
1218 (deftest format-symbol.1
1219 (let ((s (format-symbol nil "X-~D" 13)))
1220 (list (symbol-package s)
1221 (symbol-name s)))
1222 (nil "X-13"))
1224 (deftest format-symbol.2
1225 (format-symbol :keyword "SYM-~A" :bolic)
1226 :sym-bolic)
1228 (deftest format-symbol.3
1229 (let ((*package* (find-package :cl)))
1230 (format-symbol t "FIND-~A" 'package))
1231 find-package)
1233 (deftest make-keyword.1
1234 (list (make-keyword 'zot)
1235 (make-keyword "FOO")
1236 (make-keyword #\Q))
1237 (:zot :foo :q))
1239 (deftest make-gensym-list.1
1240 (let ((*gensym-counter* 0))
1241 (let ((syms (make-gensym-list 3 "FOO")))
1242 (list (find-if 'symbol-package syms)
1243 (equal '("FOO0" "FOO1" "FOO2")
1244 (mapcar 'symbol-name syms)))))
1245 (nil t))
1247 (deftest make-gensym-list.2
1248 (let ((*gensym-counter* 0))
1249 (let ((syms (make-gensym-list 3)))
1250 (list (find-if 'symbol-package syms)
1251 (equal '("G0" "G1" "G2")
1252 (mapcar 'symbol-name syms)))))
1253 (nil t))
1255 ;;;; Type-system
1257 (deftest of-type.1
1258 (locally
1259 (declare (notinline of-type))
1260 (let ((f (of-type 'string)))
1261 (list (funcall f "foo")
1262 (funcall f 'bar))))
1263 (t nil))
1265 (deftest type=.1
1266 (type= 'string 'string)
1270 (deftest type=.2
1271 (type= 'list '(or null cons))
1275 (deftest type=.3
1276 (type= 'null '(and symbol list))
1280 (deftest type=.4
1281 (type= 'string '(satisfies emptyp))
1283 nil)
1285 (deftest type=.5
1286 (type= 'string 'list)
1290 ;;;; Bindings
1292 (declaim (notinline opaque))
1293 (defun opaque (x)
1296 (deftest if-let.1
1297 (if-let (x (opaque :ok))
1299 :bad)
1300 :ok)
1302 (deftest if-let.2
1303 (if-let (x (opaque nil))
1304 :bad
1305 (and (not x) :ok))
1306 :ok)
1308 (deftest if-let.3
1309 (let ((x 1))
1310 (if-let ((x 2)
1311 (y x))
1312 (+ x y)
1313 :oops))
1316 (deftest if-let.4
1317 (if-let ((x 1)
1318 (y nil))
1319 :oops
1320 (and (not y) x))
1323 (deftest if-let.5
1324 (if-let (x)
1325 :oops
1326 (not x))
1329 (deftest if-let.error.1
1330 (handler-case
1331 (eval '(if-let x
1332 :oops
1333 :oops))
1334 (type-error ()
1335 :type-error))
1336 :type-error)
1338 (deftest if-let*.1
1339 (let ((x 1))
1340 (if-let* ((x 2)
1341 (y x))
1342 (+ x y)
1343 :oops))
1346 (deftest if-let*.2
1347 (if-let* ((x 2)
1348 (y (prog1 x (setf x nil))))
1349 :oops
1350 (and (not x) y))
1353 (deftest if-let*.3
1354 (if-let* (x 1)
1356 :oops)
1359 (deftest if-let*.error.1
1360 (handler-case
1361 (eval '(if-let* x :oops :oops))
1362 (type-error ()
1363 :type-error))
1364 :type-error)
1366 (deftest when-let.1
1367 (when-let (x (opaque :ok))
1368 (setf x (cons x x))
1370 (:ok . :ok))
1372 (deftest when-let.2
1373 (when-let ((x 1)
1374 (y nil)
1375 (z 3))
1376 :oops)
1377 nil)
1379 (deftest when-let.3
1380 (let ((x 1))
1381 (when-let ((x 2)
1382 (y x))
1383 (+ x y)))
1386 (deftest when-let.error.1
1387 (handler-case
1388 (eval '(when-let x :oops))
1389 (type-error ()
1390 :type-error))
1391 :type-error)
1393 (deftest when-let*.1
1394 (let ((x 1))
1395 (when-let* ((x 2)
1396 (y x))
1397 (+ x y)))
1400 (deftest when-let*.2
1401 (let ((y 1))
1402 (when-let* (x y)
1403 (1+ x)))
1406 (deftest when-let*.error.1
1407 (handler-case
1408 (eval '(when-let* x :oops))
1409 (type-error ()
1410 :type-error))
1411 :type-error)
1413 (deftest nth-value-or.1
1414 (multiple-value-bind (a b c)
1415 (nth-value-or 1
1416 (values 1 nil 1)
1417 (values 2 2 2))
1418 (= a b c 2))
1421 (deftest doplist.1
1422 (let (keys values)
1423 (doplist (k v '(a 1 b 2 c 3) (values t (reverse keys) (reverse values) k v))
1424 (push k keys)
1425 (push v values)))
1427 (a b c)
1428 (1 2 3)
1430 nil)