1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Sat Nov 16 09:07:02 2002 |
---|
4 | ;;;; Contains: Tests of LOOP numeric value accumulation clauses |
---|
5 | |
---|
6 | (in-package :cl-test) |
---|
7 | |
---|
8 | ;; Tests of COUNT, COUNTING |
---|
9 | |
---|
10 | (deftest loop.10.1 |
---|
11 | (loop for x from 1 to 10 count (< x 5)) |
---|
12 | 4) |
---|
13 | |
---|
14 | (deftest loop.10.2 |
---|
15 | (loop for x from 1 to 10 counting (< x 7)) |
---|
16 | 6) |
---|
17 | |
---|
18 | (deftest loop.10.3 |
---|
19 | (loop for x from 1 to 10 count (< x 5) fixnum) |
---|
20 | 4) |
---|
21 | |
---|
22 | (deftest loop.10.4 |
---|
23 | (loop for x from 1 to 10 count (< x 5) of-type integer) |
---|
24 | 4) |
---|
25 | |
---|
26 | (deftest loop.10.5 |
---|
27 | (let (z) |
---|
28 | (values |
---|
29 | (loop for x from 1 to 10 count (< x 5) into foo |
---|
30 | finally (setq z foo)) |
---|
31 | z)) |
---|
32 | nil |
---|
33 | 4) |
---|
34 | |
---|
35 | (deftest loop.10.6 |
---|
36 | (let (z) |
---|
37 | (values |
---|
38 | (loop for x from 1 to 10 count (< x 5) into foo fixnum |
---|
39 | finally (setq z foo)) |
---|
40 | z)) |
---|
41 | nil |
---|
42 | 4) |
---|
43 | |
---|
44 | (deftest loop.10.7 |
---|
45 | (let (z) |
---|
46 | (values |
---|
47 | (loop for x from 1 to 10 count (< x 5) into foo of-type (integer 0 100) |
---|
48 | finally (setq z foo)) |
---|
49 | z)) |
---|
50 | nil |
---|
51 | 4) |
---|
52 | |
---|
53 | (deftest loop.10.8 |
---|
54 | (let (z) |
---|
55 | (values |
---|
56 | (loop for x from 1 to 10 count (< x 5) into foo float |
---|
57 | finally (setq z foo)) |
---|
58 | z)) |
---|
59 | nil |
---|
60 | 4.0) |
---|
61 | |
---|
62 | (deftest loop.10.9 |
---|
63 | (signals-error |
---|
64 | (loop with foo = 10 |
---|
65 | for x in '(a b c) count x into foo |
---|
66 | finally (return foo)) |
---|
67 | program-error) |
---|
68 | t) |
---|
69 | |
---|
70 | (deftest loop.10.10 |
---|
71 | (signals-error |
---|
72 | (loop with foo = 10 |
---|
73 | for x in '(a b c) counting x into foo |
---|
74 | finally (return foo)) |
---|
75 | program-error) |
---|
76 | t) |
---|
77 | |
---|
78 | (declaim (special *loop-count-var*)) |
---|
79 | |
---|
80 | (deftest loop.10.11 |
---|
81 | (let ((*loop-count-var* 100)) |
---|
82 | (values |
---|
83 | (loop for x in '(a b c d) count x into *loop-count-var* |
---|
84 | finally (return *loop-count-var*)) |
---|
85 | *loop-count-var*)) |
---|
86 | 4 100) |
---|
87 | |
---|
88 | (deftest loop.10.12 |
---|
89 | (loop for x in '(a b nil d nil e) |
---|
90 | count x into foo |
---|
91 | collect foo) |
---|
92 | (1 2 2 3 3 4)) |
---|
93 | |
---|
94 | (deftest loop.10.13 |
---|
95 | (loop for x in '(a b nil d nil e) |
---|
96 | counting x into foo |
---|
97 | collect foo) |
---|
98 | (1 2 2 3 3 4)) |
---|
99 | |
---|
100 | (deftest loop.10.14 |
---|
101 | (loop for x in '(a b c) count (return 10)) |
---|
102 | 10) |
---|
103 | |
---|
104 | |
---|
105 | ;;; Tests of MAXIMIZE, MAXIMIZING |
---|
106 | |
---|
107 | (deftest loop.10.20 |
---|
108 | (loop for x in '(1 4 10 5 7 9) maximize x) |
---|
109 | 10) |
---|
110 | |
---|
111 | (deftest loop.10.21 |
---|
112 | (loop for x in '(1 4 10 5 7 9) maximizing x) |
---|
113 | 10) |
---|
114 | |
---|
115 | (deftest loop.10.22 |
---|
116 | (loop for x in '(1000000000000) maximizing x) |
---|
117 | 1000000000000) |
---|
118 | |
---|
119 | (deftest loop.10.23 |
---|
120 | (loop for x in '(-1000000000000) maximize x) |
---|
121 | -1000000000000) |
---|
122 | |
---|
123 | (deftest loop.10.24 |
---|
124 | (loop for x in '(1.0 2.0 3.0 -1.0) maximize x) |
---|
125 | 3.0) |
---|
126 | |
---|
127 | (deftest loop.10.25 |
---|
128 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x fixnum) |
---|
129 | 24) |
---|
130 | |
---|
131 | (deftest loop.10.26 |
---|
132 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x of-type integer) |
---|
133 | 24) |
---|
134 | |
---|
135 | (deftest loop.10.27 |
---|
136 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x of-type rational) |
---|
137 | 24) |
---|
138 | |
---|
139 | (deftest loop.10.28 |
---|
140 | (loop for x in '(1 4 10 5 7 9) maximize x into foo finally (return foo)) |
---|
141 | 10) |
---|
142 | |
---|
143 | (deftest loop.10.29 |
---|
144 | (let (z) |
---|
145 | (values |
---|
146 | (loop for x in '(1 4 10 5 7 9) maximize x into foo finally (setq z foo)) |
---|
147 | z)) |
---|
148 | nil |
---|
149 | 10) |
---|
150 | |
---|
151 | (deftest loop.10.30 |
---|
152 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x of-type real) |
---|
153 | 24) |
---|
154 | |
---|
155 | (deftest loop.10.31 |
---|
156 | (loop for x in '(0.08 0.20 0.05 0.03 0.24 0.01 0.19 0.04 0.20 0.03) maximize x of-type float) |
---|
157 | 0.24) |
---|
158 | |
---|
159 | (deftest loop.10.32 |
---|
160 | (loop for x in '(-1/8 -1/20 -1/5 -1/3 -1/24 -1/1 -1/19 -1/4 -1/20 -1/3) maximize x of-type rational) |
---|
161 | -1/24) |
---|
162 | |
---|
163 | (deftest loop.10.33 |
---|
164 | (loop for x in '(1 4 10 5 7 9) maximize x into foo fixnum finally (return foo)) |
---|
165 | 10) |
---|
166 | |
---|
167 | (deftest loop.10.34 |
---|
168 | (loop for x in '(1 4 10 5 7 9) maximize x into foo of-type integer finally (return foo)) |
---|
169 | 10) |
---|
170 | |
---|
171 | (deftest loop.10.35 |
---|
172 | (let ((foo 20)) |
---|
173 | (values |
---|
174 | (loop for x in '(3 5 8 3 7) maximize x into foo finally (return foo)) |
---|
175 | foo)) |
---|
176 | 8 20) |
---|
177 | |
---|
178 | (declaim (special *loop-max-var*)) |
---|
179 | |
---|
180 | (deftest loop.10.36 |
---|
181 | (let ((*loop-max-var* 100)) |
---|
182 | (values |
---|
183 | (loop for x in '(1 10 4 8) maximize x into *loop-max-var* |
---|
184 | finally (return *loop-max-var*)) |
---|
185 | *loop-max-var*)) |
---|
186 | 10 100) |
---|
187 | |
---|
188 | (deftest loop.10.37 |
---|
189 | (signals-error |
---|
190 | (loop with foo = 100 |
---|
191 | for i from 1 to 10 maximize i into foo |
---|
192 | finally (return foo)) |
---|
193 | program-error) |
---|
194 | t) |
---|
195 | |
---|
196 | (deftest loop.10.38 |
---|
197 | (signals-error |
---|
198 | (loop with foo = 100 |
---|
199 | for i from 1 to 10 maximizing i into foo |
---|
200 | finally (return foo)) |
---|
201 | program-error) |
---|
202 | t) |
---|
203 | |
---|
204 | |
---|
205 | (deftest loop.10.39 |
---|
206 | (loop for x in '(1 2 3) maximize (return 10)) |
---|
207 | 10) |
---|
208 | |
---|
209 | ;;; Tests of MINIMIZE, MINIMIZING |
---|
210 | |
---|
211 | (deftest loop.10.40 |
---|
212 | (loop for x in '(4 10 1 5 7 9) minimize x) |
---|
213 | 1) |
---|
214 | |
---|
215 | (deftest loop.10.41 |
---|
216 | (loop for x in '(4 10 5 7 1 9) minimizing x) |
---|
217 | 1) |
---|
218 | |
---|
219 | (deftest loop.10.42 |
---|
220 | (loop for x in '(1000000000000) minimizing x) |
---|
221 | 1000000000000) |
---|
222 | |
---|
223 | (deftest loop.10.43 |
---|
224 | (loop for x in '(-1000000000000) minimize x) |
---|
225 | -1000000000000) |
---|
226 | |
---|
227 | (deftest loop.10.44 |
---|
228 | (loop for x in '(1.0 2.0 -1.0 3.0) minimize x) |
---|
229 | -1.0) |
---|
230 | |
---|
231 | (deftest loop.10.45 |
---|
232 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x fixnum) |
---|
233 | 1) |
---|
234 | |
---|
235 | (deftest loop.10.46 |
---|
236 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x of-type integer) |
---|
237 | 1) |
---|
238 | |
---|
239 | (deftest loop.10.47 |
---|
240 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x of-type rational) |
---|
241 | 1) |
---|
242 | |
---|
243 | (deftest loop.10.48 |
---|
244 | (loop for x in '(1 4 10 5 7 9) minimize x into foo finally (return foo)) |
---|
245 | 1) |
---|
246 | |
---|
247 | (deftest loop.10.49 |
---|
248 | (let (z) |
---|
249 | (values |
---|
250 | (loop for x in '(4 1 10 1 5 7 9) minimize x into foo finally (setq z foo)) |
---|
251 | z)) |
---|
252 | nil |
---|
253 | 1) |
---|
254 | |
---|
255 | (deftest loop.10.50 |
---|
256 | (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x of-type real) |
---|
257 | 1) |
---|
258 | |
---|
259 | (deftest loop.10.51 |
---|
260 | (loop for x in '(0.08 0.40 0.05 0.03 0.44 0.01 0.19 0.04 0.40 0.03) minimize x of-type float) |
---|
261 | 0.01) |
---|
262 | |
---|
263 | (deftest loop.10.52 |
---|
264 | (loop for x in '(-1/8 -1/20 -1/5 -1/3 -1/24 -1/1 -1/19 -1/4 -1/20 -1/3) minimize x of-type rational) |
---|
265 | -1/1) |
---|
266 | |
---|
267 | (deftest loop.10.53 |
---|
268 | (loop for x in '(4 10 5 1 7 9) minimize x into foo fixnum finally (return foo)) |
---|
269 | 1) |
---|
270 | |
---|
271 | (deftest loop.10.54 |
---|
272 | (loop for x in '(1 4 10 5 7 9) minimize x into foo of-type integer finally (return foo)) |
---|
273 | 1) |
---|
274 | |
---|
275 | (deftest loop.10.55 |
---|
276 | (let ((foo 20)) |
---|
277 | (values |
---|
278 | (loop for x in '(4 5 8 3 7) minimize x into foo finally (return foo)) |
---|
279 | foo)) |
---|
280 | 3 20) |
---|
281 | |
---|
282 | (declaim (special *loop-min-var*)) |
---|
283 | |
---|
284 | (deftest loop.10.56 |
---|
285 | (let ((*loop-min-var* 100)) |
---|
286 | (values |
---|
287 | (loop for x in '(10 4 8) minimize x into *loop-min-var* |
---|
288 | finally (return *loop-min-var*)) |
---|
289 | *loop-min-var*)) |
---|
290 | 4 100) |
---|
291 | |
---|
292 | (deftest loop.10.57 |
---|
293 | (signals-error |
---|
294 | (loop with foo = 100 |
---|
295 | for i from 1 to 10 minimize i into foo |
---|
296 | finally (return foo)) |
---|
297 | program-error) |
---|
298 | t) |
---|
299 | |
---|
300 | (deftest loop.10.58 |
---|
301 | (signals-error |
---|
302 | (loop with foo = 100 |
---|
303 | for i from 1 to 10 minimizing i into foo |
---|
304 | finally (return foo)) |
---|
305 | program-error) |
---|
306 | t) |
---|
307 | |
---|
308 | (deftest loop.10.58a |
---|
309 | (loop for x in '(1 2 3) minimize (return 10)) |
---|
310 | 10) |
---|
311 | |
---|
312 | ;;; Tests combining MINIMIZE, MAXIMIZE |
---|
313 | |
---|
314 | (deftest loop.10.59 |
---|
315 | (loop for i from 1 to 10 |
---|
316 | minimize i |
---|
317 | maximize (- i)) |
---|
318 | 1) |
---|
319 | |
---|
320 | (deftest loop.10.60 |
---|
321 | (loop for i from 1 to 10 |
---|
322 | maximize (- i) |
---|
323 | minimize i) |
---|
324 | -1) |
---|
325 | |
---|
326 | (deftest loop.10.61 |
---|
327 | (loop for i from 5 downto 1 |
---|
328 | maximize i |
---|
329 | minimize (- i)) |
---|
330 | -1) |
---|
331 | |
---|
332 | |
---|
333 | ;;; Tests for SUM, SUMMING |
---|
334 | |
---|
335 | (deftest loop.10.70 |
---|
336 | (loop for i from 1 to 4 sum i) |
---|
337 | 10) |
---|
338 | |
---|
339 | (deftest loop.10.71 |
---|
340 | (loop for i from 1 to 4 summing i) |
---|
341 | 10) |
---|
342 | |
---|
343 | (deftest loop.10.72 |
---|
344 | (loop for i from 1 to 4 sum (float i)) |
---|
345 | 10.0) |
---|
346 | |
---|
347 | (deftest loop.10.73 |
---|
348 | (loop for i from 1 to 4 sum (complex i i)) |
---|
349 | #c(10 10)) |
---|
350 | |
---|
351 | (deftest loop.10.74 |
---|
352 | (loop for i from 1 to 4 sum i fixnum) |
---|
353 | 10) |
---|
354 | |
---|
355 | (deftest loop.10.75 |
---|
356 | (loop for i from 1 to 4 sum i of-type integer) |
---|
357 | 10) |
---|
358 | |
---|
359 | (deftest loop.10.76 |
---|
360 | (loop for i from 1 to 4 sum i of-type rational) |
---|
361 | 10) |
---|
362 | |
---|
363 | (deftest loop.10.77 |
---|
364 | (loop for i from 1 to 4 sum (float i) float) |
---|
365 | 10.0) |
---|
366 | |
---|
367 | (deftest loop.10.78 |
---|
368 | (loop for i from 1 to 4 sum i of-type number) |
---|
369 | 10) |
---|
370 | |
---|
371 | (deftest loop.10.79 |
---|
372 | (loop for i from 1 to 4 sum i into foo finally (return foo)) |
---|
373 | 10) |
---|
374 | |
---|
375 | (deftest loop.10.80 |
---|
376 | (loop for i from 1 to 4 sum i into foo fixnum finally (return foo)) |
---|
377 | 10) |
---|
378 | |
---|
379 | (deftest loop.10.81 |
---|
380 | (let (z) |
---|
381 | (values |
---|
382 | (loop for i from 1 to 4 sum i into foo of-type (integer 0 10) |
---|
383 | finally (setq z foo)) |
---|
384 | z)) |
---|
385 | nil |
---|
386 | 10) |
---|
387 | |
---|
388 | (deftest loop.10.82 |
---|
389 | (loop for i from 1 to 4 |
---|
390 | sum i fixnum |
---|
391 | count t) |
---|
392 | 14) |
---|
393 | |
---|
394 | (deftest loop.10.83 |
---|
395 | (loop for i from 1 to 4 |
---|
396 | sum i fixnum |
---|
397 | count t fixnum) |
---|
398 | 14) |
---|
399 | |
---|
400 | (deftest loop.10.84 |
---|
401 | (let ((foo 100)) |
---|
402 | (values |
---|
403 | (loop for i from 1 to 4 sum i into foo of-type integer |
---|
404 | finally (return foo)) |
---|
405 | foo)) |
---|
406 | 10 100) |
---|
407 | |
---|
408 | (deftest loop.10.85 |
---|
409 | (signals-error |
---|
410 | (loop with foo = 100 |
---|
411 | for i from 1 to 4 sum i into foo |
---|
412 | finally (return foo)) |
---|
413 | program-error) |
---|
414 | t) |
---|
415 | |
---|
416 | (deftest loop.10.86 |
---|
417 | (signals-error |
---|
418 | (loop with foo = 100 |
---|
419 | for i from 1 to 4 summing i into foo |
---|
420 | finally (return foo)) |
---|
421 | program-error) |
---|
422 | t) |
---|
423 | |
---|
424 | (deftest loop.10.87 |
---|
425 | (loop for i from 1 to 4 |
---|
426 | sum (complex i (1+ i)) of-type complex) |
---|
427 | #c(10 14)) |
---|
428 | |
---|
429 | (deftest loop.10.88 |
---|
430 | (loop for i from 1 to 4 |
---|
431 | sum (/ i 17) of-type rational) |
---|
432 | 10/17) |
---|
433 | |
---|
434 | (deftest loop.10.89 |
---|
435 | (loop for i from 1 to 4 summing (/ i 17)) |
---|
436 | 10/17) |
---|
437 | |
---|
438 | (deftest loop.10.90 |
---|
439 | (loop for i from 1 to 4 |
---|
440 | sum i into foo |
---|
441 | sum (1+ i) into bar |
---|
442 | finally (return (values foo bar))) |
---|
443 | 10 14) |
---|
444 | |
---|
445 | (deftest loop.10.91 |
---|
446 | (loop for i from 1 to 4 |
---|
447 | sum i into foo fixnum |
---|
448 | sum (float (1+ i)) into bar float |
---|
449 | finally (return (values foo bar))) |
---|
450 | 10 14.0) |
---|
451 | |
---|
452 | (deftest loop.10.92 |
---|
453 | (loop for i from 1 to 4 sum (return 100)) |
---|
454 | 100) |
---|
455 | |
---|
456 | (deftest loop.10.93 |
---|
457 | (loop for i from 1 to 4 summing (return 100)) |
---|
458 | 100) |
---|
459 | |
---|
460 | (deftest loop.10.94 |
---|
461 | (loop for i in nil sum i of-type integer) |
---|
462 | 0) |
---|
463 | |
---|
464 | (deftest loop.10.95 |
---|
465 | (loop for i in nil sum i of-type fixnum) |
---|
466 | 0) |
---|
467 | |
---|
468 | (deftest loop.10.96 |
---|
469 | (loop for i in nil sum i of-type bit) |
---|
470 | 0) |
---|
471 | |
---|
472 | (deftest loop.10.97 |
---|
473 | (loop for i in nil sum i of-type (integer 0 100)) |
---|
474 | 0) |
---|
475 | |
---|
476 | (deftest loop.10.98 |
---|
477 | (loop for i in nil sum i of-type (integer -100 0)) |
---|
478 | 0) |
---|
479 | |
---|
480 | (deftest loop.10.99 |
---|
481 | (loop for i in nil sum i of-type (integer -100 100)) |
---|
482 | 0) |
---|
483 | |
---|
484 | (deftest loop.10.100 |
---|
485 | (loop for i in nil sum i of-type (and integer (real -100.0 100.0))) |
---|
486 | 0) |
---|
487 | |
---|
488 | (deftest loop.10.101 |
---|
489 | (loop for i in nil sum i of-type short-float) |
---|
490 | 0.0s0) |
---|
491 | |
---|
492 | (deftest loop.10.102 |
---|
493 | (loop for i in nil sum i of-type single-float) |
---|
494 | 0.0f0) |
---|
495 | |
---|
496 | (deftest loop.10.103 |
---|
497 | (loop for i in nil sum i of-type double-float) |
---|
498 | 0.0d0) |
---|
499 | |
---|
500 | (deftest loop.10.104 |
---|
501 | (loop for i in nil sum i of-type long-float) |
---|
502 | 0.0l0) |
---|
503 | |
---|
504 | ;;; Test that explicit calls to macroexpand in subforms |
---|
505 | ;;; are done in the correct environment |
---|
506 | |
---|
507 | (deftest loop.10.105 |
---|
508 | (macrolet |
---|
509 | ((%m (z) z)) |
---|
510 | (loop for x from 1 to 10 count (expand-in-current-env (%m (< x 5))))) |
---|
511 | 4) |
---|
512 | |
---|
513 | (deftest loop.10.106 |
---|
514 | (macrolet |
---|
515 | ((%m (z) z)) |
---|
516 | (loop for x from 1 to 10 counting (expand-in-current-env (%m t)))) |
---|
517 | 10) |
---|
518 | |
---|
519 | (deftest loop.10.106 |
---|
520 | (macrolet |
---|
521 | ((%m (z) z)) |
---|
522 | (loop for x from 1 to 10 count (expand-in-current-env (%m nil)))) |
---|
523 | 0) |
---|
524 | |
---|
525 | (deftest loop.10.107 |
---|
526 | (macrolet |
---|
527 | ((%m (z) z)) |
---|
528 | (loop for x in '(1 4 10 5 7 9) maximize (expand-in-current-env (%m x)))) |
---|
529 | 10) |
---|
530 | |
---|
531 | (deftest loop.10.108 |
---|
532 | (macrolet |
---|
533 | ((%m (z) z)) |
---|
534 | (loop for x in '(1 4 10 5 7 9) maximizing (expand-in-current-env (%m 17)))) |
---|
535 | 17) |
---|
536 | |
---|
537 | (deftest loop.10.109 |
---|
538 | (macrolet |
---|
539 | ((%m (z) z)) |
---|
540 | (loop for x in '(5 4 10 1 7 9) minimize (expand-in-current-env (%m x)))) |
---|
541 | 1) |
---|
542 | |
---|
543 | (deftest loop.10.110 |
---|
544 | (macrolet |
---|
545 | ((%m (z) z)) |
---|
546 | (loop for x in '(5 4 10 1 7 9) minimizing (expand-in-current-env (%m 3)))) |
---|
547 | 3) |
---|
548 | |
---|
549 | (deftest loop.10.111 |
---|
550 | (macrolet |
---|
551 | ((%m (z) z)) |
---|
552 | (loop for x in '(1 4 10 5 7 9) sum (expand-in-current-env (%m x)))) |
---|
553 | 36) |
---|
554 | |
---|
555 | (deftest loop.10.112 |
---|
556 | (macrolet |
---|
557 | ((%m (z) z)) |
---|
558 | (loop for x in '(1 4 10 5 7 9) summing (expand-in-current-env (%m 2)))) |
---|
559 | 12) |
---|