tests/test_json.c

changeset 1063
e453e717876e
parent 1060
0a7c1bb2372d
equal deleted inserted replaced
1062:8baed9b38bc6 1063:e453e717876e
28 28
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/mempool.h" 33 #include "cx/compare.h"
34 34
35 CX_TEST(test_json_init_default) { 35 CX_TEST(test_json_init_default) {
36 CxJson json; 36 CxJson json;
37 CX_TEST_DO { 37 CX_TEST_DO {
38 cxJsonInit(&json, NULL); 38 cxJsonInit(&json, NULL);
84 CxJsonValue *position = cxJsonObjGet(obj, "position"); 84 CxJsonValue *position = cxJsonObjGet(obj, "position");
85 CX_TEST_ASSERT(cxJsonIsObject(position)); 85 CX_TEST_ASSERT(cxJsonIsObject(position));
86 CxJsonValue *longitude = cxJsonObjGet(position, "longitude"); 86 CxJsonValue *longitude = cxJsonObjGet(position, "longitude");
87 CX_TEST_ASSERT(cxJsonIsNumber(longitude)); 87 CX_TEST_ASSERT(cxJsonIsNumber(longitude));
88 CX_TEST_ASSERT(!cxJsonIsInteger(longitude)); 88 CX_TEST_ASSERT(!cxJsonIsInteger(longitude));
89 CX_TEST_ASSERT(cxJsonAsDouble(longitude) == -94.7099); 89 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(longitude), -94.7099));
90 CX_TEST_ASSERT(cxJsonAsInteger(longitude) == -94); 90 CX_TEST_ASSERT(cxJsonAsInteger(longitude) == -94);
91 CxJsonValue *latitude = cxJsonObjGet(position, "latitude"); 91 CxJsonValue *latitude = cxJsonObjGet(position, "latitude");
92 CX_TEST_ASSERT(cxJsonIsNumber(latitude)); 92 CX_TEST_ASSERT(cxJsonIsNumber(latitude));
93 CX_TEST_ASSERT(!cxJsonIsInteger(latitude)); 93 CX_TEST_ASSERT(!cxJsonIsInteger(latitude));
94 CX_TEST_ASSERT(cxJsonAsDouble(latitude) == 51.5539); 94 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(latitude), 51.5539));
95 CX_TEST_ASSERT(cxJsonAsInteger(latitude) == 51); 95 CX_TEST_ASSERT(cxJsonAsInteger(latitude) == 51);
96 96
97 CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp"); 97 CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp");
98 CX_TEST_ASSERT(cxJsonIsInteger(timestamp)); 98 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
99 CX_TEST_ASSERT(cxJsonIsNumber(timestamp)); 99 CX_TEST_ASSERT(cxJsonIsNumber(timestamp));
350 350
351 cxJsonFill(&json, "3.1415 "); 351 cxJsonFill(&json, "3.1415 ");
352 result = cxJsonNext(&json, &v); 352 result = cxJsonNext(&json, &v);
353 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 353 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
354 CX_TEST_ASSERT(cxJsonIsNumber(v)); 354 CX_TEST_ASSERT(cxJsonIsNumber(v));
355 CX_TEST_ASSERT(cxJsonAsDouble(v) == 3.1415); 355 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), 3.1415));
356 cxJsonValueFree(v); 356 cxJsonValueFree(v);
357 357
358 cxJsonFill(&json, "-47.11e2 "); 358 cxJsonFill(&json, "-47.11e2 ");
359 result = cxJsonNext(&json, &v); 359 result = cxJsonNext(&json, &v);
360 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 360 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
361 CX_TEST_ASSERT(cxJsonIsNumber(v)); 361 CX_TEST_ASSERT(cxJsonIsNumber(v));
362 CX_TEST_ASSERT(cxJsonAsDouble(v) == -4711.0); 362 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), -4711.0));
363 cxJsonValueFree(v); 363 cxJsonValueFree(v);
364 364
365 cxJsonFill(&json, "0.815e-3 "); 365 cxJsonFill(&json, "0.815e-3 ");
366 result = cxJsonNext(&json, &v); 366 result = cxJsonNext(&json, &v);
367 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 367 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
368 CX_TEST_ASSERT(cxJsonIsNumber(v)); 368 CX_TEST_ASSERT(cxJsonIsNumber(v));
369 CX_TEST_ASSERT(cxJsonAsDouble(v) == 0.000815); 369 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), 0.000815));
370 cxJsonValueFree(v); 370 cxJsonValueFree(v);
371 371
372 cxJsonFill(&json, "1.23E4 "); 372 cxJsonFill(&json, "1.23E4 ");
373 result = cxJsonNext(&json, &v); 373 result = cxJsonNext(&json, &v);
374 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 374 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
380 cxJsonFill(&json, "18446744073709551615.0123456789 "); 380 cxJsonFill(&json, "18446744073709551615.0123456789 ");
381 result = cxJsonNext(&json, &v); 381 result = cxJsonNext(&json, &v);
382 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 382 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
383 CX_TEST_ASSERT(cxJsonIsNumber(v)); 383 CX_TEST_ASSERT(cxJsonIsNumber(v));
384 // be as precise as possible 384 // be as precise as possible
385 CX_TEST_ASSERT(cxJsonAsDouble(v) == 1.8446744073709552e+19); 385 // TODO: this might produce format error / out of range in future implementations
386 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(v), 1.8446744073709552e+19));
386 cxJsonValueFree(v); 387 cxJsonValueFree(v);
387 } 388 }
388 cxJsonDestroy(&json); 389 cxJsonDestroy(&json);
389 } 390 }
390 391

mercurial