| 1 |
;; |
| 2 |
;; test error handlers |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(test-start "error and exception handlers") |
| 7 |
|
| 8 |
;; NB: this test is run just after the primitive syntax test, |
| 9 |
;; and before tests of many standard features. So we test |
| 10 |
;; the minimal features here. The full test for the exception |
| 11 |
;; handling system is done in exception.scm. |
| 12 |
|
| 13 |
;;---------------------------------------------------------------- |
| 14 |
(test-section "with-error-handler") |
| 15 |
|
| 16 |
(prim-test "basic" '(1 . 2) |
| 17 |
(lambda () |
| 18 |
(cons 1 (with-error-handler (lambda (e) 2) |
| 19 |
(lambda () (car 2)))))) |
| 20 |
(prim-test "basic" '(1 2 3) |
| 21 |
(lambda () |
| 22 |
(list (with-error-handler (lambda (e) 1) |
| 23 |
(lambda () (car 2))) |
| 24 |
(with-error-handler (lambda (e) -1) |
| 25 |
(lambda () 2)) |
| 26 |
(with-error-handler (lambda (e) 3) |
| 27 |
(lambda () (car 3)))))) |
| 28 |
|
| 29 |
(prim-test "with let" 1 |
| 30 |
(lambda () |
| 31 |
(let ((x 1)) |
| 32 |
(with-error-handler (lambda (e) x) |
| 33 |
(lambda () (car 0)))))) |
| 34 |
|
| 35 |
(prim-test "with let" 1 |
| 36 |
(lambda () |
| 37 |
(let ((x 1)) |
| 38 |
(with-error-handler (lambda (e) x) |
| 39 |
(lambda () |
| 40 |
(let ((x 2)) |
| 41 |
(car x))))))) |
| 42 |
|
| 43 |
(prim-test "cascade" 3 |
| 44 |
(lambda () |
| 45 |
(with-error-handler |
| 46 |
(lambda (e) 3) |
| 47 |
(lambda () |
| 48 |
(with-error-handler |
| 49 |
(lambda (e) (car 0)) |
| 50 |
(lambda () |
| 51 |
(car 4))))))) |
| 52 |
|
| 53 |
(prim-test "over c stack" '(1 . 2) |
| 54 |
(lambda () |
| 55 |
(cons 1 |
| 56 |
(with-error-handler |
| 57 |
(lambda (e) 2) |
| 58 |
(lambda () |
| 59 |
(sort '(1 8 3 7 4) |
| 60 |
(lambda (a b) (car a)))))))) |
| 61 |
|
| 62 |
(prim-test "with dynamic wind" '(a b c) |
| 63 |
(lambda () |
| 64 |
(let ((x '())) |
| 65 |
(with-error-handler |
| 66 |
(lambda (e) (set! x (cons 'b x))) |
| 67 |
(lambda () |
| 68 |
(dynamic-wind |
| 69 |
(lambda () (set! x (cons 'c x))) |
| 70 |
(lambda () (car 3)) |
| 71 |
(lambda () (set! x (cons 'a x)))))) |
| 72 |
x))) |
| 73 |
|
| 74 |
(prim-test "with dynamic wind" '(a b e c d f) |
| 75 |
(lambda () |
| 76 |
(let ((x '())) |
| 77 |
(dynamic-wind |
| 78 |
(lambda () (push! x 'a)) |
| 79 |
(lambda () |
| 80 |
(with-error-handler |
| 81 |
(lambda (e) (push! x 'e)) |
| 82 |
(lambda () |
| 83 |
(dynamic-wind |
| 84 |
(lambda () (push! x 'b)) |
| 85 |
(lambda () (car 3)) |
| 86 |
(lambda () (push! x 'c))))) |
| 87 |
(push! x 'd)) |
| 88 |
(lambda () (push! x 'f))) |
| 89 |
(reverse x)))) |
| 90 |
|
| 91 |
(prim-test "repeat" 10 |
| 92 |
(lambda () |
| 93 |
(let loop ((i 0)) |
| 94 |
(if (< i 10) |
| 95 |
(begin (with-error-handler |
| 96 |
(lambda (e) i) |
| 97 |
(lambda () (car i))) |
| 98 |
(loop (+ i 1))) |
| 99 |
i)))) |
| 100 |
|
| 101 |
;;---------------------------------------------------------------- |
| 102 |
(test-section "cascading errors") |
| 103 |
|
| 104 |
;; tests various interactions with with-error-handler and dynamic-wind |
| 105 |
;; when an error is raised from error handler. |
| 106 |
|
| 107 |
(prim-test "cascading error" '(a b c e d) |
| 108 |
(lambda () |
| 109 |
(let ((x '())) |
| 110 |
(with-error-handler |
| 111 |
(lambda (e) (push! x 'e)) |
| 112 |
(lambda () |
| 113 |
(dynamic-wind |
| 114 |
(lambda () (push! x 'a)) |
| 115 |
(lambda () |
| 116 |
(with-error-handler |
| 117 |
(lambda (e) (push! x 'c) (car 9)) |
| 118 |
(lambda () |
| 119 |
(push! x 'b) |
| 120 |
(car 3) |
| 121 |
(push! x 'z)))) |
| 122 |
(lambda () (push! x 'd))))) |
| 123 |
(reverse x)))) |
| 124 |
|
| 125 |
(prim-test "cascading error 2" '(a b c d e f g) |
| 126 |
(lambda () |
| 127 |
(let ((x '())) |
| 128 |
(dynamic-wind |
| 129 |
(lambda () (push! x 'a)) |
| 130 |
(lambda () |
| 131 |
(with-error-handler |
| 132 |
(lambda (e) (push! x 'e)) |
| 133 |
(lambda () |
| 134 |
(dynamic-wind |
| 135 |
(lambda () (push! x 'b)) |
| 136 |
(lambda () |
| 137 |
(with-error-handler |
| 138 |
(lambda (e) (push! x 'd) (raise e)) |
| 139 |
(lambda () (push! x 'c) (car 3) (push! x 'z)))) |
| 140 |
(lambda () (push! x 'f)))))) |
| 141 |
(lambda () (push! x 'g))) |
| 142 |
(reverse x)))) |
| 143 |
|
| 144 |
(prim-test "cascading error 3" '(a b c d f g) |
| 145 |
(lambda () |
| 146 |
(let ((x '())) |
| 147 |
(dynamic-wind |
| 148 |
(lambda () (push! x 'a)) |
| 149 |
(lambda () |
| 150 |
(with-error-handler |
| 151 |
(lambda (e) (push! x e)) |
| 152 |
(lambda () |
| 153 |
(dynamic-wind |
| 154 |
(lambda () (push! x 'b)) |
| 155 |
(lambda () |
| 156 |
(with-error-handler |
| 157 |
(lambda (e) (push! x 'd)) |
| 158 |
(lambda () (push! x 'c) (car 3) (push! x 'z)))) |
| 159 |
(lambda () (push! x 'f)))))) |
| 160 |
(lambda () (push! x 'g))) |
| 161 |
(reverse x)))) |
| 162 |
|
| 163 |
(prim-test "cascading error 4" '(a b c d e f g h i j) |
| 164 |
(lambda () |
| 165 |
(let ((x '())) |
| 166 |
(dynamic-wind |
| 167 |
(lambda () (push! x 'a)) |
| 168 |
(lambda () |
| 169 |
(with-error-handler |
| 170 |
(lambda (e) (push! x 'g)) |
| 171 |
(lambda () |
| 172 |
(dynamic-wind |
| 173 |
(lambda () (push! x 'b)) |
| 174 |
(lambda () |
| 175 |
(with-error-handler |
| 176 |
(lambda (e) (push! x 'f) (raise e)) |
| 177 |
(lambda () |
| 178 |
(dynamic-wind |
| 179 |
(lambda () (push! x 'c)) |
| 180 |
(lambda () |
| 181 |
(with-error-handler |
| 182 |
(lambda (e) (push! x 'e) (raise e)) |
| 183 |
(lambda () (push! x 'd) (car 3) (push! x 'z)))) |
| 184 |
(lambda () (push! x 'h)))))) |
| 185 |
(lambda () (push! x 'i)))))) |
| 186 |
(lambda () (push! x 'j))) |
| 187 |
(reverse x)))) |
| 188 |
|
| 189 |
(prim-test "cascading error 5" '(a b c d e f g) |
| 190 |
(lambda () |
| 191 |
(let ((x '())) |
| 192 |
(dynamic-wind |
| 193 |
(lambda () (push! x 'a)) |
| 194 |
(lambda () |
| 195 |
(with-error-handler |
| 196 |
(lambda (e) (push! x 'e)) |
| 197 |
(lambda () |
| 198 |
(with-error-handler |
| 199 |
(lambda (e) (push! x 'd) (raise e)) |
| 200 |
(lambda () |
| 201 |
(dynamic-wind |
| 202 |
(lambda () (push! x 'b)) |
| 203 |
(lambda () (push! x 'c) (car 3) (push! x 'z)) |
| 204 |
(lambda () (push! x 'f)))))))) |
| 205 |
(lambda () (push! x 'g))) |
| 206 |
(reverse x)))) |
| 207 |
|
| 208 |
(prim-test "cascading error 6" '(a b c d e f g) |
| 209 |
(lambda () |
| 210 |
(let ((x '())) |
| 211 |
(with-error-handler |
| 212 |
(lambda (e) (push! x 'e)) |
| 213 |
(lambda () |
| 214 |
(dynamic-wind |
| 215 |
(lambda () (push! x 'a)) |
| 216 |
(lambda () |
| 217 |
(dynamic-wind |
| 218 |
(lambda () (push! x 'b)) |
| 219 |
(lambda () |
| 220 |
(with-error-handler |
| 221 |
(lambda (e) (push! x 'd) (raise e)) |
| 222 |
(lambda () (push! x 'c) (open-input-file 3) (push! x 'z)))) |
| 223 |
(lambda () (push! x 'f)))) |
| 224 |
(lambda () (push! x 'g))))) |
| 225 |
(reverse x)))) |
| 226 |
|
| 227 |
;;---------------------------------------------------------------- |
| 228 |
(test-section "error in before/after thunk") |
| 229 |
|
| 230 |
(prim-test "error in before thunk" '(a c) |
| 231 |
(lambda () |
| 232 |
(let ((x '())) |
| 233 |
(with-error-handler |
| 234 |
(lambda (e) (push! x 'c)) |
| 235 |
(lambda () |
| 236 |
(dynamic-wind |
| 237 |
(lambda () (push! x 'a) (car 3) (push! x 'z)) |
| 238 |
(lambda () (push! x 'b)) |
| 239 |
(lambda () (push! x 'c))))) |
| 240 |
(reverse x)))) |
| 241 |
|
| 242 |
(prim-test "error in after thunk" '(a b c d) |
| 243 |
(lambda () |
| 244 |
(let ((x '())) |
| 245 |
(with-error-handler |
| 246 |
(lambda (e) (push! x 'd)) |
| 247 |
(lambda () |
| 248 |
(dynamic-wind |
| 249 |
(lambda () (push! x 'a)) |
| 250 |
(lambda () (push! x 'b)) |
| 251 |
(lambda () (push! x 'c) (car 3) (push! x 'z))))) |
| 252 |
(reverse x)))) |
| 253 |
|
| 254 |
(prim-test "error in before thunk (nested)" '(a b c d) |
| 255 |
(lambda () |
| 256 |
(let ((x '())) |
| 257 |
(dynamic-wind |
| 258 |
(lambda () (push! x 'a)) |
| 259 |
(lambda () |
| 260 |
(with-error-handler |
| 261 |
(lambda (e) (push! x 'c)) |
| 262 |
(lambda () |
| 263 |
(dynamic-wind |
| 264 |
(lambda () (push! x 'b) (car 3) (push! x 'z)) |
| 265 |
(lambda () (push! x 'y)) |
| 266 |
(lambda () (push! x 'x)))))) |
| 267 |
(lambda () (push! x 'd))) |
| 268 |
(reverse x)))) |
| 269 |
|
| 270 |
(prim-test "error in after thunk (nested)" '(a b c d e f) |
| 271 |
(lambda () |
| 272 |
(let ((x '())) |
| 273 |
(dynamic-wind |
| 274 |
(lambda () (push! x 'a)) |
| 275 |
(lambda () |
| 276 |
(with-error-handler |
| 277 |
(lambda (e) (push! x 'e)) |
| 278 |
(lambda () |
| 279 |
(dynamic-wind |
| 280 |
(lambda () (push! x 'b)) |
| 281 |
(lambda () (push! x 'c)) |
| 282 |
(lambda () (push! x 'd) (car 3) (push! x 'z)))))) |
| 283 |
(lambda () (push! x 'f))) |
| 284 |
(reverse x)))) |
| 285 |
|
| 286 |
(prim-test "error in before thunk (cascaded)" '(a b c d e) |
| 287 |
(lambda () |
| 288 |
(let ((x '())) |
| 289 |
(with-error-handler |
| 290 |
(lambda (e) (push! x 'd)) |
| 291 |
(lambda () |
| 292 |
(dynamic-wind |
| 293 |
(lambda () (push! x 'a)) |
| 294 |
(lambda () |
| 295 |
(with-error-handler |
| 296 |
(lambda (e) (push! x 'c) (raise e)) |
| 297 |
(lambda () |
| 298 |
(dynamic-wind |
| 299 |
(lambda () (push! x 'b) (car 3) (push! x 'z)) |
| 300 |
(lambda () (push! x 'y)) |
| 301 |
(lambda () (push! x 'x)))))) |
| 302 |
(lambda () (push! x 'e))))) |
| 303 |
(reverse x)))) |
| 304 |
|
| 305 |
(prim-test "error in after thunk (cascaded)" '(a b c d e f g) |
| 306 |
(lambda () |
| 307 |
(let ((x '())) |
| 308 |
(with-error-handler |
| 309 |
(lambda (e) (push! x 'f)) |
| 310 |
(lambda () |
| 311 |
(dynamic-wind |
| 312 |
(lambda () (push! x 'a)) |
| 313 |
(lambda () |
| 314 |
(with-error-handler |
| 315 |
(lambda (e) (push! x 'e) (raise e)) |
| 316 |
(lambda () |
| 317 |
(dynamic-wind |
| 318 |
(lambda () (push! x 'b)) |
| 319 |
(lambda () (push! x 'c)) |
| 320 |
(lambda () (push! x 'd) (car 3) (push! x 'z)))))) |
| 321 |
(lambda () (push! x 'g))))) |
| 322 |
(reverse x)))) |
| 323 |
|
| 324 |
;;---------------------------------------------------------------- |
| 325 |
(test-section "restart and error handler") |
| 326 |
|
| 327 |
(prim-test "restart" '(a b x b x) |
| 328 |
(lambda () |
| 329 |
(let ((x '()) |
| 330 |
(c #f)) |
| 331 |
(with-error-handler |
| 332 |
(lambda (e) (push! x 'x)) |
| 333 |
(lambda () |
| 334 |
(push! x 'a) |
| 335 |
(set! c (call/cc identity)) |
| 336 |
(push! x 'b) |
| 337 |
(car 3) |
| 338 |
(push! x 'z))) |
| 339 |
(when c (c #f)) |
| 340 |
(reverse x)))) |
| 341 |
|
| 342 |
(prim-test "restart & dynamic-wind" '(a b c x e f z a b x e f z) |
| 343 |
(lambda () |
| 344 |
(let ((x '()) |
| 345 |
(c #f)) |
| 346 |
(dynamic-wind |
| 347 |
(lambda () (push! x 'a)) |
| 348 |
(lambda () |
| 349 |
(with-error-handler |
| 350 |
(lambda (e) (push! x 'x)) |
| 351 |
(lambda () |
| 352 |
(dynamic-wind |
| 353 |
(lambda () (push! x 'b)) |
| 354 |
(lambda () |
| 355 |
(push! x 'c) |
| 356 |
(set! c (call/cc (lambda (k) k))) |
| 357 |
(car 3) |
| 358 |
(push! x 'd)) |
| 359 |
(lambda () (push! x 'e)))))) |
| 360 |
(lambda () (push! x 'f))) |
| 361 |
(push! x 'z) |
| 362 |
(when c (c #f)) |
| 363 |
(reverse x)))) |
| 364 |
|
| 365 |
;;---------------------------------------------------------------- |
| 366 |
(test-section "with-exception-handler") |
| 367 |
|
| 368 |
(prim-test "simple" '(a b c) |
| 369 |
(lambda () |
| 370 |
(let ((x '())) |
| 371 |
(with-exception-handler |
| 372 |
(lambda (e) (push! x e)) |
| 373 |
(lambda () |
| 374 |
(push! x 'a) |
| 375 |
(raise 'b) |
| 376 |
(push! x 'c))) |
| 377 |
(reverse x)))) |
| 378 |
|
| 379 |
(prim-test "w/dynamic-wind" '(a b c d e f g) |
| 380 |
(lambda () |
| 381 |
(let ((x '())) |
| 382 |
(dynamic-wind |
| 383 |
(lambda () (push! x 'a)) |
| 384 |
(lambda () |
| 385 |
(with-exception-handler |
| 386 |
(lambda (e) (push! x e)) |
| 387 |
(lambda () |
| 388 |
(dynamic-wind |
| 389 |
(lambda () (push! x 'b)) |
| 390 |
(lambda () (push! x 'c) (raise 'd) (push! x 'e)) |
| 391 |
(lambda () (push! x 'f)))))) |
| 392 |
(lambda () (push! x 'g))) |
| 393 |
(reverse x)))) |
| 394 |
|
| 395 |
(prim-test "manual restart (simple)" '(a b c) |
| 396 |
(lambda () |
| 397 |
(let ((x '())) |
| 398 |
(push! x |
| 399 |
(call/cc |
| 400 |
(lambda (cont) |
| 401 |
(with-exception-handler |
| 402 |
(lambda (e) |
| 403 |
(push! x 'b) |
| 404 |
(cont 'c)) |
| 405 |
(lambda () (push! x 'a) (car 3)))))) |
| 406 |
(reverse x)))) |
| 407 |
|
| 408 |
(prim-test "manual restart (w/ dynamic-wind)" '(a b c e d) |
| 409 |
(lambda () |
| 410 |
(let ((x '())) |
| 411 |
(push! x |
| 412 |
(call/cc |
| 413 |
(lambda (cont) |
| 414 |
(dynamic-wind |
| 415 |
(lambda () (push! x 'a)) |
| 416 |
(lambda () |
| 417 |
(with-exception-handler |
| 418 |
(lambda (e) |
| 419 |
(push! x 'c) |
| 420 |
(cont 'd)) |
| 421 |
(lambda () (push! x 'b) (car 3)))) |
| 422 |
(lambda () (push! x 'e)))))) |
| 423 |
(reverse x)))) |
| 424 |
|
| 425 |
(prim-test "noncontinuable error" '(a b c g y) |
| 426 |
(lambda () |
| 427 |
(let ((x '())) |
| 428 |
(with-error-handler |
| 429 |
(lambda (e) (push! x 'g)) |
| 430 |
(lambda () |
| 431 |
(with-exception-handler |
| 432 |
(lambda (e) (push! x 'c)) |
| 433 |
(lambda () |
| 434 |
(dynamic-wind |
| 435 |
(lambda () (push! x 'a)) |
| 436 |
(lambda () (push! x 'b) (car 3) (push! x 'z)) |
| 437 |
(lambda () (push! x 'y))))))) |
| 438 |
(reverse x)))) |
| 439 |
|
| 440 |
;;---------------------------------------------------------------- |
| 441 |
(test-section "nesting exception/error handlers") |
| 442 |
|
| 443 |
;(prim-test "propagating continuable exception 1" '(a b c) |
| 444 |
(prim-test "nesting exception/error handlers 1" '(a z) |
| 445 |
(lambda () |
| 446 |
(let ((x '())) |
| 447 |
(with-exception-handler |
| 448 |
(lambda (e) (push! x e)) |
| 449 |
(lambda () |
| 450 |
(with-error-handler |
| 451 |
(lambda (e) (push! x 'z)) |
| 452 |
(lambda () |
| 453 |
(push! x 'a) |
| 454 |
(raise 'b) |
| 455 |
(push! x 'c))))) |
| 456 |
(reverse x)))) |
| 457 |
|
| 458 |
;(prim-test "propagating continuable exception 2" '(a b c d e f g h) |
| 459 |
(prim-test "nesting exception/error handlers 2" '(a b c f g h) |
| 460 |
(lambda () |
| 461 |
(let ((x '())) |
| 462 |
(with-exception-handler |
| 463 |
(lambda (e) (push! x e)) |
| 464 |
(lambda () |
| 465 |
(dynamic-wind |
| 466 |
(lambda () (push! x 'a)) |
| 467 |
(lambda () |
| 468 |
(with-error-handler |
| 469 |
(lambda (e) (push! x 'z)) |
| 470 |
(lambda () |
| 471 |
(dynamic-wind |
| 472 |
(lambda () (push! x 'b)) |
| 473 |
(lambda () |
| 474 |
(with-error-handler |
| 475 |
(lambda (e) (push! x 'f)) |
| 476 |
(lambda () |
| 477 |
(push! x 'c) |
| 478 |
(raise 'd) |
| 479 |
(push! x 'e) |
| 480 |
(car 3) |
| 481 |
(push! x 'z)))) |
| 482 |
(lambda () (push! x 'g)))))) |
| 483 |
(lambda () (push! x 'h))))) |
| 484 |
(reverse x)))) |
| 485 |
|
| 486 |
;;---------------------------------------------------------------- |
| 487 |
(test-section "guard reraise") |
| 488 |
|
| 489 |
(prim-test "guard reraise 1" |
| 490 |
"[d01][d02][d04][d01][w01]1[d03][d04]" |
| 491 |
(lambda () |
| 492 |
(with-output-to-string |
| 493 |
(lambda() |
| 494 |
(with-exception-handler |
| 495 |
(lambda (e) (display "[w01]") (display e)) |
| 496 |
(lambda () |
| 497 |
(guard (exc) |
| 498 |
(dynamic-wind |
| 499 |
(lambda () (display "[d01]")) |
| 500 |
(lambda () |
| 501 |
(display "[d02]") |
| 502 |
(raise 1) |
| 503 |
(display "[d03]")) |
| 504 |
(lambda () (display "[d04]")))))))))) |
| 505 |
|
| 506 |
(prim-test "guard reraise 2 (reraise x 2)" |
| 507 |
"[d01][d02][d05][d01][w01]1[d03][d05][d01][w01]2[d04][d05]" |
| 508 |
(lambda () |
| 509 |
(with-output-to-string |
| 510 |
(lambda() |
| 511 |
(with-exception-handler |
| 512 |
(lambda (e) (display "[w01]") (display e)) |
| 513 |
(lambda () |
| 514 |
(guard (exc) |
| 515 |
(dynamic-wind |
| 516 |
(lambda () (display "[d01]")) |
| 517 |
(lambda () |
| 518 |
(display "[d02]") |
| 519 |
(raise 1) |
| 520 |
(display "[d03]") |
| 521 |
(raise 2) |
| 522 |
(display "[d04]")) |
| 523 |
(lambda () (display "[d05]")))))))))) |
| 524 |
|
| 525 |
(prim-test "guard reraise 3 (dynamic-wind x 2)" |
| 526 |
"[d01][d02][d11][d12][d14][d04][d01][d11][w01]1[d13][d14][d03][d04]" |
| 527 |
(lambda () |
| 528 |
(with-output-to-string |
| 529 |
(lambda() |
| 530 |
(with-exception-handler |
| 531 |
(lambda (e) (display "[w01]") (display e)) |
| 532 |
(lambda () |
| 533 |
(guard (exc) |
| 534 |
(dynamic-wind |
| 535 |
(lambda () (display "[d01]")) |
| 536 |
(lambda () |
| 537 |
(display "[d02]") |
| 538 |
(dynamic-wind |
| 539 |
(lambda () (display "[d11]")) |
| 540 |
(lambda () |
| 541 |
(display "[d12]") |
| 542 |
(raise 1) |
| 543 |
(display "[d13]")) |
| 544 |
(lambda () (display "[d14]"))) |
| 545 |
(display "[d03]")) |
| 546 |
(lambda () (display "[d04]")))))))))) |
| 547 |
|
| 548 |
(prim-test "guard reraise 4 (guard x 2)" |
| 549 |
"[d01][d02][d11][d12][d14][d11][d14][d04][d01][d11][w01]1[d13][d14][d03][d04]" |
| 550 |
(lambda () |
| 551 |
(with-output-to-string |
| 552 |
(lambda() |
| 553 |
(with-exception-handler |
| 554 |
(lambda (e) (display "[w01]") (display e)) |
| 555 |
(lambda () |
| 556 |
(guard (exc) |
| 557 |
(dynamic-wind |
| 558 |
(lambda () (display "[d01]")) |
| 559 |
(lambda () |
| 560 |
(display "[d02]") |
| 561 |
(guard (exc) |
| 562 |
(dynamic-wind |
| 563 |
(lambda () (display "[d11]")) |
| 564 |
(lambda () |
| 565 |
(display "[d12]") |
| 566 |
(raise 1) |
| 567 |
(display "[d13]")) |
| 568 |
(lambda () (display "[d14]")))) |
| 569 |
(display "[d03]")) |
| 570 |
(lambda () (display "[d04]")))))))))) |
| 571 |
|
| 572 |
;;---------------------------------------------------------------- |
| 573 |
(test-section "interaction with empty environment frame") |
| 574 |
|
| 575 |
(prim-test "empty do" 'ok |
| 576 |
(lambda () |
| 577 |
(let ((x 0)) |
| 578 |
(do () ((> x 0) 'ok) |
| 579 |
(with-error-handler |
| 580 |
(lambda (e) (inc! x)) |
| 581 |
(lambda () (car x))))))) |
| 582 |
|
| 583 |
|
| 584 |
(prim-test "empty let" 'ok |
| 585 |
(lambda () |
| 586 |
(let ((x 0)) |
| 587 |
(let loop () |
| 588 |
(with-error-handler |
| 589 |
(lambda (e) (inc! x) (loop)) |
| 590 |
(lambda () |
| 591 |
(if (> x 2) |
| 592 |
'ok |
| 593 |
(car x)))))))) |
| 594 |
|
| 595 |
;;---------------------------------------------------------------- |
| 596 |
(test-section "error and errorf procedures") |
| 597 |
|
| 598 |
(prim-test "error (<error>)" "Message 1 \"2\" (:a . #\\4)" |
| 599 |
(lambda () |
| 600 |
(with-error-handler |
| 601 |
(lambda (e) |
| 602 |
(and (is-a? e <error>) (slot-ref e 'message))) |
| 603 |
(lambda () |
| 604 |
(error "Message" 1 "2" (cons :a #\4)))))) |
| 605 |
|
| 606 |
(prim-test "errorf (<error>)" "Message 1 and 2 or 3 and 4" |
| 607 |
(lambda () |
| 608 |
(with-error-handler |
| 609 |
(lambda (e) |
| 610 |
(and (is-a? e <error>) (slot-ref e 'message))) |
| 611 |
(lambda () |
| 612 |
(errorf "Message ~a and ~a or ~a and ~a" 1 2 3 4))))) |
| 613 |
|
| 614 |
(prim-test "error (<system-error>)" '("Wow: \"bang!\" 4" 111) |
| 615 |
(lambda () |
| 616 |
(with-error-handler |
| 617 |
(lambda (e) |
| 618 |
(and (is-a? e <system-error>) |
| 619 |
(list (slot-ref e 'message) |
| 620 |
(slot-ref e 'errno)))) |
| 621 |
(lambda () |
| 622 |
(error <system-error> :errno 111 "Wow:" "bang!" 4))))) |
| 623 |
|
| 624 |
(prim-test "errorf (<system-error>)" '("Wow: \"bang!\" 4" 111) |
| 625 |
(lambda () |
| 626 |
(with-error-handler |
| 627 |
(lambda (e) |
| 628 |
(and (is-a? e <system-error>) |
| 629 |
(list (slot-ref e 'message) |
| 630 |
(slot-ref e 'errno)))) |
| 631 |
(lambda () |
| 632 |
(errorf <system-error> :errno 111 |
| 633 |
"Wow: ~s ~s" "bang!" 4))))) |
| 634 |
|
| 635 |
(prim-test "error (base case)" #t |
| 636 |
(lambda () |
| 637 |
(with-error-handler |
| 638 |
(lambda (e) |
| 639 |
(and (is-a? e <error>) |
| 640 |
(eq? (slot-ref e 'message) #f))) |
| 641 |
(lambda () |
| 642 |
(error <error>))))) |
| 643 |
|
| 644 |
(prim-test "errorf (base case)" #t |
| 645 |
(lambda () |
| 646 |
(with-error-handler |
| 647 |
(lambda (e) |
| 648 |
(and (is-a? e <error>) |
| 649 |
(eq? (slot-ref e 'message) #f))) |
| 650 |
(lambda () |
| 651 |
(errorf <error>))))) |
| 652 |
|
| 653 |
(prim-test "error (explicit message)" "msg" |
| 654 |
(lambda () |
| 655 |
(with-error-handler |
| 656 |
(lambda (e) |
| 657 |
(and (is-a? e <error>) (slot-ref e 'message))) |
| 658 |
(lambda () |
| 659 |
(error <error> :message "msg"))))) |
| 660 |
|
| 661 |
(prim-test "errorf (explicit message)" "msg~s" |
| 662 |
(lambda () |
| 663 |
(with-error-handler |
| 664 |
(lambda (e) |
| 665 |
(and (is-a? e <error>) (slot-ref e 'message))) |
| 666 |
(lambda () |
| 667 |
(errorf <error> :message "msg~s"))))) |
| 668 |
|
| 669 |
;;---------------------------------------------------------------- |
| 670 |
(test-section "stack overflow inside handlers") |
| 671 |
|
| 672 |
(define (stack-buster k) |
| 673 |
(if (zero? k) 1 (+ (stack-buster (- k 1)) 1))) |
| 674 |
|
| 675 |
(prim-test "stack overflow in error handler" '(4 . ok) |
| 676 |
(lambda () |
| 677 |
(cons |
| 678 |
4 |
| 679 |
(with-error-handler |
| 680 |
(lambda (e) (stack-buster 100000) 'ok) |
| 681 |
(lambda () (error "foo")))))) |
| 682 |
|
| 683 |
(prim-test "stack overflow in error handler (nested)" '(4 . ok) |
| 684 |
(lambda () |
| 685 |
(cons |
| 686 |
4 |
| 687 |
(with-error-handler |
| 688 |
(lambda (e) |
| 689 |
(with-error-handler |
| 690 |
(lambda (e) 'ok) |
| 691 |
(lambda () (stack-buster 100000) (error "pop")))) |
| 692 |
(lambda () (error "foo")))))) |
| 693 |
|
| 694 |
(prim-test "stack overflow in error handler (cascading error)" '(4 . ok) |
| 695 |
(lambda () |
| 696 |
(cons |
| 697 |
4 |
| 698 |
(with-error-handler |
| 699 |
(lambda (e) 'ok) |
| 700 |
(lambda () |
| 701 |
(with-error-handler |
| 702 |
(lambda (e) |
| 703 |
(with-error-handler |
| 704 |
(lambda (e) (error "bar")) |
| 705 |
(lambda () (stack-buster 100000) (error "pop")))) |
| 706 |
(lambda () (error "foo")))))))) |
| 707 |
|
| 708 |
(test-end) |