loop11.lsp 4.31 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Nov 16 21:39:33 2002
;;;; Contains: Tests for loop termination clauses REPEAT, WHILE and UNTIL

(in-package :cl-test)

;;; Tests of REPEAT

(deftest loop.11.1
  (let ((z 0))
    (values
     (loop repeat 10 do (incf z))
     z))
  nil
  10)

(deftest loop.11.2
  (loop repeat 10 collect 'a)
  (a a a a a a a a a a))

(deftest loop.11.3
  (let ((z 0))
    (loop repeat 0 do (incf z))
    z)
  0)

(deftest loop.11.4
  (let ((z 0))
    (loop repeat -1 do (incf z))
    z)
  0)

(deftest loop.11.5
  (let ((z 0))
    (loop repeat -1.5 do (incf z))
    z)
  0)

(deftest loop.11.6
  (let ((z 0))
    (loop repeat -1000000000000 do (incf z))
    z)
  0)

(deftest loop.11.7
  (let ((z 0))
    (loop repeat 10 do (incf z) (loop-finish))
    z)
  1)

52 53 54 55
;;; This test is wrong because REPEAT is a main clause whereas FOR is
;;; a variable clause, and no main clause can precede a variable
;;; clause.
;;;
56
;;; (deftest loop.11.8
57 58
;;;   (loop repeat 3 for i in '(a b c d e) collect i)
;;;   (a b c))
59

60 61 62 63 64 65 66
;;; Enough implementors have complained about this test that
;;; I'm removing it.  The standard is self-contradictory
;;; on whether REPEAT can occur later in a LOOP form.

;;; (deftest loop.11.9
;;;  (loop for i in '(a b c d e) collect i repeat 3)
;;;  (a b c))
67 68 69 70 71 72 73 74 75 76


;;; Tests of WHILE

(deftest loop.11.10
  (loop with i = 0 while (< i 10) collect (incf i))
  (1 2 3 4 5 6 7 8 9 10))

(deftest loop.11.11
  (loop with i = 0 while (if (< i 10) t (return 'good))
sds's avatar
sds committed
77
        collect (incf i))
78 79 80 81
  good)

(deftest loop.11.12
  (loop with i = 0
sds's avatar
sds committed
82 83 84
        while (< i 10) collect (incf i)
        while (< i 10) collect (incf i)
        while (< i 10) collect (incf i))
85 86 87 88
  (1 2 3 4 5 6 7 8 9 10))

(deftest loop.11.13
  (loop with i = 0 while (< i 10) collect (incf i)
sds's avatar
sds committed
89
        finally (return 'done))
90 91 92 93
  done)

(deftest loop.11.14
  (loop for i in '(a b c)
sds's avatar
sds committed
94 95
        while nil
        collect i)
96 97 98 99
  nil)

(deftest loop.11.15
  (loop for i in '(a b c)
sds's avatar
sds committed
100 101
        collect i
        while nil)
102 103 104 105
  (a))

(deftest loop.11.16
  (loop for i in '(a b c)
sds's avatar
sds committed
106 107
        while t
        collect i)
108 109 110 111
  (a b c))

(deftest loop.11.17
  (loop for i in '(a b c)
sds's avatar
sds committed
112 113
        collect i
        while t)
114 115 116 117
  (a b c))

(deftest loop.11.18
  (loop for i from 1 to 10
sds's avatar
sds committed
118 119
        while (< i 6)
        finally (return i))
120 121 122 123 124 125 126 127 128 129
  6)

;;; Tests of UNTIL

(deftest loop.11.20
  (loop with i = 0 until (>= i 10) collect (incf i))
  (1 2 3 4 5 6 7 8 9 10))

(deftest loop.11.21
  (loop with i = 0 while (if (< i 10) t (return 'good))
sds's avatar
sds committed
130
        collect (incf i))
131 132 133 134
  good)

(deftest loop.11.22
  (loop with i = 0
sds's avatar
sds committed
135 136 137
        until (>= i 10) collect (incf i)
        until (>= i 10) collect (incf i)
        until (>= i 10) collect (incf i))
138 139 140 141
  (1 2 3 4 5 6 7 8 9 10))

(deftest loop.11.23
  (loop with i = 0 until (>= i 10) collect (incf i)
sds's avatar
sds committed
142
        finally (return 'done))
143 144 145 146
  done)

(deftest loop.11.24
  (loop for i in '(a b c)
sds's avatar
sds committed
147 148
        until t
        collect i)
149 150 151 152
  nil)

(deftest loop.11.25
  (loop for i in '(a b c)
sds's avatar
sds committed
153 154
        collect i
        until t)
155 156 157 158
  (a))

(deftest loop.11.26
  (loop for i in '(a b c)
sds's avatar
sds committed
159 160
        until nil
        collect i)
161 162 163 164
  (a b c))

(deftest loop.11.27
  (loop for i in '(a b c)
sds's avatar
sds committed
165 166
        collect i
        until nil)
167 168 169 170
  (a b c))

(deftest loop.11.28
  (loop for i from 1 to 10
sds's avatar
sds committed
171 172
        until (>= i 6)
        finally (return i))
173
  6)
174 175 176 177 178

;;; More tests of a bug that showed up in c.l.l

(deftest loop.11.29
  (loop for i in '(4 8 9 A 13)
sds's avatar
sds committed
179 180
        when (eq i 'a) return :good
        while (< i 12) collect i)
181 182 183 184
  :good)

(deftest loop.11.30
  (loop for i in '(4 8 9 A 13)
sds's avatar
sds committed
185 186
        unless (numberp i) return :good
        while (< i 12) collect i)
187 188 189 190
  :good)

(deftest loop.11.31
  (loop for i in '(4 8 9 A 13)
sds's avatar
sds committed
191 192
        when (eq i 'a) return :good
        until (> i 12) collect i)
193 194 195 196
  :good)

(deftest loop.11.32
  (loop for i in '(4 8 9 A 13)
sds's avatar
sds committed
197 198
        unless (numberp i) return :good
        until (> i 12) collect i)
199 200
  :good)

201 202
(deftest loop.11.33
  (loop for i in '(4 8 9 A 13)
sds's avatar
sds committed
203 204
        if (not (numberp i)) return :good end
        while (< i 12) collect i)
205 206 207 208
  :good)

(deftest loop.11.34
  (loop for i in '(4 8 9 A 13)
sds's avatar
sds committed
209 210
        if (not (numberp i)) return :good end
        until (> i 12) collect i)
211
  :good)
212

213 214 215 216 217 218 219 220
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest loop.11.35
  (macrolet
   ((%m (z) z))
   (loop repeat (expand-in-current-env (%m 5)) collect 'x))
  (x x x x x))