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
))
384 (let ((list1 (list 1 2 3))
385 (list2 (list 4 5 6)))
386 (nconcf list1 list2
(list 7 8 9))
390 (deftest circular-list
.1
391 (let ((circle (circular-list 1 2 3)))
396 (eq circle
(nthcdr 3 circle
))))
399 (deftest circular-list-p
.1
400 (let* ((circle (circular-list 1 2 3 4))
401 (tree (list circle circle
))
402 (dotted (cons circle t
))
403 (proper (list 1 2 3 circle
))
404 (tailcirc (list* 1 2 3 circle
)))
405 (list (circular-list-p circle
)
406 (circular-list-p tree
)
407 (circular-list-p dotted
)
408 (circular-list-p proper
)
409 (circular-list-p tailcirc
)))
412 (deftest circular-list-p
.2
413 (circular-list-p 'foo
)
416 (deftest circular-tree-p
.1
417 (let* ((circle (circular-list 1 2 3 4))
418 (tree1 (list circle circle
))
419 (tree2 (let* ((level2 (list 1 nil
2))
420 (level1 (list level2
)))
421 (setf (second level2
) level1
)
423 (dotted (cons circle t
))
424 (proper (list 1 2 3 circle
))
425 (tailcirc (list* 1 2 3 circle
))
426 (quite-proper (list 1 2 3))
427 (quite-dotted (list 1 (cons 2 3))))
428 (list (circular-tree-p circle
)
429 (circular-tree-p tree1
)
430 (circular-tree-p tree2
)
431 (circular-tree-p dotted
)
432 (circular-tree-p proper
)
433 (circular-tree-p tailcirc
)
434 (circular-tree-p quite-proper
)
435 (circular-tree-p quite-dotted
)))
436 (t t t t t t nil nil
))
438 (deftest proper-list-p
.1
442 (l4 (list (cons 1 2) 3))
443 (l5 (circular-list 1 2)))
444 (list (proper-list-p l1
)
451 (deftest proper-list-p
.2
452 (proper-list-p '(1 2 .
3))
455 (deftest proper-list.type
.1
459 (l4 (list (cons 1 2) 3))
460 (l5 (circular-list 1 2)))
461 (list (typep l1
'proper-list
)
462 (typep l2
'proper-list
)
463 (typep l3
'proper-list
)
464 (typep l4
'proper-list
)
465 (typep l5
'proper-list
)))
475 (deftest lastcar.error
.2
478 (lastcar (circular-list 1 2 3))
484 (deftest setf-lastcar
.1
485 (let ((l (list 1 2 3 4)))
488 (setf (lastcar l
) 42)
493 (deftest setf-lastcar
.2
494 (let ((l (circular-list 1 2 3)))
495 (multiple-value-bind (res err
)
496 (ignore-errors (setf (lastcar l
) 4))
497 (typep err
'type-error
)))
500 (deftest make-circular-list
.1
501 (let ((l (make-circular-list 3 :initial-element
:x
)))
503 (list (eq l
(nthcdr 3 l
))
510 (deftest circular-list.type
.1
511 (let* ((l1 (list 1 2 3))
512 (l2 (circular-list 1 2 3))
513 (l3 (list* 1 2 3 l2
)))
514 (list (typep l1
'circular-list
)
515 (typep l2
'circular-list
)
516 (typep l3
'circular-list
)))
519 (deftest ensure-list
.1
522 (list (ensure-list x
)
526 (deftest ensure-cons
.1
530 (values (ensure-cons x
)
562 (setp '(a :a
) :key
'character
)
566 (setp '(a :a
) :key
'character
:test
(constantly nil
))
570 (set-equal '(1 2 3) '(3 1 2))
574 (set-equal '("Xa") '("Xb")
575 :test
(lambda (a b
) (eql (char a
0) (char b
0))))
579 (set-equal '(1 2) '(4 2))
583 (set-equal '(a b c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
587 (set-equal '(a d c
) '(:a
:b
:c
) :key
'string
:test
'equal
)
591 (set-equal '(a b c
) '(a b c d
))
594 (deftest map-product
.1
595 (map-product 'cons
'(2 3) '(1 4))
596 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
598 (deftest map-product
.2
599 (map-product #'cons
'(2 3) '(1 4))
600 ((2 .
1) (2 .
4) (3 .
1) (3 .
4)))
603 (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
607 (let ((orig '(a 1 b
2 c
3 d
4)))
608 (list (sans orig
'a
'c
)
612 (sans orig
'd
42 "zot")
613 (sans orig
'a
'b
'c
'd
)
614 (sans orig
'a
'b
'c
'd
'x
)
615 (equal orig
'(a 1 b
2 c
3 d
4))))
626 (mappend (compose 'list
'*) '(1 2 3) '(1 2 3))
632 (list (clamp 1.5 1 2)
639 (deftest gaussian-random
.1
642 (multiple-value-bind (g1 g2
)
643 (gaussian-random min max
)
644 (values (<= min g1 max
)
657 (iota 3 :start
0.0d0
)
661 (iota 3 :start
2 :step
3.0)
666 (declare (notinline map-iota
))
667 (values (map-iota (lambda (x) (push x all
))
696 (median '(100 0 99 1 98 2 97))
700 (median '(100 0 99 1 98 2 97 96))
704 (variance (list 1 2 3))
707 (deftest standard-deviation
.1
708 (< 0 (standard-deviation (list 1 2 3)) 1)
731 (let ((xv (vector 0 0 0))
733 (maxf (svref xv
(incf p
)) (incf p
))
744 (let ((xv (vector 10 10 10))
746 (minf (svref xv
(incf p
)) (incf p
))
753 (deftest array-index.type
)
761 (list (rotate (list 1 2 3) 0)
762 (rotate (list 1 2 3) 1)
763 (rotate (list 1 2 3) 2)
764 (rotate (list 1 2 3) 3)
765 (rotate (list 1 2 3) 4))
773 (list (rotate (vector 1 2 3 4) 0)
774 (rotate (vector 1 2 3 4))
775 (rotate (vector 1 2 3 4) 2)
776 (rotate (vector 1 2 3 4) 3)
777 (rotate (vector 1 2 3 4) 4)
778 (rotate (vector 1 2 3 4) 5))
787 (list (rotate (list 1 2 3) 0)
788 (rotate (list 1 2 3) -
1)
789 (rotate (list 1 2 3) -
2)
790 (rotate (list 1 2 3) -
3)
791 (rotate (list 1 2 3) -
4))
799 (list (rotate (vector 1 2 3 4) 0)
800 (rotate (vector 1 2 3 4) -
1)
801 (rotate (vector 1 2 3 4) -
2)
802 (rotate (vector 1 2 3 4) -
3)
803 (rotate (vector 1 2 3 4) -
4)
804 (rotate (vector 1 2 3 4) -
5))
813 (values (rotate (list 1) 17)
814 (rotate (list 1) -
5))
819 (let ((s (shuffle (iota 100))))
820 (list (equal s
(iota 100))
825 (typep x
'(integer 0 99)))
830 (let ((s (shuffle (coerce (iota 100) 'vector
))))
831 (list (equal s
(coerce (iota 100) 'vector
))
836 (typep x
'(integer 0 99)))
840 (deftest random-elt
.1
841 (let ((s1 #(1 2 3 4))
843 (list (dotimes (i 1000 nil
)
844 (unless (member (random-elt s1
) s2
)
846 (when (/= (random-elt s1
) (random-elt s1
))
848 (dotimes (i 1000 nil
)
849 (unless (member (random-elt s2
) s2
)
851 (when (/= (random-elt s2
) (random-elt s2
))
879 (deftest proper-sequence.type
.1
881 (typep x
'proper-sequence
))
885 (circular-list 1 2 3 4)))
897 (deftest sequence-of-length-p
.1
898 (mapcar #'sequence-of-length-p
919 (t t t t t t nil nil nil nil
))
921 (deftest copy-sequence
.1
922 (let ((l (list 1 2 3))
923 (v (vector #\a #\b #\c
)))
924 (declare (notinline copy-sequence
))
925 (let ((l.list
(copy-sequence 'list l
))
926 (l.vector
(copy-sequence 'vector l
))
927 (l.spec-v
(copy-sequence '(vector fixnum
) l
))
928 (v.vector
(copy-sequence 'vector v
))
929 (v.list
(copy-sequence 'list v
))
930 (v.string
(copy-sequence 'string v
)))
931 (list (member l
(list l.list l.vector l.spec-v
))
932 (member v
(list v.vector v.list v.string
))
934 (equalp l.vector
#(1 2 3))
935 (eq 'fixnum
(array-element-type l.spec-v
))
937 (equal v.list
'(#\a #\b #\c
))
938 (equal "abc" v.string
))))
939 (nil nil t t t t t t
))
948 (deftest first-elt.error
.1
963 (deftest setf-first-elt
.1
964 (let ((l (list 1 2 3))
965 (s (copy-seq "foobar"))
966 (v (vector :a
:b
:c
)))
967 (setf (first-elt l
) -
1
975 (deftest setf-first-elt.error
.1
977 (multiple-value-bind (res err
)
978 (ignore-errors (setf (first-elt l
) 4))
979 (typep err
'type-error
)))
991 (deftest last-elt.error
.1
1001 (circular-list 1 2 3)
1002 (list* 1 2 3 (circular-list 4 5))))
1010 (deftest setf-last-elt
.1
1011 (let ((l (list 1 2 3))
1012 (s (copy-seq "foobar"))
1013 (b (copy-seq #*010101001)))
1014 (setf (last-elt l
) '???
1022 (deftest setf-last-elt.error
.1
1024 (setf (last-elt 'foo
) 13)
1029 (deftest starts-with
.1
1030 (list (starts-with 1 '(1 2 3))
1031 (starts-with 1 #(1 2 3))
1032 (starts-with #\x
"xyz")
1033 (starts-with 2 '(1 2 3))
1034 (starts-with 3 #(1 2 3))
1036 (starts-with nil nil
))
1037 (t t t nil nil nil nil
))
1039 (deftest starts-with
.2
1040 (values (starts-with 1 '(-1 2 3) :key
'-
)
1041 (starts-with "foo" '("foo" "bar") :test
'equal
)
1042 (starts-with "f" '(#\f) :key
'string
:test
'equal
)
1043 (starts-with -
1 '(0 1 2) :key
#'1+)
1044 (starts-with "zot" '("ZOT") :test
'equal
))
1051 (deftest ends-with
.1
1052 (list (ends-with 3 '(1 2 3))
1053 (ends-with 3 #(1 2 3))
1054 (ends-with #\z
"xyz")
1055 (ends-with 2 '(1 2 3))
1056 (ends-with 1 #(1 2 3))
1058 (ends-with nil nil
))
1059 (t t t nil nil nil nil
))
1061 (deftest ends-with
.2
1062 (values (ends-with 2 '(0 13 1) :key
'1+)
1063 (ends-with "foo" (vector "bar" "foo") :test
'equal
)
1064 (ends-with "X" (vector 1 2 #\X
) :key
'string
:test
'equal
)
1065 (ends-with "foo" "foo" :test
'equal
))
1071 (deftest ends-with.error
.1
1073 (ends-with 3 (circular-list 3 3 3 1 3 3))
1078 (deftest with-unique-names
.1
1079 (let ((*gensym-counter
* 0))
1080 (let ((syms (with-unique-names (foo bar quux
)
1081 (list foo bar quux
))))
1082 (list (find-if #'symbol-package syms
)
1083 (equal '("FOO0" "BAR1" "QUUX2")
1084 (mapcar #'symbol-name syms
)))))
1087 (deftest with-unique-names
.2
1088 (let ((*gensym-counter
* 0))
1089 (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-
) (quux #\q
))
1090 (list foo bar quux
))))
1091 (list (find-if #'symbol-package syms
)
1092 (equal '("_foo_0" "-BAR-1" "q2")
1093 (mapcar #'symbol-name syms
)))))
1096 (deftest with-unique-names
.3
1097 (let ((*gensym-counter
* 0))
1098 (multiple-value-bind (res err
)
1102 (with-unique-names ((foo "_foo_") (bar -bar-
) (quux 42))
1103 (list foo bar quux
))))
1104 (list (find-if #'symbol-package syms
)
1105 (equal '("_foo_0" "-BAR-1" "q2")
1106 (mapcar #'symbol-name syms
))))))
1107 (typep err
'error
)))
1110 (deftest once-only
.1
1111 (macrolet ((cons1.good
(x)
1117 (list (cons1.good
(incf y
))
1119 (cons1.bad
(incf y
))
1121 ((1 .
1) 1 (2 .
3) 3))
1123 (deftest parse-body
.1
1124 (parse-body '("doc" "body") :documentation t
)
1129 (deftest parse-body
.2
1130 (parse-body '("body") :documentation t
)
1135 (deftest parse-body
.3
1136 (parse-body '("doc" "body"))
1141 (deftest parse-body
.4
1142 (parse-body '((declare (foo)) "doc" (declare (bar)) body
) :documentation t
)
1144 ((declare (foo)) (declare (bar)))
1147 (deftest parse-body
.5
1148 (parse-body '((declare (foo)) "doc" (declare (bar)) body
))
1149 ("doc" (declare (bar)) body
)
1153 (deftest parse-body
.6
1154 (multiple-value-bind (res err
)
1156 (parse-body '("foo" "bar" "quux")
1163 (deftest ensure-symbol
.1
1164 (ensure-symbol :cons
:cl
)
1168 (deftest ensure-symbol
.2
1169 (ensure-symbol "CONS" :alexandria
)
1173 (deftest ensure-symbol
.3
1174 (ensure-symbol 'foo
:keyword
)
1178 (deftest ensure-symbol
.4
1179 (ensure-symbol #\
* :alexandria
)
1183 (deftest format-symbol
.1
1184 (let ((s (format-symbol nil
"X-~D" 13)))
1185 (list (symbol-package s
)
1189 (deftest format-symbol
.2
1190 (format-symbol :keyword
"SYM-~A" :bolic
)
1193 (deftest format-symbol
.3
1194 (let ((*package
* (find-package :cl
)))
1195 (format-symbol t
"FIND-~A" 'package
))
1198 (deftest make-keyword
.1
1199 (list (make-keyword 'zot
)
1200 (make-keyword "FOO")
1204 (deftest make-gensym-list
.1
1205 (let ((*gensym-counter
* 0))
1206 (let ((syms (make-gensym-list 3 "FOO")))
1207 (list (find-if 'symbol-package syms
)
1208 (equal '("FOO0" "FOO1" "FOO2")
1209 (mapcar 'symbol-name syms
)))))
1216 (declare (notinline of-type
))
1217 (let ((f (of-type 'string
)))
1218 (list (funcall f
"foo")
1223 (type= 'string
'string
)
1228 (type= 'list
'(or null cons
))
1233 (type= 'null
'(and symbol list
))
1238 (type= 'string
'(satisfies emptyp
))
1243 (type= 'string
'list
)
1249 (declaim (notinline opaque
))
1254 (if-let (x (opaque :ok
))
1260 (if-let (x (opaque nil
))
1286 (deftest if-let.error
.1
1305 (y (prog1 x
(setf x nil
))))
1316 (deftest if-let
*.error
.1
1318 (eval '(if-let* x
:oops
:oops
))
1324 (when-let (x (opaque :ok
))
1343 (deftest when-let.error
.1
1345 (eval '(when-let x
:oops
))
1350 (deftest when-let
*.1
1357 (deftest when-let
*.2
1363 (deftest when-let
*.error
.1
1365 (eval '(when-let* x
:oops
))