tests/test_json.c

changeset 1688
27073814f654
parent 1675
36c0fb2b60b2
--- a/tests/test_json.c	Wed Dec 31 12:38:01 2025 +0100
+++ b/tests/test_json.c	Wed Dec 31 12:51:12 2025 +0100
@@ -175,7 +175,7 @@
         CX_TEST_ASSERT(value);
         CX_TEST_ASSERT(cxJsonIsArray(value));
         CX_TEST_ASSERT(value->array.size == 3);
-        for(int i=0;i<3;i++) {
+        for (unsigned i = 0; i < 3; i++) {
             CxJsonValue *v = cxJsonArrGet(value, i);
             CX_TEST_ASSERT(v);
             CX_TEST_ASSERT(cxJsonIsInteger(v));
@@ -797,8 +797,8 @@
         CxJsonStatus result;
         CxJson json;
         CxJsonValue *obj = NULL;
-        
-        for(int i=0;i<5;i++) {
+
+        for (unsigned i = 0; i < cx_nmemb(tests); i++) {
             cxJsonInit(&json, NULL);
             cxJsonFill(&json, tests[i]);
             result = cxJsonNext(&json, &obj);
@@ -1336,7 +1336,9 @@
 }
 
 CX_TEST(test_json_compare_primitives) {
-    CxJsonValue *a[14];
+    CxJsonValue *a[15];
+    CxJsonValue nothing1, nothing2;
+    nothing1.type = nothing2.type = CX_JSON_NOTHING;
     a[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL);
     a[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE);
     a[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE);
@@ -1344,15 +1346,16 @@
     a[4] = cxJsonCreateInteger(NULL, 5432);
     a[5] = cxJsonCreateInteger(NULL, -10);
     a[6] = cxJsonCreateInteger(NULL, 0);
-    a[7] = cxJsonCreateNumber(NULL, 0.0f);
-    a[8] = cxJsonCreateNumber(NULL, 13.37f);
-    a[9] = cxJsonCreateNumber(NULL, -123.456f);
-    a[10] = cxJsonCreateNumber(NULL, 1234.0f);
-    a[11] = cxJsonCreateNumber(NULL, -10.3f);
+    a[7] = cxJsonCreateNumber(NULL, 0.0);
+    a[8] = cxJsonCreateNumber(NULL, 13.37);
+    a[9] = cxJsonCreateNumber(NULL, -123.456);
+    a[10] = cxJsonCreateNumber(NULL, 1234.0);
+    a[11] = cxJsonCreateNumber(NULL, -10.3);
     a[12] = cxJsonCreateString(NULL, "hello");
     a[13] = cxJsonCreateString(NULL, "world");
+    a[14] = &nothing1;
     
-    CxJsonValue *b[14];
+    CxJsonValue *b[15];
     b[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL);
     b[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE);
     b[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE);
@@ -1360,28 +1363,24 @@
     b[4] = cxJsonCreateInteger(NULL, 5432);
     b[5] = cxJsonCreateInteger(NULL, -10);
     b[6] = cxJsonCreateInteger(NULL, 0);
-    b[7] = cxJsonCreateNumber(NULL, 0.0f);
-    b[8] = cxJsonCreateNumber(NULL, 13.37f);
-    b[9] = cxJsonCreateNumber(NULL, -123.456f);
-    b[10] = cxJsonCreateNumber(NULL, 1234.0f);
-    b[11] = cxJsonCreateNumber(NULL, -10.3f);
+    b[7] = cxJsonCreateNumber(NULL, 0.0);
+    b[8] = cxJsonCreateNumber(NULL, 13.37);
+    b[9] = cxJsonCreateNumber(NULL, -123.456);
+    b[10] = cxJsonCreateNumber(NULL, 1234.0);
+    b[11] = cxJsonCreateNumber(NULL, -10.3);
     b[12] = cxJsonCreateString(NULL, "hello");
     b[13] = cxJsonCreateString(NULL, "world");
+    b[14] = &nothing2;
     
     CX_TEST_DO {
-        for(int i=0;i<12;i++) {
-            for(int j=0;j<12;j++) {
+        for (unsigned i = 0; i < cx_nmemb(a); i++) {
+            for (unsigned j = 0; j < cx_nmemb(b); j++) {
                 int ret = cxJsonCompare(a[i], b[j]);
                 if(i == j) {
                     CX_TEST_ASSERT(ret == 0);
                 } else {
                     if(cxJsonIsNumber(a[i]) && cxJsonIsNumber(b[j])) {
-                        double diff = fabs(cxJsonAsDouble(a[i]) - cxJsonAsDouble(b[j]));
-                        if(diff < 0.001) {
-                            CX_TEST_ASSERT(ret == 0);
-                        } else {
-                            CX_TEST_ASSERT(ret != 0);
-                        }
+                        CX_TEST_ASSERT(ret == cx_vcmp_double(cxJsonAsDouble(a[i]), cxJsonAsDouble(b[j])));
                     } else {
                         CX_TEST_ASSERT(ret != 0);
                     }
@@ -1390,8 +1389,10 @@
         }
         
     }
-    for(int i=0;i<14;i++) {
+    for (unsigned i = 0; i < cx_nmemb(a); i++) {
         cxJsonValueFree(a[i]);
+    }
+    for (unsigned i = 0; i < cx_nmemb(b); i++) {
         cxJsonValueFree(b[i]);
     }
 }
@@ -1510,17 +1511,17 @@
     
     CxJsonValue *a[6];
     CxJsonValue *b[6];
-    for(int i=0;i<6;i++) {
+    for (unsigned 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<6;i++) {
+        for (unsigned i = 0; i < cx_nmemb(a); i++) {
             // make sure the test values are arrays
             CX_TEST_ASSERT(cxJsonIsArray(a[i]));
-            
-            for(int j=0;j<6;j++) {
+
+            for (unsigned j = 0; j < cx_nmemb(b); j++) {
                 int ret = cxJsonCompare(a[i], b[j]);
                 CX_TEST_ASSERT(i == j ? ret == 0 : ret != 0);
             }
@@ -1564,8 +1565,10 @@
         cxJsonValueFree(value2);
     }
     
-    for(int i=0;i<6;i++) {
+    for (unsigned i = 0; i < cx_nmemb(a); i++) {
         cxJsonValueFree(a[i]);
+    }
+    for (unsigned i = 0; i < cx_nmemb(b); i++) {
         cxJsonValueFree(b[i]);
     }
 }
@@ -1592,7 +1595,7 @@
         CX_TEST_ASSERT(n != NULL);
         CX_TEST_ASSERT(n->type == CX_JSON_NOTHING);
         
-        for(int i=0;i<14;i++) {
+        for (unsigned i = 0; i < cx_nmemb(a); i++) {
             // make sure the test setup is not broken
             CX_TEST_ASSERT(a[i]->type != CX_JSON_NOTHING);
             
@@ -1621,7 +1624,7 @@
         cxJsonValueFree(nan2);
     }
     
-    for(int i=0;i<14;i++) {
+    for (unsigned i = 0; i < cx_nmemb(a); i++) {
         cxJsonValueFree(a[i]);
     }
 }
@@ -1660,7 +1663,7 @@
     a[9] = cxJsonCreateObj(NULL);
     
     // fill the very large object (a[9])
-    for(int i=0;i<TEST_CLONE_OBJECTS_LARGE_OBJ;i++) {
+    for (unsigned i = 0; i < TEST_CLONE_OBJECTS_LARGE_OBJ; i++) {
         char buf[32];
         snprintf(buf, 32, "int%d", i);
         cxJsonObjPutInteger(a[9], buf, i);
@@ -1668,7 +1671,7 @@
         
         CxJsonValue *arr = cxJsonCreateArr(NULL, TEST_CLONE_OBJECTS_LARGE_ARR);
         int64_t *ints = calloc(TEST_CLONE_OBJECTS_LARGE_ARR, sizeof(int64_t));
-        for(int n=0;n<TEST_CLONE_OBJECTS_LARGE_ARR;n++) {
+        for (unsigned n = 0; n < TEST_CLONE_OBJECTS_LARGE_ARR; n++) {
             ints[i] = n;
         }
         cxJsonArrAddIntegers(arr, ints, TEST_CLONE_OBJECTS_LARGE_ARR);
@@ -1677,7 +1680,7 @@
     }
     
     CX_TEST_DO {
-        for(unsigned i=0;i<cx_nmemb(a);i++) {
+        for (unsigned i = 0; i < cx_nmemb(a); i++) {
             CX_TEST_ASSERT(cxJsonIsObject(a[i]));
             
             CxJsonValue *b = cxJsonClone(a[i], NULL);
@@ -1696,7 +1699,7 @@
         }
     }
     
-    for(int i=0;i<10;i++) {
+    for (unsigned i = 0; i < cx_nmemb(a); i++) {
         cxJsonValueFree(a[i]);
     }
 }
@@ -1711,7 +1714,7 @@
     cxJsonFromString(NULL, "[ { \"array\": [ 1,2,3 ]}  ]", &a[5]);
     
     CX_TEST_DO {
-        for(unsigned i=0;i<cx_nmemb(a);i++) {
+        for (unsigned i = 0; i < cx_nmemb(a); i++) {
             CX_TEST_ASSERT(cxJsonIsArray(a[i]));
             
             CxJsonValue *b = cxJsonClone(a[i], NULL);
@@ -1729,7 +1732,7 @@
         }
     }
     
-    for(int i=0;i<6;i++) {
+    for (unsigned i = 0; i < cx_nmemb(a); i++) {
         cxJsonValueFree(a[i]);
     }
 }

mercurial