1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Sat Oct 26 13:45:45 2002 |
---|
4 | ;;;; Contains: Tests of the FOR-AS-IN-LIST loop iteration control form, |
---|
5 | ;;;; and of destructuring in loop forms |
---|
6 | |
---|
7 | (in-package :cl-test) |
---|
8 | |
---|
9 | (deftest loop.2.1 |
---|
10 | (loop for x in '(1 2 3) sum x) |
---|
11 | 6) |
---|
12 | |
---|
13 | (deftest loop.2.2 |
---|
14 | (loop for x in '(1 2 3 4) |
---|
15 | do (when (evenp x) (return x))) |
---|
16 | 2) |
---|
17 | |
---|
18 | (deftest loop.2.3 |
---|
19 | (signals-error (loop for x in '(a . b) collect x) |
---|
20 | type-error) |
---|
21 | t) |
---|
22 | |
---|
23 | (deftest loop.2.4 |
---|
24 | (let ((x nil)) |
---|
25 | (loop for e in '(a b c d) do (push e x)) |
---|
26 | x) |
---|
27 | (d c b a)) |
---|
28 | |
---|
29 | (deftest loop.2.5 |
---|
30 | (loop for e in '(a b c d e f) by #'cddr |
---|
31 | collect e) |
---|
32 | (a c e)) |
---|
33 | |
---|
34 | (deftest loop.2.6 |
---|
35 | (loop for e in '(a b c d e f g) by #'cddr |
---|
36 | collect e) |
---|
37 | (a c e g)) |
---|
38 | |
---|
39 | (deftest loop.2.7 |
---|
40 | (loop for e in '(a b c d e f) |
---|
41 | by #'(lambda (l) (and (cdr l) (cons (car l) (cddr l)))) |
---|
42 | collect e) |
---|
43 | (a a a a a a)) |
---|
44 | |
---|
45 | (deftest loop.2.8 |
---|
46 | (loop for (x . y) in '((a . b) (c . d) (e . f)) |
---|
47 | collect (list x y)) |
---|
48 | ((a b) (c d) (e f))) |
---|
49 | |
---|
50 | (deftest loop.2.9 |
---|
51 | (loop for (x nil y) in '((a b c) (d e f) (g h i)) |
---|
52 | collect (list x y)) |
---|
53 | ((a c) (d f) (g i))) |
---|
54 | |
---|
55 | (deftest loop.2.10 |
---|
56 | (loop for (x y) of-type fixnum in '((1 2) (3 4) (5 6)) |
---|
57 | collect (+ x y)) |
---|
58 | (3 7 11)) |
---|
59 | |
---|
60 | (deftest loop.2.11 |
---|
61 | (loop for (x y) of-type fixnum in '((1 2) (3 4) (5 6)) |
---|
62 | collect (+ x y)) |
---|
63 | (3 7 11)) |
---|
64 | |
---|
65 | (deftest loop.2.12 |
---|
66 | (loop for (x y) of-type (fixnum fixnum) in '((1 2) (3 4) (5 6)) |
---|
67 | collect (+ x y)) |
---|
68 | (3 7 11)) |
---|
69 | |
---|
70 | |
---|
71 | (deftest loop.2.13 |
---|
72 | (loop for (x . y) of-type (fixnum . fixnum) in '((1 . 2) (3 . 4) (5 . 6)) |
---|
73 | collect (+ x y)) |
---|
74 | (3 7 11)) |
---|
75 | |
---|
76 | (deftest loop.2.14 |
---|
77 | (signals-error |
---|
78 | (loop for x in '(a b c) |
---|
79 | for x in '(d e f) collect x) |
---|
80 | program-error) |
---|
81 | t) |
---|
82 | |
---|
83 | (deftest loop.2.15 |
---|
84 | (signals-error |
---|
85 | (loop for (x . x) in '((a b) (c d)) collect x) |
---|
86 | program-error) |
---|
87 | t) |
---|
88 | |
---|
89 | (deftest loop.2.16 |
---|
90 | (loop for nil in nil do (return t)) |
---|
91 | nil) |
---|
92 | |
---|
93 | (deftest loop.2.17 |
---|
94 | (let ((x '(a b c))) |
---|
95 | (values |
---|
96 | x |
---|
97 | (loop for x in '(d e f) collect (list x)) |
---|
98 | x)) |
---|
99 | (a b c) |
---|
100 | ((d) (e) (f)) |
---|
101 | (a b c)) |
---|
102 | |
---|
103 | (deftest loop.2.18 |
---|
104 | (loop for x of-type (integer 0 10) in '(2 4 6 7) sum x) |
---|
105 | 19) |
---|
106 | |
---|
107 | ;;; Tests of the 'AS' form |
---|
108 | |
---|
109 | (deftest loop.2.19 |
---|
110 | (loop as x in '(1 2 3) sum x) |
---|
111 | 6) |
---|
112 | |
---|
113 | (deftest loop.2.20 |
---|
114 | (loop as x in '(a b c) |
---|
115 | as y in '(1 2 3) |
---|
116 | collect (list x y)) |
---|
117 | ((a 1) (b 2) (c 3))) |
---|
118 | |
---|
119 | (deftest loop.2.21 |
---|
120 | (loop as x in '(a b c) |
---|
121 | for y in '(1 2 3) |
---|
122 | collect (list x y)) |
---|
123 | ((a 1) (b 2) (c 3))) |
---|
124 | |
---|
125 | (deftest loop.2.22 |
---|
126 | (loop for x in '(a b c) |
---|
127 | as y in '(1 2 3) |
---|
128 | collect (list x y)) |
---|
129 | ((a 1) (b 2) (c 3))) |
---|
130 | |
---|
131 | (deftest loop.2.23 |
---|
132 | (let (a b (i 0)) |
---|
133 | (values |
---|
134 | (loop for e in (progn (setf a (incf i)) |
---|
135 | '(a b c d e f g)) |
---|
136 | by (progn (setf b (incf i)) #'cddr) |
---|
137 | collect e) |
---|
138 | a b i)) |
---|
139 | (a c e g) |
---|
140 | 1 2 2) |
---|
141 | |
---|
142 | |
---|
143 | ;;; Test that explicit calls to macroexpand in subforms |
---|
144 | ;;; are done in the correct environment |
---|
145 | |
---|
146 | (deftest loop.2.24 |
---|
147 | (macrolet |
---|
148 | ((%m (z) z)) |
---|
149 | (loop for x in (expand-in-current-env (%m '(1 2 3))) sum x)) |
---|
150 | 6) |
---|
151 | |
---|
152 | (deftest loop.2.25 |
---|
153 | (macrolet |
---|
154 | ((%m (z) z)) |
---|
155 | (loop for (x . y) in (expand-in-current-env (%m '((a . b) (c . d) (e . f)))) |
---|
156 | collect (list x y))) |
---|
157 | ((a b) (c d) (e f))) |
---|
158 | |
---|
159 | (deftest loop.2.26 |
---|
160 | (macrolet |
---|
161 | ((%m (z) z)) |
---|
162 | (loop as x in (expand-in-current-env (%m '(1 2 3))) sum x)) |
---|
163 | 6) |
---|