Skip to content

Commit dd5d9df

Browse files
openharmony_cigitee-org
authored andcommitted
!100 tdd add
Merge pull request !100 from liuyuxiu/master
2 parents b0c42db + fd06c3a commit dd5d9df

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

tests/misc_tests.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,22 @@ static void cjson_set_bool_value_must_not_break_objects(void)
731731
cJSON_Delete(sobj);
732732
}
733733

734+
static void cjson_parse_big_numbers_should_not_report_error(void)
735+
{
736+
cJSON *valid_big_number_json_object1 = cJSON_Parse("{\"a\": true, \"b\": [ null,9999999999999999999999999999999999999999999999912345678901234567]}");
737+
cJSON *valid_big_number_json_object2 = cJSON_Parse("{\"a\": true, \"b\": [ null,999999999999999999999999999999999999999999999991234567890.1234567E3]}");
738+
const char *invalid_big_number_json1 = "{\"a\": true, \"b\": [ null,99999999999999999999999999999999999999999999999.1234567890.1234567]}";
739+
const char *invalid_big_number_json2 = "{\"a\": true, \"b\": [ null,99999999999999999999999999999999999999999999999E1234567890e1234567]}";
740+
741+
TEST_ASSERT_NOT_NULL(valid_big_number_json_object1);
742+
TEST_ASSERT_NOT_NULL(valid_big_number_json_object2);
743+
TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(invalid_big_number_json1), "Invalid big number JSONs should not be parsed.");
744+
TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(invalid_big_number_json2), "Invalid big number JSONs should not be parsed.");
745+
746+
cJSON_Delete(valid_big_number_json_object1);
747+
cJSON_Delete(valid_big_number_json_object2);
748+
}
749+
734750
int CJSON_CDECL main(void)
735751
{
736752
UNITY_BEGIN();
@@ -761,6 +777,7 @@ int CJSON_CDECL main(void)
761777
RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
762778
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
763779
RUN_TEST(cjson_set_bool_value_must_not_break_objects);
780+
RUN_TEST(cjson_parse_big_numbers_should_not_report_error);
764781

765782
return UNITY_END();
766783
}

tests/parse_number.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,25 @@ static void assert_parse_number(const char *string, int integer, double real)
4848
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
4949
buffer.content = (const unsigned char*)string;
5050
buffer.length = strlen(string) + sizeof("");
51+
buffer.hooks = global_hooks;
5152

5253
TEST_ASSERT_TRUE(parse_number(item, &buffer));
5354
assert_is_number(item);
5455
TEST_ASSERT_EQUAL_INT(integer, item->valueint);
5556
TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble);
5657
}
5758

59+
static void assert_parse_big_number(const char *string)
60+
{
61+
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
62+
buffer.content = (const unsigned char*)string;
63+
buffer.length = strlen(string) + sizeof("");
64+
buffer.hooks = global_hooks;
65+
66+
TEST_ASSERT_TRUE(parse_number(item, &buffer));
67+
assert_is_number(item);
68+
}
69+
5870
static void parse_number_should_parse_zero(void)
5971
{
6072
assert_parse_number("0", 0, 0);
@@ -96,6 +108,13 @@ static void parse_number_should_parse_negative_reals(void)
96108
assert_parse_number("-123e-128", 0, -123e-128);
97109
}
98110

111+
static void parse_number_should_parse_big_numbers(void)
112+
{
113+
assert_parse_big_number("9999999999999999999999999999999999999999999999912345678901234567");
114+
assert_parse_big_number("9999999999999999999999999999999999999999999999912345678901234567E10");
115+
assert_parse_big_number("999999999999999999999999999999999999999999999991234567890.1234567");
116+
}
117+
99118
int CJSON_CDECL main(void)
100119
{
101120
/* initialize cJSON item */
@@ -106,5 +125,6 @@ int CJSON_CDECL main(void)
106125
RUN_TEST(parse_number_should_parse_positive_integers);
107126
RUN_TEST(parse_number_should_parse_positive_reals);
108127
RUN_TEST(parse_number_should_parse_negative_reals);
128+
RUN_TEST(parse_number_should_parse_big_numbers);
109129
return UNITY_END();
110130
}

0 commit comments

Comments
 (0)