tests/test_json.c

changeset 1060
0a7c1bb2372d
parent 1057
4e8436c3e806
child 1063
e453e717876e
equal deleted inserted replaced
1059:154eb64ce746 1060:0a7c1bb2372d
374 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); 374 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
375 CX_TEST_ASSERT(cxJsonIsNumber(v)); 375 CX_TEST_ASSERT(cxJsonIsNumber(v));
376 CX_TEST_ASSERT(cxJsonAsInteger(v) == 12300); 376 CX_TEST_ASSERT(cxJsonAsInteger(v) == 12300);
377 CX_TEST_ASSERT(cxJsonAsDouble(v) == 12300.0); 377 CX_TEST_ASSERT(cxJsonAsDouble(v) == 12300.0);
378 cxJsonValueFree(v); 378 cxJsonValueFree(v);
379
380 cxJsonFill(&json, "18446744073709551615.0123456789 ");
381 result = cxJsonNext(&json, &v);
382 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
383 CX_TEST_ASSERT(cxJsonIsNumber(v));
384 // be as precise as possible
385 CX_TEST_ASSERT(cxJsonAsDouble(v) == 1.8446744073709552e+19);
386 cxJsonValueFree(v);
379 } 387 }
380 cxJsonDestroy(&json); 388 cxJsonDestroy(&json);
381 } 389 }
382 390
383 CX_TEST(test_json_number_format_errors) { 391 CX_TEST(test_json_number_format_errors) {
392 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, 400 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
393 "leading plus is not RFC-8259 compliant"); 401 "leading plus is not RFC-8259 compliant");
394 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); 402 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
395 cxJsonReset(&json); 403 cxJsonReset(&json);
396 404
397 #if 0 // TODO: discuss if it is intended that this produces -47 as valid value
398 cxJsonFill(&json, "-47,11e2 ");
399 result = cxJsonNext(&json, &v);
400 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
401 "decimal separator cannot be a comma");
402 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
403 cxJsonReset(&json);
404 #endif
405
406 cxJsonFill(&json, "0.815e-3.0 "); 405 cxJsonFill(&json, "0.815e-3.0 ");
407 result = cxJsonNext(&json, &v); 406 result = cxJsonNext(&json, &v);
408 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, 407 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
409 "exponent must be an integer"); 408 "exponent must be an integer");
410 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); 409 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
447 446
448 cxJsonFill(&json, "184467440737095516150123456789 "); 447 cxJsonFill(&json, "184467440737095516150123456789 ");
449 result = cxJsonNext(&json, &v); 448 result = cxJsonNext(&json, &v);
450 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER, 449 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
451 "30 digit int does not fit into 64-bit int"); 450 "30 digit int does not fit into 64-bit int");
452 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
453 cxJsonReset(&json);
454
455 cxJsonFill(&json, "18446744073709551615.0123456789 ");
456 result = cxJsonNext(&json, &v);
457 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
458 "numbers with more than 30 characters are unsupported in general");
459 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING); 451 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
460 cxJsonReset(&json); 452 cxJsonReset(&json);
461 } 453 }
462 cxJsonDestroy(&json); 454 cxJsonDestroy(&json);
463 } 455 }

mercurial