/
tests.scm
193 lines (170 loc) · 4.23 KB
/
tests.scm
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
(import (srfi :248))
(import (srfi :64))
(import (rnrs io simple))
(import (rnrs conditions))
(test-begin "SRFI 248")
(test-equal 1
(with-unwind-handler
(lambda (obj k)
obj)
(lambda ()
(raise 1))))
(test-equal 2
(with-unwind-handler
(lambda (obj k)
obj)
(lambda ()
(raise-continuable 2))))
(test-equal 3
(with-unwind-handler
(lambda (obj k)
(+ 1 obj))
(lambda ()
(with-unwind-handler
(lambda (obj k)
(raise (+ 1 obj)))
(lambda ()
(raise 1))))))
(test-equal 4
(with-unwind-handler
(lambda (obj k)
(and (non-continuable-violation? obj)
4))
(lambda ()
(with-unwind-handler
(lambda (obj k)
(k 2))
(lambda ()
(raise 1))))))
(test-equal 5
(with-unwind-handler
(lambda (obj k)
(+ 2 (k (+ 3 obj))))
(lambda ()
(+ 1 (raise-continuable -1)))))
(test-equal 6
(with-unwind-handler
(lambda (obj k)
(+ 2 (k (k (+ 3 obj)))))
(lambda ()
(+ 1 (raise-continuable -1)))))
(test-equal 7
(guard (c [(number? c) (+ 1 c)])
(raise 6)))
(test-equal 8
(guard (c [(symbol? c) #f]
[else (+ 2 c)])
(raise 6)))
(test-equal 9
(guard (c [else c])
(guard (c [(symbol? c) #f])
(raise 9))))
(define (call-with-state proc init)
(define-condition-type &get &condition
make-get-condition get-condition?)
(define-condition-type &put &condition
make-put-condition put-condition?
(value condition-value))
(define (get) (raise-continuable (make-get-condition)))
(define (put val) (raise-continuable (make-put-condition val)))
((guard (obj k
[(get-condition? obj)
(lambda (s)
((k s) s))]
[(put-condition? obj)
(lambda (s)
((k) (condition-value obj)))])
(call-with-values (lambda () (proc get put))
(lambda arg*
(lambda (s)
(apply values arg*)))))
init))
(test-equal 10
(call-with-state
(lambda (get put)
(+ 1 (get)))
9))
(test-equal 11
(call-with-state
(lambda (get put)
(put (+ 1 (get)))
(+ 1 (get)))
9))
(test-equal 12
(call-with-state
(lambda (get put)
(call/cc
(lambda (c)
(put 13)
(c)))
(get))
12))
(test-equal (list 13 '(exit enter exit enter exit enter))
(let ([trace* '()])
(guard (obj k
[else
(let ([v (k (k obj))])
(list v trace*))])
(dynamic-wind
(lambda ()
(set! trace* (cons 'enter trace*)))
(lambda ()
(+ 1 (raise-continuable 11)))
(lambda ()
(set! trace* (cons 'exit trace*)))))))
(define make-coroutine-generator
(lambda (proc)
(define-condition-type &yield &condition
make-yield-condition yield-condition?
(value condition-value))
(define yield
(lambda (val)
(raise-continuable (make-yield-condition val))))
(define thunk
(lambda ()
(guard (c k
[(yield-condition? c)
(set! thunk k)
(condition-value c)])
(proc yield)
(eof-object))))
(lambda ()
(thunk))))
(define generator->list
(lambda (g)
(let f ()
(let ([x (g)])
(if (eof-object? x)
'()
(cons x (f)))))))
(test-equal '(1 2)
(let ([g (make-coroutine-generator
(lambda (yield)
(yield 1)
(yield 2)))])
(generator->list g)))
(define for-each->fold
(lambda (for-each)
(define-condition-type &yield &condition
make-yield-condition yield-condition?
(value condition-value))
(lambda (proc nil)
((guard (c k
[(yield-condition? c)
(lambda (s)
((k) (proc s (condition-value c))))])
(for-each
(lambda (x)
(raise-continuable (make-yield-condition x))))
values)
nil))))
(define (xcons x y) (cons y x))
(test-equal '(3 2 1)
((for-each->fold
(lambda (f)
(for-each f '(1 2 3))))
xcons '()))
(test-end)
;; Local Variables:
;; eval: (put 'with-unwind-handler 'scheme-indent-function 1)
;; End: