tests/test_json.c

changeset 1586
7f1cadc3ebc1
parent 1582
32b82c424252
child 1588
5c4ee11417c1
equal deleted inserted replaced
1585:8ccfbc00f690 1586:7f1cadc3ebc1
29 #include "util_allocator.h" 29 #include "util_allocator.h"
30 #include "cx/test.h" 30 #include "cx/test.h"
31 31
32 #include "cx/json.h" 32 #include "cx/json.h"
33 #include "cx/compare.h" 33 #include "cx/compare.h"
34
35 #include <math.h>
34 36
35 CX_TEST(test_json_init_default) { 37 CX_TEST(test_json_init_default) {
36 CxJson json; 38 CxJson json;
37 CX_TEST_DO { 39 CX_TEST_DO {
38 cxJsonInit(&json, NULL); 40 cxJsonInit(&json, NULL);
1246 cxJsonValueFree(obj); 1248 cxJsonValueFree(obj);
1247 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1249 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1248 } 1250 }
1249 cx_testing_allocator_destroy(&talloc); 1251 cx_testing_allocator_destroy(&talloc);
1250 } 1252 }
1253
1254 CX_TEST(test_json_compare_primitives) {
1255 CxJsonValue *a[14];
1256 a[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL);
1257 a[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE);
1258 a[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE);
1259 a[3] = cxJsonCreateInteger(NULL, 1234);
1260 a[4] = cxJsonCreateInteger(NULL, 5432);
1261 a[5] = cxJsonCreateInteger(NULL, -10);
1262 a[6] = cxJsonCreateInteger(NULL, 0);
1263 a[7] = cxJsonCreateNumber(NULL, 0.0f);
1264 a[8] = cxJsonCreateNumber(NULL, 13.37f);
1265 a[9] = cxJsonCreateNumber(NULL, -123.456f);
1266 a[10] = cxJsonCreateNumber(NULL, 1234.0f);
1267 a[11] = cxJsonCreateNumber(NULL, -10.3f);
1268 a[12] = cxJsonCreateString(NULL, "hello");
1269 a[13] = cxJsonCreateString(NULL, "world");
1270
1271 CxJsonValue *b[14];
1272 b[0] = cxJsonCreateLiteral(NULL, CX_JSON_NULL);
1273 b[1] = cxJsonCreateLiteral(NULL, CX_JSON_TRUE);
1274 b[2] = cxJsonCreateLiteral(NULL, CX_JSON_FALSE);
1275 b[3] = cxJsonCreateInteger(NULL, 1234);
1276 b[4] = cxJsonCreateInteger(NULL, 5432);
1277 b[5] = cxJsonCreateInteger(NULL, -10);
1278 b[6] = cxJsonCreateInteger(NULL, 0);
1279 b[7] = cxJsonCreateNumber(NULL, 0.0f);
1280 b[8] = cxJsonCreateNumber(NULL, 13.37f);
1281 b[9] = cxJsonCreateNumber(NULL, -123.456f);
1282 b[10] = cxJsonCreateNumber(NULL, 1234.0f);
1283 b[11] = cxJsonCreateNumber(NULL, -10.3f);
1284 b[12] = cxJsonCreateString(NULL, "hello");
1285 b[13] = cxJsonCreateString(NULL, "world");
1286
1287 CX_TEST_DO {
1288 for(int i=0;i<12;i++) {
1289 for(int j=0;j<12;j++) {
1290 int ret = cxJsonCompare(a[i], b[j]);
1291 if(i == j) {
1292 CX_TEST_ASSERT(ret == 0);
1293 } else {
1294 if(cxJsonIsNumber(a[i]) && cxJsonIsNumber(b[j])) {
1295 double diff = fabs(cxJsonAsDouble(a[i]) - cxJsonAsDouble(b[j]));
1296 if(diff < 0.001) {
1297 CX_TEST_ASSERT(ret == 0);
1298 } else {
1299 CX_TEST_ASSERT(ret != 0);
1300 }
1301 } else {
1302 CX_TEST_ASSERT(ret != 0);
1303 }
1304 }
1305 }
1306 }
1307
1308 }
1309 for(int i=0;i<14;i++) {
1310 cxJsonValueFree(a[i]);
1311 cxJsonValueFree(b[i]);
1312 }
1313 }
1314
1315 CX_TEST(test_json_compare_objects) {
1316 CxJsonValue *json[7];
1317 cxJsonFromString(NULL, "{ }", &json[0]);
1318 cxJsonFromString(NULL, "{ }", &json[1]);
1319 cxJsonFromString(NULL, "{ \"name\": \"value\" }", &json[2]);
1320 cxJsonFromString(NULL, "{ \"key0\": \"value0\", \"key1\": \"value1\", \"key2\":null }", &json[3]);
1321 cxJsonFromString(NULL, "{ \"key1\": \"value1\", \"key2\":null, \"key0\": \"value0\" }", &json[4]);
1322
1323 const char *json_complex_object =
1324 "{\n"
1325 " \"bool\": false,\n"
1326 " \"int\": 47,\n"
1327 " \"strings\": [ \"hello\", \"world\" ],\n"
1328 " \"nested\": {\n"
1329 " \"string\": \"test\",\n"
1330 " \"floats\": [ 3.1415, 47.11, 8.15 ],\n"
1331 " \"ints\": [ 4, 8, 15, 16, 23, 42 ],\n"
1332 " \"literals\": [ true, null, false ],\n"
1333 " \"objects\": [\n"
1334 " {},\n"
1335 " { \"unicode\": \"\\u03a3\\u29b0\" },\n"
1336 " { \"array\": [ 1,2,3] }\n"
1337 " ]\n"
1338 " }\n"
1339 "}\n";
1340 cxJsonFromString(NULL, json_complex_object, &json[5]);
1341 cxJsonFromString(NULL, json_complex_object, &json[6]);
1342
1343 CX_TEST_DO {
1344 for(int i=0;i<7;i++) {
1345 CX_TEST_ASSERT(cxJsonIsObject(json[i]));
1346 }
1347
1348 CX_TEST_ASSERT(cxJsonCompare(json[0], json[0]) == 0);
1349 CX_TEST_ASSERT(cxJsonCompare(json[0], json[1]) == 0);
1350 CX_TEST_ASSERT(cxJsonCompare(json[2], json[2]) == 0); // <-- fail
1351
1352 // compare empty object with all other objects
1353 for(int i=2;i<6;i++) {
1354 CX_TEST_ASSERT(cxJsonCompare(json[0], json[i]) != 0);
1355 }
1356
1357 // compare different objects
1358 CX_TEST_ASSERT(cxJsonCompare(json[2], json[3]) != 0);
1359 CX_TEST_ASSERT(cxJsonCompare(json[2], json[5]) != 0);
1360
1361 // compare equal objects with different orders
1362 CX_TEST_ASSERT(cxJsonCompare(json[3], json[4]) == 0); // <-- fail
1363
1364 // test the same complex object
1365 CX_TEST_ASSERT(cxJsonCompare(json[6], json[7])); // <-- fail, even with hashmap
1366 }
1367
1368 for(int i=0;i<7;i++) {
1369 cxJsonValueFree(json[i]);
1370 }
1371 }
1372
1373 CX_TEST(test_json_compare_arrays) {
1374 CX_TEST_DO {
1375
1376 }
1377 }
1378
1251 1379
1252 static CxJsonValue *test_json_write_create_test_object(const CxAllocator *allocator) { 1380 static CxJsonValue *test_json_write_create_test_object(const CxAllocator *allocator) {
1253 CxJsonValue *obj = cxJsonCreateObj(allocator); 1381 CxJsonValue *obj = cxJsonCreateObj(allocator);
1254 cxJsonObjPutLiteral(obj, "bool", CX_JSON_FALSE); 1382 cxJsonObjPutLiteral(obj, "bool", CX_JSON_FALSE);
1255 cxJsonObjPutNumber(obj, "int", 47); // purposely use PutNumber to put an int 1383 cxJsonObjPutNumber(obj, "int", 47); // purposely use PutNumber to put an int
1679 cx_test_register(suite, test_json_array_iterator); 1807 cx_test_register(suite, test_json_array_iterator);
1680 cx_test_register(suite, test_json_allocator); 1808 cx_test_register(suite, test_json_allocator);
1681 cx_test_register(suite, test_json_allocator_parse_error); 1809 cx_test_register(suite, test_json_allocator_parse_error);
1682 cx_test_register(suite, test_json_create_value); 1810 cx_test_register(suite, test_json_create_value);
1683 cx_test_register(suite, test_json_overwrite_value); 1811 cx_test_register(suite, test_json_overwrite_value);
1812 cx_test_register(suite, test_json_compare_primitives);
1813 cx_test_register(suite, test_json_compare_objects);
1814 cx_test_register(suite, test_json_compare_arrays);
1684 cx_test_register(suite, test_json_write_default_format); 1815 cx_test_register(suite, test_json_write_default_format);
1685 cx_test_register(suite, test_json_write_pretty_default_spaces); 1816 cx_test_register(suite, test_json_write_pretty_default_spaces);
1686 cx_test_register(suite, test_json_write_pretty_default_tabs); 1817 cx_test_register(suite, test_json_write_pretty_default_tabs);
1687 cx_test_register(suite, test_json_write_pretty_deep_nesting); 1818 cx_test_register(suite, test_json_write_pretty_deep_nesting);
1688 cx_test_register(suite, test_json_write_frac_max_digits); 1819 cx_test_register(suite, test_json_write_frac_max_digits);

mercurial