Debian Codegraph @

test/dict.scm

raw
1 ;;
2 ;; test dictionary framework
3 ;;
4
5 (use gauche.test)
6 (use scheme.list)
7
8 (test-start "dictionary framework")
9 (use gauche.dictionary)
10 (test-module 'gauche.dictionary)
11
12 (define (test-basics d)
13 (dict-put! d 'a 1)
14 (set! (dict-get d 'b) 2)
15 (test* "put/get" 1 (dict-get d 'a))
16 (test* "put/get" 2 (dict-get d 'b))
17 (test* "get nonexistent" (test-error) (dict-get d 'c))
18 (test* "get default" 3 (dict-get d 'c 3))
19 (test* "fold" '((a . 1) (b . 2)) (dict-fold d acons '())
20 (cut lset= equal? <> <>))
21 (test* "seek" '(4 b 2)
22 (dict-seek d (^[k v] (and (eq? k 'b) (* v 2))) (^[] 'huh?) list))
23 (test* "seek" 'huh?
24 (dict-seek d (^[k v] (and (eq? k 'c) (* v 2))) (^[] 'huh?) list))
25 (test* "find" '(a 1) (values->list (dict-find d (^[k v] (eqv? v 1)))))
26 (test* "find" '(#f #f) (values->list (dict-find d (^[k v] (eqv? v 3)))))
27 (test* "find" 'boo (dict-find d (^[k v] (eqv? v 3)) (^[] 'boo)))
28 (test* "any" 'a (dict-any d (^[k v] (and (eqv? v 1) k))))
29 (test* "any" #f (dict-any d (^[k v] (and (eqv? v 3) k))))
30 (test* "exists?" #f (dict-exists? d 'c))
31 (test* "exists?" #t (dict-exists? d 'a))
32 (test* "delete!" #f (begin (dict-delete! d 'a) (dict-exists? d 'a)))
33 )
34
35 (test-section "hash-table as dictionary")
36
37 (test-basics (make-hash-table 'eq?))
38
39 (test-section "tree-map as dictionary")
40
41 (test-basics
42 (make-tree-map eq? (^[a b] (string<? (x->string a) (x->string b)))))
43
44 (test-section "bimap")
45
46 (test-basics (make-bimap (make-hash-table 'eq?) (make-hash-table 'eqv?)))
47
48 (use gauche.collection)
49
50 (let1 bm (make-bimap (make-hash-table 'eq?) (make-hash-table 'eqv?))
51 (bimap-put! bm 'a 1)
52 (bimap-put! bm 'b 2)
53 (test* "right lookup" '(a b)
54 (list (bimap-right-get bm 1)
55 (bimap-right-get bm 2)))
56
57 (bimap-put! bm 'c 3)
58 (bimap-put! bm 'd 1)
59 (test* "left lookup" '(3 1)
60 (list (bimap-left-get bm 'c)
61 (bimap-left-get bm 'd)))
62
63 (test* "override by reverse insertion" #f
64 (bimap-left-get bm 'a #f)) ; must be overridden by right-put! 1 'd
65
66 ;; collection framework
67 (test* "collection protocol" '((b . 2) (c . 3) (d . 1))
68 (fold cons '() bm)
69 (cut lset= equal? <> <>))
70 )
71
72 (let1 bm (make-bimap (make-hash-table 'eqv?) (make-hash-table 'eqv?))
73 (bimap-put! bm 'a 1)
74 (test* "bimap conflict (left)" (test-error)
75 (bimap-put! bm 'a 2 :on-conflict :error))
76 (test* "bimap conflict (right)" (test-error)
77 (bimap-put! bm 'b 1 :on-conflict :error))
78 (test* "bimap conflict (ignore)" 1
79 (begin
80 (bimap-put! bm 'a 3 :on-conflict #f)
81 (bimap-left-get bm 'a))))
82
83 (let1 bm (make-bimap (make-hash-table 'eqv?) (make-hash-table 'eqv?)
84 :on-conflict :error)
85 (bimap-put! bm 'a 1)
86 (test* "bimap conflict default (left)" (test-error)
87 (bimap-put! bm 'a 2))
88 (test* "bimap conflict default (right)" (test-error)
89 (bimap-put! bm 'b 1))
90 (test* "bimap conflict default override (ignore)" 3
91 (begin
92 (bimap-put! bm 'a 3 :on-conflict :supersede)
93 (bimap-left-get bm 'a))))
94
95 (test-section "stacked map")
96
97 (let* ([m0 (alist->hash-table '((a . 0) (b . 1) (c . 2)) 'eq?)]
98 [m1 (alist->hash-table '((a . 10) (d . 11)) 'eq?)]
99 [sm (make-stacked-map m1 m0)])
100 (test* "stacked map comparator" eq-comparator
101 (dict-comparator sm))
102 (test* "stacked map search" '(10 1 2 11 none)
103 (map (cut dict-get sm <> 'none) '(a b c d e)))
104 (test* "stacked map search" (test-error)
105 (dict-get sm 'e))
106 (test* "stacked map exists?" '(#t #t #f)
107 (map (cut dict-exists? sm <>) '(a c e)))
108 (test* "stacked-map put!" '(12 12 2)
109 (begin (dict-put! sm 'c 12)
110 (list (dict-get sm 'c)
111 (dict-get m1 'c)
112 (dict-get m0 'c))))
113 (test* "stacked-map fold" '((b . 1) (a . 10) (d . 11) (c . 12))
114 (sort-by (dict-fold sm acons '()) cdr))
115 (test* "stacked-map delete"'(#f #f #f)
116 (begin (dict-delete! sm 'c)
117 (list (dict-get sm 'c #f)
118 (dict-get m1 'c #f)
119 (dict-get m0 'c #f))))
120
121 (test* "stacked-map push!"
122 '((d . 11) (a . 100) (b . 101) (c . 102) (e . 103))
123 (begin (stacked-map-push! sm (alist->hash-table '((a . 100)
124 (b . 101)
125 (c . 102)
126 (e . 103))))
127 (sort-by (dict->alist sm) cdr)))
128 )
129
130 (test-end)