hash.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. ;;;; hash.test --- test guile hashing -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2004, 2005, 2006, 2008, 2011, 2012,
  4. ;;;; 2014 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-numbers)
  20. #:use-module (test-suite lib)
  21. #:use-module (ice-9 documentation)
  22. #:use-module (ice-9 hash-table))
  23. ;;;
  24. ;;; hash
  25. ;;;
  26. (with-test-prefix "hash"
  27. (pass-if (->bool (object-documentation hash)))
  28. (pass-if-exception "hash #t -1" exception:out-of-range
  29. (hash #t -1))
  30. (pass-if-exception "hash #t 0" exception:out-of-range
  31. (hash #t 0))
  32. (pass-if (= 0 (hash #t 1)))
  33. (pass-if (= 0 (hash #f 1)))
  34. (pass-if (= 0 (hash noop 1)))
  35. (pass-if (= 0 (hash +inf.0 1)))
  36. (pass-if (= 0 (hash -inf.0 1)))
  37. (pass-if (= 0 (hash +nan.0 1)))
  38. (pass-if (= 0 (hash '#() 1)))
  39. (pass-if "cyclic vectors"
  40. (let ()
  41. (define (cyclic-vector n)
  42. (let ((v (make-vector n)))
  43. (vector-fill! v v)
  44. v))
  45. (and (= 0 (hash (cyclic-vector 3) 1))
  46. (= 0 (hash (cyclic-vector 10) 1))))))
  47. ;;;
  48. ;;; hashv
  49. ;;;
  50. (with-test-prefix "hashv"
  51. (pass-if (->bool (object-documentation hashv)))
  52. (pass-if-exception "hashv #t -1" exception:out-of-range
  53. (hashv #t -1))
  54. (pass-if-exception "hashv #t 0" exception:out-of-range
  55. (hashv #t 0))
  56. (pass-if (= 0 (hashv #t 1)))
  57. (pass-if (= 0 (hashv #f 1)))
  58. (pass-if (= 0 (hashv noop 1))))
  59. ;;;
  60. ;;; hashq
  61. ;;;
  62. (with-test-prefix "hashq"
  63. (pass-if (->bool (object-documentation hashq)))
  64. (pass-if-exception "hashq #t -1" exception:out-of-range
  65. (hashq #t -1))
  66. (pass-if-exception "hashq #t 0" exception:out-of-range
  67. (hashq #t 0))
  68. (pass-if (= 0 (hashq #t 1)))
  69. (pass-if (= 0 (hashq #f 1)))
  70. (pass-if (= 0 (hashq noop 1))))
  71. ;;;
  72. ;;; make-hash-table
  73. ;;;
  74. (with-test-prefix
  75. "make-hash-table, hash-table?"
  76. (pass-if-exception "make-hash-table -1" exception:out-of-range
  77. (make-hash-table -1))
  78. (pass-if (hash-table? (make-hash-table 0))) ;; default
  79. (pass-if (not (hash-table? 'not-a-hash-table)))
  80. (pass-if (string-suffix? " 0/113>"
  81. (with-output-to-string
  82. (lambda ()
  83. (write (make-hash-table 100)))))))
  84. ;;;
  85. ;;; alist->hash-table
  86. ;;;
  87. (with-test-prefix
  88. "alist conversion"
  89. (pass-if "alist->hash-table"
  90. (let ((table (alist->hash-table '(("foo" . 1)
  91. ("bar" . 2)
  92. ("foo" . 3)))))
  93. (and (= (hash-ref table "foo") 1)
  94. (= (hash-ref table "bar") 2))))
  95. (pass-if "alist->hashq-table"
  96. (let ((table (alist->hashq-table '((foo . 1)
  97. (bar . 2)
  98. (foo . 3)))))
  99. (and (= (hashq-ref table 'foo) 1)
  100. (= (hashq-ref table 'bar) 2))))
  101. (pass-if "alist->hashv-table"
  102. (let ((table (alist->hashv-table '((1 . 1)
  103. (2 . 2)
  104. (1 . 3)))))
  105. (and (= (hashv-ref table 1) 1)
  106. (= (hashv-ref table 2) 2))))
  107. (pass-if "alist->hashx-table"
  108. (let ((table (alist->hashx-table hash assoc '((foo . 1)
  109. (bar . 2)
  110. (foo . 3)))))
  111. (and (= (hashx-ref hash assoc table 'foo) 1)
  112. (= (hashx-ref hash assoc table 'bar) 2)))))
  113. ;;;
  114. ;;; usual set and reference
  115. ;;;
  116. (with-test-prefix
  117. "hash-set and hash-ref"
  118. ;; auto-resizing
  119. (pass-if (let ((table (make-hash-table 1))) ;;actually makes size 31
  120. (hash-set! table 'one 1)
  121. (hash-set! table 'two #t)
  122. (hash-set! table 'three #t)
  123. (hash-set! table 'four #t)
  124. (hash-set! table 'five #t)
  125. (hash-set! table 'six #t)
  126. (hash-set! table 'seven #t)
  127. (hash-set! table 'eight #t)
  128. (hash-set! table 'nine 9)
  129. (hash-set! table 'ten #t)
  130. (hash-set! table 'eleven #t)
  131. (hash-set! table 'twelve #t)
  132. (hash-set! table 'thirteen #t)
  133. (hash-set! table 'fourteen #t)
  134. (hash-set! table 'fifteen #t)
  135. (hash-set! table 'sixteen #t)
  136. (hash-set! table 'seventeen #t)
  137. (hash-set! table 18 #t)
  138. (hash-set! table 19 #t)
  139. (hash-set! table 20 #t)
  140. (hash-set! table 21 #t)
  141. (hash-set! table 22 #t)
  142. (hash-set! table 23 #t)
  143. (hash-set! table 24 #t)
  144. (hash-set! table 25 #t)
  145. (hash-set! table 26 #t)
  146. (hash-set! table 27 #t)
  147. (hash-set! table 28 #t)
  148. (hash-set! table 29 #t)
  149. (hash-set! table 30 'thirty)
  150. (hash-set! table 31 #t)
  151. (hash-set! table 32 #t)
  152. (hash-set! table 33 'thirty-three)
  153. (hash-set! table 34 #t)
  154. (hash-set! table 35 #t)
  155. (hash-set! table 'foo 'bar)
  156. (and (equal? 1 (hash-ref table 'one))
  157. (equal? 9 (hash-ref table 'nine))
  158. (equal? 'thirty (hash-ref table 30))
  159. (equal? 'thirty-three (hash-ref table 33))
  160. (equal? 'bar (hash-ref table 'foo))
  161. (string-suffix? " 36/61>"
  162. (with-output-to-string
  163. (lambda () (write table)))))))
  164. ;; 1 and 1 are equal? and eqv? (but not necessarily eq?)
  165. (pass-if (equal? 'foo
  166. (let ((table (make-hash-table)))
  167. (hash-set! table 1 'foo)
  168. (hash-ref table 1))))
  169. (pass-if (equal? 'foo
  170. (let ((table (make-hash-table)))
  171. (hashv-set! table 1 'foo)
  172. (hashv-ref table 1))))
  173. ;; 1/2 and 2/4 are equal? and eqv? (but not necessarily eq?)
  174. (pass-if (equal? 'foo
  175. (let ((table (make-hash-table)))
  176. (hash-set! table 1/2 'foo)
  177. (hash-ref table 2/4))))
  178. (pass-if (equal? 'foo
  179. (let ((table (make-hash-table)))
  180. (hashv-set! table 1/2 'foo)
  181. (hashv-ref table 2/4))))
  182. ;; (list 1 2) is equal? but not eqv? or eq? to another (list 1 2)
  183. (pass-if (equal? 'foo
  184. (let ((table (make-hash-table)))
  185. (hash-set! table (list 1 2) 'foo)
  186. (hash-ref table (list 1 2)))))
  187. (pass-if (equal? #f
  188. (let ((table (make-hash-table)))
  189. (hashv-set! table (list 1 2) 'foo)
  190. (hashv-ref table (list 1 2)))))
  191. (pass-if (equal? #f
  192. (let ((table (make-hash-table)))
  193. (hashq-set! table (list 1 2) 'foo)
  194. (hashq-ref table (list 1 2)))))
  195. ;; ref default argument
  196. (pass-if (equal? 'bar
  197. (let ((table (make-hash-table)))
  198. (hash-ref table 'foo 'bar))))
  199. (pass-if (equal? 'bar
  200. (let ((table (make-hash-table)))
  201. (hashv-ref table 'foo 'bar))))
  202. (pass-if (equal? 'bar
  203. (let ((table (make-hash-table)))
  204. (hashq-ref table 'foo 'bar))))
  205. (pass-if (equal? 'bar
  206. (let ((table (make-hash-table)))
  207. (hashx-ref hash equal? table 'foo 'bar))))
  208. ;; wrong type argument
  209. (pass-if-exception "(hash-ref 'not-a-table 'key)" exception:wrong-type-arg
  210. (hash-ref 'not-a-table 'key))
  211. )
  212. ;;;
  213. ;;; hashx
  214. ;;;
  215. (with-test-prefix
  216. "auto-resizing hashx"
  217. ;; auto-resizing
  218. (let ((table (make-hash-table 1))) ;;actually makes size 31
  219. (hashx-set! hash assoc table 1/2 'equal)
  220. (hashx-set! hash assoc table 1/3 'equal)
  221. (hashx-set! hash assoc table 4 'equal)
  222. (hashx-set! hash assoc table 1/5 'equal)
  223. (hashx-set! hash assoc table 1/6 'equal)
  224. (hashx-set! hash assoc table 7 'equal)
  225. (hashx-set! hash assoc table 1/8 'equal)
  226. (hashx-set! hash assoc table 1/9 'equal)
  227. (hashx-set! hash assoc table 10 'equal)
  228. (hashx-set! hash assoc table 1/11 'equal)
  229. (hashx-set! hash assoc table 1/12 'equal)
  230. (hashx-set! hash assoc table 13 'equal)
  231. (hashx-set! hash assoc table 1/14 'equal)
  232. (hashx-set! hash assoc table 1/15 'equal)
  233. (hashx-set! hash assoc table 16 'equal)
  234. (hashx-set! hash assoc table 1/17 'equal)
  235. (hashx-set! hash assoc table 1/18 'equal)
  236. (hashx-set! hash assoc table 19 'equal)
  237. (hashx-set! hash assoc table 1/20 'equal)
  238. (hashx-set! hash assoc table 1/21 'equal)
  239. (hashx-set! hash assoc table 22 'equal)
  240. (hashx-set! hash assoc table 1/23 'equal)
  241. (hashx-set! hash assoc table 1/24 'equal)
  242. (hashx-set! hash assoc table 25 'equal)
  243. (hashx-set! hash assoc table 1/26 'equal)
  244. (hashx-set! hash assoc table 1/27 'equal)
  245. (hashx-set! hash assoc table 28 'equal)
  246. (hashx-set! hash assoc table 1/29 'equal)
  247. (hashx-set! hash assoc table 1/30 'equal)
  248. (hashx-set! hash assoc table 31 'equal)
  249. (hashx-set! hash assoc table 1/32 'equal)
  250. (hashx-set! hash assoc table 1/33 'equal)
  251. (hashx-set! hash assoc table 34 'equal)
  252. (pass-if (equal? 'equal (hash-ref table 2/4)))
  253. (pass-if (equal? 'equal (hash-ref table 2/6)))
  254. (pass-if (equal? 'equal (hash-ref table 4)))
  255. (pass-if (equal? 'equal (hashx-ref hash assoc table 2/64)))
  256. (pass-if (equal? 'equal (hashx-ref hash assoc table 2/66)))
  257. (pass-if (equal? 'equal (hashx-ref hash assoc table 34)))
  258. (pass-if (string-suffix? " 33/61>"
  259. (with-output-to-string
  260. (lambda () (write table)))))))
  261. (with-test-prefix
  262. "hashx"
  263. (pass-if (let ((table (make-hash-table)))
  264. (hashx-set! (lambda (k v) 1)
  265. (lambda (k al) (assoc 'foo al))
  266. table 'foo 'bar)
  267. (equal?
  268. 'bar (hashx-ref (lambda (k v) 1)
  269. (lambda (k al) (assoc 'foo al))
  270. table 'baz))))
  271. (pass-if (let ((table (make-hash-table 31)))
  272. (hashx-set! (lambda (k v) 1) assoc table 'foo 'bar)
  273. (equal? #f
  274. (hashx-ref (lambda (k v) 2) assoc table 'foo))))
  275. (pass-if (let ((table (make-hash-table)))
  276. (hashx-set! hash assoc table 'foo 'bar)
  277. (equal? #f
  278. (hashx-ref hash (lambda (k al) #f) table 'foo))))
  279. (pass-if-exception
  280. "hashx-set! (lambda (k s) 1) equal? table 'foo 'bar"
  281. exception:wrong-type-arg ;; there must be a better exception than that...
  282. (hashx-set! (lambda (k s) 1) (lambda (k al) #t) (make-hash-table) 'foo 'bar))
  283. )
  284. ;;;
  285. ;;; hashx-remove!
  286. ;;;
  287. (with-test-prefix "hashx-remove!"
  288. (pass-if (->bool (object-documentation hashx-remove!)))
  289. (pass-if (let ((table (make-hash-table)))
  290. (hashx-set! hashq assq table 'x 123)
  291. (hashx-remove! hashq assq table 'x)
  292. (null? (hash-map->list noop table)))))
  293. ;;;
  294. ;;; hashx
  295. ;;;
  296. (with-test-prefix "hashx"
  297. (pass-if-exception
  298. "hashx-set! (lambda (k s) 1) (lambda (k al) #t) table 'foo 'bar"
  299. exception:wrong-type-arg
  300. (hashx-set! (lambda (k s) 1) (lambda (k al) #t) (make-hash-table) 'foo 'bar))
  301. )
  302. ;;;
  303. ;;; hash-count
  304. ;;;
  305. (with-test-prefix "hash-count"
  306. (let ((table (make-hash-table)))
  307. (hashq-set! table 'foo "bar")
  308. (hashq-set! table 'braz "zonk")
  309. (hashq-create-handle! table 'frob #f)
  310. (pass-if (equal? 3 (hash-count (const #t) table)))
  311. (pass-if (equal? 2 (hash-count (lambda (k v)
  312. (string? v)) table)))))
  313. ;;;
  314. ;;; weak key hash table
  315. ;;;
  316. (with-test-prefix "weak key hash table"
  317. (pass-if "hash-for-each after gc"
  318. (let ((table (make-weak-key-hash-table)))
  319. (hashq-set! table (list 'foo) 'bar)
  320. (gc)
  321. ;; Iterate over deleted weak ref without crashing.
  322. (unspecified? (hash-for-each (lambda (key value) key) table)))))