weaks.test 5.9 KB

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