test/test_map.cpp

changeset 563
69a83fad8a35
parent 562
fd3368c20413
child 594
d90cfa6721f9
--- a/test/test_map.cpp	Fri May 27 17:40:27 2022 +0200
+++ b/test/test_map.cpp	Wed Jun 08 21:33:31 2022 +0200
@@ -72,9 +72,9 @@
     {
         auto keyiter = cxMapIteratorKeys(map);
         std::unordered_set<std::string> keys;
-        cx_foreach(CxDataPtr*, elem, keyiter) {
+        cx_foreach(CxHashKey*, elem, keyiter) {
             // we use that our test keys contain NULL-terminated strings
-            keys.insert(std::string(reinterpret_cast<const char *>(elem->data)));
+            keys.insert(std::string(elem->data.cstr));
         }
         EXPECT_EQ(keyiter.index, map->size);
         ASSERT_EQ(keys.size(), map->size);
@@ -103,7 +103,7 @@
         auto pairiter = cxMapIterator(map);
         std::unordered_map<std::string, std::string> pairs;
         cx_foreach(CxMapEntry*, entry, pairiter) {
-            pairs[std::string((char const *) entry->key->data)] = std::string((char *) entry->value);
+            pairs[std::string(entry->key->data.cstr)] = std::string((char *) entry->value);
         }
         EXPECT_EQ(pairiter.index, map->size);
         ASSERT_EQ(pairs.size(), refmap.size());
@@ -144,7 +144,8 @@
 
     // execute operations and verify results
     for (auto &&op: ops) {
-        CxDataPtr key = cxMapKeyStr(op.key);
+        CxHashKey key = cx_hash_key_str(op.key);
+        key.hash = 0; // force the hash map to compute the hash
         if (op.op == map_operation::put) {
             // execute a put operation and verify that the exact value can be read back
             refmap[std::string(op.key)] = std::string(op.value);
@@ -176,26 +177,26 @@
     CxTestingAllocator allocator;
     auto map = cxHashMapCreate(&allocator, 4);
 
-    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, 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");
 
     auto iter = cxMapIterator(map);
     cx_foreach(CxMapEntry*, entry, iter) {
-        if (entry->key->data[4] % 2 == 1) iter.remove = true;
+        if (entry->key->data.cstr[4] % 2 == 1) iter.remove = true;
     }
     EXPECT_EQ(map->size, 3);
     EXPECT_EQ(iter.index, map->size);
 
-    EXPECT_EQ(cxMapGet(map, cxMapKeyStr("key 1")), nullptr);
-    EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 2")), nullptr);
-    EXPECT_EQ(cxMapGet(map, cxMapKeyStr("key 3")), nullptr);
-    EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 4")), nullptr);
-    EXPECT_EQ(cxMapGet(map, cxMapKeyStr("key 5")), nullptr);
-    EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 6")), nullptr);
+    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);
 
     cxMapDestroy(map);
     EXPECT_TRUE(allocator.verify());
@@ -205,12 +206,12 @@
     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, 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");
 
     // 6/8 does not exceed 0.75, therefore the function should not rehash
     int result = cxMapRehash(map);
@@ -225,26 +226,26 @@
     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");
+    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, cx_hash_key_str("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);
+    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("key 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("key 7")), "val 7"), 0);
 
     cxMapDestroy(map);
     EXPECT_TRUE(allocator.verify());

mercurial