hash.test 9.4 KB

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