7 (defpackage :alexandria-test
8 (:use
:cl
:alexandria
:sb-rt
))
10 (in-package :alexandria-test
)
15 (let* ((orig (vector 1 2 3))
16 (copy (copy-array orig
)))
17 (values (eq orig copy
) (equalp orig copy
)))
21 (let ((orig (make-array 1024 :fill-pointer
0)))
22 (vector-push-extend 1 orig
)
23 (vector-push-extend 2 orig
)
24 (vector-push-extend 3 orig
)
25 (let ((copy (copy-array orig
)))
26 (values (eq orig copy
) (equalp orig copy
)
27 (array-has-fill-pointer-p copy
)
28 (eql (fill-pointer orig
) (fill-pointer copy
)))))
31 (deftest array-index
.1
32 (typep 0 'array-index
)
44 (switch (13 :default
:yay
)
76 (let ((x (whichever 1 2 3)))
77 (and (member x
'(1 2 3)) t
))
87 (deftest define-constant
.1
88 (let ((name (gensym)))
89 (eval `(define-constant ,name
"FOO" :test equal
))
90 (eval `(define-constant ,name
"FOO" :test equal
))
91 (values (equal "FOO" (symbol-value name
))
96 (deftest define-constant
.2
97 (let ((name (gensym)))
98 (eval `(define-constant ,name
13))
99 (eval `(define-constant ,name
13))
100 (values (eql 13 (symbol-value name
))
107 (deftest required-argument
.1
108 (multiple-value-bind (res err
)
109 (ignore-errors (required-argument))
115 (deftest copy-hash-table
.1
116 (let ((orig (make-hash-table :test
'eq
:size
123))
118 (setf (gethash orig orig
) t
119 (gethash foo orig
) t
)
120 (let ((eq-copy (copy-hash-table orig
))
121 (eql-copy (copy-hash-table orig
:test
'eql
))
122 (equal-copy (copy-hash-table orig
:test
'equal
))
123 (equalp-copy (copy-hash-table orig
:test
'equalp
)))
124 (list (hash-table-size eq-copy
)
125 (hash-table-count eql-copy
)
126 (gethash orig eq-copy
)
127 (gethash (copy-seq foo
) eql-copy
)
128 (gethash foo eql-copy
)
129 (gethash (copy-seq foo
) equal-copy
)
130 (gethash "FOO" equal-copy
)
131 (gethash "FOO" equalp-copy
))))
132 (123 2 t nil t t nil t
))
134 (deftest maphash-keys
.1
136 (table (make-hash-table)))
137 (declare (notinline maphash-keys
))
139 (setf (gethash i table
) t
))
140 (maphash-keys (lambda (k) (push k keys
)) table
)
141 (set-equal keys
'(0 1 2 3 4 5 6 7 8 9)))
144 (deftest maphash-values
.1
146 (table (make-hash-table)))
147 (declare (notinline maphash-values
))
149 (setf (gethash i table
) (- i
)))
150 (maphash-values (lambda (v) (push v vals
)) table
)
151 (set-equal vals
'(0 -
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9)))
154 (deftest hash-table-keys
.1
155 (let ((table (make-hash-table)))
157 (setf (gethash i table
) t
))
158 (set-equal (hash-table-keys table
) '(0 1 2 3 4 5 6 7 8 9)))
161 (deftest hash-table-values
.1
162 (let ((table (make-hash-table)))
164 (setf (gethash (gensym) table
) i
))
165 (set-equal (hash-table-values table
) '(0 1 2 3 4 5 6 7 8 9)))
168 (deftest hash-table-alist
.1
169 (let ((table (make-hash-table)))
171 (setf (gethash i table
) (- i
)))
172 (let ((alist (hash-table-alist table
)))
178 (10 (0 .
0) (3 . -
3) (9 . -
9) nil
))
180 (deftest hash-table-plist
.1
181 (let ((table (make-hash-table)))
183 (setf (gethash i table
) (- i
)))
184 (let ((plist (hash-table-plist table
)))
192 (deftest alist-hash-table
.1
193 (let* ((alist '((0 a
) (1 b
) (2 c
)))
194 (table (alist-hash-table alist
)))
195 (list (hash-table-count table
)
199 (hash-table-test table
)))
202 (deftest plist-hash-table
.1
203 (let* ((plist '(:a
1 :b
2 :c
3))
204 (table (plist-hash-table plist
:test
'eq
)))
205 (list (hash-table-count table
)
211 (hash-table-test table
)))
212 (3 1 2 3 nil nil eq
))
217 (let ((disjunction (disjoin (lambda (x)
218 (and (consp x
) :cons
))
220 (and (stringp x
) :string
)))))
221 (list (funcall disjunction
'zot
)
222 (funcall disjunction
'(foo bar
))
223 (funcall disjunction
"test")))
227 (let ((conjunction (conjoin #'consp
232 (list (funcall conjunction
'zot
)
233 (funcall conjunction
'(foo))
234 (funcall conjunction
'("foo"))))
238 (let ((composite (compose '1+
241 #'read-from-string
)))
242 (funcall composite
"1"))
247 (locally (declare (notinline compose
))
251 #'read-from-string
))))
252 (funcall composite
"2"))
256 (let ((compose-form (funcall (compiler-macro-function 'compose
)
262 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
266 (deftest multiple-value-compose
.1
267 (let ((composite (multiple-value-compose
272 (with-input-from-string (s x
)
273 (values (read s
) (read s
)))))))
274 (multiple-value-list (funcall composite
"2 7")))
277 (deftest multiple-value-compose
.2
278 (let ((composite (locally (declare (notinline multiple-value-compose
))
279 (multiple-value-compose
284 (with-input-from-string (s x
)
285 (values (read s
) (read s
))))))))
286 (multiple-value-list (funcall composite
"2 11")))
289 (deftest multiple-value-compose
.3
290 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose
)
291 '(multiple-value-compose
296 (with-input-from-string (s x
)
297 (values (read s
) (read s
)))))
299 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
300 (multiple-value-list (funcall fun
"2 9"))))
304 (let ((curried (curry '+ 3)))
305 (funcall curried
1 5))
309 (let ((curried (locally (declare (notinline curry
))
315 (let ((curried-form (funcall (compiler-macro-function 'curry
)
318 (let ((fun (funcall (compile nil
`(lambda () ,curried-form
)))))
323 (let ((r (rcurry '/ 2)))
327 (deftest named-lambda
.1
328 (let ((fac (named-lambda fac
(x)
335 (deftest named-lambda
.2
336 (let ((fac (named-lambda fac
(&key x
)
338 (* x
(fac :x
(- x
1)))
345 (deftest alist-plist
.1
346 (alist-plist '((a .
1) (b .
2) (c .
3)))
349 (deftest plist-alist
.1
350 (plist-alist '(a 1 b
2 c
3))
351 ((a .
1) (b .
2) (c .
3)))
354 (let* ((list '(1 2 3))
356 (unionf list
'(1 2 4))
357 (values (equal orig
(list 1 2 3))
358 (eql (length list
) 4)
359 (set-difference list
(list 1 2 3 4))
360 (set-difference (list 1 2 3 4) list
)))
367 (let ((list '(1 2 3)))
368 (nunionf list
'(1 2 4))
369 (values (eql (length list
) 4)
370 (set-difference (list 1 2 3 4) list
)
371 (set-difference list
(list 1 2 3 4))))
377 (let* ((list '(1 2 3))
379 (appendf list
'(4 5 6) '(7 8))
380 (list list
(eq list orig
)))
381 ((1 2 3 4 5 6 7 8) nil
))
383 (deftest circular-list
.1
384 (let ((circle (circular-list 1 2 3)))
389 (eq circle
(nthcdr 3 circle
))))
392 (deftest circular-list-p
.1
393 (let* ((circle (circular-list 1 2 3 4))
394 (tree (list circle circle
))
395 (dotted (cons circle t
))
396 (proper (list 1 2 3 circle
))
397 (tailcirc (list* 1 2 3 circle
)))
398 (list (circular-list-p circle
)
399 (circular-list-p tree
)
400 (circular-list-p dotted
)
401 (circular-list-p proper
)
402 (circular-list-p tailcirc
)))
405 (deftest circular-list-p
.2
406 (circular-list-p 'foo
)
409 (deftest circular-tree-p
.1
410 (let* ((circle (circular-list 1 2 3 4))
411 (tree1 (list circle circle
))
412 (tree2 (let* ((level2 (list 1 nil
2))
413 (level1 (list level2
)))
414 (setf (second level2
) level1
)
416 (dotted (cons circle t
))
417 (proper (list 1 2 3 circle
))
418 (tailcirc (list* 1 2 3 circle
))
419 (quite-proper (list 1 2 3))
420 (quite-dotted (list 1 (cons 2 3))))
421 (list (circular-tree-p circle
)
422 (circular-tree-p tree1
)
423 (circular-tree-p tree2
)
424 (circular-tree-p dotted
)
425 (circular-tree-p proper
)
426 (circular-tree-p tailcirc
)
427 (circular-tree-p quite-proper
)
428 (circular-tree-p quite-dotted
)))
429 (t t t t t t nil nil
))
431 (deftest proper-list-p
.1
435 (l4 (list (cons 1 2) 3))
436 (l5 (circular-list 1 2)))
437 (list (proper-list-p l1
)
444 (deftest proper-list-p
.2
445 (proper-list-p '(1 2 .
3))
448 (deftest proper-list.type
.1
452 (l4 (list (cons 1 2) 3))
453 (l5 (circular-list 1 2)))
454 (list (typep l1
'proper-list
)
455 (typep l2
'proper-list
)
456 (typep l3
'proper-list
)
457 (typep l4
'proper-list
)
458 (typep l5
'proper-list
)))
468 (deftest lastcar.error
.2
471 (lastcar (circular-list 1 2 3))
477 (deftest setf-lastcar
.1
478 (let ((l (list 1 2 3 4)))
481 (setf (lastcar l
) 42)
486 (deftest setf-lastcar
.2
487 (let ((l (circular-list 1 2 3)))
488 (multiple-value-bind (res err
)
489 (ignore-errors (setf (lastcar l
) 4))
490 (typep err
'type-error
)))
493 (deftest make-circular-list
.1
494 (let ((l (make-circular-list 3 :initial-element
:x
)))
496 (list (eq l
(nthcdr 3 l
))
503 (deftest circular-list.type
.1
504 (let* ((l1 (list 1 2 3))
505 (l2 (circular-list 1 2 3))
506 (l3 (list* 1 2 3 l2
)))
507 (list (typep l1
'circular-list
)
508 (typep l2
'circular-list
)
509 (typep l3
'circular-list
)))
512 (deftest ensure-list
.1
515 (list (ensure-list x
)
519 (deftest ensure-cons
.1
523 (values (ensure-cons x
)
555 (setp '(a :a
) :key
'character
)
559 (setp '(a :a
) :key
'character
:test
(constantly nil
))
563 (set-equal '(1 2 3) '(3 1 2))
567 (set-equal '("Xa") '("Xb")
568 :test
(lambda (a b
) (eql (char a
0) (char b
0))))
572 (set-equal '(1 2) '(4 2))
576 (set-equal '(a b c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
580 (set-equal '(a d c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
584 (set-equal '(a b c
) '(a b c d
))
587 (deftest map-product
.1
588 (map-product 'cons
'(2 3) '(1 4))
589 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
591 (deftest map-product
.2
592 (map-product #'cons
'(2 3) '(1 4))
593 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
596 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
600 (let ((orig '(a 1 b
2 c
3 d
4)))
601 (list (sans orig
'a
'c
)
605 (sans orig
'd
42 "zot")
606 (sans orig
'a
'b
'c
'd
)
607 (sans orig
'a
'b
'c
'd
'x
)
608 (equal orig
'(a 1 b
2 c
3 d
4))))
619 (mappend (compose 'list
'*) '(1 2 3) '(1 2 3))
625 (list (clamp 1.5 1 2)
632 (deftest gaussian-random
.1
635 (multiple-value-bind (g1 g2
)
636 (gaussian-random min max
)
637 (values (<= min g1 max
)
650 (iota 3 :start
0.0d0
)
654 (iota 3 :start
2 :step
3.0)
659 (declare (notinline map-iota
))
660 (values (map-iota (lambda (x) (push x all
))
689 (median '(100 0 99 1 98 2 97))
693 (median '(100 0 99 1 98 2 97 96))
697 (variance (list 1 2 3))
700 (deftest standard-deviation
.1
701 (< 0 (standard-deviation (list 1 2 3)) 1)
724 (let ((xv (vector 0 0 0))
726 (maxf (svref xv
(incf p
)) (incf p
))
737 (let ((xv (vector 10 10 10))
739 (minf (svref xv
(incf p
)) (incf p
))
746 (deftest array-index.type
)
754 (list (rotate (list 1 2 3) 0)
755 (rotate (list 1 2 3) 1)
756 (rotate (list 1 2 3) 2)
757 (rotate (list 1 2 3) 3)
758 (rotate (list 1 2 3) 4))
766 (list (rotate (vector 1 2 3 4) 0)
767 (rotate (vector 1 2 3 4))
768 (rotate (vector 1 2 3 4) 2)
769 (rotate (vector 1 2 3 4) 3)
770 (rotate (vector 1 2 3 4) 4)
771 (rotate (vector 1 2 3 4) 5))
780 (list (rotate (list 1 2 3) 0)
781 (rotate (list 1 2 3) -
1)
782 (rotate (list 1 2 3) -
2)
783 (rotate (list 1 2 3) -
3)
784 (rotate (list 1 2 3) -
4))
792 (list (rotate (vector 1 2 3 4) 0)
793 (rotate (vector 1 2 3 4) -
1)
794 (rotate (vector 1 2 3 4) -
2)
795 (rotate (vector 1 2 3 4) -
3)
796 (rotate (vector 1 2 3 4) -
4)
797 (rotate (vector 1 2 3 4) -
5))
806 (values (rotate (list 1) 17)
807 (rotate (list 1) -
5))
812 (let ((s (shuffle (iota 100))))
813 (list (equal s
(iota 100))
818 (typep x
'(integer 0 99)))
823 (let ((s (shuffle (coerce (iota 100) 'vector
))))
824 (list (equal s
(coerce (iota 100) 'vector
))
829 (typep x
'(integer 0 99)))
833 (deftest random-elt
.1
834 (let ((s1 #(1 2 3 4))
836 (list (dotimes (i 1000 nil
)
837 (unless (member (random-elt s1
) s2
)
839 (when (/= (random-elt s1
) (random-elt s1
))
841 (dotimes (i 1000 nil
)
842 (unless (member (random-elt s2
) s2
)
844 (when (/= (random-elt s2
) (random-elt s2
))
872 (deftest proper-sequence.type
.1
874 (typep x
'proper-sequence
))
878 (circular-list 1 2 3 4)))
890 (deftest sequence-of-length-p
.1
891 (mapcar #'sequence-of-length-p
912 (t t t t t t nil nil nil nil
))
914 (deftest copy-sequence
.1
915 (let ((l (list 1 2 3))
916 (v (vector #\a #\b #\c
)))
917 (declare (notinline copy-sequence
))
918 (let ((l.list
(copy-sequence 'list l
))
919 (l.vector
(copy-sequence 'vector l
))
920 (l.spec-v
(copy-sequence '(vector fixnum
) l
))
921 (v.vector
(copy-sequence 'vector v
))
922 (v.list
(copy-sequence 'list v
))
923 (v.string
(copy-sequence 'string v
)))
924 (list (member l
(list l.list l.vector l.spec-v
))
925 (member v
(list v.vector v.list v.string
))
927 (equalp l.vector
#(1 2 3))
928 (eq 'fixnum
(array-element-type l.spec-v
))
930 (equal v.list
'(#\a #\b #\c
))
931 (equal "abc" v.string
))))
932 (nil nil t t t t t t
))
941 (deftest first-elt.error
.1
956 (deftest setf-first-elt
.1
957 (let ((l (list 1 2 3))
958 (s (copy-seq "foobar"))
959 (v (vector :a
:b
:c
)))
960 (setf (first-elt l
) -
1
968 (deftest setf-first-elt.error
.1
970 (multiple-value-bind (res err
)
971 (ignore-errors (setf (first-elt l
) 4))
972 (typep err
'type-error
)))
984 (deftest last-elt.error
.1
994 (circular-list 1 2 3)
995 (list* 1 2 3 (circular-list 4 5))))
1003 (deftest setf-last-elt
.1
1004 (let ((l (list 1 2 3))
1005 (s (copy-seq "foobar"))
1006 (b (copy-seq #*010101001)))
1007 (setf (last-elt l
) '???
1015 (deftest setf-last-elt.error
.1
1017 (setf (last-elt 'foo
) 13)
1022 (deftest starts-with
.1
1023 (list (starts-with 1 '(1 2 3))
1024 (starts-with 1 #(1 2 3))
1025 (starts-with #\x
"xyz")
1026 (starts-with 2 '(1 2 3))
1027 (starts-with 3 #(1 2 3))
1029 (starts-with nil nil
))
1030 (t t t nil nil nil nil
))
1032 (deftest starts-with
.2
1033 (values (starts-with 1 '(-1 2 3) :key
'-
)
1034 (starts-with "foo" '("foo" "bar") :test
'equal
)
1035 (starts-with "f" '(#\f) :key
'string
:test
'equal
)
1036 (starts-with -
1 '(0 1 2) :key
#'1+)
1037 (starts-with "zot" '("ZOT") :test
'equal
))
1044 (deftest ends-with
.1
1045 (list (ends-with 3 '(1 2 3))
1046 (ends-with 3 #(1 2 3))
1047 (ends-with #\z
"xyz")
1048 (ends-with 2 '(1 2 3))
1049 (ends-with 1 #(1 2 3))
1051 (ends-with nil nil
))
1052 (t t t nil nil nil nil
))
1054 (deftest ends-with
.2
1055 (values (ends-with 2 '(0 13 1) :key
'1+)
1056 (ends-with "foo" (vector "bar" "foo") :test
'equal
)
1057 (ends-with "X" (vector 1 2 #\X
) :key
'string
:test
'equal
)
1058 (ends-with "foo" "foo" :test
'equal
))
1064 (deftest ends-with.error
.1
1066 (ends-with 3 (circular-list 3 3 3 1 3 3))
1071 (deftest with-unique-names
.1
1072 (let ((*gensym-counter
* 0))
1073 (let ((syms (with-unique-names (foo bar quux
)
1074 (list foo bar quux
))))
1075 (list (find-if #'symbol-package syms
)
1076 (equal '("FOO0" "BAR1" "QUUX2")
1077 (mapcar #'symbol-name syms
)))))
1080 (deftest with-unique-names
.2
1081 (let ((*gensym-counter
* 0))
1082 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-
) (quux #\q
))
1083 (list foo bar quux
))))
1084 (list (find-if #'symbol-package syms
)
1085 (equal '("_foo_0" "-BAR-1" "q2")
1086 (mapcar #'symbol-name syms
)))))
1089 (deftest with-unique-names
.3
1090 (let ((*gensym-counter
* 0))
1091 (multiple-value-bind (res err
)
1095 (with-unique-names ((foo "_foo_") (bar -bar-
) (quux 42))
1096 (list foo bar quux
))))
1097 (list (find-if #'symbol-package syms
)
1098 (equal '("_foo_0" "-BAR-1" "q2")
1099 (mapcar #'symbol-name syms
))))))
1100 (typep err
'error
)))
1103 (deftest once-only
.1
1104 (macrolet ((cons1.good
(x)
1110 (list (cons1.good
(incf y
))
1112 (cons1.bad
(incf y
))
1114 ((1 .
1) 1 (2 .
3) 3))
1116 (deftest parse-body
.1
1117 (parse-body '("doc" "body") :documentation t
)
1122 (deftest parse-body
.2
1123 (parse-body '("body") :documentation t
)
1128 (deftest parse-body
.3
1129 (parse-body '("doc" "body"))
1134 (deftest parse-body
.4
1135 (parse-body '((declare (foo)) "doc" (declare (bar)) body
) :documentation t
)
1137 ((declare (foo)) (declare (bar)))
1140 (deftest parse-body
.5
1141 (parse-body '((declare (foo)) "doc" (declare (bar)) body
))
1142 ("doc" (declare (bar)) body
)
1146 (deftest parse-body
.6
1147 (multiple-value-bind (res err
)
1149 (parse-body '("foo" "bar" "quux")
1156 (deftest ensure-symbol
.1
1157 (ensure-symbol :cons
:cl
)
1161 (deftest ensure-symbol
.2
1162 (ensure-symbol "CONS" :alexandria
)
1166 (deftest ensure-symbol
.3
1167 (ensure-symbol 'foo
:keyword
)
1171 (deftest ensure-symbol
.4
1172 (ensure-symbol #\
* :alexandria
)
1176 (deftest format-symbol
.1
1177 (let ((s (format-symbol nil
"X-~D" 13)))
1178 (list (symbol-package s
)
1182 (deftest format-symbol
.2
1183 (format-symbol :keyword
"SYM-~A" :bolic
)
1186 (deftest format-symbol
.3
1187 (let ((*package
* (find-package :cl
)))
1188 (format-symbol t
"FIND-~A" 'package
))
1191 (deftest make-keyword
.1
1192 (list (make-keyword 'zot
)
1193 (make-keyword "FOO")
1197 (deftest make-gensym-list
.1
1198 (let ((*gensym-counter
* 0))
1199 (let ((syms (make-gensym-list 3 "FOO")))
1200 (list (find-if 'symbol-package syms
)
1201 (equal '("FOO0" "FOO1" "FOO2")
1202 (mapcar 'symbol-name syms
)))))
1209 (declare (notinline of-type
))
1210 (let ((f (of-type 'string
)))
1211 (list (funcall f
"foo")
1216 (type= 'string
'string
)
1221 (type= 'list
'(or null cons
))
1226 (type= 'null
'(and symbol list
))
1231 (type= 'string
'(satisfies emptyp
))
1236 (type= 'string
'list
)
1242 (declaim (notinline opaque
))
1247 (if-let (x (opaque :ok
))
1253 (if-let (x (opaque nil
))
1279 (deftest if-let.error
.1
1298 (y (prog1 x
(setf x nil
))))
1309 (deftest if-let
*.error
.1
1311 (eval '(if-let* x
:oops
:oops
))
1317 (when-let (x (opaque :ok
))
1336 (deftest when-let.error
.1
1338 (eval '(when-let x
:oops
))
1343 (deftest when-let
*.1
1350 (deftest when-let
*.2
1356 (deftest when-let
*.error
.1
1358 (eval '(when-let* x
:oops
))