Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
if(item)
{
item->type = cJSON_True;
item->valueint = 1;
}

return item;
Expand All @@ -2497,6 +2498,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
if(item)
{
item->type = boolean ? cJSON_True : cJSON_False;
item->valueint = boolean ? 1 : 0;
}

return item;
Expand Down
22 changes: 22 additions & 0 deletions tests/misc_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,27 @@ static void cjson_parse_big_numbers_should_not_report_error(void)
cJSON_Delete(valid_big_number_json_object2);
}

static void cjson_create_true_should_set_valueint(void)
{
cJSON *parsed = cJSON_Parse("true");
cJSON *created_true = cJSON_CreateTrue();
cJSON *created_bool = cJSON_CreateBool(1);
cJSON *created_false = cJSON_CreateFalse();
cJSON *created_bool_false = cJSON_CreateBool(0);

TEST_ASSERT_EQUAL_INT(1, parsed->valueint);
TEST_ASSERT_EQUAL_INT(1, created_true->valueint);
TEST_ASSERT_EQUAL_INT(1, created_bool->valueint);
TEST_ASSERT_EQUAL_INT(0, created_false->valueint);
TEST_ASSERT_EQUAL_INT(0, created_bool_false->valueint);

cJSON_Delete(parsed);
cJSON_Delete(created_true);
cJSON_Delete(created_bool);
cJSON_Delete(created_false);
cJSON_Delete(created_bool_false);
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();
Expand Down Expand Up @@ -833,6 +854,7 @@ int CJSON_CDECL main(void)
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
RUN_TEST(cjson_set_bool_value_must_not_break_objects);
RUN_TEST(cjson_parse_big_numbers_should_not_report_error);
RUN_TEST(cjson_create_true_should_set_valueint);

return UNITY_END();
}