| 1334 } |
1334 } |
| 1335 cx_testing_allocator_destroy(&talloc); |
1335 cx_testing_allocator_destroy(&talloc); |
| 1336 } |
1336 } |
| 1337 |
1337 |
| 1338 CX_TEST(test_json_compare_primitives) { |
1338 CX_TEST(test_json_compare_primitives) { |
| 1339 CxJsonValue *a[14]; |
1339 CxJsonValue *a[15]; |
| |
1340 CxJsonValue nothing1, nothing2; |
| |
1341 nothing1.type = nothing2.type = CX_JSON_NOTHING; |
| 1340 a[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL); |
1342 a[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL); |
| 1341 a[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE); |
1343 a[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE); |
| 1342 a[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE); |
1344 a[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE); |
| 1343 a[3] = cxJsonCreateInteger(NULL, 1234); |
1345 a[3] = cxJsonCreateInteger(NULL, 1234); |
| 1344 a[4] = cxJsonCreateInteger(NULL, 5432); |
1346 a[4] = cxJsonCreateInteger(NULL, 5432); |
| 1345 a[5] = cxJsonCreateInteger(NULL, -10); |
1347 a[5] = cxJsonCreateInteger(NULL, -10); |
| 1346 a[6] = cxJsonCreateInteger(NULL, 0); |
1348 a[6] = cxJsonCreateInteger(NULL, 0); |
| 1347 a[7] = cxJsonCreateNumber(NULL, 0.0f); |
1349 a[7] = cxJsonCreateNumber(NULL, 0.0); |
| 1348 a[8] = cxJsonCreateNumber(NULL, 13.37f); |
1350 a[8] = cxJsonCreateNumber(NULL, 13.37); |
| 1349 a[9] = cxJsonCreateNumber(NULL, -123.456f); |
1351 a[9] = cxJsonCreateNumber(NULL, -123.456); |
| 1350 a[10] = cxJsonCreateNumber(NULL, 1234.0f); |
1352 a[10] = cxJsonCreateNumber(NULL, 1234.0); |
| 1351 a[11] = cxJsonCreateNumber(NULL, -10.3f); |
1353 a[11] = cxJsonCreateNumber(NULL, -10.3); |
| 1352 a[12] = cxJsonCreateString(NULL, "hello"); |
1354 a[12] = cxJsonCreateString(NULL, "hello"); |
| 1353 a[13] = cxJsonCreateString(NULL, "world"); |
1355 a[13] = cxJsonCreateString(NULL, "world"); |
| |
1356 a[14] = ¬hing1; |
| 1354 |
1357 |
| 1355 CxJsonValue *b[14]; |
1358 CxJsonValue *b[15]; |
| 1356 b[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL); |
1359 b[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL); |
| 1357 b[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE); |
1360 b[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE); |
| 1358 b[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE); |
1361 b[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE); |
| 1359 b[3] = cxJsonCreateInteger(NULL, 1234); |
1362 b[3] = cxJsonCreateInteger(NULL, 1234); |
| 1360 b[4] = cxJsonCreateInteger(NULL, 5432); |
1363 b[4] = cxJsonCreateInteger(NULL, 5432); |
| 1361 b[5] = cxJsonCreateInteger(NULL, -10); |
1364 b[5] = cxJsonCreateInteger(NULL, -10); |
| 1362 b[6] = cxJsonCreateInteger(NULL, 0); |
1365 b[6] = cxJsonCreateInteger(NULL, 0); |
| 1363 b[7] = cxJsonCreateNumber(NULL, 0.0f); |
1366 b[7] = cxJsonCreateNumber(NULL, 0.0); |
| 1364 b[8] = cxJsonCreateNumber(NULL, 13.37f); |
1367 b[8] = cxJsonCreateNumber(NULL, 13.37); |
| 1365 b[9] = cxJsonCreateNumber(NULL, -123.456f); |
1368 b[9] = cxJsonCreateNumber(NULL, -123.456); |
| 1366 b[10] = cxJsonCreateNumber(NULL, 1234.0f); |
1369 b[10] = cxJsonCreateNumber(NULL, 1234.0); |
| 1367 b[11] = cxJsonCreateNumber(NULL, -10.3f); |
1370 b[11] = cxJsonCreateNumber(NULL, -10.3); |
| 1368 b[12] = cxJsonCreateString(NULL, "hello"); |
1371 b[12] = cxJsonCreateString(NULL, "hello"); |
| 1369 b[13] = cxJsonCreateString(NULL, "world"); |
1372 b[13] = cxJsonCreateString(NULL, "world"); |
| |
1373 b[14] = ¬hing2; |
| 1370 |
1374 |
| 1371 CX_TEST_DO { |
1375 CX_TEST_DO { |
| 1372 for(int i=0;i<12;i++) { |
1376 for (unsigned i = 0; i < cx_nmemb(a); i++) { |
| 1373 for(int j=0;j<12;j++) { |
1377 for (unsigned j = 0; j < cx_nmemb(b); j++) { |
| 1374 int ret = cxJsonCompare(a[i], b[j]); |
1378 int ret = cxJsonCompare(a[i], b[j]); |
| 1375 if(i == j) { |
1379 if(i == j) { |
| 1376 CX_TEST_ASSERT(ret == 0); |
1380 CX_TEST_ASSERT(ret == 0); |
| 1377 } else { |
1381 } else { |
| 1378 if(cxJsonIsNumber(a[i]) && cxJsonIsNumber(b[j])) { |
1382 if(cxJsonIsNumber(a[i]) && cxJsonIsNumber(b[j])) { |
| 1379 double diff = fabs(cxJsonAsDouble(a[i]) - cxJsonAsDouble(b[j])); |
1383 CX_TEST_ASSERT(ret == cx_vcmp_double(cxJsonAsDouble(a[i]), cxJsonAsDouble(b[j]))); |
| 1380 if(diff < 0.001) { |
|
| 1381 CX_TEST_ASSERT(ret == 0); |
|
| 1382 } else { |
|
| 1383 CX_TEST_ASSERT(ret != 0); |
|
| 1384 } |
|
| 1385 } else { |
1384 } else { |
| 1386 CX_TEST_ASSERT(ret != 0); |
1385 CX_TEST_ASSERT(ret != 0); |
| 1387 } |
1386 } |
| 1388 } |
1387 } |
| 1389 } |
1388 } |
| 1390 } |
1389 } |
| 1391 |
1390 |
| 1392 } |
1391 } |
| 1393 for(int i=0;i<14;i++) { |
1392 for (unsigned i = 0; i < cx_nmemb(a); i++) { |
| 1394 cxJsonValueFree(a[i]); |
1393 cxJsonValueFree(a[i]); |
| |
1394 } |
| |
1395 for (unsigned i = 0; i < cx_nmemb(b); i++) { |
| 1395 cxJsonValueFree(b[i]); |
1396 cxJsonValueFree(b[i]); |
| 1396 } |
1397 } |
| 1397 } |
1398 } |
| 1398 |
1399 |
| 1399 CX_TEST(test_json_compare_objects) { |
1400 CX_TEST(test_json_compare_objects) { |
| 1508 str[4] = "[ 0, 1, \"hello\", true, false, null, {}, {\"a\":\"b\"}]"; |
1509 str[4] = "[ 0, 1, \"hello\", true, false, null, {}, {\"a\":\"b\"}]"; |
| 1509 str[5] = "[ \"test\", [ 1, 2, [ \"sub\", \"sub1\", [{\"end\":null}]]]]"; |
1510 str[5] = "[ \"test\", [ 1, 2, [ \"sub\", \"sub1\", [{\"end\":null}]]]]"; |
| 1510 |
1511 |
| 1511 CxJsonValue *a[6]; |
1512 CxJsonValue *a[6]; |
| 1512 CxJsonValue *b[6]; |
1513 CxJsonValue *b[6]; |
| 1513 for(int i=0;i<6;i++) { |
1514 for (unsigned i = 0; i < 6; i++) { |
| 1514 cxJsonFromString(NULL, str[i], &a[i]); |
1515 cxJsonFromString(NULL, str[i], &a[i]); |
| 1515 cxJsonFromString(NULL, str[i], &b[i]); |
1516 cxJsonFromString(NULL, str[i], &b[i]); |
| 1516 } |
1517 } |
| 1517 |
1518 |
| 1518 CX_TEST_DO { |
1519 CX_TEST_DO { |
| 1519 for(int i=0;i<6;i++) { |
1520 for (unsigned i = 0; i < cx_nmemb(a); i++) { |
| 1520 // make sure the test values are arrays |
1521 // make sure the test values are arrays |
| 1521 CX_TEST_ASSERT(cxJsonIsArray(a[i])); |
1522 CX_TEST_ASSERT(cxJsonIsArray(a[i])); |
| 1522 |
1523 |
| 1523 for(int j=0;j<6;j++) { |
1524 for (unsigned j = 0; j < cx_nmemb(b); j++) { |
| 1524 int ret = cxJsonCompare(a[i], b[j]); |
1525 int ret = cxJsonCompare(a[i], b[j]); |
| 1525 CX_TEST_ASSERT(i == j ? ret == 0 : ret != 0); |
1526 CX_TEST_ASSERT(i == j ? ret == 0 : ret != 0); |
| 1526 } |
1527 } |
| 1527 } |
1528 } |
| 1528 |
1529 |
| 1658 "}", |
1661 "}", |
| 1659 &a[8]); |
1662 &a[8]); |
| 1660 a[9] = cxJsonCreateObj(NULL); |
1663 a[9] = cxJsonCreateObj(NULL); |
| 1661 |
1664 |
| 1662 // fill the very large object (a[9]) |
1665 // fill the very large object (a[9]) |
| 1663 for(int i=0;i<TEST_CLONE_OBJECTS_LARGE_OBJ;i++) { |
1666 for (unsigned i = 0; i < TEST_CLONE_OBJECTS_LARGE_OBJ; i++) { |
| 1664 char buf[32]; |
1667 char buf[32]; |
| 1665 snprintf(buf, 32, "int%d", i); |
1668 snprintf(buf, 32, "int%d", i); |
| 1666 cxJsonObjPutInteger(a[9], buf, i); |
1669 cxJsonObjPutInteger(a[9], buf, i); |
| 1667 |
1670 |
| 1668 |
1671 |
| 1669 CxJsonValue *arr = cxJsonCreateArr(NULL, TEST_CLONE_OBJECTS_LARGE_ARR); |
1672 CxJsonValue *arr = cxJsonCreateArr(NULL, TEST_CLONE_OBJECTS_LARGE_ARR); |
| 1670 int64_t *ints = calloc(TEST_CLONE_OBJECTS_LARGE_ARR, sizeof(int64_t)); |
1673 int64_t *ints = calloc(TEST_CLONE_OBJECTS_LARGE_ARR, sizeof(int64_t)); |
| 1671 for(int n=0;n<TEST_CLONE_OBJECTS_LARGE_ARR;n++) { |
1674 for (unsigned n = 0; n < TEST_CLONE_OBJECTS_LARGE_ARR; n++) { |
| 1672 ints[i] = n; |
1675 ints[i] = n; |
| 1673 } |
1676 } |
| 1674 cxJsonArrAddIntegers(arr, ints, TEST_CLONE_OBJECTS_LARGE_ARR); |
1677 cxJsonArrAddIntegers(arr, ints, TEST_CLONE_OBJECTS_LARGE_ARR); |
| 1675 free(ints); |
1678 free(ints); |
| 1676 cxJsonObjPut(a[9], "array", arr); |
1679 cxJsonObjPut(a[9], "array", arr); |
| 1677 } |
1680 } |
| 1678 |
1681 |
| 1679 CX_TEST_DO { |
1682 CX_TEST_DO { |
| 1680 for(unsigned i=0;i<cx_nmemb(a);i++) { |
1683 for (unsigned i = 0; i < cx_nmemb(a); i++) { |
| 1681 CX_TEST_ASSERT(cxJsonIsObject(a[i])); |
1684 CX_TEST_ASSERT(cxJsonIsObject(a[i])); |
| 1682 |
1685 |
| 1683 CxJsonValue *b = cxJsonClone(a[i], NULL); |
1686 CxJsonValue *b = cxJsonClone(a[i], NULL); |
| 1684 |
1687 |
| 1685 CX_TEST_ASSERT(b != NULL); |
1688 CX_TEST_ASSERT(b != NULL); |
| 1709 cxJsonFromString(NULL, "[[1, [[2, [3, 4, 5]]], true], false]", &a[3]); |
1712 cxJsonFromString(NULL, "[[1, [[2, [3, 4, 5]]], true], false]", &a[3]); |
| 1710 cxJsonFromString(NULL, "[ { \"abc\": 200, \"str\": \"hello\" }, { }, null ]", &a[4]); |
1713 cxJsonFromString(NULL, "[ { \"abc\": 200, \"str\": \"hello\" }, { }, null ]", &a[4]); |
| 1711 cxJsonFromString(NULL, "[ { \"array\": [ 1,2,3 ]} ]", &a[5]); |
1714 cxJsonFromString(NULL, "[ { \"array\": [ 1,2,3 ]} ]", &a[5]); |
| 1712 |
1715 |
| 1713 CX_TEST_DO { |
1716 CX_TEST_DO { |
| 1714 for(unsigned i=0;i<cx_nmemb(a);i++) { |
1717 for (unsigned i = 0; i < cx_nmemb(a); i++) { |
| 1715 CX_TEST_ASSERT(cxJsonIsArray(a[i])); |
1718 CX_TEST_ASSERT(cxJsonIsArray(a[i])); |
| 1716 |
1719 |
| 1717 CxJsonValue *b = cxJsonClone(a[i], NULL); |
1720 CxJsonValue *b = cxJsonClone(a[i], NULL); |
| 1718 CX_TEST_ASSERT(b); |
1721 CX_TEST_ASSERT(b); |
| 1719 CX_TEST_ASSERT(cxJsonIsArray(b)); |
1722 CX_TEST_ASSERT(cxJsonIsArray(b)); |