--- a/test/test_map.cpp Fri May 27 14:14:55 2022 +0200 +++ b/test/test_map.cpp Fri May 27 17:40:27 2022 +0200 @@ -200,3 +200,52 @@ cxMapDestroy(map); EXPECT_TRUE(allocator.verify()); } + +TEST(CxHashMap, RehashNotRequired) { + CxTestingAllocator allocator; + auto map = cxHashMapCreate(&allocator, 8); + + cxMapPut(map, cxMapKeyStr("key 1"), (void *) "val 1"); + cxMapPut(map, cxMapKeyStr("key 2"), (void *) "val 2"); + cxMapPut(map, cxMapKeyStr("key 3"), (void *) "val 3"); + cxMapPut(map, cxMapKeyStr("key 4"), (void *) "val 4"); + cxMapPut(map, cxMapKeyStr("key 5"), (void *) "val 5"); + cxMapPut(map, cxMapKeyStr("key 6"), (void *) "val 6"); + + // 6/8 does not exceed 0.75, therefore the function should not rehash + int result = cxMapRehash(map); + EXPECT_EQ(result, 0); + EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 8); + + cxMapDestroy(map); + EXPECT_TRUE(allocator.verify()); +} + +TEST(CxHashMap, Rehash) { + CxTestingAllocator allocator; + auto map = cxHashMapCreate(&allocator, 8); + + cxMapPut(map, cxMapKeyStr("key 1"), (void *) "val 1"); + cxMapPut(map, cxMapKeyStr("key 2"), (void *) "val 2"); + cxMapPut(map, cxMapKeyStr("key 3"), (void *) "val 3"); + cxMapPut(map, cxMapKeyStr("key 4"), (void *) "val 4"); + cxMapPut(map, cxMapKeyStr("key 5"), (void *) "val 5"); + cxMapPut(map, cxMapKeyStr("key 6"), (void *) "val 6"); + cxMapPut(map, cxMapKeyStr("key 7"), (void *) "val 7"); + + int result = cxMapRehash(map); + EXPECT_EQ(result, 0); + EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 17); + EXPECT_EQ(map->size, 7); + + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 1")), "val 1"), 0); + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 2")), "val 2"), 0); + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 3")), "val 3"), 0); + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 4")), "val 4"), 0); + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 5")), "val 5"), 0); + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 6")), "val 6"), 0); + EXPECT_EQ(strcmp((char *) cxMapGet(map, cxMapKeyStr("key 7")), "val 7"), 0); + + cxMapDestroy(map); + EXPECT_TRUE(allocator.verify()); +} \ No newline at end of file