fluids.test 8.8 KB

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