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
*)))
16 (let* ((orig (vector 1 2 3))
17 (copy (copy-array orig
)))
18 (values (eq orig copy
) (equalp orig copy
)))
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
)))))
33 (let* ((orig (vector 1 2 3))
34 (copy (copy-array orig
)))
35 (typep copy
'simple-array
))
39 (let ((orig (make-array 21
43 (vector-push-extend n orig
))
44 (let ((copy (copy-array orig
47 (typep copy
'simple-array
)))
50 (deftest array-index
.1
51 (typep 0 'array-index
)
56 (deftest unwind-protect-case
.1
58 (unwind-protect-case ()
60 (:normal
(push :normal result
))
61 (:abort
(push :abort result
))
62 (:always
(push :always result
)))
66 (deftest unwind-protect-case
.2
68 (unwind-protect-case ()
70 (:always
(push :always result
))
71 (:normal
(push :normal result
))
72 (:abort
(push :abort result
)))
76 (deftest unwind-protect-case
.3
77 (let (result1 result2 result3
)
79 (unwind-protect-case ()
81 (:normal
(push :normal result1
))
82 (:abort
(push :abort result1
))
83 (:always
(push :always result1
))))
85 (unwind-protect-case ()
87 (:normal
(push :normal result2
))
88 (:abort
(push :abort result2
))
89 (:always
(push :always result2
))))
91 (unwind-protect-case ()
93 (:normal
(push :normal result3
))
94 (:abort
(push :abort result3
))
95 (:always
(push :always result3
))))
96 (values result1 result2 result3
))
101 (deftest unwind-protect-case
.4
103 (unwind-protect-case (aborted-p)
105 (:always
(setq result aborted-p
)))
109 (deftest unwind-protect-case
.5
112 (unwind-protect-case (aborted-p)
114 (:always
(setq result aborted-p
))))
148 (cswitch (13 :test
=)
154 (cswitch (13 :key
1-
)
159 (deftest multiple-value-prog2.1
160 (multiple-value-prog2
166 (deftest nth-value-or
.1
167 (multiple-value-bind (a b c
)
175 (let ((x (whichever 1 2 3)))
176 (and (member x
'(1 2 3)) t
))
183 (x (whichever a b c
)))
184 (and (member x
'(1 2 3)) t
))
204 (deftest define-constant
.1
205 (let ((name (gensym)))
206 (eval `(define-constant ,name
"FOO" :test
'equal
))
207 (eval `(define-constant ,name
"FOO" :test
'equal
))
208 (values (equal "FOO" (symbol-value name
))
213 (deftest define-constant
.2
214 (let ((name (gensym)))
215 (eval `(define-constant ,name
13))
216 (eval `(define-constant ,name
13))
217 (values (eql 13 (symbol-value name
))
224 ;;; TYPEP is specified to return a generalized boolean and, for
225 ;;; example, ECL exploits this by returning the superclasses of ERROR
228 (not (null (typep x
'error
))))
230 (deftest required-argument
.1
231 (multiple-value-bind (res err
)
232 (ignore-errors (required-argument))
238 (deftest ensure-hash-table
.1
239 (let ((table (make-hash-table))
241 (multiple-value-bind (value already-there
)
242 (ensure-gethash x table
42)
245 (= 42 (gethash x table
))
246 (multiple-value-bind (value2 already-there2
)
247 (ensure-gethash x table
13)
250 (= 42 (gethash x table
)))))))
253 #+clisp
(pushnew 'copy-hash-table
.1 *expected-failures
*)
255 (deftest copy-hash-table
.1
256 (let ((orig (make-hash-table :test
'eq
:size
123))
258 (setf (gethash orig orig
) t
259 (gethash foo orig
) t
)
260 (let ((eq-copy (copy-hash-table orig
))
261 (eql-copy (copy-hash-table orig
:test
'eql
))
262 (equal-copy (copy-hash-table orig
:test
'equal
))
263 ;; CLISP overflows the stack with this bit.
264 ;; See <http://sourceforge.net/tracker/index.php?func=detail&aid=2029069&group_id=1355&atid=101355>.
265 #-clisp
(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 #-clisp
(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 #+clisp
(pushnew 'alist-hash-table
.1 *expected-failures
*)
356 (deftest alist-hash-table
.1
357 (let* ((alist '((0 a
) (1 b
) (2 c
)))
358 (table (alist-hash-table alist
)))
359 (list (hash-table-count table
)
363 (hash-table-test table
))) ; CLISP returns EXT:FASTHASH-EQL.
366 #+clisp
(pushnew 'plist-hash-table
.1 *expected-failures
*)
368 (deftest plist-hash-table
.1
369 (let* ((plist '(:a
1 :b
2 :c
3))
370 (table (plist-hash-table plist
:test
'eq
)))
371 (list (hash-table-count table
)
377 (hash-table-test table
))) ; CLISP returns EXT:FASTHASH-EQ.
378 (3 1 2 3 nil nil eq
))
383 (let ((disjunction (disjoin (lambda (x)
384 (and (consp x
) :cons
))
386 (and (stringp x
) :string
)))))
387 (list (funcall disjunction
'zot
)
388 (funcall disjunction
'(foo bar
))
389 (funcall disjunction
"test")))
393 (let ((disjunction (disjoin #'zerop
)))
394 (list (funcall disjunction
0)
395 (funcall disjunction
1)))
399 (let ((conjunction (conjoin #'consp
404 (list (funcall conjunction
'zot
)
405 (funcall conjunction
'(foo))
406 (funcall conjunction
'("foo"))))
410 (let ((conjunction (conjoin #'zerop
)))
411 (list (funcall conjunction
0)
412 (funcall conjunction
1)))
416 (let ((composite (compose '1+
419 #'read-from-string
)))
420 (funcall composite
"1"))
425 (locally (declare (notinline compose
))
429 #'read-from-string
))))
430 (funcall composite
"2"))
434 (let ((compose-form (funcall (compiler-macro-function 'compose
)
440 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
445 (let ((composite (compose #'zerop
)))
446 (list (funcall composite
0)
447 (funcall composite
1)))
450 (deftest multiple-value-compose
.1
451 (let ((composite (multiple-value-compose
456 (with-input-from-string (s x
)
457 (values (read s
) (read s
)))))))
458 (multiple-value-list (funcall composite
"2 7")))
461 (deftest multiple-value-compose
.2
462 (let ((composite (locally (declare (notinline multiple-value-compose
))
463 (multiple-value-compose
468 (with-input-from-string (s x
)
469 (values (read s
) (read s
))))))))
470 (multiple-value-list (funcall composite
"2 11")))
473 (deftest multiple-value-compose
.3
474 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose
)
475 '(multiple-value-compose
480 (with-input-from-string (s x
)
481 (values (read s
) (read s
)))))
483 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
484 (multiple-value-list (funcall fun
"2 9"))))
487 (deftest multiple-value-compose
.4
488 (let ((composite (multiple-value-compose #'truncate
)))
489 (multiple-value-list (funcall composite
9 2)))
493 (let ((curried (curry '+ 3)))
494 (funcall curried
1 5))
498 (let ((curried (locally (declare (notinline curry
))
504 (let ((curried-form (funcall (compiler-macro-function 'curry
)
507 (let ((fun (funcall (compile nil
`(lambda () ,curried-form
)))))
513 (curried (curry (progn
515 (lambda (y z
) (* x y z
)))
517 (list (funcall curried
7)
523 (let ((r (rcurry '/ 2)))
529 (curried (rcurry (progn
531 (lambda (y z
) (* x y z
)))
533 (list (funcall curried
7)
538 (deftest named-lambda
.1
539 (let ((fac (named-lambda fac
(x)
546 (deftest named-lambda
.2
547 (let ((fac (named-lambda fac
(&key x
)
549 (* x
(fac :x
(- x
1)))
556 (deftest alist-plist
.1
557 (alist-plist '((a .
1) (b .
2) (c .
3)))
560 (deftest plist-alist
.1
561 (plist-alist '(a 1 b
2 c
3))
562 ((a .
1) (b .
2) (c .
3)))
565 (let* ((list (list 1 2 3))
567 (unionf list
(list 1 2 4))
568 (values (equal orig
(list 1 2 3))
569 (eql (length list
) 4)
570 (set-difference list
(list 1 2 3 4))
571 (set-difference (list 1 2 3 4) list
)))
578 (let ((list (list 1 2 3)))
579 (nunionf list
(list 1 2 4))
580 (values (eql (length list
) 4)
581 (set-difference (list 1 2 3 4) list
)
582 (set-difference list
(list 1 2 3 4))))
588 (let* ((list (list 1 2 3))
590 (appendf list
'(4 5 6) '(7 8))
591 (list list
(eq list orig
)))
592 ((1 2 3 4 5 6 7 8) nil
))
595 (let ((list1 (list 1 2 3))
596 (list2 (list 4 5 6)))
597 (nconcf list1 list2
(list 7 8 9))
601 (deftest circular-list
.1
602 (let ((circle (circular-list 1 2 3)))
607 (eq circle
(nthcdr 3 circle
))))
610 (deftest circular-list-p
.1
611 (let* ((circle (circular-list 1 2 3 4))
612 (tree (list circle circle
))
613 (dotted (cons circle t
))
614 (proper (list 1 2 3 circle
))
615 (tailcirc (list* 1 2 3 circle
)))
616 (list (circular-list-p circle
)
617 (circular-list-p tree
)
618 (circular-list-p dotted
)
619 (circular-list-p proper
)
620 (circular-list-p tailcirc
)))
623 (deftest circular-list-p
.2
624 (circular-list-p 'foo
)
627 (deftest circular-tree-p
.1
628 (let* ((circle (circular-list 1 2 3 4))
629 (tree1 (list circle circle
))
630 (tree2 (let* ((level2 (list 1 nil
2))
631 (level1 (list level2
)))
632 (setf (second level2
) level1
)
634 (dotted (cons circle t
))
635 (proper (list 1 2 3 circle
))
636 (tailcirc (list* 1 2 3 circle
))
637 (quite-proper (list 1 2 3))
638 (quite-dotted (list 1 (cons 2 3))))
639 (list (circular-tree-p circle
)
640 (circular-tree-p tree1
)
641 (circular-tree-p tree2
)
642 (circular-tree-p dotted
)
643 (circular-tree-p proper
)
644 (circular-tree-p tailcirc
)
645 (circular-tree-p quite-proper
)
646 (circular-tree-p quite-dotted
)))
647 (t t t t t t nil nil
))
649 (deftest circular-tree-p
.2
650 (alexandria:circular-tree-p
'#1=(#1#))
653 (deftest proper-list-p
.1
657 (l4 (list (cons 1 2) 3))
658 (l5 (circular-list 1 2)))
659 (list (proper-list-p l1
)
666 (deftest proper-list-p
.2
667 (proper-list-p '(1 2 .
3))
670 (deftest proper-list.type
.1
674 (l4 (list (cons 1 2) 3))
675 (l5 (circular-list 1 2)))
676 (list (typep l1
'proper-list
)
677 (typep l2
'proper-list
)
678 (typep l3
'proper-list
)
679 (typep l4
'proper-list
)
680 (typep l5
'proper-list
)))
683 (deftest proper-list-length
.1
685 (proper-list-length nil
)
686 (proper-list-length (list 1))
687 (proper-list-length (list 2 2))
688 (proper-list-length (list 3 3 3))
689 (proper-list-length (list 4 4 4 4))
690 (proper-list-length (list 5 5 5 5 5))
691 (proper-list-length (list 6 6 6 6 6 6))
692 (proper-list-length (list 7 7 7 7 7 7 7))
693 (proper-list-length (list 8 8 8 8 8 8 8 8))
694 (proper-list-length (list 9 9 9 9 9 9 9 9 9)))
697 (deftest proper-list-length
.2
700 (proper-list-length x
)
705 (plength (list* 2 2))
706 (plength (list* 3 3 3))
707 (plength (list* 4 4 4 4))
708 (plength (list* 5 5 5 5 5))
709 (plength (list* 6 6 6 6 6 6))
710 (plength (list* 7 7 7 7 7 7 7))
711 (plength (list* 8 8 8 8 8 8 8 8))
712 (plength (list* 9 9 9 9 9 9 9 9 9))))
724 (deftest lastcar.error
.2
727 (lastcar (circular-list 1 2 3))
733 (deftest setf-lastcar
.1
734 (let ((l (list 1 2 3 4)))
737 (setf (lastcar l
) 42)
742 (deftest setf-lastcar
.2
743 (let ((l (circular-list 1 2 3)))
744 (multiple-value-bind (res err
)
745 (ignore-errors (setf (lastcar l
) 4))
746 (typep err
'type-error
)))
749 (deftest make-circular-list
.1
750 (let ((l (make-circular-list 3 :initial-element
:x
)))
752 (list (eq l
(nthcdr 3 l
))
759 (deftest circular-list.type
.1
760 (let* ((l1 (list 1 2 3))
761 (l2 (circular-list 1 2 3))
762 (l3 (list* 1 2 3 l2
)))
763 (list (typep l1
'circular-list
)
764 (typep l2
'circular-list
)
765 (typep l3
'circular-list
)))
768 (deftest ensure-list
.1
771 (list (ensure-list x
)
775 (deftest ensure-cons
.1
779 (values (ensure-cons x
)
811 (setp '(a :a
) :key
'character
)
815 (setp '(a :a
) :key
'character
:test
(constantly nil
))
819 (set-equal '(1 2 3) '(3 1 2))
823 (set-equal '("Xa") '("Xb")
824 :test
(lambda (a b
) (eql (char a
0) (char b
0))))
828 (set-equal '(1 2) '(4 2))
832 (set-equal '(a b c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
836 (set-equal '(a d c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
840 (set-equal '(a b c
) '(a b c d
))
843 (deftest map-product
.1
844 (map-product 'cons
'(2 3) '(1 4))
845 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
847 (deftest map-product
.2
848 (map-product #'cons
'(2 3) '(1 4))
849 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
852 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
855 (deftest remove-from-plist
.1
856 (let ((orig '(a 1 b
2 c
3 d
4)))
857 (list (remove-from-plist orig
'a
'c
)
858 (remove-from-plist orig
'b
'd
)
859 (remove-from-plist orig
'b
)
860 (remove-from-plist orig
'a
)
861 (remove-from-plist orig
'd
42 "zot")
862 (remove-from-plist orig
'a
'b
'c
'd
)
863 (remove-from-plist orig
'a
'b
'c
'd
'x
)
864 (equal orig
'(a 1 b
2 c
3 d
4))))
875 (mappend (compose 'list
'*) '(1 2 3) '(1 2 3))
878 (deftest assoc-value
.1
879 (let ((key1 '(complex key
))
883 (push 1 (assoc-value alist key1
:test
#'equal
))
884 (push 2 (assoc-value alist key1
:test
'equal
))
885 (push 42 (assoc-value alist key2
))
886 (push 43 (assoc-value alist key2
:test
'eq
))
887 (push (assoc-value alist key1
:test
#'equal
) result
)
888 (push (assoc-value alist key2
) result
)
890 (push 'very
(rassoc-value alist
(list 2 1) :test
#'equal
))
891 (push (cdr (assoc '(very complex key
) alist
:test
#'equal
)) result
)
893 ((2 1) (43 42) (2 1)))
898 (list (clamp 1.5 1 2)
905 (deftest gaussian-random
.1
908 (multiple-value-bind (g1 g2
)
909 (gaussian-random min max
)
910 (values (<= min g1 max
)
923 (iota 3 :start
0.0d0
)
927 (iota 3 :start
2 :step
3.0)
932 (declare (notinline map-iota
))
933 (values (map-iota (lambda (x) (push x all
))
962 (median '(100 0 99 1 98 2 97))
966 (median '(100 0 99 1 98 2 97 96))
970 (variance (list 1 2 3))
973 (deftest standard-deviation
.1
974 (< 0 (standard-deviation (list 1 2 3)) 1)
997 (let ((xv (vector 0 0 0))
999 (maxf (svref xv
(incf p
)) (incf p
))
1010 (let ((xv (vector 10 10 10))
1012 (minf (svref xv
(incf p
)) (incf p
))
1016 (deftest subfactorial
.1
1017 (mapcar #'subfactorial
(iota 22))
1039 18795307255050944540))
1044 (deftest array-index.type
)
1047 (deftest copy-array
)
1052 (list (rotate (list 1 2 3) 0)
1053 (rotate (list 1 2 3) 1)
1054 (rotate (list 1 2 3) 2)
1055 (rotate (list 1 2 3) 3)
1056 (rotate (list 1 2 3) 4))
1064 (list (rotate (vector 1 2 3 4) 0)
1065 (rotate (vector 1 2 3 4))
1066 (rotate (vector 1 2 3 4) 2)
1067 (rotate (vector 1 2 3 4) 3)
1068 (rotate (vector 1 2 3 4) 4)
1069 (rotate (vector 1 2 3 4) 5))
1078 (list (rotate (list 1 2 3) 0)
1079 (rotate (list 1 2 3) -
1)
1080 (rotate (list 1 2 3) -
2)
1081 (rotate (list 1 2 3) -
3)
1082 (rotate (list 1 2 3) -
4))
1090 (list (rotate (vector 1 2 3 4) 0)
1091 (rotate (vector 1 2 3 4) -
1)
1092 (rotate (vector 1 2 3 4) -
2)
1093 (rotate (vector 1 2 3 4) -
3)
1094 (rotate (vector 1 2 3 4) -
4)
1095 (rotate (vector 1 2 3 4) -
5))
1104 (values (rotate (list 1) 17)
1105 (rotate (list 1) -
5))
1110 (let ((s (shuffle (iota 100))))
1111 (list (equal s
(iota 100))
1116 (typep x
'(integer 0 99)))
1121 (let ((s (shuffle (coerce (iota 100) 'vector
))))
1122 (list (equal s
(coerce (iota 100) 'vector
))
1127 (typep x
'(integer 0 99)))
1131 (deftest random-elt
.1
1132 (let ((s1 #(1 2 3 4))
1134 (list (dotimes (i 1000 nil
)
1135 (unless (member (random-elt s1
) s2
)
1137 (when (/= (random-elt s1
) (random-elt s1
))
1139 (dotimes (i 1000 nil
)
1140 (unless (member (random-elt s2
) s2
)
1142 (when (/= (random-elt s2
) (random-elt s2
))
1160 (let* ((x (list 1 2 3))
1170 (deftest map-permutations
.1
1171 (let ((seq (list 1 2 3))
1174 (map-permutations (lambda (s)
1175 (unless (set-equal s seq
)
1177 (when (member s seen
:test
'equal
)
1182 (values ok
(length seen
)))
1186 (deftest proper-sequence.type
.1
1188 (typep x
'proper-sequence
))
1192 (circular-list 1 2 3 4)))
1204 (deftest sequence-of-length-p
.1
1205 (mapcar #'sequence-of-length-p
1226 (t t t t t t nil nil nil nil
))
1250 (t t t t t t nil nil nil nil
))
1253 ;; test the compiler macro
1254 (macrolet ((x (&rest args
)
1258 (length= ,@args
))))))
1266 (deftest copy-sequence
.1
1267 (let ((l (list 1 2 3))
1268 (v (vector #\a #\b #\c
)))
1269 (declare (notinline copy-sequence
))
1270 (let ((l.list
(copy-sequence 'list l
))
1271 (l.vector
(copy-sequence 'vector l
))
1272 (l.spec-v
(copy-sequence '(vector fixnum
) l
))
1273 (v.vector
(copy-sequence 'vector v
))
1274 (v.list
(copy-sequence 'list v
))
1275 (v.string
(copy-sequence 'string v
)))
1276 (list (member l
(list l.list l.vector l.spec-v
))
1277 (member v
(list v.vector v.list v.string
))
1279 (equalp l.vector
#(1 2 3))
1280 (eql (upgraded-array-element-type 'fixnum
)
1281 (array-element-type l.spec-v
))
1283 (equal v.list
'(#\a #\b #\c
))
1284 (equal "abc" v.string
))))
1285 (nil nil t t t t t t
))
1287 (deftest first-elt
.1
1294 (deftest first-elt.error
.1
1309 (deftest setf-first-elt
.1
1310 (let ((l (list 1 2 3))
1311 (s (copy-seq "foobar"))
1312 (v (vector :a
:b
:c
)))
1313 (setf (first-elt l
) -
1
1321 (deftest setf-first-elt.error
.1
1323 (multiple-value-bind (res err
)
1324 (ignore-errors (setf (first-elt l
) 4))
1325 (typep err
'type-error
)))
1337 (deftest last-elt.error
.1
1347 (circular-list 1 2 3)
1348 (list* 1 2 3 (circular-list 4 5))))
1356 (deftest setf-last-elt
.1
1357 (let ((l (list 1 2 3))
1358 (s (copy-seq "foobar"))
1359 (b (copy-seq #*010101001)))
1360 (setf (last-elt l
) '???
1368 (deftest setf-last-elt.error
.1
1370 (setf (last-elt 'foo
) 13)
1375 (deftest starts-with
.1
1376 (list (starts-with 1 '(1 2 3))
1377 (starts-with 1 #(1 2 3))
1378 (starts-with #\x
"xyz")
1379 (starts-with 2 '(1 2 3))
1380 (starts-with 3 #(1 2 3))
1382 (starts-with nil nil
))
1383 (t t t nil nil nil nil
))
1385 (deftest starts-with
.2
1386 (values (starts-with 1 '(-1 2 3) :key
'-
)
1387 (starts-with "foo" '("foo" "bar") :test
'equal
)
1388 (starts-with "f" '(#\f) :key
'string
:test
'equal
)
1389 (starts-with -
1 '(0 1 2) :key
#'1+)
1390 (starts-with "zot" '("ZOT") :test
'equal
))
1397 (deftest ends-with
.1
1398 (list (ends-with 3 '(1 2 3))
1399 (ends-with 3 #(1 2 3))
1400 (ends-with #\z
"xyz")
1401 (ends-with 2 '(1 2 3))
1402 (ends-with 1 #(1 2 3))
1404 (ends-with nil nil
))
1405 (t t t nil nil nil nil
))
1407 (deftest ends-with
.2
1408 (values (ends-with 2 '(0 13 1) :key
'1+)
1409 (ends-with "foo" (vector "bar" "foo") :test
'equal
)
1410 (ends-with "X" (vector 1 2 #\X
) :key
'string
:test
'equal
)
1411 (ends-with "foo" "foo" :test
'equal
))
1417 (deftest ends-with.error
.1
1419 (ends-with 3 (circular-list 3 3 3 1 3 3))
1424 (deftest sequences.passing-improper-lists
1425 (macrolet ((signals-error-p (form)
1430 (cut (fn &rest args
)
1432 (print`(lambda (,arg
)
1433 (apply ,fn
(list ,@(substitute arg
'_ args
))))))))
1434 (let ((circular-list (make-circular-list 5 :initial-element
:foo
))
1435 (dotted-list (list* 'a
'b
'c
'd
)))
1436 (loop for nth from
0
1442 (cut #'random-elt _
)
1444 (cut #'ends-with
:foo _
))
1446 (let ((on-circular-p (signals-error-p (funcall fn circular-list
)))
1447 (on-dotted-p (signals-error-p (funcall fn dotted-list
))))
1448 (when (or (not on-circular-p
) (not on-dotted-p
))
1450 (unless on-circular-p
1451 (let ((*print-circle
* t
))
1454 "No appropriate error signalled when passing ~S to ~Ath entry."
1455 circular-list nth
))))
1459 "No appropriate error signalled when passing ~S to ~Ath entry."
1460 dotted-list nth
)))))))))
1463 (deftest with-unique-names
.1
1464 (let ((*gensym-counter
* 0))
1465 (let ((syms (with-unique-names (foo bar quux
)
1466 (list foo bar quux
))))
1467 (list (find-if #'symbol-package syms
)
1468 (equal '("FOO0" "BAR1" "QUUX2")
1469 (mapcar #'symbol-name syms
)))))
1472 (deftest with-unique-names
.2
1473 (let ((*gensym-counter
* 0))
1474 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-
) (quux #\q
))
1475 (list foo bar quux
))))
1476 (list (find-if #'symbol-package syms
)
1477 (equal '("_foo_0" "-BAR-1" "q2")
1478 (mapcar #'symbol-name syms
)))))
1481 (deftest with-unique-names
.3
1482 (let ((*gensym-counter
* 0))
1483 (multiple-value-bind (res err
)
1487 (with-unique-names ((foo "_foo_") (bar -bar-
) (quux 42))
1488 (list foo bar quux
))))
1489 (list (find-if #'symbol-package syms
)
1490 (equal '("_foo_0" "-BAR-1" "q2")
1491 (mapcar #'symbol-name syms
))))))
1495 (deftest once-only
.1
1496 (macrolet ((cons1.good
(x)
1502 (list (cons1.good
(incf y
))
1504 (cons1.bad
(incf y
))
1506 ((1 .
1) 1 (2 .
3) 3))
1508 (deftest once-only
.2
1509 (macrolet ((cons1 (x)
1513 (list (cons1 (incf z
))
1516 ((1 .
1) 1 (2 .
2)))
1518 (deftest parse-body
.1
1519 (parse-body '("doc" "body") :documentation t
)
1524 (deftest parse-body
.2
1525 (parse-body '("body") :documentation t
)
1530 (deftest parse-body
.3
1531 (parse-body '("doc" "body"))
1536 (deftest parse-body
.4
1537 (parse-body '((declare (foo)) "doc" (declare (bar)) body
) :documentation t
)
1539 ((declare (foo)) (declare (bar)))
1542 (deftest parse-body
.5
1543 (parse-body '((declare (foo)) "doc" (declare (bar)) body
))
1544 ("doc" (declare (bar)) body
)
1548 (deftest parse-body
.6
1549 (multiple-value-bind (res err
)
1551 (parse-body '("foo" "bar" "quux")
1558 (deftest ensure-symbol
.1
1559 (ensure-symbol :cons
:cl
)
1563 (deftest ensure-symbol
.2
1564 (ensure-symbol "CONS" :alexandria
)
1568 (deftest ensure-symbol
.3
1569 (ensure-symbol 'foo
:keyword
)
1573 (deftest ensure-symbol
.4
1574 (ensure-symbol #\
* :alexandria
)
1578 (deftest format-symbol
.1
1579 (let ((s (format-symbol nil
"X-~D" 13)))
1580 (list (symbol-package s
)
1584 (deftest format-symbol
.2
1585 (format-symbol :keyword
"SYM-~A" :bolic
)
1588 (deftest format-symbol
.3
1589 (let ((*package
* (find-package :cl
)))
1590 (format-symbol t
"FIND-~A" 'package
))
1593 (deftest make-keyword
.1
1594 (list (make-keyword 'zot
)
1595 (make-keyword "FOO")
1599 (deftest make-gensym-list
.1
1600 (let ((*gensym-counter
* 0))
1601 (let ((syms (make-gensym-list 3 "FOO")))
1602 (list (find-if 'symbol-package syms
)
1603 (equal '("FOO0" "FOO1" "FOO2")
1604 (mapcar 'symbol-name syms
)))))
1607 (deftest make-gensym-list
.2
1608 (let ((*gensym-counter
* 0))
1609 (let ((syms (make-gensym-list 3)))
1610 (list (find-if 'symbol-package syms
)
1611 (equal '("G0" "G1" "G2")
1612 (mapcar 'symbol-name syms
)))))
1619 (declare (notinline of-type
))
1620 (let ((f (of-type 'string
)))
1621 (list (funcall f
"foo")
1626 (type= 'string
'string
)
1631 (type= 'list
'(or null cons
))
1636 (type= 'null
'(and symbol list
))
1641 (type= 'string
'(satisfies emptyp
))
1646 (type= 'string
'list
)
1651 ((test (type numbers
)
1652 `(deftest ,(format-symbol t
"CDR5.~A" type
)
1653 (let ((numbers ,numbers
))
1654 (values (mapcar (of-type ',(format-symbol t
"NEGATIVE-~A" type
)) numbers
)
1655 (mapcar (of-type ',(format-symbol t
"NON-POSITIVE-~A" type
)) numbers
)
1656 (mapcar (of-type ',(format-symbol t
"NON-NEGATIVE-~A" type
)) numbers
)
1657 (mapcar (of-type ',(format-symbol t
"POSITIVE-~A" type
)) numbers
)))
1658 (t t t nil nil nil nil
)
1659 (t t t t nil nil nil
)
1660 (nil nil nil t t t t
)
1661 (nil nil nil nil t t t
))))
1662 (test fixnum
(list most-negative-fixnum -
42 -
1 0 1 42 most-positive-fixnum
))
1663 (test integer
(list (1- most-negative-fixnum
) -
42 -
1 0 1 42 (1+ most-positive-fixnum
)))
1664 (test rational
(list (1- most-negative-fixnum
) -
42/13 -
1 0 1 42/13 (1+ most-positive-fixnum
)))
1665 (test real
(list most-negative-long-float -
42/13 -
1 0 1 42/13 most-positive-long-float
))
1666 (test float
(list most-negative-short-float -
42.02 -
1.0 0.0 1.0 42.02 most-positive-short-float
))
1667 (test short-float
(list most-negative-short-float -
42.02s0 -
1.0s0
0.0s0
1.0s0
42.02s0 most-positive-short-float
))
1668 (test single-float
(list most-negative-single-float -
42.02f0 -
1.0f0
0.0f0
1.0f0
42.02f0 most-positive-single-float
))
1669 (test double-float
(list most-negative-double-float -
42.02d0 -
1.0d0
0.0d0
1.0d0
42.02d0 most-positive-double-float
))
1670 (test long-float
(list most-negative-long-float -
42.02l0 -
1.0l0 0.0l0 1.0l0 42.02l0 most-positive-long-float
)))
1674 (declaim (notinline opaque
))
1679 (if-let (x (opaque :ok
))
1685 (if-let (x (opaque nil
))
1711 (deftest if-let.error
.1
1721 (when-let (x (opaque :ok
))
1740 (deftest when-let.error
.1
1742 (eval '(when-let x
:oops
))
1747 (deftest when-let
*.1
1754 (deftest when-let
*.2
1760 (deftest when-let
*.3
1767 (deftest when-let
*.error
.1
1769 (eval '(when-let* x
:oops
))
1776 (doplist (k v
'(a 1 b
2 c
3) (values t
(reverse keys
) (reverse values
) k v
))
1785 (deftest count-permutations
.1
1786 (values (count-permutations 31 7)
1787 (count-permutations 1 1)
1788 (count-permutations 2 1)
1789 (count-permutations 2 2)
1790 (count-permutations 3 2)
1791 (count-permutations 3 1))
1799 (deftest binomial-coefficient
.1
1800 (alexandria:binomial-coefficient
1239 139)
1801 28794902202288970200771694600561826718847179309929858835480006683522184441358211423695124921058123706380656375919763349913245306834194782172712255592710204598527867804110129489943080460154)