| 1 |
;; |
| 2 |
;; test for dbm package |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use scheme.list) |
| 7 |
(use file.util) |
| 8 |
(use gauche.collection) |
| 9 |
(use gauche.dictionary) |
| 10 |
|
| 11 |
(test-start "dbm") |
| 12 |
|
| 13 |
(use dbm) |
| 14 |
(test-module 'dbm) |
| 15 |
|
| 16 |
;; |
| 17 |
;; Common test suite |
| 18 |
;; |
| 19 |
|
| 20 |
(define-syntax catch |
| 21 |
(syntax-rules () |
| 22 |
[(_ body ...) |
| 23 |
(guard (e [else #t]) |
| 24 |
body ... #f)])) |
| 25 |
|
| 26 |
(define *test-dbm* "test.dbm") |
| 27 |
(define *test2-dbm* "test2.dbm") |
| 28 |
(define *current-dbm* #f) |
| 29 |
|
| 30 |
;; prepare dataset |
| 31 |
(define *test1-dataset* (make-hash-table 'equal?)) ;string only |
| 32 |
(define *test2-dataset* (make-hash-table 'equal?)) ;other objects |
| 33 |
|
| 34 |
(define (generate-test-set size) |
| 35 |
(do ((i 0 (+ i 1)) |
| 36 |
(key (sys-random) (sys-random)) |
| 37 |
(val (cons (sys-random) (sys-random)) (cons (sys-random) (sys-random)))) |
| 38 |
((>= i size)) |
| 39 |
(hash-table-put! *test1-dataset* |
| 40 |
(x->string key) |
| 41 |
(x->string val)) |
| 42 |
(hash-table-put! *test2-dataset* key val))) |
| 43 |
|
| 44 |
(generate-test-set 1000) |
| 45 |
|
| 46 |
;; create test |
| 47 |
(define (test:make class rw-mode serializer) |
| 48 |
(set! *current-dbm* |
| 49 |
(dbm-open class |
| 50 |
:path *test-dbm* :rw-mode rw-mode |
| 51 |
:key-convert serializer |
| 52 |
:value-convert serializer)) |
| 53 |
#t) |
| 54 |
|
| 55 |
(define (test:db-exists? class) |
| 56 |
(dbm-db-exists? class *test-dbm*)) |
| 57 |
|
| 58 |
;; put everything to the database |
| 59 |
(define (test:put! dataset) |
| 60 |
(hash-table-for-each dataset |
| 61 |
(^[k v] (dbm-put! *current-dbm* k v))) |
| 62 |
#t) |
| 63 |
|
| 64 |
;; does database has all of them? |
| 65 |
(define (test:get dataset) |
| 66 |
(let/cc return |
| 67 |
(hash-table-for-each dataset |
| 68 |
(^[k v] |
| 69 |
(unless (dbm-exists? *current-dbm* k) |
| 70 |
(return #f)) |
| 71 |
(unless (equal? v (dbm-get *current-dbm* k)) |
| 72 |
(return #f)))) |
| 73 |
#t)) |
| 74 |
|
| 75 |
;; does database properly deal with exceptional case? |
| 76 |
(define (test:get-exceptional) |
| 77 |
(and |
| 78 |
;; must raise an error |
| 79 |
(catch (dbm-get *current-dbm* "this_is_not_a_key")) |
| 80 |
;; use default |
| 81 |
(dbm-get *current-dbm* "this_is_not_a_key" #t))) |
| 82 |
|
| 83 |
;; does for-each and map do a right thing? |
| 84 |
(define (test:for-each dataset) |
| 85 |
(let/cc break |
| 86 |
(let1 r '() |
| 87 |
(dbm-for-each *current-dbm* |
| 88 |
(^[k v] |
| 89 |
(unless (equal? v (hash-table-get dataset k #f)) |
| 90 |
(break #f)) |
| 91 |
(set! r (cons v r)))) |
| 92 |
(equal? (reverse r) (dbm-map *current-dbm* (^[k v] v)))))) |
| 93 |
|
| 94 |
;; does collection framework work? |
| 95 |
(define (test:collection-read dataset) |
| 96 |
(let/cc break |
| 97 |
(for-each (^[entry] |
| 98 |
(unless (equal? (hash-table-get dataset (car entry)) |
| 99 |
(cdr entry)) |
| 100 |
(break #f))) |
| 101 |
*current-dbm*) |
| 102 |
#t)) |
| 103 |
|
| 104 |
(define (test:collection-coerce dataset) |
| 105 |
(lset= equal? (hash-table->alist dataset) |
| 106 |
(coerce-to <list> *current-dbm*))) |
| 107 |
|
| 108 |
;; does dictionary framework work? |
| 109 |
(define (test:dict-for-each dataset) |
| 110 |
(let/cc break |
| 111 |
(dict-for-each *current-dbm* |
| 112 |
(^[k v] |
| 113 |
(unless (equal? (hash-table-get dataset k) v) |
| 114 |
(break #f)))) |
| 115 |
#t)) |
| 116 |
|
| 117 |
(define (test:dict-map dataset) |
| 118 |
(every (^p (equal? (hash-table-get dataset (car p)) (cdr p))) |
| 119 |
(dict-map *current-dbm* cons))) |
| 120 |
|
| 121 |
(define (test:dict-keys dataset) |
| 122 |
(every (cut hash-table-exists? dataset <>) (dict-keys *current-dbm*))) |
| 123 |
|
| 124 |
(define (test:dict-values dataset) |
| 125 |
(and (every (cute member <> (hash-table-values dataset)) |
| 126 |
(dict-values *current-dbm*)) |
| 127 |
#t)) |
| 128 |
|
| 129 |
;; does delete work? |
| 130 |
(define (test:delete dataset) |
| 131 |
(let/cc return |
| 132 |
(hash-table-for-each |
| 133 |
dataset |
| 134 |
(^[k v] |
| 135 |
(unless (and (dbm-exists? *current-dbm* k) |
| 136 |
(begin (dbm-delete! *current-dbm* k) |
| 137 |
(not (dbm-exists? *current-dbm* k)))) |
| 138 |
(return #f)))) |
| 139 |
#t)) |
| 140 |
|
| 141 |
;; does read-only work? |
| 142 |
(define (test:read-only) |
| 143 |
;; if db is read-only, following procedures must throw an error. |
| 144 |
(and (catch (dbm-put! *current-dbm* "" "")) |
| 145 |
(catch (dbm-delete! *current-dbm* "")))) |
| 146 |
|
| 147 |
;; does copy work? |
| 148 |
(define (test:copy class from to) |
| 149 |
(dbm-db-copy class from to) |
| 150 |
(let ([f (dbm-open class :path from :rw-mode :read)] |
| 151 |
[tab (make-hash-table 'equal?)]) |
| 152 |
(dbm-for-each f (cut hash-table-put! tab <> <>)) |
| 153 |
(dbm-close f) |
| 154 |
(let1 t (dbm-open class :path to :rw-mode :read) |
| 155 |
(begin0 |
| 156 |
(every (^k (equal? (dbm-get tab k) (dbm-get t k))) |
| 157 |
(dict-keys tab)) |
| 158 |
(dbm-close t))))) |
| 159 |
|
| 160 |
;; does close work? |
| 161 |
(define (test:close) |
| 162 |
(dbm-close *current-dbm*) |
| 163 |
(and (dbm-closed? *current-dbm*) |
| 164 |
;; following procedures must throw an error. |
| 165 |
(catch (dbm-get *current-dbm* "" #f)) |
| 166 |
(catch (dbm-exists? *current-dbm* "")) |
| 167 |
(catch (dbm-put! *current-dbm* "" "")) |
| 168 |
(catch (dbm-delete! *current-dbm* "")) |
| 169 |
(catch (dbm-for-each *current-dbm* (^ _ #f))) |
| 170 |
(catch (dbm-map *current-dbm* (^ _ #f))))) |
| 171 |
|
| 172 |
;; does db-remove work? |
| 173 |
(define (test:db-remove class name) |
| 174 |
(and (dbm-db-exists? class name) |
| 175 |
(begin (dbm-db-remove class name) |
| 176 |
(not (dbm-db-exists? class name))))) |
| 177 |
|
| 178 |
;; clean up files |
| 179 |
(define (clean-up) |
| 180 |
(define (remover f) |
| 181 |
(remove-files (list f (string-append f ".dir") (string-append f ".pag") |
| 182 |
(string-append f ".db")))) |
| 183 |
(remover *test-dbm*) |
| 184 |
(remover *test2-dbm*)) |
| 185 |
|
| 186 |
;; a series of test per dataset and class |
| 187 |
(define (run-through-test class dataset serializer) |
| 188 |
(define (tag msg) (format #f "~s ~a" class msg)) |
| 189 |
(dynamic-wind |
| 190 |
clean-up |
| 191 |
(^[] |
| 192 |
;; create read/write db |
| 193 |
(test* (tag "db-exists? (pre)") #f (test:db-exists? class)) |
| 194 |
(test* (tag "make") #t (test:make class :create serializer)) |
| 195 |
(test* (tag "db-exists? (post)") #t (test:db-exists? class)) |
| 196 |
;; put stuffs |
| 197 |
(test* (tag "put!") #t (test:put! dataset)) |
| 198 |
;; get stuffs |
| 199 |
(test* (tag "get") #t (test:get dataset)) |
| 200 |
(test* (tag "get-exceptional") #t (test:get-exceptional)) |
| 201 |
;; traverse |
| 202 |
(test* (tag "for-each") #t (test:for-each dataset)) |
| 203 |
(test* (tag "collection-read") #t |
| 204 |
(test:collection-read dataset)) |
| 205 |
(test* (tag "collection-coerce") #t |
| 206 |
(test:collection-coerce dataset)) |
| 207 |
(test* (tag "dict-for-each") #t (test:dict-for-each dataset)) |
| 208 |
(test* (tag "dict-map") #t (test:dict-map dataset)) |
| 209 |
(test* (tag "dict-keys") #t (test:dict-keys dataset)) |
| 210 |
(test* (tag "dict-values") #t (test:dict-values dataset)) |
| 211 |
;; close |
| 212 |
(test* (tag "close") #t (test:close)) |
| 213 |
;; open again with read only |
| 214 |
(test* (tag "read-only open") #t (test:make class :read serializer)) |
| 215 |
;; does it still have stuffs? |
| 216 |
(test* (tag "get again") #t (test:get dataset)) |
| 217 |
;; does it work as read-only? |
| 218 |
(test* (tag "read-only") #t (test:read-only)) |
| 219 |
;; close and open it again |
| 220 |
(test* (tag "close again") #t |
| 221 |
(begin |
| 222 |
(dbm-close *current-dbm*) |
| 223 |
(test:make class :write serializer))) |
| 224 |
;; delete stuffs |
| 225 |
(test* (tag "delete") #t (test:delete dataset)) |
| 226 |
;; close again |
| 227 |
(test* (tag "close again") #t (test:close)) |
| 228 |
;; copy |
| 229 |
(test* (tag "db-copy") #t (test:copy class *test-dbm* *test2-dbm*)) |
| 230 |
;; remove |
| 231 |
(test* (tag "db-remove") #t (test:db-remove class *test2-dbm*)) |
| 232 |
) |
| 233 |
clean-up)) |
| 234 |
|
| 235 |
|
| 236 |
;; Do test for two datasets |
| 237 |
(define (full-test class) |
| 238 |
(test-section (format #f "~a dataset 1" (class-name class))) |
| 239 |
(run-through-test class *test1-dataset* #f) |
| 240 |
(test-section (format #f "~a dataset 2" (class-name class))) |
| 241 |
(run-through-test class *test2-dataset* #t) |
| 242 |
) |
| 243 |
|
| 244 |
;; conditionally test |
| 245 |
(define-macro (test-if-exists file module class) |
| 246 |
(when (file-exists? (string-append file "." (gauche-dso-suffix))) |
| 247 |
`(begin (use ,module) |
| 248 |
(test-module ',module) |
| 249 |
(full-test ,class)) |
| 250 |
)) |
| 251 |
|
| 252 |
;; |
| 253 |
;; FSDBM test |
| 254 |
;; |
| 255 |
|
| 256 |
(use dbm.fsdbm) |
| 257 |
(test-module 'dbm.fsdbm) |
| 258 |
(full-test <fsdbm>) |
| 259 |
|
| 260 |
;; |
| 261 |
;; GDBM test |
| 262 |
;; |
| 263 |
|
| 264 |
(test-if-exists "dbm--gdbm" dbm.gdbm <gdbm>) |
| 265 |
|
| 266 |
;; |
| 267 |
;; NDBM test |
| 268 |
;; |
| 269 |
|
| 270 |
(test-if-exists "dbm--ndbm" dbm.ndbm <ndbm>) |
| 271 |
|
| 272 |
;; |
| 273 |
;; DBM test |
| 274 |
;; |
| 275 |
|
| 276 |
(test-if-exists "dbm--odbm" dbm.odbm <odbm>) |
| 277 |
|
| 278 |
(test-end) |