7 (defpackage :alexandria-test
8 (:use
:cl
:alexandria
:sb-rt
))
10 (in-package :alexandria-test
)
14 (deftest copy-hash-table
.1
15 (let ((orig (make-hash-table :test
'eq
:size
123))
17 (setf (gethash orig orig
) t
19 (let ((eq-copy (copy-hash-table orig
))
20 (eql-copy (copy-hash-table orig
:test
'eql
))
21 (equal-copy (copy-hash-table orig
:test
'equal
))
22 (equalp-copy (copy-hash-table orig
:test
'equalp
)))
23 (list (hash-table-size eq-copy
)
24 (hash-table-count eql-copy
)
25 (gethash orig eq-copy
)
26 (gethash (copy-seq foo
) eql-copy
)
27 (gethash foo eql-copy
)
28 (gethash (copy-seq foo
) equal-copy
)
29 (gethash "FOO" equal-copy
)
30 (gethash "FOO" equalp-copy
))))
31 (123 2 t nil t t nil t
))
33 (deftest maphash-keys
.1
35 (table (make-hash-table)))
37 (setf (gethash i table
) t
))
38 (maphash-keys (lambda (k) (push k keys
)) table
)
39 (set-equal keys
'(0 1 2 3 4 5 6 7 8 9)))
42 (deftest maphash-values
.1
44 (table (make-hash-table)))
46 (setf (gethash i table
) (- i
)))
47 (maphash-values (lambda (v) (push v vals
)) table
)
48 (set-equal vals
'(0 -
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9)))
51 (deftest hash-table-keys
.1
52 (let ((table (make-hash-table)))
54 (setf (gethash i table
) t
))
55 (set-equal (hash-table-keys table
) '(0 1 2 3 4 5 6 7 8 9)))
58 (deftest hash-table-values
.1
59 (let ((table (make-hash-table)))
61 (setf (gethash (gensym) table
) i
))
62 (set-equal (hash-table-values table
) '(0 1 2 3 4 5 6 7 8 9)))
65 (deftest hash-table-alist
.1
66 (let ((table (make-hash-table)))
68 (setf (gethash i table
) (- i
)))
69 (let ((alist (hash-table-alist table
)))
75 (10 (0 .
0) (3 . -
3) (9 . -
9) nil
))
77 (deftest hash-table-plist
.1
78 (let ((table (make-hash-table)))
80 (setf (gethash i table
) (- i
)))
81 (let ((plist (hash-table-plist table
)))
89 (deftest alist-hash-table
.1
90 (let* ((alist '((0 a
) (1 b
) (2 c
)))
91 (table (alist-hash-table alist
)))
92 (list (hash-table-count table
)
96 (hash-table-test table
)))
99 (deftest plist-hash-table
.1
100 (let* ((plist '(:a
1 :b
2 :c
3))
101 (table (plist-hash-table plist
:test
'eq
)))
102 (list (hash-table-count table
)
108 (hash-table-test table
)))
109 (3 1 2 3 nil nil eq
))
114 (let ((disjunction (disjoin (lambda (x)
115 (and (consp x
) :cons
))
117 (and (stringp x
) :string
)))))
118 (list (funcall disjunction
'zot
)
119 (funcall disjunction
'(foo bar
))
120 (funcall disjunction
"test")))
124 (let ((conjunction (conjoin #'consp
129 (list (funcall conjunction
'zot
)
130 (funcall conjunction
'(foo))
131 (funcall conjunction
'("foo"))))
135 (let ((composite (compose '1+
138 #'read-from-string
)))
139 (funcall composite
"1"))
144 (locally (declare (notinline compose
))
148 #'read-from-string
))))
149 (funcall composite
"2"))
153 (let ((compose-form (funcall (compiler-macro-function 'compose
)
159 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
163 (deftest multiple-value-compose
.1
164 (let ((composite (multiple-value-compose
169 (with-input-from-string (s x
)
170 (values (read s
) (read s
)))))))
171 (multiple-value-list (funcall composite
"2 7")))
174 (deftest multiple-value-compose
.2
175 (let ((composite (locally (declare (notinline multiple-value-compose
))
176 (multiple-value-compose
181 (with-input-from-string (s x
)
182 (values (read s
) (read s
))))))))
183 (multiple-value-list (funcall composite
"2 11")))
186 (deftest multiple-value-compose
.3
187 (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose
)
188 '(multiple-value-compose
193 (with-input-from-string (s x
)
194 (values (read s
) (read s
)))))
196 (let ((fun (funcall (compile nil
`(lambda () ,compose-form
)))))
197 (multiple-value-list (funcall fun
"2 9"))))
201 (let ((curried (curry '+ 3)))
202 (funcall curried
1 5))
206 (let ((curried (locally (declare (notinline curry
))
212 (let ((curried-form (funcall (compiler-macro-function 'curry
)
215 (let ((fun (funcall (compile nil
`(lambda () ,curried-form
)))))
220 (let ((r (rcurry '/ 2)))
227 (let* ((list '(1 2 3))
229 (appendf list
'(4 5 6) '(7 8))
230 (list list
(eq list orig
)))
231 ((1 2 3 4 5 6 7 8) nil
))
233 (deftest circular-list
.1
234 (let ((circle (circular-list 1 2 3)))
239 (eq circle
(nthcdr 3 circle
))))
242 (deftest circular-list-p
.1
243 (let* ((circle (circular-list 1 2 3 4))
244 (tree (list circle circle
))
245 (dotted (cons circle t
))
246 (proper (list 1 2 3 circle
))
247 (tailcirc (list* 1 2 3 circle
)))
248 (list (circular-list-p circle
)
249 (circular-list-p tree
)
250 (circular-list-p dotted
)
251 (circular-list-p proper
)
252 (circular-list-p tailcirc
)))
255 (deftest circular-tree-p
.1
256 (let* ((circle (circular-list 1 2 3 4))
257 (tree1 (list circle circle
))
258 (tree2 (let* ((level2 (list 1 nil
2))
259 (level1 (list level2
)))
260 (setf (second level2
) level1
)
262 (dotted (cons circle t
))
263 (proper (list 1 2 3 circle
))
264 (tailcirc (list* 1 2 3 circle
))
265 (quite-proper (list 1 2 3))
266 (quite-dotted (list 1 (cons 2 3))))
267 (list (circular-tree-p circle
)
268 (circular-tree-p tree1
)
269 (circular-tree-p tree2
)
270 (circular-tree-p dotted
)
271 (circular-tree-p proper
)
272 (circular-tree-p tailcirc
)
273 (circular-tree-p quite-proper
)
274 (circular-tree-p quite-dotted
)))
275 (t t t t t t nil nil
))
277 (deftest proper-list-p
.1
281 (l4 (list (cons 1 2) 3))
282 (l5 (circular-list 1 2)))
283 (list (proper-list-p l1
)
290 (deftest proper-list.type
.1
294 (l4 (list (cons 1 2) 3))
295 (l5 (circular-list 1 2)))
296 (list (typep l1
'proper-list
)
297 (typep l2
'proper-list
)
298 (typep l3
'proper-list
)
299 (typep l4
'proper-list
)
300 (typep l5
'proper-list
)))
310 (deftest lastcar.error
.2
313 (lastcar (circular-list 1 2 3))
319 (deftest make-circular-list
.1
320 (let ((l (make-circular-list 3 :initial-element
:x
)))
322 (list (eq l
(nthcdr 3 l
))
329 (deftest circular-list.type
.1
330 (let* ((l1 (list 1 2 3))
331 (l2 (circular-list 1 2 3))
332 (l3 (list* 1 2 3 l2
)))
333 (list (typep l1
'circular-list
)
334 (typep l2
'circular-list
)
335 (typep l3
'circular-list
)))
338 (deftest ensure-list
.1
341 (list (ensure-list x
)
345 (deftest remove-keys
.1
346 (let* ((orig '(a 1 b
2 c
3 d
4))
347 (copy (copy-seq orig
)))
348 (list (remove-keys '(a c
) copy
)
349 (remove-keys '(b d
) copy
)
350 (remove-keys '(b) copy
)
351 (remove-keys '(a) copy
)
352 (remove-keys '(d) copy
)
353 (remove-keys '(a b c d
) copy
)
354 (remove-keys '(a b c d e
) copy
)
366 (mappend (compose 'list
'*) '(1 2 3) '(1 2 3))
372 (list (clamp 1.5 1 2)
380 (deftest gaussian-random
.1
389 (iota 3 :start
0.0d0
)
393 (iota 3 :start
2 :step
3.0)
417 (median '(100 0 99 1 98 2 97))
421 (median '(100 0 99 1 98 2 97 96))
428 (deftest standard-deviation
)
450 (let ((xv (vector 0 0 0))
452 (maxf (svref xv
(incf p
)) (incf p
))
463 (let ((xv (vector 10 10 10))
465 (minf (svref xv
(incf p
)) (incf p
))
472 (deftest array-index.type
)
479 (deftest rotate-left
.1
480 (list (rotate-left (list 1 2 3) 0)
481 (rotate-left (list 1 2 3))
482 (rotate-left (list 1 2 3) 2)
483 (rotate-left (list 1 2 3) 3)
484 (rotate-left (list 1 2 3) 4))
491 (deftest rotate-left
.2
492 (list (rotate-left (vector 1 2 3 4) 0)
493 (rotate-left (vector 1 2 3 4))
494 (rotate-left (vector 1 2 3 4) 2)
495 (rotate-left (vector 1 2 3 4) 3)
496 (rotate-left (vector 1 2 3 4) 4)
497 (rotate-left (vector 1 2 3 4) 5))
505 (deftest rotate-right
.1
506 (list (rotate-right (list 1 2 3) 0)
507 (rotate-right (list 1 2 3))
508 (rotate-right (list 1 2 3) 2)
509 (rotate-right (list 1 2 3) 3)
510 (rotate-right (list 1 2 3) 4))
517 (deftest rotate-right
.2
518 (list (rotate-right (vector 1 2 3 4) 0)
519 (rotate-right (vector 1 2 3 4))
520 (rotate-right (vector 1 2 3 4) 2)
521 (rotate-right (vector 1 2 3 4) 3)
522 (rotate-right (vector 1 2 3 4) 4)
523 (rotate-right (vector 1 2 3 4) 5))
532 (let ((s (suffle (iota 100))))
533 (list (equal s
(iota 100))
538 (typep x
'(integer 0 99)))
542 (deftest random-elt
.1
543 (let ((s1 #(1 2 3 4))
545 (list (dotimes (i 1000 nil
)
546 (unless (member (random-elt s1
) s2
)
548 (when (/= (random-elt s1
) (random-elt s1
))
550 (dotimes (i 1000 nil
)
551 (unless (member (random-elt s2
) s2
)
553 (when (/= (random-elt s2
) (random-elt s2
))
581 (deftest proper-sequence.type
.1
583 (typep x
'proper-sequence
))
587 (circular-list 1 2 3 4)))
599 (deftest sequence-of-length-p
.1
600 (mapcar #'sequence-of-length-p
621 (t t t t t t nil nil nil nil
))
623 (deftest copy-sequence
.1
624 (let ((l (list 1 2 3))
625 (v (vector #\a #\b #\c
)))
626 (let ((l.list
(copy-sequence 'list l
))
627 (l.vector
(copy-sequence 'vector l
))
628 (l.spec-v
(copy-sequence '(vector fixnum
) l
))
629 (v.vector
(copy-sequence 'vector v
))
630 (v.list
(copy-sequence 'list v
))
631 (v.string
(copy-sequence 'string v
)))
632 (list (member l
(list l.list l.vector l.spec-v
))
633 (member v
(list v.vector v.list v.string
))
635 (equalp l.vector
#(1 2 3))
636 (eq 'fixnum
(array-element-type l.spec-v
))
638 (equal v.list
'(#\a #\b #\c
))
639 (equal "abc" v.string
))))
640 (nil nil t t t t t t
))
649 (deftest first-elt.error
.1
673 (deftest last-elt.error
.1
683 (circular-list 1 2 3)
684 (list* 1 2 3 (circular-list 4 5))))
692 (deftest starts-with
.1
693 (list (starts-with 1 '(1 2 3))
694 (starts-with 1 #(1 2 3))
695 (starts-with #\x
"xyz")
696 (starts-with 2 '(1 2 3))
697 (starts-with 3 #(1 2 3))
699 (starts-with nil nil
))
700 (t t t nil nil nil nil
))
703 (list (ends-with 3 '(1 2 3))
704 (ends-with 3 #(1 2 3))
705 (ends-with #\z
"xyz")
706 (ends-with 2 '(1 2 3))
707 (ends-with 1 #(1 2 3))
710 (t t t nil nil nil nil
))
712 (deftest ends-with.error
.1
714 (ends-with 3 (circular-list 3 3 3 1 3 3))
719 (deftest with-unique-names
.1
720 (let ((*gensym-counter
* 0))
721 (let ((syms (with-unique-names (foo bar quux
)
722 (list foo bar quux
))))
723 (list (find-if #'symbol-package syms
)
724 (equal '("FOO0" "BAR1" "QUUX2")
725 (mapcar #'symbol-name syms
)))))
729 (macrolet ((cons1.good
(x)
735 (list (cons1.good
(incf y
))
739 ((1 .
1) 1 (2 .
3) 3))
741 (deftest parse-body
.1
742 (parse-body '("doc" "body") :documentation t
)
747 (deftest parse-body
.2
748 (parse-body '("body") :documentation t
)
753 (deftest parse-body
.3
754 (parse-body '("doc" "body"))
759 (deftest parse-body
.4
760 (parse-body '((declare (foo)) "doc" (declare (bar)) body
) :documentation t
)
762 ((declare (foo)) (declare (bar)))
765 (deftest parse-body
.5
766 (parse-body '((declare (foo)) "doc" (declare (bar)) body
))
767 ("doc" (declare (bar)) body
)
773 (deftest ensure-symbol
.1
774 (ensure-symbol :cons
:cl
)
777 (deftest ensure-symbol
.2
778 (ensure-symbol "CONS")
781 (deftest ensure-symbol
.3
782 (ensure-symbol 'foo
:keyword
)
785 (deftest ensure-symbol
.4
789 (deftest format-symbol
.1
790 (let ((s (format-symbol nil
"X-~D" 13)))
791 (list (symbol-package s
)
795 (deftest format-symbol
.2
796 (format-symbol :keyword
"SYM-~A" :bolic
)
799 (deftest format-symbol
.3
800 (let ((*package
* (find-package :cl
)))
801 (format-symbol t
"FIND-~A" 'package
))
804 (deftest make-keyword
.1
805 (list (make-keyword 'zot
)
810 (deftest make-gensym-list
.1
811 (let ((*gensym-counter
* 0))
812 (let ((syms (make-gensym-list 3 "FOO")))
813 (list (find-if 'symbol-package syms
)
814 (equal '("FOO0" "FOO1" "FOO2")
815 (mapcar 'symbol-name syms
)))))
821 (let ((f (of-type 'string
)))
822 (list (funcall f
"foo")
827 (type= 'string
'string
)
832 (type= 'list
'(or null cons
))
837 (type= 'null
'(and symbol list
))
842 (type= 'string
'(satisfies emptyp
))
847 (type= 'string
'list
)