tests/test_json.c

changeset 1117
54df904472b0
parent 1112
22dc2163fffd
equal deleted inserted replaced
1116:b381da3a9b19 1117:54df904472b0
885 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 885 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
886 } 886 }
887 cx_testing_allocator_destroy(&talloc); 887 cx_testing_allocator_destroy(&talloc);
888 } 888 }
889 889
890 CX_TEST(test_json_write_frac_max_digits) {
891 CxJsonValue* num = cxJsonCreateNumber(NULL, 3.141592653589793);
892 CxJsonWriter writer = cxJsonWriterCompact();
893 CxBuffer buf;
894 cxBufferInit(&buf, NULL, 32, NULL, 0);
895 CX_TEST_DO {
896 // test default settings (6 digits)
897 cxJsonWrite(&buf,num, cxBufferWriteFunc, &writer);
898 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141592")));
899
900 // test too many digits
901 cxBufferReset(&buf);
902 writer.frac_max_digits = 200;
903 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
904 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141592653589793")));
905
906 // test 0 digits
907 cxBufferReset(&buf);
908 writer.frac_max_digits = 0;
909 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
910 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3")));
911
912 // test 2 digits
913 cxBufferReset(&buf);
914 writer.frac_max_digits = 2;
915 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
916 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.14")));
917
918 // test 3 digits
919 cxBufferReset(&buf);
920 writer.frac_max_digits = 3;
921 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
922 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141")));
923
924 // test 6 digits, but two are left of the decimal point
925 num->value.number = 47.110815;
926 cxBufferReset(&buf);
927 writer.frac_max_digits = 6;
928 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
929 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("47.110815")));
930
931 // test 4 digits with exponent
932 num->value.number = 5.11223344e23;
933 cxBufferReset(&buf);
934 writer.frac_max_digits = 4;
935 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
936 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("5.1122e+23")));
937 }
938 cxBufferDestroy(&buf);
939 cxJsonValueFree(num);
940 }
941
890 CxTestSuite *cx_test_suite_json(void) { 942 CxTestSuite *cx_test_suite_json(void) {
891 CxTestSuite *suite = cx_test_suite_new("json"); 943 CxTestSuite *suite = cx_test_suite_new("json");
892 944
893 cx_test_register(suite, test_json_init_default); 945 cx_test_register(suite, test_json_init_default);
894 cx_test_register(suite, test_json_simple_object); 946 cx_test_register(suite, test_json_simple_object);
907 cx_test_register(suite, test_json_create_value); 959 cx_test_register(suite, test_json_create_value);
908 cx_test_register(suite, test_json_write_default_format); 960 cx_test_register(suite, test_json_write_default_format);
909 cx_test_register(suite, test_json_write_pretty_default_spaces); 961 cx_test_register(suite, test_json_write_pretty_default_spaces);
910 cx_test_register(suite, test_json_write_pretty_default_tabs); 962 cx_test_register(suite, test_json_write_pretty_default_tabs);
911 cx_test_register(suite, test_json_write_pretty_preserve_order); 963 cx_test_register(suite, test_json_write_pretty_preserve_order);
964 cx_test_register(suite, test_json_write_frac_max_digits);
912 965
913 return suite; 966 return suite;
914 } 967 }
915 968

mercurial