tests/test_kv_list.c

changeset 1382
f61119884dcd
parent 1380
bf5866e47b15
child 1383
3db28cb1e5ec
--- a/tests/test_kv_list.c	Sat Sep 20 12:19:49 2025 +0200
+++ b/tests/test_kv_list.c	Sat Sep 20 12:27:36 2025 +0200
@@ -424,6 +424,39 @@
     cxListFree(list);
 }
 
+CX_TEST(test_kv_list_set_key_already_exists) {
+    CxList *list = cxKvListCreateSimple(sizeof(int));
+    int x;
+    CX_TEST_DO {
+        x = 47;
+        cxListAdd(list, &x);
+        x = 11;
+        cxListAdd(list, &x);
+        CX_TEST_ASSERT(0 == cxKvListSetKey(list, 1, "xyz"));
+        // already exists
+        CX_TEST_ASSERT(0 != cxKvListSetKey(list, 0, "xyz"));
+
+        CxMap *map = cxKvListAsMap(list);
+
+        CX_TEST_ASSERT(cxMapSize(map) == 1);
+
+        int *y = cxMapGet(map, "xyz");
+        CX_TEST_ASSERT(y != NULL);
+        CX_TEST_ASSERT(*y == 11);
+
+        CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz"));
+        CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 1);
+
+        // now we can assign the key again
+        CX_TEST_ASSERT(0 == cxKvListSetKey(list, 0, "xyz"));
+        y = cxMapGet(map, "xyz");
+        CX_TEST_ASSERT(y != NULL);
+        CX_TEST_ASSERT(*y == 47);
+    }
+    cxListFree(list);
+}
+
 CX_TEST(test_kv_list_remove_key) {
     CxList *list = cxKvListCreateSimple(sizeof(int));
     int x;
@@ -765,6 +798,7 @@
     cx_test_register(suite, test_kv_list_map_remove);
     cx_test_register(suite, test_kv_list_map_remove_and_get);
     cx_test_register(suite, test_kv_list_set_key);
+    cx_test_register(suite, test_kv_list_set_key_already_exists);
     cx_test_register(suite, test_kv_list_remove_key);
     cx_test_register(suite, test_kv_list_insert_with_key);
     cx_test_register(suite, test_kv_list_insert_ptr_with_key);

mercurial