STRING-TRIM and friends.
[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 ;; POSITION-IF, POSITION-IF-NOT
41 (test (= 2 (position-if #'oddp '((1) (2) (3) (4)) :start 1 :key #'car)))
42 (test (= 4 (position-if-not #'integerp '(1 2 3 4 X))))  ;; (hyperspec example used "5.0", but we don't have a full numeric tower yet!)
43
44 ; REMOVE-IF
45 (test (equal (remove-if     #'zerop '(1 0 2 0 3)) '(1 2 3)))
46 (test (equal (remove-if-not #'zerop '(1 0 2 0 3)) '(0 0)))
47 ;; TODO: Rewrite these tests when EQUALP exists and works on vectors
48 (let ((v1 (remove-if #'zerop #(1 0 2 0 3))))
49   (test (and (= (aref v1 0) 1) (= (aref v1 1) 2) (= (aref v1 2) 3)))) 
50 (test (every #'zerop (remove-if-not #'zerop #(1 0 2 0 3))))
51
52 ; SUBSEQ
53 (let ((nums '(1 2 3 4 5)))
54   (test (equal (subseq nums 3) '(4 5)))
55   (test (equal (subseq nums 2 4) '(3 4)))
56   ; Test that nums hasn't been altered: SUBSEQ should construct fresh lists
57   (test (equal nums '(1 2 3 4 5))))