add smoketests for FIND and REPLACE
[jscl.git] / tests / list.lisp
1 ;; Tests for list functions
2
3 ;; TODO: EQUAL doesn't compare lists correctly at the moment.
4 ;; Once it does the lists can be compared directly in many of these tests
5
6 ; COPY-TREE
7 (test (let* ((foo (list '(1 2) '(3 4)))
8              (bar (copy-tree foo)))
9         ;; (SETF (CAR (CAR FOO)) 0) doesn't work in the test for some reason,
10         ;; despite working fine in the REPL
11         (rplaca (car foo) 0)
12         (not (= (car (car foo))
13                 (car (car bar))))))
14
15 ; TREE-EQUAL
16 (test (tree-equal '(1 2 3) '(1 2 3)))
17 (test (tree-equal '(1 (2 (3 4) 5) 6) '(1 (2 (3 4) 5) 6)))
18 (test (tree-equal (cons 1 2) (cons 2 3)
19                   :test (lambda (a b) (not (= a b)))))
20
21 ; FIRST to TENTH
22 (let ((nums '(1 2 3 4 5 6 7 8 9 10)))
23   (test (= (first   nums) 1))
24   (test (= (second  nums) 2))
25   (test (= (third   nums) 3))
26   (test (= (fourth  nums) 4))
27   (test (= (fifth   nums) 5))
28   (test (= (sixth   nums) 6))
29   (test (= (seventh nums) 7))
30   (test (= (eighth  nums) 8))
31   (test (= (ninth   nums) 9))
32   (test (= (tenth   nums) 10)))
33
34 ; TAILP
35 (let* ((a (list 1 2 3))
36        (b (cdr a)))
37   (test (tailp b a))
38   (test (tailp a a)))
39 (test (tailp 'a (cons 'b 'a)))
40
41 ; ACONS
42 (test (equal '((1 . 2) (3 . 4))
43              (acons 1 2 '((3 . 4)))))
44 (test (equal '((1 . 2)) (acons 1 2 ())))
45
46 ; PAIRLIS
47 (test (equal '((1 . 3) (0 . 2))
48              (pairlis '(0 1) '(2 3))))
49 (test (equal '((1 . 2) (a . b))
50              (pairlis '(1) '(2) '((a . b)))))
51
52 ; COPY-ALIST
53 (let* ((alist '((1 . 2) (3 . 4)))
54        (copy (copy-alist alist)))
55   (test (not (eql alist copy)))
56   (test (not (eql (car alist) (car copy))))
57   (test (equal alist copy)))
58
59 ; ASSOC and RASSOC
60 (let ((alist '((1 . 2) (3 . 4))))
61   (test (equal (assoc  1 alist) '(1 . 2)))
62   (test (equal (rassoc 2 alist) '(1 . 2)))
63   (test (not   (assoc  2 alist)))
64   (test (not   (rassoc 1 alist))))
65
66 ; MEMBER
67 (test (equal (member 2 '(1 2 3)) '(2 3)))
68 (test (not   (member 4 '(1 2 3))))
69 (test (equal (member 4 '((1 . 2) (3 . 4)) :key #'cdr) '((3 . 4))))
70 (test (member '(2) '((1) (2) (3)) :test #'equal))
71
72 ; ADJOIN
73 (test (equal (adjoin 1 '(2 3))   '(1 2 3)))
74 (test (equal (adjoin 1 '(1 2 3)) '(1 2 3)))
75 (test (equal (adjoin '(1) '((1) (2)) :test #'equal) '((1) (2))))
76
77 ; INTERSECTION
78 (test (equal (intersection '(1 2) '(2 3)) '(2)))
79 (test (not (intersection '(1 2 3) '(4 5 6))))
80 (test (equal (intersection '((1) (2)) '((2) (3)) :test #'equal) '((2))))
81
82 ; SUBST
83 ; Can't really test this until EQUAL works properly on lists
84
85 ; POP
86 (test (let* ((foo '(1 2 3))
87              (bar (pop foo)))
88         (and (= bar 1)
89              (= (car foo) 2))))