Alanscut 1fc755ac09 array's item should be in the list 5 년 전
..
inputs fa00278f66 Remove trailing space 7 년 전
json-patch-tests 543ab5d08a Update json-patch-tests 6 년 전
unity 166c814cda eliminate warning when compiling cJSON 5 년 전
CMakeLists.txt 19ff92da79 Add link dependency to fix tests link error when ENABLE_CJSON_UTILS is ON 6 년 전
cjson_add.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
common.h eb7c681a4b Fix tests when building as static library 7 년 전
compare_tests.c 4e114c1f31 update testcase, fixes #433 5 년 전
json_patch_tests.c 4e0c119391 Add warning -Wmissing-variable-declarations 8 년 전
minify_tests.c 08d2bc766a Fix infinite loop in cJSON_Minify 6 년 전
misc_tests.c 65578af8cc Merge pull request #453 from Alanscut/add-return-value 5 년 전
misc_utils_tests.c 18ad8a8770 misc_utils_tests: call all utils function with NULL pointers 8 년 전
old_utils_tests.c d92c0de7e8 fix encode_string_as_pointer 5 년 전
parse_array.c 1fc755ac09 array's item should be in the list 5 년 전
parse_examples.c 983bb2b4d6 Added cJSON_ParseWithLength (#358) 5 년 전
parse_hex4.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
parse_number.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
parse_object.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
parse_string.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
parse_value.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
parse_with_opts.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
print_array.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
print_number.c 446ff0d6cc fix issue#327 5 년 전
print_object.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
print_string.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
print_value.c f25b8448e4 Support default __stdcall calling convention on tests as well 6 년 전
readme_examples.c 1f970e7db3 fix double comparison 5 년 전
unity_setup.c d9fe34bade Add newline to end of unity_setup.c 6 년 전

readme_examples.c

/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include
#include
#include

#include "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"

static const char *json = "{\n\
\t\"name\":\t\"Awesome 4K\",\n\
\t\"resolutions\":\t[{\n\
\t\t\t\"width\":\t1280,\n\
\t\t\t\"height\":\t720\n\
\t\t}, {\n\
\t\t\t\"width\":\t1920,\n\
\t\t\t\"height\":\t1080\n\
\t\t}, {\n\
\t\t\t\"width\":\t3840,\n\
\t\t\t\"height\":\t2160\n\
\t\t}]\n\
}";

static char* create_monitor(void)
{
const unsigned int resolution_numbers[3][2] = {
{1280, 720},
{1920, 1080},
{3840, 2160}
};
char *string = NULL;
cJSON *name = NULL;
cJSON *resolutions = NULL;
cJSON *resolution = NULL;
cJSON *width = NULL;
cJSON *height = NULL;
size_t index = 0;

cJSON *monitor = cJSON_CreateObject();
if (monitor == NULL)
{
goto end;
}

name = cJSON_CreateString("Awesome 4K");
if (name == NULL)
{
goto end;
}
/* after creation was successful, immediately add it to the monitor,
* thereby transfering ownership of the pointer to it */
cJSON_AddItemToObject(monitor, "name", name);

resolutions = cJSON_CreateArray();
if (resolutions == NULL)
{
goto end;
}
cJSON_AddItemToObject(monitor, "resolutions", resolutions);

for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
{
resolution = cJSON_CreateObject();
if (resolution == NULL)
{
goto end;
}
cJSON_AddItemToArray(resolutions, resolution);

width = cJSON_CreateNumber(resolution_numbers[index][0]);
if (width == NULL)
{
goto end;
}
cJSON_AddItemToObject(resolution, "width", width);

height = cJSON_CreateNumber(resolution_numbers[index][1]);
if (height == NULL)
{
goto end;
}
cJSON_AddItemToObject(resolution, "height", height);
}

string = cJSON_Print(monitor);
if (string == NULL)
{
fprintf(stderr, "Failed to print monitor.\n");
}

end:
cJSON_Delete(monitor);
return string;
}

static char *create_monitor_with_helpers(void)
{
const unsigned int resolution_numbers[3][2] = {
{1280, 720},
{1920, 1080},
{3840, 2160}
};
char *string = NULL;
cJSON *resolutions = NULL;
size_t index = 0;

cJSON *monitor = cJSON_CreateObject();

if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL)
{
goto end;
}

resolutions = cJSON_AddArrayToObject(monitor, "resolutions");
if (resolutions == NULL)
{
goto end;
}

for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
{
cJSON *resolution = cJSON_CreateObject();

if (cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]) == NULL)
{
goto end;
}

if(cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]) == NULL)
{
goto end;
}

cJSON_AddItemToArray(resolutions, resolution);
}

string = cJSON_Print(monitor);
if (string == NULL) {
fprintf(stderr, "Failed to print monitor.\n");
}

end:
cJSON_Delete(monitor);
return string;
}

/* return 1 if the monitor supports full hd, 0 otherwise */
static int supports_full_hd(const char * const monitor)
{
const cJSON *resolution = NULL;
const cJSON *resolutions = NULL;
const cJSON *name = NULL;
int status = 0;
cJSON *monitor_json = cJSON_Parse(monitor);
if (monitor_json == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
status = 0;
goto end;
}

name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name");
if (cJSON_IsString(name) && (name->valuestring != NULL))
{
printf("Checking monitor \"%s\"\n", name->valuestring);
}

resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions");
cJSON_ArrayForEach(resolution, resolutions)
{
cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height");

if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height))
{
status = 0;
goto end;
}

if (compare_double(width->valuedouble, 1920) && compare_double(height->valuedouble, 1080))
{
status = 1;
goto end;
}
}

end:
cJSON_Delete(monitor_json);
return status;
}

static void create_monitor_should_create_a_monitor(void)
{
char *monitor = create_monitor();

TEST_ASSERT_EQUAL_STRING(monitor, json);

free(monitor);
}

static void create_monitor_with_helpers_should_create_a_monitor(void)
{
char *monitor = create_monitor_with_helpers();

TEST_ASSERT_EQUAL_STRING(json, monitor);

free(monitor);
}

static void supports_full_hd_should_check_for_full_hd_support(void)
{
static const char *monitor_without_hd = "{\n\
\t\t\"name\": \"lame monitor\",\n\
\t\t\"resolutions\":\t[{\n\
\t\t\t\"width\":\t640,\n\
\t\t\t\"height\":\t480\n\
\t\t}]\n\
}";

TEST_ASSERT(supports_full_hd(json));
TEST_ASSERT_FALSE(supports_full_hd(monitor_without_hd));
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();

RUN_TEST(create_monitor_should_create_a_monitor);
RUN_TEST(create_monitor_with_helpers_should_create_a_monitor);
RUN_TEST(supports_full_hd_should_check_for_full_hd_support);

return UNITY_END();
}