Debian Codegraph @

test/continuation.scm

raw
1 ;;
2 ;; Test continuations and related stuff
3 ;;
4
5 (use gauche.test)
6
7 (test-start "continuations")
8
9 (define c #f)
10
11 ;;-----------------------------------------------------------------------
12 ;; Test for continuation
13
14 (define (callcc-test1)
15 (let1 r '()
16 (let1 w (rlet1 v 1
17 (set! v (+ (call-with-current-continuation (^[c0] (set! c c0) v))
18 v))
19 (set! r (cons v r)))
20 (if (<= w 1024) (c w) r))))
21
22 (test "call/cc (env)" '(2048 1024 512 256 128 64 32 16 8 4 2)
23 callcc-test1)
24
25 ;; continuation with multiple values
26
27 (test* "call/cc (values)" '(1 2 3)
28 (receive x (call-with-current-continuation (^c (c 1 2 3)))
29 x))
30
31 (test* "call/cc (values2)" '(1 2 3)
32 (receive (x y z) (call-with-current-continuation (^c (c 1 2 3)))
33 (list x y z)))
34
35 (test* "call/cc (values3)" '(1 2 (3))
36 (receive (x y . z)
37 (call-with-current-continuation (^c (c 1 2 3)))
38 (list x y z)))
39
40 (test* "call/cc (values4)" (test-error)
41 (receive (x y)
42 (call-with-current-continuation (^c (c 1 2 3)))
43 (list x y)))
44
45 ;; continuation with zero values
46 (test* "call/cc (zero value)" '()
47 (call-with-values (^[] (call-with-current-continuation (^c (c))))
48 (^ x x)))
49
50 ;; continuation invoked while inline procedure is prepared.
51 ;; a test to see call/cc won't mess up the VM stack.
52
53 (define (callcc-test2)
54 (let ([cc #f]
55 [r '()])
56 (let1 s (list 1 2 3 4 (call/cc (^c (set! cc c) 5)) 6 7 8)
57 (if (null? r)
58 (begin (set! r s) (cc -1))
59 (list r s)))))
60
61 (test "call/cc (inline)" '((1 2 3 4 5 6 7 8) (1 2 3 4 -1 6 7 8))
62 callcc-test2)
63
64 ;; continuation created during do frame preparation.
65 ;; the TAILBIND instruction failed this test.
66
67 (test "call/cc (do)" 6
68 (^[] (do ([x 0 (+ x 1)]
69 [y 0 (call/cc (^c c))])
70 [(> x 5) x]
71 #f)))
72
73 ;; When the continuation is restarted, the incomplete frame at the
74 ;; time of capture must be restored. After introducing BOXing of mutable
75 ;; vars and before d84eb08, this was broken since we BOX variables before
76 ;; the frame is completed, and continuation captures the reference to the
77 ;; box instead of the original value---and the content of box wasn't
78 ;; restored by restarting the continuation!
79 ;; The following tests check the regression.
80 (test "call/cc and set!" '((#t . 2) (#t . 1))
81 (lambda ()
82 (let ([cont #f]
83 [r '()])
84 (let ([f (lambda (x y) (set! r (acons x y r)) (set! x #f))])
85 (f #t (call/cc (lambda (c) (set! cont c) 1))))
86 (if cont
87 (let ([k cont])
88 (set! cont #f)
89 (k 2))
90 r))))
91
92 (test "call/cc and set! w/LOCAL-ENV-SHIFT"
93 '(((0) ()))
94 (lambda ()
95 (let loop ([j 0]
96 [t '()]
97 [r '()]
98 [k 0])
99 (let ([x (list j)]
100 [y (list t)])
101 (push! t (cons x y))
102 (if (= k 1)
103 r
104 (loop (+ j 1) '() t (+ k 1)))))))
105
106 ;;------------------------------------------------------------------------
107 ;; Test for continuation thrown over C stack boundary
108 ;;
109
110 ;; We use internal proceduer %apply-rec to cross C stack boundary
111 ;; intentionally.
112
113 (define %apply-rec (with-module gauche.internal %apply-rec))
114
115 (define (callcc-over-cstack)
116 (call-with-current-continuation (^c (%apply-rec (^[] (c 10))))))
117
118 (test "call/cc (cstack)" 10 callcc-over-cstack)
119
120 (test "call/cc (cstack2)" '(10 . 11) (^[] (cons (callcc-over-cstack) 11)))
121
122 (test "call/cc (cstack, values)" '(10 11)
123 (^[] (receive x
124 (call-with-current-continuation (^c (%apply-rec (^[] (c 10 11)))))
125 x)))
126
127 (test "call/cc (cstack, two level)" '(10 . 11)
128 (^[] (cons (call-with-current-continuation
129 (^c (%apply-rec (^[] (%apply-rec (^[] (c 10)))))))
130 11)))
131
132 (test "call/cc (cstack, two level, two hop)" '(11 . 11)
133 (^[] (cons (call-with-current-continuation
134 (^c (%apply-rec
135 (^[] (c (+ (call-with-current-continuation
136 (^d (%apply-rec (^[] (d 10)))))
137 1))))))
138 11)))
139
140 (test "call/cc (ghost continuation)" (test-error)
141 (^[] (%apply-rec
142 (^k (%apply-rec
143 (^[] (cons 1 (call-with-current-continuation
144 (^c (set! k c) 1)))))
145 (k 2))
146 #f)))
147
148 (test "call/cc (ghost continuation, escaping out by call/cc)" '(1 . 2)
149 (^[] (call-with-current-continuation
150 (^[safe-return]
151 (let1 s values
152 (%apply-rec
153 (^k (%apply-rec
154 (^[] (s (cons 1 (call-with-current-continuation
155 (^c (set! k c) 1))))))
156 (set! s safe-return)
157 (k 2))
158 #f))))))
159
160 (test "call/cc (ghost continuation, escaping out by error)" '(1 . 2)
161 (^[] (with-error-handler values
162 (^[] (let1 s values
163 (%apply-rec
164 (^k (%apply-rec
165 (^[] (s (cons 1 (call-with-current-continuation
166 (^c (set! k c) 1))))))
167 (set! s raise)
168 (k 2))
169 #f))))))
170
171 (test "call/cc (ghost continuation, escaping out by error 2)" '(1 . 2)
172 (^[] (guard (e [else e])
173 (let1 s values
174 (%apply-rec
175 (^k (%apply-rec
176 (^[] (s (cons 1 (call-with-current-continuation
177 (^c (set! k c) 1))))))
178 (set! s raise)
179 (k 2))
180 #f)))))
181
182 ;; Paranoia
183
184 (test "call/cc & dynwind (cstack)" '(a b c)
185 (^[] (let1 x '()
186 (call-with-current-continuation
187 (^c (dynamic-wind
188 (^[] (set! x (cons 'c x)))
189 (^[]
190 (%apply-rec (^[] (set! x (cons 'b x)) (c 0)))
191 (set! x (cons 'z x)))
192 (^[] (set! x (cons 'a x))))))
193 x)))
194
195 ;;------------------------------------------------------------------------
196 ;; Test for dynamic-wind
197
198 ;; An example in R5RS
199 (define (dynwind-test1)
200 (let1 path '()
201 (let1 add (^s (set! path (cons s path)))
202 (dynamic-wind
203 (^[] (add 'connect))
204 (^[] (add (call-with-current-continuation (^[c0] (set! c c0) 'talk1))))
205 (^[] (add 'disconnect)))
206 (if (< (length path) 4)
207 (c 'talk2)
208 (reverse path)))))
209
210 (test "dynamic-wind"
211 '(connect talk1 disconnect connect talk2 disconnect)
212 dynwind-test1)
213
214 ;; Test for handler stack.
215 (define (dynwind-test2)
216 (rlet1 path '()
217 (dynamic-wind
218 (^[] (set! path (cons 1 path)))
219 (^[] (set! path (append (dynwind-test1) path)))
220 (^[] (set! path (cons 3 path))))))
221
222 (test "dynamic-wind"
223 '(3 connect talk1 disconnect connect talk2 disconnect 1)
224 dynwind-test2)
225
226 (test "dynamic-wind" '(a b c d e f g b c d e f g h)
227 (^[] (let ([x '()]
228 [c #f])
229 (dynamic-wind
230 (^[] (push! x 'a))
231 (^[]
232 (dynamic-wind
233 (^[] (push! x 'b))
234 (^[] (dynamic-wind
235 (^[] (push! x 'c))
236 (^[] (set! c (call/cc identity)))
237 (^[] (push! x 'd))))
238 (^[] (push! x 'e)))
239 (dynamic-wind
240 (^[] (push! x 'f))
241 (^[] (when c (c #f)))
242 (^[] (push! x 'g))))
243 (^[] (push! x 'h)))
244 (reverse x))))
245
246 ;; Test for multiple values
247 (test "dynamic-wind (multival)" '(a b c)
248 (^[] (receive x
249 (dynamic-wind (^[] #f)
250 (^[] (values 'a 'b 'c))
251 (^[] #f))
252 x)))
253
254 (test "dynamic-wind (multival)" '()
255 (^[] (receive x
256 (dynamic-wind (^[] #f)
257 (^[] (values))
258 (^[] #f))
259 x)))
260
261 ;; Test for error handling with dynamic-wind
262 (test "dynamic-wind - error in before thunk"
263 '(a b c d h)
264 (^[] (let1 k '()
265 (with-error-handler (^_ #f)
266 (^[]
267 (push! k 'a)
268 (dynamic-wind
269 (^[] (push! k 'b))
270 (^[]
271 (push! k 'c)
272 (dynamic-wind
273 (^[] (push! k 'd) (error "ho"))
274 (^[] (push! k 'e))
275 (^[] (push! k 'f)))
276 (push! k 'g))
277 (^[] (push! k 'h)))
278 (push! k 'i)))
279 (reverse k))))
280
281 (test "dynamic-wind - error in after thunk"
282 '(a b c d e f h)
283 (^[] (let1 k '()
284 (with-error-handler (^_ #f)
285 (^[]
286 (push! k 'a)
287 (dynamic-wind
288 (^[] (push! k 'b))
289 (^[]
290 (push! k 'c)
291 (dynamic-wind
292 (^[] (push! k 'd))
293 (^[] (push! k 'e))
294 (^[] (push! k 'f) (error "ho")))
295 (push! k 'g))
296 (^[] (push! k 'h)))
297 (push! k 'i)))
298 (reverse k))))
299
300 ;; test for error during dynamic handler reinstallation by call/cc.
301 ;; (problem found and fixed by Kazuki Tsujimoto)
302
303 (define (test-thunk body)
304 (let1 x '()
305 (with-error-handler
306 (^_ (push! x 'x))
307 (^[] (call/cc
308 (^c (dynamic-wind
309 (^[] (push! x 'a))
310 (^[] (dynamic-wind
311 (^[] (push! x 'b))
312 (^[] (body c))
313 (^[] (push! x 'c) (car 3))))
314 (^[] (push! x 'd)))))))
315 (reverse x)))
316
317 (test* "restart & dynamic-wind with error(1)" '(a b c x d)
318 (test-thunk (^[cont] (cont #t))))
319
320 (test* "restart & dynamic-wind with error(2)" '(a b c x d)
321 (test-thunk (^[cont] (with-error-handler
322 (^_ (cont #t))
323 (^[] (car 3))))))
324
325 ;; test for compiler optimizer propery tracks local procedure usage
326 ;; (problem reported by SaitoAtsushi)
327
328 (test* "compiler optimizer for dynamic-wind" 2
329 (rlet1 a 1
330 (let1 lv (^[] (set! a 2))
331 (dynamic-wind lv (^[] 'hoge) lv))))
332
333 ;; make sure the evaluation order isn't changed in presense of
334 ;; dynamic-wind inlining; that is, the arguments should be fully
335 ;; evaluated before before/after thunks are executed.
336 (test* "optimization and inlined dynamic-wind" 2
337 (let1 a 1
338 (define (make-prepost x) (^[] (set! a x)))
339 (define (make-thunk) (^[] a))
340 (dynamic-wind
341 (make-prepost 2)
342 (make-thunk)
343 (make-prepost 3))))
344
345 ;;-----------------------------------------------------------------------
346 ;; Test for stack overflow handling
347
348 ;; Single call of fact-rec consumes
349 ;; 5 (continuation) + 1 (n) + 4 (argframe) = 10
350 ;; words. With the default stack size 10000, n=1000 is enough to generate
351 ;; the stack overflow. There's no way to obtain compiled-in stack size
352 ;; right now, so you need to adjust the parameters if you change the stack
353 ;; size.
354
355 (define (sum-rec n)
356 (if (> n 0)
357 (+ n (sum-rec (- n 1)))
358 0))
359
360 (test "stack overflow" (/ (* 1000 1001) 2)
361 (^[] (sum-rec 1000)))
362
363 (test "stack overflow" (/ (* 4000 4001) 2)
364 (^[] (sum-rec 4000)))
365
366 (define (sum-rec-apply n)
367 (if (> n 0)
368 (apply + n (apply sum-rec (- n 1) '()) '())
369 0))
370
371 (test "stack overflow (apply)" (/ (* 2000 2001) 2)
372 (^[] (sum-rec-apply 2000)))
373
374 (test "stack overflow (apply)" (/ (* 3000 3001) 2)
375 (^[] (sum-rec-apply 3000)))
376
377 ;;-----------------------------------------------------------------------
378 ;; See if port stuff is cleaned up properly
379
380 (test "call-with-output-file -> port-closed?"
381 #t
382 (^[] (let ([p #f])
383 (call-with-output-file "tmp1.o"
384 (^[port]
385 (write '(a b c d e) port)
386 (set! p port)))
387 (port-closed? p))))
388
389 (test "call-with-input-file -> port-closed?"
390 '(#t a b c d e)
391 (^[] (let* ([p #f]
392 [r (call-with-input-file "tmp1.o"
393 (^[port]
394 (set! p port)
395 (read port)))])
396 (cons (port-closed? p) r))))
397
398 (test "with-output-to-file -> port-closed?"
399 '(#t #f)
400 (^[] (let ([p #f])
401 (with-output-to-file "tmp1.o"
402 (^[]
403 (set! p (current-output-port))
404 (write '(a b c d e))))
405 (list (port-closed? p)
406 (eq? p (current-output-port))))))
407
408 (test "with-input-from-file -> port-closed?"
409 '(#t #f)
410 (^[] (let* ([p #f]
411 [r (with-input-from-file "tmp1.o"
412 (^[]
413 (set! p (current-input-port))
414 (read)))])
415 (list (port-closed? p)
416 (eq? p (current-input-port))))))
417
418 ;;-----------------------------------------------------------------------
419 ;; Al Petrofsky's finding
420 ;; http://groups.google.com/groups?dq=&hl=ja&selm=87g00y4b6l.fsf%40radish.petrofsky.org
421
422 (test "Al's call/cc test" 1
423 (^[] (call/cc (^c (0 (c 1))))))
424
425 ;;-----------------------------------------------------------------------
426 ;; Continuation prompts
427 ;;
428
429 (test-section "continuation prompts")
430
431 (test* "continuation-prompt-tag" #t
432 (continuation-prompt-tag? (make-continuation-prompt-tag)))
433 (test* "continuation-prompt-tag" #t
434 (continuation-prompt-tag? (make-continuation-prompt-tag 'a)))
435 (test* "continuation-prompt-tag" #t
436 (continuation-prompt-tag? (default-continuation-prompt-tag)))
437 (test* "continuation-prompt-tag" #f
438 (equal? (make-continuation-prompt-tag)
439 (default-continuation-prompt-tag)))
440 (test* "continuation-prompt-tag" #f
441 (equal? (make-continuation-prompt-tag)
442 (make-continuation-prompt-tag)))
443
444 (test* "call-with-continuation-prompt basic 1" 1
445 (call-with-continuation-prompt (lambda () 1)))
446 (test* "call-with-continuation-prompt basic 1" 1
447 (call-with-continuation-prompt (lambda () 1)
448 (make-continuation-prompt-tag)))
449 (test* "call-with-continuation-prompt basic 1" 1
450 (call-with-continuation-prompt (lambda () 1)
451 (make-continuation-prompt-tag)
452 list))
453
454 (test* "abort-current-continuation" '(foo bar)
455 (let ([tag (make-continuation-prompt-tag)])
456 (call-with-continuation-prompt
457 (lambda ()
458 (+ 1
459 (abort-current-continuation tag 'foo 'bar)
460 2))
461 tag
462 list)))
463
464 (test* "abort-current-continuation (default handler)" 'aborted
465 (let ([tag (make-continuation-prompt-tag)])
466 (define (abt)
467 (abort-current-continuation tag (lambda () 'aborted)))
468 (call-with-continuation-prompt
469 (lambda () (abt) 'normal)
470 tag)))
471
472 (test* "abort-current-continuation (default handler, nested)" 'aborted
473 (let ([tag (make-continuation-prompt-tag)])
474 (define (abt)
475 ($ abort-current-continuation tag
476 (lambda () (abort-current-continuation tag
477 (lambda () 'aborted)))))
478 (call-with-continuation-prompt
479 (lambda () (abt) 'normal)
480 tag)))
481
482 (test* "abort-current-continuation (invalid tag)"
483 (test-error <continuation-violation>)
484 (let ([tag1 (make-continuation-prompt-tag)]
485 [tag2 (make-continuation-prompt-tag)])
486 (call-with-continuation-prompt
487 (lambda ()
488 (abort-current-continuation tag2 'foo))
489 tag1)))
490
491 (let ([tag1 (make-continuation-prompt-tag 'stray)])
492 (test* "abort-current-continuation (&continuation violation)"
493 tag1
494 (guard (e [(continuation-violation? e)
495 (continuation-violation-prompt-tag e)])
496 (call-with-continuation-prompt
497 (lambda ()
498 (abort-current-continuation tag1 'foo))))))
499
500 '(let ([tag (make-continuation-prompt-tag 'tag)])
501 (test* "abort-current-continuation crosses cstack boundary"
502 '(a)
503 (call-with-continuation-prompt
504 (^[] (with-error-handler (^e (abort-current-continuation tag 'a))
505 (^[] (car 1))))
506 tag list)))
507
508 ;;-----------------------------------------------------------------------
509 ;; Parameterizations
510 ;;
511
512 (test-section "parameterization")
513
514 ;; from SRFI-226
515 (let ()
516 (define p (make-parameter 10 (^x (* x x))))
517 (define ps #f)
518
519 (test* "parameter" 100 (p))
520 (test* "parameter" 100 (p 12))
521 (test* "parameter" 144 (p))
522 (test* "parameter?" #t (parameter? p))
523
524 (test* "current-parameterization" 20736
525 (parameterize ([p (p)])
526 (set! ps (current-parameterization))
527 (p)))
528 (test* "parameter restore" 144 (p))
529
530 (test* "parameterization?" #t (parameterization? ps))
531
532 (test* "call-with-parameterization" '((20736 0) 144)
533 (let1 r (call-with-parameterization ps
534 (^[] (let ([x (p)])
535 (p 0)
536 (list x (p)))))
537 (list r (p))))
538 )
539
540 ;;-----------------------------------------------------------------------
541 ;; Partial continuations
542
543 (test-section "partial continuations")
544 (use gauche.partcont)
545 (test-module 'gauche.partcont)
546
547 (test "reset" 6
548 (^[] (+ 1 (reset (+ 2 3)))))
549 (test "reset" '(1 2)
550 (^[] (cons 1 (reset (cons 2 '())))))
551
552 (test "shift, ignoring k" 4
553 (^[] (+ 1 (reset (+ 2 (shift k 3))))))
554 (test "shift, ignoring k" '(1 3)
555 (^[] (cons 1 (reset (cons 2 (shift k (list 3)))))))
556
557 (test "calling pc" 10
558 (^[] (+ 1 (reset (+ 2 (shift k (+ 3 (k 4))))))))
559 (test "calling pc" '(1 3 2 4)
560 (^[] (cons 1 (reset (cons 2 (shift k (cons 3 (k (cons 4 '())))))))))
561
562 (test "calling pc multi" 14
563 (^[] (+ 1 (reset (+ 2 (shift k (+ 3 (k 5) (k 1))))))))
564 (test "calling pc multi" '(1 3 2 2 4)
565 (^[] (cons 1 (reset (cons 2 (shift k (cons 3 (k (k (cons 4 '()))))))))))
566
567 ;; 'amb' example in Gasbichler&Sperber ICFP2002 paper
568 (let ()
569 (define (eta x) (list (x))) ; unit
570 (define (extend f l) (apply append (map f l))) ; bind
571 (define (reflect meaning) (shift k (extend k meaning)))
572 (define (reify thunk) (reset (eta thunk)))
573 (define (amb* . t)
574 (reflect (apply append (map reify t))))
575 (let-syntax ([amb (syntax-rules () [(amb x ...) (amb* (^[] x) ...)])])
576 (define (www)
577 (let1 f (^x (+ x (amb 6 4 2 8) (amb 2 4 5 4 1)))
578 (reify (^[] (f (f (amb 0 2 3 4 5 32)))))))
579
580 (define (wwww)
581 (let1 f (^x (+ x (amb 6 4 2 8) (amb 2 4 5 4 1)))
582 (reify (^[] (f (f (f (amb 0 2 3 4 5 32))))))))
583
584 (test "www" 2400 (^[] (length (www))))
585 (test "wwww" 48000 (^[] (length (wwww))))
586 ))
587
588 ;; inversion of iterator
589 (let ()
590 (define (inv lis)
591 (define (kk)
592 (reset (for-each (^e (shift k (set! kk k) e)) lis)
593 (set! kk (^[] (eof-object)))
594 (eof-object)))
595 (^[] (kk)))
596 (define iter (inv '(1 2 3 4 5)))
597 (test "inversion" 1 iter)
598 (test "inversion" 2 iter)
599 (test "inversion" 3 iter)
600 (test "inversion" 4 iter)
601 (test "inversion" 5 iter)
602 (test "inversion" (eof-object) iter)
603 (test "inversion" (eof-object) iter))
604
605 ;; A-normal conversion, from
606 ;; http://pllab.is.ocha.ac.jp/~asai/cw2011tutorial/main-j.pdf pp. 11--12
607 (let ()
608 (define (gensym) 't) ;; to have reproducible results
609 (define (a-normal term)
610 (cond [(and (pair? term)
611 (eq? (car term) 'lambda))
612 (let ([var (car (cadr term))]
613 [body (caddr term)])
614 `(lambda (,var) ,(reset (a-normal body))))]
615 [(pair? term)
616 (let ([abs (car term)]
617 [arg (cadr term)])
618 (shift k
619 (let* ([v (gensym)]
620 [body (k v)])
621 `(let ((,v (,(a-normal abs) ,(a-normal arg))))
622 ,body))))]
623 [else term]))
624
625 (test "a-normal"
626 '(lambda (x) (lambda (y) (let ((t (x y))) t)))
627 (^[] (a-normal '(lambda (x) (lambda (y) (x y))))))
628 )
629
630 ;; Typed Printf via Delimited Continuations
631 ;; http://www.cs.ox.ac.uk/ralf.hinze/WG2.8/27/slides/kenichi2.pdf
632 (let ()
633 (define (sprintf format)
634 (reset (format)))
635 (define (fmt dir)
636 (shift k (^v (k (dir v)))))
637 (define s values)
638 (test "sprintf" "hello, world"
639 (^[] ((sprintf (^[] (string-append "hello, " (fmt s)))) "world")))
640 (test "sprintf" "world"
641 (^[] ((sprintf (^[] (fmt s))) "world")))
642 )
643
644 (let-syntax ([gauche-only
645 (syntax-rules ()
646 [(_ expr ...) (begin expr ...)])])
647 (include "partcont.scm"))
648
649 ;;
650 ;; Continuation marks
651 ;;
652
653 (test-section "continuation marks")
654
655 ;; Examples from SRFI-157
656
657 (test* "continuation marks 1" '(1)
658 (let ((key (vector 'key)))
659 (with-continuation-mark key 1
660 (continuation-mark-set->list (current-continuation-marks) key))))
661
662 (test* "continuation marks 2" '(foo 2 1)
663 (let ((key (vector 'key)))
664 (with-continuation-mark key 1
665 (cons 'foo
666 (with-continuation-mark key 2
667 (continuation-mark-set->list (current-continuation-marks) key))))))
668
669 (test* "continuation marks 3" '(2)
670 (let ((key (vector 'key)))
671 (with-continuation-mark key 1
672 (with-continuation-mark key 2
673 (continuation-mark-set->list (current-continuation-marks) key)))))
674
675 (test* "continuation marks w/ captured cont 1" '(1)
676 (let ((key '#:key))
677 (with-continuation-mark key 1
678 (let/cc k
679 (with-continuation-mark key 2
680 (continuation-mark-set->list (continuation-marks k) key))))))
681
682 (test* "continuation marks w/ captured cont 1" '(2 1)
683 (let* ([key '#:key]
684 [p (with-continuation-mark key 1
685 (cons (with-continuation-mark key 2
686 (let/cc k
687 (with-continuation-mark key 3
688 (continuation-marks k))))
689 'a))])
690 (continuation-mark-set->list (car p) key)))
691
692 (define-module ccm-fact
693 (use gauche.test)
694
695 (define %key (vector 'key))
696
697 (define (ccm)
698 (continuation-mark-set->list (current-continuation-marks) %key))
699
700 (define (fact1 n)
701 (let loop ((n n))
702 (if (zero? n)
703 (begin
704 (display (ccm))
705 (newline)
706 1)
707 (with-continuation-mark %key n (* n (loop (- n 1)))))))
708
709 (define (fact2 n)
710 (let loop ((n n) (a 1))
711 (if (zero? n)
712 (begin
713 (display (ccm))
714 (newline)
715 a)
716 (with-continuation-mark %key n (loop (- n 1) (* n a))))))
717
718 (define (t f)
719 (read-from-string (with-output-to-string (cut f 3))))
720
721 (test* "non-tail-recursive fact" '(1 2 3)
722 (t fact1))
723 (test* "tail-recursive fact" '(1)
724 (t fact2)))
725
726 (test* "with-continuation-marks"
727 '((5 1) (2) (4 3))
728 (let1 p (with-continuation-marks (['a 1] ['b 2] ['c 3])
729 (cons (with-continuation-marks (['c 4] ['a 5])
730 (current-continuation-marks))
731 3))
732 (list (continuation-mark-set->list (car p) 'a)
733 (continuation-mark-set->list (car p) 'b)
734 (continuation-mark-set->list (car p) 'c))))
735
736 ;; delimited
737 (test* "current-continuation-marks w/tag" '(d c)
738 (let* ([tag (make-continuation-prompt-tag)]
739 [cms
740 (with-continuation-mark 'a 'b
741 (call-with-continuation-prompt
742 (^[]
743 (with-continuation-mark 'a 'c
744 (apply identity
745 (list (with-continuation-mark 'a 'd
746 (current-continuation-marks tag))))))
747 tag))])
748 (continuation-mark-set->list cms 'a)))
749
750 (test* "current-continuation-marks w/tag"
751 (test-error <continuation-violation>)
752 (let1 tag (make-continuation-prompt-tag)
753 (current-continuation-marks tag)))
754
755 ;; call-with-immediate-continuation-mark
756
757 (test* "call-with-immediate-continuation-mark" 'mark
758 (with-continuation-mark 'key 'mark
759 (call-with-immediate-continuation-mark 'key values)))
760
761 (test* "call-with-immediate-continuation-mark" '(foo . default)
762 (with-continuation-mark 'key 'mark
763 (cons 'foo
764 (call-with-immediate-continuation-mark 'key values 'default))))
765
766 (test* "call-with-immediate-continuation-mark" #f
767 (with-continuation-mark 'key 'mark
768 (call-with-immediate-continuation-mark '#:nonexistent values)))
769
770 ;; continuation-mark-set-first
771
772 (test* "continuation-mark-set-first" 'mark2
773 (with-continuation-mark 'key 'mark1
774 (with-continuation-mark 'key 'mark2
775 (continuation-mark-set-first #f 'key 'default))))
776
777 (test* "continuation-mark-set-first" 'mark1
778 (with-continuation-mark 'key 'mark1
779 (let1 cm (current-continuation-marks)
780 (with-continuation-mark 'key 'mark2
781 (continuation-mark-set-first cm 'key 'default)))))
782
783 (test* "continuation-mark-set-first" 'default
784 (with-continuation-mark 'key 'mark1
785 (with-continuation-mark 'key 'mark2
786 (continuation-mark-set-first #f 'key2 'default))))
787
788 ;; See if parameterize body is evaluated in tail context
789 ;; (SRFI-226)
790 (test* "parameterize body in tail context" #t
791 (with-continuation-mark 'in-tail-context #t
792 (parameterize ([(make-parameter 0) 1])
793 (call-with-immediate-continuation-mark 'in-tail-context identity))))
794
795 (test-end)