diff --git a/cJSON.h b/cJSON.h index fa0372a..6279654 100644 --- a/cJSON.h +++ b/cJSON.h @@ -100,9 +100,10 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ #define cJSON_StringIsConst 512 #define notCJSON_INT_USE_LONGLONG -// Note: Default cmake rules will force C89 and prevent long long. -// To use long long, delete build tree, then run: -// CFLAGS="-Wall -Werror" cmake -DENABLE_CUSTOM_COMPILER_FLAGS=Off +/* Note: Default cmake rules will force C89 and prevent long long. + To use long long, delete build tree, then run: + CFLAGS="-Wall -Werror" cmake -DENABLE_CUSTOM_COMPILER_FLAGS=Off + */ #ifdef CJSON_INT_USE_LONGLONG typedef long long cJSON_int; diff --git a/tests/compare_tests.c b/tests/compare_tests.c index 797c774..8db00ac 100644 --- a/tests/compare_tests.c +++ b/tests/compare_tests.c @@ -64,9 +64,14 @@ static void cjson_compare_should_compare_numbers(void) TEST_ASSERT_TRUE(compare_from_string("1", "1", false)); TEST_ASSERT_TRUE(compare_from_string("0.0001", "0.0001", true)); TEST_ASSERT_TRUE(compare_from_string("0.0001", "0.0001", false)); - TEST_ASSERT_TRUE(compare_from_string("1E100", "10E99", false)); + TEST_ASSERT_TRUE(compare_from_string("1E20", "10E19", false)); + TEST_ASSERT_FALSE(compare_from_string("0.5E-20", "0.5E-21", false)); + +#ifndef CJSON_FLOAT_USE_FLOAT + TEST_ASSERT_TRUE(compare_from_string("1E100", "10E99", false)); TEST_ASSERT_FALSE(compare_from_string("0.5E-100", "0.5E-101", false)); +#endif TEST_ASSERT_FALSE(compare_from_string("1", "2", true)); TEST_ASSERT_FALSE(compare_from_string("1", "2", false)); diff --git a/tests/inputs/test7float b/tests/inputs/test7float new file mode 100644 index 0000000..1f6c99a --- /dev/null +++ b/tests/inputs/test7float @@ -0,0 +1,22 @@ +[ + { + "precision": "zip", + "Latitude": 37.7668, + "Longitude": -122.395, + "Address": "", + "City": "SAN FRANCISCO", + "State": "CA", + "Zip": "94107", + "Country": "US" + }, + { + "precision": "zip", + "Latitude": 37.3719, + "Longitude": -122.0260, + "Address": "", + "City": "SUNNYVALE", + "State": "CA", + "Zip": "94085", + "Country": "US" + } + ] diff --git a/tests/inputs/test7float.expected b/tests/inputs/test7float.expected new file mode 100644 index 0000000..e3cf6fa --- /dev/null +++ b/tests/inputs/test7float.expected @@ -0,0 +1,19 @@ +[{ + "precision": "zip", + "Latitude": 37.7668, + "Longitude": -122.395, + "Address": "", + "City": "SAN FRANCISCO", + "State": "CA", + "Zip": "94107", + "Country": "US" + }, { + "precision": "zip", + "Latitude": 37.3719, + "Longitude": -122.026, + "Address": "", + "City": "SUNNYVALE", + "State": "CA", + "Zip": "94085", + "Country": "US" + }] \ No newline at end of file diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 3080dee..b375847 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -28,6 +28,11 @@ #include "unity/src/unity.h" #include "common.h" +#ifdef CJSON_FLOAT_USE_FLOAT +#undef TEST_ASSERT_EQUAL_DOUBLE +#define TEST_ASSERT_EQUAL_DOUBLE TEST_ASSERT_EQUAL_FLOAT +#endif + static void cjson_array_foreach_should_loop_over_arrays(void) { cJSON array[1]; diff --git a/tests/parse_examples.c b/tests/parse_examples.c index 95a0959..402b225 100644 --- a/tests/parse_examples.c +++ b/tests/parse_examples.c @@ -156,7 +156,11 @@ static void file_test6_should_not_be_parsed(void) static void file_test7_should_be_parsed_and_printed(void) { +#ifdef CJSON_FLOAT_USE_FLOAT + do_test("test7float"); +#else do_test("test7"); +#endif } static void file_test8_should_be_parsed_and_printed(void) diff --git a/tests/parse_number.c b/tests/parse_number.c index 502f10b..d0d04f5 100644 --- a/tests/parse_number.c +++ b/tests/parse_number.c @@ -43,7 +43,7 @@ static void assert_is_number(cJSON *number_item) assert_has_no_string(number_item); } -static void assert_parse_number(const char *string, cJSON_int integer, double real) +static void assert_parse_number(const char *string, cJSON_int integer, cJSON_float real) { parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.content = (const unsigned char*)string; @@ -56,7 +56,11 @@ static void assert_parse_number(const char *string, cJSON_int integer, double re #else TEST_ASSERT_EQUAL_INT(integer, item->valueint); #endif +#ifdef CJSON_FLOAT_USE_FLOAT + TEST_ASSERT_EQUAL_FLOAT(real, item->valuedouble); +#else TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble); +#endif } static void parse_number_should_parse_zero(void) @@ -96,11 +100,15 @@ static void parse_number_should_parse_positive_reals(void) assert_parse_number("10e-10", 0, 10e-10); assert_parse_number("10E-10", 0, 10e-10); #ifdef CJSON_INT_USE_LONGLONG +#ifndef CJSON_FLOAT_USE_FLOAT assert_parse_number("10e10", 100000000000LL, 10e10); +#endif #endif assert_parse_number("10e20", CJSON_INT_MAX, 10e20); +#ifndef CJSON_FLOAT_USE_FLOAT assert_parse_number("123e+127", CJSON_INT_MAX, 123e127); assert_parse_number("123e-128", 0, 123e-128); +#endif } static void parse_number_should_parse_negative_reals(void) @@ -109,11 +117,15 @@ static void parse_number_should_parse_negative_reals(void) assert_parse_number("-10e-10", 0, -10e-10); assert_parse_number("-10E-10", 0, -10e-10); #ifdef CJSON_INT_USE_LONGLONG +#ifndef CJSON_FLOAT_USE_FLOAT assert_parse_number("-10e10", -100000000000LL, -10e10); +#endif #endif assert_parse_number("-10e20", CJSON_INT_MIN, -10e20); +#ifndef CJSON_FLOAT_USE_FLOAT assert_parse_number("-123e+127", CJSON_INT_MIN, -123e127); assert_parse_number("-123e-128", 0, -123e-128); +#endif } int CJSON_CDECL main(void) diff --git a/tests/print_number.c b/tests/print_number.c index 2ebca03..617bed1 100644 --- a/tests/print_number.c +++ b/tests/print_number.c @@ -24,7 +24,7 @@ #include "unity/src/unity.h" #include "common.h" -static void assert_print_number(const char *expected, double input) +static void assert_print_number(const char *expected, cJSON_float input) { unsigned char printed[1024]; unsigned char new_buffer[26]; @@ -71,34 +71,41 @@ static void print_number_should_print_negative_integers(void) { assert_print_number("-1", -1.0); assert_print_number("-32768", -32768.0); +#ifndef CJSON_FLOAT_USE_FLOAT assert_print_number("-2147483648", -2147483648.0); assert_print_number("-2147483649", -2147483649.0); assert_print_number("-4294967296", -4294967296.0); assert_print_number("-4294967297", -4294967297.0); /* Approx lowest integer exactly representable in double */ assert_print_number("-8765432101234567", -8765432101234567.0); +#endif } static void print_number_should_print_positive_integers(void) { assert_print_number("1", 1.0); assert_print_number("32767", 32767.0); +#ifndef CJSON_FLOAT_USE_FLOAT assert_print_number("2147483647", 2147483647.0); assert_print_number("2147483648", 2147483648.0); assert_print_number("4294967295", 4294967295.0); assert_print_number("4294967296", 4294967296.0); /* Approx highest integer exactly representable in double */ assert_print_number("8765432101234567", 8765432101234567.0); +#endif } static void print_number_should_print_positive_reals(void) { assert_print_number("0.123", 0.123); assert_print_number("1e-09", 10e-10); + assert_print_number("1e+21", 10e20); +#ifndef CJSON_FLOAT_USE_FLOAT assert_print_number("1000000000000", 10e11); assert_print_number("1.23e+129", 123e+127); assert_print_number("1.23e-126", 123e-128); assert_print_number("3.1415926535897931", 3.1415926535897931); +#endif } static void print_number_should_print_negative_reals(void) @@ -106,8 +113,10 @@ static void print_number_should_print_negative_reals(void) assert_print_number("-0.0123", -0.0123); assert_print_number("-1e-09", -10e-10); assert_print_number("-1e+21", -10e20); +#ifndef CJSON_FLOAT_USE_FLOAT assert_print_number("-1.23e+129", -123e+127); assert_print_number("-1.23e-126", -123e-128); +#endif } static void print_number_should_print_non_number(void)