1 ;;;; guardians.test --- test suite for Guile Guardians -*- scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- July 1999
4 ;;;; Copyright (C) 1999, 2001, 2006 Free Software Foundation, Inc.
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.
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.
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
20 ;;; These tests make some questionable assumptions.
21 ;;; - They assume that a GC will find all dead objects, so they
22 ;;; will become flaky if we have a generational GC.
23 ;;; - They assume that objects won't be saved by the guardian until
24 ;;; they explicitly invoke GC --- in other words, they assume that GC
25 ;;; won't happen too often.
27 (define-module (test-guardians)
28 :use-module (test-suite lib)
29 :use-module (ice-9 documentation)
30 :use-module (ice-9 weak-vector))
37 (define (documented? object)
38 (not (not (object-documentation object))))
43 ;;; Who guards the guardian?
45 (define g2 (make-guardian))
46 (g2 (list 'g2-garbage))
47 (define g3 (make-guardian))
48 (g3 (list 'g3-garbage))
50 (pass-if "g2-garbage not collected yet" (equal? (g2) #f))
51 (pass-if "g3-garbage not collected yet" (equal? (g3) #f))
54 (let ((seen-g3-garbage #f)
56 (seen-something-else #f))
62 ((equal? saved '(g3-garbage)) (set! seen-g3-garbage #t))
63 ((procedure? saved) (set! seen-g2 saved))
64 (else (pk saved) (set! seen-something-else #t)))
66 (pass-if "g3-garbage saved" (or seen-g3-garbage (throw 'unresolved)))
67 (pass-if "g2-saved" (or (procedure? seen-g2) (throw 'unresolved)))
68 (pass-if "nothing else saved" (not seen-something-else))
69 (pass-if "g2-garbage saved" (or (and (procedure? seen-g2)
70 (equal? (seen-g2) '(g2-garbage)))
71 (throw 'unresolved))))
73 (with-test-prefix "standard guardian functionality"
75 (with-test-prefix "make-guardian"
77 (pass-if "documented?"
78 (documented? make-guardian))
80 (pass-if "returns procedure"
81 (procedure? (make-guardian)))
83 (pass-if "returns new procedure each time"
84 (not (equal? (make-guardian) (make-guardian)))))
86 (with-test-prefix "empty guardian"
89 (eq? ((make-guardian)) #f))
91 (pass-if "returns always #f"
92 (let ((g (make-guardian)))
94 (begin (gc) (eq? (g) #f))
95 (begin (gc) (eq? (g) #f))))))
97 (with-test-prefix "guarding independent objects"
99 (pass-if "guarding immediate"
100 (let ((g (make-guardian)))
103 (begin (gc) (eq? (g) #f))
104 (begin (gc) (eq? (g) #f)))))
106 (pass-if "guarding non-immediate"
107 (let ((g (make-guardian)))
110 (if (not (eq? (g) #f))
114 (if (not (equal? (g) (cons #f #f)))
118 (pass-if "guarding two non-immediates"
119 (let ((g (make-guardian)))
123 (if (not (eq? (g) #f))
127 (let ((l (list (g) (g))))
128 (if (not (or (equal? l (list (cons #f #f) (cons #t #t)))
129 (equal? l (list (cons #t #t) (cons #f #f)))))
133 (pass-if "re-guarding non-immediates"
134 (let ((g (make-guardian)))
137 (if (not (eq? (g) #f))
142 (if (not (equal? p (cons #f #f)))
148 (if (not (equal? (g) (cons #f #f)))
152 (pass-if "guarding living non-immediate"
153 (let ((g (make-guardian))
156 (if (not (eq? (g) #f))
160 (not (eq? (g) p)))))))
162 (with-test-prefix "guarding weakly referenced objects"
164 (pass-if "guarded weak vector element gets returned from guardian"
165 (let ((g (make-guardian))
166 (v (weak-vector #f)))
168 (let ((p (cons #f #f)))
171 (if (not (eq? (g) #f))
175 (if (not (equal? (g) (cons #f #f)))
179 (pass-if "guarded element of weak vector gets eventually removed from weak vector"
180 (let ((g (make-guardian))
181 (v (weak-vector #f)))
183 (let ((p (cons #f #f)))
188 (if (not (equal? (g) (cons #f #f)))
192 (or (not (vector-ref v 0))
193 (throw 'unresolved))))))))
195 (with-test-prefix "guarding weak containers"
197 (pass-if "element of guarded weak vector gets collected"
198 (let ((g (make-guardian))
199 (v (weak-vector (cons #f #f))))
202 (if (equal? (vector-ref v 0) (cons #f #f))
206 (with-test-prefix "guarding guardians"
209 (with-test-prefix "guarding dependent objects"
211 ;; We don't make any guarantees about the order objects are
212 ;; returned from guardians and therefore we skip the following
216 (pass-if "guarding vector and element"
217 (let ((g (make-guardian)))
219 (let ((p (cons #f #f)))
222 (if (not (eq? (g) #f))
226 (if (not (equal? (g) (vector (cons #f #f))))
228 (if (not (eq? (g) #f))
232 (if (not (equal? (g) (cons #f #f)))
234 (eq? (g) #f)))))))))))
236 (with-test-prefix "guarding objects more than once"
238 (pass-if "guarding twice in one guardian"
239 (let ((g (make-guardian)))
241 (let ((p (cons #f #f)))
244 (if (not (eq? (g) #f))
248 (or (and (and=> (g) (lambda (o) (equal? o (cons #f #f))))
249 (and=> (g) (lambda (o) (equal? o (cons #f #f)))))
250 (throw 'unresolved))))))
252 (pass-if "guarding twice in two guardians"
253 (let ((g (make-guardian))
256 (let ((p (cons #f #f)))
259 (if (not (eq? (g) #f))
263 (or (and (and=> (g) (lambda (o) (equal? o (cons #f #f))))
264 (and=> (h) (lambda (o) (equal? o (cons #f #f)))))
265 (throw 'unresolved)))))))
267 (with-test-prefix "guarding cyclic dependencies"