3 (defpackage :alexandria-tests
4 (:use
:cl
:alexandria
#+sbcl
:sb-rt
#-sbcl
:rtest
)
5 (:import-from
#+sbcl
:sb-rt
#-sbcl
:rtest
6 #:*compile-tests
* #:*expected-failures
*))
8 (in-package :alexandria-tests
)
10 (defun run-tests (&key
((:compiled
*compile-tests
*)))
13 (defun hash-table-test-name (name)
14 ;; Workaround for Clisp calling EQL in a hash-table FASTHASH-EQL.
15 (hash-table-test (make-hash-table :test name
)))
20 (let* ((orig (vector 1 2 3))
21 (copy (copy-array orig
)))
22 (values (eq orig copy
) (equalp orig copy
)))
26 (let ((orig (make-array 1024 :fill-pointer
0)))
27 (vector-push-extend 1 orig
)
28 (vector-push-extend 2 orig
)
29 (vector-push-extend 3 orig
)
30 (let ((copy (copy-array orig
)))
31 (values (eq orig copy
) (equalp orig copy
)
32 (array-has-fill-pointer-p copy
)
33 (eql (fill-pointer orig
) (fill-pointer copy
)))))
37 (let* ((orig (vector 1 2 3))
38 (copy (copy-array orig
)))
39 (typep copy
'simple-array
))
43 (let ((orig (make-array 21
47 (vector-push-extend n orig
))
48 (let ((copy (copy-array orig
51 (typep copy
'simple-array
)))
54 (deftest array-index
.1
55 (typep 0 'array-index
)
60 (deftest unwind-protect-case
.1
62 (unwind-protect-case ()
64 (:normal
(push :normal result
))
65 (:abort
(push :abort result
))
66 (:always
(push :always result
)))
70 (deftest unwind-protect-case
.2
72 (unwind-protect-case ()
74 (:always
(push :always result
))
75 (:normal
(push :normal result
))
76 (:abort
(push :abort result
)))
80 (deftest unwind-protect-case
.3
81 (let (result1 result2 result3
)
83 (unwind-protect-case ()
85 (:normal
(push :normal result1
))
86 (:abort
(push :abort result1
))
87 (:always
(push :always result1
))))
89 (unwind-protect-case ()
91 (:normal
(push :normal result2
))
92 (:abort
(push :abort result2
))
93 (:always
(push :always result2
))))
95 (unwind-protect-case ()
97 (:normal
(push :normal result3
))
98 (:abort
(push :abort result3
))
99 (:always
(push :always result3
))))
100 (values result1 result2 result3
))
105 (deftest unwind-protect-case
.4
107 (unwind-protect-case (aborted-p)
109 (:always
(setq result aborted-p
)))
113 (deftest unwind-protect-case
.5
116 (unwind-protect-case (aborted-p)
118 (:always
(setq result aborted-p
))))
152 (cswitch (13 :test
=)
158 (cswitch (13 :key
1-
)
163 (deftest multiple-value-prog2.1
164 (multiple-value-prog2
170 (deftest nth-value-or
.1
171 (multiple-value-bind (a b c
)
179 (let ((x (whichever 1 2 3)))
180 (and (member x
'(1 2 3)) t
))
187 (x (whichever a b c
)))
188 (and (member x
'(1 2 3)) t
))
208 (deftest define-constant
.1
209 (let ((name (gensym)))
210 (eval `(define-constant ,name
"FOO" :test
'equal
))
211 (eval `(define-constant ,name
"FOO" :test
'equal
))
212 (values (equal "FOO" (symbol-value name
))
217 (deftest define-constant
.2
218 (let ((name (gensym)))
219 (eval `(define-constant ,name
13))
220 (eval `(define-constant ,name
13))
221 (values (eql 13 (symbol-value name
))
228 ;;; TYPEP is specified to return a generalized boolean and, for
229 ;;; example, ECL exploits this by returning the superclasses of ERROR
232 (not (null (typep x
'error
))))
234 (deftest required-argument
.1
235 (multiple-value-bind (res err
)
236 (ignore-errors (required-argument))
242 (deftest ensure-hash-table
.1
243 (let ((table (make-hash-table))
245 (multiple-value-bind (value already-there
)
246 (ensure-gethash x table
42)
249 (= 42 (gethash x table
))
250 (multiple-value-bind (value2 already-there2
)
251 (ensure-gethash x table
13)
254 (= 42 (gethash x table
)))))))
257 (deftest copy-hash-table
.1
258 (let ((orig (make-hash-table :test
'eq
:size
123))
260 (setf (gethash orig orig
) t
261 (gethash foo orig
) t
)
262 (let ((eq-copy (copy-hash-table orig
))
263 (eql-copy (copy-hash-table orig
:test
'eql
))
264 (equal-copy (copy-hash-table orig
:test
'equal
))
265 (equalp-copy (copy-hash-table orig
:test
'equalp
)))
266 (list (eql (hash-table-size eq-copy
) (hash-table-size orig
))
267 (eql (hash-table-rehash-size eq-copy
)
268 (hash-table-rehash-size orig
))
269 (hash-table-count eql-copy
)
270 (gethash orig eq-copy
)
271 (gethash (copy-seq foo
) eql-copy
)
272 (gethash foo eql-copy
)
273 (gethash (copy-seq foo
) equal-copy
)
274 (gethash "FOO" equal-copy
)
275 (gethash "FOO" equalp-copy
))))
276 (t t
2 t nil t t nil t
))
278 (deftest copy-hash-table
.2
279 (let ((ht (make-hash-table))
280 (list (list :list
(vector :A
:B
:C
))))
281 (setf (gethash 'list ht
) list
)
282 (let* ((shallow-copy (copy-hash-table ht
))
283 (deep1-copy (copy-hash-table ht
:key
'copy-list
))
284 (list (gethash 'list ht
))
285 (shallow-list (gethash 'list shallow-copy
))
286 (deep1-list (gethash 'list deep1-copy
)))
287 (list (eq ht shallow-copy
)
289 (eq list shallow-list
)
290 (eq list deep1-list
) ; outer list was copied.
291 (eq (second list
) (second shallow-list
))
292 (eq (second list
) (second deep1-list
)) ; inner vector wasn't copied.
296 (deftest maphash-keys
.1
298 (table (make-hash-table)))
299 (declare (notinline maphash-keys
))
301 (setf (gethash i table
) t
))
302 (maphash-keys (lambda (k) (push k keys
)) table
)
303 (set-equal keys
'(0 1 2 3 4 5 6 7 8 9)))
306 (deftest maphash-values
.1
308 (table (make-hash-table)))
309 (declare (notinline maphash-values
))
311 (setf (gethash i table
) (- i
)))
312 (maphash-values (lambda (v) (push v vals
)) table
)
313 (set-equal vals
'(0 -
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9)))
316 (deftest hash-table-keys
.1
317 (let ((table (make-hash-table)))
319 (setf (gethash i table
) t
))
320 (set-equal (hash-table-keys table
) '(0 1 2 3 4 5 6 7 8 9)))
323 (deftest hash-table-values
.1
324 (let ((table (make-hash-table)))
326 (setf (gethash (gensym) table
) i
))
327 (set-equal (hash-table-values table
) '(0 1 2 3 4 5 6 7 8 9)))
330 (deftest hash-table-alist
.1
331 (let ((table (make-hash-table)))
333 (setf (gethash i table
) (- i
)))
334 (let ((alist (hash-table-alist table
)))
340 (10 (0 .
0) (3 . -
3) (9 . -
9) nil
))
342 (deftest hash-table-plist
.1
343 (let ((table (make-hash-table)))
345 (setf (gethash i table
) (- i
)))
346 (let ((plist (hash-table-plist table
)))
354 (deftest alist-hash-table
.1
355 (let* ((alist '((0 a
) (1 b
) (2 c
)))
356 (table (alist-hash-table alist
)))
357 (list (hash-table-count table
)
361 (eq (hash-table-test-name 'eql
)
362 (hash-table-test table
))))
365 (deftest plist-hash-table
.1
366 (let* ((plist '(:a
1 :b
2 :c
3))
367 (table (plist-hash-table plist
:test
'eq
)))
368 (list (hash-table-count table
)
374 (eq (hash-table-test-name 'eq
)
375 (hash-table-test table
))))
381 (let ((disjunction (disjoin (lambda (x)
382 (and (consp x
) :cons
))
384 (and (stringp x
) :string
)))))
385 (list (funcall disjunction
'zot
)
386 (funcall disjunction
'(foo bar
))
387 (funcall disjunction
"test")))
391 (let ((disjunction (disjoin #'zerop
)))
392 (list (funcall disjunction
0)
393 (funcall disjunction
1)))
397 (let ((conjunction (conjoin #'consp
402 (list (funcall conjunction
'zot
)
403 (funcall conjunction
'(foo))
404 (funcall conjunction
'("foo"))))
408 (let ((conjunction (conjoin #'zerop
)))
409 (list (funcall conjunction
0)
410 (funcall conjunction
1)))
414 (let ((composite (compose '1+
417 #'read-from-string
)))
418 (funcall composite
"1"))
423 (locally (declare (notinline compose
))
427 #'read-from-string
))))
428 (funcall composite
"2"))
432 (let ((compose-form (funcall (compiler-macro-function 'compose
)
438 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
443 (let ((composite (compose #'zerop
)))
444 (list (funcall composite
0)
445 (funcall composite
1)))
448 (deftest multiple-value-compose
.1
449 (let ((composite (multiple-value-compose
454 (with-input-from-string (s x
)
455 (values (read s
) (read s
)))))))
456 (multiple-value-list (funcall composite
"2 7")))
459 (deftest multiple-value-compose
.2
460 (let ((composite (locally (declare (notinline multiple-value-compose
))
461 (multiple-value-compose
466 (with-input-from-string (s x
)
467 (values (read s
) (read s
))))))))
468 (multiple-value-list (funcall composite
"2 11")))
471 (deftest multiple-value-compose
.3
472 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose
)
473 '(multiple-value-compose
478 (with-input-from-string (s x
)
479 (values (read s
) (read s
)))))
481 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
482 (multiple-value-list (funcall fun
"2 9"))))
485 (deftest multiple-value-compose
.4
486 (let ((composite (multiple-value-compose #'truncate
)))
487 (multiple-value-list (funcall composite
9 2)))
491 (let ((curried (curry '+ 3)))
492 (funcall curried
1 5))
496 (let ((curried (locally (declare (notinline curry
))
502 (let ((curried-form (funcall (compiler-macro-function 'curry
)
505 (let ((fun (funcall (compile nil
`(lambda () ,curried-form
)))))
511 (curried (curry (progn
513 (lambda (y z
) (* x y z
)))
515 (list (funcall curried
7)
521 (let ((r (rcurry '/ 2)))
527 (curried (rcurry (progn
529 (lambda (y z
) (* x y z
)))
531 (list (funcall curried
7)
536 (deftest named-lambda
.1
537 (let ((fac (named-lambda fac
(x)
544 (deftest named-lambda
.2
545 (let ((fac (named-lambda fac
(&key x
)
547 (* x
(fac :x
(- x
1)))
554 (deftest alist-plist
.1
555 (alist-plist '((a .
1) (b .
2) (c .
3)))
558 (deftest plist-alist
.1
559 (plist-alist '(a 1 b
2 c
3))
560 ((a .
1) (b .
2) (c .
3)))
563 (let* ((list (list 1 2 3))
565 (unionf list
(list 1 2 4))
566 (values (equal orig
(list 1 2 3))
567 (eql (length list
) 4)
568 (set-difference list
(list 1 2 3 4))
569 (set-difference (list 1 2 3 4) list
)))
576 (let ((list (list 1 2 3)))
577 (nunionf list
(list 1 2 4))
578 (values (eql (length list
) 4)
579 (set-difference (list 1 2 3 4) list
)
580 (set-difference list
(list 1 2 3 4))))
586 (let* ((list (list 1 2 3))
588 (appendf list
'(4 5 6) '(7 8))
589 (list list
(eq list orig
)))
590 ((1 2 3 4 5 6 7 8) nil
))
593 (let ((list1 (list 1 2 3))
594 (list2 (list 4 5 6)))
595 (nconcf list1 list2
(list 7 8 9))
599 (deftest circular-list
.1
600 (let ((circle (circular-list 1 2 3)))
605 (eq circle
(nthcdr 3 circle
))))
608 (deftest circular-list-p
.1
609 (let* ((circle (circular-list 1 2 3 4))
610 (tree (list circle circle
))
611 (dotted (cons circle t
))
612 (proper (list 1 2 3 circle
))
613 (tailcirc (list* 1 2 3 circle
)))
614 (list (circular-list-p circle
)
615 (circular-list-p tree
)
616 (circular-list-p dotted
)
617 (circular-list-p proper
)
618 (circular-list-p tailcirc
)))
621 (deftest circular-list-p
.2
622 (circular-list-p 'foo
)
625 (deftest circular-tree-p
.1
626 (let* ((circle (circular-list 1 2 3 4))
627 (tree1 (list circle circle
))
628 (tree2 (let* ((level2 (list 1 nil
2))
629 (level1 (list level2
)))
630 (setf (second level2
) level1
)
632 (dotted (cons circle t
))
633 (proper (list 1 2 3 circle
))
634 (tailcirc (list* 1 2 3 circle
))
635 (quite-proper (list 1 2 3))
636 (quite-dotted (list 1 (cons 2 3))))
637 (list (circular-tree-p circle
)
638 (circular-tree-p tree1
)
639 (circular-tree-p tree2
)
640 (circular-tree-p dotted
)
641 (circular-tree-p proper
)
642 (circular-tree-p tailcirc
)
643 (circular-tree-p quite-proper
)
644 (circular-tree-p quite-dotted
)))
645 (t t t t t t nil nil
))
647 (deftest circular-tree-p
.2
648 (alexandria:circular-tree-p
'#1=(#1#))
651 (deftest proper-list-p
.1
655 (l4 (list (cons 1 2) 3))
656 (l5 (circular-list 1 2)))
657 (list (proper-list-p l1
)
664 (deftest proper-list-p
.2
665 (proper-list-p '(1 2 .
3))
668 (deftest proper-list.type
.1
672 (l4 (list (cons 1 2) 3))
673 (l5 (circular-list 1 2)))
674 (list (typep l1
'proper-list
)
675 (typep l2
'proper-list
)
676 (typep l3
'proper-list
)
677 (typep l4
'proper-list
)
678 (typep l5
'proper-list
)))
681 (deftest proper-list-length
.1
683 (proper-list-length nil
)
684 (proper-list-length (list 1))
685 (proper-list-length (list 2 2))
686 (proper-list-length (list 3 3 3))
687 (proper-list-length (list 4 4 4 4))
688 (proper-list-length (list 5 5 5 5 5))
689 (proper-list-length (list 6 6 6 6 6 6))
690 (proper-list-length (list 7 7 7 7 7 7 7))
691 (proper-list-length (list 8 8 8 8 8 8 8 8))
692 (proper-list-length (list 9 9 9 9 9 9 9 9 9)))
695 (deftest proper-list-length
.2
698 (proper-list-length x
)
703 (plength (list* 2 2))
704 (plength (list* 3 3 3))
705 (plength (list* 4 4 4 4))
706 (plength (list* 5 5 5 5 5))
707 (plength (list* 6 6 6 6 6 6))
708 (plength (list* 7 7 7 7 7 7 7))
709 (plength (list* 8 8 8 8 8 8 8 8))
710 (plength (list* 9 9 9 9 9 9 9 9 9))))
722 (deftest lastcar.error
.2
725 (lastcar (circular-list 1 2 3))
731 (deftest setf-lastcar
.1
732 (let ((l (list 1 2 3 4)))
735 (setf (lastcar l
) 42)
740 (deftest setf-lastcar
.2
741 (let ((l (circular-list 1 2 3)))
742 (multiple-value-bind (res err
)
743 (ignore-errors (setf (lastcar l
) 4))
744 (typep err
'type-error
)))
747 (deftest make-circular-list
.1
748 (let ((l (make-circular-list 3 :initial-element
:x
)))
750 (list (eq l
(nthcdr 3 l
))
757 (deftest circular-list.type
.1
758 (let* ((l1 (list 1 2 3))
759 (l2 (circular-list 1 2 3))
760 (l3 (list* 1 2 3 l2
)))
761 (list (typep l1
'circular-list
)
762 (typep l2
'circular-list
)
763 (typep l3
'circular-list
)))
766 (deftest ensure-list
.1
769 (list (ensure-list x
)
773 (deftest ensure-cons
.1
777 (values (ensure-cons x
)
809 (setp '(a :a
) :key
'character
)
813 (setp '(a :a
) :key
'character
:test
(constantly nil
))
817 (set-equal '(1 2 3) '(3 1 2))
821 (set-equal '("Xa") '("Xb")
822 :test
(lambda (a b
) (eql (char a
0) (char b
0))))
826 (set-equal '(1 2) '(4 2))
830 (set-equal '(a b c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
834 (set-equal '(a d c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
838 (set-equal '(a b c
) '(a b c d
))
841 (deftest map-product
.1
842 (map-product 'cons
'(2 3) '(1 4))
843 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
845 (deftest map-product
.2
846 (map-product #'cons
'(2 3) '(1 4))
847 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
850 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
853 (deftest remove-from-plist
.1
854 (let ((orig '(a 1 b
2 c
3 d
4)))
855 (list (remove-from-plist orig
'a
'c
)
856 (remove-from-plist orig
'b
'd
)
857 (remove-from-plist orig
'b
)
858 (remove-from-plist orig
'a
)
859 (remove-from-plist orig
'd
42 "zot")
860 (remove-from-plist orig
'a
'b
'c
'd
)
861 (remove-from-plist orig
'a
'b
'c
'd
'x
)
862 (equal orig
'(a 1 b
2 c
3 d
4))))
872 (deftest delete-from-plist
.1
873 (let ((orig '(a 1 b
2 c
3 d
4)))
874 (list (delete-from-plist (copy-list orig
) 'a
'c
)
875 (delete-from-plist (copy-list orig
) 'b
'd
)
876 (delete-from-plist (copy-list orig
) 'b
)
877 (delete-from-plist (copy-list orig
) 'a
)
878 (delete-from-plist (copy-list orig
) 'd
42 "zot")
879 (delete-from-plist (copy-list orig
) 'a
'b
'c
'd
)
880 (delete-from-plist (copy-list orig
) 'a
'b
'c
'd
'x
)
881 (equal orig
(delete-from-plist orig
))
882 (eq orig
(delete-from-plist orig
))))
894 (mappend (compose 'list
'*) '(1 2 3) '(1 2 3))
897 (deftest assoc-value
.1
898 (let ((key1 '(complex key
))
902 (push 1 (assoc-value alist key1
:test
#'equal
))
903 (push 2 (assoc-value alist key1
:test
'equal
))
904 (push 42 (assoc-value alist key2
))
905 (push 43 (assoc-value alist key2
:test
'eq
))
906 (push (assoc-value alist key1
:test
#'equal
) result
)
907 (push (assoc-value alist key2
) result
)
909 (push 'very
(rassoc-value alist
(list 2 1) :test
#'equal
))
910 (push (cdr (assoc '(very complex key
) alist
:test
#'equal
)) result
)
912 ((2 1) (43 42) (2 1)))
917 (list (clamp 1.5 1 2)
924 (deftest gaussian-random
.1
927 (multiple-value-bind (g1 g2
)
928 (gaussian-random min max
)
929 (values (<= min g1 max
)
938 (deftest gaussian-random
.2
940 (sb-ext:with-timeout
2
944 :do
(gaussian-random 0 nil
))
955 (iota 3 :start
0.0d0
)
959 (iota 3 :start
2 :step
3.0)
964 (declare (notinline map-iota
))
965 (values (map-iota (lambda (x) (push x all
))
994 (median '(100 0 99 1 98 2 97))
998 (median '(100 0 99 1 98 2 97 96))
1002 (variance (list 1 2 3))
1005 (deftest standard-deviation
.1
1006 (< 0 (standard-deviation (list 1 2 3)) 1)
1029 (let ((xv (vector 0 0 0))
1031 (maxf (svref xv
(incf p
)) (incf p
))
1042 (let ((xv (vector 10 10 10))
1044 (minf (svref xv
(incf p
)) (incf p
))
1048 (deftest subfactorial
.1
1049 (mapcar #'subfactorial
(iota 22))
1071 18795307255050944540))
1076 (deftest array-index.type
)
1079 (deftest copy-array
)
1084 (list (rotate (list 1 2 3) 0)
1085 (rotate (list 1 2 3) 1)
1086 (rotate (list 1 2 3) 2)
1087 (rotate (list 1 2 3) 3)
1088 (rotate (list 1 2 3) 4))
1096 (list (rotate (vector 1 2 3 4) 0)
1097 (rotate (vector 1 2 3 4))
1098 (rotate (vector 1 2 3 4) 2)
1099 (rotate (vector 1 2 3 4) 3)
1100 (rotate (vector 1 2 3 4) 4)
1101 (rotate (vector 1 2 3 4) 5))
1110 (list (rotate (list 1 2 3) 0)
1111 (rotate (list 1 2 3) -
1)
1112 (rotate (list 1 2 3) -
2)
1113 (rotate (list 1 2 3) -
3)
1114 (rotate (list 1 2 3) -
4))
1122 (list (rotate (vector 1 2 3 4) 0)
1123 (rotate (vector 1 2 3 4) -
1)
1124 (rotate (vector 1 2 3 4) -
2)
1125 (rotate (vector 1 2 3 4) -
3)
1126 (rotate (vector 1 2 3 4) -
4)
1127 (rotate (vector 1 2 3 4) -
5))
1136 (values (rotate (list 1) 17)
1137 (rotate (list 1) -
5))
1142 (let ((s (shuffle (iota 100))))
1143 (list (equal s
(iota 100))
1148 (typep x
'(integer 0 99)))
1153 (let ((s (shuffle (coerce (iota 100) 'vector
))))
1154 (list (equal s
(coerce (iota 100) 'vector
))
1159 (typep x
'(integer 0 99)))
1164 (let* ((orig (coerce (iota 21) 'vector
))
1165 (copy (copy-seq orig
)))
1166 (shuffle copy
:start
10 :end
15)
1167 (list (every #'eql
(subseq copy
0 10) (subseq orig
0 10))
1168 (every #'eql
(subseq copy
15) (subseq orig
15))))
1171 (deftest random-elt
.1
1172 (let ((s1 #(1 2 3 4))
1174 (list (dotimes (i 1000 nil
)
1175 (unless (member (random-elt s1
) s2
)
1177 (when (/= (random-elt s1
) (random-elt s1
))
1179 (dotimes (i 1000 nil
)
1180 (unless (member (random-elt s2
) s2
)
1182 (when (/= (random-elt s2
) (random-elt s2
))
1200 (let* ((x (list 1 2 3))
1210 (deftest map-permutations
.1
1211 (let ((seq (list 1 2 3))
1214 (map-permutations (lambda (s)
1215 (unless (set-equal s seq
)
1217 (when (member s seen
:test
'equal
)
1222 (values ok
(length seen
)))
1226 (deftest proper-sequence.type
.1
1228 (typep x
'proper-sequence
))
1232 (circular-list 1 2 3 4)))
1244 (deftest sequence-of-length-p
.1
1245 (mapcar #'sequence-of-length-p
1266 (t t t t t t nil nil nil nil
))
1290 (t t t t t t nil nil nil nil
))
1293 ;; test the compiler macro
1294 (macrolet ((x (&rest args
)
1298 (length= ,@args
))))))
1306 (deftest copy-sequence
.1
1307 (let ((l (list 1 2 3))
1308 (v (vector #\a #\b #\c
)))
1309 (declare (notinline copy-sequence
))
1310 (let ((l.list
(copy-sequence 'list l
))
1311 (l.vector
(copy-sequence 'vector l
))
1312 (l.spec-v
(copy-sequence '(vector fixnum
) l
))
1313 (v.vector
(copy-sequence 'vector v
))
1314 (v.list
(copy-sequence 'list v
))
1315 (v.string
(copy-sequence 'string v
)))
1316 (list (member l
(list l.list l.vector l.spec-v
))
1317 (member v
(list v.vector v.list v.string
))
1319 (equalp l.vector
#(1 2 3))
1320 (type= (upgraded-array-element-type 'fixnum
)
1321 (array-element-type l.spec-v
))
1323 (equal v.list
'(#\a #\b #\c
))
1324 (equal "abc" v.string
))))
1325 (nil nil t t t t t t
))
1327 (deftest first-elt
.1
1334 (deftest first-elt.error
.1
1349 (deftest setf-first-elt
.1
1350 (let ((l (list 1 2 3))
1351 (s (copy-seq "foobar"))
1352 (v (vector :a
:b
:c
)))
1353 (setf (first-elt l
) -
1
1361 (deftest setf-first-elt.error
.1
1363 (multiple-value-bind (res err
)
1364 (ignore-errors (setf (first-elt l
) 4))
1365 (typep err
'type-error
)))
1377 (deftest last-elt.error
.1
1387 (circular-list 1 2 3)
1388 (list* 1 2 3 (circular-list 4 5))))
1396 (deftest setf-last-elt
.1
1397 (let ((l (list 1 2 3))
1398 (s (copy-seq "foobar"))
1399 (b (copy-seq #*010101001)))
1400 (setf (last-elt l
) '???
1408 (deftest setf-last-elt.error
.1
1410 (setf (last-elt 'foo
) 13)
1415 (deftest starts-with
.1
1416 (list (starts-with 1 '(1 2 3))
1417 (starts-with 1 #(1 2 3))
1418 (starts-with #\x
"xyz")
1419 (starts-with 2 '(1 2 3))
1420 (starts-with 3 #(1 2 3))
1422 (starts-with nil nil
))
1423 (t t t nil nil nil nil
))
1425 (deftest starts-with
.2
1426 (values (starts-with 1 '(-1 2 3) :key
'-
)
1427 (starts-with "foo" '("foo" "bar") :test
'equal
)
1428 (starts-with "f" '(#\f) :key
'string
:test
'equal
)
1429 (starts-with -
1 '(0 1 2) :key
#'1+)
1430 (starts-with "zot" '("ZOT") :test
'equal
))
1437 (deftest ends-with
.1
1438 (list (ends-with 3 '(1 2 3))
1439 (ends-with 3 #(1 2 3))
1440 (ends-with #\z
"xyz")
1441 (ends-with 2 '(1 2 3))
1442 (ends-with 1 #(1 2 3))
1444 (ends-with nil nil
))
1445 (t t t nil nil nil nil
))
1447 (deftest ends-with
.2
1448 (values (ends-with 2 '(0 13 1) :key
'1+)
1449 (ends-with "foo" (vector "bar" "foo") :test
'equal
)
1450 (ends-with "X" (vector 1 2 #\X
) :key
'string
:test
'equal
)
1451 (ends-with "foo" "foo" :test
'equal
))
1457 (deftest ends-with.error
.1
1459 (ends-with 3 (circular-list 3 3 3 1 3 3))
1464 (deftest sequences.passing-improper-lists
1465 (macrolet ((signals-error-p (form)
1470 (cut (fn &rest args
)
1472 (print`(lambda (,arg
)
1473 (apply ,fn
(list ,@(substitute arg
'_ args
))))))))
1474 (let ((circular-list (make-circular-list 5 :initial-element
:foo
))
1475 (dotted-list (list* 'a
'b
'c
'd
)))
1476 (loop for nth from
0
1482 (cut #'random-elt _
)
1484 (cut #'ends-with
:foo _
))
1486 (let ((on-circular-p (signals-error-p (funcall fn circular-list
)))
1487 (on-dotted-p (signals-error-p (funcall fn dotted-list
))))
1488 (when (or (not on-circular-p
) (not on-dotted-p
))
1490 (unless on-circular-p
1491 (let ((*print-circle
* t
))
1494 "No appropriate error signalled when passing ~S to ~Ath entry."
1495 circular-list nth
))))
1499 "No appropriate error signalled when passing ~S to ~Ath entry."
1500 dotted-list nth
)))))))))
1503 (deftest with-unique-names
.1
1504 (let ((*gensym-counter
* 0))
1505 (let ((syms (with-unique-names (foo bar quux
)
1506 (list foo bar quux
))))
1507 (list (find-if #'symbol-package syms
)
1508 (equal '("FOO0" "BAR1" "QUUX2")
1509 (mapcar #'symbol-name syms
)))))
1512 (deftest with-unique-names
.2
1513 (let ((*gensym-counter
* 0))
1514 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-
) (quux #\q
))
1515 (list foo bar quux
))))
1516 (list (find-if #'symbol-package syms
)
1517 (equal '("_foo_0" "-BAR-1" "q2")
1518 (mapcar #'symbol-name syms
)))))
1521 (deftest with-unique-names
.3
1522 (let ((*gensym-counter
* 0))
1523 (multiple-value-bind (res err
)
1527 (with-unique-names ((foo "_foo_") (bar -bar-
) (quux 42))
1528 (list foo bar quux
))))
1529 (list (find-if #'symbol-package syms
)
1530 (equal '("_foo_0" "-BAR-1" "q2")
1531 (mapcar #'symbol-name syms
))))))
1535 (deftest once-only
.1
1536 (macrolet ((cons1.good
(x)
1542 (list (cons1.good
(incf y
))
1544 (cons1.bad
(incf y
))
1546 ((1 .
1) 1 (2 .
3) 3))
1548 (deftest once-only
.2
1549 (macrolet ((cons1 (x)
1553 (list (cons1 (incf z
))
1556 ((1 .
1) 1 (2 .
2)))
1558 (deftest parse-body
.1
1559 (parse-body '("doc" "body") :documentation t
)
1564 (deftest parse-body
.2
1565 (parse-body '("body") :documentation t
)
1570 (deftest parse-body
.3
1571 (parse-body '("doc" "body"))
1576 (deftest parse-body
.4
1577 (parse-body '((declare (foo)) "doc" (declare (bar)) body
) :documentation t
)
1579 ((declare (foo)) (declare (bar)))
1582 (deftest parse-body
.5
1583 (parse-body '((declare (foo)) "doc" (declare (bar)) body
))
1584 ("doc" (declare (bar)) body
)
1588 (deftest parse-body
.6
1589 (multiple-value-bind (res err
)
1591 (parse-body '("foo" "bar" "quux")
1598 (deftest ensure-symbol
.1
1599 (ensure-symbol :cons
:cl
)
1603 (deftest ensure-symbol
.2
1604 (ensure-symbol "CONS" :alexandria
)
1608 (deftest ensure-symbol
.3
1609 (ensure-symbol 'foo
:keyword
)
1613 (deftest ensure-symbol
.4
1614 (ensure-symbol #\
* :alexandria
)
1618 (deftest format-symbol
.1
1619 (let ((s (format-symbol nil
'#:x-~d
13)))
1620 (list (symbol-package s
)
1621 (string= (string '#:x-13
) (symbol-name s
))))
1624 (deftest format-symbol
.2
1625 (format-symbol :keyword
'#:sym-~a
(string :bolic
))
1628 (deftest format-symbol
.3
1629 (let ((*package
* (find-package :cl
)))
1630 (format-symbol t
'#:find-~a
(string 'package
)))
1633 (deftest make-keyword
.1
1634 (list (make-keyword 'zot
)
1635 (make-keyword "FOO")
1639 (deftest make-gensym-list
.1
1640 (let ((*gensym-counter
* 0))
1641 (let ((syms (make-gensym-list 3 "FOO")))
1642 (list (find-if 'symbol-package syms
)
1643 (equal '("FOO0" "FOO1" "FOO2")
1644 (mapcar 'symbol-name syms
)))))
1647 (deftest make-gensym-list
.2
1648 (let ((*gensym-counter
* 0))
1649 (let ((syms (make-gensym-list 3)))
1650 (list (find-if 'symbol-package syms
)
1651 (equal '("G0" "G1" "G2")
1652 (mapcar 'symbol-name syms
)))))
1659 (declare (notinline of-type
))
1660 (let ((f (of-type 'string
)))
1661 (list (funcall f
"foo")
1666 (type= 'string
'string
)
1671 (type= 'list
'(or null cons
))
1676 (type= 'null
'(and symbol list
))
1681 (type= 'string
'(satisfies emptyp
))
1686 (type= 'string
'list
)
1691 ((test (type numbers
)
1692 `(deftest ,(format-symbol t
'#:cdr5.~a
(string type
))
1693 (let ((numbers ,numbers
))
1694 (values (mapcar (of-type ',(format-symbol t
'#:negative-~a
(string type
))) numbers
)
1695 (mapcar (of-type ',(format-symbol t
'#:non-positive-~a
(string type
))) numbers
)
1696 (mapcar (of-type ',(format-symbol t
'#:non-negative-~a
(string type
))) numbers
)
1697 (mapcar (of-type ',(format-symbol t
'#:positive-~a
(string type
))) numbers
)))
1698 (t t t nil nil nil nil
)
1699 (t t t t nil nil nil
)
1700 (nil nil nil t t t t
)
1701 (nil nil nil nil t t t
))))
1702 (test fixnum
(list most-negative-fixnum -
42 -
1 0 1 42 most-positive-fixnum
))
1703 (test integer
(list (1- most-negative-fixnum
) -
42 -
1 0 1 42 (1+ most-positive-fixnum
)))
1704 (test rational
(list (1- most-negative-fixnum
) -
42/13 -
1 0 1 42/13 (1+ most-positive-fixnum
)))
1705 (test real
(list most-negative-long-float -
42/13 -
1 0 1 42/13 most-positive-long-float
))
1706 (test float
(list most-negative-short-float -
42.02 -
1.0 0.0 1.0 42.02 most-positive-short-float
))
1707 (test short-float
(list most-negative-short-float -
42.02s0 -
1.0s0
0.0s0
1.0s0
42.02s0 most-positive-short-float
))
1708 (test single-float
(list most-negative-single-float -
42.02f0 -
1.0f0
0.0f0
1.0f0
42.02f0 most-positive-single-float
))
1709 (test double-float
(list most-negative-double-float -
42.02d0 -
1.0d0
0.0d0
1.0d0
42.02d0 most-positive-double-float
))
1710 (test long-float
(list most-negative-long-float -
42.02l0 -
1.0l0 0.0l0 1.0l0 42.02l0 most-positive-long-float
)))
1714 (declaim (notinline opaque
))
1719 (if-let (x (opaque :ok
))
1725 (if-let (x (opaque nil
))
1751 (deftest if-let.error
.1
1761 (when-let (x (opaque :ok
))
1780 (deftest when-let.error
.1
1782 (eval '(when-let x
:oops
))
1787 (deftest when-let
*.1
1794 (deftest when-let
*.2
1800 (deftest when-let
*.3
1807 (deftest when-let
*.error
.1
1809 (eval '(when-let* x
:oops
))
1816 (doplist (k v
'(a 1 b
2 c
3) (values t
(reverse keys
) (reverse values
) k v
))
1825 (deftest count-permutations
.1
1826 (values (count-permutations 31 7)
1827 (count-permutations 1 1)
1828 (count-permutations 2 1)
1829 (count-permutations 2 2)
1830 (count-permutations 3 2)
1831 (count-permutations 3 1))
1839 (deftest binomial-coefficient
.1
1840 (alexandria:binomial-coefficient
1239 139)
1841 28794902202288970200771694600561826718847179309929858835480006683522184441358211423695124921058123706380656375919763349913245306834194782172712255592710204598527867804110129489943080460154)
1843 (deftest copy-stream
.1
1844 (let ((data "sdkfjhsakfh weior763495ewofhsdfk sdfadlkfjhsadf woif sdlkjfhslkdfh sdklfjh"))
1846 (with-input-from-string (in data
)
1847 (with-output-to-string (out)
1848 (alexandria:copy-stream in out
))))
1849 (equal (subseq data
10 20)
1850 (with-input-from-string (in data
)
1851 (with-output-to-string (out)
1852 (alexandria:copy-stream in out
:start
10 :end
20))))
1853 (equal (subseq data
10)
1854 (with-input-from-string (in data
)
1855 (with-output-to-string (out)
1856 (alexandria:copy-stream in out
:start
10))))
1857 (equal (subseq data
0 20)
1858 (with-input-from-string (in data
)
1859 (with-output-to-string (out)
1860 (alexandria:copy-stream in out
:end
20))))))
1869 (let ((data (shuffle (coerce (iota 10000 :start i
) 'vector
)))
1871 (unless (eql i
(extremum data
#'<))
1873 (unless (eql i
(extremum (coerce data
'list
) #'<))
1875 (unless (eql (+ 9999 i
) (extremum data
#'>))
1877 (unless (eql (+ 9999 i
) (extremum (coerce data
'list
) #'>))
1881 (when (eql 10 (extremum #(100 1 10 1000) #'> :start
1 :end
3))
1883 (when (eql -
1000 (extremum #(100 1 10 -
1000) #'> :key
'abs
))
1885 (when (eq nil
(extremum "" (lambda (a b
) (error "wtf? ~S, ~S" a b
))))
1890 (deftest starts-with-subseq.start1
1891 (starts-with-subseq "foo" "oop" :start1
1)
1895 (deftest starts-with-subseq.start2
1896 (starts-with-subseq "foo" "xfoop" :start2
1)
1900 (deftest format-symbol.print-case-bound
1901 (let ((upper (intern "FOO-BAR"))
1902 (lower (intern "foo-bar"))
1903 (*print-escape
* nil
))
1905 (let ((*print-case
* :downcase
))
1906 (and (eq upper
(format-symbol t
"~A" upper
))
1907 (eq lower
(format-symbol t
"~A" lower
))))
1908 (let ((*print-case
* :upcase
))
1909 (and (eq upper
(format-symbol t
"~A" upper
))
1910 (eq lower
(format-symbol t
"~A" lower
))))
1911 (let ((*print-case
* :capitalize
))
1912 (and (eq upper
(format-symbol t
"~A" upper
))
1913 (eq lower
(format-symbol t
"~A" lower
))))))
1918 (deftest iota.fp-start-and-complex-integer-step
1919 (equal '(#C
(0.0
0.0) #C
(0.0
2.0) #C
(0.0
4.0))
1920 (iota 3 :start
0.0 :step
#C
(0 2)))
1923 (deftest parse-ordinary-lambda-list
.1
1924 (multiple-value-bind (req opt rest keys allowp aux keyp
)
1925 (parse-ordinary-lambda-list '(a b c
&optional d
&key
))
1926 (and (equal '(a b c
) req
)
1927 (equal '((d nil nil
)) opt
)