continuations.test 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. (define-module (test-suite test-continuations)
  20. :use-module (test-suite lib))
  21. (define (block-reentry body)
  22. (let ((active #f))
  23. (dynamic-wind
  24. (lambda ()
  25. (if active
  26. (throw 'no-reentry)))
  27. (lambda ()
  28. (set! active #t)
  29. (body))
  30. (lambda () #f))))
  31. (define (catch-tag body)
  32. (catch #t
  33. body
  34. (lambda (tag . args) tag)))
  35. (define (check-cont)
  36. (catch-tag
  37. (lambda ()
  38. (block-reentry (lambda () (call/cc identity))))))
  39. (define (dont-crash-please)
  40. (let ((k (check-cont)))
  41. (if (procedure? k)
  42. (k 12)
  43. k)))
  44. (with-test-prefix "continuations"
  45. (pass-if "throwing to a rewound catch context"
  46. (eq? (dont-crash-please) 'no-reentry))
  47. (pass-if "can print a continuation"
  48. (let ((s (with-output-to-string
  49. (lambda ()
  50. (call-with-current-continuation write)))))
  51. (string=? "#<continuation " (substring s 0 15))))
  52. (pass-if "blocked attempt to cross a continuation barrier"
  53. (call-with-current-continuation
  54. (lambda (k)
  55. (with-continuation-barrier
  56. (lambda ()
  57. (catch 'misc-error
  58. (lambda ()
  59. (k 1)
  60. #f)
  61. (lambda _
  62. #t)))))))
  63. (pass-if "uncaught exception is handled by continuation barrier"
  64. (let* ((handled #f)
  65. (s (with-error-to-string
  66. (lambda ()
  67. (set! handled
  68. (not (with-continuation-barrier
  69. (lambda ()
  70. (error "Catch me if you can!")))))))))
  71. handled))
  72. (pass-if "exit unwinds dynwinds inside a continuation barrier"
  73. (let ((s (with-error-to-string
  74. (lambda ()
  75. (with-continuation-barrier
  76. (lambda ()
  77. (dynamic-wind
  78. (lambda () #f)
  79. (lambda () (exit 1))
  80. (lambda () (throw 'abcde)))))))))
  81. (and (string-contains s "abcde") #t)))
  82. (with-debugging-evaluator
  83. (pass-if "make a stack from a continuation"
  84. (stack? (call-with-current-continuation make-stack)))
  85. (pass-if "get a continuation's stack ID"
  86. (let ((id (call-with-current-continuation stack-id)))
  87. (or (boolean? id) (symbol? id)))))
  88. )