1.0.43.72: fix regression from 1.0.43.64
[sbcl.git] / tests / loop.pure.lisp
1 ;;;; miscellaneous tests of LOOP-related stuff
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
13
14 (in-package "CL-USER")
15
16 ;;; The bug reported by Alexei Dejneka on sbcl-devel 2001-09-03
17 ;;; is fixed now.
18 (assert (equal (let ((hash (make-hash-table)))
19                  (setf (gethash 'key1 hash) 'val1)
20                  (setf (gethash 'key2 hash) 'val2)
21                  (sort (loop for key being each hash-key in hash
22                              collect key)
23                        #'string<))
24                '(key1 key2)))
25
26 ;;; Bug 81, reported by Wolfhard Buss on cmucl-help 2001-02-14, was
27 ;;; fixed by Alexey Dejneka's patch on sbcl-devel 2001-09-30.
28 (assert (equal '(0.0 1.0 2.0 3.0)
29                (loop with (a . b) of-type float = '(0.0 . 1.0)
30                      and (c . d) of-type float = '(2.0 . 3.0)
31                      return (list a b c d))))
32
33 ;;; a bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-05:
34 ;;; The type declarations should apply, hence under Python's
35 ;;; declarations-are-assertions rule, the code should signal a type
36 ;;; error. (Except when running interpreted code)
37 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
38 (assert (typep (nth-value 1
39                           (ignore-errors
40                             (funcall (lambda ()
41                                        (loop with (a . b)
42                                              of-type float = '(5 . 5)
43                                              return (list a b))))))
44                'type-error))
45
46 ;;; bug 103, reported by Arthur Lemmens sbcl-devel 2001-05-05,
47 ;;; fixed by Alexey Dejneka patch sbcl-devel 2001-10-05:
48 ;;; LOOP syntax requires that forms after INITIALLY, FINALLY, and DO
49 ;;; must be compound forms.
50 (multiple-value-bind (function warnings-p failure-p)
51     (compile nil
52              '(lambda ()
53                 (loop while t do
54                       *print-level*
55                       (print t))))
56   (declare (ignore function warnings-p))
57   (assert failure-p))
58
59 ;;; a bug reported by Paul F. Dietz (in his ANSI test suite):
60 ;;; duplicate bindings in LOOP must signal errors of type
61 ;;; PROGRAM-ERROR.
62 (assert (typep (nth-value 1
63                           (ignore-errors
64                             (funcall (lambda ()
65                                        (loop for (a . a) in '((1 . 2) (3 . 4))
66                                              return a)))))
67                'program-error))
68
69 ;;; similar to gcl/ansi-test LOOP.1.27, and fixed at the same time:
70 (assert (equal (loop for x downto 7 by 2 from 13 collect x) '(13 11 9 7)))
71
72 ;;; some more from gcl/ansi-test:
73 (let ((table (make-hash-table)))
74   (setf (gethash 'foo table) '(bar baz))
75   (assert (= (loop for nil being the hash-keys of table count t) 1))
76   (assert (equal (loop for nil being the hash-keys of table
77                                using (hash-value (v1 . v2))
78                        when v1
79                          return v2)
80                  '(baz))))
81
82 (assert (= (loop for nil being the external-symbols of :cl count t) 978))
83 (assert (= (loop for x being the external-symbols of :cl count x) 977))
84
85 (let ((*package* (find-package :cl)))
86   (assert (= (loop for x being each external-symbol count t) 978)))
87
88 (assert (eq (loop for a = (return t) return nil) t))
89
90 (multiple-value-bind (result error)
91     (ignore-errors
92       (loop for nil being the external-symbols of :nonexistent-package
93             count t))
94   (assert (null result))
95   (assert (typep error 'package-error)))
96
97 (assert (equal (loop for i from 1 repeat (the (integer 7 7) 7) collect i)
98                '(1 2 3 4 5 6 7)))
99
100 (multiple-value-bind (result error)
101     (ignore-errors
102       (eval '(loop for i from 1 repeat 7 of-type fixnum collect i)))
103   (assert (null result))
104   (assert (typep error 'program-error)))
105
106 (assert (equal
107          (ignore-errors (loop for i from 1 repeat 6.5 collect i))
108          (ignore-errors (loop for i from 1 repeat (eval '6.5) collect i))))
109
110 (assert (eq (block nil
111               (loop named foo do (loop-finish) finally (return :good))
112               :bad)
113             :good))
114
115 (assert (= (loop with (a nil) = '(1 2) return a) 1))
116 (assert (= (loop with (nil a) = '(1 2) return a) 2))
117 (assert (= (loop with (a . nil) = '(1 2) return a) 1))
118 (assert (equal (loop with (nil . a) = '(1 2) return a) '(2)))
119
120 (multiple-value-bind (result error)
121     (ignore-errors
122       (loop for i in '(1 2 3) collect i always (< i 4)))
123   (assert (null result))
124   (assert (typep error 'program-error)))
125 (assert (equal
126          (loop for i in '(1 2 3) collect i into foo always (< i 4)
127                finally (return foo))
128          '(1 2 3)))
129 (assert (equal
130          (loop for i in '(1 2 3) collect i into foo always (= i 4)
131                finally (return foo))
132          nil))
133 (multiple-value-bind (result error)
134     (ignore-errors
135       (loop for i in '(1 2 3) always (< i 4) collect i))
136   (assert (null result))
137   (assert (typep error 'program-error)))
138 (assert (equal
139          (loop for i in '(1 2 3) always (< i 4) collect i into foo
140                finally (return foo))
141          '(1 2 3)))
142 (assert (equal
143          (loop for i in '(1 2 3) always (= i 4) collect i into foo
144                finally (return foo))
145          nil))
146 (multiple-value-bind (result error)
147     (ignore-errors
148       (loop for i in '(1 2 3) thereis (= i 3) collect i))
149   (assert (null result))
150   (assert (typep error 'program-error)))
151
152 (multiple-value-bind (result error)
153     (ignore-errors
154       (loop with i = 1 for x from 1 to 3 collect x into i))
155   (assert (null result))
156   (assert (typep error 'program-error)))
157 (multiple-value-bind (result error)
158     ;; this one has a plausible interpretation in terms of LET*, but
159     ;; ANSI seems specifically to disallow it
160     (ignore-errors
161       (loop with i = 1 with i = (1+ i)
162             for x from 1 to 3
163             collect (+ x i)))
164   (assert (null result))
165   (assert (typep error 'program-error)))
166
167 (let ((it 'z))
168   (assert (equal
169            ;; this one just seems weird.  Nevertheless...
170            (loop for i in '(a b c d)
171                  when i
172                    collect it
173                    and collect it)
174            '(a z b z c z d z))))
175
176 (let ((ht (make-hash-table)))
177   (setf (gethash 1 ht) 3)
178   (setf (gethash 7 ht) 15)
179   (assert (= (loop for v fixnum being each hash-key in ht sum v) 8))
180   (assert (= (loop for v fixnum being each hash-value in ht sum v) 18))
181   #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
182   (assert (raises-error? (loop for v float being each hash-value in ht sum v)
183                          type-error)))
184
185 ;; arithmetic indexes can be NIL or symbols.
186 (assert (equal (loop for nil from 0 to 2 collect nil)
187                '(nil nil nil)))
188 (assert (equal (loop for nil to 2 collect nil)
189                '(nil nil nil)))
190
191 ;; although allowed by the loop syntax definition in 6.2/LOOP,
192 ;; 6.1.2.1.1 says: "The variable var is bound to the value of form1 in
193 ;; the first iteration[...]"; since we can't bind (i j) to anything,
194 ;; we give a program error.
195 (multiple-value-bind (function warnings-p failure-p)
196     (compile nil
197              `(lambda ()
198                 (loop for (i j) from 4 to 6 collect nil)))
199   (assert failure-p))
200
201 ;; ...and another for indexes without FROM forms (these are treated
202 ;; differently by the loop code right now
203 (multiple-value-bind (function warnings-p failure-p)
204     (compile nil
205              `(lambda ()
206                 (loop for (i j) to 6 collect nil)))
207   (assert failure-p))
208
209 (assert
210  (equal
211   (let ((x 2d0))
212     (loop for d of-type double-float from 0d0 to 10d0 by x collect d))
213   '(0d0 2d0 4d0 6d0 8d0 10d0)))
214 (assert
215  (equal
216   (let ((x 2d0))
217     (loop for d of-type double-float downfrom 10d0 to 0d0 by x collect d))
218   '(10d0 8d0 6d0 4d0 2d0 0d0)))
219
220 (let ((fn (handler-case
221               (compile nil '(lambda ()
222                              (declare (special x y))
223                              (loop thereis (pop x) thereis (pop y))))
224             (warning (c) (error "Warned: ~S" c)))))
225   (let ((x (list nil nil 1))
226         (y (list nil 2 nil)))
227     (declare (special x y))
228     (assert (= (funcall fn) 2))))
229
230 ;;; Incorrect LIST type declaration, reported and patched by Teemu
231 ;;; Kalvas: end testing is done "as if by atom" so this is supposed
232 ;;; to work.
233 (assert (equal '(1 2) (loop for (a . b) on '(1 2 . 3)  collect a)))
234
235 ;;; Detection of duplicate bindings, reported by Bruno Haible for CMUCL.
236 (multiple-value-bind (_ condition)
237     (ignore-errors
238       (macroexpand '(LOOP WITH A = 0 FOR A DOWNFROM 10 TO 0 DO (PRINT A))))
239   (declare (ignore _))
240   (assert (typep condition 'program-error)))
241
242 ;;; Loop variable with a range excluding 0, reported by Andras Simon.
243 ;;; (Used to signal an error during macroexpansion.)
244 (assert (not (loop with foo of-type (single-float 1.0 2.0) = 1.5 do (return))))
245
246 ;;; 1.0.26.12 used to signal a bogus type error for this.
247 (loop with x of-type (simple-vector 1) = (make-array '(1))
248       repeat 1
249       return x)
250
251 (with-test (:name :bug-540186)
252   (let ((fun (compile nil `(lambda (x)
253                              (loop for i from 0 below (length x)
254                                    for vec of-type vector = (aref x i)
255                                    collect vec)))))
256     (assert (equal '("foo" "bar")
257              (funcall fun
258                       (vector "foo" "bar"))))))
259
260 (with-test (:name :bug-lp613871)
261   (multiple-value-bind (function warnings-p failure-p)
262       (compile nil '(lambda () (loop with nil = 1 repeat 2 collect t)))
263     (assert (null warnings-p))
264     (assert (null failure-p))
265     (assert (equal '(t t) (funcall function))))
266   (multiple-value-bind (function warnings-p failure-p)
267       (compile nil '(lambda () (loop with nil repeat 2 collect t)))
268     (assert (null warnings-p))
269     (assert (null failure-p))
270     (assert (equal '(t t) (funcall function)))))
271
272 (with-test (:name :bug-654220-regression)
273   (assert (= 32640 (loop for i to 255
274                          sum i into sum of-type fixnum
275                          finally (return sum)))))