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
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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
;;;; -*- scheme -*-
;;;; control.test --- test suite for delimited continuations
;;;;
;;;; Copyright (C) 2010 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-suite test-control)
#:use-module (ice-9 control)
#:use-module (system vm vm)
#:use-module (srfi srfi-11)
#:use-module (test-suite lib))
;; For these, the compiler should be able to prove that "k" is not referenced,
;; so it avoids reifying the continuation. Since that's a slightly different
;; codepath, we test them both.
(with-test-prefix "escape-only continuations"
(pass-if "no values, normal exit"
(equal? '()
(call-with-values
(lambda ()
(% (values)
(lambda (k . args)
(error "unexpected exit" args))))
list)))
(pass-if "no values, abnormal exit"
(equal? '()
(% (begin
(abort)
(error "unexpected exit"))
(lambda (k . args)
args))))
(pass-if "single value, normal exit"
(equal? '(foo)
(call-with-values
(lambda ()
(% 'foo
(lambda (k . args)
(error "unexpected exit" args))))
list)))
(pass-if "single value, abnormal exit"
(equal? '(foo)
(% (begin
(abort 'foo)
(error "unexpected exit"))
(lambda (k . args)
args))))
(pass-if "multiple values, normal exit"
(equal? '(foo bar baz)
(call-with-values
(lambda ()
(% (values 'foo 'bar 'baz)
(lambda (k . args)
(error "unexpected exit" args))))
list)))
(pass-if "multiple values, abnormal exit"
(equal? '(foo bar baz)
(% (begin
(abort 'foo 'bar 'baz)
(error "unexpected exit"))
(lambda (k . args)
args)))))
;;; And the case in which the compiler has to reify the continuation.
(with-test-prefix "reified continuations"
(pass-if "no values, normal exit"
(equal? '()
(call-with-values
(lambda ()
(% (values)
(lambda (k . args)
(error "unexpected exit" k args))))
list)))
(pass-if "no values, abnormal exit"
(equal? '()
(cdr
(% (begin
(abort)
(error "unexpected exit"))
(lambda args
args)))))
(pass-if "single value, normal exit"
(equal? '(foo)
(call-with-values
(lambda ()
(% 'foo
(lambda (k . args)
(error "unexpected exit" k args))))
list)))
(pass-if "single value, abnormal exit"
(equal? '(foo)
(cdr
(% (begin
(abort 'foo)
(error "unexpected exit"))
(lambda args
args)))))
(pass-if "multiple values, normal exit"
(equal? '(foo bar baz)
(call-with-values
(lambda ()
(% (values 'foo 'bar 'baz)
(lambda (k . args)
(error "unexpected exit" k args))))
list)))
(pass-if "multiple values, abnormal exit"
(equal? '(foo bar baz)
(cdr
(% (begin
(abort 'foo 'bar 'baz)
(error "unexpected exit"))
(lambda args
args))))))
;; The variants check different cases in the compiler.
(with-test-prefix "restarting partial continuations"
(pass-if "in side-effect position"
(let ((k (% (begin (abort) 'foo)
(lambda (k) k))))
(eq? (k)
'foo)))
(pass-if "passing values to side-effect abort"
(let ((k (% (begin (abort) 'foo)
(lambda (k) k))))
(eq? (k 'qux 'baz 'hello)
'foo)))
(pass-if "called for one value"
(let ((k (% (+ (abort) 3)
(lambda (k) k))))
(eqv? (k 39)
42)))
(pass-if "called for multiple values"
(let ((k (% (let-values (((a b . c) (abort)))
(list a b c))
(lambda (k) k))))
(equal? (k 1 2 3 4)
'(1 2 (3 4)))))
(pass-if "in tail position"
(let ((k (% (abort)
(lambda (k) k))))
(eq? (k 'xyzzy)
'xyzzy))))
(define fl (make-fluid))
(fluid-set! fl 0)
(with-test-prefix "suspend/resume with fluids"
(pass-if "normal"
(zero? (% (fluid-ref fl)
error)))
(pass-if "with-fluids normal"
(equal? (% (with-fluids ((fl (1+ (fluid-ref fl))))
(fluid-ref fl))
error)
1))
(pass-if "normal (post)"
(zero? (fluid-ref fl)))
(pass-if "with-fluids and fluid-set!"
(equal? (% (with-fluids ((fl (1+ (fluid-ref fl))))
(fluid-set! fl (1+ (fluid-ref fl)))
(fluid-ref fl))
error)
2))
(pass-if "normal (post2)"
(zero? (fluid-ref fl)))
(pass-if "normal fluid-set!"
(equal? (begin
(fluid-set! fl (1+ (fluid-ref fl)))
(fluid-ref fl))
1))
(pass-if "reset fluid-set!"
(equal? (begin
(fluid-set! fl (1- (fluid-ref fl)))
(fluid-ref fl))
0))
(let ((k (% (with-fluids ((fl (1+ (fluid-ref fl))))
(abort)
(fluid-ref fl))
(lambda (k) k))))
(pass-if "pre"
(equal? (fluid-ref fl) 0))
(pass-if "res"
(equal? (k) 1))
(pass-if "post"
(equal? (fluid-ref fl) 0))))
(with-test-prefix "rewinding prompts"
(pass-if "nested prompts"
(let ((k (% 'a
(% 'b
(begin
(abort-to-prompt 'a)
(abort-to-prompt 'b #t))
(lambda (k x) x))
(lambda (k) k))))
(k))))
(with-test-prefix "abort to unknown prompt"
(pass-if-exception "foo" '(misc-error . "^Abort to unknown prompt")
(abort-to-prompt 'does-not-exist)))
(with-test-prefix "the-vm"
(pass-if "unwind changes VMs"
(let ((new-vm (make-vm))
(prev-vm (the-vm))
(proc (lambda (x y)
(expt x y)))
(call (lambda (p x y)
(p x y))))
(catch 'foo
(lambda ()
(call-with-vm new-vm (lambda () (throw 'foo (the-vm)))))
(lambda (key vm)
(and (eq? key 'foo)
(eq? vm new-vm)
(eq? (the-vm) prev-vm)))))))
|