Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a6424b8

Browse files
sdrsdrAlanscut
andauthored
feat: add cJSON_SetBoolValue and test (DaveGamble#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>
1 parent 3cecc40 commit a6424b8

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

‎cJSON.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
279279
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
280280
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
281281

282+
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
283+
#define cJSON_SetBoolValue(object, boolValue) ( \
284+
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
285+
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
286+
cJSON_Invalid\
287+
)
288+
282289
/* Macro for iterating over an array or object */
283290
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
284291

‎tests/misc_tests.c‎

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "unity/src/unity.h"
2929
#include "common.h"
3030

31-
3231
static void cjson_array_foreach_should_loop_over_arrays(void)
3332
{
3433
cJSON array[1];
@@ -77,7 +76,6 @@ static void cjson_get_object_item_should_get_object_items(void)
7776
found = cJSON_GetObjectItem(item, NULL);
7877
TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
7978

80-
8179
found = cJSON_GetObjectItem(item, "one");
8280
TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
8381
TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
@@ -127,7 +125,8 @@ static void cjson_get_object_item_case_sensitive_should_get_object_items(void)
127125
cJSON_Delete(item);
128126
}
129127

130-
static void cjson_get_object_item_should_not_crash_with_array(void) {
128+
static void cjson_get_object_item_should_not_crash_with_array(void)
129+
{
131130
cJSON *array = NULL;
132131
cJSON *found = NULL;
133132
array = cJSON_Parse("[1]");
@@ -138,7 +137,8 @@ static void cjson_get_object_item_should_not_crash_with_array(void) {
138137
cJSON_Delete(array);
139138
}
140139

141-
static void cjson_get_object_item_case_sensitive_should_not_crash_with_array(void) {
140+
static void cjson_get_object_item_case_sensitive_should_not_crash_with_array(void)
141+
{
142142
cJSON *array = NULL;
143143
cJSON *found = NULL;
144144
array = cJSON_Parse("[1]");
@@ -302,7 +302,6 @@ static void cjson_replace_item_via_pointer_should_replace_items(void)
302302
cJSON_AddItemToArray(array, middle);
303303
cJSON_AddItemToArray(array, end);
304304

305-
306305
memset(replacements, '0円', sizeof(replacements));
307306

308307
/* replace beginning */
@@ -329,7 +328,7 @@ static void cjson_replace_item_via_pointer_should_replace_items(void)
329328

330329
static void cjson_replace_item_in_object_should_preserve_name(void)
331330
{
332-
cJSON root[1] = {{NULL, NULL, NULL, 0, NULL, 0, 0, NULL}};
331+
cJSON root[1] = {{NULL, NULL, NULL, 0, NULL, 0, 0, NULL}};
333332
cJSON *child = NULL;
334333
cJSON *replacement = NULL;
335334
cJSON_bool flag = false;
@@ -339,7 +338,7 @@ static void cjson_replace_item_in_object_should_preserve_name(void)
339338
replacement = cJSON_CreateNumber(2);
340339
TEST_ASSERT_NOT_NULL(replacement);
341340

342-
flag = cJSON_AddItemToObject(root, "child", child);
341+
flag = cJSON_AddItemToObject(root, "child", child);
343342
TEST_ASSERT_TRUE_MESSAGE(flag, "add item to object failed");
344343
cJSON_ReplaceItemInObject(root, "child", replacement);
345344

@@ -435,7 +434,7 @@ static void cjson_functions_should_not_crash_with_null_pointers(void)
435434
cJSON_Delete(item);
436435
}
437436

438-
static void *CJSON_CDECL failing_realloc(void *pointer, size_t size)
437+
static void *CJSON_CDECL failing_realloc(void *pointer, size_t size)
439438
{
440439
(void)size;
441440
(void)pointer;
@@ -445,7 +444,7 @@ static void * CJSON_CDECL failing_realloc(void *pointer, size_t size)
445444
static void ensure_should_fail_on_failed_realloc(void)
446445
{
447446
printbuffer buffer = {NULL, 10, 0, 0, false, false, {&malloc, &free, &failing_realloc}};
448-
buffer.buffer = (unsigned char*)malloc(100);
447+
buffer.buffer = (unsigned char*)malloc(100);
449448
TEST_ASSERT_NOT_NULL(buffer.buffer);
450449

451450
TEST_ASSERT_NULL_MESSAGE(ensure(&buffer, 200), "Ensure didn't fail with failing realloc.");
@@ -454,7 +453,7 @@ static void ensure_should_fail_on_failed_realloc(void)
454453
static void skip_utf8_bom_should_skip_bom(void)
455454
{
456455
const unsigned char string[] = "\xEF\xBB\xBF{}";
457-
parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0 } };
456+
parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}};
458457
buffer.content = string;
459458
buffer.length = sizeof(string);
460459
buffer.hooks = global_hooks;
@@ -466,7 +465,7 @@ static void skip_utf8_bom_should_skip_bom(void)
466465
static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
467466
{
468467
const unsigned char string[] = " \xEF\xBB\xBF{}";
469-
parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0 } };
468+
parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}};
470469
buffer.content = string;
471470
buffer.length = sizeof(string);
472471
buffer.hooks = global_hooks;
@@ -496,12 +495,13 @@ static void cjson_get_number_value_should_get_a_number(void)
496495
TEST_ASSERT_EQUAL_DOUBLE(cJSON_GetNumberValue(number), number->valuedouble);
497496
TEST_ASSERT_DOUBLE_IS_NAN(cJSON_GetNumberValue(string));
498497
TEST_ASSERT_DOUBLE_IS_NAN(cJSON_GetNumberValue(NULL));
499-
498+
500499
cJSON_Delete(number);
501500
cJSON_Delete(string);
502501
}
503502

504-
static void cjson_create_string_reference_should_create_a_string_reference(void) {
503+
static void cjson_create_string_reference_should_create_a_string_reference(void)
504+
{
505505
const char *string = "I am a string!";
506506

507507
cJSON *string_reference = cJSON_CreateStringReference(string);
@@ -511,7 +511,8 @@ static void cjson_create_string_reference_should_create_a_string_reference(void)
511511
cJSON_Delete(string_reference);
512512
}
513513

514-
static void cjson_create_object_reference_should_create_an_object_reference(void) {
514+
static void cjson_create_object_reference_should_create_an_object_reference(void)
515+
{
515516
cJSON *number_reference = NULL;
516517
cJSON *number_object = cJSON_CreateObject();
517518
cJSON *number = cJSON_CreateNumber(42);
@@ -529,7 +530,8 @@ static void cjson_create_object_reference_should_create_an_object_reference(void
529530
cJSON_Delete(number_reference);
530531
}
531532

532-
static void cjson_create_array_reference_should_create_an_array_reference(void) {
533+
static void cjson_create_array_reference_should_create_an_array_reference(void)
534+
{
533535
cJSON *number_reference = NULL;
534536
cJSON *number_array = cJSON_CreateArray();
535537
cJSON *number = cJSON_CreateNumber(42);
@@ -566,7 +568,7 @@ static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_al
566568
{
567569
cJSON *object = cJSON_CreateObject();
568570
cJSON *number = cJSON_CreateNumber(42);
569-
char *name = (char*)cJSON_strdup((const unsigned char*)"number", &global_hooks);
571+
char *name = (char*)cJSON_strdup((const unsigned char*)"number", &global_hooks);
570572

571573
TEST_ASSERT_NOT_NULL(object);
572574
TEST_ASSERT_NOT_NULL(number);
@@ -626,7 +628,7 @@ static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
626628
cJSON *item2 = cJSON_CreateStringReference(reference_valuestring);
627629
char *ptr1 = NULL;
628630
char *return_value = NULL;
629-
631+
630632
cJSON_AddItemToObject(root, "one", item1);
631633
cJSON_AddItemToObject(root, "two", item2);
632634

@@ -650,6 +652,64 @@ static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
650652
cJSON_Delete(root);
651653
}
652654

655+
static void cjson_set_bool_value_must_not_break_objects(void)
656+
{
657+
cJSON *bobj, *sobj, *oobj, *refobj = NULL;
658+
659+
TEST_ASSERT_TRUE((cJSON_SetBoolValue(refobj, 1) == cJSON_Invalid));
660+
661+
bobj = cJSON_CreateFalse();
662+
TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
663+
TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj, 1) == cJSON_True));
664+
TEST_ASSERT_TRUE(cJSON_IsTrue(bobj));
665+
cJSON_SetBoolValue(bobj, 1);
666+
TEST_ASSERT_TRUE(cJSON_IsTrue(bobj));
667+
TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj, 0) == cJSON_False));
668+
TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
669+
cJSON_SetBoolValue(bobj, 0);
670+
TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
671+
672+
sobj = cJSON_CreateString("test");
673+
TEST_ASSERT_TRUE(cJSON_IsString(sobj));
674+
cJSON_SetBoolValue(sobj, 1);
675+
TEST_ASSERT_TRUE(cJSON_IsString(sobj));
676+
cJSON_SetBoolValue(sobj, 0);
677+
TEST_ASSERT_TRUE(cJSON_IsString(sobj));
678+
679+
oobj = cJSON_CreateObject();
680+
TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
681+
cJSON_SetBoolValue(oobj, 1);
682+
TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
683+
cJSON_SetBoolValue(oobj, 0);
684+
TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
685+
686+
refobj = cJSON_CreateStringReference("conststring");
687+
TEST_ASSERT_TRUE(cJSON_IsString(refobj));
688+
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
689+
cJSON_SetBoolValue(refobj, 1);
690+
TEST_ASSERT_TRUE(cJSON_IsString(refobj));
691+
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
692+
cJSON_SetBoolValue(refobj, 0);
693+
TEST_ASSERT_TRUE(cJSON_IsString(refobj));
694+
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
695+
cJSON_Delete(refobj);
696+
697+
refobj = cJSON_CreateObjectReference(oobj);
698+
TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
699+
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
700+
cJSON_SetBoolValue(refobj, 1);
701+
TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
702+
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
703+
cJSON_SetBoolValue(refobj, 0);
704+
TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
705+
TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
706+
cJSON_Delete(refobj);
707+
708+
cJSON_Delete(oobj);
709+
cJSON_Delete(bobj);
710+
cJSON_Delete(sobj);
711+
}
712+
653713
int CJSON_CDECL main(void)
654714
{
655715
UNITY_BEGIN();
@@ -679,6 +739,7 @@ int CJSON_CDECL main(void)
679739
RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased);
680740
RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
681741
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
742+
RUN_TEST(cjson_set_bool_value_must_not_break_objects);
682743

683744
return UNITY_END();
684745
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /