loop14.lsp 8.03 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Wed Nov 20 06:33:21 2002
;;;; Contains: Tests of LOOP conditional execution clauses

(in-package :cl-test)

(deftest loop.14.1
  (loop for x from 1 to 6
sds's avatar
sds committed
10 11
        when (evenp x)
        collect x)
12 13 14 15
  (2 4 6))

(deftest loop.14.2
  (loop for x from 1 to 6
sds's avatar
sds committed
16 17
        unless (evenp x)
        collect x)
18 19 20 21
  (1 3 5))

(deftest loop.14.3
  (loop for x from 1 to 10
sds's avatar
sds committed
22 23 24 25
        when (evenp x)
          collect x into foo
          and count t into bar
        finally (return (values foo bar)))
26 27 28 29 30
  (2 4 6 8 10)
  5)

(deftest loop.14.4
  (loop for x from 1 to 10
sds's avatar
sds committed
31
        when (evenp x) collect x end)
32 33 34 35
  (2 4 6 8 10))

(deftest loop.14.5
  (loop for x from 1 to 10
sds's avatar
sds committed
36 37 38 39
        when (evenp x) collect x into evens
        else collect x into odds
        end
        finally (return (values evens odds)))
40 41 42 43 44
  (2 4 6 8 10)
  (1 3 5 7 9))

(deftest loop.14.6
  (loop for x from 1 to 10
sds's avatar
sds committed
45 46 47 48
        unless (oddp x)
          collect x into foo
          and count t into bar
        finally (return (values foo bar)))
49 50 51 52 53
  (2 4 6 8 10)
  5)

(deftest loop.14.7
  (loop for x from 1 to 10
sds's avatar
sds committed
54
        unless (oddp x) collect x end)
55 56 57 58
  (2 4 6 8 10))

(deftest loop.14.8
  (loop for x from 1 to 10
sds's avatar
sds committed
59 60 61 62
        unless (oddp x) collect x into evens
        else collect x into odds
        end
        finally (return (values evens odds)))
63 64 65 66 67
  (2 4 6 8 10)
  (1 3 5 7 9))

(deftest loop.14.9
  (loop for x from 1 to 6
sds's avatar
sds committed
68 69
        if (evenp x)
        collect x)
70 71 72 73
  (2 4 6))

(deftest loop.14.10
  (loop for x from 1 to 10
sds's avatar
sds committed
74 75 76 77
        if (evenp x)
          collect x into foo
          and count t into bar
        finally (return (values foo bar)))
78 79 80 81 82
  (2 4 6 8 10)
  5)

(deftest loop.14.11
  (loop for x from 1 to 10
sds's avatar
sds committed
83
        if (evenp x) collect x end)
84 85 86 87
  (2 4 6 8 10))

(deftest loop.14.12
  (loop for x from 1 to 10
sds's avatar
sds committed
88 89 90 91
        if (evenp x) collect x into evens
        else collect x into odds
        end
        finally (return (values evens odds)))
92 93 94 95 96 97 98 99
  (2 4 6 8 10)
  (1 3 5 7 9))

;;; Test that else associates with the nearest conditional unclosed
;;; by end

(deftest loop.14.13
  (loop for i from 1 to 20
sds's avatar
sds committed
100 101 102 103 104
        if (evenp i)
          if (= (mod i 3) 0)
            collect i into list1
            else collect i into list2
        finally (return (values list1 list2)))
105 106 107 108 109
  (6 12 18)
  (2 4 8 10 14 16 20))

(deftest loop.14.14
  (loop for i from 1 to 20
sds's avatar
sds committed
110 111 112 113 114
        when (evenp i)
          if (= (mod i 3) 0)
            collect i into list1
            else collect i into list2
        finally (return (values list1 list2)))
115 116 117 118 119
  (6 12 18)
  (2 4 8 10 14 16 20))

(deftest loop.14.15
  (loop for i from 1 to 20
sds's avatar
sds committed
120 121 122 123 124
        if (evenp i)
          when (= (mod i 3) 0)
            collect i into list1
            else collect i into list2
        finally (return (values list1 list2)))
125 126 127 128 129
  (6 12 18)
  (2 4 8 10 14 16 20))

(deftest loop.14.16
  (loop for i from 1 to 20
sds's avatar
sds committed
130 131 132 133 134 135
        if (evenp i)
          if (= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
136 137 138 139 140
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

(deftest loop.14.17
  (loop for i from 1 to 20
sds's avatar
sds committed
141 142 143 144 145 146
        when (evenp i)
          if (= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
147 148 149 150 151
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

(deftest loop.14.18
  (loop for i from 1 to 20
sds's avatar
sds committed
152 153 154 155 156 157
        if (evenp i)
          when (= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
158 159 160 161 162
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

(deftest loop.14.19
  (loop for i from 1 to 20
sds's avatar
sds committed
163 164 165 166 167 168
        when (evenp i)
          when (= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
169 170 171 172 173
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

(deftest loop.14.20
  (loop for i from 1 to 20
sds's avatar
sds committed
174 175 176 177 178 179
        unless (oddp i)
          if (= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
180 181 182 183 184
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

(deftest loop.14.21
  (loop for i from 1 to 20
sds's avatar
sds committed
185 186 187 188 189 190
        if (evenp i)
          unless (/= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
191 192 193 194 195
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

(deftest loop.14.22
  (loop for i from 1 to 20
sds's avatar
sds committed
196 197 198 199 200 201
        unless (oddp i)
          unless (/= (mod i 3) 0)
            collect i into list1
          end
          else collect i into list2
        finally (return (values list1 list2)))
202 203 204 205 206 207 208
  (6 12 18)
  (1 3 5 7 9 11 13 15 17 19))

;;; More tests conditionals

(deftest loop.14.23
  (loop for i from 1 to 20
sds's avatar
sds committed
209 210 211 212 213 214
        if (evenp i)
          collect i into list1
        else if (= (mod i 3) 0)
          collect i into list2
        else collect i into list3
        finally (return (values list1 list2 list3)))
215 216 217 218 219 220 221 222
  (2 4 6 8 10 12 14 16 18 20)
  (3 9 15)
  (1 5 7 11 13 17 19))

;;; Tests of 'IT'

(deftest loop.14.24
  (loop for x in '((a) nil (b) (c) (nil) (d))
sds's avatar
sds committed
223
        when (car x) collect it)
224 225 226 227
  (a b c d))

(deftest loop.14.25
  (loop for x in '((a) nil (b) (c) (nil) (d))
sds's avatar
sds committed
228
        if (car x) collect it)
229 230 231 232
  (a b c d))

(deftest loop.14.26
  (loop for x in '(nil (a) nil (b) (c) (nil) (d))
sds's avatar
sds committed
233
        when (car x) return it)
234 235 236 237
  a)

(deftest loop.14.27
  (loop for x in '(nil (a) nil (b) (c) (nil) (d))
sds's avatar
sds committed
238
        if (car x) return it)
239 240 241 242
  a)

(deftest loop.14.28
  (loop for x in '((a) nil (b) (c) (nil) (d))
sds's avatar
sds committed
243
        when (car x) collect it and collect 'foo)
244 245 246 247 248
  (a foo b foo c foo d foo))

(deftest loop.14.29
  (let ((it 'z))
    (loop for x in '(a b c d)
sds's avatar
sds committed
249
          when x collect it and collect it))
250 251 252 253 254
  (a z b z c z d z))

(deftest loop.14.30
  (let ((it 'z))
    (loop for x in '(a b c d)
sds's avatar
sds committed
255 256
          if x collect it end
          collect it))
257 258 259 260
  (a z b z c z d z))

(deftest loop.14.31
  (loop for it on '(a b c d)
sds's avatar
sds committed
261
        when (car it) collect it)
262 263 264 265
  (a b c d))

(deftest loop.14.32
  (loop for x in '(a b nil c d nil e)
sds's avatar
sds committed
266
        when x collecting it)
267 268 269 270
  (a b c d e))

(deftest loop.14.33
  (loop for x in '(a b nil c d nil e)
sds's avatar
sds committed
271
        when x append (list x))
272 273 274 275
  (a b c d e))

(deftest loop.14.34
  (loop for x in '(a b nil c d nil e)
sds's avatar
sds committed
276
        when x appending (list x))
277 278 279 280
  (a b c d e))

(deftest loop.14.35
  (loop for x in '(a b nil c d nil e)
sds's avatar
sds committed
281
        when x nconc (list x))
282 283 284 285
  (a b c d e))

(deftest loop.14.36
  (loop for x in '(a b nil c d nil e)
sds's avatar
sds committed
286
        when x nconcing (list x))
287 288 289 290
  (a b c d e))

(deftest loop.14.37
  (loop for it on '(a b c d)
sds's avatar
sds committed
291 292
        when (car it) collect it into foo
        finally (return foo))
293 294 295 296
  (a b c d))

(deftest loop.14.38
  (loop for x in '(1 2 nil 3 4 nil 5 nil)
sds's avatar
sds committed
297
        when x count it)
298 299 300 301
  5)

(deftest loop.14.39
  (loop for x in '(1 2 nil 3 4 nil 5 nil)
sds's avatar
sds committed
302
        when x counting it)
303 304 305 306
  5)

(deftest loop.14.40
  (loop for x in '(1 2 nil 3 4 nil 6 nil)
sds's avatar
sds committed
307
        when x maximize it)
308 309 310 311
  6)

(deftest loop.14.41
  (loop for x in '(1 2 nil 3 4 nil 6 nil)
sds's avatar
sds committed
312
        when x maximizing it)
313 314 315 316
  6)

(deftest loop.14.42
  (loop for x in '(1 2 nil 3 4 nil 6 nil)
sds's avatar
sds committed
317
        when x minimize it)
318 319 320 321
  1)

(deftest loop.14.43
  (loop for x in '(1 2 nil 3 4 nil 6 nil)
sds's avatar
sds committed
322
        when x minimizing it)
323 324 325 326
  1)

(deftest loop.14.44
  (loop for x in '(1 2 nil 3 4 nil 6 nil)
sds's avatar
sds committed
327
        when x sum it)
328 329 330 331
  16)

(deftest loop.14.45
  (loop for x in '(1 2 nil 3 4 nil 6 nil)
sds's avatar
sds committed
332
        when x summing it)
333
  16)
334 335 336 337 338 339 340 341

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

(deftest loop.14.46
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 6
sds's avatar
sds committed
342 343
         when (expand-in-current-env (%m (evenp x)))
         collect x))
344 345 346 347 348 349
  (2 4 6))

(deftest loop.14.47
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 6
sds's avatar
sds committed
350 351
         unless (expand-in-current-env (%m (evenp x)))
         collect x))
352 353 354 355 356 357
  (1 3 5))

(deftest loop.14.48
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 6
sds's avatar
sds committed
358 359
         when (expand-in-current-env (%m t))
         sum x))
360 361 362 363 364 365
  21)

(deftest loop.14.49
  (macrolet
   ((%m (z) z))
   (loop for x from 1 to 10
sds's avatar
sds committed
366 367
         if  (expand-in-current-env (%m (evenp x)))
         collect x end))
368
  (2 4 6 8 10))