feat: add cJSON_SetBoolValue and test (#639)

* cJSON_SetBoolValue plus test

* cJSON_Invalid insted of just 0

* Update tests/misc_tests.c

* VSCode standard C formater applied

Co-authored-by: Alan Wang <wp_scut@163.com>
This commit is contained in:
Stoian Ivanov 2022-01-26 14:24:50 +02:00 committed by GitHub
parent 3cecc40466
commit a6424b85dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 17 deletions

View File

@ -279,6 +279,13 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
#define cJSON_SetBoolValue(object, boolValue) ( \
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
cJSON_Invalid\
)
/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)

View File

@ -28,7 +28,6 @@
#include "unity/src/unity.h"
#include "common.h"
static void cjson_array_foreach_should_loop_over_arrays(void)
{
cJSON array[1];
@ -77,7 +76,6 @@ static void cjson_get_object_item_should_get_object_items(void)
found = cJSON_GetObjectItem(item, NULL);
TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
found = cJSON_GetObjectItem(item, "one");
TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
@ -127,7 +125,8 @@ static void cjson_get_object_item_case_sensitive_should_get_object_items(void)
cJSON_Delete(item);
}
static void cjson_get_object_item_should_not_crash_with_array(void) {
static void cjson_get_object_item_should_not_crash_with_array(void)
{
cJSON *array = NULL;
cJSON *found = NULL;
array = cJSON_Parse("[1]");
@ -138,7 +137,8 @@ static void cjson_get_object_item_should_not_crash_with_array(void) {
cJSON_Delete(array);
}
static void cjson_get_object_item_case_sensitive_should_not_crash_with_array(void) {
static void cjson_get_object_item_case_sensitive_should_not_crash_with_array(void)
{
cJSON *array = NULL;
cJSON *found = NULL;
array = cJSON_Parse("[1]");
@ -302,7 +302,6 @@ static void cjson_replace_item_via_pointer_should_replace_items(void)
cJSON_AddItemToArray(array, middle);
cJSON_AddItemToArray(array, end);
memset(replacements, '\0', sizeof(replacements));
/* replace beginning */
@ -501,7 +500,8 @@ static void cjson_get_number_value_should_get_a_number(void)
cJSON_Delete(string);
}
static void cjson_create_string_reference_should_create_a_string_reference(void) {
static void cjson_create_string_reference_should_create_a_string_reference(void)
{
const char *string = "I am a string!";
cJSON *string_reference = cJSON_CreateStringReference(string);
@ -511,7 +511,8 @@ static void cjson_create_string_reference_should_create_a_string_reference(void)
cJSON_Delete(string_reference);
}
static void cjson_create_object_reference_should_create_an_object_reference(void) {
static void cjson_create_object_reference_should_create_an_object_reference(void)
{
cJSON *number_reference = NULL;
cJSON *number_object = cJSON_CreateObject();
cJSON *number = cJSON_CreateNumber(42);
@ -529,7 +530,8 @@ static void cjson_create_object_reference_should_create_an_object_reference(void
cJSON_Delete(number_reference);
}
static void cjson_create_array_reference_should_create_an_array_reference(void) {
static void cjson_create_array_reference_should_create_an_array_reference(void)
{
cJSON *number_reference = NULL;
cJSON *number_array = cJSON_CreateArray();
cJSON *number = cJSON_CreateNumber(42);
@ -650,6 +652,64 @@ static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
cJSON_Delete(root);
}
static void cjson_set_bool_value_must_not_break_objects(void)
{
cJSON *bobj, *sobj, *oobj, *refobj = NULL;
TEST_ASSERT_TRUE((cJSON_SetBoolValue(refobj, 1) == cJSON_Invalid));
bobj = cJSON_CreateFalse();
TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj, 1) == cJSON_True));
TEST_ASSERT_TRUE(cJSON_IsTrue(bobj));
cJSON_SetBoolValue(bobj, 1);
TEST_ASSERT_TRUE(cJSON_IsTrue(bobj));
TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj, 0) == cJSON_False));
TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
cJSON_SetBoolValue(bobj, 0);
TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
sobj = cJSON_CreateString("test");
TEST_ASSERT_TRUE(cJSON_IsString(sobj));
cJSON_SetBoolValue(sobj, 1);
TEST_ASSERT_TRUE(cJSON_IsString(sobj));
cJSON_SetBoolValue(sobj, 0);
TEST_ASSERT_TRUE(cJSON_IsString(sobj));
oobj = cJSON_CreateObject();
TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
cJSON_SetBoolValue(oobj, 1);
TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
cJSON_SetBoolValue(oobj, 0);
TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
refobj = cJSON_CreateStringReference("conststring");
TEST_ASSERT_TRUE(cJSON_IsString(refobj));
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
cJSON_SetBoolValue(refobj, 1);
TEST_ASSERT_TRUE(cJSON_IsString(refobj));
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
cJSON_SetBoolValue(refobj, 0);
TEST_ASSERT_TRUE(cJSON_IsString(refobj));
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
cJSON_Delete(refobj);
refobj = cJSON_CreateObjectReference(oobj);
TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
cJSON_SetBoolValue(refobj, 1);
TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
cJSON_SetBoolValue(refobj, 0);
TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
cJSON_Delete(refobj);
cJSON_Delete(oobj);
cJSON_Delete(bobj);
cJSON_Delete(sobj);
}
int CJSON_CDECL main(void)
{
UNITY_BEGIN();
@ -679,6 +739,7 @@ int CJSON_CDECL main(void)
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);
RUN_TEST(cjson_set_bool_value_must_not_break_objects);
return UNITY_END();
}