Commit | Line | Data |
---|---|---|
f70d7468 | 1 | ;;;; weaks.test --- tests guile's weaks -*- scheme -*- |
5a99a574 | 2 | ;;;; Copyright (C) 1999, 2001, 2003, 2006, 2009, 2010 Free Software Foundation, Inc. |
f70d7468 | 3 | ;;;; |
73be1d9e MV |
4 | ;;;; This library is free software; you can redistribute it and/or |
5 | ;;;; modify it under the terms of the GNU Lesser General Public | |
6 | ;;;; License as published by the Free Software Foundation; either | |
53befeb7 | 7 | ;;;; version 3 of the License, or (at your option) any later version. |
f70d7468 | 8 | ;;;; |
73be1d9e | 9 | ;;;; This library is distributed in the hope that it will be useful, |
f70d7468 | 10 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
73be1d9e MV |
11 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | ;;;; Lesser General Public License for more details. | |
f70d7468 | 13 | ;;;; |
73be1d9e MV |
14 | ;;;; You should have received a copy of the GNU Lesser General Public |
15 | ;;;; License along with this library; if not, write to the Free Software | |
92205699 | 16 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
f70d7468 MD |
17 | |
18 | ;;; {Description} | |
19 | ||
20 | ;;; This is a semi test suite for weaks; I say semi, because weaks | |
21 | ;;; are pretty non-deterministic given the amount of information we | |
22 | ;;; can infer from scheme. | |
23 | ;;; | |
24 | ;;; In particular, we can't always reliably test the more important | |
25 | ;;; aspects of weaks (i.e., that an object is removed when it's dead) | |
26 | ;;; because we have no way of knowing for certain that the object is | |
27 | ;;; really dead. It tests it anyway, but the failures of any `death' | |
28 | ;;; tests really shouldn't be surprising. | |
29 | ;;; | |
30 | ;;; Interpret failures in the dying functions here as a hint that you | |
31 | ;;; should look at any changes you've made involving weaks | |
32 | ;;; (everything else should always pass), but there are a host of | |
33 | ;;; other reasons why they might not work as tested here, so if you | |
34 | ;;; haven't done anything to weaks, don't sweat it :) | |
35 | ||
eb074bfc MV |
36 | (use-modules (test-suite lib) |
37 | (ice-9 weak-vector)) | |
cbbeea66 | 38 | |
f70d7468 MD |
39 | ;;; Creation functions |
40 | ||
41 | ||
57e7f270 DH |
42 | (with-test-prefix |
43 | "weak-creation" | |
44 | (with-test-prefix "make-weak-vector" | |
45 | (pass-if "normal" | |
6b4113af DH |
46 | (make-weak-vector 10 #f) |
47 | #t) | |
48 | (pass-if-exception "bad size" | |
49 | exception:wrong-type-arg | |
50 | (make-weak-vector 'foo))) | |
57e7f270 DH |
51 | |
52 | (with-test-prefix "list->weak-vector" | |
53 | (pass-if "create" | |
54 | (let* ((lst '(a b c d e f g)) | |
55 | (wv (list->weak-vector lst))) | |
56 | (and (eq? (vector-ref wv 0) 'a) | |
57 | (eq? (vector-ref wv 1) 'b) | |
58 | (eq? (vector-ref wv 2) 'c) | |
59 | (eq? (vector-ref wv 3) 'd) | |
60 | (eq? (vector-ref wv 4) 'e) | |
61 | (eq? (vector-ref wv 5) 'f) | |
62 | (eq? (vector-ref wv 6) 'g)))) | |
6b4113af DH |
63 | (pass-if-exception "bad-args" |
64 | exception:wrong-type-arg | |
65 | (list->weak-vector 32))) | |
57e7f270 | 66 | |
cbbeea66 | 67 | (with-test-prefix "make-weak-key-alist-vector" |
57e7f270 | 68 | (pass-if "create" |
cbbeea66 | 69 | (make-weak-key-alist-vector 17) |
6b4113af DH |
70 | #t) |
71 | (pass-if-exception "bad-args" | |
72 | exception:wrong-type-arg | |
cbbeea66 MD |
73 | (make-weak-key-alist-vector '(bad arg)))) |
74 | (with-test-prefix "make-weak-value-alist-vector" | |
57e7f270 | 75 | (pass-if "create" |
cbbeea66 | 76 | (make-weak-value-alist-vector 17) |
6b4113af DH |
77 | #t) |
78 | (pass-if-exception "bad-args" | |
79 | exception:wrong-type-arg | |
cbbeea66 | 80 | (make-weak-value-alist-vector '(bad arg)))) |
57e7f270 | 81 | |
cbbeea66 | 82 | (with-test-prefix "make-doubly-weak-alist-vector" |
57e7f270 | 83 | (pass-if "create" |
cbbeea66 | 84 | (make-doubly-weak-alist-vector 17) |
6b4113af DH |
85 | #t) |
86 | (pass-if-exception "bad-args" | |
87 | exception:wrong-type-arg | |
cbbeea66 | 88 | (make-doubly-weak-alist-vector '(bad arg))))) |
f70d7468 MD |
89 | |
90 | ||
91 | ||
92 | ||
93 | ;; This should remove most of the non-dying problems associated with | |
94 | ;; trying this inside a closure | |
95 | ||
96 | (define global-weak (make-weak-vector 10 #f)) | |
97 | (begin | |
43b03fbb LC |
98 | (vector-set! global-weak 0 (string-copy "string")) |
99 | (vector-set! global-weak 1 (string-copy "beans")) | |
100 | (vector-set! global-weak 2 (string-copy "to")) | |
101 | (vector-set! global-weak 3 (string-copy "utah")) | |
102 | (vector-set! global-weak 4 (string-copy "yum yum")) | |
f70d7468 MD |
103 | (gc)) |
104 | ||
105 | ;;; Normal weak vectors | |
57e7f270 DH |
106 | (let ((x (make-weak-vector 10 #f)) |
107 | (bar "bar")) | |
108 | (with-test-prefix | |
109 | "weak-vector" | |
110 | (pass-if "lives" | |
111 | (begin | |
112 | (vector-set! x 0 bar) | |
113 | (gc) | |
114 | (and (vector-ref x 0) (eq? bar (vector-ref x 0))))) | |
115 | (pass-if "dies" | |
116 | (begin | |
117 | (gc) | |
eb074bfc MV |
118 | (or (and (not (vector-ref global-weak 0)) |
119 | (not (vector-ref global-weak 1)) | |
120 | (not (vector-ref global-weak 2)) | |
121 | (not (vector-ref global-weak 3)) | |
122 | (not (vector-ref global-weak 4))) | |
123 | (throw 'unresolved)))))) | |
57e7f270 | 124 | |
cbbeea66 MD |
125 | (let ((x (make-weak-key-alist-vector 17)) |
126 | (y (make-weak-value-alist-vector 17)) | |
127 | (z (make-doubly-weak-alist-vector 17)) | |
57e7f270 DH |
128 | (test-key "foo") |
129 | (test-value "bar")) | |
130 | (with-test-prefix | |
131 | "weak-hash" | |
132 | (pass-if "lives" | |
133 | (begin | |
5a99a574 LC |
134 | (hash-set! x test-key test-value) |
135 | (hash-set! y test-key test-value) | |
136 | (hash-set! z test-key test-value) | |
57e7f270 DH |
137 | (gc) |
138 | (gc) | |
5a99a574 LC |
139 | (and (hash-ref x test-key) |
140 | (hash-ref y test-key) | |
141 | (hash-ref z test-key) | |
5c96bc39 | 142 | #t))) |
5a99a574 LC |
143 | |
144 | ;; In the tests below we use `string-copy' to avoid the risk of | |
145 | ;; unintended retention of a string that we want to be GC'd. | |
146 | ||
57e7f270 | 147 | (pass-if "weak-key dies" |
5a99a574 LC |
148 | (begin |
149 | (hash-set! x (string-copy "this") "is") | |
150 | (hash-set! x (string-copy "a") "test") | |
151 | (hash-set! x (string-copy "of") "the") | |
152 | (hash-set! x (string-copy "emergency") "weak") | |
153 | (hash-set! x (string-copy "key") "hash system") | |
154 | (gc) | |
155 | (and | |
156 | (or (not (hash-ref x "this")) | |
157 | (not (hash-ref x "a")) | |
158 | (not (hash-ref x "of")) | |
159 | (not (hash-ref x "emergency")) | |
160 | (not (hash-ref x "key"))) | |
161 | (hash-ref x test-key) | |
162 | #t))) | |
57e7f270 DH |
163 | |
164 | (pass-if "weak-value dies" | |
5a99a574 LC |
165 | (begin |
166 | (hash-set! y "this" (string-copy "is")) | |
167 | (hash-set! y "a" (string-copy "test")) | |
168 | (hash-set! y "of" (string-copy "the")) | |
169 | (hash-set! y "emergency" (string-copy "weak")) | |
170 | (hash-set! y "value" (string-copy "hash system")) | |
171 | (gc) | |
172 | (and (or (not (hash-ref y "this")) | |
173 | (not (hash-ref y "a")) | |
174 | (not (hash-ref y "of")) | |
175 | (not (hash-ref y "emergency")) | |
176 | (not (hash-ref y "value"))) | |
177 | (hash-ref y test-key) | |
178 | #t))) | |
179 | ||
57e7f270 | 180 | (pass-if "doubly-weak dies" |
5a99a574 LC |
181 | (begin |
182 | (hash-set! z (string-copy "this") (string-copy "is")) | |
183 | (hash-set! z "a" (string-copy "test")) | |
184 | (hash-set! z (string-copy "of") "the") | |
185 | (hash-set! z "emergency" (string-copy "weak")) | |
186 | (hash-set! z (string-copy "all") (string-copy "hash system")) | |
187 | (gc) | |
188 | (and (or (not (hash-ref z "this")) | |
189 | (not (hash-ref z "a")) | |
190 | (not (hash-ref z "of")) | |
191 | (not (hash-ref z "emergency")) | |
192 | (not (hash-ref z "all"))) | |
193 | (hash-ref z test-key) | |
194 | #t))))) |