| 1 |
;;;; -*- scheme -*- |
| 2 |
;;;; control.test --- test suite for delimited continuations |
| 3 |
;;;; |
| 4 |
;;;; Copyright (C) 2010, 2011, 2013 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-control) |
| 21 |
#:use-module (ice-9 control) |
| 22 |
#:use-module (system vm vm) |
| 23 |
#:use-module (srfi srfi-1) |
| 24 |
#:use-module (srfi srfi-11) |
| 25 |
#:use-module (test-suite lib)) |
| 26 |
|
| 27 |
|
| 28 |
;; For these, the compiler should be able to prove that "k" is not referenced, |
| 29 |
;; so it avoids reifying the continuation. Since that's a slightly different |
| 30 |
;; codepath, we test them both. |
| 31 |
(with-test-prefix/c&e "escape-only continuations" |
| 32 |
(pass-if "no values, normal exit" |
| 33 |
(equal? '() |
| 34 |
(call-with-values |
| 35 |
(lambda () |
| 36 |
(% (values) |
| 37 |
(lambda (k . args) |
| 38 |
(error "unexpected exit" args)))) |
| 39 |
list))) |
| 40 |
|
| 41 |
(pass-if "no values, abnormal exit" |
| 42 |
(equal? '() |
| 43 |
(% (begin |
| 44 |
(abort) |
| 45 |
(error "unexpected exit")) |
| 46 |
(lambda (k . args) |
| 47 |
args)))) |
| 48 |
|
| 49 |
(pass-if "single value, normal exit" |
| 50 |
(equal? '(foo) |
| 51 |
(call-with-values |
| 52 |
(lambda () |
| 53 |
(% 'foo |
| 54 |
(lambda (k . args) |
| 55 |
(error "unexpected exit" args)))) |
| 56 |
list))) |
| 57 |
|
| 58 |
(pass-if "single value, abnormal exit" |
| 59 |
(equal? '(foo) |
| 60 |
(% (begin |
| 61 |
(abort 'foo) |
| 62 |
(error "unexpected exit")) |
| 63 |
(lambda (k . args) |
| 64 |
args)))) |
| 65 |
|
| 66 |
(pass-if "multiple values, normal exit" |
| 67 |
(equal? '(foo bar baz) |
| 68 |
(call-with-values |
| 69 |
(lambda () |
| 70 |
(% (values 'foo 'bar 'baz) |
| 71 |
(lambda (k . args) |
| 72 |
(error "unexpected exit" args)))) |
| 73 |
list))) |
| 74 |
|
| 75 |
(pass-if "multiple values, abnormal exit" |
| 76 |
(equal? '(foo bar baz) |
| 77 |
(% (begin |
| 78 |
(abort 'foo 'bar 'baz) |
| 79 |
(error "unexpected exit")) |
| 80 |
(lambda (k . args) |
| 81 |
args)))) |
| 82 |
|
| 83 |
(pass-if-equal "call/ec" '(0 1 2) ; example from the manual |
| 84 |
(let ((prefix |
| 85 |
(lambda (x lst) |
| 86 |
(call/ec |
| 87 |
(lambda (return) |
| 88 |
(fold (lambda (element prefix) |
| 89 |
(if (equal? element x) |
| 90 |
(return (reverse prefix)) |
| 91 |
(cons element prefix))) |
| 92 |
'() |
| 93 |
lst)))))) |
| 94 |
(prefix 'a '(0 1 2 a 3 4 5)))) |
| 95 |
|
| 96 |
(pass-if-equal "let/ec" '(0 1 2) |
| 97 |
(let ((prefix |
| 98 |
(lambda (x lst) |
| 99 |
(let/ec return |
| 100 |
(fold (lambda (element prefix) |
| 101 |
(if (equal? element x) |
| 102 |
(return (reverse prefix)) |
| 103 |
(cons element prefix))) |
| 104 |
'() |
| 105 |
lst))))) |
| 106 |
(prefix 'a '(0 1 2 a 3 4 5)))) |
| 107 |
|
| 108 |
(pass-if "loop only in handler" |
| 109 |
(let ((n #f)) |
| 110 |
(let lp () |
| 111 |
(or n |
| 112 |
(call-with-prompt 'foo |
| 113 |
(lambda () |
| 114 |
(set! n #t) |
| 115 |
(abort-to-prompt 'foo)) |
| 116 |
(lambda (k) (lp)))))))) |
| 117 |
|
| 118 |
;;; And the case in which the compiler has to reify the continuation. |
| 119 |
(with-test-prefix/c&e "reified continuations" |
| 120 |
(pass-if "no values, normal exit" |
| 121 |
(equal? '() |
| 122 |
(call-with-values |
| 123 |
(lambda () |
| 124 |
(% (values) |
| 125 |
(lambda (k . args) |
| 126 |
(error "unexpected exit" k args)))) |
| 127 |
list))) |
| 128 |
|
| 129 |
(pass-if "no values, abnormal exit" |
| 130 |
(equal? '() |
| 131 |
(cdr |
| 132 |
(% (begin |
| 133 |
(abort) |
| 134 |
(error "unexpected exit")) |
| 135 |
(lambda args |
| 136 |
args))))) |
| 137 |
|
| 138 |
(pass-if "single value, normal exit" |
| 139 |
(equal? '(foo) |
| 140 |
(call-with-values |
| 141 |
(lambda () |
| 142 |
(% 'foo |
| 143 |
(lambda (k . args) |
| 144 |
(error "unexpected exit" k args)))) |
| 145 |
list))) |
| 146 |
|
| 147 |
(pass-if "single value, abnormal exit" |
| 148 |
(equal? '(foo) |
| 149 |
(cdr |
| 150 |
(% (begin |
| 151 |
(abort 'foo) |
| 152 |
(error "unexpected exit")) |
| 153 |
(lambda args |
| 154 |
args))))) |
| 155 |
|
| 156 |
(pass-if "multiple values, normal exit" |
| 157 |
(equal? '(foo bar baz) |
| 158 |
(call-with-values |
| 159 |
(lambda () |
| 160 |
(% (values 'foo 'bar 'baz) |
| 161 |
(lambda (k . args) |
| 162 |
(error "unexpected exit" k args)))) |
| 163 |
list))) |
| 164 |
|
| 165 |
(pass-if "multiple values, abnormal exit" |
| 166 |
(equal? '(foo bar baz) |
| 167 |
(cdr |
| 168 |
(% (begin |
| 169 |
(abort 'foo 'bar 'baz) |
| 170 |
(error "unexpected exit")) |
| 171 |
(lambda args |
| 172 |
args))))) |
| 173 |
|
| 174 |
(pass-if "reified pending call frames, instantiated elsewhere on the stack" |
| 175 |
(equal? 'foo |
| 176 |
((call-with-prompt |
| 177 |
'p0 |
| 178 |
(lambda () |
| 179 |
(identity ((abort-to-prompt 'p0) 'foo))) |
| 180 |
(lambda (c) c)) |
| 181 |
(lambda (x) x))))) |
| 182 |
|
| 183 |
|
| 184 |
;; The variants check different cases in the compiler. |
| 185 |
(with-test-prefix/c&e "restarting partial continuations" |
| 186 |
(pass-if "in side-effect position" |
| 187 |
(let ((k (% (begin (abort) 'foo) |
| 188 |
(lambda (k) k)))) |
| 189 |
(eq? (k) |
| 190 |
'foo))) |
| 191 |
|
| 192 |
(pass-if "passing values to side-effect abort" |
| 193 |
(let ((k (% (begin (abort) 'foo) |
| 194 |
(lambda (k) k)))) |
| 195 |
(eq? (k 'qux 'baz 'hello) |
| 196 |
'foo))) |
| 197 |
|
| 198 |
(pass-if "called for one value" |
| 199 |
(let ((k (% (+ (abort) 3) |
| 200 |
(lambda (k) k)))) |
| 201 |
(eqv? (k 39) |
| 202 |
42))) |
| 203 |
|
| 204 |
(pass-if "called for multiple values" |
| 205 |
(let ((k (% (let-values (((a b . c) (abort))) |
| 206 |
(list a b c)) |
| 207 |
(lambda (k) k)))) |
| 208 |
(equal? (k 1 2 3 4) |
| 209 |
'(1 2 (3 4))))) |
| 210 |
|
| 211 |
(pass-if "in tail position" |
| 212 |
(let ((k (% (abort) |
| 213 |
(lambda (k) k)))) |
| 214 |
(eq? (k 'xyzzy) |
| 215 |
'xyzzy)))) |
| 216 |
|
| 217 |
;; Here we test different cases for the `prompt'. |
| 218 |
(with-test-prefix/c&e "prompt in different contexts" |
| 219 |
(pass-if "push, normal exit" |
| 220 |
(car (call-with-prompt |
| 221 |
'foo |
| 222 |
(lambda () '(#t)) |
| 223 |
(lambda (k) '(#f))))) |
| 224 |
|
| 225 |
(pass-if "push, nonlocal exit" |
| 226 |
(car (call-with-prompt |
| 227 |
'foo |
| 228 |
(lambda () (abort-to-prompt 'foo) '(#f)) |
| 229 |
(lambda (k) '(#t))))) |
| 230 |
|
| 231 |
(pass-if "push with RA, normal exit" |
| 232 |
(car (letrec ((test (lambda () |
| 233 |
(call-with-prompt |
| 234 |
'foo |
| 235 |
(lambda () '(#t)) |
| 236 |
(lambda (k) '(#f)))))) |
| 237 |
(test)))) |
| 238 |
|
| 239 |
(pass-if "push with RA, nonlocal exit" |
| 240 |
(car (letrec ((test (lambda () |
| 241 |
(call-with-prompt |
| 242 |
'foo |
| 243 |
(lambda () (abort-to-prompt 'foo) '(#f)) |
| 244 |
(lambda (k) '(#t)))))) |
| 245 |
(test)))) |
| 246 |
|
| 247 |
(pass-if "tail, normal exit" |
| 248 |
(call-with-prompt |
| 249 |
'foo |
| 250 |
(lambda () #t) |
| 251 |
(lambda (k) #f))) |
| 252 |
|
| 253 |
(pass-if "tail, nonlocal exit" |
| 254 |
(call-with-prompt |
| 255 |
'foo |
| 256 |
(lambda () (abort-to-prompt 'foo) #f) |
| 257 |
(lambda (k) #t))) |
| 258 |
|
| 259 |
(pass-if "tail with RA, normal exit" |
| 260 |
(letrec ((test (lambda () |
| 261 |
(call-with-prompt |
| 262 |
'foo |
| 263 |
(lambda () #t) |
| 264 |
(lambda (k) #f))))) |
| 265 |
(test))) |
| 266 |
|
| 267 |
(pass-if "tail with RA, nonlocal exit" |
| 268 |
(letrec ((test (lambda () |
| 269 |
(call-with-prompt |
| 270 |
'foo |
| 271 |
(lambda () (abort-to-prompt 'foo) #f) |
| 272 |
(lambda (k) #t))))) |
| 273 |
(test))) |
| 274 |
|
| 275 |
(pass-if "drop, normal exit" |
| 276 |
(begin |
| 277 |
(call-with-prompt |
| 278 |
'foo |
| 279 |
(lambda () #f) |
| 280 |
(lambda (k) #f)) |
| 281 |
#t)) |
| 282 |
|
| 283 |
(pass-if "drop, nonlocal exit" |
| 284 |
(begin |
| 285 |
(call-with-prompt |
| 286 |
'foo |
| 287 |
(lambda () (abort-to-prompt 'foo)) |
| 288 |
(lambda (k) #f)) |
| 289 |
#t)) |
| 290 |
|
| 291 |
(pass-if "drop with RA, normal exit" |
| 292 |
(begin |
| 293 |
(letrec ((test (lambda () |
| 294 |
(call-with-prompt |
| 295 |
'foo |
| 296 |
(lambda () #f) |
| 297 |
(lambda (k) #f))))) |
| 298 |
(test)) |
| 299 |
#t)) |
| 300 |
|
| 301 |
(pass-if "drop with RA, nonlocal exit" |
| 302 |
(begin |
| 303 |
(letrec ((test (lambda () |
| 304 |
(call-with-prompt |
| 305 |
'foo |
| 306 |
(lambda () (abort-to-prompt 'foo) #f) |
| 307 |
(lambda (k) #f))))) |
| 308 |
(test)) |
| 309 |
#t))) |
| 310 |
|
| 311 |
|
| 312 |
(define fl (make-fluid)) |
| 313 |
(fluid-set! fl 0) |
| 314 |
|
| 315 |
;; Not c&e as it assumes this block executes once. |
| 316 |
;; |
| 317 |
(with-test-prefix "suspend/resume with fluids" |
| 318 |
(pass-if "normal" |
| 319 |
(zero? (% (fluid-ref fl) |
| 320 |
error))) |
| 321 |
(pass-if "with-fluids normal" |
| 322 |
(equal? (% (with-fluids ((fl (1+ (fluid-ref fl)))) |
| 323 |
(fluid-ref fl)) |
| 324 |
error) |
| 325 |
1)) |
| 326 |
(pass-if "normal (post)" |
| 327 |
(zero? (fluid-ref fl))) |
| 328 |
(pass-if "with-fluids and fluid-set!" |
| 329 |
(equal? (% (with-fluids ((fl (1+ (fluid-ref fl)))) |
| 330 |
(fluid-set! fl (1+ (fluid-ref fl))) |
| 331 |
(fluid-ref fl)) |
| 332 |
error) |
| 333 |
2)) |
| 334 |
(pass-if "normal (post2)" |
| 335 |
(zero? (fluid-ref fl))) |
| 336 |
(pass-if "normal fluid-set!" |
| 337 |
(equal? (begin |
| 338 |
(fluid-set! fl (1+ (fluid-ref fl))) |
| 339 |
(fluid-ref fl)) |
| 340 |
1)) |
| 341 |
(pass-if "reset fluid-set!" |
| 342 |
(equal? (begin |
| 343 |
(fluid-set! fl (1- (fluid-ref fl))) |
| 344 |
(fluid-ref fl)) |
| 345 |
0)) |
| 346 |
|
| 347 |
(let ((k (% (with-fluids ((fl (1+ (fluid-ref fl)))) |
| 348 |
(abort) |
| 349 |
(fluid-ref fl)) |
| 350 |
(lambda (k) k)))) |
| 351 |
(pass-if "pre" |
| 352 |
(equal? (fluid-ref fl) 0)) |
| 353 |
(pass-if "res" |
| 354 |
(equal? (k) 1)) |
| 355 |
(pass-if "post" |
| 356 |
(equal? (fluid-ref fl) 0)))) |
| 357 |
|
| 358 |
(with-test-prefix/c&e "rewinding prompts" |
| 359 |
(pass-if "nested prompts" |
| 360 |
(let ((k (% 'a |
| 361 |
(% 'b |
| 362 |
(begin |
| 363 |
(abort-to-prompt 'a) |
| 364 |
(abort-to-prompt 'b #t)) |
| 365 |
(lambda (k x) x)) |
| 366 |
(lambda (k) k)))) |
| 367 |
(k)))) |
| 368 |
|
| 369 |
(with-test-prefix/c&e "abort to unknown prompt" |
| 370 |
(pass-if-exception "foo" '(misc-error . "^Abort to unknown prompt") |
| 371 |
(abort-to-prompt 'does-not-exist))) |
| 372 |
|
| 373 |
(with-test-prefix/c&e "unwind" |
| 374 |
|
| 375 |
(pass-if "unwind through call-with-vm" |
| 376 |
(let ((proc (lambda (x y) |
| 377 |
(expt x y))) |
| 378 |
(call (lambda (p x y) |
| 379 |
(p x y)))) |
| 380 |
(catch 'foo |
| 381 |
(lambda () |
| 382 |
(call-with-vm (lambda () (throw 'foo)))) |
| 383 |
(lambda (key) |
| 384 |
(eq? key 'foo)))))) |
| 385 |
|
| 386 |
;; These tests from Oleg Kiselyov's delim-control-n.scm, available at |
| 387 |
;; http://okmij.org/ftp/Scheme/delim-control-n.scm. Public domain. |
| 388 |
;; |
| 389 |
(with-test-prefix "shift and reset" |
| 390 |
(pass-if (equal? |
| 391 |
117 |
| 392 |
(+ 10 (reset (+ 2 (shift k (+ 100 (k (k 3))))))))) |
| 393 |
|
| 394 |
(pass-if (equal? |
| 395 |
60 |
| 396 |
(* 10 (reset (* 2 (shift g (* 5 (shift f (+ (f 1) 1))))))))) |
| 397 |
|
| 398 |
(pass-if (equal? |
| 399 |
121 |
| 400 |
(let ((f (lambda (x) (shift k (k (k x)))))) |
| 401 |
(+ 1 (reset (+ 10 (f 100))))))) |
| 402 |
|
| 403 |
(pass-if (equal? |
| 404 |
'a |
| 405 |
(car (reset |
| 406 |
(let ((x (shift f |
| 407 |
(shift f1 (f1 (cons 'a (f '()))))))) |
| 408 |
(shift g x)))))) |
| 409 |
|
| 410 |
;; Example by Olivier Danvy |
| 411 |
(pass-if (equal? |
| 412 |
'(1 2 3 4 5) |
| 413 |
(let () |
| 414 |
(define (traverse xs) |
| 415 |
(define (visit xs) |
| 416 |
(if (null? xs) |
| 417 |
'() |
| 418 |
(visit (shift* |
| 419 |
(lambda (k) |
| 420 |
(cons (car xs) (k (cdr xs)))))))) |
| 421 |
(reset* (lambda () (visit xs)))) |
| 422 |
(traverse '(1 2 3 4 5)))))) |
| 423 |
|
| 424 |
(with-test-prefix "suspendable-continuation?" |
| 425 |
(let ((tag (make-prompt-tag))) |
| 426 |
(pass-if "escape-only" |
| 427 |
(call-with-prompt tag |
| 428 |
(lambda () |
| 429 |
(suspendable-continuation? tag)) |
| 430 |
(lambda _ (error "unreachable")))) |
| 431 |
(pass-if "full" |
| 432 |
(call-with-prompt tag |
| 433 |
(lambda () |
| 434 |
(suspendable-continuation? tag)) |
| 435 |
(lambda (k) (error "unreachable" k)))) |
| 436 |
(pass-if "escape-only with barrier" |
| 437 |
(call-with-prompt tag |
| 438 |
(lambda () |
| 439 |
(with-continuation-barrier |
| 440 |
(lambda () |
| 441 |
(not (suspendable-continuation? tag))))) |
| 442 |
(lambda _ (error "unreachable")))) |
| 443 |
(pass-if "full with barrier" |
| 444 |
(call-with-prompt tag |
| 445 |
(lambda () |
| 446 |
(with-continuation-barrier |
| 447 |
(lambda () |
| 448 |
(not (suspendable-continuation? tag))))) |
| 449 |
(lambda (k) (error "unreachable" k)))))) |