1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Sun Nov 10 21:13:04 2002 |
---|
4 | ;;;; Contains: Tests for LOOP-AS-HASH forms |
---|
5 | |
---|
6 | (in-package :cl-test) |
---|
7 | |
---|
8 | (defparameter *loop.6.alist* |
---|
9 | '((a . 1) (b . 2) (c . 3))) |
---|
10 | |
---|
11 | (defparameter *loop.6.alist.2* |
---|
12 | '(("a" . 1) ("b" . 2) ("c" . 3))) |
---|
13 | |
---|
14 | (defparameter *loop.6.alist.3* |
---|
15 | '(((a1 . a2) . 1) ((b1 . b2) . 2) ((c1 . c2) . 3))) |
---|
16 | |
---|
17 | (defparameter *loop.6.hash.1* |
---|
18 | (let ((table (make-hash-table :test #'eq))) |
---|
19 | (loop for (key . val) in *loop.6.alist* |
---|
20 | do (setf (gethash key table) val)) |
---|
21 | table)) |
---|
22 | |
---|
23 | (defparameter *loop.6.hash.2* |
---|
24 | (let ((table (make-hash-table :test #'eql))) |
---|
25 | (loop for (key . val) in *loop.6.alist* |
---|
26 | do (setf (gethash key table) val)) |
---|
27 | table)) |
---|
28 | |
---|
29 | (defparameter *loop.6.hash.3* |
---|
30 | (let ((table (make-hash-table :test #'equal))) |
---|
31 | (loop for (key . val) in *loop.6.alist.3* |
---|
32 | do (setf (gethash key table) val)) |
---|
33 | table)) |
---|
34 | |
---|
35 | ;;; (defparameter *loop.6.hash.4* |
---|
36 | ;;; (let ((table (make-hash-table :test #'equalp))) |
---|
37 | ;;; (loop for (key . val) in *loop.6.alist.2* |
---|
38 | ;;; do (setf (gethash key table) val)) |
---|
39 | ;;; table)) |
---|
40 | |
---|
41 | (defparameter *loop.6.hash.5* |
---|
42 | (let ((table (make-hash-table :test #'eql))) |
---|
43 | (loop for (val . key) in *loop.6.alist.3* |
---|
44 | do (setf (gethash key table) val)) |
---|
45 | table)) |
---|
46 | |
---|
47 | (defparameter *loop.6.hash.6* |
---|
48 | (let ((table (make-hash-table :test #'eq))) |
---|
49 | (loop for (key . val) in *loop.6.alist* |
---|
50 | do (setf (gethash key table) (coerce val 'float))) |
---|
51 | table)) |
---|
52 | |
---|
53 | (defparameter *loop.6.hash.7* |
---|
54 | (let ((table (make-hash-table :test #'equal))) |
---|
55 | (loop for (val . key) in *loop.6.alist.3* |
---|
56 | do (setf (gethash (coerce key 'float) table) val)) |
---|
57 | table)) |
---|
58 | |
---|
59 | (defparameter *loop.6.alist.8* |
---|
60 | '(((1 . 2) . 1) ((3 . 4) . b) ((5 . 6) . c))) |
---|
61 | |
---|
62 | (defparameter *loop.6.hash.8* |
---|
63 | (let ((table (make-hash-table :test #'equal))) |
---|
64 | (loop for (key . val) in *loop.6.alist.8* |
---|
65 | do (setf (gethash key table) val)) |
---|
66 | table)) |
---|
67 | |
---|
68 | (defparameter *loop.6.hash.9* |
---|
69 | (let ((table (make-hash-table :test #'equal))) |
---|
70 | (loop for (val . key) in *loop.6.alist.8* |
---|
71 | do (setf (gethash key table) val)) |
---|
72 | table)) |
---|
73 | |
---|
74 | ;;; being {each | the} {hash-value | hash-values | hash-key | hash-keys} {in | of } |
---|
75 | |
---|
76 | (deftest loop.6.1 |
---|
77 | (loop for x being the hash-value of *loop.6.hash.1* sum x) |
---|
78 | 6) |
---|
79 | |
---|
80 | (deftest loop.6.2 |
---|
81 | (loop for x being the hash-values of *loop.6.hash.1* sum x) |
---|
82 | 6) |
---|
83 | |
---|
84 | (deftest loop.6.3 |
---|
85 | (loop for x being each hash-value of *loop.6.hash.1* sum x) |
---|
86 | 6) |
---|
87 | |
---|
88 | (deftest loop.6.4 |
---|
89 | (loop for x being each hash-values of *loop.6.hash.1* sum x) |
---|
90 | 6) |
---|
91 | |
---|
92 | (deftest loop.6.5 |
---|
93 | (loop for x being the hash-values in *loop.6.hash.1* sum x) |
---|
94 | 6) |
---|
95 | |
---|
96 | (deftest loop.6.6 |
---|
97 | (sort (loop for x being the hash-key of *loop.6.hash.1* collect x) |
---|
98 | #'symbol<) |
---|
99 | (a b c)) |
---|
100 | |
---|
101 | (deftest loop.6.7 |
---|
102 | (sort (loop for x being the hash-keys of *loop.6.hash.1* collect x) |
---|
103 | #'symbol<) |
---|
104 | (a b c)) |
---|
105 | |
---|
106 | (deftest loop.6.8 |
---|
107 | (sort (loop for x being each hash-key of *loop.6.hash.1* collect x) |
---|
108 | #'symbol<) |
---|
109 | (a b c)) |
---|
110 | |
---|
111 | (deftest loop.6.9 |
---|
112 | (sort (loop for x being each hash-keys of *loop.6.hash.1* collect x) |
---|
113 | #'symbol<) |
---|
114 | (a b c)) |
---|
115 | |
---|
116 | (deftest loop.6.10 |
---|
117 | (sort (loop for x being each hash-keys in *loop.6.hash.1* collect x) |
---|
118 | #'symbol<) |
---|
119 | (a b c)) |
---|
120 | |
---|
121 | (deftest loop.6.11 |
---|
122 | (sort (loop for (u . v) being the hash-keys of *loop.6.hash.3* collect u) |
---|
123 | #'symbol<) |
---|
124 | (a1 b1 c1)) |
---|
125 | |
---|
126 | (deftest loop.6.12 |
---|
127 | (sort (loop for (u . v) being the hash-keys of *loop.6.hash.3* collect v) |
---|
128 | #'symbol<) |
---|
129 | (a2 b2 c2)) |
---|
130 | |
---|
131 | (deftest loop.6.13 |
---|
132 | (sort (loop for (u . v) being the hash-values of *loop.6.hash.5* collect u) |
---|
133 | #'symbol<) |
---|
134 | (a1 b1 c1)) |
---|
135 | |
---|
136 | (deftest loop.6.14 |
---|
137 | (sort (loop for (u . v) being the hash-values of *loop.6.hash.5* collect v) |
---|
138 | #'symbol<) |
---|
139 | (a2 b2 c2)) |
---|
140 | |
---|
141 | (deftest loop.6.15 |
---|
142 | (sort (loop for k being the hash-keys of *loop.6.hash.1* using (hash-value v) |
---|
143 | collect (list k v)) |
---|
144 | #'< :key #'second) |
---|
145 | ((a 1) (b 2) (c 3))) |
---|
146 | |
---|
147 | (deftest loop.6.16 |
---|
148 | (sort (loop for v being the hash-values of *loop.6.hash.1* using (hash-key k) |
---|
149 | collect (list k v)) |
---|
150 | #'< :key #'second) |
---|
151 | ((a 1) (b 2) (c 3))) |
---|
152 | |
---|
153 | (deftest loop.6.17 |
---|
154 | (sort (loop for (u . nil) being the hash-values of *loop.6.hash.5* collect u) |
---|
155 | #'symbol<) |
---|
156 | (a1 b1 c1)) |
---|
157 | |
---|
158 | (deftest loop.6.18 |
---|
159 | (sort (loop for (nil . v) being the hash-values of *loop.6.hash.5* collect v) |
---|
160 | #'symbol<) |
---|
161 | (a2 b2 c2)) |
---|
162 | |
---|
163 | (deftest loop.6.19 |
---|
164 | (loop for nil being the hash-values of *loop.6.hash.5* count t) |
---|
165 | 3) |
---|
166 | |
---|
167 | (deftest loop.6.20 |
---|
168 | (loop for nil being the hash-keys of *loop.6.hash.5* count t) |
---|
169 | 3) |
---|
170 | |
---|
171 | (deftest loop.6.21 |
---|
172 | (loop for v being the hash-values of *loop.6.hash.5* using (hash-key nil) count t) |
---|
173 | 3) |
---|
174 | |
---|
175 | (deftest loop.6.22 |
---|
176 | (loop for k being the hash-keys of *loop.6.hash.5* using (hash-value nil) count t) |
---|
177 | 3) |
---|
178 | |
---|
179 | (deftest loop.6.23 |
---|
180 | (loop for v fixnum being the hash-values of *loop.6.hash.1* sum v) |
---|
181 | 6) |
---|
182 | |
---|
183 | (deftest loop.6.24 |
---|
184 | (loop for v of-type fixnum being the hash-values of *loop.6.hash.1* sum v) |
---|
185 | 6) |
---|
186 | |
---|
187 | (deftest loop.6.25 |
---|
188 | (loop for k fixnum being the hash-keys of *loop.6.hash.5* sum k) |
---|
189 | 6) |
---|
190 | |
---|
191 | (deftest loop.6.26 |
---|
192 | (loop for k of-type fixnum being the hash-keys of *loop.6.hash.5* sum k) |
---|
193 | 6) |
---|
194 | |
---|
195 | (deftest loop.6.27 |
---|
196 | (loop for k t being the hash-keys of *loop.6.hash.5* sum k) |
---|
197 | 6) |
---|
198 | |
---|
199 | (deftest loop.6.28 |
---|
200 | (loop for k of-type t being the hash-keys of *loop.6.hash.5* sum k) |
---|
201 | 6) |
---|
202 | |
---|
203 | (deftest loop.6.29 |
---|
204 | (loop for v t being the hash-values of *loop.6.hash.1* sum v) |
---|
205 | 6) |
---|
206 | |
---|
207 | (deftest loop.6.30 |
---|
208 | (loop for v of-type t being the hash-values of *loop.6.hash.1* sum v) |
---|
209 | 6) |
---|
210 | |
---|
211 | (deftest loop.6.31 |
---|
212 | (loop for v float being the hash-values of *loop.6.hash.6* sum v) |
---|
213 | 6.0) |
---|
214 | |
---|
215 | (deftest loop.6.32 |
---|
216 | (loop for v of-type float being the hash-values of *loop.6.hash.6* sum v) |
---|
217 | 6.0) |
---|
218 | |
---|
219 | (deftest loop.6.33 |
---|
220 | (loop for k float being the hash-keys of *loop.6.hash.7* sum k) |
---|
221 | 6.0) |
---|
222 | |
---|
223 | (deftest loop.6.34 |
---|
224 | (loop for k of-type float being the hash-keys of *loop.6.hash.7* sum k) |
---|
225 | 6.0) |
---|
226 | |
---|
227 | (deftest loop.6.35 |
---|
228 | (loop for (k1 . k2) of-type (integer . integer) being the hash-keys |
---|
229 | of *loop.6.hash.8* sum (+ k1 k2)) |
---|
230 | 21) |
---|
231 | |
---|
232 | (deftest loop.6.36 |
---|
233 | (loop for (v1 . v2) of-type (integer . integer) being the hash-values |
---|
234 | of *loop.6.hash.9* sum (+ v1 v2)) |
---|
235 | 21) |
---|
236 | |
---|
237 | (deftest loop.6.37 |
---|
238 | (loop for v being the hash-values of *loop.6.hash.8* |
---|
239 | using (hash-key (k1 . k2)) sum (+ k1 k2)) |
---|
240 | 21) |
---|
241 | |
---|
242 | (deftest loop.6.38 |
---|
243 | (loop for k being the hash-keys of *loop.6.hash.9* |
---|
244 | using (hash-value (v1 . v2)) sum (+ v1 v2)) |
---|
245 | 21) |
---|
246 | |
---|
247 | (deftest loop.6.39 |
---|
248 | (loop as x being the hash-value of *loop.6.hash.1* sum x) |
---|
249 | 6) |
---|
250 | |
---|
251 | (deftest loop.6.40 |
---|
252 | (sort (loop as x being the hash-key of *loop.6.hash.1* collect x) |
---|
253 | #'symbol<) |
---|
254 | (a b c)) |
---|
255 | |
---|
256 | ;;; Test that explicit calls to macroexpand in subforms |
---|
257 | ;;; are done in the correct environment |
---|
258 | |
---|
259 | (deftest loop.6.41 |
---|
260 | (macrolet |
---|
261 | ((%m (z) z)) |
---|
262 | (loop for x being the hash-value of |
---|
263 | (expand-in-current-env (%m *loop.6.hash.1*)) sum x)) |
---|
264 | 6) |
---|
265 | |
---|
266 | (deftest loop.6.42 |
---|
267 | (macrolet |
---|
268 | ((%m (z) z)) |
---|
269 | (sort (loop for x being the hash-key of |
---|
270 | (expand-in-current-env (%m *loop.6.hash.1*)) collect x) |
---|
271 | #'symbol<)) |
---|
272 | (a b c)) |
---|
273 | |
---|
274 | ;;; Error tests |
---|
275 | |
---|
276 | (deftest loop.6.error.1 |
---|
277 | (signals-error |
---|
278 | (loop for k from 1 to 10 |
---|
279 | for k being the hash-keys of *loop.6.hash.1* |
---|
280 | count t) |
---|
281 | program-error) |
---|
282 | t) |
---|
283 | |
---|
284 | (deftest loop.6.error.2 |
---|
285 | (signals-error |
---|
286 | (loop for k being the hash-keys of *loop.6.hash.1* |
---|
287 | for k from 1 to 10 |
---|
288 | count t) |
---|
289 | program-error) |
---|
290 | t) |
---|
291 | |
---|
292 | (deftest loop.6.error.3 |
---|
293 | (signals-error |
---|
294 | (loop for (k . k) being the hash-keys of *loop.6.hash.3* |
---|
295 | count t) |
---|
296 | program-error) |
---|
297 | t) |
---|
298 | |
---|
299 | (deftest loop.6.error.4 |
---|
300 | (signals-error |
---|
301 | (loop for k being the hash-keys of *loop.6.hash.3* |
---|
302 | using (hash-value k) |
---|
303 | count t) |
---|
304 | program-error) |
---|
305 | t) |
---|
306 | |
---|
307 | (deftest loop.6.error.5 |
---|
308 | (signals-error |
---|
309 | (loop for k being the hash-values of *loop.6.hash.3* |
---|
310 | using (hash-key k) |
---|
311 | count t) |
---|
312 | program-error) |
---|
313 | t) |
---|