tests/test_kv_list.c

changeset 1373
a6aaa77b6809
parent 1372
9c176073e771
--- a/tests/test_kv_list.c	Thu Sep 11 20:17:43 2025 +0200
+++ b/tests/test_kv_list.c	Fri Sep 12 16:56:04 2025 +0200
@@ -69,6 +69,153 @@
     cx_testing_allocator_destroy(&talloc);
 }
 
+CX_TEST(test_kv_list_remove) {
+    CxList *list = cxKvListCreateSimple(sizeof(int));
+    int x;
+    CX_TEST_DO {
+        CxMap *map = cxKvListAsMap(list);
+
+        x = 13;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x));
+        x = 37;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "abc", &x));
+        x = 47;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "efg", &x));
+        x = 90;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "hij", &x));
+
+        CX_TEST_ASSERT(cxMapSize(map) == 4);
+        CX_TEST_ASSERT(cxListSize(list) == 4);
+
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "efg") == 47);
+        CX_TEST_ASSERT(0 == cxListRemove(list, 2));
+        CX_TEST_ASSERT(cxListSize(list) == 3);
+        CX_TEST_ASSERT(cxMapSize(map) == 3);
+        CX_TEST_ASSERT(cxMapGet(map, "efg") == NULL);
+
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 13);
+        CX_TEST_ASSERT(0 == cxListRemove(list, 0));
+        CX_TEST_ASSERT(cxListSize(list) == 2);
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+        CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL);
+
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "hij") == 90);
+        CX_TEST_ASSERT(0 == cxListRemove(list, 1));
+        CX_TEST_ASSERT(cxListSize(list) == 1);
+        CX_TEST_ASSERT(cxMapSize(map) == 1);
+        CX_TEST_ASSERT(cxMapGet(map, "hij") == NULL);
+    }
+    cxListFree(list);
+}
+
+CX_TEST(test_kv_list_remove_and_get) {
+    CxList *list = cxKvListCreateSimple(sizeof(int));
+    int x, y;
+    CX_TEST_DO {
+        CxMap *map = cxKvListAsMap(list);
+
+        x = 13;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x));
+        x = 37;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "abc", &x));
+        x = 47;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "efg", &x));
+        x = 90;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "hij", &x));
+
+        CX_TEST_ASSERT(cxMapSize(map) == 4);
+        CX_TEST_ASSERT(cxListSize(list) == 4);
+
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "efg") == 47);
+        CX_TEST_ASSERT(0 == cxListRemoveAndGet(list, 2, &y));
+        CX_TEST_ASSERT(y == 47);
+        CX_TEST_ASSERT(cxListSize(list) == 3);
+        CX_TEST_ASSERT(cxMapSize(map) == 3);
+        CX_TEST_ASSERT(cxMapGet(map, "efg") == NULL);
+
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 13);
+        CX_TEST_ASSERT(0 == cxListRemoveAndGet(list, 0, &y));
+        CX_TEST_ASSERT(y == 13);
+        CX_TEST_ASSERT(cxListSize(list) == 2);
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+        CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL);
+
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "hij") == 90);
+        CX_TEST_ASSERT(0 == cxListRemoveAndGet(list, 1, &y));
+        CX_TEST_ASSERT(y == 90);
+        CX_TEST_ASSERT(cxListSize(list) == 1);
+        CX_TEST_ASSERT(cxMapSize(map) == 1);
+        CX_TEST_ASSERT(cxMapGet(map, "hij") == NULL);
+    }
+    cxListFree(list);
+}
+
+CX_TEST(test_kv_list_remove_array) {
+    CxList *list = cxKvListCreateSimple(sizeof(int));
+    int x;
+    CX_TEST_DO {
+        CxMap *map = cxKvListAsMap(list);
+
+        x = 13;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x));
+        x = 37;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "abc", &x));
+        x = 47;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "efg", &x));
+        x = 90;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "hij", &x));
+
+        CX_TEST_ASSERT(cxMapSize(map) == 4);
+        CX_TEST_ASSERT(cxListSize(list) == 4);
+
+        CX_TEST_ASSERT(2 == cxListRemoveArray(list, 1, 2));
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+        CX_TEST_ASSERT(cxListSize(list) == 2);
+        CX_TEST_ASSERT(cxMapGet(map, "abc") == NULL);
+        CX_TEST_ASSERT(cxMapGet(map, "efg") == NULL);
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 13);
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "hij") == 90);
+        CX_TEST_ASSERT(cxListAt(list, 0) == cxMapGet(map, "xyz"));
+        CX_TEST_ASSERT(cxListAt(list, 1) == cxMapGet(map, "hij"));
+    }
+    cxListFree(list);
+}
+
+CX_TEST(test_kv_list_remove_array_and_get) {
+    CxList *list = cxKvListCreateSimple(sizeof(int));
+    int x;
+    CX_TEST_DO {
+        CxMap *map = cxKvListAsMap(list);
+
+        x = 13;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x));
+        x = 37;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "abc", &x));
+        x = 47;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "efg", &x));
+        x = 90;
+        CX_TEST_ASSERT(0 == cxMapPut(map, "hij", &x));
+
+        CX_TEST_ASSERT(cxMapSize(map) == 4);
+        CX_TEST_ASSERT(cxListSize(list) == 4);
+
+        int y[2];
+
+        CX_TEST_ASSERT(2 == cxListRemoveArrayAndGet(list, 1, 2, y));
+        CX_TEST_ASSERT(y[0] == 37);
+        CX_TEST_ASSERT(y[1] == 47);
+        CX_TEST_ASSERT(cxMapSize(map) == 2);
+        CX_TEST_ASSERT(cxListSize(list) == 2);
+        CX_TEST_ASSERT(cxMapGet(map, "abc") == NULL);
+        CX_TEST_ASSERT(cxMapGet(map, "efg") == NULL);
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 13);
+        CX_TEST_ASSERT(*(int*)cxMapGet(map, "hij") == 90);
+        CX_TEST_ASSERT(cxListAt(list, 0) == cxMapGet(map, "xyz"));
+        CX_TEST_ASSERT(cxListAt(list, 1) == cxMapGet(map, "hij"));
+    }
+    cxListFree(list);
+}
+
 CX_TEST(test_kv_list_map_put) {
     CxList *list = cxKvListCreateSimple(sizeof(int));
     int x;
@@ -331,6 +478,10 @@
     cx_test_register(suite, test_kv_list_map_as_list);
     cx_test_register(suite, test_kv_list_free_as_map);
     cx_test_register(suite, test_kv_list_free_as_list);
+    cx_test_register(suite, test_kv_list_remove);
+    cx_test_register(suite, test_kv_list_remove_and_get);
+    cx_test_register(suite, test_kv_list_remove_array);
+    cx_test_register(suite, test_kv_list_remove_array_and_get);
     cx_test_register(suite, test_kv_list_map_put);
     cx_test_register(suite, test_kv_list_map_put_ptr);
     cx_test_register(suite, test_kv_list_map_remove);

mercurial