mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Fix tests for all combinations of int/longlong and double/float
This commit is contained in:
parent
5cdc8d4c27
commit
882361c428
7
cJSON.h
7
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 <sourcedir>
|
||||
/* 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 <sourcedir>
|
||||
*/
|
||||
|
||||
#ifdef CJSON_INT_USE_LONGLONG
|
||||
typedef long long cJSON_int;
|
||||
|
@ -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));
|
||||
|
22
tests/inputs/test7float
Normal file
22
tests/inputs/test7float
Normal file
@ -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"
|
||||
}
|
||||
]
|
19
tests/inputs/test7float.expected
Normal file
19
tests/inputs/test7float.expected
Normal file
@ -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"
|
||||
}]
|
@ -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];
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user