loop1.lsp 8.2 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Oct 25 19:07:19 2002
;;;; Contains: Tests of extended loop, part 1

Daniel Kochmański's avatar
Daniel Kochmański committed
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

;;; Tests of variable initialization and stepping clauses

;;; for-as-arithmetic

(deftest loop.1.1
  (loop for x from 1 to 10 collect x)
  (1 2 3 4 5 6 7 8 9 10))

(deftest loop.1.2
  (loop for x from 6 downto 1 collect x)
  (6 5 4 3 2 1))

(deftest loop.1.3
  (loop for x from 1 to 1 collect x)
  (1))

(deftest loop.1.4
  (loop for x from 1 to 0 collect x)
  nil)

(deftest loop.1.5
  (loop for x to 5 collect x)
  (0 1 2 3 4 5))

(deftest loop.1.6
  (loop for x downfrom 5 to 0 collect x)
  (5 4 3 2 1 0))

(deftest loop.1.7
  (loop for x upfrom 1 to 5 collect x)
  (1 2 3 4 5))

(deftest loop.1.8
  (loop for x from 1.0 to 5.0 count x)
  5)

(deftest loop.1.9
  (loop for x from 1 to 9 by 2 collect x)
  (1 3 5 7 9))

(deftest loop.1.10
  (loop for x from 1 to 10 by 2 collect x)
  (1 3 5 7 9))

(deftest loop.1.11
  (loop for x to 10 from 1 collect x)
  (1 2 3 4 5 6 7 8 9 10))

(deftest loop.1.12
  (loop for x to 10 by 2 from 1 collect x)
  (1 3 5 7 9))

(deftest loop.1.13
  (loop for x by 2 to 10 from 1 collect x)
  (1 3 5 7 9))

(deftest loop.1.14
  (loop for x by 2 to 10 collect x)
  (0 2 4 6 8 10))

(deftest loop.1.15
  (loop for x to 10 by 2 collect x)
  (0 2 4 6 8 10))

(deftest loop.1.16
  (let ((n 0))
    (loop for x from (incf n) to (+ n 5) collect x))
  (1 2 3 4 5 6))

(deftest loop.1.17
  (let ((n 0))
    (loop for x to (+ n 5) from (incf n) collect x))
  (1 2 3 4 5))

(deftest loop.1.18
  (let ((n 0))
    (loop for x from (incf n) to (+ n 9) by (incf n) collect x))
  (1 3 5 7 9))

(deftest loop.1.19
  (let ((n 0))
    (loop for x from (incf n) by (incf n) to (+ n 9) collect x))
  (1 3 5 7 9 11))

(deftest loop.1.20
  (let ((a 0) (b 5) (c 1))
    (loop for x from a to b by c
sds's avatar
sds committed
95
          collect (progn (incf a) (incf b 2) (incf c 3) x)))
96 97
  (0 1 2 3 4 5))

98 99 100
(deftest loop.1.21
  (loop for x from 0 to 5 by 1/2 collect x)
  (0 1/2 1 3/2 2 5/2 3 7/2 4 9/2 5))
101

102 103 104
(deftest loop.1.22
  (loop for x from 1 below 5 collect x)
  (1 2 3 4))
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
(deftest loop.1.23
  (loop for x from 1 below 5.01 collect x)
  (1 2 3 4 5))

(deftest loop.1.24
  (loop for x below 5 from 2 collect x)
  (2 3 4))

(deftest loop.1.25
  (loop for x from 10 above 4 collect x)
  (10 9 8 7 6 5))

(deftest loop.1.26
  (loop for x from 14 above 6 by 2 collect x)
  (14 12 10 8))

(deftest loop.1.27
  (loop for x above 6 from 14 by 2 collect x)
  (14 12 10 8))

(deftest loop.1.28
  (loop for x downfrom 16 above 7 by 3 collect x)
  (16 13 10))

(deftest loop.1.29
  (let (a b c (i 0))
    (values
     (loop for x from (progn (setq a (incf i)) 0)
sds's avatar
sds committed
134 135 136
           below (progn (setq b (incf i)) 9)
           by (progn (setq c (incf i)) 2)
           collect x)
137 138 139 140 141 142 143 144
     a b c i))
  (0 2 4 6 8)
  1 2 3 3)

(deftest loop.1.30
  (let (a b c (i 0))
    (values
     (loop for x from (progn (setq a (incf i)) 0)
sds's avatar
sds committed
145 146 147
           by (progn (setq c (incf i)) 2)
           below (progn (setq b (incf i)) 9)
           collect x)
148 149 150 151 152 153 154 155
     a b c i))
  (0 2 4 6 8)
  1 3 2 3)

(deftest loop.1.31
  (let (a b c (i 0))
    (values
     (loop for x
sds's avatar
sds committed
156 157 158 159
           below (progn (setq b (incf i)) 9)
           by (progn (setq c (incf i)) 2)
           from (progn (setq a (incf i)) 0)
           collect x)
160 161 162 163 164 165 166 167
     a b c i))
  (0 2 4 6 8)
  3 1 2 3)

(deftest loop.1.32
  (let (a b c (i 0))
    (values
     (loop for x
sds's avatar
sds committed
168 169 170 171
           by (progn (setq c (incf i)) 2)
           below (progn (setq b (incf i)) 9)
           from (progn (setq a (incf i)) 0)
           collect x)
172 173 174
     a b c i))
  (0 2 4 6 8)
  3 2 1 3)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

(deftest loop.1.33
  (loop for x from 1 upto 5 collect x)
  (1 2 3 4 5))

(deftest loop.1.34
  (loop for x from 1 to 4.0 collect x)
  (1 2 3 4))

(deftest loop.1.35
  (loop for x below 5 collect x)
  (0 1 2 3 4))

(deftest loop.1.36
  (loop for x below 20 by 3 collect x)
  (0 3 6 9 12 15 18))

(deftest loop.1.37
  (loop for x by 3 below 20 collect x)
  (0 3 6 9 12 15 18))

(deftest loop.1.38
  (loop for x of-type fixnum from 1 to 5 collect x)
  (1 2 3 4 5))

;;; The following provides an example where an incorrect
;;; implementation will assign X an out-of-range value
;;; at the end.
(deftest loop.1.39
204
  :notes (:loop-iteration-values-in-finally :ansi-spec-problem)
205 206 207 208 209 210
  (loop for x of-type (integer 1 5) from 1 to 5 collect x)
  (1 2 3 4 5))

;;; Test that the index variable achieves the inclusive
;;; upper bound, but does not exceed it.
(deftest loop.1.40
211
  :notes (:loop-iteration-values-in-finally :ansi-spec-problem)
212
  (loop for x from 1 to 5 do (progn) finally (return x))
213 214 215 216 217
  5)

;;; Test that the index variable acheives the exclusive
;;; upper bound, but does not exceed it.
(deftest loop.1.41
218
  :notes (:loop-iteration-values-in-finally :ansi-spec-problem)
219
  (loop for x from 1 below 5 do (progn) finally (return x))
220 221 222
  4)

(deftest loop.1.42
223
  :notes (:loop-iteration-values-in-finally :ansi-spec-problem)
224
  (loop for x from 10 downto 0 do (progn) finally (return x))
225 226 227
  0)

(deftest loop.1.43
228
  :notes (:loop-iteration-values-in-finally :ansi-spec-problem)
229
  (loop for x from 10 above 0 do (progn) finally (return x))
230
  1)
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

;;; The arithmetic loop form says the types are numbers, not
;;; reals, so arguably they should work on complexes (which are
;;; numbers.)  Comparing these for termination could be problematic,
;;; but a clause without termination should work just fine.

(deftest loop.1.44
  (loop for i from 1 to 5 for c from #c(0 1) collect c)
  (#c(0 1) #c(1 1) #c(2 1) #c(3 1) #c(4 1)))

(deftest loop.1.45
  (loop for i from 1 to 5 for c from #c(0 1) by 2 collect c)
  (#c(0 1) #c(2 1) #c(4 1) #c(6 1) #c(8 1)))

(deftest loop.1.46
  (loop for i from 1 to 5 for c downfrom #c(5 1) collect c)
  (#c(5 1) #c(4 1) #c(3 1) #c(2 1) #c(1 1)))

(deftest loop.1.47
  (loop for i from 1 to 5 for c downfrom #c(10 1) by 2 collect c)
  (#c(10 1) #c(8 1) #c(6 1) #c(4 1) #c(2 1)))

(deftest loop.1.48
  (loop for i from 1 to 5 for c upfrom #c(0 1) collect c)
  (#c(0 1) #c(1 1) #c(2 1) #c(3 1) #c(4 1)))

(deftest loop.1.49
  (loop for i from 1 to 5 for c upfrom #c(0 1) by 2 collect c)
  (#c(0 1) #c(2 1) #c(4 1) #c(6 1) #c(8 1)))
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

;;; The variable in the loop for-as-arithmetic clause
;;; can be a d-var-spec, so 'NIL' should mean don't bind anything

(deftest loop.1.50
  (let ((i 0))
    (loop for nil from 10 to 15 collect (incf i)))
  (1 2 3 4 5 6))

(deftest loop.1.51
  (let ((i 0))
    (loop for nil from 10 below 15 collect (incf i)))
  (1 2 3 4 5))

(deftest loop.1.52
  (loop for nil from 10 to 0 collect 'a)
  nil)

(deftest loop.1.53
  (let ((i 0))
    (loop for nil from 0 to 10 by 2 collect (incf i)))
  (1 2 3 4 5 6))

(deftest loop.1.54
  (let ((i 0))
    (loop for nil from 1 to 4
sds's avatar
sds committed
286
          for nil from 1 to 10 collect (incf i)))
287 288 289 290 291 292 293 294 295 296 297 298
  (1 2 3 4))

(deftest loop.1.55
  (let ((i 0))
    (loop for nil from 5 downto 0 collect (incf i)))
  (1 2 3 4 5 6))

(deftest loop.1.56
  (let ((i 0))
    (loop for nil from 5 above 0 collect (incf i)))
  (1 2 3 4 5))

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

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
(deftest loop.1.57
  (macrolet
   ((%m (z) z))
   (loop for i from (expand-in-current-env (%m 1)) to 5 collect i))
  (1 2 3 4 5))

(deftest loop.1.58
  (macrolet
   ((%m (z) z))
   (loop for i from 1 to (expand-in-current-env (%m 5)) collect i))
  (1 2 3 4 5))

(deftest loop.1.59
  (macrolet
   ((%m (z) z))
   (loop for i from 1 to 5 by (expand-in-current-env (%m 2)) collect i))
  (1 3 5))

(deftest loop.1.60
  (macrolet
   ((%m (z) z))
   (loop for i downfrom (expand-in-current-env (%m 10))
sds's avatar
sds committed
324 325
         to 3
         collect i))
326 327 328 329 330 331
  (10 9 8 7 6 5 4 3))

(deftest loop.1.61
  (macrolet
   ((%m (z) z))
   (loop for i downfrom 10
sds's avatar
sds committed
332 333
         to (expand-in-current-env (%m 3))
         collect i))
334 335 336 337 338 339
  (10 9 8 7 6 5 4 3))

(deftest loop.1.62
  (macrolet
   ((%m (z) z))
   (loop for i from (expand-in-current-env (%m 10))
sds's avatar
sds committed
340 341
         downto 3
         collect i))
342 343 344 345 346 347
  (10 9 8 7 6 5 4 3))

(deftest loop.1.63
  (macrolet
   ((%m (z) z))
   (loop for i from 10
sds's avatar
sds committed
348 349
         downto (expand-in-current-env (%m 3))
         collect i))
350 351 352 353 354 355 356
  (10 9 8 7 6 5 4 3))

(deftest loop.1.64
  (macrolet
   ((%m (z) z))
   (loop for i from (expand-in-current-env (%m 1)) below 5 collect i))
  (1 2 3 4))
sds's avatar
sds committed
357

358 359 360 361 362 363
(deftest loop.1.65
  (macrolet
   ((%m (z) z))
   (loop for i from 1 below (expand-in-current-env (%m 5)) collect i))
  (1 2 3 4))

sds's avatar
sds committed
364