Commit | Line | Data |
---|---|---|
2e109b65 JB |
1 | ;;;; guardians.test --- test suite for Guile Guardians -*- scheme -*- |
2 | ;;;; Jim Blandy <jimb@red-bean.com> --- July 1999 | |
3 | ;;;; | |
6e7d5622 | 4 | ;;;; Copyright (C) 1999, 2001, 2006 Free Software Foundation, Inc. |
2e109b65 | 5 | ;;;; |
53befeb7 NJ |
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. | |
2e109b65 | 10 | ;;;; |
53befeb7 | 11 | ;;;; This library is distributed in the hope that it will be useful, |
2e109b65 | 12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
53befeb7 NJ |
13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | ;;;; Lesser General Public License for more details. | |
2e109b65 | 15 | ;;;; |
53befeb7 NJ |
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 | |
2e109b65 JB |
19 | |
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. | |
26 | ||
7acc33d7 LC |
27 | (define-module (test-guardians) |
28 | :use-module (test-suite lib) | |
29 | :use-module (ice-9 documentation) | |
30 | :use-module (ice-9 weak-vector)) | |
9247b5bc | 31 | |
7acc33d7 | 32 | \f |
9247b5bc DH |
33 | ;;; |
34 | ;;; miscellaneous | |
35 | ;;; | |
36 | ||
37 | (define (documented? object) | |
38 | (not (not (object-documentation object)))) | |
39 | ||
2e109b65 | 40 | |
2e109b65 | 41 | (gc) |
2e109b65 JB |
42 | |
43 | ;;; Who guards the guardian? | |
44 | (gc) | |
45 | (define g2 (make-guardian)) | |
46 | (g2 (list 'g2-garbage)) | |
47 | (define g3 (make-guardian)) | |
48 | (g3 (list 'g3-garbage)) | |
49 | (g3 g2) | |
50 | (pass-if "g2-garbage not collected yet" (equal? (g2) #f)) | |
51 | (pass-if "g3-garbage not collected yet" (equal? (g3) #f)) | |
52 | (set! g2 #f) | |
53 | (gc) | |
54 | (let ((seen-g3-garbage #f) | |
55 | (seen-g2 #f) | |
56 | (seen-something-else #f)) | |
57 | (let loop () | |
58 | (let ((saved (g3))) | |
59 | (if saved | |
60 | (begin | |
61 | (cond | |
62 | ((equal? saved '(g3-garbage)) (set! seen-g3-garbage #t)) | |
63 | ((procedure? saved) (set! seen-g2 saved)) | |
2924541b | 64 | (else (pk saved) (set! seen-something-else #t))) |
2e109b65 | 65 | (loop))))) |
2924541b MV |
66 | (pass-if "g3-garbage saved" (or seen-g3-garbage (throw 'unresolved))) |
67 | (pass-if "g2-saved" (or (procedure? seen-g2) (throw 'unresolved))) | |
2e109b65 | 68 | (pass-if "nothing else saved" (not seen-something-else)) |
2924541b MV |
69 | (pass-if "g2-garbage saved" (or (and (procedure? seen-g2) |
70 | (equal? (seen-g2) '(g2-garbage))) | |
71 | (throw 'unresolved)))) | |
9247b5bc DH |
72 | |
73 | (with-test-prefix "standard guardian functionality" | |
74 | ||
75 | (with-test-prefix "make-guardian" | |
76 | ||
77 | (pass-if "documented?" | |
78 | (documented? make-guardian)) | |
79 | ||
80 | (pass-if "returns procedure" | |
81 | (procedure? (make-guardian))) | |
82 | ||
83 | (pass-if "returns new procedure each time" | |
84 | (not (equal? (make-guardian) (make-guardian))))) | |
85 | ||
86 | (with-test-prefix "empty guardian" | |
87 | ||
88 | (pass-if "returns #f" | |
89 | (eq? ((make-guardian)) #f)) | |
90 | ||
91 | (pass-if "returns always #f" | |
92 | (let ((g (make-guardian))) | |
93 | (and (eq? (g) #f) | |
94 | (begin (gc) (eq? (g) #f)) | |
95 | (begin (gc) (eq? (g) #f)))))) | |
96 | ||
97 | (with-test-prefix "guarding independent objects" | |
98 | ||
99 | (pass-if "guarding immediate" | |
100 | (let ((g (make-guardian))) | |
101 | (g #f) | |
102 | (and (eq? (g) #f) | |
103 | (begin (gc) (eq? (g) #f)) | |
104 | (begin (gc) (eq? (g) #f))))) | |
105 | ||
106 | (pass-if "guarding non-immediate" | |
107 | (let ((g (make-guardian))) | |
108 | (gc) | |
109 | (g (cons #f #f)) | |
110 | (if (not (eq? (g) #f)) | |
111 | (throw 'unresolved) | |
112 | (begin | |
113 | (gc) | |
114 | (if (not (equal? (g) (cons #f #f))) | |
115 | (throw 'unresolved) | |
116 | (eq? (g) #f)))))) | |
117 | ||
118 | (pass-if "guarding two non-immediates" | |
119 | (let ((g (make-guardian))) | |
120 | (gc) | |
121 | (g (cons #f #f)) | |
122 | (g (cons #t #t)) | |
123 | (if (not (eq? (g) #f)) | |
124 | (throw 'unresolved) | |
125 | (begin | |
126 | (gc) | |
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))))) | |
130 | (throw 'unresolved) | |
131 | (eq? (g) #f))))))) | |
132 | ||
133 | (pass-if "re-guarding non-immediates" | |
134 | (let ((g (make-guardian))) | |
135 | (gc) | |
136 | (g (cons #f #f)) | |
137 | (if (not (eq? (g) #f)) | |
138 | (throw 'unresolved) | |
139 | (begin | |
140 | (gc) | |
141 | (let ((p (g))) | |
142 | (if (not (equal? p (cons #f #f))) | |
143 | (throw 'unresolved) | |
144 | (begin | |
145 | (g p) | |
146 | (set! p #f) | |
147 | (gc) | |
148 | (if (not (equal? (g) (cons #f #f))) | |
149 | (throw 'unresolved) | |
150 | (eq? (g) #f))))))))) | |
151 | ||
152 | (pass-if "guarding living non-immediate" | |
153 | (let ((g (make-guardian)) | |
154 | (p (cons #f #f))) | |
155 | (g p) | |
156 | (if (not (eq? (g) #f)) | |
157 | (throw 'fail) | |
158 | (begin | |
159 | (gc) | |
160 | (not (eq? (g) p))))))) | |
161 | ||
162 | (with-test-prefix "guarding weakly referenced objects" | |
163 | ||
164 | (pass-if "guarded weak vector element gets returned from guardian" | |
165 | (let ((g (make-guardian)) | |
166 | (v (weak-vector #f))) | |
167 | (gc) | |
168 | (let ((p (cons #f #f))) | |
169 | (g p) | |
170 | (vector-set! v 0 p)) | |
171 | (if (not (eq? (g) #f)) | |
172 | (throw 'unresolved) | |
173 | (begin | |
174 | (gc) | |
175 | (if (not (equal? (g) (cons #f #f))) | |
176 | (throw 'unresolved) | |
177 | (eq? (g) #f)))))) | |
178 | ||
2924541b | 179 | (pass-if "guarded element of weak vector gets eventually removed from weak vector" |
9247b5bc DH |
180 | (let ((g (make-guardian)) |
181 | (v (weak-vector #f))) | |
182 | (gc) | |
183 | (let ((p (cons #f #f))) | |
184 | (g p) | |
185 | (vector-set! v 0 p)) | |
2924541b MV |
186 | (begin |
187 | (gc) | |
188 | (if (not (equal? (g) (cons #f #f))) | |
189 | (throw 'unresolved) | |
190 | (begin | |
191 | (gc) | |
192 | (or (not (vector-ref v 0)) | |
193 | (throw 'unresolved)))))))) | |
9247b5bc DH |
194 | |
195 | (with-test-prefix "guarding weak containers" | |
196 | ||
197 | (pass-if "element of guarded weak vector gets collected" | |
198 | (let ((g (make-guardian)) | |
199 | (v (weak-vector (cons #f #f)))) | |
200 | (g v) | |
201 | (gc) | |
202 | (if (equal? (vector-ref v 0) (cons #f #f)) | |
203 | (throw 'unresolved) | |
204 | #t)))) | |
205 | ||
206 | (with-test-prefix "guarding guardians" | |
2924541b | 207 | #t) |
9247b5bc DH |
208 | |
209 | (with-test-prefix "guarding dependent objects" | |
210 | ||
2924541b MV |
211 | ;; We don't make any guarantees about the order objects are |
212 | ;; returned from guardians and therefore we skip the following | |
213 | ;; test. | |
214 | ||
215 | (if #f | |
216 | (pass-if "guarding vector and element" | |
217 | (let ((g (make-guardian))) | |
218 | (gc) | |
219 | (let ((p (cons #f #f))) | |
220 | (g p) | |
221 | (g (vector p))) | |
222 | (if (not (eq? (g) #f)) | |
223 | (throw 'unresolved) | |
224 | (begin | |
225 | (gc) | |
226 | (if (not (equal? (g) (vector (cons #f #f)))) | |
9247b5bc | 227 | (throw 'unresolved) |
2924541b MV |
228 | (if (not (eq? (g) #f)) |
229 | (throw 'unresolved) | |
230 | (begin | |
231 | (gc) | |
232 | (if (not (equal? (g) (cons #f #f))) | |
233 | (throw 'unresolved) | |
234 | (eq? (g) #f))))))))))) | |
9247b5bc DH |
235 | |
236 | (with-test-prefix "guarding objects more than once" | |
2924541b MV |
237 | |
238 | (pass-if "guarding twice in one guardian" | |
239 | (let ((g (make-guardian))) | |
240 | (gc) | |
241 | (let ((p (cons #f #f))) | |
242 | (g p) | |
243 | (g p)) | |
244 | (if (not (eq? (g) #f)) | |
245 | (throw 'unresolved) | |
246 | (begin | |
247 | (gc) | |
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)))))) | |
251 | ||
252 | (pass-if "guarding twice in two guardians" | |
253 | (let ((g (make-guardian)) | |
254 | (h (make-guardian))) | |
255 | (gc) | |
256 | (let ((p (cons #f #f))) | |
257 | (g p) | |
258 | (h p)) | |
259 | (if (not (eq? (g) #f)) | |
260 | (throw 'unresolved) | |
261 | (begin | |
262 | (gc) | |
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))))))) | |
9247b5bc DH |
266 | |
267 | (with-test-prefix "guarding cyclic dependencies" | |
268 | #t) | |
269 | ||
270 | ) |