weaks.test 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ;;;; weaks.test --- tests guile's weaks -*- scheme -*-
  2. ;;;; Copyright (C) 1999, 2001 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program 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
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;;;; Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;;; As a special exception, the Free Software Foundation gives permission
  20. ;;;; for additional uses of the text contained in its release of GUILE.
  21. ;;;;
  22. ;;;; The exception is that, if you link the GUILE library with other files
  23. ;;;; to produce an executable, this does not by itself cause the
  24. ;;;; resulting executable to be covered by the GNU General Public License.
  25. ;;;; Your use of that executable is in no way restricted on account of
  26. ;;;; linking the GUILE library code into it.
  27. ;;;;
  28. ;;;; This exception does not however invalidate any other reasons why
  29. ;;;; the executable file might be covered by the GNU General Public License.
  30. ;;;;
  31. ;;;; This exception applies only to the code released by the
  32. ;;;; Free Software Foundation under the name GUILE. If you copy
  33. ;;;; code from other Free Software Foundation releases into a copy of
  34. ;;;; GUILE, as the General Public License permits, the exception does
  35. ;;;; not apply to the code that you add in this way. To avoid misleading
  36. ;;;; anyone as to the status of such modified files, you must delete
  37. ;;;; this exception notice from them.
  38. ;;;;
  39. ;;;; If you write modifications of your own for GUILE, it is your choice
  40. ;;;; whether to permit this exception to apply to your modifications.
  41. ;;;; If you do not wish that, delete this exception notice.
  42. ;;; {Description}
  43. ;;; This is a semi test suite for weaks; I say semi, because weaks
  44. ;;; are pretty non-deterministic given the amount of information we
  45. ;;; can infer from scheme.
  46. ;;;
  47. ;;; In particular, we can't always reliably test the more important
  48. ;;; aspects of weaks (i.e., that an object is removed when it's dead)
  49. ;;; because we have no way of knowing for certain that the object is
  50. ;;; really dead. It tests it anyway, but the failures of any `death'
  51. ;;; tests really shouldn't be surprising.
  52. ;;;
  53. ;;; Interpret failures in the dying functions here as a hint that you
  54. ;;; should look at any changes you've made involving weaks
  55. ;;; (everything else should always pass), but there are a host of
  56. ;;; other reasons why they might not work as tested here, so if you
  57. ;;; haven't done anything to weaks, don't sweat it :)
  58. ;;; Creation functions
  59. (with-test-prefix
  60. "weak-creation"
  61. (with-test-prefix "make-weak-vector"
  62. (pass-if "normal"
  63. (make-weak-vector 10 #f)
  64. #t)
  65. (pass-if-exception "bad size"
  66. exception:wrong-type-arg
  67. (make-weak-vector 'foo)))
  68. (with-test-prefix "list->weak-vector"
  69. (pass-if "create"
  70. (let* ((lst '(a b c d e f g))
  71. (wv (list->weak-vector lst)))
  72. (and (eq? (vector-ref wv 0) 'a)
  73. (eq? (vector-ref wv 1) 'b)
  74. (eq? (vector-ref wv 2) 'c)
  75. (eq? (vector-ref wv 3) 'd)
  76. (eq? (vector-ref wv 4) 'e)
  77. (eq? (vector-ref wv 5) 'f)
  78. (eq? (vector-ref wv 6) 'g))))
  79. (pass-if-exception "bad-args"
  80. exception:wrong-type-arg
  81. (list->weak-vector 32)))
  82. (with-test-prefix "make-weak-key-hash-table"
  83. (pass-if "create"
  84. (make-weak-key-hash-table 17)
  85. #t)
  86. (pass-if-exception "bad-args"
  87. exception:wrong-type-arg
  88. (make-weak-key-hash-table '(bad arg))))
  89. (with-test-prefix "make-weak-value-hash-table"
  90. (pass-if "create"
  91. (make-weak-value-hash-table 17)
  92. #t)
  93. (pass-if-exception "bad-args"
  94. exception:wrong-type-arg
  95. (make-weak-value-hash-table '(bad arg))))
  96. (with-test-prefix "make-doubly-weak-hash-table"
  97. (pass-if "create"
  98. (make-doubly-weak-hash-table 17)
  99. #t)
  100. (pass-if-exception "bad-args"
  101. exception:wrong-type-arg
  102. (make-doubly-weak-hash-table '(bad arg)))))
  103. ;; This should remove most of the non-dying problems associated with
  104. ;; trying this inside a closure
  105. (define global-weak (make-weak-vector 10 #f))
  106. (begin
  107. (vector-set! global-weak 0 "string")
  108. (vector-set! global-weak 1 "beans")
  109. (vector-set! global-weak 2 "to")
  110. (vector-set! global-weak 3 "utah")
  111. (vector-set! global-weak 4 "yum yum")
  112. (gc))
  113. ;;; Normal weak vectors
  114. (let ((x (make-weak-vector 10 #f))
  115. (bar "bar"))
  116. (with-test-prefix
  117. "weak-vector"
  118. (pass-if "lives"
  119. (begin
  120. (vector-set! x 0 bar)
  121. (gc)
  122. (and (vector-ref x 0) (eq? bar (vector-ref x 0)))))
  123. (pass-if "dies"
  124. (begin
  125. (gc)
  126. (or (not (vector-ref global-weak 0))
  127. (not (vector-ref global-weak 1))
  128. (not (vector-ref global-weak 2))
  129. (not (vector-ref global-weak 3))
  130. (not (vector-ref global-weak 4)))))))
  131. (let ((x (make-weak-key-hash-table 17))
  132. (y (make-weak-value-hash-table 17))
  133. (z (make-doubly-weak-hash-table 17))
  134. (test-key "foo")
  135. (test-value "bar"))
  136. (with-test-prefix
  137. "weak-hash"
  138. (pass-if "lives"
  139. (begin
  140. (hashq-set! x test-key test-value)
  141. (hashq-set! y test-key test-value)
  142. (hashq-set! z test-key test-value)
  143. (gc)
  144. (gc)
  145. (and (hashq-ref x test-key)
  146. (hashq-ref y test-key)
  147. (hashq-ref z test-key)
  148. #t)))
  149. (pass-if "weak-key dies"
  150. (begin
  151. (hashq-set! x "this" "is")
  152. (hashq-set! x "a" "test")
  153. (hashq-set! x "of" "the")
  154. (hashq-set! x "emergency" "weak")
  155. (hashq-set! x "key" "hash system")
  156. (gc)
  157. (and
  158. (or (not (hashq-ref x "this"))
  159. (not (hashq-ref x "a"))
  160. (not (hashq-ref x "of"))
  161. (not (hashq-ref x "emergency"))
  162. (not (hashq-ref x "key")))
  163. (hashq-ref x test-key)
  164. #t)))
  165. (pass-if "weak-value dies"
  166. (begin
  167. (hashq-set! y "this" "is")
  168. (hashq-set! y "a" "test")
  169. (hashq-set! y "of" "the")
  170. (hashq-set! y "emergency" "weak")
  171. (hashq-set! y "value" "hash system")
  172. (gc)
  173. (and (or (not (hashq-ref y "this"))
  174. (not (hashq-ref y "a"))
  175. (not (hashq-ref y "of"))
  176. (not (hashq-ref y "emergency"))
  177. (not (hashq-ref y "value")))
  178. (hashq-ref y test-key)
  179. #t)))
  180. (pass-if "doubly-weak dies"
  181. (begin
  182. (hashq-set! z "this" "is")
  183. (hashq-set! z "a" "test")
  184. (hashq-set! z "of" "the")
  185. (hashq-set! z "emergency" "weak")
  186. (hashq-set! z "all" "hash system")
  187. (gc)
  188. (and (or (not (hashq-ref z "this"))
  189. (not (hashq-ref z "a"))
  190. (not (hashq-ref z "of"))
  191. (not (hashq-ref z "emergency"))
  192. (not (hashq-ref z "all")))
  193. (hashq-ref z test-key)
  194. #t)))))