mirror of
https://github.com/DaveGamble/cJSON.git
synced 2024-12-26 21:04:24 +08:00
add testcase for cJSON_SetValuestringToObject
This commit is contained in:
parent
6b35f1c5bc
commit
4790c3c8f5
@ -550,7 +550,8 @@ static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_al
|
||||
cJSON_Delete(object);
|
||||
}
|
||||
|
||||
static void cjson_delete_item_from_array_should_not_broken_list_structure(void) {
|
||||
static void cjson_delete_item_from_array_should_not_broken_list_structure(void)
|
||||
{
|
||||
const char expected_json1[] = "{\"rd\":[{\"a\":\"123\"}]}";
|
||||
const char expected_json2[] = "{\"rd\":[{\"a\":\"123\"},{\"b\":\"456\"}]}";
|
||||
const char expected_json3[] = "{\"rd\":[{\"b\":\"456\"}]}";
|
||||
@ -583,6 +584,31 @@ static void cjson_delete_item_from_array_should_not_broken_list_structure(void)
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
|
||||
{
|
||||
cJSON *root = cJSON_Parse("{}");
|
||||
cJSON *item1 = cJSON_CreateString("valuestring could be changed safely");
|
||||
cJSON *item2 = cJSON_CreateStringReference("reference item should be freed by yourself");
|
||||
const char *newValuestring = "new valuestring which much longer than previous";
|
||||
char *returnValue = NULL;
|
||||
|
||||
cJSON_AddItemToObject(root, "one", item1);
|
||||
cJSON_AddItemToObject(root, "two", item2);
|
||||
|
||||
/* we needn't to free the original valuestring manually */
|
||||
returnValue = cJSON_SetValuestringToObject(cJSON_GetObjectItem(root, "one"), newValuestring);
|
||||
TEST_ASSERT_NOT_NULL(returnValue);
|
||||
TEST_ASSERT_EQUAL_STRING(newValuestring, cJSON_GetObjectItem(root, "one")->valuestring);
|
||||
|
||||
returnValue = cJSON_SetValuestringToObject(cJSON_GetObjectItem(root, "two"), newValuestring);
|
||||
TEST_ASSERT_NOT_NULL(returnValue);
|
||||
TEST_ASSERT_EQUAL_STRING(newValuestring, cJSON_GetObjectItem(root, "two")->valuestring);
|
||||
/* we must free the memory manually when the item's type is cJSON_IsReference */
|
||||
cJSON_free(item2->valuestring);
|
||||
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
int CJSON_CDECL main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
@ -609,6 +635,7 @@ int CJSON_CDECL main(void)
|
||||
RUN_TEST(cjson_create_array_reference_should_create_an_array_reference);
|
||||
RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased);
|
||||
RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
|
||||
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user