#199 test removing via iterator

Fri, 27 May 2022 14:14:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 27 May 2022 14:14:55 +0200
changeset 561
bb17790af41e
parent 560
2d6a3e2dc8ff
child 562
fd3368c20413

#199 test removing via iterator

test/test_map.cpp file | annotate | diff | comparison | revisions
     1.1 --- a/test/test_map.cpp	Fri May 27 14:02:27 2022 +0200
     1.2 +++ b/test/test_map.cpp	Fri May 27 14:14:55 2022 +0200
     1.3 @@ -76,6 +76,7 @@
     1.4              // we use that our test keys contain NULL-terminated strings
     1.5              keys.insert(std::string(reinterpret_cast<const char *>(elem->data)));
     1.6          }
     1.7 +        EXPECT_EQ(keyiter.index, map->size);
     1.8          ASSERT_EQ(keys.size(), map->size);
     1.9          for (auto &&k: keys) {
    1.10              EXPECT_NE(refmap.find(k), refmap.end());
    1.11 @@ -89,6 +90,7 @@
    1.12          cx_foreach(char const*, elem, valiter) {
    1.13              values.insert(std::string(elem));
    1.14          }
    1.15 +        EXPECT_EQ(valiter.index, map->size);
    1.16          ASSERT_EQ(values.size(), map->size);
    1.17          for (auto &&v: values) {
    1.18              EXPECT_NE(std::find_if(refmap.begin(), refmap.end(),
    1.19 @@ -103,6 +105,7 @@
    1.20          cx_foreach(CxMapEntry*, entry, pairiter) {
    1.21              pairs[std::string((char const *) entry->key->data)] = std::string((char *) entry->value);
    1.22          }
    1.23 +        EXPECT_EQ(pairiter.index, map->size);
    1.24          ASSERT_EQ(pairs.size(), refmap.size());
    1.25          for (auto &&p: pairs) {
    1.26              ASSERT_EQ(p.second, refmap.at(p.first));
    1.27 @@ -141,7 +144,7 @@
    1.28  
    1.29      // execute operations and verify results
    1.30      for (auto &&op: ops) {
    1.31 -        CxDataPtr key = {reinterpret_cast<const unsigned char *>(op.key), 1 + strlen(op.key)};
    1.32 +        CxDataPtr key = cxMapKeyStr(op.key);
    1.33          if (op.op == map_operation::put) {
    1.34              // execute a put operation and verify that the exact value can be read back
    1.35              refmap[std::string(op.key)] = std::string(op.value);
    1.36 @@ -168,3 +171,32 @@
    1.37      cxMapDestroy(map);
    1.38      EXPECT_TRUE(allocator.verify());
    1.39  }
    1.40 +
    1.41 +TEST(CxHashMap, RemoveViaIterator) {
    1.42 +    CxTestingAllocator allocator;
    1.43 +    auto map = cxHashMapCreate(&allocator, 4);
    1.44 +
    1.45 +    cxMapPut(map, cxMapKeyStr("key 1"), (void *) "val 1");
    1.46 +    cxMapPut(map, cxMapKeyStr("key 2"), (void *) "val 2");
    1.47 +    cxMapPut(map, cxMapKeyStr("key 3"), (void *) "val 3");
    1.48 +    cxMapPut(map, cxMapKeyStr("key 4"), (void *) "val 4");
    1.49 +    cxMapPut(map, cxMapKeyStr("key 5"), (void *) "val 5");
    1.50 +    cxMapPut(map, cxMapKeyStr("key 6"), (void *) "val 6");
    1.51 +
    1.52 +    auto iter = cxMapIterator(map);
    1.53 +    cx_foreach(CxMapEntry*, entry, iter) {
    1.54 +        if (entry->key->data[4] % 2 == 1) iter.remove = true;
    1.55 +    }
    1.56 +    EXPECT_EQ(map->size, 3);
    1.57 +    EXPECT_EQ(iter.index, map->size);
    1.58 +
    1.59 +    EXPECT_EQ(cxMapGet(map, cxMapKeyStr("key 1")), nullptr);
    1.60 +    EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 2")), nullptr);
    1.61 +    EXPECT_EQ(cxMapGet(map, cxMapKeyStr("key 3")), nullptr);
    1.62 +    EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 4")), nullptr);
    1.63 +    EXPECT_EQ(cxMapGet(map, cxMapKeyStr("key 5")), nullptr);
    1.64 +    EXPECT_NE(cxMapGet(map, cxMapKeyStr("key 6")), nullptr);
    1.65 +
    1.66 +    cxMapDestroy(map);
    1.67 +    EXPECT_TRUE(allocator.verify());
    1.68 +}

mercurial