tests/test_kv_list.c

changeset 1384
a2cfbff39e5d
parent 1383
3db28cb1e5ec
--- a/tests/test_kv_list.c	Sat Sep 20 12:30:07 2025 +0200
+++ b/tests/test_kv_list.c	Sat Sep 20 18:34:15 2025 +0200
@@ -808,6 +808,68 @@
     cxMapFree(map);
 }
 
+CX_TEST(test_kv_list_map_iterator) {
+    CxMap *map = cxKvListCreateAsMapSimple(sizeof(int));
+    CX_TEST_DO {
+        int x;
+        x = 0xc0ffee; // this element shall be skipped
+        cxListAdd(cxKvListAsList(map), &x);
+        x = 815;
+        cxMapPut(map, "xyz", &x);
+        x = 8016;
+        cxMapPut(map, "abcd", &x);
+        x = 0xbeef;
+        cxListAdd(cxKvListAsList(map), &x);
+        x = 80017;
+        cxMapPut(map, "efghi", &x);
+
+        const CxMapEntry *entry;
+        CxMapIterator it = cxMapIterator(map);
+        CX_TEST_ASSERT(it.elem_count == 3);
+        CX_TEST_ASSERT(it.elem_size == sizeof(CxMapEntry));
+
+        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);
+
+        cxIteratorNext(it);
+        CX_TEST_ASSERT(cxIteratorValid(it));
+        CX_TEST_ASSERT(it.index == 1);
+        entry = cxIteratorCurrent(it);
+        CX_TEST_ASSERT(*(int*)entry->value == 8016);
+        CX_TEST_ASSERT(entry->key->len == 4);
+        CX_TEST_ASSERT(strncmp(entry->key->data, "abcd", 4) == 0);
+
+        cxIteratorNext(it);
+        CX_TEST_ASSERT(cxIteratorValid(it));
+        CX_TEST_ASSERT(it.index == 2);
+        entry = cxIteratorCurrent(it);
+        CX_TEST_ASSERT(*(int*)entry->value == 80017);
+        CX_TEST_ASSERT(entry->key->len == 5);
+        CX_TEST_ASSERT(strncmp(entry->key->data, "efghi", 5) == 0);
+
+        cxIteratorNext(it);
+        CX_TEST_ASSERT(!cxIteratorValid(it));
+
+        // remove the first element (which was skipped anyway) and try again
+        cxListRemove(cxKvListAsList(map), 0);
+        it = cxMapIterator(map);
+        CX_TEST_ASSERT(it.elem_count == 3);
+        CX_TEST_ASSERT(it.elem_size == sizeof(CxMapEntry));
+
+        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);
+    }
+    cxMapFree(map);
+}
+
 CxTestSuite *cx_test_suite_kv_list_specifics(void) {
     CxTestSuite *suite = cx_test_suite_new("kv_list specifics");
 
@@ -844,6 +906,7 @@
     cx_test_register(suite, test_kv_list_map_clear_destr_in_list);
     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);
 
     return suite;
 }

mercurial