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 program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 2, or (at your option)
9 ;;;; any later version.
11 ;;;; This program 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
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this software; see the file COPYING. If not, write to
18 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ;;;; Boston, MA 02110-1301 USA
21 ;;; These tests make some questionable assumptions.
22 ;;; - They assume that a GC will find all dead objects, so they
23 ;;; will become flaky if we have a generational GC.
24 ;;; - They assume that objects won't be saved by the guardian until
25 ;;; they explicitly invoke GC --- in other words, they assume that GC
26 ;;; won't happen too often.
28 (define-module (test-guardians)
29 :use-module (test-suite lib)
30 :use-module (ice-9 documentation)
31 :use-module (ice-9 weak-vector))
38 (define (documented? object)
39 (not (not (object-documentation object))))
44 ;;; Who guards the guardian?
46 (define g2 (make-guardian))
47 (g2 (list 'g2-garbage))
48 (define g3 (make-guardian))
49 (g3 (list 'g3-garbage))
51 (pass-if "g2-garbage not collected yet" (equal? (g2) #f))
52 (pass-if "g3-garbage not collected yet" (equal? (g3) #f))
55 (let ((seen-g3-garbage #f)
57 (seen-something-else #f))
63 ((equal? saved '(g3-garbage)) (set! seen-g3-garbage #t))
64 ((procedure? saved) (set! seen-g2 saved))
65 (else (pk saved) (set! seen-something-else #t)))
67 (pass-if "g3-garbage saved" (or seen-g3-garbage (throw 'unresolved)))
68 (pass-if "g2-saved" (or (procedure? seen-g2) (throw 'unresolved)))
69 (pass-if "nothing else saved" (not seen-something-else))
70 (pass-if "g2-garbage saved" (or (and (procedure? seen-g2)
71 (equal? (seen-g2) '(g2-garbage)))
72 (throw 'unresolved))))
74 (with-test-prefix "standard guardian functionality"
76 (with-test-prefix "make-guardian"
78 (pass-if "documented?"
79 (documented? make-guardian))
81 (pass-if "returns procedure"
82 (procedure? (make-guardian)))
84 (pass-if "returns new procedure each time"
85 (not (equal? (make-guardian) (make-guardian)))))
87 (with-test-prefix "empty guardian"
90 (eq? ((make-guardian)) #f))
92 (pass-if "returns always #f"
93 (let ((g (make-guardian)))
95 (begin (gc) (eq? (g) #f))
96 (begin (gc) (eq? (g) #f))))))
98 (with-test-prefix "guarding independent objects"
100 (pass-if "guarding immediate"
101 (let ((g (make-guardian)))
104 (begin (gc) (eq? (g) #f))
105 (begin (gc) (eq? (g) #f)))))
107 (pass-if "guarding non-immediate"
108 (let ((g (make-guardian)))
111 (if (not (eq? (g) #f))
115 (if (not (equal? (g) (cons #f #f)))
119 (pass-if "guarding two non-immediates"
120 (let ((g (make-guardian)))
124 (if (not (eq? (g) #f))
128 (let ((l (list (g) (g))))
129 (if (not (or (equal? l (list (cons #f #f) (cons #t #t)))
130 (equal? l (list (cons #t #t) (cons #f #f)))))
134 (pass-if "re-guarding non-immediates"
135 (let ((g (make-guardian)))
138 (if (not (eq? (g) #f))
143 (if (not (equal? p (cons #f #f)))
149 (if (not (equal? (g) (cons #f #f)))
153 (pass-if "guarding living non-immediate"
154 (let ((g (make-guardian))
157 (if (not (eq? (g) #f))
161 (not (eq? (g) p)))))))
163 (with-test-prefix "guarding weakly referenced objects"
165 (pass-if "guarded weak vector element gets returned from guardian"
166 (let ((g (make-guardian))
167 (v (weak-vector #f)))
169 (let ((p (cons #f #f)))
172 (if (not (eq? (g) #f))
176 (if (not (equal? (g) (cons #f #f)))
180 (pass-if "guarded element of weak vector gets eventually removed from weak vector"
181 (let ((g (make-guardian))
182 (v (weak-vector #f)))
184 (let ((p (cons #f #f)))
189 (if (not (equal? (g) (cons #f #f)))
193 (or (not (vector-ref v 0))
194 (throw 'unresolved))))))))
196 (with-test-prefix "guarding weak containers"
198 (pass-if "element of guarded weak vector gets collected"
199 (let ((g (make-guardian))
200 (v (weak-vector (cons #f #f))))
203 (if (equal? (vector-ref v 0) (cons #f #f))
207 (with-test-prefix "guarding guardians"
210 (with-test-prefix "guarding dependent objects"
212 ;; We don't make any guarantees about the order objects are
213 ;; returned from guardians and therefore we skip the following
217 (pass-if "guarding vector and element"
218 (let ((g (make-guardian)))
220 (let ((p (cons #f #f)))
223 (if (not (eq? (g) #f))
227 (if (not (equal? (g) (vector (cons #f #f))))
229 (if (not (eq? (g) #f))
233 (if (not (equal? (g) (cons #f #f)))
235 (eq? (g) #f)))))))))))
237 (with-test-prefix "guarding objects more than once"
239 (pass-if "guarding twice in one guardian"
240 (let ((g (make-guardian)))
242 (let ((p (cons #f #f)))
245 (if (not (eq? (g) #f))
249 (or (and (and=> (g) (lambda (o) (equal? o (cons #f #f))))
250 (and=> (g) (lambda (o) (equal? o (cons #f #f)))))
251 (throw 'unresolved))))))
253 (pass-if "guarding twice in two guardians"
254 (let ((g (make-guardian))
257 (let ((p (cons #f #f)))
260 (if (not (eq? (g) #f))
264 (or (and (and=> (g) (lambda (o) (equal? o (cons #f #f))))
265 (and=> (h) (lambda (o) (equal? o (cons #f #f)))))
266 (throw 'unresolved)))))))
268 (with-test-prefix "guarding cyclic dependencies"