0.7.9.59:
[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.
37 (assert (typep (nth-value 1
38                           (ignore-errors
39                             (funcall (lambda ()
40                                        (loop with (a . b)
41                                              of-type float = '(5 . 5)
42                                              return (list a b))))))
43                'type-error))
44
45 ;;; bug 103, reported by Arthur Lemmens sbcl-devel 2001-05-05,
46 ;;; fixed by Alexey Dejneka patch sbcl-devel 2001-10-05:
47 ;;; LOOP syntax requires that forms after INITIALLY, FINALLY, and DO
48 ;;; must be compound forms.
49 (multiple-value-bind (function warnings-p failure-p)
50     (compile nil
51              '(lambda ()
52                 (loop while t do
53                       *print-level*
54                       (print t))))
55   (declare (ignore function warnings-p))
56   (assert failure-p))
57
58 ;;; a bug reported by Paul F. Dietz (in his ANSI test suite):
59 ;;; duplicate bindings in LOOP must signal errors of type
60 ;;; PROGRAM-ERROR.
61 (assert (typep (nth-value 1
62                           (ignore-errors
63                             (funcall (lambda ()
64                                        (loop for (a . a) in '((1 . 2) (3 . 4))
65                                              return a)))))
66                'program-error))
67
68 ;;; similar to gcl/ansi-test LOOP.1.27, and fixed at the same time:
69 (assert (equal (loop for x downto 7 by 2 from 13 collect x) '(13 11 9 7)))
70
71 ;;; some more from gcl/ansi-test:
72 (let ((table (make-hash-table)))
73   (setf (gethash 'foo table) '(bar baz))
74   (assert (= (loop for nil being the hash-keys of table count t) 1))
75   (assert (equal (loop for nil being the hash-keys of table
76                                using (hash-value (v1 . v2))
77                        when v1
78                          return v2)
79                  '(baz))))
80
81 (assert (= (loop for nil being the external-symbols of :cl count t) 978))
82 (assert (= (loop for x being the external-symbols of :cl count x) 977))
83
84 (let ((*package* (find-package :cl)))
85   (assert (= (loop for x being each external-symbol count t) 978)))
86
87 (assert (eq (loop for a = (return t) return nil) t))
88
89 (multiple-value-bind (result error)
90     (ignore-errors
91       (loop for nil being the external-symbols of :nonexistent-package
92             count t))
93   (assert (null result))
94   (assert (typep error 'package-error)))
95
96 (assert (equal (loop for i from 1 repeat (the (integer 7 7) 7) collect i)
97                '(1 2 3 4 5 6 7)))
98
99 (multiple-value-bind (result error)
100     (ignore-errors
101       (eval '(loop for i from 1 repeat 7 of-type fixnum collect i)))
102   (assert (null result))
103   (assert (typep error 'program-error)))
104
105 (assert (equal
106          (ignore-errors (loop for i from 1 repeat 6.5 collect i))
107          (ignore-errors (loop for i from 1 repeat (eval '6.5) collect i))))
108
109 (assert (eq (block nil
110               (loop named foo do (loop-finish) finally (return :good))
111               :bad)
112             :good))