3 (eval-when (:compile-toplevel
:load-toplevel
)
8 (defpackage :alexandria-test
9 (:use
:cl
:alexandria
:sb-rt
))
11 (in-package :alexandria-test
)
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
)))))
32 (deftest array-index
.1
33 (typep 0 'array-index
)
78 (let ((x (whichever 1 2 3)))
79 (and (member x
'(1 2 3)) t
))
89 (deftest define-constant
.1
90 (let ((name (gensym)))
91 (eval `(define-constant ,name
"FOO" :test equal
))
92 (eval `(define-constant ,name
"FOO" :test equal
))
93 (values (equal "FOO" (symbol-value name
))
98 (deftest define-constant
.2
99 (let ((name (gensym)))
100 (eval `(define-constant ,name
13))
101 (eval `(define-constant ,name
13))
102 (values (eql 13 (symbol-value name
))
109 (deftest required-argument
.1
110 (multiple-value-bind (res err
)
111 (ignore-errors (required-argument))
117 (deftest ensure-hash-table
.1
118 (let ((table (make-hash-table))
120 (multiple-value-bind (value already-there
)
121 (ensure-gethash x table
42)
124 (= 42 (gethash x table
))
125 (multiple-value-bind (value2 already-there2
)
126 (ensure-gethash x table
13)
129 (= 42 (gethash x table
)))))))
132 (deftest copy-hash-table
.1
133 (let ((orig (make-hash-table :test
'eq
:size
123))
135 (setf (gethash orig orig
) t
136 (gethash foo orig
) t
)
137 (let ((eq-copy (copy-hash-table orig
))
138 (eql-copy (copy-hash-table orig
:test
'eql
))
139 (equal-copy (copy-hash-table orig
:test
'equal
))
140 (equalp-copy (copy-hash-table orig
:test
'equalp
)))
141 (list (hash-table-size eq-copy
)
142 (hash-table-count eql-copy
)
143 (gethash orig eq-copy
)
144 (gethash (copy-seq foo
) eql-copy
)
145 (gethash foo eql-copy
)
146 (gethash (copy-seq foo
) equal-copy
)
147 (gethash "FOO" equal-copy
)
148 (gethash "FOO" equalp-copy
))))
149 (123 2 t nil t t nil t
))
151 (deftest maphash-keys
.1
153 (table (make-hash-table)))
154 (declare (notinline maphash-keys
))
156 (setf (gethash i table
) t
))
157 (maphash-keys (lambda (k) (push k keys
)) table
)
158 (set-equal keys
'(0 1 2 3 4 5 6 7 8 9)))
161 (deftest maphash-values
.1
163 (table (make-hash-table)))
164 (declare (notinline maphash-values
))
166 (setf (gethash i table
) (- i
)))
167 (maphash-values (lambda (v) (push v vals
)) table
)
168 (set-equal vals
'(0 -
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9)))
171 (deftest hash-table-keys
.1
172 (let ((table (make-hash-table)))
174 (setf (gethash i table
) t
))
175 (set-equal (hash-table-keys table
) '(0 1 2 3 4 5 6 7 8 9)))
178 (deftest hash-table-values
.1
179 (let ((table (make-hash-table)))
181 (setf (gethash (gensym) table
) i
))
182 (set-equal (hash-table-values table
) '(0 1 2 3 4 5 6 7 8 9)))
185 (deftest hash-table-alist
.1
186 (let ((table (make-hash-table)))
188 (setf (gethash i table
) (- i
)))
189 (let ((alist (hash-table-alist table
)))
195 (10 (0 .
0) (3 . -
3) (9 . -
9) nil
))
197 (deftest hash-table-plist
.1
198 (let ((table (make-hash-table)))
200 (setf (gethash i table
) (- i
)))
201 (let ((plist (hash-table-plist table
)))
209 (deftest alist-hash-table
.1
210 (let* ((alist '((0 a
) (1 b
) (2 c
)))
211 (table (alist-hash-table alist
)))
212 (list (hash-table-count table
)
216 (hash-table-test table
)))
219 (deftest plist-hash-table
.1
220 (let* ((plist '(:a
1 :b
2 :c
3))
221 (table (plist-hash-table plist
:test
'eq
)))
222 (list (hash-table-count table
)
228 (hash-table-test table
)))
229 (3 1 2 3 nil nil eq
))
234 (let ((disjunction (disjoin (lambda (x)
235 (and (consp x
) :cons
))
237 (and (stringp x
) :string
)))))
238 (list (funcall disjunction
'zot
)
239 (funcall disjunction
'(foo bar
))
240 (funcall disjunction
"test")))
244 (let ((conjunction (conjoin #'consp
249 (list (funcall conjunction
'zot
)
250 (funcall conjunction
'(foo))
251 (funcall conjunction
'("foo"))))
255 (let ((composite (compose '1+
258 #'read-from-string
)))
259 (funcall composite
"1"))
264 (locally (declare (notinline compose
))
268 #'read-from-string
))))
269 (funcall composite
"2"))
273 (let ((compose-form (funcall (compiler-macro-function 'compose
)
279 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
283 (deftest multiple-value-compose
.1
284 (let ((composite (multiple-value-compose
289 (with-input-from-string (s x
)
290 (values (read s
) (read s
)))))))
291 (multiple-value-list (funcall composite
"2 7")))
294 (deftest multiple-value-compose
.2
295 (let ((composite (locally (declare (notinline multiple-value-compose
))
296 (multiple-value-compose
301 (with-input-from-string (s x
)
302 (values (read s
) (read s
))))))))
303 (multiple-value-list (funcall composite
"2 11")))
306 (deftest multiple-value-compose
.3
307 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose
)
308 '(multiple-value-compose
313 (with-input-from-string (s x
)
314 (values (read s
) (read s
)))))
316 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
317 (multiple-value-list (funcall fun
"2 9"))))
321 (let ((curried (curry '+ 3)))
322 (funcall curried
1 5))
326 (let ((curried (locally (declare (notinline curry
))
332 (let ((curried-form (funcall (compiler-macro-function 'curry
)
335 (let ((fun (funcall (compile nil
`(lambda () ,curried-form
)))))
340 (let ((r (rcurry '/ 2)))
344 (deftest named-lambda
.1
345 (let ((fac (named-lambda fac
(x)
352 (deftest named-lambda
.2
353 (let ((fac (named-lambda fac
(&key x
)
355 (* x
(fac :x
(- x
1)))
362 (deftest alist-plist
.1
363 (alist-plist '((a .
1) (b .
2) (c .
3)))
366 (deftest plist-alist
.1
367 (plist-alist '(a 1 b
2 c
3))
368 ((a .
1) (b .
2) (c .
3)))
371 (let* ((list (list 1 2 3))
373 (unionf list
(list 1 2 4))
374 (values (equal orig
(list 1 2 3))
375 (eql (length list
) 4)
376 (set-difference list
(list 1 2 3 4))
377 (set-difference (list 1 2 3 4) list
)))
384 (let ((list (list 1 2 3)))
385 (nunionf list
(list 1 2 4))
386 (values (eql (length list
) 4)
387 (set-difference (list 1 2 3 4) list
)
388 (set-difference list
(list 1 2 3 4))))
394 (let* ((list (list 1 2 3))
396 (appendf list
'(4 5 6) '(7 8))
397 (list list
(eq list orig
)))
398 ((1 2 3 4 5 6 7 8) nil
))
401 (let ((list1 (list 1 2 3))
402 (list2 (list 4 5 6)))
403 (nconcf list1 list2
(list 7 8 9))
407 (deftest circular-list
.1
408 (let ((circle (circular-list 1 2 3)))
413 (eq circle
(nthcdr 3 circle
))))
416 (deftest circular-list-p
.1
417 (let* ((circle (circular-list 1 2 3 4))
418 (tree (list circle circle
))
419 (dotted (cons circle t
))
420 (proper (list 1 2 3 circle
))
421 (tailcirc (list* 1 2 3 circle
)))
422 (list (circular-list-p circle
)
423 (circular-list-p tree
)
424 (circular-list-p dotted
)
425 (circular-list-p proper
)
426 (circular-list-p tailcirc
)))
429 (deftest circular-list-p
.2
430 (circular-list-p 'foo
)
433 (deftest circular-tree-p
.1
434 (let* ((circle (circular-list 1 2 3 4))
435 (tree1 (list circle circle
))
436 (tree2 (let* ((level2 (list 1 nil
2))
437 (level1 (list level2
)))
438 (setf (second level2
) level1
)
440 (dotted (cons circle t
))
441 (proper (list 1 2 3 circle
))
442 (tailcirc (list* 1 2 3 circle
))
443 (quite-proper (list 1 2 3))
444 (quite-dotted (list 1 (cons 2 3))))
445 (list (circular-tree-p circle
)
446 (circular-tree-p tree1
)
447 (circular-tree-p tree2
)
448 (circular-tree-p dotted
)
449 (circular-tree-p proper
)
450 (circular-tree-p tailcirc
)
451 (circular-tree-p quite-proper
)
452 (circular-tree-p quite-dotted
)))
453 (t t t t t t nil nil
))
455 (deftest proper-list-p
.1
459 (l4 (list (cons 1 2) 3))
460 (l5 (circular-list 1 2)))
461 (list (proper-list-p l1
)
468 (deftest proper-list-p
.2
469 (proper-list-p '(1 2 .
3))
472 (deftest proper-list.type
.1
476 (l4 (list (cons 1 2) 3))
477 (l5 (circular-list 1 2)))
478 (list (typep l1
'proper-list
)
479 (typep l2
'proper-list
)
480 (typep l3
'proper-list
)
481 (typep l4
'proper-list
)
482 (typep l5
'proper-list
)))
492 (deftest lastcar.error
.2
495 (lastcar (circular-list 1 2 3))
501 (deftest setf-lastcar
.1
502 (let ((l (list 1 2 3 4)))
505 (setf (lastcar l
) 42)
510 (deftest setf-lastcar
.2
511 (let ((l (circular-list 1 2 3)))
512 (multiple-value-bind (res err
)
513 (ignore-errors (setf (lastcar l
) 4))
514 (typep err
'type-error
)))
517 (deftest make-circular-list
.1
518 (let ((l (make-circular-list 3 :initial-element
:x
)))
520 (list (eq l
(nthcdr 3 l
))
527 (deftest circular-list.type
.1
528 (let* ((l1 (list 1 2 3))
529 (l2 (circular-list 1 2 3))
530 (l3 (list* 1 2 3 l2
)))
531 (list (typep l1
'circular-list
)
532 (typep l2
'circular-list
)
533 (typep l3
'circular-list
)))
536 (deftest ensure-list
.1
539 (list (ensure-list x
)
543 (deftest ensure-cons
.1
547 (values (ensure-cons x
)
579 (setp '(a :a
) :key
'character
)
583 (setp '(a :a
) :key
'character
:test
(constantly nil
))
587 (set-equal '(1 2 3) '(3 1 2))
591 (set-equal '("Xa") '("Xb")
592 :test
(lambda (a b
) (eql (char a
0) (char b
0))))
596 (set-equal '(1 2) '(4 2))
600 (set-equal '(a b c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
604 (set-equal '(a d c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
608 (set-equal '(a b c
) '(a b c d
))
611 (deftest map-product
.1
612 (map-product 'cons
'(2 3) '(1 4))
613 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
615 (deftest map-product
.2
616 (map-product #'cons
'(2 3) '(1 4))
617 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
620 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
623 (deftest remove-from-plist
.1
624 (let ((orig '(a 1 b
2 c
3 d
4)))
625 (list (remove-from-plist orig
'a
'c
)
626 (remove-from-plist orig
'b
'd
)
627 (remove-from-plist orig
'b
)
628 (remove-from-plist orig
'a
)
629 (remove-from-plist orig
'd
42 "zot")
630 (remove-from-plist orig
'a
'b
'c
'd
)
631 (remove-from-plist orig
'a
'b
'c
'd
'x
)
632 (equal orig
'(a 1 b
2 c
3 d
4))))
643 (mappend (compose 'list
'*) '(1 2 3) '(1 2 3))
649 (list (clamp 1.5 1 2)
656 (deftest gaussian-random
.1
659 (multiple-value-bind (g1 g2
)
660 (gaussian-random min max
)
661 (values (<= min g1 max
)
674 (iota 3 :start
0.0d0
)
678 (iota 3 :start
2 :step
3.0)
683 (declare (notinline map-iota
))
684 (values (map-iota (lambda (x) (push x all
))
713 (median '(100 0 99 1 98 2 97))
717 (median '(100 0 99 1 98 2 97 96))
721 (variance (list 1 2 3))
724 (deftest standard-deviation
.1
725 (< 0 (standard-deviation (list 1 2 3)) 1)
748 (let ((xv (vector 0 0 0))
750 (maxf (svref xv
(incf p
)) (incf p
))
761 (let ((xv (vector 10 10 10))
763 (minf (svref xv
(incf p
)) (incf p
))
770 (deftest array-index.type
)
778 (list (rotate (list 1 2 3) 0)
779 (rotate (list 1 2 3) 1)
780 (rotate (list 1 2 3) 2)
781 (rotate (list 1 2 3) 3)
782 (rotate (list 1 2 3) 4))
790 (list (rotate (vector 1 2 3 4) 0)
791 (rotate (vector 1 2 3 4))
792 (rotate (vector 1 2 3 4) 2)
793 (rotate (vector 1 2 3 4) 3)
794 (rotate (vector 1 2 3 4) 4)
795 (rotate (vector 1 2 3 4) 5))
804 (list (rotate (list 1 2 3) 0)
805 (rotate (list 1 2 3) -
1)
806 (rotate (list 1 2 3) -
2)
807 (rotate (list 1 2 3) -
3)
808 (rotate (list 1 2 3) -
4))
816 (list (rotate (vector 1 2 3 4) 0)
817 (rotate (vector 1 2 3 4) -
1)
818 (rotate (vector 1 2 3 4) -
2)
819 (rotate (vector 1 2 3 4) -
3)
820 (rotate (vector 1 2 3 4) -
4)
821 (rotate (vector 1 2 3 4) -
5))
830 (values (rotate (list 1) 17)
831 (rotate (list 1) -
5))
836 (let ((s (shuffle (iota 100))))
837 (list (equal s
(iota 100))
842 (typep x
'(integer 0 99)))
847 (let ((s (shuffle (coerce (iota 100) 'vector
))))
848 (list (equal s
(coerce (iota 100) 'vector
))
853 (typep x
'(integer 0 99)))
857 (deftest random-elt
.1
858 (let ((s1 #(1 2 3 4))
860 (list (dotimes (i 1000 nil
)
861 (unless (member (random-elt s1
) s2
)
863 (when (/= (random-elt s1
) (random-elt s1
))
865 (dotimes (i 1000 nil
)
866 (unless (member (random-elt s2
) s2
)
868 (when (/= (random-elt s2
) (random-elt s2
))
896 (deftest proper-sequence.type
.1
898 (typep x
'proper-sequence
))
902 (circular-list 1 2 3 4)))
914 (deftest sequence-of-length-p
.1
915 (mapcar #'sequence-of-length-p
936 (t t t t t t nil nil nil nil
))
938 (deftest copy-sequence
.1
939 (let ((l (list 1 2 3))
940 (v (vector #\a #\b #\c
)))
941 (declare (notinline copy-sequence
))
942 (let ((l.list
(copy-sequence 'list l
))
943 (l.vector
(copy-sequence 'vector l
))
944 (l.spec-v
(copy-sequence '(vector fixnum
) l
))
945 (v.vector
(copy-sequence 'vector v
))
946 (v.list
(copy-sequence 'list v
))
947 (v.string
(copy-sequence 'string v
)))
948 (list (member l
(list l.list l.vector l.spec-v
))
949 (member v
(list v.vector v.list v.string
))
951 (equalp l.vector
#(1 2 3))
952 (eq 'fixnum
(array-element-type l.spec-v
))
954 (equal v.list
'(#\a #\b #\c
))
955 (equal "abc" v.string
))))
956 (nil nil t t t t t t
))
965 (deftest first-elt.error
.1
980 (deftest setf-first-elt
.1
981 (let ((l (list 1 2 3))
982 (s (copy-seq "foobar"))
983 (v (vector :a
:b
:c
)))
984 (setf (first-elt l
) -
1
992 (deftest setf-first-elt.error
.1
994 (multiple-value-bind (res err
)
995 (ignore-errors (setf (first-elt l
) 4))
996 (typep err
'type-error
)))
1008 (deftest last-elt.error
.1
1018 (circular-list 1 2 3)
1019 (list* 1 2 3 (circular-list 4 5))))
1027 (deftest setf-last-elt
.1
1028 (let ((l (list 1 2 3))
1029 (s (copy-seq "foobar"))
1030 (b (copy-seq #*010101001)))
1031 (setf (last-elt l
) '???
1039 (deftest setf-last-elt.error
.1
1041 (setf (last-elt 'foo
) 13)
1046 (deftest starts-with
.1
1047 (list (starts-with 1 '(1 2 3))
1048 (starts-with 1 #(1 2 3))
1049 (starts-with #\x
"xyz")
1050 (starts-with 2 '(1 2 3))
1051 (starts-with 3 #(1 2 3))
1053 (starts-with nil nil
))
1054 (t t t nil nil nil nil
))
1056 (deftest starts-with
.2
1057 (values (starts-with 1 '(-1 2 3) :key
'-
)
1058 (starts-with "foo" '("foo" "bar") :test
'equal
)
1059 (starts-with "f" '(#\f) :key
'string
:test
'equal
)
1060 (starts-with -
1 '(0 1 2) :key
#'1+)
1061 (starts-with "zot" '("ZOT") :test
'equal
))
1068 (deftest ends-with
.1
1069 (list (ends-with 3 '(1 2 3))
1070 (ends-with 3 #(1 2 3))
1071 (ends-with #\z
"xyz")
1072 (ends-with 2 '(1 2 3))
1073 (ends-with 1 #(1 2 3))
1075 (ends-with nil nil
))
1076 (t t t nil nil nil nil
))
1078 (deftest ends-with
.2
1079 (values (ends-with 2 '(0 13 1) :key
'1+)
1080 (ends-with "foo" (vector "bar" "foo") :test
'equal
)
1081 (ends-with "X" (vector 1 2 #\X
) :key
'string
:test
'equal
)
1082 (ends-with "foo" "foo" :test
'equal
))
1088 (deftest ends-with.error
.1
1090 (ends-with 3 (circular-list 3 3 3 1 3 3))
1095 (deftest with-unique-names
.1
1096 (let ((*gensym-counter
* 0))
1097 (let ((syms (with-unique-names (foo bar quux
)
1098 (list foo bar quux
))))
1099 (list (find-if #'symbol-package syms
)
1100 (equal '("FOO0" "BAR1" "QUUX2")
1101 (mapcar #'symbol-name syms
)))))
1104 (deftest with-unique-names
.2
1105 (let ((*gensym-counter
* 0))
1106 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-
) (quux #\q
))
1107 (list foo bar quux
))))
1108 (list (find-if #'symbol-package syms
)
1109 (equal '("_foo_0" "-BAR-1" "q2")
1110 (mapcar #'symbol-name syms
)))))
1113 (deftest with-unique-names
.3
1114 (let ((*gensym-counter
* 0))
1115 (multiple-value-bind (res err
)
1119 (with-unique-names ((foo "_foo_") (bar -bar-
) (quux 42))
1120 (list foo bar quux
))))
1121 (list (find-if #'symbol-package syms
)
1122 (equal '("_foo_0" "-BAR-1" "q2")
1123 (mapcar #'symbol-name syms
))))))
1124 (typep err
'error
)))
1127 (deftest once-only
.1
1128 (macrolet ((cons1.good
(x)
1134 (list (cons1.good
(incf y
))
1136 (cons1.bad
(incf y
))
1138 ((1 .
1) 1 (2 .
3) 3))
1140 (deftest parse-body
.1
1141 (parse-body '("doc" "body") :documentation t
)
1146 (deftest parse-body
.2
1147 (parse-body '("body") :documentation t
)
1152 (deftest parse-body
.3
1153 (parse-body '("doc" "body"))
1158 (deftest parse-body
.4
1159 (parse-body '((declare (foo)) "doc" (declare (bar)) body
) :documentation t
)
1161 ((declare (foo)) (declare (bar)))
1164 (deftest parse-body
.5
1165 (parse-body '((declare (foo)) "doc" (declare (bar)) body
))
1166 ("doc" (declare (bar)) body
)
1170 (deftest parse-body
.6
1171 (multiple-value-bind (res err
)
1173 (parse-body '("foo" "bar" "quux")
1180 (deftest ensure-symbol
.1
1181 (ensure-symbol :cons
:cl
)
1185 (deftest ensure-symbol
.2
1186 (ensure-symbol "CONS" :alexandria
)
1190 (deftest ensure-symbol
.3
1191 (ensure-symbol 'foo
:keyword
)
1195 (deftest ensure-symbol
.4
1196 (ensure-symbol #\
* :alexandria
)
1200 (deftest format-symbol
.1
1201 (let ((s (format-symbol nil
"X-~D" 13)))
1202 (list (symbol-package s
)
1206 (deftest format-symbol
.2
1207 (format-symbol :keyword
"SYM-~A" :bolic
)
1210 (deftest format-symbol
.3
1211 (let ((*package
* (find-package :cl
)))
1212 (format-symbol t
"FIND-~A" 'package
))
1215 (deftest make-keyword
.1
1216 (list (make-keyword 'zot
)
1217 (make-keyword "FOO")
1221 (deftest make-gensym-list
.1
1222 (let ((*gensym-counter
* 0))
1223 (let ((syms (make-gensym-list 3 "FOO")))
1224 (list (find-if 'symbol-package syms
)
1225 (equal '("FOO0" "FOO1" "FOO2")
1226 (mapcar 'symbol-name syms
)))))
1229 (deftest make-gensym-list
.2
1230 (let ((*gensym-counter
* 0))
1231 (let ((syms (make-gensym-list 3)))
1232 (list (find-if 'symbol-package syms
)
1233 (equal '("G0" "G1" "G2")
1234 (mapcar 'symbol-name syms
)))))
1241 (declare (notinline of-type
))
1242 (let ((f (of-type 'string
)))
1243 (list (funcall f
"foo")
1248 (type= 'string
'string
)
1253 (type= 'list
'(or null cons
))
1258 (type= 'null
'(and symbol list
))
1263 (type= 'string
'(satisfies emptyp
))
1268 (type= 'string
'list
)
1274 (declaim (notinline opaque
))
1279 (if-let (x (opaque :ok
))
1285 (if-let (x (opaque nil
))
1311 (deftest if-let.error
.1
1330 (y (prog1 x
(setf x nil
))))
1341 (deftest if-let
*.error
.1
1343 (eval '(if-let* x
:oops
:oops
))
1349 (when-let (x (opaque :ok
))
1368 (deftest when-let.error
.1
1370 (eval '(when-let x
:oops
))
1375 (deftest when-let
*.1
1382 (deftest when-let
*.2
1388 (deftest when-let
*.error
.1
1390 (eval '(when-let* x
:oops
))