test/test_map.cpp

changeset 561
bb17790af41e
parent 556
3d19cae7e924
child 562
fd3368c20413
--- a/test/test_map.cpp	Fri May 27 14:02:27 2022 +0200
+++ b/test/test_map.cpp	Fri May 27 14:14:55 2022 +0200
@@ -76,6 +76,7 @@
             // we use that our test keys contain NULL-terminated strings
             keys.insert(std::string(reinterpret_cast<const char *>(elem->data)));
         }
+        EXPECT_EQ(keyiter.index, map->size);
         ASSERT_EQ(keys.size(), map->size);
         for (auto &&k: keys) {
             EXPECT_NE(refmap.find(k), refmap.end());
@@ -89,6 +90,7 @@
         cx_foreach(char const*, elem, valiter) {
             values.insert(std::string(elem));
         }
+        EXPECT_EQ(valiter.index, map->size);
         ASSERT_EQ(values.size(), map->size);
         for (auto &&v: values) {
             EXPECT_NE(std::find_if(refmap.begin(), refmap.end(),
@@ -103,6 +105,7 @@
         cx_foreach(CxMapEntry*, entry, pairiter) {
             pairs[std::string((char const *) entry->key->data)] = std::string((char *) entry->value);
         }
+        EXPECT_EQ(pairiter.index, map->size);
         ASSERT_EQ(pairs.size(), refmap.size());
         for (auto &&p: pairs) {
             ASSERT_EQ(p.second, refmap.at(p.first));
@@ -141,7 +144,7 @@
 
     // execute operations and verify results
     for (auto &&op: ops) {
-        CxDataPtr key = {reinterpret_cast<const unsigned char *>(op.key), 1 + strlen(op.key)};
+        CxDataPtr key = cxMapKeyStr(op.key);
         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);
@@ -168,3 +171,32 @@
     cxMapDestroy(map);
     EXPECT_TRUE(allocator.verify());
 }
+
+TEST(CxHashMap, RemoveViaIterator) {
+    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");
+
+    auto iter = cxMapIterator(map);
+    cx_foreach(CxMapEntry*, entry, iter) {
+        if (entry->key->data[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);
+
+    cxMapDestroy(map);
+    EXPECT_TRUE(allocator.verify());
+}

mercurial