28
28
#include "unity/src/unity.h"
29
29
#include "common.h"
30
30
31
-
32
31
static void cjson_array_foreach_should_loop_over_arrays (void )
33
32
{
34
33
cJSON array [1 ];
@@ -77,7 +76,6 @@ static void cjson_get_object_item_should_get_object_items(void)
77
76
found = cJSON_GetObjectItem (item , NULL );
78
77
TEST_ASSERT_NULL_MESSAGE (found , "Failed to fail on NULL string." );
79
78
80
-
81
79
found = cJSON_GetObjectItem (item , "one" );
82
80
TEST_ASSERT_NOT_NULL_MESSAGE (found , "Failed to find first item." );
83
81
TEST_ASSERT_EQUAL_DOUBLE (found -> valuedouble , 1 );
@@ -127,7 +125,8 @@ static void cjson_get_object_item_case_sensitive_should_get_object_items(void)
127
125
cJSON_Delete (item );
128
126
}
129
127
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
+ {
131
130
cJSON * array = NULL ;
132
131
cJSON * found = NULL ;
133
132
array = cJSON_Parse ("[1]" );
@@ -138,7 +137,8 @@ static void cjson_get_object_item_should_not_crash_with_array(void) {
138
137
cJSON_Delete (array );
139
138
}
140
139
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
+ {
142
142
cJSON * array = NULL ;
143
143
cJSON * found = NULL ;
144
144
array = cJSON_Parse ("[1]" );
@@ -302,7 +302,6 @@ static void cjson_replace_item_via_pointer_should_replace_items(void)
302
302
cJSON_AddItemToArray (array , middle );
303
303
cJSON_AddItemToArray (array , end );
304
304
305
-
306
305
memset (replacements , '0円' , sizeof (replacements ));
307
306
308
307
/* replace beginning */
@@ -329,7 +328,7 @@ static void cjson_replace_item_via_pointer_should_replace_items(void)
329
328
330
329
static void cjson_replace_item_in_object_should_preserve_name (void )
331
330
{
332
- cJSON root [1 ] = {{NULL , NULL , NULL , 0 , NULL , 0 , 0 , NULL }};
331
+ cJSON root [1 ] = {{NULL , NULL , NULL , 0 , NULL , 0 , 0 , NULL }};
333
332
cJSON * child = NULL ;
334
333
cJSON * replacement = NULL ;
335
334
cJSON_bool flag = false;
@@ -339,7 +338,7 @@ static void cjson_replace_item_in_object_should_preserve_name(void)
339
338
replacement = cJSON_CreateNumber (2 );
340
339
TEST_ASSERT_NOT_NULL (replacement );
341
340
342
- flag = cJSON_AddItemToObject (root , "child" , child );
341
+ flag = cJSON_AddItemToObject (root , "child" , child );
343
342
TEST_ASSERT_TRUE_MESSAGE (flag , "add item to object failed" );
344
343
cJSON_ReplaceItemInObject (root , "child" , replacement );
345
344
@@ -435,7 +434,7 @@ static void cjson_functions_should_not_crash_with_null_pointers(void)
435
434
cJSON_Delete (item );
436
435
}
437
436
438
- static void * CJSON_CDECL failing_realloc (void * pointer , size_t size )
437
+ static void * CJSON_CDECL failing_realloc (void * pointer , size_t size )
439
438
{
440
439
(void )size ;
441
440
(void )pointer ;
@@ -445,7 +444,7 @@ static void * CJSON_CDECL failing_realloc(void *pointer, size_t size)
445
444
static void ensure_should_fail_on_failed_realloc (void )
446
445
{
447
446
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 );
449
448
TEST_ASSERT_NOT_NULL (buffer .buffer );
450
449
451
450
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)
454
453
static void skip_utf8_bom_should_skip_bom (void )
455
454
{
456
455
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 } };
458
457
buffer .content = string ;
459
458
buffer .length = sizeof (string );
460
459
buffer .hooks = global_hooks ;
@@ -466,7 +465,7 @@ static void skip_utf8_bom_should_skip_bom(void)
466
465
static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning (void )
467
466
{
468
467
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 } };
470
469
buffer .content = string ;
471
470
buffer .length = sizeof (string );
472
471
buffer .hooks = global_hooks ;
@@ -496,12 +495,13 @@ static void cjson_get_number_value_should_get_a_number(void)
496
495
TEST_ASSERT_EQUAL_DOUBLE (cJSON_GetNumberValue (number ), number -> valuedouble );
497
496
TEST_ASSERT_DOUBLE_IS_NAN (cJSON_GetNumberValue (string ));
498
497
TEST_ASSERT_DOUBLE_IS_NAN (cJSON_GetNumberValue (NULL ));
499
-
498
+
500
499
cJSON_Delete (number );
501
500
cJSON_Delete (string );
502
501
}
503
502
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
+ {
505
505
const char * string = "I am a string!" ;
506
506
507
507
cJSON * string_reference = cJSON_CreateStringReference (string );
@@ -511,7 +511,8 @@ static void cjson_create_string_reference_should_create_a_string_reference(void)
511
511
cJSON_Delete (string_reference );
512
512
}
513
513
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
+ {
515
516
cJSON * number_reference = NULL ;
516
517
cJSON * number_object = cJSON_CreateObject ();
517
518
cJSON * number = cJSON_CreateNumber (42 );
@@ -529,7 +530,8 @@ static void cjson_create_object_reference_should_create_an_object_reference(void
529
530
cJSON_Delete (number_reference );
530
531
}
531
532
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
+ {
533
535
cJSON * number_reference = NULL ;
534
536
cJSON * number_array = cJSON_CreateArray ();
535
537
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
566
568
{
567
569
cJSON * object = cJSON_CreateObject ();
568
570
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 );
570
572
571
573
TEST_ASSERT_NOT_NULL (object );
572
574
TEST_ASSERT_NOT_NULL (number );
@@ -626,7 +628,7 @@ static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
626
628
cJSON * item2 = cJSON_CreateStringReference (reference_valuestring );
627
629
char * ptr1 = NULL ;
628
630
char * return_value = NULL ;
629
-
631
+
630
632
cJSON_AddItemToObject (root , "one" , item1 );
631
633
cJSON_AddItemToObject (root , "two" , item2 );
632
634
@@ -650,6 +652,64 @@ static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
650
652
cJSON_Delete (root );
651
653
}
652
654
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
+
653
713
int CJSON_CDECL main (void )
654
714
{
655
715
UNITY_BEGIN ();
@@ -679,6 +739,7 @@ int CJSON_CDECL main(void)
679
739
RUN_TEST (cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased );
680
740
RUN_TEST (cjson_delete_item_from_array_should_not_broken_list_structure );
681
741
RUN_TEST (cjson_set_valuestring_to_object_should_not_leak_memory );
742
+ RUN_TEST (cjson_set_bool_value_must_not_break_objects );
682
743
683
744
return UNITY_END ();
684
745
}
0 commit comments