fluids.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;;; -*- scheme -*-
  2. ;;;; fluids.test --- test suite for fluid values
  3. ;;;;
  4. ;;;; Copyright (C) 2010 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-fluids)
  20. :use-module (test-suite lib))
  21. (define exception:syntax-error
  22. (cons 'syntax-error "failed to match"))
  23. (define exception:duplicate-binding
  24. (cons 'syntax-error "duplicate"))
  25. (define a (make-fluid))
  26. (define b (make-fluid))
  27. (define c #f)
  28. (with-test-prefix "syntax"
  29. (pass-if-exception "with-fluids missing expression"
  30. exception:syntax-error
  31. (eval '(with-fluids ((a 1)))
  32. (interaction-environment)))
  33. (pass-if-exception "with-fluids bad bindings"
  34. exception:syntax-error
  35. (eval '(with-fluids (a) #f)
  36. (interaction-environment)))
  37. (pass-if-exception "with-fluids bad bindings"
  38. exception:syntax-error
  39. (eval '(with-fluids ((a)) #f)
  40. (interaction-environment))))
  41. (with-test-prefix "initial fluid values"
  42. (pass-if "fluid-ref uninitialized fluid is #f"
  43. (not (fluid-ref a)))
  44. (pass-if "initial value is inherited from parent thread"
  45. (if (provided? 'threads)
  46. (let ((f (make-fluid)))
  47. (fluid-set! f 'initial)
  48. (let ((child (call-with-new-thread
  49. (lambda ()
  50. (let ((init (fluid-ref f)))
  51. (fluid-set! f 'new)
  52. (list init (fluid-ref f)))))))
  53. (equal? '(initial new) (join-thread child))))
  54. (throw 'unresolved))))
  55. (with-test-prefix "with-fluids with non-fluid"
  56. (pass-if-exception "exception raised if nonfluid passed to with-fluids"
  57. exception:wrong-type-arg
  58. (with-fluids ((c #t))
  59. c))
  60. (pass-if "fluids not modified if nonfluid passed to with-fluids"
  61. (catch 'wrong-type-arg
  62. (lambda ()
  63. (with-fluids ((a #t)
  64. (c #t))
  65. #f))
  66. (lambda _
  67. (not (fluid-ref a))))))
  68. (with-test-prefix "with-fluids with duplicate fluid"
  69. (pass-if "last value wins"
  70. (with-fluids ((a 1)
  71. (a 2))
  72. (eqv? (fluid-ref a) 2)))
  73. (pass-if "original value restored"
  74. (and (with-fluids ((a 1)
  75. (a 2))
  76. (eqv? (fluid-ref a) 2))
  77. (eqv? (fluid-ref a) #f))))
  78. (pass-if "fluid values are thread-local"
  79. (if (provided? 'threads)
  80. (let ((f (make-fluid)))
  81. (fluid-set! f 'parent)
  82. (let ((child (call-with-new-thread
  83. (lambda ()
  84. (fluid-set! f 'child)
  85. (fluid-ref f)))))
  86. (and (eq? (join-thread child) 'child)
  87. (eq? (fluid-ref f) 'parent))))
  88. (throw 'unresolved)))
  89. (pass-if "fluids are GC'd"
  90. (let ((g (make-guardian)))
  91. (g (make-fluid))
  92. (let loop ((i 1000))
  93. (and (> i 0)
  94. (begin
  95. (make-fluid)
  96. (loop (1- i)))))
  97. (gc)
  98. (fluid? (g))))
  99. (with-test-prefix "with-fluids"
  100. (pass-if "with-fluids binds"
  101. (= (with-fluids ((a 1)) (fluid-ref a)) 1))
  102. (pass-if "with-fluids unbinds"
  103. (begin
  104. (fluid-set! a 0)
  105. (with-fluids ((a 1)) (fluid-ref a))
  106. (= (fluid-ref a) 0)))
  107. (pass-if "with-fluids and dynamic-wind"
  108. (letrec ((co-routine #f)
  109. (spawn (lambda (proc)
  110. (set! co-routine proc)))
  111. (yield (lambda (val)
  112. (call-with-current-continuation
  113. (lambda (k)
  114. (let ((next co-routine))
  115. (set! co-routine k)
  116. (next val)))))))
  117. (spawn (lambda (val)
  118. (with-fluids ((a 'inside))
  119. (yield (fluid-ref a))
  120. (yield (fluid-ref a)))))
  121. (fluid-set! a 'outside)
  122. (let ((inside-a (yield #f)))
  123. (let ((outside-a (fluid-ref a)))
  124. (let ((inside-a2 (yield #f)))
  125. (and (eq? inside-a 'inside)
  126. (eq? outside-a 'outside)
  127. (eq? inside-a2 'inside))))))))
  128. (with-test-prefix "unbound fluids"
  129. (pass-if "fluid-ref of unbound fluid"
  130. (catch #t
  131. (lambda () (fluid-ref (make-unbound-fluid)))
  132. (lambda (key . args) #t)))
  133. (pass-if "fluid-bound? of bound fluid"
  134. (fluid-bound? (make-fluid)))
  135. (pass-if "fluid-bound? of unbound fluid"
  136. (not (fluid-bound? (make-unbound-fluid))))
  137. (pass-if "unbound fluids can be set"
  138. (let ((fluid (make-unbound-fluid)))
  139. (fluid-set! fluid #t)
  140. (fluid-ref fluid)))
  141. (pass-if "bound fluids can be unset"
  142. (let ((fluid (make-fluid)))
  143. (fluid-unset! fluid)
  144. (catch #t
  145. (lambda () (fluid-ref fluid))
  146. (lambda (key . args) #t)))))