| 1 |
;;;; -*- scheme -*- |
| 2 |
;;;; continuations.test --- test suite for continutations |
| 3 |
;;;; |
| 4 |
;;;; Copyright (C) 2003, 2006, 2009, 2011 Free Software Foundation, Inc. |
| 5 |
;;;; |
| 6 |
;;;; This library is free software; you can redistribute it and/or |
| 7 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 8 |
;;;; License as published by the Free Software Foundation; either |
| 9 |
;;;; version 3 of the License, or (at your option) any later version. |
| 10 |
;;;; |
| 11 |
;;;; This library is distributed in the hope that it will be useful, |
| 12 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
;;;; Lesser General Public License for more details. |
| 15 |
;;;; |
| 16 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 17 |
;;;; License along with this library; if not, write to the Free Software |
| 18 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
|
| 20 |
(define-module (test-suite test-continuations) |
| 21 |
:use-module (test-suite lib)) |
| 22 |
|
| 23 |
(define (block-reentry body) |
| 24 |
(let ((active #f)) |
| 25 |
(dynamic-wind |
| 26 |
(lambda () |
| 27 |
(if active |
| 28 |
(throw 'no-reentry))) |
| 29 |
(lambda () |
| 30 |
(set! active #t) |
| 31 |
(body)) |
| 32 |
(lambda () #f)))) |
| 33 |
|
| 34 |
(define (catch-tag body) |
| 35 |
(catch #t |
| 36 |
body |
| 37 |
(lambda (tag . args) tag))) |
| 38 |
|
| 39 |
(define (check-cont) |
| 40 |
(catch-tag |
| 41 |
(lambda () |
| 42 |
(block-reentry (lambda () (call/cc identity)))))) |
| 43 |
|
| 44 |
(define (dont-crash-please) |
| 45 |
(let ((k (check-cont))) |
| 46 |
(if (procedure? k) |
| 47 |
(k 12) |
| 48 |
k))) |
| 49 |
|
| 50 |
(with-test-prefix "continuations" |
| 51 |
|
| 52 |
(pass-if "throwing to a rewound catch context" |
| 53 |
(eq? (dont-crash-please) 'no-reentry)) |
| 54 |
|
| 55 |
(pass-if "can print a continuation" |
| 56 |
(let ((s (with-output-to-string |
| 57 |
(lambda () |
| 58 |
(call-with-current-continuation write))))) |
| 59 |
(string=? "#<continuation " (substring s 0 15)))) |
| 60 |
|
| 61 |
(pass-if "blocked attempt to cross a continuation barrier" |
| 62 |
(call-with-current-continuation |
| 63 |
(lambda (k) |
| 64 |
(with-continuation-barrier |
| 65 |
(lambda () |
| 66 |
(catch 'misc-error |
| 67 |
(lambda () |
| 68 |
(k 1) |
| 69 |
#f) |
| 70 |
(lambda _ |
| 71 |
#t))))))) |
| 72 |
|
| 73 |
(pass-if "uncaught exception is handled by continuation barrier" |
| 74 |
(let* ((handled #f) |
| 75 |
(s (with-error-to-string |
| 76 |
(lambda () |
| 77 |
(set! handled |
| 78 |
(not (with-continuation-barrier |
| 79 |
(lambda () |
| 80 |
(error "Catch me if you can!"))))))))) |
| 81 |
handled)) |
| 82 |
|
| 83 |
(pass-if "exit unwinds dynwinds inside a continuation barrier" |
| 84 |
(let ((s (with-error-to-string |
| 85 |
(lambda () |
| 86 |
(with-continuation-barrier |
| 87 |
(lambda () |
| 88 |
(dynamic-wind |
| 89 |
(lambda () #f) |
| 90 |
(lambda () (exit 1)) |
| 91 |
(lambda () (throw 'abcde))))))))) |
| 92 |
(and (string-contains s "abcde") #t))) |
| 93 |
|
| 94 |
(with-debugging-evaluator |
| 95 |
|
| 96 |
(pass-if "make a stack from a continuation" |
| 97 |
(stack? (call-with-current-continuation make-stack))) |
| 98 |
|
| 99 |
(pass-if "get a continuation's stack ID" |
| 100 |
(let ((id (call-with-current-continuation stack-id))) |
| 101 |
(or (boolean? id) (symbol? id))))) |
| 102 |
|
| 103 |
) |