tests passing
[alexandria.git] / tests.lisp
blob6a177fd034c06ccf6668911da8f570423eac7610
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 make-circular-list.1
320 (let ((l (make-circular-list 3 :initial-element :x)))
321 (setf (car l) :y)
322 (list (eq l (nthcdr 3 l))
323 (first l)
324 (second l)
325 (third l)
326 (fourth l)))
327 (t :y :x :x :y))
329 (deftest circular-list.type.1
330 (let* ((l1 (list 1 2 3))
331 (l2 (circular-list 1 2 3))
332 (l3 (list* 1 2 3 l2)))
333 (list (typep l1 'circular-list)
334 (typep l2 'circular-list)
335 (typep l3 'circular-list)))
336 (nil t t))
338 (deftest ensure-list.1
339 (let ((x (list 1))
340 (y 2))
341 (list (ensure-list x)
342 (ensure-list y)))
343 ((1) (2)))
345 (deftest remove-keys.1
346 (let* ((orig '(a 1 b 2 c 3 d 4))
347 (copy (copy-seq orig)))
348 (list (remove-keys '(a c) copy)
349 (remove-keys '(b d) copy)
350 (remove-keys '(b) copy)
351 (remove-keys '(a) copy)
352 (remove-keys '(d) copy)
353 (remove-keys '(a b c d) copy)
354 (remove-keys '(a b c d e) copy)
355 (equal copy orig)))
356 ((b 2 d 4)
357 (a 1 c 3)
358 (a 1 c 3 d 4)
359 (b 2 c 3 d 4)
360 (a 1 b 2 c 3)
365 (deftest mappend.1
366 (mappend (compose 'list '*) '(1 2 3) '(1 2 3))
367 (1 4 9))
369 ;;;; Numbers
371 (deftest clamp.1
372 (list (clamp 1.5 1 2)
373 (clamp 2.0 1 2)
374 (clamp 1.0 1 2)
375 (clamp 3 1 2)
376 (clamp 0 1 2))
377 (1.5 2.0 1.0 2 1))
379 #+(or)
380 (deftest gaussian-random.1
384 (deftest iota.1
385 (iota 3)
386 (0 1 2))
388 (deftest iota.2
389 (iota 3 :start 0.0d0)
390 (0.0d0 1.0d0 2.0d0))
392 (deftest iota.3
393 (iota 3 :start 2 :step 3.0)
394 (2.0 5.0 8.0))
396 (deftest lerp.1
397 (lerp 0.5 1 2)
398 1.5)
400 (deftest lerp.2
401 (lerp 0.1 1 2)
402 1.1)
404 (deftest mean.1
405 (mean '(1 2 3))
408 (deftest mean.2
409 (mean '(1 2 3 4))
410 5/2)
412 (deftest mean.3
413 (mean '(1 2 10))
414 13/3)
416 (deftest median.1
417 (median '(100 0 99 1 98 2 97))
420 (deftest median.2
421 (median '(100 0 99 1 98 2 97 96))
422 195/2)
424 #+(or)
425 (deftest variance)
427 #+nil
428 (deftest standard-deviation)
430 (deftest maxf.1
431 (let ((x 1))
432 (maxf x 2)
436 (deftest maxf.2
437 (let ((x 1))
438 (maxf x 0)
442 (deftest maxf.3
443 (let ((x 1)
444 (c 0))
445 (maxf x (incf c))
446 (list x c))
447 (1 1))
449 (deftest maxf.4
450 (let ((xv (vector 0 0 0))
451 (p 0))
452 (maxf (svref xv (incf p)) (incf p))
453 (list p xv))
454 (2 #(0 2 0)))
456 (deftest minf.1
457 (let ((y 1))
458 (minf y 0)
462 (deftest minf.2
463 (let ((xv (vector 10 10 10))
464 (p 0))
465 (minf (svref xv (incf p)) (incf p))
466 (list p xv))
467 (2 #(10 2 10)))
469 ;;;; Arrays
471 #+nil
472 (deftest array-index.type)
474 #+nil
475 (deftest copy-array)
477 ;;;; Sequences
479 (deftest rotate-left.1
480 (list (rotate-left (list 1 2 3) 0)
481 (rotate-left (list 1 2 3))
482 (rotate-left (list 1 2 3) 2)
483 (rotate-left (list 1 2 3) 3)
484 (rotate-left (list 1 2 3) 4))
485 ((1 2 3)
486 (3 1 2)
487 (2 3 1)
488 (1 2 3)
489 (3 1 2)))
491 (deftest rotate-left.2
492 (list (rotate-left (vector 1 2 3 4) 0)
493 (rotate-left (vector 1 2 3 4))
494 (rotate-left (vector 1 2 3 4) 2)
495 (rotate-left (vector 1 2 3 4) 3)
496 (rotate-left (vector 1 2 3 4) 4)
497 (rotate-left (vector 1 2 3 4) 5))
498 (#(1 2 3 4)
499 #(4 1 2 3)
500 #(3 4 1 2)
501 #(2 3 4 1)
502 #(1 2 3 4)
503 #(4 1 2 3)))
505 (deftest rotate-right.1
506 (list (rotate-right (list 1 2 3) 0)
507 (rotate-right (list 1 2 3))
508 (rotate-right (list 1 2 3) 2)
509 (rotate-right (list 1 2 3) 3)
510 (rotate-right (list 1 2 3) 4))
511 ((1 2 3)
512 (2 3 1)
513 (3 1 2)
514 (1 2 3)
515 (2 3 1)))
517 (deftest rotate-right.2
518 (list (rotate-right (vector 1 2 3 4) 0)
519 (rotate-right (vector 1 2 3 4))
520 (rotate-right (vector 1 2 3 4) 2)
521 (rotate-right (vector 1 2 3 4) 3)
522 (rotate-right (vector 1 2 3 4) 4)
523 (rotate-right (vector 1 2 3 4) 5))
524 (#(1 2 3 4)
525 #(2 3 4 1)
526 #(3 4 1 2)
527 #(4 1 2 3)
528 #(1 2 3 4)
529 #(2 3 4 1)))
531 (deftest suffle.1
532 (let ((s (suffle (iota 100))))
533 (list (equal s (iota 100))
534 (every (lambda (x)
535 (member x s))
536 (iota 100))
537 (every (lambda (x)
538 (typep x '(integer 0 99)))
539 s)))
540 (nil t t))
542 (deftest random-elt.1
543 (let ((s1 #(1 2 3 4))
544 (s2 '(1 2 3 4)))
545 (list (dotimes (i 1000 nil)
546 (unless (member (random-elt s1) s2)
547 (return nil))
548 (when (/= (random-elt s1) (random-elt s1))
549 (return t)))
550 (dotimes (i 1000 nil)
551 (unless (member (random-elt s2) s2)
552 (return nil))
553 (when (/= (random-elt s2) (random-elt s2))
554 (return t)))))
555 (t t))
557 (deftest removef.1
558 (let* ((x '(1 2 3))
559 (x* x)
560 (y #(1 2 3))
561 (y* y))
562 (removef x 1)
563 (removef y 3)
564 (list x x* y y*))
565 ((2 3)
566 (1 2 3)
567 #(1 2)
568 #(1 2 3)))
570 (deftest deletef.1
571 (let* ((x '(1 2 3))
572 (x* x)
573 (y (vector 1 2 3)))
574 (deletef x 2)
575 (deletef y 1)
576 (list x x* y))
577 ((1 3)
578 (1 3)
579 #(2 3)))
581 (deftest proper-sequence.type.1
582 (mapcar (lambda (x)
583 (typep x 'proper-sequence))
584 (list (list 1 2 3)
585 (vector 1 2 3)
586 #2a((1 2) (3 4))
587 (circular-list 1 2 3 4)))
588 (t t nil nil))
590 (deftest emptyp.1
591 (mapcar #'emptyp
592 (list (list 1)
593 (circular-list 1)
595 (vector)
596 (vector 1)))
597 (nil nil t t nil))
599 (deftest sequence-of-length-p.1
600 (mapcar #'sequence-of-length-p
601 (list nil
603 (list 1)
604 (vector 1)
605 (list 1 2)
606 (vector 1 2)
607 (list 1 2)
608 (vector 1 2)
609 (list 1 2)
610 (vector 1 2))
611 (list 0
621 (t t t t t t nil nil nil nil))
623 (deftest copy-sequence.1
624 (let ((l (list 1 2 3))
625 (v (vector #\a #\b #\c)))
626 (let ((l.list (copy-sequence 'list l))
627 (l.vector (copy-sequence 'vector l))
628 (l.spec-v (copy-sequence '(vector fixnum) l))
629 (v.vector (copy-sequence 'vector v))
630 (v.list (copy-sequence 'list v))
631 (v.string (copy-sequence 'string v)))
632 (list (member l (list l.list l.vector l.spec-v))
633 (member v (list v.vector v.list v.string))
634 (equal l.list l)
635 (equalp l.vector #(1 2 3))
636 (eq 'fixnum (array-element-type l.spec-v))
637 (equalp v.vector v)
638 (equal v.list '(#\a #\b #\c))
639 (equal "abc" v.string))))
640 (nil nil t t t t t t))
642 (deftest first-elt.1
643 (mapcar #'first-elt
644 (list (list 1 2 3)
645 "abc"
646 (vector :a :b :c)))
647 (1 #\a :a))
649 (deftest first-elt.error.1
650 (mapcar (lambda (x)
651 (handler-case
652 (first-elt x)
653 (type-error ()
654 :type-error)))
655 (list nil
658 :zot))
659 (:type-error
660 :type-error
661 :type-error
662 :type-error))
664 (deftest last-elt.1
665 (mapcar #'last-elt
666 (list (list 1 2 3)
667 (vector :a :b :c)
668 "FOOBAR"
669 #*001
670 #*010))
671 (3 :c #\R 1 0))
673 (deftest last-elt.error.1
674 (mapcar (lambda (x)
675 (handler-case
676 (last-elt x)
677 (type-error ()
678 :type-error)))
679 (list nil
682 :zot
683 (circular-list 1 2 3)
684 (list* 1 2 3 (circular-list 4 5))))
685 (:type-error
686 :type-error
687 :type-error
688 :type-error
689 :type-error
690 :type-error))
692 (deftest starts-with.1
693 (list (starts-with 1 '(1 2 3))
694 (starts-with 1 #(1 2 3))
695 (starts-with #\x "xyz")
696 (starts-with 2 '(1 2 3))
697 (starts-with 3 #(1 2 3))
698 (starts-with 1 1)
699 (starts-with nil nil))
700 (t t t nil nil nil nil))
702 (deftest ends-with.1
703 (list (ends-with 3 '(1 2 3))
704 (ends-with 3 #(1 2 3))
705 (ends-with #\z "xyz")
706 (ends-with 2 '(1 2 3))
707 (ends-with 1 #(1 2 3))
708 (ends-with 1 1)
709 (ends-with nil nil))
710 (t t t nil nil nil nil))
712 (deftest ends-with.error.1
713 (handler-case
714 (ends-with 3 (circular-list 3 3 3 1 3 3))
715 (type-error ()
716 :type-error))
717 :type-error)
719 (deftest with-unique-names.1
720 (let ((*gensym-counter* 0))
721 (let ((syms (with-unique-names (foo bar quux)
722 (list foo bar quux))))
723 (list (find-if #'symbol-package syms)
724 (equal '("FOO0" "BAR1" "QUUX2")
725 (mapcar #'symbol-name syms)))))
726 (nil t))
728 (deftest once-only.1
729 (macrolet ((cons1.good (x)
730 (once-only (x)
731 `(cons ,x ,x)))
732 (cons1.bad (x)
733 `(cons ,x ,x)))
734 (let ((y 0))
735 (list (cons1.good (incf y))
737 (cons1.bad (incf y))
738 y)))
739 ((1 . 1) 1 (2 . 3) 3))
741 (deftest parse-body.1
742 (parse-body '("doc" "body") :documentation t)
743 ("body")
744 nil
745 "doc")
747 (deftest parse-body.2
748 (parse-body '("body") :documentation t)
749 ("body")
750 nil
751 nil)
753 (deftest parse-body.3
754 (parse-body '("doc" "body"))
755 ("doc" "body")
756 nil
757 nil)
759 (deftest parse-body.4
760 (parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t)
761 (body)
762 ((declare (foo)) (declare (bar)))
763 "doc")
765 (deftest parse-body.5
766 (parse-body '((declare (foo)) "doc" (declare (bar)) body))
767 ("doc" (declare (bar)) body)
768 ((declare (foo)))
769 nil)
771 ;;;; Symbols
773 (deftest ensure-symbol.1
774 (ensure-symbol :cons :cl)
775 cons)
777 (deftest ensure-symbol.2
778 (ensure-symbol "CONS")
779 cons)
781 (deftest ensure-symbol.3
782 (ensure-symbol 'foo :keyword)
783 :foo)
785 (deftest ensure-symbol.4
786 (ensure-symbol #\*)
789 (deftest format-symbol.1
790 (let ((s (format-symbol nil "X-~D" 13)))
791 (list (symbol-package s)
792 (symbol-name s)))
793 (nil "X-13"))
795 (deftest format-symbol.2
796 (format-symbol :keyword "SYM-~A" :bolic)
797 :sym-bolic)
799 (deftest format-symbol.3
800 (let ((*package* (find-package :cl)))
801 (format-symbol t "FIND-~A" 'package))
802 find-package)
804 (deftest make-keyword.1
805 (list (make-keyword 'zot)
806 (make-keyword "FOO")
807 (make-keyword #\Q))
808 (:zot :foo :q))
810 (deftest make-gensym-list.1
811 (let ((*gensym-counter* 0))
812 (let ((syms (make-gensym-list 3 "FOO")))
813 (list (find-if 'symbol-package syms)
814 (equal '("FOO0" "FOO1" "FOO2")
815 (mapcar 'symbol-name syms)))))
816 (nil t))
818 ;;;; Type-system
820 (deftest of-type.1
821 (let ((f (of-type 'string)))
822 (list (funcall f "foo")
823 (funcall f 'bar)))
824 (t nil))
826 (deftest type=.1
827 (type= 'string 'string)
831 (deftest type=.2
832 (type= 'list '(or null cons))
836 (deftest type=.3
837 (type= 'null '(and symbol list))
841 (deftest type=.4
842 (type= 'string '(satisfies emptyp))
844 nil)
846 (deftest type=.5
847 (type= 'string 'list)