1 ;;;; environments.test -*- scheme -*-
2 ;;;; Copyright (C) 2000 Free Software Foundation, Inc.
4 ;;;; This program is free software; you can redistribute it and/or modify
5 ;;;; it under the terms of the GNU General Public License as published by
6 ;;;; the Free Software Foundation; either version 2, or (at your option)
7 ;;;; any later version.
9 ;;;; This program is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;;; GNU General Public License for more details.
14 ;;;; You should have received a copy of the GNU General Public License
15 ;;;; along with this software; see the file COPYING. If not, write to
16 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 ;;;; Boston, MA 02111-1307 USA
19 ;;;; As a special exception, the Free Software Foundation gives permission
20 ;;;; for additional uses of the text contained in its release of GUILE.
22 ;;;; The exception is that, if you link the GUILE library with other files
23 ;;;; to produce an executable, this does not by itself cause the
24 ;;;; resulting executable to be covered by the GNU General Public License.
25 ;;;; Your use of that executable is in no way restricted on account of
26 ;;;; linking the GUILE library code into it.
28 ;;;; This exception does not however invalidate any other reasons why
29 ;;;; the executable file might be covered by the GNU General Public License.
31 ;;;; This exception applies only to the code released by the
32 ;;;; Free Software Foundation under the name GUILE. If you copy
33 ;;;; code from other Free Software Foundation releases into a copy of
34 ;;;; GUILE, as the General Public License permits, the exception does
35 ;;;; not apply to the code that you add in this way. To avoid misleading
36 ;;;; anyone as to the status of such modified files, you must delete
37 ;;;; this exception notice from them.
39 ;;;; If you write modifications of your own for GUILE, it is your choice
40 ;;;; whether to permit this exception to apply to your modifications.
41 ;;;; If you do not wish that, delete this exception notice.
43 (use-modules (ice-9 documentation))
51 (define (documented? object)
52 (object-documentation object))
57 (if (not (null? increment))
58 (set! counter (+ counter (car increment))))
61 (define (folder sym val res)
62 (cons (cons sym val) res))
69 (with-test-prefix "leaf-environments"
71 (with-test-prefix "leaf-environment?"
73 (pass-if "documented?"
74 (documented? leaf-environment?))
76 (pass-if "non-environment-object"
77 (not (leaf-environment? #f))))
80 (with-test-prefix "make-leaf-environment"
82 (pass-if "documented?"
83 (documented? make-leaf-environment))
85 (pass-if "produces an environment"
86 (environment? (make-leaf-environment)))
88 (pass-if "produces a leaf-environment"
89 (leaf-environment? (make-leaf-environment)))
91 (pass-if "produces always a new environment"
92 (not (eq? (make-leaf-environment) (make-leaf-environment)))))
95 (with-test-prefix "bound, define, ref, set!, cell"
97 (let* ((env (make-leaf-environment))
100 (pass-if "unbound by default"
101 (and (not (environment-bound? env 'a))
102 (not (environment-bound? env 'b))
103 (not (environment-bound? env 'c))))
105 (pass-if "bound after define"
106 (environment-define env 'a (ctr 1))
107 (environment-bound? env 'a))
109 (pass-if "ref defined"
111 (environment-define env 'a (ctr 1))
112 (eq? (environment-ref env 'a) (ctr)))
114 (environment-define env 'a (ctr 1))
115 (eq? (environment-ref env 'a) (ctr)))))
117 (pass-if "set! defined"
119 (environment-set! env 'a (ctr 1))
120 (eq? (environment-ref env 'a) (ctr)))
122 (environment-set! env 'a (ctr 1))
123 (eq? (environment-ref env 'a) (ctr)))))
125 (pass-if "read-only cell"
126 (let* ((cell (environment-cell env 'a #f)))
128 (environment-set! env 'a (ctr 1))
129 (eq? (cdr cell) (ctr))))))
131 (pass-if "read-only cell rebound after define"
132 (let* ((cell (environment-cell env 'a #f)))
133 (environment-define env 'a (ctr 1))
134 (not (eq? (environment-cell env 'a #f) cell))))
136 (pass-if "writable cell"
137 (let* ((readable (environment-cell env 'a #f))
138 (writable (environment-cell env 'a #t)))
139 (and (eq? readable writable)
141 (environment-set! env 'a (ctr 1))
142 (eq? (cdr writable) (ctr)))
144 (set-cdr! writable (ctr 1))
145 (eq? (environment-ref env 'a) (ctr)))
147 (set-cdr! (environment-cell env 'a #t) (ctr 1))
148 (eq? (cdr writable) (ctr))))))
150 (pass-if "writable cell rebound after define"
151 (let* ((cell (environment-cell env 'a #t)))
152 (environment-define env 'a (ctr 1))
153 (not (eq? (environment-cell env 'a #t) cell))))
155 (pass-if "referencing undefined"
158 (environment-ref env 'b)
163 (pass-if "set!ing undefined"
166 (environment-set! env 'b)
171 (pass-if "readable cell from undefined"
174 (environment-cell env 'b #f)
179 (pass-if "writable cell from undefined"
182 (environment-cell env 'b #t)
188 (with-test-prefix "undefine"
190 (let* ((env (make-leaf-environment)))
192 (pass-if "undefine defined"
193 (environment-define env 'a 1)
194 (and (environment-bound? env 'a)
196 (environment-undefine env 'a)
197 (not (environment-bound? env 'a)))))
199 (pass-if "undefine undefined"
200 (and (not (environment-bound? env 'a))
202 (environment-undefine env 'a)
203 (not (environment-bound? env 'a)))))))
206 (with-test-prefix "fold"
208 (let* ((env (make-leaf-environment))
211 (pass-if "fold empty"
212 (eq? 'success (environment-fold env folder 'success)))
214 (pass-if "after define"
215 (environment-define env 'a (ctr 1))
216 (equal? `((a . ,(ctr))) (environment-fold env folder '())))
218 (pass-if "after undefine"
219 (environment-undefine env 'a)
220 (eq? 'success (environment-fold env folder 'success)))
222 (pass-if "after two defines"
225 (environment-define env 'a i)
226 (environment-define env 'b j)
227 (let ((folded (environment-fold env folder '())))
228 (or (equal? folded `((a . ,i) (b . ,j)))
229 (equal? folded `((b . ,j) (a . ,i)))))))
231 (pass-if "after set!"
232 (let* ((i (environment-ref env 'a)))
233 (environment-set! env 'b i)
234 (let ((folded (environment-fold env folder '())))
235 (or (equal? folded `((a . ,i) (b . ,i)))
236 (equal? folded `((b . ,i) (a . ,i)))))))))
239 (with-test-prefix "observe"
241 (let* ((env (make-leaf-environment))
243 (func (lambda (env) (set! tag (not tag))))
246 (pass-if "observe unobserved"
247 (set! observer (environment-observe env func))
250 (pass-if "define undefined"
252 (environment-define env 'a 1)
255 (pass-if "define defined"
257 (environment-define env 'a 1)
260 (pass-if "set! defined"
262 (environment-set! env 'a 1)
265 (pass-if "undefine defined"
267 (environment-undefine env 'a)
270 (pass-if "undefine undefined"
272 (environment-undefine env 'a)
275 (pass-if "unobserve observed"
277 (environment-unobserve observer)
278 (environment-define env 'a 1)
281 (pass-if "unobserve unobserved"
282 (environment-unobserve observer)
286 (with-test-prefix "observe-weak"
288 (let* ((env (make-leaf-environment))
290 (func (lambda (env) (set! tag (not tag))))
293 (pass-if "weak-observe unobserved"
294 (set! observer (environment-observe-weak env func))
297 (pass-if "define undefined"
299 (environment-define env 'a 1)
302 (pass-if "define defined"
304 (environment-define env 'a 1)
307 (pass-if "set! defined"
309 (environment-set! env 'a 1)
312 (pass-if "undefine defined"
314 (environment-undefine env 'a)
317 (pass-if "undefine undefined"
319 (environment-undefine env 'a)
322 (pass-if "unobserve observed"
324 (environment-unobserve observer)
325 (environment-define env 'a 1)
328 (pass-if "unobserve unobserved"
329 (environment-unobserve observer)
332 (pass-if "weak observer gets collected"
334 (environment-observe-weak env func)
336 (environment-define env 'a 1)
340 (environment-define env 'a 1)
344 (with-test-prefix "observer-errors"
346 (let* ((env (make-leaf-environment))
349 (func-1 (lambda (env)
350 (set! tag-1 (not tag-1))
352 (func-2 (lambda (env)
353 (set! tag-2 (not tag-2))
356 (pass-if "update continues after error"
357 (environment-observe env func-1)
358 (environment-observe env func-2)
361 (environment-define env 'a 1)
364 (and tag-1 tag-2)))))))