add tests for JSON create functions and fix a bug in cxJsonCreateCxString() default tip

Thu, 26 Dec 2024 20:33:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 26 Dec 2024 20:33:06 +0100
changeset 1057
4e8436c3e806
parent 1056
e180bd389fbc

add tests for JSON create functions and fix a bug in cxJsonCreateCxString()

resolves issue #527

src/json.c file | annotate | diff | comparison | revisions
tests/test_json.c file | annotate | diff | comparison | revisions
--- a/src/json.c	Thu Dec 26 19:26:37 2024 +0100
+++ b/src/json.c	Thu Dec 26 20:33:06 2024 +0100
@@ -714,6 +714,7 @@
     v->type = CX_JSON_STRING;
     cxmutstr s = cx_strdup_a(allocator, str);
     if (s.ptr == NULL) { cxFree(allocator, v); return NULL; }
+    v->value.string = s;
     return v;
 }
 
--- a/tests/test_json.c	Thu Dec 26 19:26:37 2024 +0100
+++ b/tests/test_json.c	Thu Dec 26 20:33:06 2024 +0100
@@ -618,6 +618,100 @@
     cx_testing_allocator_destroy(&talloc);
 }
 
+CX_TEST(test_json_create_value) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *allocator = &talloc.base;
+    CX_TEST_DO {
+        /*
+         * This is the value we want to create in this test:
+         *
+         * {
+         *    "bool": false,
+         *    "int": 47,
+         *    "strings": [ "hello", "world" ],
+         *    "nested": {
+         *       "string": "test",
+         *       "floats": [ 3.1415, 47.11, 8.15 ],
+         *       "ints": [ 4, 8, 15, 16, 23, 42 ],
+         *       "literals": [ true, null, false ]
+         *    }
+         * }
+         */
+
+
+        // create the object
+        CxJsonValue *obj = cxJsonCreateObj(allocator);
+        CX_TEST_ASSERT(obj != NULL);
+        CX_TEST_ASSERT(cxJsonIsObject(obj));
+        CX_TEST_ASSERT(obj->allocator == allocator);
+
+        // add the members
+        {
+            cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE);
+            cxJsonObjPutInteger(obj, CX_STR("int"), 47);
+            CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings"));
+            CX_TEST_ASSERT(strings != NULL);
+            CX_TEST_ASSERT(cxJsonIsArray(strings));
+            const char* str[] = {"hello", "world"};
+            CX_TEST_ASSERT(0 == cxJsonArrAddStrings(strings, str, 2));
+
+            CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested"));
+            CX_TEST_ASSERT(nested != NULL);
+            CX_TEST_ASSERT(cxJsonIsObject(nested));
+            cxJsonObjPutCxString(nested, CX_STR("string"), CX_STR("test"));
+
+            cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")),
+                (double[]){3.1415, 47.11, 8.15}, 3);
+            cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")),
+                (int64_t[]){4, 8, 15, 16, 23, 42}, 6);
+            cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")),
+                (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3);
+        }
+
+        // verify the contents
+        {
+            CX_TEST_ASSERT(cxJsonIsFalse(cxJsonObjGet(obj, "bool")));
+            CX_TEST_ASSERT(47 == cxJsonAsInteger(cxJsonObjGet(obj, "int")));
+            CxJsonValue *strings = cxJsonObjGet(obj, "strings");
+            CX_TEST_ASSERT(cxJsonIsArray(strings));
+            CX_TEST_ASSERT(2 == cxJsonArrSize(strings));
+            CX_TEST_ASSERT(0 == cx_strcmp(CX_STR("hello"), cxJsonAsCxString(cxJsonArrGet(strings, 0))));
+            CX_TEST_ASSERT(0 == cx_strcmp(CX_STR("world"), cxJsonAsCxString(cxJsonArrGet(strings, 1))));
+
+            CxJsonValue *nested = cxJsonObjGet(obj, "nested");
+            CX_TEST_ASSERT(cxJsonIsObject(nested));
+            CX_TEST_ASSERT(0 == strcmp("test", cxJsonAsString(cxJsonObjGet(nested, "string"))));
+            CxJsonValue *floats = cxJsonObjGet(nested, "floats");
+            CX_TEST_ASSERT(cxJsonIsArray(floats));
+            CX_TEST_ASSERT(3 == cxJsonArrSize(floats));
+            CX_TEST_ASSERT(3.1415 == cxJsonAsDouble(cxJsonArrGet(floats, 0)));
+            CX_TEST_ASSERT(47.11 == cxJsonAsDouble(cxJsonArrGet(floats, 1)));
+            CX_TEST_ASSERT(8.15 == cxJsonAsDouble(cxJsonArrGet(floats, 2)));
+            CxJsonValue *ints = cxJsonObjGet(nested, "ints");
+            CX_TEST_ASSERT(cxJsonIsArray(ints));
+            CX_TEST_ASSERT(6 == cxJsonArrSize(ints));
+            CX_TEST_ASSERT(4 == cxJsonAsInteger(cxJsonArrGet(ints, 0)));
+            CX_TEST_ASSERT(8 == cxJsonAsInteger(cxJsonArrGet(ints, 1)));
+            CX_TEST_ASSERT(15 == cxJsonAsInteger(cxJsonArrGet(ints, 2)));
+            CX_TEST_ASSERT(16 == cxJsonAsInteger(cxJsonArrGet(ints, 3)));
+            CX_TEST_ASSERT(23 == cxJsonAsInteger(cxJsonArrGet(ints, 4)));
+            CX_TEST_ASSERT(42 == cxJsonAsInteger(cxJsonArrGet(ints, 5)));
+            CxJsonValue *literals = cxJsonObjGet(nested, "literals");
+            CX_TEST_ASSERT(cxJsonIsArray(literals));
+            CX_TEST_ASSERT(3 == cxJsonArrSize(literals));
+            CX_TEST_ASSERT(cxJsonIsTrue(cxJsonArrGet(literals, 0)));
+            CX_TEST_ASSERT(cxJsonIsNull(cxJsonArrGet(literals, 1)));
+            CX_TEST_ASSERT(cxJsonIsFalse(cxJsonArrGet(literals, 2)));
+        }
+
+        // destroy the value and verify the allocations
+        cxJsonValueFree(obj);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CxTestSuite *cx_test_suite_json(void) {
     CxTestSuite *suite = cx_test_suite_new("json");
 
@@ -635,6 +729,7 @@
     cx_test_register(suite, test_json_array_iterator);
     cx_test_register(suite, test_json_allocator);
     cx_test_register(suite, test_json_allocator_parse_error);
+    cx_test_register(suite, test_json_create_value);
     
     return suite;
 }

mercurial