tests/test_json.c

changeset 1688
27073814f654
parent 1675
36c0fb2b60b2
equal deleted inserted replaced
1687:b803081b40ce 1688:27073814f654
173 result = cxJsonNext(&json1, &value); 173 result = cxJsonNext(&json1, &value);
174 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 174 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
175 CX_TEST_ASSERT(value); 175 CX_TEST_ASSERT(value);
176 CX_TEST_ASSERT(cxJsonIsArray(value)); 176 CX_TEST_ASSERT(cxJsonIsArray(value));
177 CX_TEST_ASSERT(value->array.size == 3); 177 CX_TEST_ASSERT(value->array.size == 3);
178 for(int i=0;i<3;i++) { 178 for (unsigned i = 0; i < 3; i++) {
179 CxJsonValue *v = cxJsonArrGet(value, i); 179 CxJsonValue *v = cxJsonArrGet(value, i);
180 CX_TEST_ASSERT(v); 180 CX_TEST_ASSERT(v);
181 CX_TEST_ASSERT(cxJsonIsInteger(v)); 181 CX_TEST_ASSERT(cxJsonIsInteger(v));
182 CX_TEST_ASSERT(v->integer == i); 182 CX_TEST_ASSERT(v->integer == i);
183 } 183 }
795 795
796 CX_TEST_DO { 796 CX_TEST_DO {
797 CxJsonStatus result; 797 CxJsonStatus result;
798 CxJson json; 798 CxJson json;
799 CxJsonValue *obj = NULL; 799 CxJsonValue *obj = NULL;
800 800
801 for(int i=0;i<5;i++) { 801 for (unsigned i = 0; i < cx_nmemb(tests); i++) {
802 cxJsonInit(&json, NULL); 802 cxJsonInit(&json, NULL);
803 cxJsonFill(&json, tests[i]); 803 cxJsonFill(&json, tests[i]);
804 result = cxJsonNext(&json, &obj); 804 result = cxJsonNext(&json, &obj);
805 805
806 CX_TEST_ASSERT(result == errors[i]); 806 CX_TEST_ASSERT(result == errors[i]);
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] = &nothing1;
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] = &nothing2;
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
1562 1563
1563 cxJsonValueFree(value); 1564 cxJsonValueFree(value);
1564 cxJsonValueFree(value2); 1565 cxJsonValueFree(value2);
1565 } 1566 }
1566 1567
1567 for(int i=0;i<6;i++) { 1568 for (unsigned i = 0; i < cx_nmemb(a); i++) {
1568 cxJsonValueFree(a[i]); 1569 cxJsonValueFree(a[i]);
1570 }
1571 for (unsigned i = 0; i < cx_nmemb(b); i++) {
1569 cxJsonValueFree(b[i]); 1572 cxJsonValueFree(b[i]);
1570 } 1573 }
1571 } 1574 }
1572 1575
1573 CX_TEST(test_json_clone_primitives) { 1576 CX_TEST(test_json_clone_primitives) {
1590 CX_TEST_DO { 1593 CX_TEST_DO {
1591 CxJsonValue *n = cxJsonClone(NULL, NULL); 1594 CxJsonValue *n = cxJsonClone(NULL, NULL);
1592 CX_TEST_ASSERT(n != NULL); 1595 CX_TEST_ASSERT(n != NULL);
1593 CX_TEST_ASSERT(n->type == CX_JSON_NOTHING); 1596 CX_TEST_ASSERT(n->type == CX_JSON_NOTHING);
1594 1597
1595 for(int i=0;i<14;i++) { 1598 for (unsigned i = 0; i < cx_nmemb(a); i++) {
1596 // make sure the test setup is not broken 1599 // make sure the test setup is not broken
1597 CX_TEST_ASSERT(a[i]->type != CX_JSON_NOTHING); 1600 CX_TEST_ASSERT(a[i]->type != CX_JSON_NOTHING);
1598 1601
1599 CxJsonValue *b = cxJsonClone(a[i], NULL); 1602 CxJsonValue *b = cxJsonClone(a[i], NULL);
1600 CX_TEST_ASSERT(b != NULL); 1603 CX_TEST_ASSERT(b != NULL);
1619 1622
1620 cxJsonValueFree(nan1); 1623 cxJsonValueFree(nan1);
1621 cxJsonValueFree(nan2); 1624 cxJsonValueFree(nan2);
1622 } 1625 }
1623 1626
1624 for(int i=0;i<14;i++) { 1627 for (unsigned i = 0; i < cx_nmemb(a); i++) {
1625 cxJsonValueFree(a[i]); 1628 cxJsonValueFree(a[i]);
1626 } 1629 }
1627 } 1630 }
1628 1631
1629 #ifndef TEST_CLONE_OBJECTS_LARGE_OBJ 1632 #ifndef TEST_CLONE_OBJECTS_LARGE_OBJ
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);
1694 1697
1695 cxJsonValueFree(b); 1698 cxJsonValueFree(b);
1696 } 1699 }
1697 } 1700 }
1698 1701
1699 for(int i=0;i<10;i++) { 1702 for (unsigned i = 0; i < cx_nmemb(a); i++) {
1700 cxJsonValueFree(a[i]); 1703 cxJsonValueFree(a[i]);
1701 } 1704 }
1702 } 1705 }
1703 1706
1704 CX_TEST(test_json_clone_arrays) { 1707 CX_TEST(test_json_clone_arrays) {
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));
1727 1730
1728 cxJsonValueFree(b); 1731 cxJsonValueFree(b);
1729 } 1732 }
1730 } 1733 }
1731 1734
1732 for(int i=0;i<6;i++) { 1735 for (unsigned i = 0; i < cx_nmemb(a); i++) {
1733 cxJsonValueFree(a[i]); 1736 cxJsonValueFree(a[i]);
1734 } 1737 }
1735 } 1738 }
1736 1739
1737 1740

mercurial