Added a faster loop based remove-from-plist
[alexandria.git] / tests.lisp
blob496878f6d03f6037d2159ae566e48719f69d9fe2
1 (in-package :cl-user)
3 (require :sb-rt)
5 (require :alexandria)
7 (defpackage :alexandria-test
8 (:use :cl :alexandria :sb-rt))
10 (in-package :alexandria-test)
12 ;;;; Hash tables
14 (deftest copy-hash-table.1
15 (let ((orig (make-hash-table :test 'eq :size 123))
16 (foo "foo"))
17 (setf (gethash orig orig) t
18 (gethash foo orig) t)
19 (let ((eq-copy (copy-hash-table orig))
20 (eql-copy (copy-hash-table orig :test 'eql))
21 (equal-copy (copy-hash-table orig :test 'equal))
22 (equalp-copy (copy-hash-table orig :test 'equalp)))
23 (list (hash-table-size eq-copy)
24 (hash-table-count eql-copy)
25 (gethash orig eq-copy)
26 (gethash (copy-seq foo) eql-copy)
27 (gethash foo eql-copy)
28 (gethash (copy-seq foo) equal-copy)
29 (gethash "FOO" equal-copy)
30 (gethash "FOO" equalp-copy))))
31 (123 2 t nil t t nil t))
33 (deftest maphash-keys.1
34 (let ((keys nil)
35 (table (make-hash-table)))
36 (dotimes (i 10)
37 (setf (gethash i table) t))
38 (maphash-keys (lambda (k) (push k keys)) table)
39 (set-equal keys '(0 1 2 3 4 5 6 7 8 9)))
42 (deftest maphash-values.1
43 (let ((vals nil)
44 (table (make-hash-table)))
45 (dotimes (i 10)
46 (setf (gethash i table) (- i)))
47 (maphash-values (lambda (v) (push v vals)) table)
48 (set-equal vals '(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)))
51 (deftest hash-table-keys.1
52 (let ((table (make-hash-table)))
53 (dotimes (i 10)
54 (setf (gethash i table) t))
55 (set-equal (hash-table-keys table) '(0 1 2 3 4 5 6 7 8 9)))
58 (deftest hash-table-values.1
59 (let ((table (make-hash-table)))
60 (dotimes (i 10)
61 (setf (gethash (gensym) table) i))
62 (set-equal (hash-table-values table) '(0 1 2 3 4 5 6 7 8 9)))
65 (deftest hash-table-alist.1
66 (let ((table (make-hash-table)))
67 (dotimes (i 10)
68 (setf (gethash i table) (- i)))
69 (let ((alist (hash-table-alist table)))
70 (list (length alist)
71 (assoc 0 alist)
72 (assoc 3 alist)
73 (assoc 9 alist)
74 (assoc nil alist))))
75 (10 (0 . 0) (3 . -3) (9 . -9) nil))
77 (deftest hash-table-plist.1
78 (let ((table (make-hash-table)))
79 (dotimes (i 10)
80 (setf (gethash i table) (- i)))
81 (let ((plist (hash-table-plist table)))
82 (list (length plist)
83 (getf plist 0)
84 (getf plist 2)
85 (getf plist 7)
86 (getf plist nil))))
87 (20 0 -2 -7 nil))
89 (deftest alist-hash-table.1
90 (let* ((alist '((0 a) (1 b) (2 c)))
91 (table (alist-hash-table alist)))
92 (list (hash-table-count table)
93 (gethash 0 table)
94 (gethash 1 table)
95 (gethash 2 table)
96 (hash-table-test table)))
97 (3 (a) (b) (c) eql))
99 (deftest plist-hash-table.1
100 (let* ((plist '(:a 1 :b 2 :c 3))
101 (table (plist-hash-table plist :test 'eq)))
102 (list (hash-table-count table)
103 (gethash :a table)
104 (gethash :b table)
105 (gethash :c table)
106 (gethash 2 table)
107 (gethash nil table)
108 (hash-table-test table)))
109 (3 1 2 3 nil nil eq))
111 ;;;; Functions
113 (deftest disjoin.1
114 (let ((disjunction (disjoin (lambda (x)
115 (and (consp x) :cons))
116 (lambda (x)
117 (and (stringp x) :string)))))
118 (list (funcall disjunction 'zot)
119 (funcall disjunction '(foo bar))
120 (funcall disjunction "test")))
121 (nil :cons :string))
123 (deftest conjoin.1
124 (let ((conjunction (conjoin #'consp
125 (lambda (x)
126 (stringp (car x)))
127 (lambda (x)
128 (char (car x) 0)))))
129 (list (funcall conjunction 'zot)
130 (funcall conjunction '(foo))
131 (funcall conjunction '("foo"))))
132 (nil nil #\f))
134 (deftest compose.1
135 (let ((composite (compose '1+
136 (lambda (x)
137 (* x 2))
138 #'read-from-string)))
139 (funcall composite "1"))
142 (deftest compose.2
143 (let ((composite
144 (locally (declare (notinline compose))
145 (compose '1+
146 (lambda (x)
147 (* x 2))
148 #'read-from-string))))
149 (funcall composite "2"))
152 (deftest compose.3
153 (let ((compose-form (funcall (compiler-macro-function 'compose)
154 '(compose '1+
155 (lambda (x)
156 (* x 2))
157 #'read-from-string)
158 nil)))
159 (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
160 (funcall fun "3")))
163 (deftest multiple-value-compose.1
164 (let ((composite (multiple-value-compose
165 #'truncate
166 (lambda (x y)
167 (values y x))
168 (lambda (x)
169 (with-input-from-string (s x)
170 (values (read s) (read s)))))))
171 (multiple-value-list (funcall composite "2 7")))
172 (3 1))
174 (deftest multiple-value-compose.2
175 (let ((composite (locally (declare (notinline multiple-value-compose))
176 (multiple-value-compose
177 #'truncate
178 (lambda (x y)
179 (values y x))
180 (lambda (x)
181 (with-input-from-string (s x)
182 (values (read s) (read s))))))))
183 (multiple-value-list (funcall composite "2 11")))
184 (5 1))
186 (deftest multiple-value-compose.3
187 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose)
188 '(multiple-value-compose
189 #'truncate
190 (lambda (x y)
191 (values y x))
192 (lambda (x)
193 (with-input-from-string (s x)
194 (values (read s) (read s)))))
195 nil)))
196 (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
197 (multiple-value-list (funcall fun "2 9"))))
198 (4 1))
200 (deftest curry.1
201 (let ((curried (curry '+ 3)))
202 (funcall curried 1 5))
205 (deftest curry.2
206 (let ((curried (locally (declare (notinline curry))
207 (curry '* 2 3))))
208 (funcall curried 7))
211 (deftest curry.3
212 (let ((curried-form (funcall (compiler-macro-function 'curry)
213 '(curry '/ 8)
214 nil)))
215 (let ((fun (funcall (compile nil `(lambda () ,curried-form)))))
216 (funcall fun 2)))
219 (deftest rcurry.1
220 (let ((r (rcurry '/ 2)))
221 (funcall r 8))
224 ;;;; Lists
226 (deftest appendf.1
227 (let* ((list '(1 2 3))
228 (orig list))
229 (appendf list '(4 5 6) '(7 8))
230 (list list (eq list orig)))
231 ((1 2 3 4 5 6 7 8) nil))
233 (deftest circular-list.1
234 (let ((circle (circular-list 1 2 3)))
235 (list (first circle)
236 (second circle)
237 (third circle)
238 (fourth circle)
239 (eq circle (nthcdr 3 circle))))
240 (1 2 3 1 t))
242 (deftest circular-list-p.1
243 (let* ((circle (circular-list 1 2 3 4))
244 (tree (list circle circle))
245 (dotted (cons circle t))
246 (proper (list 1 2 3 circle))
247 (tailcirc (list* 1 2 3 circle)))
248 (list (circular-list-p circle)
249 (circular-list-p tree)
250 (circular-list-p dotted)
251 (circular-list-p proper)
252 (circular-list-p tailcirc)))
253 (t nil nil nil t))
255 (deftest circular-tree-p.1
256 (let* ((circle (circular-list 1 2 3 4))
257 (tree1 (list circle circle))
258 (tree2 (let* ((level2 (list 1 nil 2))
259 (level1 (list level2)))
260 (setf (second level2) level1)
261 level1))
262 (dotted (cons circle t))
263 (proper (list 1 2 3 circle))
264 (tailcirc (list* 1 2 3 circle))
265 (quite-proper (list 1 2 3))
266 (quite-dotted (list 1 (cons 2 3))))
267 (list (circular-tree-p circle)
268 (circular-tree-p tree1)
269 (circular-tree-p tree2)
270 (circular-tree-p dotted)
271 (circular-tree-p proper)
272 (circular-tree-p tailcirc)
273 (circular-tree-p quite-proper)
274 (circular-tree-p quite-dotted)))
275 (t t t t t t nil nil))
277 (deftest proper-list-p.1
278 (let ((l1 (list 1))
279 (l2 (list 1 2))
280 (l3 (cons 1 2))
281 (l4 (list (cons 1 2) 3))
282 (l5 (circular-list 1 2)))
283 (list (proper-list-p l1)
284 (proper-list-p l2)
285 (proper-list-p l3)
286 (proper-list-p l4)
287 (proper-list-p l5)))
288 (t t nil t nil))
290 (deftest proper-list.type.1
291 (let ((l1 (list 1))
292 (l2 (list 1 2))
293 (l3 (cons 1 2))
294 (l4 (list (cons 1 2) 3))
295 (l5 (circular-list 1 2)))
296 (list (typep l1 'proper-list)
297 (typep l2 'proper-list)
298 (typep l3 'proper-list)
299 (typep l4 'proper-list)
300 (typep l5 'proper-list)))
301 (t t nil t nil))
303 (deftest lastcar.1
304 (let ((l1 (list 1))
305 (l2 (list 1 2)))
306 (list (lastcar l1)
307 (lastcar l2)))
308 (1 2))
310 (deftest lastcar.error.2
311 (handler-case
312 (progn
313 (lastcar (circular-list 1 2 3))
314 nil)
315 (error ()
319 (deftest setf-lastcar.1
320 (let ((l (list 1 2 3 4)))
321 (values (lastcar l)
322 (progn
323 (setf (lastcar l) 42)
324 (lastcar l))))
328 (deftest make-circular-list.1
329 (let ((l (make-circular-list 3 :initial-element :x)))
330 (setf (car l) :y)
331 (list (eq l (nthcdr 3 l))
332 (first l)
333 (second l)
334 (third l)
335 (fourth l)))
336 (t :y :x :x :y))
338 (deftest circular-list.type.1
339 (let* ((l1 (list 1 2 3))
340 (l2 (circular-list 1 2 3))
341 (l3 (list* 1 2 3 l2)))
342 (list (typep l1 'circular-list)
343 (typep l2 'circular-list)
344 (typep l3 'circular-list)))
345 (nil t t))
347 (deftest ensure-list.1
348 (let ((x (list 1))
349 (y 2))
350 (list (ensure-list x)
351 (ensure-list y)))
352 ((1) (2)))
354 (deftest setp.1
355 (setp '(1))
358 (deftest setp.2
359 (setp nil)
362 (deftest setp.3
363 (setp "foo")
364 nil)
366 (deftest setp.4
367 (setp '(1 2 3 1))
368 nil)
370 (deftest setp.5
371 (setp '(1 2 3))
374 (deftest setp.6
375 (setp '(a :a))
378 (deftest setp.7
379 (setp '(a :a) :key 'character)
380 nil)
382 (deftest setp.8
383 (setp '(a :a) :key 'character :test (constantly nil))
386 (deftest set-equal.1
387 (set-equal '(1 2 3) '(3 1 2))
390 (deftest set-equal.2
391 (set-equal '("Xa") '("Xb")
392 :test (lambda (a b) (eql (char a 0) (char b 0))))
395 (deftest set-equal.3
396 (set-equal '(1 2) '(4 2))
397 nil)
399 (deftest set-equal.4
400 (set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal)
403 (deftest set-equal.5
404 (set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal)
405 nil)
407 (deftest map-product.1
408 (map-product 'cons '(2 3) '(1 4))
409 ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))
411 (deftest flatten.1
412 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
413 (1 2 3 4 5 6 7))
415 (deftest sans.1
416 (let ((orig '(a 1 b 2 c 3 d 4)))
417 (list (sans orig 'a 'c)
418 (sans orig 'b 'd)
419 (sans orig 'b)
420 (sans orig 'a)
421 (sans orig 'd 42 "zot")
422 (sans orig 'a 'b 'c 'd)
423 (sans orig 'a 'b 'c 'd 'x)
424 (equal orig '(a 1 b 2 c 3 d 4))))
425 ((b 2 d 4)
426 (a 1 c 3)
427 (a 1 c 3 d 4)
428 (b 2 c 3 d 4)
429 (a 1 b 2 c 3)
434 (deftest mappend.1
435 (mappend (compose 'list '*) '(1 2 3) '(1 2 3))
436 (1 4 9))
438 ;;;; Numbers
440 (deftest clamp.1
441 (list (clamp 1.5 1 2)
442 (clamp 2.0 1 2)
443 (clamp 1.0 1 2)
444 (clamp 3 1 2)
445 (clamp 0 1 2))
446 (1.5 2.0 1.0 2 1))
448 #+(or)
449 (deftest gaussian-random.1
453 (deftest iota.1
454 (iota 3)
455 (0 1 2))
457 (deftest iota.2
458 (iota 3 :start 0.0d0)
459 (0.0d0 1.0d0 2.0d0))
461 (deftest iota.3
462 (iota 3 :start 2 :step 3.0)
463 (2.0 5.0 8.0))
465 (deftest lerp.1
466 (lerp 0.5 1 2)
467 1.5)
469 (deftest lerp.2
470 (lerp 0.1 1 2)
471 1.1)
473 (deftest mean.1
474 (mean '(1 2 3))
477 (deftest mean.2
478 (mean '(1 2 3 4))
479 5/2)
481 (deftest mean.3
482 (mean '(1 2 10))
483 13/3)
485 (deftest median.1
486 (median '(100 0 99 1 98 2 97))
489 (deftest median.2
490 (median '(100 0 99 1 98 2 97 96))
491 195/2)
493 #+(or)
494 (deftest variance)
496 #+nil
497 (deftest standard-deviation)
499 (deftest maxf.1
500 (let ((x 1))
501 (maxf x 2)
505 (deftest maxf.2
506 (let ((x 1))
507 (maxf x 0)
511 (deftest maxf.3
512 (let ((x 1)
513 (c 0))
514 (maxf x (incf c))
515 (list x c))
516 (1 1))
518 (deftest maxf.4
519 (let ((xv (vector 0 0 0))
520 (p 0))
521 (maxf (svref xv (incf p)) (incf p))
522 (list p xv))
523 (2 #(0 2 0)))
525 (deftest minf.1
526 (let ((y 1))
527 (minf y 0)
531 (deftest minf.2
532 (let ((xv (vector 10 10 10))
533 (p 0))
534 (minf (svref xv (incf p)) (incf p))
535 (list p xv))
536 (2 #(10 2 10)))
538 ;;;; Arrays
540 #+nil
541 (deftest array-index.type)
543 #+nil
544 (deftest copy-array)
546 ;;;; Sequences
548 (deftest rotate.1
549 (list (rotate (list 1 2 3) 0)
550 (rotate (list 1 2 3) 1)
551 (rotate (list 1 2 3) 2)
552 (rotate (list 1 2 3) 3)
553 (rotate (list 1 2 3) 4))
554 ((1 2 3)
555 (3 1 2)
556 (2 3 1)
557 (1 2 3)
558 (3 1 2)))
560 (deftest rotate.2
561 (list (rotate (vector 1 2 3 4) 0)
562 (rotate (vector 1 2 3 4))
563 (rotate (vector 1 2 3 4) 2)
564 (rotate (vector 1 2 3 4) 3)
565 (rotate (vector 1 2 3 4) 4)
566 (rotate (vector 1 2 3 4) 5))
567 (#(1 2 3 4)
568 #(4 1 2 3)
569 #(3 4 1 2)
570 #(2 3 4 1)
571 #(1 2 3 4)
572 #(4 1 2 3)))
574 (deftest rotate.3
575 (list (rotate (list 1 2 3) 0)
576 (rotate (list 1 2 3) -1)
577 (rotate (list 1 2 3) -2)
578 (rotate (list 1 2 3) -3)
579 (rotate (list 1 2 3) -4))
580 ((1 2 3)
581 (2 3 1)
582 (3 1 2)
583 (1 2 3)
584 (2 3 1)))
586 (deftest rotate.4
587 (list (rotate (vector 1 2 3 4) 0)
588 (rotate (vector 1 2 3 4) -1)
589 (rotate (vector 1 2 3 4) -2)
590 (rotate (vector 1 2 3 4) -3)
591 (rotate (vector 1 2 3 4) -4)
592 (rotate (vector 1 2 3 4) -5))
593 (#(1 2 3 4)
594 #(2 3 4 1)
595 #(3 4 1 2)
596 #(4 1 2 3)
597 #(1 2 3 4)
598 #(2 3 4 1)))
600 (deftest suffle.1
601 (let ((s (suffle (iota 100))))
602 (list (equal s (iota 100))
603 (every (lambda (x)
604 (member x s))
605 (iota 100))
606 (every (lambda (x)
607 (typep x '(integer 0 99)))
608 s)))
609 (nil t t))
611 (deftest random-elt.1
612 (let ((s1 #(1 2 3 4))
613 (s2 '(1 2 3 4)))
614 (list (dotimes (i 1000 nil)
615 (unless (member (random-elt s1) s2)
616 (return nil))
617 (when (/= (random-elt s1) (random-elt s1))
618 (return t)))
619 (dotimes (i 1000 nil)
620 (unless (member (random-elt s2) s2)
621 (return nil))
622 (when (/= (random-elt s2) (random-elt s2))
623 (return t)))))
624 (t t))
626 (deftest removef.1
627 (let* ((x '(1 2 3))
628 (x* x)
629 (y #(1 2 3))
630 (y* y))
631 (removef x 1)
632 (removef y 3)
633 (list x x* y y*))
634 ((2 3)
635 (1 2 3)
636 #(1 2)
637 #(1 2 3)))
639 (deftest deletef.1
640 (let* ((x '(1 2 3))
641 (x* x)
642 (y (vector 1 2 3)))
643 (deletef x 2)
644 (deletef y 1)
645 (list x x* y))
646 ((1 3)
647 (1 3)
648 #(2 3)))
650 (deftest proper-sequence.type.1
651 (mapcar (lambda (x)
652 (typep x 'proper-sequence))
653 (list (list 1 2 3)
654 (vector 1 2 3)
655 #2a((1 2) (3 4))
656 (circular-list 1 2 3 4)))
657 (t t nil nil))
659 (deftest emptyp.1
660 (mapcar #'emptyp
661 (list (list 1)
662 (circular-list 1)
664 (vector)
665 (vector 1)))
666 (nil nil t t nil))
668 (deftest sequence-of-length-p.1
669 (mapcar #'sequence-of-length-p
670 (list nil
672 (list 1)
673 (vector 1)
674 (list 1 2)
675 (vector 1 2)
676 (list 1 2)
677 (vector 1 2)
678 (list 1 2)
679 (vector 1 2))
680 (list 0
690 (t t t t t t nil nil nil nil))
692 (deftest copy-sequence.1
693 (let ((l (list 1 2 3))
694 (v (vector #\a #\b #\c)))
695 (let ((l.list (copy-sequence 'list l))
696 (l.vector (copy-sequence 'vector l))
697 (l.spec-v (copy-sequence '(vector fixnum) l))
698 (v.vector (copy-sequence 'vector v))
699 (v.list (copy-sequence 'list v))
700 (v.string (copy-sequence 'string v)))
701 (list (member l (list l.list l.vector l.spec-v))
702 (member v (list v.vector v.list v.string))
703 (equal l.list l)
704 (equalp l.vector #(1 2 3))
705 (eq 'fixnum (array-element-type l.spec-v))
706 (equalp v.vector v)
707 (equal v.list '(#\a #\b #\c))
708 (equal "abc" v.string))))
709 (nil nil t t t t t t))
711 (deftest first-elt.1
712 (mapcar #'first-elt
713 (list (list 1 2 3)
714 "abc"
715 (vector :a :b :c)))
716 (1 #\a :a))
718 (deftest first-elt.error.1
719 (mapcar (lambda (x)
720 (handler-case
721 (first-elt x)
722 (type-error ()
723 :type-error)))
724 (list nil
727 :zot))
728 (:type-error
729 :type-error
730 :type-error
731 :type-error))
733 (deftest setf-first-elt.1
734 (let ((l (list 1 2 3))
735 (s (copy-seq "foobar"))
736 (v (vector :a :b :c)))
737 (setf (first-elt l) -1
738 (first-elt s) #\x
739 (first-elt v) 'zot)
740 (values l s v))
741 (-1 2 3)
742 "xoobar"
743 #(zot :b :c))
745 (deftest last-elt.1
746 (mapcar #'last-elt
747 (list (list 1 2 3)
748 (vector :a :b :c)
749 "FOOBAR"
750 #*001
751 #*010))
752 (3 :c #\R 1 0))
754 (deftest last-elt.error.1
755 (mapcar (lambda (x)
756 (handler-case
757 (last-elt x)
758 (type-error ()
759 :type-error)))
760 (list nil
763 :zot
764 (circular-list 1 2 3)
765 (list* 1 2 3 (circular-list 4 5))))
766 (:type-error
767 :type-error
768 :type-error
769 :type-error
770 :type-error
771 :type-error))
773 (deftest setf-last-elt.1
774 (let ((l (list 1 2 3))
775 (s (copy-seq "foobar"))
776 (b (copy-seq #*010101001)))
777 (setf (last-elt l) '???
778 (last-elt s) #\?
779 (last-elt b) 0)
780 (values l s b))
781 (1 2 ???)
782 "fooba?"
783 #*010101000)
785 (deftest starts-with.1
786 (list (starts-with 1 '(1 2 3))
787 (starts-with 1 #(1 2 3))
788 (starts-with #\x "xyz")
789 (starts-with 2 '(1 2 3))
790 (starts-with 3 #(1 2 3))
791 (starts-with 1 1)
792 (starts-with nil nil))
793 (t t t nil nil nil nil))
795 (deftest starts-with.2
796 (values (starts-with 1 '(-1 2 3) :key '-)
797 (starts-with "foo" '("foo" "bar") :test 'equal)
798 (starts-with "f" '(#\f) :key 'string :test 'equal)
799 (starts-with -1 '(0 1 2) :key #'1+)
800 (starts-with "zot" '("ZOT") :test 'equal))
805 nil)
807 (deftest ends-with.1
808 (list (ends-with 3 '(1 2 3))
809 (ends-with 3 #(1 2 3))
810 (ends-with #\z "xyz")
811 (ends-with 2 '(1 2 3))
812 (ends-with 1 #(1 2 3))
813 (ends-with 1 1)
814 (ends-with nil nil))
815 (t t t nil nil nil nil))
817 (deftest ends-with.2
818 (values (ends-with 2 '(0 13 1) :key '1+)
819 (ends-with "foo" (vector "bar" "foo") :test 'equal)
820 (ends-with "X" (vector 1 2 #\X) :key 'string :test 'equal)
821 (ends-with "foo" "foo" :test 'equal))
825 nil)
827 (deftest ends-with.error.1
828 (handler-case
829 (ends-with 3 (circular-list 3 3 3 1 3 3))
830 (type-error ()
831 :type-error))
832 :type-error)
834 (deftest with-unique-names.1
835 (let ((*gensym-counter* 0))
836 (let ((syms (with-unique-names (foo bar quux)
837 (list foo bar quux))))
838 (list (find-if #'symbol-package syms)
839 (equal '("FOO0" "BAR1" "QUUX2")
840 (mapcar #'symbol-name syms)))))
841 (nil t))
843 (deftest once-only.1
844 (macrolet ((cons1.good (x)
845 (once-only (x)
846 `(cons ,x ,x)))
847 (cons1.bad (x)
848 `(cons ,x ,x)))
849 (let ((y 0))
850 (list (cons1.good (incf y))
852 (cons1.bad (incf y))
853 y)))
854 ((1 . 1) 1 (2 . 3) 3))
856 (deftest parse-body.1
857 (parse-body '("doc" "body") :documentation t)
858 ("body")
859 nil
860 "doc")
862 (deftest parse-body.2
863 (parse-body '("body") :documentation t)
864 ("body")
865 nil
866 nil)
868 (deftest parse-body.3
869 (parse-body '("doc" "body"))
870 ("doc" "body")
871 nil
872 nil)
874 (deftest parse-body.4
875 (parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t)
876 (body)
877 ((declare (foo)) (declare (bar)))
878 "doc")
880 (deftest parse-body.5
881 (parse-body '((declare (foo)) "doc" (declare (bar)) body))
882 ("doc" (declare (bar)) body)
883 ((declare (foo)))
884 nil)
886 ;;;; Symbols
888 (deftest ensure-symbol.1
889 (ensure-symbol :cons :cl)
890 cons)
892 (deftest ensure-symbol.2
893 (ensure-symbol "CONS")
894 cons)
896 (deftest ensure-symbol.3
897 (ensure-symbol 'foo :keyword)
898 :foo)
900 (deftest ensure-symbol.4
901 (ensure-symbol #\*)
904 (deftest format-symbol.1
905 (let ((s (format-symbol nil "X-~D" 13)))
906 (list (symbol-package s)
907 (symbol-name s)))
908 (nil "X-13"))
910 (deftest format-symbol.2
911 (format-symbol :keyword "SYM-~A" :bolic)
912 :sym-bolic)
914 (deftest format-symbol.3
915 (let ((*package* (find-package :cl)))
916 (format-symbol t "FIND-~A" 'package))
917 find-package)
919 (deftest make-keyword.1
920 (list (make-keyword 'zot)
921 (make-keyword "FOO")
922 (make-keyword #\Q))
923 (:zot :foo :q))
925 (deftest make-gensym-list.1
926 (let ((*gensym-counter* 0))
927 (let ((syms (make-gensym-list 3 "FOO")))
928 (list (find-if 'symbol-package syms)
929 (equal '("FOO0" "FOO1" "FOO2")
930 (mapcar 'symbol-name syms)))))
931 (nil t))
933 ;;;; Type-system
935 (deftest of-type.1
936 (let ((f (of-type 'string)))
937 (list (funcall f "foo")
938 (funcall f 'bar)))
939 (t nil))
941 (deftest type=.1
942 (type= 'string 'string)
946 (deftest type=.2
947 (type= 'list '(or null cons))
951 (deftest type=.3
952 (type= 'null '(and symbol list))
956 (deftest type=.4
957 (type= 'string '(satisfies emptyp))
959 nil)
961 (deftest type=.5
962 (type= 'string 'list)
966 ;;;; Bindings
968 (declaim (notinline opaque))
969 (defun opaque (x)
972 (deftest if-let.1
973 (if-let (x (opaque :ok))
975 :bad)
976 :ok)
978 (deftest if-let.2
979 (if-let (x (opaque nil))
980 :bad
981 (and (not x) :ok))
982 :ok)
984 (deftest if-let.3
985 (let ((x 1))
986 (if-let ((x 2)
987 (y x))
988 (+ x y)
989 :oops))
992 (deftest if-let.4
993 (if-let ((x 1)
994 (y nil))
995 :oops
996 (and (not y) x))
999 (deftest if-let*.1
1000 (let ((x 1))
1001 (if-let* ((x 2)
1002 (y x))
1003 (+ x y)
1004 :oops))
1007 (deftest if-let*.2
1008 (if-let* ((x 2)
1009 (y (prog1 x (setf x nil))))
1010 :oops
1011 (and (not x) y))
1014 (deftest when-let.1
1015 (when-let (x (opaque :ok))
1016 (setf x (cons x x))
1018 (:ok . :ok))
1020 (deftest when-let.2
1021 (when-let ((x 1)
1022 (y nil)
1023 (z 3))
1024 :oops)
1025 nil)
1027 (deftest when-let.3
1028 (let ((x 1))
1029 (when-let ((x 2)
1030 (y x))
1031 (+ x y)))
1034 (deftest when-let*.1
1035 (let ((x 1))
1036 (when-let* ((x 2)
1037 (y x))
1038 (+ x y)))