[test] Add tests for CONS, CONSP, ATOM, RPLACA and RPLACD
[jscl.git] / tests / list.lisp
1 ;; Tests for list functions
2
3 ;; CONS
4 (test (equal (cons 1 2) '(1 . 2)))
5 (test (equal (cons 1 nil) '(1)))
6 (test (equal (cons nil 2) '(NIL . 2)))
7 (test (equal (cons nil nil) '(NIL)))
8 (test (equal (cons 1 (cons 2 (cons 3 (cons 4 nil)))) '(1 2 3 4)))
9 (test (equal (cons 'a 'b) '(A . B)))
10 (test (equal (cons 'a (cons 'b (cons 'c '()))) '(A B C)))
11 (test (equal (cons 'a '(b c d)) '(A B C D)))
12
13 ;; CONSP
14 (test (not (consp 'nil)))
15 (test (not (consp nil)))
16 (test (not (consp ())))
17 (test (not (consp '())))
18 (test (consp (cons 1 2)))
19
20 ;; ATOM
21 (test (atom 'sss))
22 (test (not (atom (cons 1 2))))
23 (test (atom nil))
24 (test (atom '()))
25 (test (atom 3))
26
27 ;; RPLACA
28 (let ((some-list (list* 'one 'two 'three 'four)))
29   (test (equal (rplaca some-list 'uno) '(UNO TWO THREE . FOUR)))
30   (test (equal some-list '(UNO TWO THREE . FOUR))))
31
32 ;; RPLACD
33 (let ((some-list (list* 'one 'two 'three 'four)))
34   (test (equal (rplacd (last some-list) (list 'IV)) '(THREE IV)))
35   (test (equal some-list '(ONE TWO THREE IV))))
36
37 ; COPY-TREE
38 (test (let* ((foo (list '(1 2) '(3 4)))
39              (bar (copy-tree foo)))
40         ;; (SETF (CAR (CAR FOO)) 0) doesn't work in the test for some reason,
41         ;; despite working fine in the REPL
42         (rplaca (car foo) 0)
43         (not (= (car (car foo))
44                 (car (car bar))))))
45
46 ; TREE-EQUAL
47 (test (tree-equal '(1 2 3) '(1 2 3)))
48 (test (tree-equal '(1 (2 (3 4) 5) 6) '(1 (2 (3 4) 5) 6)))
49 (test (tree-equal (cons 1 2) (cons 2 3)
50                   :test (lambda (a b) (not (= a b)))))
51
52 ; FIRST to TENTH
53 (let ((nums '(1 2 3 4 5 6 7 8 9 10)))
54   (test (= (first   nums) 1))
55   (test (= (second  nums) 2))
56   (test (= (third   nums) 3))
57   (test (= (fourth  nums) 4))
58   (test (= (fifth   nums) 5))
59   (test (= (sixth   nums) 6))
60   (test (= (seventh nums) 7))
61   (test (= (eighth  nums) 8))
62   (test (= (ninth   nums) 9))
63   (test (= (tenth   nums) 10)))
64
65 ; TAILP
66 (let* ((a (list 1 2 3))
67        (b (cdr a)))
68   (test (tailp b a))
69   (test (tailp a a)))
70 (test (tailp 'a (cons 'b 'a)))
71
72 ; ACONS
73 (test (equal '((1 . 2) (3 . 4))
74              (acons 1 2 '((3 . 4)))))
75 (test (equal '((1 . 2)) (acons 1 2 ())))
76
77 ; PAIRLIS
78 (test (equal '((1 . 3) (0 . 2))
79              (pairlis '(0 1) '(2 3))))
80 (test (equal '((1 . 2) (a . b))
81              (pairlis '(1) '(2) '((a . b)))))
82
83 ; COPY-ALIST
84 (let* ((alist '((1 . 2) (3 . 4)))
85        (copy (copy-alist alist)))
86   (test (not (eql alist copy)))
87   (test (not (eql (car alist) (car copy))))
88   (test (equal alist copy)))
89
90 ; ASSOC and RASSOC
91 (let ((alist '((1 . 2) (3 . 4))))
92   (test (equal (assoc  1 alist) '(1 . 2)))
93   (test (equal (rassoc 2 alist) '(1 . 2)))
94   (test (not   (assoc  2 alist)))
95   (test (not   (rassoc 1 alist))))
96
97 ; MEMBER
98 (test (equal (member 2 '(1 2 3)) '(2 3)))
99 (test (not   (member 4 '(1 2 3))))
100 (test (equal (member 4 '((1 . 2) (3 . 4)) :key #'cdr) '((3 . 4))))
101 (test (member '(2) '((1) (2) (3)) :test #'equal))
102
103 ; ADJOIN
104 (test (equal (adjoin 1 '(2 3))   '(1 2 3)))
105 (test (equal (adjoin 1 '(1 2 3)) '(1 2 3)))
106 (test (equal (adjoin '(1) '((1) (2)) :test #'equal) '((1) (2))))
107
108 ; INTERSECTION
109 (test (equal (intersection '(1 2) '(2 3)) '(2)))
110 (test (not (intersection '(1 2 3) '(4 5 6))))
111 (test (equal (intersection '((1) (2)) '((2) (3)) :test #'equal) '((2))))
112
113 ; SUBST
114
115 ; POP
116 (test (let* ((foo '(1 2 3))
117              (bar (pop foo)))
118         (and (= bar 1)
119              (= (car foo) 2))))
120
121 ;; MAPCAR
122 (test (equal (mapcar #'+ '(1 2) '(3) '(4 5 6)) '(8)))
123
124 ;; MAPC
125 (test (equal (mapc #'+ '(1 2) '(3) '(4 5 6)) '(1 2)))
126 (test (let (foo)
127         (mapc (lambda (x y z) (print (list x y z)) (push (+ x y z) foo)) '(1 2) '(3) '(4 5 6))
128         (equal foo '(8))))