hash.test 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;;;; hash.test --- test guile hashing -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2004, 2005, 2006, 2008, 2011 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. (define-module (test-suite test-numbers)
  19. #:use-module (test-suite lib)
  20. #:use-module (ice-9 documentation))
  21. ;;;
  22. ;;; hash
  23. ;;;
  24. (with-test-prefix "hash"
  25. (pass-if (->bool (object-documentation hash)))
  26. (pass-if-exception "hash #t -1" exception:out-of-range
  27. (hash #t -1))
  28. (pass-if-exception "hash #t 0" exception:out-of-range
  29. (hash #t 0))
  30. (pass-if (= 0 (hash #t 1)))
  31. (pass-if (= 0 (hash #f 1)))
  32. (pass-if (= 0 (hash noop 1)))
  33. (pass-if (= 0 (hash +inf.0 1)))
  34. (pass-if (= 0 (hash -inf.0 1)))
  35. (pass-if (= 0 (hash +nan.0 1))))
  36. ;;;
  37. ;;; hashv
  38. ;;;
  39. (with-test-prefix "hashv"
  40. (pass-if (->bool (object-documentation hashv)))
  41. (pass-if-exception "hashv #t -1" exception:out-of-range
  42. (hashv #t -1))
  43. (pass-if-exception "hashv #t 0" exception:out-of-range
  44. (hashv #t 0))
  45. (pass-if (= 0 (hashv #t 1)))
  46. (pass-if (= 0 (hashv #f 1)))
  47. (pass-if (= 0 (hashv noop 1))))
  48. ;;;
  49. ;;; hashq
  50. ;;;
  51. (with-test-prefix "hashq"
  52. (pass-if (->bool (object-documentation hashq)))
  53. (pass-if-exception "hashq #t -1" exception:out-of-range
  54. (hashq #t -1))
  55. (pass-if-exception "hashq #t 0" exception:out-of-range
  56. (hashq #t 0))
  57. (pass-if (= 0 (hashq #t 1)))
  58. (pass-if (= 0 (hashq #f 1)))
  59. (pass-if (= 0 (hashq noop 1))))
  60. ;;;
  61. ;;; make-hash-table
  62. ;;;
  63. (with-test-prefix
  64. "make-hash-table, hash-table?"
  65. (pass-if-exception "make-hash-table -1" exception:out-of-range
  66. (make-hash-table -1))
  67. (pass-if (hash-table? (make-hash-table 0))) ;; default
  68. (pass-if (not (hash-table? 'not-a-hash-table)))
  69. (pass-if (equal? "#<hash-table 0/113>"
  70. (with-output-to-string
  71. (lambda () (write (make-hash-table 100)))))))
  72. ;;;
  73. ;;; usual set and reference
  74. ;;;
  75. (with-test-prefix
  76. "hash-set and hash-ref"
  77. ;; auto-resizing
  78. (pass-if (let ((table (make-hash-table 1))) ;;actually makes size 31
  79. (hash-set! table 'one 1)
  80. (hash-set! table 'two #t)
  81. (hash-set! table 'three #t)
  82. (hash-set! table 'four #t)
  83. (hash-set! table 'five #t)
  84. (hash-set! table 'six #t)
  85. (hash-set! table 'seven #t)
  86. (hash-set! table 'eight #t)
  87. (hash-set! table 'nine 9)
  88. (hash-set! table 'ten #t)
  89. (hash-set! table 'eleven #t)
  90. (hash-set! table 'twelve #t)
  91. (hash-set! table 'thirteen #t)
  92. (hash-set! table 'fourteen #t)
  93. (hash-set! table 'fifteen #t)
  94. (hash-set! table 'sixteen #t)
  95. (hash-set! table 'seventeen #t)
  96. (hash-set! table 18 #t)
  97. (hash-set! table 19 #t)
  98. (hash-set! table 20 #t)
  99. (hash-set! table 21 #t)
  100. (hash-set! table 22 #t)
  101. (hash-set! table 23 #t)
  102. (hash-set! table 24 #t)
  103. (hash-set! table 25 #t)
  104. (hash-set! table 26 #t)
  105. (hash-set! table 27 #t)
  106. (hash-set! table 28 #t)
  107. (hash-set! table 29 #t)
  108. (hash-set! table 30 'thirty)
  109. (hash-set! table 31 #t)
  110. (hash-set! table 32 #t)
  111. (hash-set! table 33 'thirty-three)
  112. (hash-set! table 34 #t)
  113. (hash-set! table 35 #t)
  114. (hash-set! table 'foo 'bar)
  115. (and (equal? 1 (hash-ref table 'one))
  116. (equal? 9 (hash-ref table 'nine))
  117. (equal? 'thirty (hash-ref table 30))
  118. (equal? 'thirty-three (hash-ref table 33))
  119. (equal? 'bar (hash-ref table 'foo))
  120. (equal? "#<hash-table 36/61>"
  121. (with-output-to-string (lambda () (write table)))))))
  122. ;; 1 and 1 are equal? and eqv? and eq?
  123. (pass-if (equal? 'foo
  124. (let ((table (make-hash-table)))
  125. (hash-set! table 1 'foo)
  126. (hash-ref table 1))))
  127. (pass-if (equal? 'foo
  128. (let ((table (make-hash-table)))
  129. (hashv-set! table 1 'foo)
  130. (hashv-ref table 1))))
  131. (pass-if (equal? 'foo
  132. (let ((table (make-hash-table)))
  133. (hashq-set! table 1 'foo)
  134. (hashq-ref table 1))))
  135. ;; 1/2 and 2/4 are equal? and eqv? but not eq?
  136. (pass-if (equal? 'foo
  137. (let ((table (make-hash-table)))
  138. (hash-set! table 1/2 'foo)
  139. (hash-ref table 2/4))))
  140. (pass-if (equal? 'foo
  141. (let ((table (make-hash-table)))
  142. (hashv-set! table 1/2 'foo)
  143. (hashv-ref table 2/4))))
  144. (pass-if (equal? #f
  145. (let ((table (make-hash-table)))
  146. (hashq-set! table 1/2 'foo)
  147. (hashq-ref table 2/4))))
  148. ;; (list 1 2) is equal? but not eqv? or eq? to another (list 1 2)
  149. (pass-if (equal? 'foo
  150. (let ((table (make-hash-table)))
  151. (hash-set! table (list 1 2) 'foo)
  152. (hash-ref table (list 1 2)))))
  153. (pass-if (equal? #f
  154. (let ((table (make-hash-table)))
  155. (hashv-set! table (list 1 2) 'foo)
  156. (hashv-ref table (list 1 2)))))
  157. (pass-if (equal? #f
  158. (let ((table (make-hash-table)))
  159. (hashq-set! table (list 1 2) 'foo)
  160. (hashq-ref table (list 1 2)))))
  161. ;; ref default argument
  162. (pass-if (equal? 'bar
  163. (let ((table (make-hash-table)))
  164. (hash-ref table 'foo 'bar))))
  165. (pass-if (equal? 'bar
  166. (let ((table (make-hash-table)))
  167. (hashv-ref table 'foo 'bar))))
  168. (pass-if (equal? 'bar
  169. (let ((table (make-hash-table)))
  170. (hashq-ref table 'foo 'bar))))
  171. (pass-if (equal? 'bar
  172. (let ((table (make-hash-table)))
  173. (hashx-ref hash equal? table 'foo 'bar))))
  174. ;; wrong type argument
  175. (pass-if-exception "(hash-ref 'not-a-table 'key)" exception:wrong-type-arg
  176. (hash-ref 'not-a-table 'key))
  177. )
  178. ;;;
  179. ;;; hashx
  180. ;;;
  181. (with-test-prefix
  182. "auto-resizing hashx"
  183. ;; auto-resizing
  184. (let ((table (make-hash-table 1))) ;;actually makes size 31
  185. (hashx-set! hash assoc table 1/2 'equal)
  186. (hashx-set! hash assoc table 1/3 'equal)
  187. (hashx-set! hash assoc table 4 'equal)
  188. (hashx-set! hash assoc table 1/5 'equal)
  189. (hashx-set! hash assoc table 1/6 'equal)
  190. (hashx-set! hash assoc table 7 'equal)
  191. (hashx-set! hash assoc table 1/8 'equal)
  192. (hashx-set! hash assoc table 1/9 'equal)
  193. (hashx-set! hash assoc table 10 'equal)
  194. (hashx-set! hash assoc table 1/11 'equal)
  195. (hashx-set! hash assoc table 1/12 'equal)
  196. (hashx-set! hash assoc table 13 'equal)
  197. (hashx-set! hash assoc table 1/14 'equal)
  198. (hashx-set! hash assoc table 1/15 'equal)
  199. (hashx-set! hash assoc table 16 'equal)
  200. (hashx-set! hash assoc table 1/17 'equal)
  201. (hashx-set! hash assoc table 1/18 'equal)
  202. (hashx-set! hash assoc table 19 'equal)
  203. (hashx-set! hash assoc table 1/20 'equal)
  204. (hashx-set! hash assoc table 1/21 'equal)
  205. (hashx-set! hash assoc table 22 'equal)
  206. (hashx-set! hash assoc table 1/23 'equal)
  207. (hashx-set! hash assoc table 1/24 'equal)
  208. (hashx-set! hash assoc table 25 'equal)
  209. (hashx-set! hash assoc table 1/26 'equal)
  210. (hashx-set! hash assoc table 1/27 'equal)
  211. (hashx-set! hash assoc table 28 'equal)
  212. (hashx-set! hash assoc table 1/29 'equal)
  213. (hashx-set! hash assoc table 1/30 'equal)
  214. (hashx-set! hash assoc table 31 'equal)
  215. (hashx-set! hash assoc table 1/32 'equal)
  216. (hashx-set! hash assoc table 1/33 'equal)
  217. (hashx-set! hash assoc table 34 'equal)
  218. (pass-if (equal? 'equal (hash-ref table 2/4)))
  219. (pass-if (equal? 'equal (hash-ref table 2/6)))
  220. (pass-if (equal? 'equal (hash-ref table 4)))
  221. (pass-if (equal? 'equal (hashx-ref hash assoc table 2/64)))
  222. (pass-if (equal? 'equal (hashx-ref hash assoc table 2/66)))
  223. (pass-if (equal? 'equal (hashx-ref hash assoc table 34)))
  224. (pass-if (equal? "#<hash-table 33/61>"
  225. (with-output-to-string (lambda () (write table)))))))
  226. (with-test-prefix
  227. "hashx"
  228. (pass-if (let ((table (make-hash-table)))
  229. (hashx-set! (lambda (k v) 1)
  230. (lambda (k al) (assoc 'foo al))
  231. table 'foo 'bar)
  232. (equal?
  233. 'bar (hashx-ref (lambda (k v) 1)
  234. (lambda (k al) (assoc 'foo al))
  235. table 'baz))))
  236. (pass-if (let ((table (make-hash-table 31)))
  237. (hashx-set! (lambda (k v) 1) assoc table 'foo 'bar)
  238. (equal? #f
  239. (hashx-ref (lambda (k v) 2) assoc table 'foo))))
  240. (pass-if (let ((table (make-hash-table)))
  241. (hashx-set! hash assoc table 'foo 'bar)
  242. (equal? #f
  243. (hashx-ref hash (lambda (k al) #f) table 'foo))))
  244. (pass-if-exception
  245. "hashx-set! (lambda (k s) 1) equal? table 'foo 'bar"
  246. exception:wrong-type-arg ;; there must be a better exception than that...
  247. (hashx-set! (lambda (k s) 1) (lambda (k al) #t) (make-hash-table) 'foo 'bar))
  248. )
  249. ;;;
  250. ;;; hashx-remove!
  251. ;;;
  252. (with-test-prefix "hashx-remove!"
  253. (pass-if (->bool (object-documentation hashx-remove!)))
  254. (pass-if (let ((table (make-hash-table)))
  255. (hashx-set! hashq assq table 'x 123)
  256. (hashx-remove! hashq assq table 'x)
  257. (null? (hash-map->list noop table)))))
  258. ;;;
  259. ;;; hashx
  260. ;;;
  261. (with-test-prefix "hashx"
  262. (pass-if-exception
  263. "hashx-set! (lambda (k s) 1) (lambda (k al) #t) table 'foo 'bar"
  264. exception:wrong-type-arg
  265. (hashx-set! (lambda (k s) 1) (lambda (k al) #t) (make-hash-table) 'foo 'bar))
  266. )