tests/test_json.c

changeset 1596
2d2c4ec38133
parent 1595
2e0353a9bc30
child 1597
be79be26b01d
--- a/tests/test_json.c	Sun Dec 14 11:05:03 2025 +0100
+++ b/tests/test_json.c	Sun Dec 14 11:45:43 2025 +0100
@@ -125,7 +125,7 @@
         cxJsonObjPutString(obj, "mystring", "test");
         char buf[10];
         for (unsigned i = 0 ; i < 300 ; i++) {
-            sprintf(buf, "key %d", i);
+            snprintf(buf, 10, "key %d", i);
             cxJsonObjPutInteger(obj, buf, i);
         }
         CX_TEST_ASSERT(301 == cxJsonObjSize(obj));
@@ -145,6 +145,89 @@
     cxJsonValueFree(obj);
 }
 
+CX_TEST(test_json_simple_array) {
+    cxstring empty_array = cx_str("[]");
+    cxstring int_array = cx_str("[ 0, 1, 2 ]");
+    cxstring str_array = cx_str("[ \"str1\", \"str2\" ]");
+    cxstring mixed_array = cx_str("[ true, false, 12, { \"a\": \"b\" }, [ 1,2,3,4] ]");
+    
+    CxJsonValue *value;
+    CxJsonStatus result;
+    CxJson json0, json1, json2, json3;
+    
+    CX_TEST_DO {
+        // empty array
+        cxJsonInit(&json0, NULL);
+        cxJsonFill(&json0, empty_array);
+        result = cxJsonNext(&json0, &value);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(value);
+        CX_TEST_ASSERT(cxJsonIsArray(value));
+        
+        cxJsonValueFree(value);
+        cxJsonDestroy(&json0);
+        
+        // int array
+        cxJsonInit(&json1, NULL);
+        cxJsonFill(&json1, int_array);
+        result = cxJsonNext(&json1, &value);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(value);
+        CX_TEST_ASSERT(cxJsonIsArray(value));
+        CX_TEST_ASSERT(value->array.data_size == 3);
+        for(int i=0;i<3;i++) {
+            CxJsonValue *v = cxJsonArrGet(value, i);
+            CX_TEST_ASSERT(v);
+            CX_TEST_ASSERT(cxJsonIsInteger(v));
+            CX_TEST_ASSERT(v->integer == i);
+        }
+        
+        cxJsonValueFree(value);
+        cxJsonDestroy(&json1);
+        
+        // str array
+        cxJsonInit(&json2, NULL);
+        cxJsonFill(&json2, str_array);
+        result = cxJsonNext(&json2, &value);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(value);
+        CX_TEST_ASSERT(cxJsonIsArray(value));
+        CX_TEST_ASSERT(value->array.data_size == 2);
+        CxJsonValue *s0 = cxJsonArrGet(value, 0);
+        CxJsonValue *s1 = cxJsonArrGet(value, 1);
+        CX_TEST_ASSERT(s0 && s1);
+        CX_TEST_ASSERT(cxJsonIsString(s0) && cxJsonIsString(s1));
+        CX_TEST_ASSERT(cx_strcmp(s0->string, "str1") == 0);
+        CX_TEST_ASSERT(cx_strcmp(s1->string, "str2") == 0);
+        
+        cxJsonValueFree(value);
+        cxJsonDestroy(&json2);
+        
+        // mixed array
+        cxJsonInit(&json3, NULL);
+        cxJsonFill(&json3, mixed_array);
+        result = cxJsonNext(&json3, &value);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(value);
+        CX_TEST_ASSERT(cxJsonIsArray(value));
+        CX_TEST_ASSERT(value->array.data_size == 5);
+        CxJsonValue *m0 = cxJsonArrGet(value, 0);
+        CxJsonValue *m1 = cxJsonArrGet(value, 1);
+        CxJsonValue *m2 = cxJsonArrGet(value, 2);
+        CxJsonValue *m3 = cxJsonArrGet(value, 3);
+        CxJsonValue *m4 = cxJsonArrGet(value, 4);
+        CX_TEST_ASSERT(m0 && m1 && m2 && m3 && m4);
+        CX_TEST_ASSERT(cxJsonIsLiteral(m0));
+        CX_TEST_ASSERT(cxJsonIsLiteral(m1));
+        CX_TEST_ASSERT(cxJsonIsInteger(m2));
+        CX_TEST_ASSERT(cxJsonIsObject(m3));
+        CX_TEST_ASSERT(cxJsonIsArray(m4));
+        
+        cxJsonValueFree(value);
+        cxJsonDestroy(&json3);
+    }
+}
+
 CX_TEST(test_json_from_string) {
     cxstring text = cx_str(
             "{\n"
@@ -1416,29 +1499,34 @@
 }
 
 CX_TEST(test_json_compare_arrays) {
-    char *str[4];
+    char *str[6];
     str[0] = "[]";
-    str[1] = "[ true, false ]";
-    str[2] = "[ 0, 1, \"hello\", true, false, null, {}, {\"a\":\"b\"}]";
-    str[3] = "[ \"test\", [ 1, 2, [ \"sub\", \"sub1\", [{\"end\":null}]]]]";
+    str[1] = "[[]]";
+    str[2] = "[[ {} ], { \"a\": [[[[ {} ], null]]]} ]";
+    str[3] = "[ true, false ]";
+    str[4] = "[ 0, 1, \"hello\", true, false, null, {}, {\"a\":\"b\"}]";
+    str[5] = "[ \"test\", [ 1, 2, [ \"sub\", \"sub1\", [{\"end\":null}]]]]";
     
-    CxJsonValue *a[4];
-    CxJsonValue *b[4];
-    for(int i=0;i<4;i++) {
+    CxJsonValue *a[6];
+    CxJsonValue *b[6];
+    for(int i=0;i<6;i++) {
         cxJsonFromString(NULL, str[i], &a[i]);
         cxJsonFromString(NULL, str[i], &b[i]);
     }
     
     CX_TEST_DO {
-        for(int i=0;i<4;i++) {
-            for(int j=0;j<4;j++) {
+        for(int i=0;i<6;i++) {
+            // make sure the test values are arrays
+            CX_TEST_ASSERT(cxJsonIsArray(a[i]));
+            
+            for(int j=0;j<6;j++) {
                 int ret = cxJsonCompare(a[i], b[j]);
                 CX_TEST_ASSERT(i == j ? ret == 0 : ret != 0);
             }
         }
     }
     
-    for(int i=0;i<4;i++) {
+    for(int i=0;i<6;i++) {
         cxJsonValueFree(a[i]);
         cxJsonValueFree(b[i]);
     }
@@ -1989,6 +2077,7 @@
     cx_test_register(suite, test_json_init_default);
     cx_test_register(suite, test_json_simple_object);
     cx_test_register(suite, test_json_large_object);
+    cx_test_register(suite, test_json_simple_array);
     cx_test_register(suite, test_json_from_string);
     cx_test_register(suite, test_json_from_string_errors);
     cx_test_register(suite, test_json_from_string_multiple_values);

mercurial