~ chicken-core (chicken-5) /tests/dwindtst.scm
Trap1;;;; "dwindtst.scm", routines for characterizing dynamic-wind.
2;Copyright (C) 1992 Aubrey Jaffer
3;
4;Permission to copy this software, to modify it, to redistribute it,
5;to distribute modified versions, and to use it for any purpose is
6;granted, subject to the following restrictions and understandings.
7;
8;1. Any copy made of this software must include this copyright notice
9;in full.
10;
11;2. I have made no warranty or representation that the operation of
12;this software will be error-free, and I am under no obligation to
13;provide any services, by way of maintenance, update, or otherwise.
14;
15;3. In conjunction with products arising from the use of this
16;material, there shall be no use of my name in any advertising,
17;promotional, or sales literature without prior written consent in
18;each case.
19
20(define (dwtest n)
21 (define cont #f)
22 (display "testing escape from thunk") (display n) (newline)
23 (display "visiting:") (newline)
24 (call-with-current-continuation
25 (lambda (x) (set! cont x)))
26 (if n
27 (dynamic-wind
28 (lambda ()
29 (display "thunk1") (newline)
30 (if (eqv? n 1) (let ((ntmp n))
31 (set! n #f)
32 (cont ntmp))))
33 (lambda ()
34 (display "thunk2") (newline)
35 (if (eqv? n 2) (let ((ntmp n))
36 (set! n #f)
37 (cont ntmp))))
38 (lambda ()
39 (display "thunk3") (newline)
40 (if (eqv? n 3) (let ((ntmp n))
41 (set! n #f)
42 (cont ntmp)))))))
43(define (dwctest n)
44 (define cont #f)
45 (define ccont #f)
46 (display "creating continuation thunk") (newline)
47 (display "visiting:") (newline)
48 (call-with-current-continuation
49 (lambda (x) (set! cont x)))
50 (if n (set! n (- n)))
51 (if n
52 (dynamic-wind
53 (lambda ()
54 (display "thunk1") (newline)
55 (if (eqv? n 1) (let ((ntmp n))
56 (set! n #f)
57 (cont ntmp))))
58 (lambda ()
59 (call-with-current-continuation
60 (lambda (x) (set! ccont x)))
61 (display "thunk2") (newline)
62 (if (eqv? n 2) (let ((ntmp n))
63 (set! n #f)
64 (cont ntmp))))
65 (lambda ()
66 (display "thunk3") (newline)
67 (if (eqv? n 3) (let ((ntmp n))
68 (set! n #f)
69 (cont ntmp))))))
70 (cond
71 (n
72 (set! n (- n))
73 (display "testing escape from continuation thunk") (display n) (newline)
74 (display "visiting:") (newline)
75 (ccont #f))))
76
77(dwtest 1) (dwtest 2) (dwtest 3)
78(dwctest 1) (dwctest 2) (dwctest 3)