198 EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 6")), nullptr); |
198 EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 6")), nullptr); |
199 |
199 |
200 cxMapDestroy(map); |
200 cxMapDestroy(map); |
201 EXPECT_TRUE(allocator.verify()); |
201 EXPECT_TRUE(allocator.verify()); |
202 } |
202 } |
|
203 |
|
204 TEST(CxHashMap, RehashNotRequired) { |
|
205 CxTestingAllocator allocator; |
|
206 auto map = cxHashMapCreate(&allocator, 8); |
|
207 |
|
208 cxMapPut(map, cxMapKeyStr("key 1"), (void *) "val 1"); |
|
209 cxMapPut(map, cxMapKeyStr("key 2"), (void *) "val 2"); |
|
210 cxMapPut(map, cxMapKeyStr("key 3"), (void *) "val 3"); |
|
211 cxMapPut(map, cxMapKeyStr("key 4"), (void *) "val 4"); |
|
212 cxMapPut(map, cxMapKeyStr("key 5"), (void *) "val 5"); |
|
213 cxMapPut(map, cxMapKeyStr("key 6"), (void *) "val 6"); |
|
214 |
|
215 // 6/8 does not exceed 0.75, therefore the function should not rehash |
|
216 int result = cxMapRehash(map); |
|
217 EXPECT_EQ(result, 0); |
|
218 EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 8); |
|
219 |
|
220 cxMapDestroy(map); |
|
221 EXPECT_TRUE(allocator.verify()); |
|
222 } |
|
223 |
|
224 TEST(CxHashMap, Rehash) { |
|
225 CxTestingAllocator allocator; |
|
226 auto map = cxHashMapCreate(&allocator, 8); |
|
227 |
|
228 cxMapPut(map, cxMapKeyStr("key 1"), (void *) "val 1"); |
|
229 cxMapPut(map, cxMapKeyStr("key 2"), (void *) "val 2"); |
|
230 cxMapPut(map, cxMapKeyStr("key 3"), (void *) "val 3"); |
|
231 cxMapPut(map, cxMapKeyStr("key 4"), (void *) "val 4"); |
|
232 cxMapPut(map, cxMapKeyStr("key 5"), (void *) "val 5"); |
|
233 cxMapPut(map, cxMapKeyStr("key 6"), (void *) "val 6"); |
|
234 cxMapPut(map, cxMapKeyStr("key 7"), (void *) "val 7"); |
|
235 |
|
236 int result = cxMapRehash(map); |
|
237 EXPECT_EQ(result, 0); |
|
238 EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 17); |
|
239 EXPECT_EQ(map->size, 7); |
|
240 |
|
241 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 1")), "val 1"), 0); |
|
242 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 2")), "val 2"), 0); |
|
243 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 3")), "val 3"), 0); |
|
244 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 4")), "val 4"), 0); |
|
245 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 5")), "val 5"), 0); |
|
246 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 6")), "val 6"), 0); |
|
247 EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 7")), "val 7"), 0); |
|
248 |
|
249 cxMapDestroy(map); |
|
250 EXPECT_TRUE(allocator.verify()); |
|
251 } |