Allow DO / DO* to take bare symbols as vars.
[jscl.git] / tests / seq.lisp
1 ; Functions used as :KEY argument in tests
2 (defvar halve  (lambda (x) (/ x 2)))
3 (defvar double (lambda (x) (* x 2)))
4
5 ; FIND
6 (test (find 1 #(2 1 3)))
7 (test (find 1 '(2 1 3)))
8 (test (not (find 1 #(2 2 2))))
9 (test (not (find 1 '(2 2 2))))
10 (test (not (find 1 #(1 1 1) :test-not #'=)))
11 (test (not (find 1 '(1 1 1) :test-not #'=)))
12 (test (not (find 1 #(1 2 3) :key double)))
13 (test (not (find 1 '(1 2 3) :key double)))
14
15 ; REMOVE
16 (test (not (find 1 (remove 1 #(1 2 3 1)))))
17 (test (not (find 1 (remove 1 '(1 2 3 1)))))
18 (test (not (find 2 (remove 1 #(1 2 3 1) :key halve))))
19 (test (not (find 2 (remove 1 '(1 2 3 1) :key halve))))
20 ;; TODO: Rewrite this test when EQUALP exists and works on vectors
21 (test (equal (length (remove '(1 2) #((1 2) (1 2)) :test #'equal)) 0))
22 (test (null          (remove '(1 2) '((1 2) (1 2)) :test #'equal)))
23 (test (find 2 (remove 2 #(1 2 3) :test-not #'=)))
24 (test (find 2 (remove 2 '(1 2 3) :test-not #'=)))
25
26 ; POSITION
27 (test (= (position 1 #(1 2 3))  0))
28 (test (= (position 1 '(1 2 3))  0))
29 (test (= (position 1 #(1 2 3 1)) 0))
30 (test (= (position 1 '(1 2 3 1)) 0))
31 (test (not (position 1 #(2 3 4))))
32 (test (not (position 1 '(2 3 4))))
33 (test (= (position 1 '(1 2 3) :key halve) 1))
34 (test (= (position 1 #(1 2 3) :key halve) 1))
35 (test (= (position '(1 2) '((1 2) (3 4)) :test #'equal) 0))
36 (test (= (position '(1 2) #((1 2) (3 4)) :test #'equal) 0))
37 (test (= (position 1 #(1 1 3) :test-not #'=) 2))
38 (test (= (position 1 '(1 1 3) :test-not #'=) 2))
39
40 ; REMOVE-IF
41 (test (equal (remove-if     #'zerop '(1 0 2 0 3)) '(1 2 3)))
42 (test (equal (remove-if-not #'zerop '(1 0 2 0 3)) '(0 0)))
43 ;; TODO: Rewrite these tests when EQUALP exists and works on vectors
44 (let ((v1 (remove-if #'zerop #(1 0 2 0 3))))
45   (test (and (= (aref v1 0) 1) (= (aref v1 1) 2) (= (aref v1 2) 3)))) 
46 (test (every #'zerop (remove-if-not #'zerop #(1 0 2 0 3))))
47
48 ; SUBSEQ
49 (let ((nums '(1 2 3 4 5)))
50   (test (equal (subseq nums 3) '(4 5)))
51   (test (equal (subseq nums 2 4) '(3 4)))
52   ; Test that nums hasn't been altered: SUBSEQ should construct fresh lists
53   (test (equal nums '(1 2 3 4 5))))