do.lsp 3.58 KB
Newer Older
pfdietz's avatar
pfdietz committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jan  8 07:25:18 2005
;;;; Contains: Tests of DO

(in-package :cl-test)


(deftest do.1
  (do ((i 0 (1+ i)))
      ((>= i 10) i))
  10)

(deftest do.2
  (do ((i 0 (1+ j))
       (j 0 (1+ i)))
      ((>= i 10) (+ i j)))
  20)

(deftest do.3
  (let ((x nil))
    (do ((i 0 (1+ i)))
	((>= i 10) x)
      (push i x)))
  (9 8 7 6 5 4 3 2 1 0))

(deftest do.4
  (let ((x nil))
    (do ((i 0 (1+ i)))
	((>= i 10) x)
      (declare (fixnum i))
      (push i x)))
  (9 8 7 6 5 4 3 2 1 0))

(deftest do.5
  (do ((i 0 (1+ i)))
      (nil)
    (when (> i 10) (return i)))
  11)

;;; Zero iterations
(deftest do.6
  (do ((i 0 (+ i 10)))
      ((> i -1) i)
    (return 'bad))
  0)

;;; Tests of go tags
(deftest do.7
  (let ((x nil))
    (do ((i 0 (1+ i)))
	((>= i 10) x)
      (go around)
      small
      (push 'a x)
      (go done)
      big
      (push 'b x)
      (go done)
      around
      (if (> i 4) (go big) (go small))
      done))
  (b b b b b a a a a a))

;;; No increment form
(deftest do.8
  (do ((i 0 (1+ i))
       (x nil))
      ((>= i 10) x)
    (push 'a x))
  (a a a a a a a a a a))

;;; No do locals
(deftest do.9
  (let ((i 0))
    (do ()
	((>= i 10) i)
      (incf i)))
  10)

;;; Return of no values
(deftest do.10
  (do ((i 0 (1+ i)))
      ((> i 10) (values))))

;;; Return of two values
(deftest do.11
  (do ((i 0 (1+ i)))
      ((> i 10) (values i (1+ i))))
  11 12)

;;; The results* list is an implicit progn
(deftest do.12
  (do ((i 0 (1+ i)))
      ((> i 10) (incf i) (incf i) i))
  13)

(deftest do.13
  (do ((i 0 (1+ i)))
      ((> i 10)))
  nil)

;; Special var
(deftest do.14
  (let ((x 0))
    (flet ((%f () (locally (declare (special i))
			   (incf x i))))
      (do ((i 0 (1+ i)))
	  ((>= i 10) x)
	(declare (special i))
	(%f))))
  45)

;;; Confirm that the variables in succesive iterations are
;;; identical
(deftest do.15
  (mapcar #'funcall
	  (let ((x nil))
	    (do ((i 0 (1+ i)))
		((= i 5) x)
	      (push #'(lambda () i) x))))
  (5 5 5 5 5))

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; Scope of free declarations

(deftest do.16
  (block done
    (let ((x :bad))
      (declare (special x))
      (let ((x :good))
	(do ((i (return-from done x) 0))
	    (t nil)
	  (declare (special x))))))
  :good)

(deftest do.17
  (block done
    (let ((x :good))
      (declare (special x))
      (let ((x :bad))
	(do ((i 0 (return-from done x)))
	    (nil nil)
	  (declare (special x))))))
  :good)

(deftest do.18
  (block done
    (let ((x :good))
      (declare (special x))
      (let ((x :bad))
	(do ((i 0 0))
	    ((return-from done x) nil)
	  (declare (special x))))))
  :good)

(deftest do.19
  (let ((x :good))
    (declare (special x))
    (let ((x :bad))
      (do () (t x)
	(declare (special x)))))
  :good)

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest do.20
  (let ((result nil))
    (macrolet
     ((%m (z) z))
     (do ((x (expand-in-current-env (%m 0)) (+ x 2)))
	 ((> x 10) result)
	 (push x result))))
  (10 8 6 4 2 0))

(deftest do.21
  (let ((result nil))
    (macrolet
     ((%m (z) z))
     (do ((x 0 (expand-in-current-env (%m (+ x 2)))))
	 ((> x 10) result)
	 (push x result))))
  (10 8 6 4 2 0))

(deftest do.22
  (let ((result nil))
    (macrolet
     ((%m (z) z))
     (do ((x 0 (+ x 2)))
	 ((expand-in-current-env (%m (> x 10))) result)
	 (push x result))))
  (10 8 6 4 2 0))

(deftest do.23
  (let ((result nil))
    (macrolet
     ((%m (z) z))
     (do ((x 0 (+ x 2)))
	 ((> x 10) (expand-in-current-env (%m result)))
	 (push x result))))
  (10 8 6 4 2 0))

pfdietz's avatar
pfdietz committed
203
204
(def-macro-test do.error.1
  (do ((i 0 (1+ i))) ((= i 5) 'a)))