1 ;;;; guardians.test --- test suite for Guile Guardians -*- scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- July 1999
4 ;;;; Copyright (C) 1999, 2001 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., 59 Temple Place, Suite 330,
19 ;;;; Boston, MA 02111-1307 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 (use-modules (ice-9 documentation))
35 (define (documented? object)
36 (not (not (object-documentation object))))
41 ;;; Who guards the guardian?
43 (define g2 (make-guardian))
44 (g2 (list 'g2-garbage))
45 (define g3 (make-guardian))
46 (g3 (list 'g3-garbage))
48 (pass-if "g2-garbage not collected yet" (equal? (g2) #f))
49 (pass-if "g3-garbage not collected yet" (equal? (g3) #f))
52 (let ((seen-g3-garbage #f)
54 (seen-something-else #f))
60 ((equal? saved '(g3-garbage)) (set! seen-g3-garbage #t))
61 ((procedure? saved) (set! seen-g2 saved))
62 (else (set! seen-something-else #t)))
64 (pass-if "g3-garbage saved" seen-g3-garbage)
65 (pass-if "g2-saved" (procedure? seen-g2))
66 (pass-if "nothing else saved" (not seen-something-else))
67 (pass-if "g2-garbage saved" (and (procedure? seen-g2)
68 (equal? (seen-g2) '(g2-garbage)))))
70 (with-test-prefix "standard guardian functionality"
72 (with-test-prefix "make-guardian"
74 (pass-if "documented?"
75 (documented? make-guardian))
77 (pass-if "returns procedure"
78 (procedure? (make-guardian)))
80 (pass-if "returns new procedure each time"
81 (not (equal? (make-guardian) (make-guardian)))))
83 (with-test-prefix "empty guardian"
86 (eq? ((make-guardian)) #f))
88 (pass-if "returns always #f"
89 (let ((g (make-guardian)))
91 (begin (gc) (eq? (g) #f))
92 (begin (gc) (eq? (g) #f))))))
94 (with-test-prefix "guarding independent objects"
96 (pass-if "guarding immediate"
97 (let ((g (make-guardian)))
100 (begin (gc) (eq? (g) #f))
101 (begin (gc) (eq? (g) #f)))))
103 (pass-if "guarding non-immediate"
104 (let ((g (make-guardian)))
107 (if (not (eq? (g) #f))
111 (if (not (equal? (g) (cons #f #f)))
115 (pass-if "guarding two non-immediates"
116 (let ((g (make-guardian)))
120 (if (not (eq? (g) #f))
124 (let ((l (list (g) (g))))
125 (if (not (or (equal? l (list (cons #f #f) (cons #t #t)))
126 (equal? l (list (cons #t #t) (cons #f #f)))))
130 (pass-if "re-guarding non-immediates"
131 (let ((g (make-guardian)))
134 (if (not (eq? (g) #f))
139 (if (not (equal? p (cons #f #f)))
145 (if (not (equal? (g) (cons #f #f)))
149 (pass-if "guarding living non-immediate"
150 (let ((g (make-guardian))
153 (if (not (eq? (g) #f))
157 (not (eq? (g) p)))))))
159 (with-test-prefix "guarding weakly referenced objects"
161 (pass-if "guarded weak vector element gets returned from guardian"
162 (let ((g (make-guardian))
163 (v (weak-vector #f)))
165 (let ((p (cons #f #f)))
168 (if (not (eq? (g) #f))
172 (if (not (equal? (g) (cons #f #f)))
176 (pass-if "guarded element of weak vector gets removed from weak vector"
177 ;; How should this be handled? Should weak objects be removed from
178 ;; their containers before they become zombies? Let's take a look at
179 ;; the possible scenarios: a) Weak objects that are also guarded are
180 ;; not removed from their containers as long as they are guarded.
181 ;; However, they still can become zombies. The consequence is, that the
182 ;; object can be retrieved from its container, thus being alive, while
183 ;; on the other hand it can at the same time be retrieved from a
184 ;; guardian. This is unfortunate, since when retrieving an object from
185 ;; a guardian one would not expect any other reference to the object.
186 ;; b) Weak objects are removed from their containers if they are not
187 ;; referenced any more or if the only references are from guardians.
188 ;; That means that it is guaranteed that there are no other references
189 ;; to an object that is retrieved from a guardian. However, this means
190 ;; that there is no chance to update containers like weak hash tables
191 ;; using the information that one of their contained objects will be
192 ;; removed. It may be however, that this is not necessary anyway.
193 (let ((g (make-guardian))
194 (v (weak-vector #f)))
196 (let ((p (cons #f #f)))
199 (if (not (equal? (vector-ref v 0) (cons #f #f)))
203 (if (equal? (vector-ref v 0) (cons #f #f))
207 (with-test-prefix "guarding weak containers"
209 (pass-if "element of guarded weak vector gets collected"
210 (let ((g (make-guardian))
211 (v (weak-vector (cons #f #f))))
214 (if (equal? (vector-ref v 0) (cons #f #f))
218 (with-test-prefix "guarding guardians"
221 (with-test-prefix "guile guardian functionality"
223 (with-test-prefix "guarding dependent objects"
225 (pass-if "guarding vector and element"
226 (let ((g (make-guardian)))
228 (let ((p (cons #f #f)))
231 (if (not (eq? (g) #f))
235 (if (not (equal? (g) (vector (cons #f #f))))
237 (if (not (eq? (g) #f))
241 (if (not (equal? (g) (cons #f #f)))
247 (with-test-prefix "guarding objects more than once"
250 (with-test-prefix "guarding cyclic dependencies"