Max Bruckner 08738673be Reorder the context fields 7 lat temu
..
inputs fa00278f66 Remove trailing space 7 lat temu
json-patch-tests d26a42af8d json patch: adding to a subfield of a non-object now fails as expected 7 lat temu
unity 0476590a0c Update Unity to 2.4.3 7 lat temu
CMakeLists.txt 064eec8208 Change name from Configuration to Context 7 lat temu
cjson_add.c 77931e7fc0 cJSON_Add...ToObject: Add tests for failure conditions 7 lat temu
common.h 064eec8208 Change name from Configuration to Context 7 lat temu
compare_tests.c 03ba72faec cJSON_Compare: Fix comparison of objects 8 lat temu
context_tests.c 409c2aaea7 cJSON_MakeDuplicateRecursive 7 lat temu
json_patch_tests.c 4e0c119391 Add warning -Wmissing-variable-declarations 8 lat temu
misc_tests.c 08738673be Reorder the context fields 7 lat temu
misc_utils_tests.c 18ad8a8770 misc_utils_tests: call all utils function with NULL pointers 8 lat temu
old_utils_tests.c b537ca70a3 old_utils_tests: Remove leftover unused attribute 8 lat temu
parse_array.c 064eec8208 Change name from Configuration to Context 7 lat temu
parse_examples.c 1b2236a9a6 Improve existing tests showing behaviour of Parse and ParseWithOpts functions. 7 lat temu
parse_hex4.c bfbd8fe0d8 tests/parse_hex4: Fix GCC 7 compiler warning (fixes #179) 8 lat temu
parse_number.c 064eec8208 Change name from Configuration to Context 7 lat temu
parse_object.c 064eec8208 Change name from Configuration to Context 7 lat temu
parse_string.c 064eec8208 Change name from Configuration to Context 7 lat temu
parse_value.c 064eec8208 Change name from Configuration to Context 7 lat temu
parse_with_opts.c 97d7347a6e Merge branch 'develop' 7 lat temu
print_array.c 064eec8208 Change name from Configuration to Context 7 lat temu
print_number.c 064eec8208 Change name from Configuration to Context 7 lat temu
print_object.c 064eec8208 Change name from Configuration to Context 7 lat temu
print_string.c 064eec8208 Change name from Configuration to Context 7 lat temu
print_value.c 064eec8208 Change name from Configuration to Context 7 lat temu
readme_examples.c e7d0c1dc37 Tests: Test if the readme examples are working 7 lat temu

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 ((width->valuedouble == 1920) && (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 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();
}