loop10.lsp 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Nov 16 09:07:02 2002
;;;; Contains: Tests of LOOP numeric value accumulation clauses

(in-package :cl-test)

;; Tests of COUNT, COUNTING

(deftest loop.10.1
  (loop for x from 1 to 10 count (< x 5))
  4)

(deftest loop.10.2
  (loop for x from 1 to 10 counting (< x 7))
  6)

(deftest loop.10.3
  (loop for x from 1 to 10 count (< x 5) fixnum)
  4)

(deftest loop.10.4
  (loop for x from 1 to 10 count (< x 5) of-type integer)
  4)

(deftest loop.10.5
  (let (z)
    (values
     (loop for x from 1 to 10 count (< x 5) into foo
sds's avatar
sds committed
30
           finally (setq z foo))
31 32 33 34 35 36 37 38
     z))
  nil
  4)

(deftest loop.10.6
  (let (z)
    (values
     (loop for x from 1 to 10 count (< x 5) into foo fixnum
sds's avatar
sds committed
39
           finally (setq z foo))
40 41 42 43 44 45 46 47
     z))
  nil
  4)

(deftest loop.10.7
  (let (z)
    (values
     (loop for x from 1 to 10 count (< x 5) into foo of-type (integer 0 100)
sds's avatar
sds committed
48
           finally (setq z foo))
49 50 51 52 53 54 55 56
     z))
  nil
  4)

(deftest loop.10.8
  (let (z)
    (values
     (loop for x from 1 to 10 count (< x 5) into foo float
sds's avatar
sds committed
57
           finally (setq z foo))
58 59 60 61 62
     z))
  nil
  4.0)

(deftest loop.10.9
63
  (signals-error
64
   (loop with foo = 10
sds's avatar
sds committed
65 66
         for x in '(a b c) count x into foo
         finally (return foo))
67 68
   program-error)
  t)
69 70

(deftest loop.10.10
71
  (signals-error
72
   (loop with foo = 10
sds's avatar
sds committed
73 74
         for x in '(a b c) counting x into foo
         finally (return foo))
75 76
   program-error)
  t)
77 78 79 80 81 82 83

(declaim (special *loop-count-var*))

(deftest loop.10.11
  (let ((*loop-count-var* 100))
    (values
     (loop for x in '(a b c d) count x into *loop-count-var*
sds's avatar
sds committed
84
           finally (return *loop-count-var*))
85 86 87 88 89
     *loop-count-var*))
  4 100)

(deftest loop.10.12
  (loop for x in '(a b nil d nil e)
sds's avatar
sds committed
90 91
        count x into foo
        collect foo)
92
  (1 2 2 3 3 4))
sds's avatar
sds committed
93

94 95
(deftest loop.10.13
  (loop for x in '(a b nil d nil e)
sds's avatar
sds committed
96 97
        counting x into foo
        collect foo)
98
  (1 2 2 3 3 4))
99 100 101 102

(deftest loop.10.14
  (loop for x in '(a b c) count (return 10))
  10)
sds's avatar
sds committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

;;; Tests of MAXIMIZE, MAXIMIZING

(deftest loop.10.20
  (loop for x in '(1 4 10 5 7 9) maximize x)
  10)

(deftest loop.10.21
  (loop for x in '(1 4 10 5 7 9) maximizing x)
  10)

(deftest loop.10.22
  (loop for x in '(1000000000000) maximizing x)
  1000000000000)

(deftest loop.10.23
  (loop for x in '(-1000000000000) maximize x)
  -1000000000000)

(deftest loop.10.24
  (loop for x in '(1.0 2.0 3.0 -1.0) maximize x)
  3.0)

(deftest loop.10.25
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x fixnum)
  24)

(deftest loop.10.26
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x of-type integer)
  24)

(deftest loop.10.27
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x of-type rational)
  24)

(deftest loop.10.28
  (loop for x in '(1 4 10 5 7 9) maximize x into foo finally (return foo))
  10)

(deftest loop.10.29
  (let (z)
    (values
     (loop for x in '(1 4 10 5 7 9) maximize x into foo finally (setq z foo))
     z))
  nil
  10)

(deftest loop.10.30
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) maximize x of-type real)
  24)

(deftest loop.10.31
  (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)
  0.24)

(deftest loop.10.32
  (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)
  -1/24)

(deftest loop.10.33
  (loop for x in '(1 4 10 5 7 9) maximize x into foo fixnum finally (return foo))
  10)

(deftest loop.10.34
  (loop for x in '(1 4 10 5 7 9) maximize x into foo of-type integer finally (return foo))
  10)

(deftest loop.10.35
  (let ((foo 20))
    (values
     (loop for x in '(3 5 8 3 7) maximize x into foo finally (return foo))
     foo))
  8 20)

(declaim (special *loop-max-var*))

(deftest loop.10.36
  (let ((*loop-max-var* 100))
    (values
     (loop for x in '(1 10 4 8) maximize x into *loop-max-var*
sds's avatar
sds committed
184
           finally (return *loop-max-var*))
185 186 187 188
     *loop-max-var*))
  10 100)

(deftest loop.10.37
189
  (signals-error
190
   (loop with foo = 100
sds's avatar
sds committed
191 192
         for i from 1 to 10 maximize i into foo
         finally (return foo))
193 194
   program-error)
  t)
195 196

(deftest loop.10.38
197
  (signals-error
198
   (loop with foo = 100
sds's avatar
sds committed
199 200
         for i from 1 to 10 maximizing i into foo
         finally (return foo))
201 202
   program-error)
  t)
203 204


205 206 207 208
(deftest loop.10.39
  (loop for x in '(1 2 3) maximize (return 10))
  10)

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
;;; Tests of MINIMIZE, MINIMIZING

(deftest loop.10.40
  (loop for x in '(4 10 1 5 7 9) minimize x)
  1)

(deftest loop.10.41
  (loop for x in '(4 10 5 7 1 9) minimizing x)
  1)

(deftest loop.10.42
  (loop for x in '(1000000000000) minimizing x)
  1000000000000)

(deftest loop.10.43
  (loop for x in '(-1000000000000) minimize x)
  -1000000000000)

(deftest loop.10.44
  (loop for x in '(1.0 2.0 -1.0 3.0) minimize x)
  -1.0)

(deftest loop.10.45
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x fixnum)
  1)

(deftest loop.10.46
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x of-type integer)
  1)

(deftest loop.10.47
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x of-type rational)
  1)

(deftest loop.10.48
  (loop for x in '(1 4 10 5 7 9) minimize x into foo finally (return foo))
  1)

(deftest loop.10.49
  (let (z)
    (values
     (loop for x in '(4 1 10 1 5 7 9) minimize x into foo finally (setq z foo))
     z))
  nil
  1)

(deftest loop.10.50
  (loop for x in '(8 20 5 3 24 1 19 4 20 3) minimize x of-type real)
  1)

(deftest loop.10.51
  (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)
  0.01)

(deftest loop.10.52
  (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)
  -1/1)

(deftest loop.10.53
  (loop for x in '(4 10 5 1 7 9) minimize x into foo fixnum finally (return foo))
  1)

(deftest loop.10.54
  (loop for x in '(1 4 10 5 7 9) minimize x into foo of-type integer finally (return foo))
  1)

(deftest loop.10.55
  (let ((foo 20))
    (values
     (loop for x in '(4 5 8 3 7) minimize x into foo finally (return foo))
     foo))
  3 20)

(declaim (special *loop-min-var*))

(deftest loop.10.56
  (let ((*loop-min-var* 100))
    (values
     (loop for x in '(10 4 8) minimize x into *loop-min-var*
sds's avatar
sds committed
288
           finally (return *loop-min-var*))
289 290 291 292
     *loop-min-var*))
  4 100)

(deftest loop.10.57
293
  (signals-error
294
   (loop with foo = 100
sds's avatar
sds committed
295 296
         for i from 1 to 10 minimize i into foo
         finally (return foo))
297 298
   program-error)
  t)
299 300

(deftest loop.10.58
301
  (signals-error
302
   (loop with foo = 100
sds's avatar
sds committed
303 304
         for i from 1 to 10 minimizing i into foo
         finally (return foo))
305 306
   program-error)
  t)
307

308 309 310 311
(deftest loop.10.58a
  (loop for x in '(1 2 3) minimize (return 10))
  10)

312 313 314 315
;;; Tests combining MINIMIZE, MAXIMIZE

(deftest loop.10.59
  (loop for i from 1 to 10
sds's avatar
sds committed
316 317
        minimize i
        maximize (- i))
318 319 320 321
  1)

(deftest loop.10.60
  (loop for i from 1 to 10
sds's avatar
sds committed
322 323
        maximize (- i)
        minimize i)
324 325 326 327
  -1)

(deftest loop.10.61
  (loop for i from 5 downto 1
sds's avatar
sds committed
328 329
        maximize i
        minimize (- i))
330
  -1)
sds's avatar
sds committed
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

;;; Tests for SUM, SUMMING

(deftest loop.10.70
  (loop for i from 1 to 4 sum i)
  10)

(deftest loop.10.71
  (loop for i from 1 to 4 summing i)
  10)

(deftest loop.10.72
  (loop for i from 1 to 4 sum (float i))
  10.0)

(deftest loop.10.73
  (loop for i from 1 to 4 sum (complex i i))
  #c(10 10))

(deftest loop.10.74
  (loop for i from 1 to 4 sum i fixnum)
  10)

(deftest loop.10.75
  (loop for i from 1 to 4 sum i of-type integer)
  10)

(deftest loop.10.76
  (loop for i from 1 to 4 sum i of-type rational)
  10)

(deftest loop.10.77
  (loop for i from 1 to 4 sum (float i) float)
  10.0)

(deftest loop.10.78
  (loop for i from 1 to 4 sum i of-type number)
  10)

(deftest loop.10.79
  (loop for i from 1 to 4 sum i into foo finally (return foo))
  10)

(deftest loop.10.80
  (loop for i from 1 to 4 sum i into foo fixnum finally (return foo))
  10)

(deftest loop.10.81
  (let (z)
    (values
     (loop for i from 1 to 4 sum i into foo of-type (integer 0 10)
sds's avatar
sds committed
383
           finally (setq z foo))
384 385 386 387 388 389
     z))
  nil
  10)

(deftest loop.10.82
  (loop for i from 1 to 4
sds's avatar
sds committed
390 391
        sum i fixnum
        count t)
392 393 394 395
  14)

(deftest loop.10.83
  (loop for i from 1 to 4
sds's avatar
sds committed
396 397
        sum i fixnum
        count t fixnum)
398 399 400 401 402 403
  14)

(deftest loop.10.84
  (let ((foo 100))
    (values
     (loop for i from 1 to 4 sum i into foo of-type integer
sds's avatar
sds committed
404
           finally (return foo))
405 406 407 408
     foo))
  10 100)

(deftest loop.10.85
409
  (signals-error
410
   (loop with foo = 100
sds's avatar
sds committed
411 412
         for i from 1 to 4 sum i into foo
         finally (return foo))
413 414
   program-error)
  t)
415 416

(deftest loop.10.86
417
  (signals-error
418
   (loop with foo = 100
sds's avatar
sds committed
419 420
         for i from 1 to 4 summing i into foo
         finally (return foo))
421 422
   program-error)
  t)
423 424 425

(deftest loop.10.87
  (loop for i from 1 to 4
sds's avatar
sds committed
426
        sum (complex i (1+ i)) of-type complex)
427 428 429 430
  #c(10 14))

(deftest loop.10.88
  (loop for i from 1 to 4
sds's avatar
sds committed
431
        sum (/ i 17) of-type rational)
432 433 434 435 436 437 438 439
  10/17)

(deftest loop.10.89
  (loop for i from 1 to 4 summing (/ i 17))
  10/17)

(deftest loop.10.90
  (loop for i from 1 to 4
sds's avatar
sds committed
440 441 442
        sum i into foo
        sum (1+ i) into bar
        finally (return (values foo bar)))
443 444 445 446
  10 14)

(deftest loop.10.91
  (loop for i from 1 to 4
sds's avatar
sds committed
447 448 449
        sum i into foo fixnum
        sum (float (1+ i)) into bar float
        finally (return (values foo bar)))
450
  10 14.0)
451 452 453 454 455 456 457 458

(deftest loop.10.92
  (loop for i from 1 to 4 sum (return 100))
  100)

(deftest loop.10.93
  (loop for i from 1 to 4 summing (return 100))
  100)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

(deftest loop.10.94
  (loop for i in nil sum i of-type integer)
  0)

(deftest loop.10.95
  (loop for i in nil sum i of-type fixnum)
  0)

(deftest loop.10.96
  (loop for i in nil sum i of-type bit)
  0)

(deftest loop.10.97
  (loop for i in nil sum i of-type (integer 0 100))
  0)

(deftest loop.10.98
  (loop for i in nil sum i of-type (integer -100 0))
  0)

(deftest loop.10.99
  (loop for i in nil sum i of-type (integer -100 100))
  0)

(deftest loop.10.100
  (loop for i in nil sum i of-type (and integer (real -100.0 100.0)))
  0)

(deftest loop.10.101
  (loop for i in nil sum i of-type short-float)
  0.0s0)

(deftest loop.10.102
  (loop for i in nil sum i of-type single-float)
  0.0f0)

(deftest loop.10.103
  (loop for i in nil sum i of-type double-float)
  0.0d0)

(deftest loop.10.104
  (loop for i in nil sum i of-type long-float)
  0.0l0)
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest loop.10.105
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 10 count (expand-in-current-env (%m (< x 5)))))
  4)

(deftest loop.10.106
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 10 counting (expand-in-current-env (%m t))))
  10)

519
(deftest loop.10.107
520 521 522 523 524
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 10 count (expand-in-current-env (%m nil))))
  0)

525
(deftest loop.10.108
526 527 528 529 530
  (macrolet
   ((%m (z) z))
   (loop for x in '(1 4 10 5 7 9) maximize (expand-in-current-env (%m x))))
  10)

531
(deftest loop.10.109
532 533 534 535 536
  (macrolet
   ((%m (z) z))
   (loop for x in '(1 4 10 5 7 9) maximizing (expand-in-current-env (%m 17))))
  17)

537
(deftest loop.10.110
538 539 540 541 542
  (macrolet
   ((%m (z) z))
   (loop for x in '(5 4 10 1 7 9) minimize (expand-in-current-env (%m x))))
  1)

543
(deftest loop.10.111
544 545 546 547 548
  (macrolet
   ((%m (z) z))
   (loop for x in '(5 4 10 1 7 9) minimizing (expand-in-current-env (%m 3))))
  3)

549
(deftest loop.10.112
550 551 552 553 554
  (macrolet
   ((%m (z) z))
   (loop for x in '(1 4 10 5 7 9) sum (expand-in-current-env (%m x))))
  36)

555
(deftest loop.10.113
556 557 558 559
  (macrolet
   ((%m (z) z))
   (loop for x in '(1 4 10 5 7 9) summing (expand-in-current-env (%m 2))))
  12)