1.1 --- a/tests/test_map.cpp Fri Apr 21 18:38:18 2023 +0200 1.2 +++ b/tests/test_map.cpp Fri Apr 21 19:50:43 2023 +0200 1.3 @@ -30,6 +30,7 @@ 1.4 #include "cx/utils.h" 1.5 #include "cx/string.h" 1.6 #include "util_allocator.h" 1.7 +#include "test_map_generics.h" 1.8 1.9 #include <gtest/gtest.h> 1.10 #include <unordered_map> 1.11 @@ -74,7 +75,7 @@ 1.12 auto keyiter = cxMapIteratorKeys(map); 1.13 std::unordered_set<std::string> keys; 1.14 cx_foreach(CxHashKey*, elem, keyiter) { 1.15 - keys.insert(std::string(reinterpret_cast<char const*>(elem->data), elem->len)); 1.16 + keys.insert(std::string(reinterpret_cast<char const *>(elem->data), elem->len)); 1.17 } 1.18 EXPECT_EQ(keyiter.index, map->size); 1.19 ASSERT_EQ(keys.size(), map->size); 1.20 @@ -103,7 +104,8 @@ 1.21 auto pairiter = cxMapIterator(map); 1.22 std::unordered_map<std::string, std::string> pairs; 1.23 cx_foreach(CxMapEntry*, entry, pairiter) { 1.24 - pairs[std::string(reinterpret_cast<char const*>(entry->key->data), entry->key->len)] = std::string((char *) entry->value); 1.25 + pairs[std::string(reinterpret_cast<char const *>(entry->key->data), entry->key->len)] = std::string( 1.26 + (char *) entry->value); 1.27 } 1.28 EXPECT_EQ(pairiter.index, map->size); 1.29 ASSERT_EQ(pairs.size(), refmap.size()); 1.30 @@ -205,26 +207,26 @@ 1.31 CxTestingAllocator allocator; 1.32 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4); 1.33 1.34 - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 1.35 - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 1.36 - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); 1.37 - cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4"); 1.38 - cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); 1.39 - cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); 1.40 + cxMapPut(map, "key 1", (void *) "val 1"); 1.41 + cxMapPut(map, "key 2", (void *) "val 2"); 1.42 + cxMapPut(map, "key 3", (void *) "val 3"); 1.43 + cxMapPut(map, "key 4", (void *) "val 4"); 1.44 + cxMapPut(map, "key 5", (void *) "val 5"); 1.45 + cxMapPut(map, "key 6", (void *) "val 6"); 1.46 1.47 auto iter = cxMapMutIterator(map); 1.48 cx_foreach(CxMapEntry*, entry, iter) { 1.49 - if (reinterpret_cast<char const*>(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); 1.50 + if (reinterpret_cast<char const *>(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); 1.51 } 1.52 EXPECT_EQ(map->size, 3); 1.53 EXPECT_EQ(iter.index, map->size); 1.54 1.55 - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr); 1.56 - EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 2")), nullptr); 1.57 - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr); 1.58 - EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 4")), nullptr); 1.59 - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 5")), nullptr); 1.60 - EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 6")), nullptr); 1.61 + EXPECT_EQ(cxMapGet(map, "key 1"), nullptr); 1.62 + EXPECT_NE(cxMapGet(map, "key 2"), nullptr); 1.63 + EXPECT_EQ(cxMapGet(map, "key 3"), nullptr); 1.64 + EXPECT_NE(cxMapGet(map, "key 4"), nullptr); 1.65 + EXPECT_EQ(cxMapGet(map, "key 5"), nullptr); 1.66 + EXPECT_NE(cxMapGet(map, "key 6"), nullptr); 1.67 1.68 cxMapDestroy(map); 1.69 EXPECT_TRUE(allocator.verify()); 1.70 @@ -234,12 +236,12 @@ 1.71 CxTestingAllocator allocator; 1.72 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); 1.73 1.74 - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 1.75 - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 1.76 - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); 1.77 - cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4"); 1.78 - cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); 1.79 - cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); 1.80 + cxMapPut(map, "key 1", (void *) "val 1"); 1.81 + cxMapPut(map, "key 2", (void *) "val 2"); 1.82 + cxMapPut(map, "key 3", (void *) "val 3"); 1.83 + cxMapPut(map, "key 4", (void *) "val 4"); 1.84 + cxMapPut(map, "key 5", (void *) "val 5"); 1.85 + cxMapPut(map, "key 6", (void *) "val 6"); 1.86 1.87 // 6/8 does not exceed 0.75, therefore the function should not rehash 1.88 int result = cxMapRehash(map); 1.89 @@ -254,32 +256,32 @@ 1.90 CxTestingAllocator allocator; 1.91 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 7); 1.92 1.93 - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 1.94 - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 1.95 - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); 1.96 - cxMapPut(map, cx_hash_key_str("foo 4"), (void *) "val 4"); 1.97 - cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5"); 1.98 - cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6"); 1.99 - cxMapPut(map, cx_hash_key_str("bar 7"), (void *) "val 7"); 1.100 - cxMapPut(map, cx_hash_key_str("key 8"), (void *) "val 8"); 1.101 - cxMapPut(map, cx_hash_key_str("key 9"), (void *) "val 9"); 1.102 - cxMapPut(map, cx_hash_key_str("key 10"), (void *) "val 10"); 1.103 + cxMapPut(map, "key 1", (void *) "val 1"); 1.104 + cxMapPut(map, "key 2", (void *) "val 2"); 1.105 + cxMapPut(map, "key 3", (void *) "val 3"); 1.106 + cxMapPut(map, "foo 4", (void *) "val 4"); 1.107 + cxMapPut(map, "key 5", (void *) "val 5"); 1.108 + cxMapPut(map, "key 6", (void *) "val 6"); 1.109 + cxMapPut(map, "bar 7", (void *) "val 7"); 1.110 + cxMapPut(map, "key 8", (void *) "val 8"); 1.111 + cxMapPut(map, "key 9", (void *) "val 9"); 1.112 + cxMapPut(map, "key 10", (void *) "val 10"); 1.113 1.114 int result = cxMapRehash(map); 1.115 EXPECT_EQ(result, 0); 1.116 EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 25); 1.117 EXPECT_EQ(map->size, 10); 1.118 1.119 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 1")), "val 1"), 0); 1.120 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 2")), "val 2"), 0); 1.121 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 3")), "val 3"), 0); 1.122 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("foo 4")), "val 4"), 0); 1.123 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 5")), "val 5"), 0); 1.124 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 6")), "val 6"), 0); 1.125 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("bar 7")), "val 7"), 0); 1.126 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 8")), "val 8"), 0); 1.127 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 9")), "val 9"), 0); 1.128 - EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 10")), "val 10"), 0); 1.129 + EXPECT_STREQ((char *) cxMapGet(map, "key 1"), "val 1"); 1.130 + EXPECT_STREQ((char *) cxMapGet(map, "key 2"), "val 2"); 1.131 + EXPECT_STREQ((char *) cxMapGet(map, "key 3"), "val 3"); 1.132 + EXPECT_STREQ((char *) cxMapGet(map, "foo 4"), "val 4"); 1.133 + EXPECT_STREQ((char *) cxMapGet(map, "key 5"), "val 5"); 1.134 + EXPECT_STREQ((char *) cxMapGet(map, "key 6"), "val 6"); 1.135 + EXPECT_STREQ((char *) cxMapGet(map, "bar 7"), "val 7"); 1.136 + EXPECT_STREQ((char *) cxMapGet(map, "key 8"), "val 8"); 1.137 + EXPECT_STREQ((char *) cxMapGet(map, "key 9"), "val 9"); 1.138 + EXPECT_STREQ((char *) cxMapGet(map, "key 10"), "val 10"); 1.139 1.140 cxMapDestroy(map); 1.141 EXPECT_TRUE(allocator.verify()); 1.142 @@ -289,18 +291,18 @@ 1.143 CxTestingAllocator allocator; 1.144 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); 1.145 1.146 - cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 1.147 - cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 1.148 - cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); 1.149 + cxMapPut(map, "key 1", (void *) "val 1"); 1.150 + cxMapPut(map, "key 2", (void *) "val 2"); 1.151 + cxMapPut(map, "key 3", (void *) "val 3"); 1.152 1.153 EXPECT_EQ(map->size, 3); 1.154 1.155 cxMapClear(map); 1.156 1.157 EXPECT_EQ(map->size, 0); 1.158 - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr); 1.159 - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 2")), nullptr); 1.160 - EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr); 1.161 + EXPECT_EQ(cxMapGet(map, "key 1"), nullptr); 1.162 + EXPECT_EQ(cxMapGet(map, "key 2"), nullptr); 1.163 + EXPECT_EQ(cxMapGet(map, "key 3"), nullptr); 1.164 1.165 cxMapDestroy(map); 1.166 EXPECT_TRUE(allocator.verify()); 1.167 @@ -319,22 +321,22 @@ 1.168 auto s5 = CX_STR("setup"); 1.169 1.170 // put them into the map 1.171 - cxMapPut(map, cx_hash_key_str("s1"), &s1); 1.172 - cxMapPut(map, cx_hash_key_str("s2"), &s2); 1.173 - cxMapPut(map, cx_hash_key_str("s3"), &s3); 1.174 - cxMapPut(map, cx_hash_key_str("s4"), &s4); 1.175 + cxMapPut(map, "s1", &s1); 1.176 + cxMapPut(map, "s2", &s2); 1.177 + cxMapPut(map, "s3", &s3); 1.178 + cxMapPut(map, "s4", &s4); 1.179 1.180 // overwrite a value 1.181 - cxMapPut(map, cx_hash_key_str("s1"), &s5); 1.182 + cxMapPut(map, "s1", &s5); 1.183 1.184 // look up a string 1.185 - auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, cx_hash_key_str("s3"))); 1.186 + auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, "s3")); 1.187 EXPECT_EQ(s3p->length, s3.length); 1.188 EXPECT_EQ(s3p->ptr, s3.ptr); 1.189 EXPECT_NE(s3p, &s3); 1.190 1.191 // remove a string 1.192 - cxMapRemove(map, cx_hash_key_str("s2")); 1.193 + cxMapRemove(map, "s2"); 1.194 1.195 // iterate 1.196 auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr}; 1.197 @@ -405,7 +407,7 @@ 1.198 { 1.199 auto iter = cxMapMutIteratorKeys(map); 1.200 cx_foreach(CxHashKey*, key, iter) { 1.201 - if (reinterpret_cast<char const*>(key->data)[4] == '1') cxIteratorFlagRemoval(iter); 1.202 + if (reinterpret_cast<char const *>(key->data)[4] == '1') cxIteratorFlagRemoval(iter); 1.203 } 1.204 } 1.205 { 1.206 @@ -447,3 +449,26 @@ 1.207 verify_any_destructor(map); 1.208 EXPECT_TRUE(allocator.verify()); 1.209 } 1.210 + 1.211 +TEST(CxHashMap, Generics) { 1.212 + CxTestingAllocator allocator; 1.213 + auto map = test_map_generics_step_1(&allocator); 1.214 + 1.215 + EXPECT_EQ(map->size, 3); 1.216 + EXPECT_STREQ((char *) cxMapGet(map, "test"), "test"); 1.217 + EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar"); 1.218 + EXPECT_STREQ((char *) cxMapGet(map, "hallo"), "welt"); 1.219 + 1.220 + test_map_generics_step_2(map); 1.221 + 1.222 + EXPECT_EQ(map->size, 2); 1.223 + EXPECT_STREQ((char *) cxMapGet(map, "key"), "value"); 1.224 + EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar"); 1.225 + 1.226 + test_map_generics_step_3(map); 1.227 + 1.228 + EXPECT_EQ(map->size, 0); 1.229 + 1.230 + cxMapDestroy(map); 1.231 + EXPECT_TRUE(allocator.verify()); 1.232 +}