| 1 |
;;; r6rs-hashtables.test --- Test suite for R6RS (rnrs hashtables) |
| 2 |
|
| 3 |
;; Copyright (C) 2010 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 Lice6nse, 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 |
|
| 19 |
|
| 20 |
(define-module (test-suite test-rnrs-hashtable) |
| 21 |
:use-module (ice-9 receive) |
| 22 |
:use-module ((rnrs hashtables) :version (6)) |
| 23 |
:use-module ((rnrs exceptions) :version (6)) |
| 24 |
:use-module (srfi srfi-1) |
| 25 |
:use-module (test-suite lib)) |
| 26 |
|
| 27 |
(with-test-prefix "make-eq-hashtable" |
| 28 |
(pass-if "eq hashtable compares keys with eq?" |
| 29 |
(let ((eq-hashtable (make-eq-hashtable))) |
| 30 |
(hashtable-set! eq-hashtable (list 'foo) #t) |
| 31 |
(hashtable-set! eq-hashtable 'sym #t) |
| 32 |
(and (not (hashtable-contains? eq-hashtable (list 'foo))) |
| 33 |
(hashtable-contains? eq-hashtable 'sym))))) |
| 34 |
|
| 35 |
(with-test-prefix "make-eqv-hashtable" |
| 36 |
(pass-if "eqv hashtable compares keys with eqv?" |
| 37 |
(let ((eqv-hashtable (make-eqv-hashtable))) |
| 38 |
(hashtable-set! eqv-hashtable (list 'foo) #t) |
| 39 |
(hashtable-set! eqv-hashtable 4 #t) |
| 40 |
(and (not (hashtable-contains? eqv-hashtable (list 'foo))) |
| 41 |
(hashtable-contains? eqv-hashtable 4))))) |
| 42 |
|
| 43 |
(with-test-prefix "make-hashtable" |
| 44 |
(pass-if "hashtable compares keys with custom equality function" |
| 45 |
(let* ((abs-eqv? (lambda (x y) (eqv? (abs x) (abs y)))) |
| 46 |
(abs-hashtable (make-hashtable abs abs-eqv?))) |
| 47 |
(hashtable-set! abs-hashtable -4 #t) |
| 48 |
(and (not (hashtable-contains? abs-hashtable 6)) |
| 49 |
(hashtable-contains? abs-hashtable 4)))) |
| 50 |
|
| 51 |
(pass-if "hash function value used modulo capacity" |
| 52 |
(let* ((constant-hash (lambda (x) most-positive-fixnum)) |
| 53 |
(constant-hashtable (make-hashtable constant-hash eq?))) |
| 54 |
(hashtable-set! constant-hashtable 'foo 'bar) |
| 55 |
(hashtable-contains? constant-hashtable 'foo)))) |
| 56 |
|
| 57 |
(with-test-prefix "hashtable?" |
| 58 |
(pass-if "hashtable? is #t on hashtables" |
| 59 |
(let ((hashtable (make-eq-hashtable))) |
| 60 |
(hashtable? hashtable))) |
| 61 |
|
| 62 |
(pass-if "hashtable? is #f on non-hashtables" |
| 63 |
(let ((not-hashtable (list))) |
| 64 |
(not (hashtable? not-hashtable))))) |
| 65 |
|
| 66 |
(with-test-prefix "hashtable-size" |
| 67 |
(pass-if "hashtable-size returns current size" |
| 68 |
(let ((hashtable (make-eq-hashtable))) |
| 69 |
(and (eqv? (hashtable-size hashtable) 0) |
| 70 |
(hashtable-set! hashtable 'foo #t) |
| 71 |
(eqv? (hashtable-size hashtable) 1))))) |
| 72 |
|
| 73 |
(with-test-prefix "hashtable-ref" |
| 74 |
(pass-if "hashtable-ref returns value for bound key" |
| 75 |
(let ((hashtable (make-eq-hashtable))) |
| 76 |
(hashtable-set! hashtable 'sym 'foo) |
| 77 |
(eq? (hashtable-ref hashtable 'sym 'bar) 'foo))) |
| 78 |
|
| 79 |
(pass-if "hashtable-ref returns default for unbound key" |
| 80 |
(let ((hashtable (make-eq-hashtable))) |
| 81 |
(eq? (hashtable-ref hashtable 'sym 'bar) 'bar)))) |
| 82 |
|
| 83 |
(with-test-prefix "hashtable-set!" |
| 84 |
(pass-if "hashtable-set! returns unspecified" |
| 85 |
(let ((hashtable (make-eq-hashtable))) |
| 86 |
(unspecified? (hashtable-set! hashtable 'foo 'bar)))) |
| 87 |
|
| 88 |
(pass-if "hashtable-set! allows storing #f" |
| 89 |
(let ((hashtable (make-eq-hashtable))) |
| 90 |
(hashtable-set! hashtable 'foo #f) |
| 91 |
(not (hashtable-ref hashtable 'foo 'bar))))) |
| 92 |
|
| 93 |
(with-test-prefix "hashtable-delete!" |
| 94 |
(pass-if "hashtable-delete! removes association" |
| 95 |
(let ((hashtable (make-eq-hashtable))) |
| 96 |
(hashtable-set! hashtable 'foo 'bar) |
| 97 |
(and (unspecified? (hashtable-delete! hashtable 'foo)) |
| 98 |
(not (hashtable-ref hashtable 'foo #f)))))) |
| 99 |
|
| 100 |
(with-test-prefix "hashtable-contains?" |
| 101 |
(pass-if "hashtable-contains? returns #t when association present" |
| 102 |
(let ((hashtable (make-eq-hashtable))) |
| 103 |
(hashtable-set! hashtable 'foo 'bar) |
| 104 |
(let ((contains (hashtable-contains? hashtable 'foo))) |
| 105 |
(and (boolean? contains) contains)))) |
| 106 |
|
| 107 |
(pass-if "hashtable-contains? returns #f when association not present" |
| 108 |
(let ((hashtable (make-eq-hashtable))) |
| 109 |
(not (hashtable-contains? hashtable 'foo))))) |
| 110 |
|
| 111 |
(with-test-prefix "hashtable-update!" |
| 112 |
(pass-if "hashtable-update! adds return value of proc on bound key" |
| 113 |
(let ((hashtable (make-eq-hashtable))) |
| 114 |
(hashtable-set! hashtable 'foo 0) |
| 115 |
(hashtable-update! hashtable 'foo (lambda (x) (+ x 1)) 100) |
| 116 |
(eqv? (hashtable-ref hashtable 'foo #f) 1))) |
| 117 |
|
| 118 |
(pass-if "hashtable-update! adds default value on unbound key" |
| 119 |
(let ((hashtable (make-eq-hashtable))) |
| 120 |
(hashtable-update! hashtable 'foo (lambda (x) (+ x 1)) 100) |
| 121 |
(eqv? (hashtable-ref hashtable 'foo #f) 101)))) |
| 122 |
|
| 123 |
(with-test-prefix "hashtable-copy" |
| 124 |
(pass-if "hashtable-copy produces copy of hashtable" |
| 125 |
(let ((hashtable (make-eq-hashtable))) |
| 126 |
(hashtable-set! hashtable 'foo 1) |
| 127 |
(hashtable-set! hashtable 'bar 2) |
| 128 |
(let ((copied-table (hashtable-copy hashtable))) |
| 129 |
(and (eqv? (hashtable-ref hashtable 'foo #f) 1) |
| 130 |
(eqv? (hashtable-ref hashtable 'bar #f) 2))))) |
| 131 |
|
| 132 |
(pass-if "hashtable-copy with mutability #f produces immutable copy" |
| 133 |
(let ((copied-table (hashtable-copy (make-eq-hashtable) #f))) |
| 134 |
(guard (exc (else #t)) |
| 135 |
(hashtable-set! copied-table 'foo 1) |
| 136 |
#f)))) |
| 137 |
|
| 138 |
(with-test-prefix "hashtable-clear!" |
| 139 |
(pass-if "hashtable-clear! removes all values from hashtable" |
| 140 |
(let ((hashtable (make-eq-hashtable))) |
| 141 |
(hashtable-set! hashtable 'foo 1) |
| 142 |
(hashtable-set! hashtable 'bar 2) |
| 143 |
(and (unspecified? (hashtable-clear! hashtable)) |
| 144 |
(eqv? (hashtable-size hashtable) 0))))) |
| 145 |
|
| 146 |
(with-test-prefix "hashtable-keys" |
| 147 |
(pass-if "hashtable-keys returns all keys" |
| 148 |
(let ((hashtable (make-eq-hashtable))) |
| 149 |
(hashtable-set! hashtable 'foo #t) |
| 150 |
(hashtable-set! hashtable 'bar #t) |
| 151 |
(let ((keys (vector->list (hashtable-keys hashtable)))) |
| 152 |
(and (memq 'foo keys) (memq 'bar keys) #t))))) |
| 153 |
|
| 154 |
(with-test-prefix "hashtable-entries" |
| 155 |
(pass-if "hashtable-entries returns all entries" |
| 156 |
(let ((hashtable (make-eq-hashtable))) |
| 157 |
(hashtable-set! hashtable 'foo 1) |
| 158 |
(hashtable-set! hashtable 'bar 2) |
| 159 |
(receive |
| 160 |
(keys values) |
| 161 |
(hashtable-entries hashtable) |
| 162 |
(let f ((counter 0) (success #t)) |
| 163 |
(if (or (not success) (= counter 2)) |
| 164 |
success |
| 165 |
(case (vector-ref keys counter) |
| 166 |
((foo) (f (+ counter 1) (eqv? (vector-ref values counter) 1))) |
| 167 |
((bar) (f (+ counter 1) (eqv? (vector-ref values counter) 2))) |
| 168 |
(else f 0 #f)))))))) |
| 169 |
|
| 170 |
(with-test-prefix "hashtable-equivalence-function" |
| 171 |
(pass-if "hashtable-equivalence-function returns eqv function" |
| 172 |
(let* ((abs-eqv? (lambda (x y) (eqv? (abs x) (abs y)))) |
| 173 |
(abs-hashtable (make-hashtable abs abs-eqv?))) |
| 174 |
(eq? (hashtable-equivalence-function abs-hashtable) abs-eqv?)))) |
| 175 |
|
| 176 |
(with-test-prefix "hashtable-hash-function" |
| 177 |
(pass-if "hashtable-hash-function returns hash function" |
| 178 |
(let ((abs-hashtable (make-hashtable abs eqv?))) |
| 179 |
(eq? (hashtable-hash-function abs-hashtable) abs))) |
| 180 |
(pass-if "hashtable-hash-function returns #f on eq table" |
| 181 |
(eq? #f (hashtable-hash-function (make-eq-hashtable)))) |
| 182 |
(pass-if "hashtable-hash-function returns #f on eqv table" |
| 183 |
(eq? #f (hashtable-hash-function (make-eqv-hashtable))))) |
| 184 |
|
| 185 |
(with-test-prefix "hashtable-mutable?" |
| 186 |
(pass-if "hashtable-mutable? is #t on mutable hashtables" |
| 187 |
(hashtable-mutable? (hashtable-copy (make-eq-hashtable) #t))) |
| 188 |
|
| 189 |
(pass-if "hashtable-mutable? is #f on immutable hashtables" |
| 190 |
(not (hashtable-mutable? (hashtable-copy (make-eq-hashtable) #f))))) |