1#!/bin/sh
2
3test_description='test hashmap and string hash functions'
4. ./test-lib.sh
5
6test_hashmap() {
7 echo "$1" | test-tool hashmap $3 > actual &&
8 echo "$2" > expect &&
9 test_cmp expect actual
10}
11
12test_expect_success 'put' '
13
14test_hashmap "put key1 value1
15put key2 value2
16put fooBarFrotz value3
17put foobarfrotz value4
18size" "NULL
19NULL
20NULL
21NULL
2264 4"
23
24'
25
26test_expect_success 'put (case insensitive)' '
27
28test_hashmap "put key1 value1
29put key2 value2
30put fooBarFrotz value3
31size" "NULL
32NULL
33NULL
3464 3" ignorecase
35
36'
37
38test_expect_success 'replace' '
39
40test_hashmap "put key1 value1
41put key1 value2
42put fooBarFrotz value3
43put fooBarFrotz value4
44size" "NULL
45value1
46NULL
47value3
4864 2"
49
50'
51
52test_expect_success 'replace (case insensitive)' '
53
54test_hashmap "put key1 value1
55put Key1 value2
56put fooBarFrotz value3
57put foobarfrotz value4
58size" "NULL
59value1
60NULL
61value3
6264 2" ignorecase
63
64'
65
66test_expect_success 'get' '
67
68test_hashmap "put key1 value1
69put key2 value2
70put fooBarFrotz value3
71put foobarfrotz value4
72get key1
73get key2
74get fooBarFrotz
75get notInMap" "NULL
76NULL
77NULL
78NULL
79value1
80value2
81value3
82NULL"
83
84'
85
86test_expect_success 'get (case insensitive)' '
87
88test_hashmap "put key1 value1
89put key2 value2
90put fooBarFrotz value3
91get Key1
92get keY2
93get foobarfrotz
94get notInMap" "NULL
95NULL
96NULL
97value1
98value2
99value3
100NULL" ignorecase
101
102'
103
104test_expect_success 'add' '
105
106test_hashmap "add key1 value1
107add key1 value2
108add fooBarFrotz value3
109add fooBarFrotz value4
110get key1
111get fooBarFrotz
112get notInMap" "value2
113value1
114value4
115value3
116NULL"
117
118'
119
120test_expect_success 'add (case insensitive)' '
121
122test_hashmap "add key1 value1
123add Key1 value2
124add fooBarFrotz value3
125add foobarfrotz value4
126get key1
127get Foobarfrotz
128get notInMap" "value2
129value1
130value4
131value3
132NULL" ignorecase
133
134'
135
136test_expect_success 'remove' '
137
138test_hashmap "put key1 value1
139put key2 value2
140put fooBarFrotz value3
141remove key1
142remove key2
143remove notInMap
144size" "NULL
145NULL
146NULL
147value1
148value2
149NULL
15064 1"
151
152'
153
154test_expect_success 'remove (case insensitive)' '
155
156test_hashmap "put key1 value1
157put key2 value2
158put fooBarFrotz value3
159remove Key1
160remove keY2
161remove notInMap
162size" "NULL
163NULL
164NULL
165value1
166value2
167NULL
16864 1" ignorecase
169
170'
171
172test_expect_success 'iterate' '
173
174test_hashmap "put key1 value1
175put key2 value2
176put fooBarFrotz value3
177iterate" "NULL
178NULL
179NULL
180key2 value2
181key1 value1
182fooBarFrotz value3"
183
184'
185
186test_expect_success 'iterate (case insensitive)' '
187
188test_hashmap "put key1 value1
189put key2 value2
190put fooBarFrotz value3
191iterate" "NULL
192NULL
193NULL
194fooBarFrotz value3
195key2 value2
196key1 value1" ignorecase
197
198'
199
200test_expect_success 'grow / shrink' '
201
202 rm -f in &&
203 rm -f expect &&
204 for n in $(test_seq 51)
205 do
206 echo put key$n value$n >> in &&
207 echo NULL >> expect
208 done &&
209 echo size >> in &&
210 echo 64 51 >> expect &&
211 echo put key52 value52 >> in &&
212 echo NULL >> expect &&
213 echo size >> in &&
214 echo 256 52 >> expect &&
215 for n in $(test_seq 12)
216 do
217 echo remove key$n >> in &&
218 echo value$n >> expect
219 done &&
220 echo size >> in &&
221 echo 256 40 >> expect &&
222 echo remove key40 >> in &&
223 echo value40 >> expect &&
224 echo size >> in &&
225 echo 64 39 >> expect &&
226 cat in | test-tool hashmap > out &&
227 test_cmp expect out
228
229'
230
231test_expect_success 'string interning' '
232
233test_hashmap "intern value1
234intern Value1
235intern value2
236intern value2
237" "value1
238Value1
239value2
240value2"
241
242'
243
244test_done