tests/test_json.c

changeset 1337
6dfa1eb58ce3
parent 1247
e30d38e06559
--- a/tests/test_json.c	Sat Jul 19 21:09:07 2025 +0200
+++ b/tests/test_json.c	Thu Aug 14 23:03:01 2025 +0200
@@ -715,6 +715,39 @@
     cxJsonDestroy(&json);
 }
 
+CX_TEST(test_json_array) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    const CxAllocator *allocator = &talloc.base;
+    CX_TEST_DO {
+        CxJsonValue *arr = cxJsonCreateArr(allocator);
+        cxJsonArrAddIntegers(arr, (int64_t[]){ 0, 3, 6, 9, 12, 15}, 6);
+        CX_TEST_ASSERT(cxJsonArrSize(arr) == 6);
+        CxJsonValue *e = cxJsonArrGet(arr, 3);
+        CX_TEST_ASSERT(cxJsonIsNumber(e));
+        CX_TEST_ASSERT(cxJsonAsInteger(e) == 9);
+        CX_TEST_ASSERT(cxJsonAsDouble(e) == 9.0);
+        CX_TEST_ASSERT(cxJsonArrSize(arr) == 6);
+        e = cxJsonArrGet(arr, 6);
+        CX_TEST_ASSERT(!cxJsonIsNumber(e));
+        // also not null, because "nothing" is not null
+        CX_TEST_ASSERT(!cxJsonIsNull(e));
+        CX_TEST_ASSERT(e->type == CX_JSON_NOTHING);
+        CxJsonValue *removed = cxJsonArrRemove(arr, 2);
+        CX_TEST_ASSERT(cxJsonIsNumber(removed));
+        CX_TEST_ASSERT(cxJsonAsInteger(removed) == 6);
+        CX_TEST_ASSERT(cxJsonArrSize(arr) == 5);
+        e = cxJsonArrRemove(arr, 5);
+        CX_TEST_ASSERT(e == NULL);
+        cxJsonValueFree(arr);
+        // the removed element still needs to be freed
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxJsonValueFree(removed);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CX_TEST(test_json_array_iterator) {
     CxJson json;
     cxJsonInit(&json, NULL);
@@ -1199,6 +1232,7 @@
     cx_test_register(suite, test_json_number);
     cx_test_register(suite, test_json_number_format_errors);
     cx_test_register(suite, test_json_multiple_values);
+    cx_test_register(suite, test_json_array);
     cx_test_register(suite, test_json_array_iterator);
     cx_test_register(suite, test_json_allocator);
     cx_test_register(suite, test_json_allocator_parse_error);

mercurial