# HG changeset patch # User Mike Becker # Date 1767181872 -3600 # Node ID 27073814f654a2dd07dd942488c5ff285a2803df # Parent b803081b40ceb12cd3e59ef66839c0d8c4bb9cfd fix JSON tests diff -r b803081b40ce -r 27073814f654 src/json.c --- a/src/json.c Wed Dec 31 12:38:01 2025 +0100 +++ b/src/json.c Wed Dec 31 12:51:12 2025 +0100 @@ -1427,7 +1427,7 @@ CxBuffer buffer; if (cxBufferInit(&buffer, allocator, NULL, 128, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) { - return CX_NULLSTR; + return CX_NULLSTR; // LCOV_EXCL_LINE } if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0) || cxBufferTerminate(&buffer)) { @@ -1485,8 +1485,7 @@ return cx_vcmp_double(json->number, cxJsonAsDouble(other)); case CX_JSON_LITERAL: return json->literal == other->literal ? 0 : -1; - default: - // LCOV_EXCL_START + default: // LCOV_EXCL_START // unreachable assert(false); return -1; @@ -1536,8 +1535,7 @@ arr->array.size = elem_count; for (size_t i = 0 ; i < elem_count ; i++) { CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL); - if (e == NULL) { - // LCOV_EXCL_START + if (e == NULL) { // LCOV_EXCL_START cxJsonValueFree(arr); return NULL; // LCOV_EXCL_STOP @@ -1554,8 +1552,7 @@ return_value(cxJsonCreateNumber(allocator, source->number)); case CX_JSON_LITERAL: return_value(cxJsonCreateLiteral(allocator, source->literal)); - default: - // LCOV_EXCL_START + default: // LCOV_EXCL_START // unreachable assert(false); return NULL; diff -r b803081b40ce -r 27073814f654 tests/test_json.c --- 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] = ¬hing1; - 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] = ¬hing2; 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