diff -r 2c2304622981 -r 65baf7f45ac8 tests/test_map.cpp --- a/tests/test_map.cpp Fri Apr 21 18:38:18 2023 +0200 +++ b/tests/test_map.cpp Fri Apr 21 19:50:43 2023 +0200 @@ -30,6 +30,7 @@ #include "cx/utils.h" #include "cx/string.h" #include "util_allocator.h" +#include "test_map_generics.h" #include #include @@ -74,7 +75,7 @@ auto keyiter = cxMapIteratorKeys(map); std::unordered_set keys; cx_foreach(CxHashKey*, elem, keyiter) { - keys.insert(std::string(reinterpret_cast(elem->data), elem->len)); + keys.insert(std::string(reinterpret_cast(elem->data), elem->len)); } EXPECT_EQ(keyiter.index, map->size); ASSERT_EQ(keys.size(), map->size); @@ -103,7 +104,8 @@ auto pairiter = cxMapIterator(map); std::unordered_map pairs; cx_foreach(CxMapEntry*, entry, pairiter) { - pairs[std::string(reinterpret_cast(entry->key->data), entry->key->len)] = std::string((char *) entry->value); + pairs[std::string(reinterpret_cast(entry->key->data), entry->key->len)] = std::string( + (char *) entry->value); } EXPECT_EQ(pairiter.index, map->size); ASSERT_EQ(pairs.size(), refmap.size()); @@ -205,26 +207,26 @@ CxTestingAllocator allocator; auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4); - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); - cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4"); - cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); - cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + cxMapPut(map, "key 4", (void *) "val 4"); + cxMapPut(map, "key 5", (void *) "val 5"); + cxMapPut(map, "key 6", (void *) "val 6"); auto iter = cxMapMutIterator(map); cx_foreach(CxMapEntry*, entry, iter) { - if (reinterpret_cast(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); + if (reinterpret_cast(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); } EXPECT_EQ(map->size, 3); EXPECT_EQ(iter.index, map->size); - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr); - EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 2")), nullptr); - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr); - EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 4")), nullptr); - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 5")), nullptr); - EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 6")), nullptr); + EXPECT_EQ(cxMapGet(map, "key 1"), nullptr); + EXPECT_NE(cxMapGet(map, "key 2"), nullptr); + EXPECT_EQ(cxMapGet(map, "key 3"), nullptr); + EXPECT_NE(cxMapGet(map, "key 4"), nullptr); + EXPECT_EQ(cxMapGet(map, "key 5"), nullptr); + EXPECT_NE(cxMapGet(map, "key 6"), nullptr); cxMapDestroy(map); EXPECT_TRUE(allocator.verify()); @@ -234,12 +236,12 @@ CxTestingAllocator allocator; auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); - cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4"); - cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); - cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + cxMapPut(map, "key 4", (void *) "val 4"); + cxMapPut(map, "key 5", (void *) "val 5"); + cxMapPut(map, "key 6", (void *) "val 6"); // 6/8 does not exceed 0.75, therefore the function should not rehash int result = cxMapRehash(map); @@ -254,32 +256,32 @@ CxTestingAllocator allocator; auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 7); - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); - cxMapPut(map, cx_hash_key_str("foo 4"), (void *) "val 4"); - cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); - cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); - cxMapPut(map, cx_hash_key_str("bar 7"), (void *) "val 7"); - cxMapPut(map, cx_hash_key_str("key 8"), (void *) "val 8"); - cxMapPut(map, cx_hash_key_str("key 9"), (void *) "val 9"); - cxMapPut(map, cx_hash_key_str("key 10"), (void *) "val 10"); + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + cxMapPut(map, "foo 4", (void *) "val 4"); + cxMapPut(map, "key 5", (void *) "val 5"); + cxMapPut(map, "key 6", (void *) "val 6"); + cxMapPut(map, "bar 7", (void *) "val 7"); + cxMapPut(map, "key 8", (void *) "val 8"); + cxMapPut(map, "key 9", (void *) "val 9"); + cxMapPut(map, "key 10", (void *) "val 10"); int result = cxMapRehash(map); EXPECT_EQ(result, 0); EXPECT_EQ(reinterpret_cast(map)->bucket_count, 25); EXPECT_EQ(map->size, 10); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 1")), "val 1"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 2")), "val 2"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 3")), "val 3"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("foo 4")), "val 4"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 5")), "val 5"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 6")), "val 6"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("bar 7")), "val 7"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 8")), "val 8"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 9")), "val 9"), 0); - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 10")), "val 10"), 0); + EXPECT_STREQ((char *) cxMapGet(map, "key 1"), "val 1"); + EXPECT_STREQ((char *) cxMapGet(map, "key 2"), "val 2"); + EXPECT_STREQ((char *) cxMapGet(map, "key 3"), "val 3"); + EXPECT_STREQ((char *) cxMapGet(map, "foo 4"), "val 4"); + EXPECT_STREQ((char *) cxMapGet(map, "key 5"), "val 5"); + EXPECT_STREQ((char *) cxMapGet(map, "key 6"), "val 6"); + EXPECT_STREQ((char *) cxMapGet(map, "bar 7"), "val 7"); + EXPECT_STREQ((char *) cxMapGet(map, "key 8"), "val 8"); + EXPECT_STREQ((char *) cxMapGet(map, "key 9"), "val 9"); + EXPECT_STREQ((char *) cxMapGet(map, "key 10"), "val 10"); cxMapDestroy(map); EXPECT_TRUE(allocator.verify()); @@ -289,18 +291,18 @@ CxTestingAllocator allocator; auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); EXPECT_EQ(map->size, 3); cxMapClear(map); EXPECT_EQ(map->size, 0); - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr); - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 2")), nullptr); - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr); + EXPECT_EQ(cxMapGet(map, "key 1"), nullptr); + EXPECT_EQ(cxMapGet(map, "key 2"), nullptr); + EXPECT_EQ(cxMapGet(map, "key 3"), nullptr); cxMapDestroy(map); EXPECT_TRUE(allocator.verify()); @@ -319,22 +321,22 @@ auto s5 = CX_STR("setup"); // put them into the map - cxMapPut(map, cx_hash_key_str("s1"), &s1); - cxMapPut(map, cx_hash_key_str("s2"), &s2); - cxMapPut(map, cx_hash_key_str("s3"), &s3); - cxMapPut(map, cx_hash_key_str("s4"), &s4); + cxMapPut(map, "s1", &s1); + cxMapPut(map, "s2", &s2); + cxMapPut(map, "s3", &s3); + cxMapPut(map, "s4", &s4); // overwrite a value - cxMapPut(map, cx_hash_key_str("s1"), &s5); + cxMapPut(map, "s1", &s5); // look up a string - auto s3p = reinterpret_cast(cxMapGet(map, cx_hash_key_str("s3"))); + auto s3p = reinterpret_cast(cxMapGet(map, "s3")); EXPECT_EQ(s3p->length, s3.length); EXPECT_EQ(s3p->ptr, s3.ptr); EXPECT_NE(s3p, &s3); // remove a string - cxMapRemove(map, cx_hash_key_str("s2")); + cxMapRemove(map, "s2"); // iterate auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr}; @@ -405,7 +407,7 @@ { auto iter = cxMapMutIteratorKeys(map); cx_foreach(CxHashKey*, key, iter) { - if (reinterpret_cast(key->data)[4] == '1') cxIteratorFlagRemoval(iter); + if (reinterpret_cast(key->data)[4] == '1') cxIteratorFlagRemoval(iter); } } { @@ -447,3 +449,26 @@ verify_any_destructor(map); EXPECT_TRUE(allocator.verify()); } + +TEST(CxHashMap, Generics) { + CxTestingAllocator allocator; + auto map = test_map_generics_step_1(&allocator); + + EXPECT_EQ(map->size, 3); + EXPECT_STREQ((char *) cxMapGet(map, "test"), "test"); + EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar"); + EXPECT_STREQ((char *) cxMapGet(map, "hallo"), "welt"); + + test_map_generics_step_2(map); + + EXPECT_EQ(map->size, 2); + EXPECT_STREQ((char *) cxMapGet(map, "key"), "value"); + EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar"); + + test_map_generics_step_3(map); + + EXPECT_EQ(map->size, 0); + + cxMapDestroy(map); + EXPECT_TRUE(allocator.verify()); +}