weaks.test 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ;;;; weaks.test --- tests guile's weaks -*- scheme -*-
  2. ;;;; Copyright (C) 1999, 2001, 2003, 2006, 2009, 2010, 2011, 2012, 2014
  3. ;;;; Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; {Description}
  19. ;;; This is a semi test suite for weaks; I say semi, because weaks
  20. ;;; are pretty non-deterministic given the amount of information we
  21. ;;; can infer from scheme.
  22. ;;;
  23. ;;; In particular, we can't always reliably test the more important
  24. ;;; aspects of weaks (i.e., that an object is removed when it's dead)
  25. ;;; because we have no way of knowing for certain that the object is
  26. ;;; really dead. It tests it anyway, but the failures of any `death'
  27. ;;; tests really shouldn't be surprising.
  28. ;;;
  29. ;;; Interpret failures in the dying functions here as a hint that you
  30. ;;; should look at any changes you've made involving weaks
  31. ;;; (everything else should always pass), but there are a host of
  32. ;;; other reasons why they might not work as tested here, so if you
  33. ;;; haven't done anything to weaks, don't sweat it :)
  34. (define-module (test-weaks)
  35. #:use-module (test-suite lib)
  36. #:use-module (ice-9 weak-vector)
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-26))
  39. ;;; Creation functions
  40. (with-test-prefix
  41. "weak-creation"
  42. (with-test-prefix "make-weak-vector"
  43. (pass-if "normal"
  44. (make-weak-vector 10 #f)
  45. #t)
  46. (pass-if-exception "bad size"
  47. exception:wrong-type-arg
  48. (make-weak-vector 'foo)))
  49. (with-test-prefix "list->weak-vector"
  50. (pass-if "create"
  51. (let* ((lst '(a b c d e f g))
  52. (wv (list->weak-vector lst)))
  53. (and (eq? (weak-vector-ref wv 0) 'a)
  54. (eq? (weak-vector-ref wv 1) 'b)
  55. (eq? (weak-vector-ref wv 2) 'c)
  56. (eq? (weak-vector-ref wv 3) 'd)
  57. (eq? (weak-vector-ref wv 4) 'e)
  58. (eq? (weak-vector-ref wv 5) 'f)
  59. (eq? (weak-vector-ref wv 6) 'g))))
  60. (pass-if-exception "bad-args"
  61. exception:wrong-type-arg
  62. (list->weak-vector 32)))
  63. (with-test-prefix "make-weak-key-hash-table"
  64. (pass-if "create"
  65. (make-weak-key-hash-table 17)
  66. #t)
  67. (pass-if-exception "bad-args"
  68. exception:wrong-type-arg
  69. (make-weak-key-hash-table '(bad arg))))
  70. (with-test-prefix "make-weak-value-hash-table"
  71. (pass-if "create"
  72. (make-weak-value-hash-table 17)
  73. #t)
  74. (pass-if-exception "bad-args"
  75. exception:wrong-type-arg
  76. (make-weak-value-hash-table '(bad arg))))
  77. (with-test-prefix "make-doubly-weak-hash-table"
  78. (pass-if "create"
  79. (make-doubly-weak-hash-table 17)
  80. #t)
  81. (pass-if-exception "bad-args"
  82. exception:wrong-type-arg
  83. (make-doubly-weak-hash-table '(bad arg)))))
  84. ;; This should remove most of the non-dying problems associated with
  85. ;; trying this inside a closure
  86. (define global-weak (make-weak-vector 10 #f))
  87. (begin
  88. (weak-vector-set! global-weak 0 (string-copy "string"))
  89. (weak-vector-set! global-weak 1 (string-copy "beans"))
  90. (weak-vector-set! global-weak 2 (string-copy "to"))
  91. (weak-vector-set! global-weak 3 (string-copy "utah"))
  92. (weak-vector-set! global-weak 4 (string-copy "yum yum"))
  93. (gc))
  94. ;;; Normal weak vectors
  95. (let ((x (make-weak-vector 10 #f))
  96. (bar "bar"))
  97. (with-test-prefix
  98. "weak-vector"
  99. (pass-if "lives"
  100. (begin
  101. (weak-vector-set! x 0 bar)
  102. (gc)
  103. (and (weak-vector-ref x 0) (eq? bar (weak-vector-ref x 0)))))
  104. (pass-if "dies"
  105. (begin
  106. (gc)
  107. (or (and (not (weak-vector-ref global-weak 0))
  108. (not (weak-vector-ref global-weak 1))
  109. (not (weak-vector-ref global-weak 2))
  110. (not (weak-vector-ref global-weak 3))
  111. (not (weak-vector-ref global-weak 4)))
  112. (throw 'unresolved))))))
  113. ;;;
  114. ;;; Weak hash tables & weak alist vectors.
  115. ;;;
  116. (define (valid? value initial-value)
  117. ;; Return true if VALUE is "valid", i.e., if it's either #f or
  118. ;; INITIAL-VALUE. The idea is to make sure `hash-ref' doesn't return
  119. ;; garbage.
  120. (or (not value)
  121. (equal? value initial-value)))
  122. (let ((x (make-weak-key-hash-table 17))
  123. (y (make-weak-value-hash-table 17))
  124. (z (make-doubly-weak-hash-table 17))
  125. (test-key "foo")
  126. (test-value "bar"))
  127. (with-test-prefix
  128. "weak-hash"
  129. (pass-if "lives"
  130. (begin
  131. (hash-set! x test-key test-value)
  132. (hash-set! y test-key test-value)
  133. (hash-set! z test-key test-value)
  134. (gc)
  135. (gc)
  136. (and (hash-ref x test-key)
  137. (hash-ref y test-key)
  138. (hash-ref z test-key)
  139. #t)))
  140. ;; In the tests below we use `string-copy' to avoid the risk of
  141. ;; unintended retention of a string that we want to be GC'd.
  142. (pass-if "weak-key dies"
  143. (begin
  144. (hash-set! x (string-copy "this") "is")
  145. (hash-set! x (string-copy "a") "test")
  146. (hash-set! x (string-copy "of") "the")
  147. (hash-set! x (string-copy "emergency") "weak")
  148. (hash-set! x (string-copy "key") "hash system")
  149. (gc)
  150. (let ((values (map (cut hash-ref x <>)
  151. '("this" "a" "of" "emergency" "key"))))
  152. (and (every valid? values
  153. '("is" "test" "the" "weak" "hash system"))
  154. (any not values)
  155. (hash-ref x test-key)
  156. #t))))
  157. (pass-if "weak-value dies"
  158. (begin
  159. (hash-set! y "this" (string-copy "is"))
  160. (hash-set! y "a" (string-copy "test"))
  161. (hash-set! y "of" (string-copy "the"))
  162. (hash-set! y "emergency" (string-copy "weak"))
  163. (hash-set! y "value" (string-copy "hash system"))
  164. (gc)
  165. (let ((values (map (cut hash-ref y <>)
  166. '("this" "a" "of" "emergency" "key"))))
  167. (and (every valid? values
  168. '("is" "test" "the" "weak" "hash system"))
  169. (any not values)
  170. (hash-ref y test-key)
  171. #t))))
  172. (pass-if "doubly-weak dies"
  173. (begin
  174. (hash-set! z (string-copy "this") (string-copy "is"))
  175. (hash-set! z "a" (string-copy "test"))
  176. (hash-set! z (string-copy "of") "the")
  177. (hash-set! z "emergency" (string-copy "weak"))
  178. (hash-set! z (string-copy "all") (string-copy "hash system"))
  179. (gc)
  180. (let ((values (map (cut hash-ref z <>)
  181. '("this" "a" "of" "emergency" "key"))))
  182. (and (every valid? values
  183. '("is" "test" "the" "weak" "hash system"))
  184. (any not values)
  185. (hash-ref z test-key)
  186. #t))))
  187. (pass-if "hash-set!, weak val, im -> im"
  188. (let ((t (make-weak-value-hash-table)))
  189. (hash-set! t "foo" 1)
  190. (hash-set! t "foo" 2)
  191. (equal? (hash-ref t "foo") 2)))
  192. (pass-if "hash-set!, weak val, im -> nim"
  193. (let ((t (make-weak-value-hash-table)))
  194. (hash-set! t "foo" 1)
  195. (hash-set! t "foo" "baz")
  196. (equal? (hash-ref t "foo") "baz")))
  197. (pass-if "hash-set!, weak val, nim -> nim"
  198. (let ((t (make-weak-value-hash-table)))
  199. (hash-set! t "foo" "bar")
  200. (hash-set! t "foo" "baz")
  201. (equal? (hash-ref t "foo") "baz")))
  202. (pass-if "hash-set!, weak val, nim -> im"
  203. (let ((t (make-weak-value-hash-table)))
  204. (hash-set! t "foo" "bar")
  205. (hash-set! t "foo" 1)
  206. (equal? (hash-ref t "foo") 1)))
  207. (pass-if "hash-set!, weak key, returns value"
  208. (let ((t (make-weak-value-hash-table))
  209. (val (string #\f #\o #\o)))
  210. (eq? (hashq-set! t "bar" val)
  211. (hashv-set! t "bar" val)
  212. (hash-set! t "bar" val)
  213. val)))
  214. (pass-if "assoc can do anything"
  215. ;; Until 1.9.12, as hash table's custom ASSOC procedure was
  216. ;; called with the GC lock alloc held, which imposed severe
  217. ;; restrictions on what it could do (bug #29616). This test
  218. ;; makes sure this is no longer the case.
  219. (let ((h (make-doubly-weak-hash-table 2))
  220. (c 123)
  221. (k "GNU"))
  222. (define (assoc-ci key bucket)
  223. (make-list 123) ;; this should be possible
  224. (gc) ;; this too
  225. (find (lambda (p)
  226. (string-ci=? key (car p)))
  227. bucket))
  228. (hashx-set! string-hash-ci assoc-ci h
  229. (string-copy "hello") (string-copy "world"))
  230. (hashx-set! string-hash-ci assoc-ci h
  231. k "Guile")
  232. (and (every (cut valid? <> "Guile")
  233. (unfold (cut >= <> c)
  234. (lambda (_)
  235. (hashx-ref string-hash-ci assoc-ci
  236. h "gnu"))
  237. 1+
  238. 0))
  239. (every (cut valid? <> "world")
  240. (unfold (cut >= <> c)
  241. (lambda (_)
  242. (hashx-ref string-hash-ci assoc-ci
  243. h "HELLO"))
  244. 1+
  245. 0))
  246. #t)))))