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