tests/test_kv_list.c

changeset 1386
748d0d40881e
parent 1384
a2cfbff39e5d
--- a/tests/test_kv_list.c	Sun Sep 21 18:42:18 2025 +0200
+++ b/tests/test_kv_list.c	Sun Sep 21 19:31:30 2025 +0200
@@ -856,16 +856,79 @@
 
         // remove the first element (which was skipped anyway) and try again
         cxListRemove(cxKvListAsList(map), 0);
-        it = cxMapIterator(map);
+        it = cxMapIteratorKeys(map);
         CX_TEST_ASSERT(it.elem_count == 3);
-        CX_TEST_ASSERT(it.elem_size == sizeof(CxMapEntry));
+        CX_TEST_ASSERT(it.elem_size == sizeof(CxHashKey));
 
         CX_TEST_ASSERT(cxIteratorValid(it));
         CX_TEST_ASSERT(it.index == 0);
-        entry = cxIteratorCurrent(it);
-        CX_TEST_ASSERT(*(int*)entry->value == 815);
-        CX_TEST_ASSERT(entry->key->len == 3);
-        CX_TEST_ASSERT(strncmp(entry->key->data, "xyz", 3) == 0);
+        CxHashKey *key = cxIteratorCurrent(it);
+        CX_TEST_ASSERT(key->len == 3);
+        CX_TEST_ASSERT(strncmp(key->data, "xyz", 3) == 0);
+    }
+    cxMapFree(map);
+}
+
+CX_TEST(test_kv_list_map_iterator_remove) {
+    CxMap *map = cxKvListCreateAsMapSimple(CX_STORE_POINTERS);
+    int x, y, z;
+    CX_TEST_DO {
+        cxDefineDestructor(map, kv_list_test_destr);
+        x = 815;
+        cxMapPut(map, "xyz", &x);
+        y = 8016;
+        cxMapPut(map, "abcd", &y);
+        z = 80017;
+        cxMapPut(map, "efghi", &z);
+
+        kv_list_test_destr_val = 0;
+        CxMapIterator iter = cxMapMutIteratorValues(map);
+        cx_foreach(int *, elem, iter) {
+            if (*elem == 8016) {
+                cxIteratorFlagRemoval(iter);
+            }
+        }
+
+        CX_TEST_ASSERT(kv_list_test_destr_val == 8016);
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+        CX_TEST_ASSERT(cxMapGet(map, "abcd") == NULL);
+        CxList *list = cxKvListAsList(map);
+        CX_TEST_ASSERT(cxListSize(list) == 2);
+        CX_TEST_ASSERT(*(int*)cxListAt(list, 0) == 815);
+        CX_TEST_ASSERT(*(int*)cxListAt(list, 1) == 80017);
+    }
+    cxMapFree(map);
+}
+
+CX_TEST(test_kv_list_list_iterator_remove) {
+    CxMap *map = cxKvListCreateAsMapSimple(sizeof(int));
+    CX_TEST_DO {
+        cxDefineAdvancedDestructor(map, kv_list_test_destr2, (void*)0xf00);
+        int x;
+        x = 815;
+        cxMapPut(map, "xyz", &x);
+        x = 8016;
+        cxMapPut(map, "abcd", &x);
+        x = 80017;
+        cxMapPut(map, "efghi", &x);
+
+        CxList *list = cxKvListAsList(map);
+        kv_list_test_destr2_val = 0;
+        kv_list_test_destr2_extra = NULL;
+        CxIterator iter = cxListMutIterator(list);
+        cx_foreach(int *, elem, iter) {
+            if (*elem == 8016) {
+                cxIteratorFlagRemoval(iter);
+            }
+        }
+
+        CX_TEST_ASSERT(kv_list_test_destr2_val == 8016);
+        CX_TEST_ASSERT(kv_list_test_destr2_extra == (void*)0xf00);
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+        CX_TEST_ASSERT(cxMapGet(map, "abcd") == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 2);
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 815);
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "efghi") == 80017);
     }
     cxMapFree(map);
 }
@@ -907,6 +970,8 @@
     cx_test_register(suite, test_kv_list_map_clear_destr_in_map);
     cx_test_register(suite, test_kv_list_destr_ptr);
     cx_test_register(suite, test_kv_list_map_iterator);
+    cx_test_register(suite, test_kv_list_map_iterator_remove);
+    cx_test_register(suite, test_kv_list_list_iterator_remove);
 
     return suite;
 }

mercurial