tests/test_hash_map.c

changeset 994
3603bdf4a78b
parent 993
b642eca4b956
--- a/tests/test_hash_map.c	Tue Nov 26 22:16:27 2024 +0100
+++ b/tests/test_hash_map.c	Wed Nov 27 22:33:30 2024 +0100
@@ -207,8 +207,10 @@
         CX_TEST_ASSERT(s3p !=  &s3);
 
         // remove a string
-        cxMapRemove(map, "s2");
+        cxstring ret = {0};
+        CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "s2", &ret));
         CX_TEST_ASSERT(map->collection.size == 3);
+        CX_TEST_ASSERT(0 == cx_strcmp(ret, cx_str("is")));
 
         // iterate
         bool s3found = false, s4found = false, s5found = false;
@@ -289,9 +291,9 @@
     cxMapPut(map, k3, v3);
     cxMapPut(map, k4, v4);
 
-    cxMapRemove(map, k2);
-    char *r = cxMapRemoveAndGet(map, k3);
-    cxMapDetach(map, k1);
+    CX_TEST_ASSERT(0 == cxMapRemove(map, k2));
+    char *r;
+    CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, k3, &r));
 
     CX_TEST_ASSERT(0 == strcmp(v1, "val 1"));
     CX_TEST_ASSERT(0 == strcmp(v2, "OK"));
@@ -426,7 +428,7 @@
     cx_testing_allocator_init(&talloc);
     CxAllocator *allocator = &talloc.base;
     CX_TEST_DO {
-        CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0);
+        CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
         cxMapPut(map, "test", "test");
         cxMapPut(map, cx_mutstr("foo"), "bar");
         cxMapPut(map, cx_str("hallo"), "welt");
@@ -436,20 +438,23 @@
         CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
         CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt"));
 
-        // note: we don't have a destructor here, so remove and detach are the same
-        cxMapRemove(map, cx_str("test"));
+        CX_TEST_ASSERT(0 == cxMapRemove(map, cx_str("test")));
         const char *hallo = "hallo";
-        cxMapDetach(map, hallo);
+        CX_TEST_ASSERT(0 == cxMapRemove(map, hallo));
         cxMapPut(map, cx_hash_key_str("key"), "value");
 
         CX_TEST_ASSERT(map->collection.size == 2);
         CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value"));
         CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
 
-        void *r;
-        r = cxMapRemoveAndGet(map, "key");
-        r = cxMapRemoveAndGet(map, cx_str("foo"));
-        if (r != NULL) map->collection.size = 47;
+        const char *r1, *r2;
+        CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "key", &r1));
+        CX_TEST_ASSERT(0 == strcmp(r1, "value"));
+        CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, cx_str("foo"), &r2));
+        CX_TEST_ASSERT(0 == strcmp(r2, "bar"));
+        r2 = "nope";
+        CX_TEST_ASSERT(0 != cxMapRemoveAndGet(map, cx_hash_key("notfound",9), &r2));
+        CX_TEST_ASSERT(0 == strcmp(r2, "nope"));
 
         CX_TEST_ASSERT(map->collection.size == 0);
 
@@ -641,8 +646,16 @@
             } else {
                 // execute a remove and verify that the removed element was returned (or NULL)
                 const char *found = test_map_reference_remove(kv.key);
-                void *removed = cxMapRemoveAndGet(map, key);
-                CX_TEST_ASSERT(found == removed);
+                void *removed = (void*) 0x1337;
+                int result = cxMapRemoveAndGet(map, key, &removed);
+                if (found == NULL) {
+                    CX_TEST_ASSERT(0 != result);
+                    CX_TEST_ASSERT(removed == (void*) 0x1337);
+                } else {
+                    CX_TEST_ASSERT(0 == result);
+                    CX_TEST_ASSERT(removed == found);
+                }
+
             }
             // compare the current map state with the reference map
             CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);

mercurial