| 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 |
|
| 20 |
(define-module (test-suite test-fluids) |
| 21 |
#:use-module (ice-9 threads) |
| 22 |
#:use-module (test-suite lib) |
| 23 |
#:use-module (system base compile)) |
| 24 |
|
| 25 |
|
| 26 |
(define exception:syntax-error |
| 27 |
(cons 'syntax-error "failed to match")) |
| 28 |
(define exception:duplicate-binding |
| 29 |
(cons 'syntax-error "duplicate")) |
| 30 |
|
| 31 |
(define a (make-fluid)) |
| 32 |
(define b (make-fluid)) |
| 33 |
(define c #f) |
| 34 |
|
| 35 |
(with-test-prefix "syntax" |
| 36 |
(pass-if-exception "with-fluids missing expression" |
| 37 |
exception:syntax-error |
| 38 |
(eval '(with-fluids ((a 1))) |
| 39 |
(interaction-environment))) |
| 40 |
|
| 41 |
(pass-if-exception "with-fluids bad bindings" |
| 42 |
exception:syntax-error |
| 43 |
(eval '(with-fluids (a) #f) |
| 44 |
(interaction-environment))) |
| 45 |
|
| 46 |
(pass-if-exception "with-fluids bad bindings" |
| 47 |
exception:syntax-error |
| 48 |
(eval '(with-fluids ((a)) #f) |
| 49 |
(interaction-environment)))) |
| 50 |
|
| 51 |
(with-test-prefix "initial fluid values" |
| 52 |
|
| 53 |
(pass-if "fluid-ref returns #f for uninitialized fluid" |
| 54 |
(eq? #f (fluid-ref (make-fluid)))) |
| 55 |
|
| 56 |
(pass-if "fluid-ref returns #f for uninitialized thread local fluid" |
| 57 |
(eq? #f (fluid-ref (make-thread-local-fluid)))) |
| 58 |
|
| 59 |
(pass-if "fluid-ref returns default" |
| 60 |
(eq? #t (fluid-ref (make-fluid #t)))) |
| 61 |
|
| 62 |
(pass-if "fluid-ref returns thread local default" |
| 63 |
(eq? #t (fluid-ref (make-thread-local-fluid #t)))) |
| 64 |
|
| 65 |
(pass-if "initial value is inherited from parent thread" |
| 66 |
(if (provided? 'threads) |
| 67 |
(let ((f (make-fluid))) |
| 68 |
(fluid-set! f 'initial) |
| 69 |
(let ((child (call-with-new-thread |
| 70 |
(lambda () |
| 71 |
(let ((init (fluid-ref f))) |
| 72 |
(fluid-set! f 'new) |
| 73 |
(list init (fluid-ref f))))))) |
| 74 |
(equal? '(initial new) (join-thread child)))) |
| 75 |
(throw 'unresolved)))) |
| 76 |
|
| 77 |
(with-test-prefix "with-fluids with non-fluid" |
| 78 |
(pass-if-exception "exception raised if nonfluid passed to with-fluids" |
| 79 |
exception:wrong-type-arg |
| 80 |
(with-fluids ((c #t)) |
| 81 |
c)) |
| 82 |
|
| 83 |
(pass-if "fluids not modified if nonfluid passed to with-fluids" |
| 84 |
(catch 'wrong-type-arg |
| 85 |
(lambda () |
| 86 |
(with-fluids ((a #t) |
| 87 |
(c #t)) |
| 88 |
#f)) |
| 89 |
(lambda _ |
| 90 |
(not (fluid-ref a)))))) |
| 91 |
|
| 92 |
(with-test-prefix "with-fluids with duplicate fluid" |
| 93 |
;; These tests must be compiled, because the evaluator |
| 94 |
;; effectively transforms (with-fluids ((a 1) (b 2)) ...) |
| 95 |
;; into (with-fluids ((a 1)) (with-fluids ((b 2)) ...)) |
| 96 |
|
| 97 |
(pass-if "last value wins" |
| 98 |
(compile '(with-fluids ((a 1) |
| 99 |
(a 2) |
| 100 |
(a 3)) |
| 101 |
(eqv? (fluid-ref a) 3)) |
| 102 |
#:env (current-module))) |
| 103 |
|
| 104 |
(pass-if "remove the duplicate, not the last binding" |
| 105 |
(compile '(with-fluids ((a 1) |
| 106 |
(a 2) |
| 107 |
(a 3) |
| 108 |
(b 4)) |
| 109 |
(eqv? (fluid-ref b) 4)) |
| 110 |
#:env (current-module))) |
| 111 |
|
| 112 |
(pass-if "original value restored" |
| 113 |
(compile '(and (with-fluids ((a 1) |
| 114 |
(a 2)) |
| 115 |
(eqv? (fluid-ref a) 2)) |
| 116 |
(eqv? (fluid-ref a) #f)) |
| 117 |
#:env (current-module)))) |
| 118 |
|
| 119 |
(pass-if "fluid values are thread-local" |
| 120 |
(if (provided? 'threads) |
| 121 |
(let ((f (make-fluid))) |
| 122 |
(fluid-set! f 'parent) |
| 123 |
(let ((child (call-with-new-thread |
| 124 |
(lambda () |
| 125 |
(fluid-set! f 'child) |
| 126 |
(fluid-ref f))))) |
| 127 |
(and (eq? (join-thread child) 'child) |
| 128 |
(eq? (fluid-ref f) 'parent)))) |
| 129 |
(throw 'unresolved))) |
| 130 |
|
| 131 |
(pass-if "fluids are GC'd" |
| 132 |
(let ((g (make-guardian))) |
| 133 |
(g (make-fluid)) |
| 134 |
(let loop ((i 0)) |
| 135 |
;; nudge the gc, though this didn't appear to be needed |
| 136 |
(iota 10000) |
| 137 |
(cond |
| 138 |
((= i 100) #f) |
| 139 |
((fluid? (g)) #t) |
| 140 |
(else |
| 141 |
;; Note that a usleep (of any duration) here didn't work on at |
| 142 |
;; least s390x. Making usleep work even more identically to |
| 143 |
;; sleep by changing usleep to convert the interval to/from C as |
| 144 |
;; a uint rather than a long "fixed" the problem. |
| 145 |
(sleep 0) |
| 146 |
(gc) |
| 147 |
(loop (1+ i))))))) |
| 148 |
|
| 149 |
(pass-if-exception "fluid-ref* depth must be non-negative" |
| 150 |
exception:out-of-range |
| 151 |
(fluid-ref* (make-fluid) -1)) |
| 152 |
|
| 153 |
(with-test-prefix "with-fluids" |
| 154 |
|
| 155 |
(pass-if "with-fluids binds" |
| 156 |
(= (with-fluids ((a 1)) (fluid-ref a)) 1)) |
| 157 |
|
| 158 |
(pass-if "with-fluids unbinds" |
| 159 |
(begin |
| 160 |
(fluid-set! a 0) |
| 161 |
(with-fluids ((a 1)) (fluid-ref a)) |
| 162 |
(= (fluid-ref a) 0))) |
| 163 |
|
| 164 |
(pass-if "with-fluids and dynamic-wind" |
| 165 |
(letrec ((co-routine #f) |
| 166 |
(spawn (lambda (proc) |
| 167 |
(set! co-routine proc))) |
| 168 |
(yield (lambda (val) |
| 169 |
(call-with-current-continuation |
| 170 |
(lambda (k) |
| 171 |
(let ((next co-routine)) |
| 172 |
(set! co-routine k) |
| 173 |
(next val))))))) |
| 174 |
|
| 175 |
(spawn (lambda (val) |
| 176 |
(with-fluids ((a 'inside)) |
| 177 |
(yield (fluid-ref a)) |
| 178 |
(yield (fluid-ref a))))) |
| 179 |
|
| 180 |
(fluid-set! a 'outside) |
| 181 |
(let ((inside-a (yield #f))) |
| 182 |
(let ((outside-a (fluid-ref a))) |
| 183 |
(let ((inside-a2 (yield #f))) |
| 184 |
(and (eq? inside-a 'inside) |
| 185 |
(eq? outside-a 'outside) |
| 186 |
(eq? inside-a2 'inside)))))))) |
| 187 |
|
| 188 |
(with-test-prefix "unbound fluids" |
| 189 |
(pass-if "fluid-ref of unbound fluid" |
| 190 |
(catch #t |
| 191 |
(lambda () (fluid-ref (make-unbound-fluid))) |
| 192 |
(lambda (key . args) #t))) |
| 193 |
(pass-if "fluid-bound? of bound fluid" |
| 194 |
(fluid-bound? (make-fluid))) |
| 195 |
(pass-if "fluid-bound? of unbound fluid" |
| 196 |
(not (fluid-bound? (make-unbound-fluid)))) |
| 197 |
(pass-if "unbound fluids can be set" |
| 198 |
(let ((fluid (make-unbound-fluid))) |
| 199 |
(fluid-set! fluid #t) |
| 200 |
(fluid-ref fluid))) |
| 201 |
(pass-if "bound fluids can be unset" |
| 202 |
(let ((fluid (make-fluid))) |
| 203 |
(fluid-unset! fluid) |
| 204 |
(catch #t |
| 205 |
(lambda () (fluid-ref fluid)) |
| 206 |
(lambda (key . args) #t))))) |
| 207 |
|
| 208 |
(with-test-prefix "dynamic states" |
| 209 |
(pass-if "basics" |
| 210 |
(dynamic-state? (current-dynamic-state))) |
| 211 |
|
| 212 |
(pass-if "with a fluid (basic)" |
| 213 |
(let ((fluid (make-fluid #f)) |
| 214 |
(state (current-dynamic-state))) |
| 215 |
(with-dynamic-state |
| 216 |
state |
| 217 |
(lambda () |
| 218 |
(eqv? (fluid-ref fluid) #f))))) |
| 219 |
|
| 220 |
(pass-if "with a fluid (set outer)" |
| 221 |
(let ((fluid (make-fluid #f)) |
| 222 |
(state (current-dynamic-state))) |
| 223 |
(fluid-set! fluid #t) |
| 224 |
(and (with-dynamic-state |
| 225 |
state |
| 226 |
(lambda () |
| 227 |
(eqv? (fluid-ref fluid) #f))) |
| 228 |
(eqv? (fluid-ref fluid) #t)))) |
| 229 |
|
| 230 |
(pass-if "with a fluid (set inner)" |
| 231 |
(let ((fluid (make-fluid #f)) |
| 232 |
(state (current-dynamic-state))) |
| 233 |
(and (with-dynamic-state |
| 234 |
state |
| 235 |
(lambda () |
| 236 |
(fluid-set! fluid #t) |
| 237 |
(eqv? (fluid-ref fluid) #t))) |
| 238 |
(eqv? (fluid-ref fluid) #f)))) |
| 239 |
|
| 240 |
(pass-if "dynstate captured (1)" |
| 241 |
(let ((fluid (make-fluid #f)) |
| 242 |
(state (current-dynamic-state)) |
| 243 |
(tag (make-prompt-tag "hey"))) |
| 244 |
(let ((k (call-with-prompt tag |
| 245 |
(lambda () |
| 246 |
(with-dynamic-state |
| 247 |
state |
| 248 |
(lambda () |
| 249 |
(abort-to-prompt tag) |
| 250 |
(fluid-ref fluid)))) |
| 251 |
(lambda (k) k)))) |
| 252 |
(eqv? (k) #f)))) |
| 253 |
|
| 254 |
(pass-if "dynstate captured (2)" |
| 255 |
(let ((fluid (make-fluid #f)) |
| 256 |
(state (current-dynamic-state)) |
| 257 |
(tag (make-prompt-tag "hey"))) |
| 258 |
(let ((k (call-with-prompt tag |
| 259 |
(lambda () |
| 260 |
(with-dynamic-state |
| 261 |
state |
| 262 |
(lambda () |
| 263 |
(abort-to-prompt tag) |
| 264 |
(fluid-ref fluid)))) |
| 265 |
(lambda (k) k)))) |
| 266 |
(fluid-set! fluid #t) |
| 267 |
(eqv? (k) #f)))) |
| 268 |
|
| 269 |
(pass-if "dynstate captured (3)" |
| 270 |
(let ((fluid (make-fluid #f)) |
| 271 |
(state (current-dynamic-state)) |
| 272 |
(tag (make-prompt-tag "hey"))) |
| 273 |
(let ((k (call-with-prompt tag |
| 274 |
(lambda () |
| 275 |
(with-dynamic-state |
| 276 |
state |
| 277 |
(lambda () |
| 278 |
(fluid-set! fluid #t) |
| 279 |
(abort-to-prompt tag) |
| 280 |
(fluid-ref fluid)))) |
| 281 |
(lambda (k) k)))) |
| 282 |
(and (eqv? (fluid-ref fluid) #f) |
| 283 |
(eqv? (k) #t))))) |
| 284 |
|
| 285 |
(pass-if "exception handler not captured" |
| 286 |
(let ((state (catch #t (lambda () (current-dynamic-state)) error))) |
| 287 |
(catch #t |
| 288 |
(lambda () (with-dynamic-state state (lambda () (/ 1 0)))) |
| 289 |
(lambda _ #t))))) |