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