loop12.lsp 4.81 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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Nov 17 08:47:43 2002
;;;; Contains: Tests for ALWAYS, NEVER, THEREIS

(in-package :cl-test)

;;; Tests of ALWAYS clauses

(deftest loop.12.1
  (loop for i in '(1 2 3 4) always (< i 10))
  t)

(deftest loop.12.2
  (loop for i in nil always nil)
  t)

(deftest loop.12.3
  (loop for i in '(a) always nil)
  nil)

(deftest loop.12.4
  (loop for i in '(1 2 3 4 5 6 7)
sds's avatar
sds committed
24 25
        always t
        until (> i 5))
26 27 28 29
  t)

(deftest loop.12.5
  (loop for i in '(1 2 3 4 5 6 7)
sds's avatar
sds committed
30 31
        always (< i 6)
        until (>= i 5))
32 33 34 35 36 37 38 39
  t)

(deftest loop.12.6
  (loop for x in '(a b c d e) always x)
  t)

(deftest loop.12.7
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
40 41
        always (< x 20)
        never (> x 10))
42 43 44 45
  t)

(deftest loop.12.8
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
46 47
        always (< x 20)
        never (> x 5))
48 49 50 51
  nil)

(deftest loop.12.9
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
52 53
        never (> x 5)
        always (< x 20))
54 55 56 57
  nil)

(deftest loop.12.10
  (loop for x in '(1 2 3 4 5)
sds's avatar
sds committed
58 59
        always (< x 10)
        finally (return 'good))
60 61 62 63
  good)

(deftest loop.12.11
  (loop for x in '(1 2 3 4 5)
sds's avatar
sds committed
64 65
        always (< x 3)
        finally (return 'bad))
66 67 68 69
  nil)

(deftest loop.12.12
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
70 71
        always t
        when (= x 4) do (loop-finish))
72 73 74 75
  t)

(deftest loop.12.13
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
76 77
        do (loop-finish)
        always nil)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  t)

;;; Tests of NEVER

(deftest loop.12.21
  (loop for i in '(1 2 3 4) never (> i 10))
  t)

(deftest loop.12.22
  (loop for i in nil never t)
  t)

(deftest loop.12.23
  (loop for i in '(a) never t)
  nil)

(deftest loop.12.24
  (loop for i in '(1 2 3 4 5 6 7)
sds's avatar
sds committed
96 97
        never nil
        until (> i 5))
98 99 100 101
  t)

(deftest loop.12.25
  (loop for i in '(1 2 3 4 5 6 7)
sds's avatar
sds committed
102 103
        never (>= i 6)
        until (>= i 5))
104 105 106 107 108 109 110 111
  t)

(deftest loop.12.26
  (loop for x in '(a b c d e) never (not x))
  t)

(deftest loop.12.30
  (loop for x in '(1 2 3 4 5)
sds's avatar
sds committed
112 113
        never (>= x 10)
        finally (return 'good))
114 115 116 117
  good)

(deftest loop.12.31
  (loop for x in '(1 2 3 4 5)
sds's avatar
sds committed
118 119
        never (>= x 3)
        finally (return 'bad))
120 121 122 123
  nil)

(deftest loop.12.32
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
124 125
        never nil
        when (= x 4) do (loop-finish))
126 127 128 129
  t)

(deftest loop.12.33
  (loop for x in '(1 2 3 4 5 6)
sds's avatar
sds committed
130 131
        do (loop-finish)
        never t)
132 133 134 135 136 137
  t)

;;; Tests of THEREIS

(deftest loop.12.41
  (loop for x in '(1 2 3 4 5)
sds's avatar
sds committed
138
        thereis (and (eqlt x 3) 'good))
139 140 141 142
  good)

(deftest loop.12.42
  (loop for x in '(nil nil a nil nil)
sds's avatar
sds committed
143
        thereis x)
144 145 146 147
  a)

(deftest loop.12.43
  (loop for x in '(1 2 3 4 5)
sds's avatar
sds committed
148 149
        thereis (eql x 4)
        when (eql x 2) do (loop-finish))
150 151 152 153 154
  nil)

;;; Error cases

(deftest loop.12.error.50
155
  (signals-error
156
   (loop for i from 1 to 10
sds's avatar
sds committed
157 158
         collect i
         always (< i 20))
159 160
   program-error)
  t)
161

162
(deftest loop.12.error.50a
163
  (signals-error
164
   (loop for i from 1 to 10
sds's avatar
sds committed
165 166
         always (< i 20)
         collect i)
167 168
   program-error)
  t)
169

170
(deftest loop.12.error.51
171
  (signals-error
172
   (loop for i from 1 to 10
sds's avatar
sds committed
173 174
         collect i
         never (> i 20))
175 176
   program-error)
  t)
177

178
(deftest loop.12.error.51a
179
  (signals-error
180
   (loop for i from 1 to 10
sds's avatar
sds committed
181 182
         never (> i 20)
         collect i)
183 184
   program-error)
  t)
185

186
(deftest loop.12.error.52
187
  (signals-error
188
   (loop for i from 1 to 10
sds's avatar
sds committed
189 190
         collect i
         thereis (> i 20))
191 192
   program-error)
  t)
193

194
(deftest loop.12.error.52a
195
  (signals-error
196
   (loop for i from 1 to 10
sds's avatar
sds committed
197 198
         thereis (> i 20)
         collect i)
199 200
   program-error)
  t)
201 202 203

;;; Non-error cases

204 205
(deftest loop.12.53
  (loop for i from 1 to 10
sds's avatar
sds committed
206 207
        collect i into foo
        always (< i 20))
208 209
  t)

210 211
(deftest loop.12.53a
  (loop for i from 1 to 10
sds's avatar
sds committed
212 213
        always (< i 20)
        collect i into foo)
214 215
  t)

216 217
(deftest loop.12.54
  (loop for i from 1 to 10
sds's avatar
sds committed
218 219
        collect i into foo
        never (> i 20))
220 221
  t)

222 223
(deftest loop.12.54a
  (loop for i from 1 to 10
sds's avatar
sds committed
224 225
        never (> i 20)
        collect i into foo)
226 227
  t)

228 229
(deftest loop.12.55
  (loop for i from 1 to 10
sds's avatar
sds committed
230 231
        collect i into foo
        thereis i)
232 233
  1)

234 235
(deftest loop.12.55a
  (loop for i from 1 to 10
sds's avatar
sds committed
236 237
        thereis i
        collect i into foo)
238 239
  1)

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
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest loop.12.56
  (macrolet
   ((%m (z) z))
   (loop for i in '(1 2 3 4) always (expand-in-current-env (%m (< i 10)))))
  t)

(deftest loop.12.57
  (macrolet
   ((%m (z) z))
   (loop for i in '(1 2 3 4) always (expand-in-current-env (%m t))))
  t)

(deftest loop.12.58
  (macrolet
   ((%m (z) z))
   (loop for i in '(1 2 3 4) never (expand-in-current-env (%m (>= i 10)))))
  t)

(deftest loop.12.59
  (macrolet
   ((%m (z) z))
   (loop for i in '(1 2 3 4) never (expand-in-current-env (%m t))))
  nil)

(deftest loop.12.60
  (macrolet
   ((%m (z) z))
   (loop for i in '(1 2 3 4)
sds's avatar
sds committed
271
         thereis (expand-in-current-env (%m (and (>= i 2) (+ i 1))))))
272 273
  3)

274