| 1 |
;; Tests for memo-table |
| 2 |
;; memo-table is not for general use, but it is used with built-in type |
| 3 |
;; contructors, so we want to test it in the early stage. |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(test-start "memo-table") |
| 7 |
|
| 8 |
(define make-memo-table (with-module gauche.internal make-memo-table)) |
| 9 |
(define memo-table-putv! (with-module gauche.internal memo-table-putv!)) |
| 10 |
(define memo-table-put! (with-module gauche.internal memo-table-put!)) |
| 11 |
(define memo-table-getv2 (with-module gauche.internal memo-table-getv2)) |
| 12 |
(define memo-table-get2 (with-module gauche.internal memo-table-get2)) |
| 13 |
|
| 14 |
(define (memo-table-getv1 tab x) (values-ref (memo-table-getv2 tab x) 0)) |
| 15 |
(define (memo-table-get1 tab x) (values-ref (memo-table-get2 tab x) 0)) |
| 16 |
(define (memo-table-exists? tab x) (values-ref (memo-table-getv2 tab x) 1)) |
| 17 |
|
| 18 |
(define (test-get-put msg args data) |
| 19 |
(define tab) ; will be set |
| 20 |
(define (expand-data vec) |
| 21 |
(if (> (cadr args) 0) |
| 22 |
(vector->list vec) ; no 'rest' keys |
| 23 |
(apply list* (vector->list vec)))) ; 'rest' keys |
| 24 |
|
| 25 |
;; passing keys in vector |
| 26 |
(test* #"build ~|msg|~args" #t |
| 27 |
(begin (set! tab (apply make-memo-table args)) #t)) |
| 28 |
(test* #"empty ~|msg|~args" #f |
| 29 |
(any (cut memo-table-exists? tab <>) data)) |
| 30 |
(test* #"put 1 ~|msg|~args" 0 |
| 31 |
(begin |
| 32 |
(memo-table-putv! tab (car data) 0) |
| 33 |
(memo-table-getv1 tab (car data)))) |
| 34 |
(test* #"put 1 (rest) ~|msg|~args" #f |
| 35 |
(any (cut memo-table-exists? tab <>) (cdr data))) |
| 36 |
(test* #"put 2 ~|msg|~args" '(0 1) |
| 37 |
(begin |
| 38 |
(memo-table-putv! tab (cadr data) 1) |
| 39 |
(list (memo-table-getv1 tab (car data)) |
| 40 |
(memo-table-getv1 tab (cadr data))))) |
| 41 |
(test* #"put 2 (rest) ~|msg|~args" #f |
| 42 |
(any (cut memo-table-exists? tab <>) (cddr data))) |
| 43 |
(test* #"put all ~|msg|~args" (iota (length data)) |
| 44 |
(begin |
| 45 |
(for-each (^[d v] (memo-table-putv! tab d v)) |
| 46 |
(cddr data) (iota (length (cddr data)) 2)) |
| 47 |
(map (cut memo-table-getv1 tab <>) data))) |
| 48 |
|
| 49 |
;; passing keys in list |
| 50 |
(test* #"build ~|msg|~args" #t |
| 51 |
(begin (set! tab (apply make-memo-table args)) #t)) |
| 52 |
(test* #"put 1 ~|msg|~args" 0 |
| 53 |
(begin |
| 54 |
(memo-table-put! tab (expand-data (car data)) 0) |
| 55 |
(memo-table-get1 tab (expand-data (car data))))) |
| 56 |
(test* #"put 1 (rest) ~|msg|~args" #f |
| 57 |
(any (cut memo-table-exists? tab <>) (cdr data))) |
| 58 |
(test* #"put 2 ~|msg|~args" '(0 1) |
| 59 |
(begin |
| 60 |
(memo-table-put! tab (expand-data (cadr data)) 1) |
| 61 |
(list (memo-table-get1 tab (expand-data (car data))) |
| 62 |
(memo-table-get1 tab (expand-data (cadr data)))))) |
| 63 |
(test* #"put 2 (rest) ~|msg|~args" #f |
| 64 |
(any (cut memo-table-exists? tab <>) (cddr data))) |
| 65 |
(test* #"put all ~|msg|~args" (iota (length data)) |
| 66 |
(begin |
| 67 |
(for-each (^[d v] (memo-table-put! tab (expand-data d) v)) |
| 68 |
(cddr data) (iota (length (cddr data)) 2)) |
| 69 |
(map (^d (memo-table-get1 tab (expand-data d))) data))) |
| 70 |
) |
| 71 |
|
| 72 |
(test-get-put "" '(16 1) |
| 73 |
'(#(a) #(#t) #(4) #("abc") #(4.35))) |
| 74 |
|
| 75 |
(test-get-put "" '(16 2) |
| 76 |
'(#(a b) #(#t #f) #(4 9) #("abc" "def") #(4.35 9.15))) |
| 77 |
|
| 78 |
(test-get-put "" '(16 0) |
| 79 |
'(#((a b)) #((#t)) #((4)) #(("abc" "def")) #((4.35)))) |
| 80 |
|
| 81 |
(test-get-put "" '(16 -1) |
| 82 |
'(#(a (b)) #(#t ()) #(4 (8 7)) #("abc" ("def")) #(4.35 (1.0 -i)))) |
| 83 |
|
| 84 |
|
| 85 |
(test-get-put "extend " '(4 1) |
| 86 |
'(#(a) #(b) #(c) #(d) #(e) #(f) #(g) #(h) #(i) #(j) #(k) |
| 87 |
#(l) #(m) #(n) #(o) #(p) #(q) #(r) #(s) #(t) #(u) #(v))) |
| 88 |
(test-get-put "extend " '(4 -1) |
| 89 |
'(#(a (A)) #(b (B)) #(c (C)) #(d (D)) #(e (E)) #(f (F)) |
| 90 |
#(g (G)) #(h (H)) #(i (I)) #(j (J)) #(k (K)) #(l (L)) |
| 91 |
#(m (M)) #(n (N)) #(o (O)) #(p (P)) #(q (Q)) #(r (R)) |
| 92 |
#(s (S)) #(t (T)) #(u (U)) #(v (V)) #(w (W)) #(x (X)))) |
| 93 |
|
| 94 |
(test-end) |