|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-weaks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(with-test-prefix |
|
"weak-creation" |
|
(with-test-prefix "make-weak-vector" |
|
(pass-if "normal" |
|
(make-weak-vector 10 |
|
|
|
(pass-if-exception "bad size" |
|
exception:wrong-type-arg |
|
(make-weak-vector 'foo))) |
|
|
|
(with-test-prefix "list->weak-vector" |
|
(pass-if "create" |
|
(let* ((lst '(a b c d e f g)) |
|
(wv (list->weak-vector lst))) |
|
(and (eq? (weak-vector-ref wv 0) 'a) |
|
(eq? (weak-vector-ref wv 1) 'b) |
|
(eq? (weak-vector-ref wv 2) 'c) |
|
(eq? (weak-vector-ref wv 3) 'd) |
|
(eq? (weak-vector-ref wv 4) 'e) |
|
(eq? (weak-vector-ref wv 5) 'f) |
|
(eq? (weak-vector-ref wv 6) 'g)))) |
|
(pass-if-exception "bad-args" |
|
exception:wrong-type-arg |
|
(list->weak-vector 32))) |
|
|
|
(with-test-prefix "make-weak-key-hash-table" |
|
(pass-if "create" |
|
(make-weak-key-hash-table 17) |
|
#t) |
|
(pass-if-exception "bad-args" |
|
exception:wrong-type-arg |
|
(make-weak-key-hash-table '(bad arg)))) |
|
(with-test-prefix "make-weak-value-hash-table" |
|
(pass-if "create" |
|
(make-weak-value-hash-table 17) |
|
|
|
(pass-if-exception "bad-args" |
|
exception:wrong-type-arg |
|
(make-weak-value-hash-table '(bad arg)))) |
|
|
|
(with-test-prefix "make-doubly-weak-hash-table" |
|
(pass-if "create" |
|
(make-doubly-weak-hash-table 17) |
|
#t) |
|
(pass-if-exception "bad-args" |
|
exception:wrong-type-arg |
|
(make-doubly-weak-hash-table '(bad arg))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define global-weak (make-weak-vector 10 |
|
(begin |
|
(weak-vector-set! global-weak 0 (string-copy "string")) |
|
(weak-vector-set! global-weak 1 (string-copy "beans")) |
|
(weak-vector-set! global-weak 2 (string-copy "to")) |
|
(weak-vector-set! global-weak 3 (string-copy "utah")) |
|
(weak-vector-set! global-weak 4 (string-copy "yum yum")) |
|
(gc)) |
|
|
|
|
|
(let ((x (make-weak-vector 10 |
|
(bar "bar")) |
|
(with-test-prefix |
|
"weak-vector" |
|
(pass-if "lives" |
|
(begin |
|
(weak-vector-set! x 0 bar) |
|
(gc) |
|
(and (weak-vector-ref x 0) (eq? bar (weak-vector-ref x 0))))) |
|
(pass-if "dies" |
|
(begin |
|
(gc) |
|
(or (and (not (weak-vector-ref global-weak 0)) |
|
(not (weak-vector-ref global-weak 1)) |
|
(not (weak-vector-ref global-weak 2)) |
|
(not (weak-vector-ref global-weak 3)) |
|
(not (weak-vector-ref global-weak 4))) |
|
(throw 'unresolved)))))) |
|
|
|
|
|
;;; |
|
;;; Weak hash tables & weak alist vectors. |
|
;;; |
|
|
|
(define (valid? value initial-value) |
|
;; Return true if VALUE is "valid", i.e., if it's either |
|
|
|
|
|
(or (not value) |
|
(equal? value initial-value))) |
|
|
|
(let ((x (make-weak-key-hash-table 17)) |
|
(y (make-weak-value-hash-table 17)) |
|
(z (make-doubly-weak-hash-table 17)) |
|
(test-key "foo") |
|
(test-value "bar")) |
|
(with-test-prefix |
|
"weak-hash" |
|
(pass-if "lives" |
|
(begin |
|
(hash-set! x test-key test-value) |
|
(hash-set! y test-key test-value) |
|
(hash-set! z test-key test-value) |
|
(gc) |
|
(gc) |
|
(and (hash-ref x test-key) |
|
(hash-ref y test-key) |
|
(hash-ref z test-key) |
|
|
|
|
|
|
|
|
|
|
|
(pass-if "weak-key dies" |
|
(begin |
|
(hash-set! x (string-copy "this") "is") |
|
(hash-set! x (string-copy "a") "test") |
|
(hash-set! x (string-copy "of") "the") |
|
(hash-set! x (string-copy "emergency") "weak") |
|
(hash-set! x (string-copy "key") "hash system") |
|
(gc) |
|
(let ((values (map (cut hash-ref x <>) |
|
'("this" "a" "of" "emergency" "key")))) |
|
(and (every valid? values |
|
'("is" "test" "the" "weak" "hash system")) |
|
(any not values) |
|
(hash-ref x test-key) |
|
|
|
|
|
(pass-if "weak-value dies" |
|
(begin |
|
(hash-set! y "this" (string-copy "is")) |
|
(hash-set! y "a" (string-copy "test")) |
|
(hash-set! y "of" (string-copy "the")) |
|
(hash-set! y "emergency" (string-copy "weak")) |
|
(hash-set! y "value" (string-copy "hash system")) |
|
(gc) |
|
(let ((values (map (cut hash-ref y <>) |
|
'("this" "a" "of" "emergency" "key")))) |
|
(and (every valid? values |
|
'("is" "test" "the" "weak" "hash system")) |
|
(any not values) |
|
(hash-ref y test-key) |
|
|
|
|
|
(pass-if "doubly-weak dies" |
|
(begin |
|
(hash-set! z (string-copy "this") (string-copy "is")) |
|
(hash-set! z "a" (string-copy "test")) |
|
(hash-set! z (string-copy "of") "the") |
|
(hash-set! z "emergency" (string-copy "weak")) |
|
(hash-set! z (string-copy "all") (string-copy "hash system")) |
|
(gc) |
|
(let ((values (map (cut hash-ref z <>) |
|
'("this" "a" "of" "emergency" "key")))) |
|
(and (every valid? values |
|
'("is" "test" "the" "weak" "hash system")) |
|
(any not values) |
|
(hash-ref z test-key) |
|
|
|
|
|
(pass-if "hash-set!, weak val, im -> im" |
|
(let ((t (make-weak-value-hash-table))) |
|
(hash-set! t "foo" 1) |
|
(hash-set! t "foo" 2) |
|
(equal? (hash-ref t "foo") 2))) |
|
|
|
(pass-if "hash-set!, weak val, im -> nim" |
|
(let ((t (make-weak-value-hash-table))) |
|
(hash-set! t "foo" 1) |
|
(hash-set! t "foo" "baz") |
|
(equal? (hash-ref t "foo") "baz"))) |
|
|
|
(pass-if "hash-set!, weak val, nim -> nim" |
|
(let ((t (make-weak-value-hash-table))) |
|
(hash-set! t "foo" "bar") |
|
(hash-set! t "foo" "baz") |
|
(equal? (hash-ref t "foo") "baz"))) |
|
|
|
(pass-if "hash-set!, weak val, nim -> im" |
|
(let ((t (make-weak-value-hash-table))) |
|
(hash-set! t "foo" "bar") |
|
(hash-set! t "foo" 1) |
|
(equal? (hash-ref t "foo") 1))) |
|
|
|
(pass-if "hash-set!, weak key, returns value" |
|
(let ((t (make-weak-value-hash-table)) |
|
(val (string |
|
(eq? (hashq-set! t "bar" val) |
|
(hashv-set! t "bar" val) |
|
(hash-set! t "bar" val) |
|
val))) |
|
|
|
(pass-if "assoc can do anything" |
|
|
|
|
|
|
|
|
|
(let ((h (make-doubly-weak-hash-table 2)) |
|
(c 123) |
|
(k "GNU")) |
|
|
|
(define (assoc-ci key bucket) |
|
(make-list 123) |
|
(gc) |
|
(find (lambda (p) |
|
(string-ci=? key (car p))) |
|
bucket)) |
|
|
|
(hashx-set! string-hash-ci assoc-ci h |
|
(string-copy "hello") (string-copy "world")) |
|
(hashx-set! string-hash-ci assoc-ci h |
|
k "Guile") |
|
|
|
(and (every (cut valid? <> "Guile") |
|
(unfold (cut >= <> c) |
|
(lambda (_) |
|
(hashx-ref string-hash-ci assoc-ci |
|
h "gnu")) |
|
1+ |
|
0)) |
|
(every (cut valid? <> "world") |
|
(unfold (cut >= <> c) |
|
(lambda (_) |
|
(hashx-ref string-hash-ci assoc-ci |
|
h "HELLO")) |
|
1+ |
|
0)) |
|
|
|
|