fluids.test 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 (ice-9 threads)
  21. #:use-module (test-suite lib)
  22. #:use-module (system base compile))
  23. (define exception:syntax-error
  24. (cons 'syntax-error "failed to match"))
  25. (define exception:duplicate-binding
  26. (cons 'syntax-error "duplicate"))
  27. (define a (make-fluid))
  28. (define b (make-fluid))
  29. (define c #f)
  30. (with-test-prefix "syntax"
  31. (pass-if-exception "with-fluids missing expression"
  32. exception:syntax-error
  33. (eval '(with-fluids ((a 1)))
  34. (interaction-environment)))
  35. (pass-if-exception "with-fluids bad bindings"
  36. exception:syntax-error
  37. (eval '(with-fluids (a) #f)
  38. (interaction-environment)))
  39. (pass-if-exception "with-fluids bad bindings"
  40. exception:syntax-error
  41. (eval '(with-fluids ((a)) #f)
  42. (interaction-environment))))
  43. (with-test-prefix "initial fluid values"
  44. (pass-if "fluid-ref uninitialized fluid is #f"
  45. (not (fluid-ref a)))
  46. (pass-if "initial value is inherited from parent thread"
  47. (if (provided? 'threads)
  48. (let ((f (make-fluid)))
  49. (fluid-set! f 'initial)
  50. (let ((child (call-with-new-thread
  51. (lambda ()
  52. (let ((init (fluid-ref f)))
  53. (fluid-set! f 'new)
  54. (list init (fluid-ref f)))))))
  55. (equal? '(initial new) (join-thread child))))
  56. (throw 'unresolved))))
  57. (with-test-prefix "with-fluids with non-fluid"
  58. (pass-if-exception "exception raised if nonfluid passed to with-fluids"
  59. exception:wrong-type-arg
  60. (with-fluids ((c #t))
  61. c))
  62. (pass-if "fluids not modified if nonfluid passed to with-fluids"
  63. (catch 'wrong-type-arg
  64. (lambda ()
  65. (with-fluids ((a #t)
  66. (c #t))
  67. #f))
  68. (lambda _
  69. (not (fluid-ref a))))))
  70. (with-test-prefix "with-fluids with duplicate fluid"
  71. ;; These tests must be compiled, because the evaluator
  72. ;; effectively transforms (with-fluids ((a 1) (b 2)) ...)
  73. ;; into (with-fluids ((a 1)) (with-fluids ((b 2)) ...))
  74. (pass-if "last value wins"
  75. (compile '(with-fluids ((a 1)
  76. (a 2)
  77. (a 3))
  78. (eqv? (fluid-ref a) 3))
  79. #:env (current-module)))
  80. (pass-if "remove the duplicate, not the last binding"
  81. (compile '(with-fluids ((a 1)
  82. (a 2)
  83. (a 3)
  84. (b 4))
  85. (eqv? (fluid-ref b) 4))
  86. #:env (current-module)))
  87. (pass-if "original value restored"
  88. (compile '(and (with-fluids ((a 1)
  89. (a 2))
  90. (eqv? (fluid-ref a) 2))
  91. (eqv? (fluid-ref a) #f))
  92. #:env (current-module))))
  93. (pass-if "fluid values are thread-local"
  94. (if (provided? 'threads)
  95. (let ((f (make-fluid)))
  96. (fluid-set! f 'parent)
  97. (let ((child (call-with-new-thread
  98. (lambda ()
  99. (fluid-set! f 'child)
  100. (fluid-ref f)))))
  101. (and (eq? (join-thread child) 'child)
  102. (eq? (fluid-ref f) 'parent))))
  103. (throw 'unresolved)))
  104. (pass-if "fluids are GC'd"
  105. (let ((g (make-guardian)))
  106. (g (make-fluid))
  107. (let loop ((i 1000))
  108. (and (> i 0)
  109. (begin
  110. (make-fluid)
  111. (loop (1- i)))))
  112. (gc)
  113. (fluid? (g))))
  114. (with-test-prefix "with-fluids"
  115. (pass-if "with-fluids binds"
  116. (= (with-fluids ((a 1)) (fluid-ref a)) 1))
  117. (pass-if "with-fluids unbinds"
  118. (begin
  119. (fluid-set! a 0)
  120. (with-fluids ((a 1)) (fluid-ref a))
  121. (= (fluid-ref a) 0)))
  122. (pass-if "with-fluids and dynamic-wind"
  123. (letrec ((co-routine #f)
  124. (spawn (lambda (proc)
  125. (set! co-routine proc)))
  126. (yield (lambda (val)
  127. (call-with-current-continuation
  128. (lambda (k)
  129. (let ((next co-routine))
  130. (set! co-routine k)
  131. (next val)))))))
  132. (spawn (lambda (val)
  133. (with-fluids ((a 'inside))
  134. (yield (fluid-ref a))
  135. (yield (fluid-ref a)))))
  136. (fluid-set! a 'outside)
  137. (let ((inside-a (yield #f)))
  138. (let ((outside-a (fluid-ref a)))
  139. (let ((inside-a2 (yield #f)))
  140. (and (eq? inside-a 'inside)
  141. (eq? outside-a 'outside)
  142. (eq? inside-a2 'inside))))))))
  143. (with-test-prefix "unbound fluids"
  144. (pass-if "fluid-ref of unbound fluid"
  145. (catch #t
  146. (lambda () (fluid-ref (make-unbound-fluid)))
  147. (lambda (key . args) #t)))
  148. (pass-if "fluid-bound? of bound fluid"
  149. (fluid-bound? (make-fluid)))
  150. (pass-if "fluid-bound? of unbound fluid"
  151. (not (fluid-bound? (make-unbound-fluid))))
  152. (pass-if "unbound fluids can be set"
  153. (let ((fluid (make-unbound-fluid)))
  154. (fluid-set! fluid #t)
  155. (fluid-ref fluid)))
  156. (pass-if "bound fluids can be unset"
  157. (let ((fluid (make-fluid)))
  158. (fluid-unset! fluid)
  159. (catch #t
  160. (lambda () (fluid-ref fluid))
  161. (lambda (key . args) #t)))))
  162. (with-test-prefix "dynamic states"
  163. (pass-if "basics"
  164. (dynamic-state? (current-dynamic-state)))
  165. (pass-if "with a fluid (basic)"
  166. (let ((fluid (make-fluid #f))
  167. (state (current-dynamic-state)))
  168. (with-dynamic-state
  169. state
  170. (lambda ()
  171. (eqv? (fluid-ref fluid) #f)))))
  172. (pass-if "with a fluid (set outer)"
  173. (let ((fluid (make-fluid #f))
  174. (state (current-dynamic-state)))
  175. (fluid-set! fluid #t)
  176. (and (with-dynamic-state
  177. state
  178. (lambda ()
  179. (eqv? (fluid-ref fluid) #f)))
  180. (eqv? (fluid-ref fluid) #t))))
  181. (pass-if "with a fluid (set inner)"
  182. (let ((fluid (make-fluid #f))
  183. (state (current-dynamic-state)))
  184. (and (with-dynamic-state
  185. state
  186. (lambda ()
  187. (fluid-set! fluid #t)
  188. (eqv? (fluid-ref fluid) #t)))
  189. (eqv? (fluid-ref fluid) #f))))
  190. (pass-if "dynstate captured (1)"
  191. (let ((fluid (make-fluid #f))
  192. (state (current-dynamic-state))
  193. (tag (make-prompt-tag "hey")))
  194. (let ((k (call-with-prompt tag
  195. (lambda ()
  196. (with-dynamic-state
  197. state
  198. (lambda ()
  199. (abort-to-prompt tag)
  200. (fluid-ref fluid))))
  201. (lambda (k) k))))
  202. (eqv? (k) #f))))
  203. (pass-if "dynstate captured (2)"
  204. (let ((fluid (make-fluid #f))
  205. (state (current-dynamic-state))
  206. (tag (make-prompt-tag "hey")))
  207. (let ((k (call-with-prompt tag
  208. (lambda ()
  209. (with-dynamic-state
  210. state
  211. (lambda ()
  212. (abort-to-prompt tag)
  213. (fluid-ref fluid))))
  214. (lambda (k) k))))
  215. (fluid-set! fluid #t)
  216. (eqv? (k) #f))))
  217. (pass-if "dynstate captured (3)"
  218. (let ((fluid (make-fluid #f))
  219. (state (current-dynamic-state))
  220. (tag (make-prompt-tag "hey")))
  221. (let ((k (call-with-prompt tag
  222. (lambda ()
  223. (with-dynamic-state
  224. state
  225. (lambda ()
  226. (fluid-set! fluid #t)
  227. (abort-to-prompt tag)
  228. (fluid-ref fluid))))
  229. (lambda (k) k))))
  230. (and (eqv? (fluid-ref fluid) #f)
  231. (eqv? (k) #t)))))
  232. (pass-if "exception handler not captured"
  233. (let ((state (catch #t (lambda () (current-dynamic-state)) error)))
  234. (catch #t
  235. (lambda () (with-dynamic-state state (lambda () (/ 1 0))))
  236. (lambda _ #t)))))