From 4f58695ed32065f34a509fcda6c7b481059b754a Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Wed, 15 Feb 2017 18:41:55 +0100 Subject: [PATCH] tests: extract common functionality to common.c --- tests/CMakeLists.txt | 4 +- tests/common.c | 97 ++++++++++++++++++++++++++++++++++++++++++ tests/common.h | 31 ++++++++++++++ tests/parse_array.c | 23 +++------- tests/parse_examples.c | 58 +------------------------ tests/parse_hex4.c | 2 +- tests/parse_number.c | 2 +- tests/parse_object.c | 23 +++------- tests/parse_string.c | 2 +- tests/parse_value.c | 31 ++++---------- 10 files changed, 158 insertions(+), 115 deletions(-) create mode 100644 tests/common.c create mode 100644 tests/common.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7107c5..c372127 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,6 +16,8 @@ if(ENABLE_CJSON_TEST) parse_value ) + add_library(test-common common.c) + option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.") if (ENABLE_VALGRIND) find_program(MEMORYCHECK_COMMAND valgrind) @@ -29,7 +31,7 @@ if(ENABLE_CJSON_TEST) foreach(unity_test ${unity_tests}) add_executable("${unity_test}" "${unity_test}.c") - target_link_libraries("${unity_test}" "${CJSON_LIB}" unity) + target_link_libraries("${unity_test}" "${CJSON_LIB}" unity test-common) if(MEMORYCHECK_COMMAND) add_test(NAME "${unity_test}" COMMAND "${MEMORYCHECK_COMMAND}" ${MEMORYCHECK_COMMAND_OPTIONS} "./${unity_test}") diff --git a/tests/common.c b/tests/common.c new file mode 100644 index 0000000..65ef97c --- /dev/null +++ b/tests/common.c @@ -0,0 +1,97 @@ +/* + 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 "common.h" + +extern void reset(cJSON *item) +{ + if ((item != NULL) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if ((item->valuestring != NULL) && !(item->type & cJSON_IsReference)) + { + cJSON_free(item->valuestring); + } + if ((item->string != NULL) && !(item->type & cJSON_StringIsConst)) + { + cJSON_free(item->string); + } + + memset(item, 0, sizeof(cJSON)); +} + +extern char *read_file(const char *filename) +{ + FILE *file = NULL; + long length = 0; + char *content = NULL; + size_t read_chars = 0; + + /* open in read binary mode */ + file = fopen(filename, "rb"); + if (file == NULL) + { + goto cleanup; + } + + /* get the length */ + if (fseek(file, 0, SEEK_END) != 0) + { + goto cleanup; + } + length = ftell(file); + if (length < 0) + { + goto cleanup; + } + if (fseek(file, 0, SEEK_SET) != 0) + { + goto cleanup; + } + + /* allocate content buffer */ + content = (char*)malloc((size_t)length + sizeof('\0')); + if (content == NULL) + { + goto cleanup; + } + + /* read the file into memory */ + read_chars = fread(content, sizeof(char), (size_t)length, file); + if ((long)read_chars != length) + { + free(content); + content = NULL; + goto cleanup; + } + content[read_chars] = '\0'; + + +cleanup: + if (file != NULL) + { + fclose(file); + } + + return content; +} diff --git a/tests/common.h b/tests/common.h new file mode 100644 index 0000000..ea87a57 --- /dev/null +++ b/tests/common.h @@ -0,0 +1,31 @@ +/* + 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. +*/ + +#ifndef CJSON_TESTS_COMMON_H +#define CJSON_TESTS_COMMON_H + +#include "../cJSON.c" + +extern void reset(cJSON *item); +extern char *read_file(const char *filename); + +#endif diff --git a/tests/parse_array.c b/tests/parse_array.c index 306964c..e8638f5 100644 --- a/tests/parse_array.c +++ b/tests/parse_array.c @@ -26,7 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.c" +#include "common.h" static cJSON item[1]; @@ -56,15 +56,6 @@ static void assert_parse_array(const char *json) assert_is_array(item); } -static void reset(void) -{ - if (item->child != NULL) - { - cJSON_Delete(item->child); - } - memset(item, 0, sizeof(cJSON)); -} - static void parse_array_should_parse_empty_arrays(void) { assert_parse_array("[]"); @@ -80,24 +71,24 @@ static void parse_array_should_parse_arrays_with_one_element(void) assert_parse_array("[1]"); TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->type); - reset(); + reset(item); assert_parse_array("[\"hello!\"]"); TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_BITS(0xFF, cJSON_String, item->child->type); TEST_ASSERT_EQUAL_STRING("hello!", item->child->valuestring); - reset(); + reset(item); assert_parse_array("[[]]"); TEST_ASSERT_NOT_NULL(item->child); assert_is_array(item->child); TEST_ASSERT_NULL(item->child->child); - reset(); + reset(item); assert_parse_array("[null]"); TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_BITS(0xFF, cJSON_NULL, item->child->type); - reset(); + reset(item); } static void parse_array_should_parse_arrays_with_multiple_elements(void) @@ -110,7 +101,7 @@ static void parse_array_should_parse_arrays_with_multiple_elements(void) TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->type); TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->type); TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->next->type); - reset(); + reset(item); { size_t i = 0; @@ -137,7 +128,7 @@ static void parse_array_should_parse_arrays_with_multiple_elements(void) TEST_ASSERT_BITS(0xFF, expected_types[i], node->type); } TEST_ASSERT_EQUAL_INT(i, 7); - reset(); + reset(item); } } diff --git a/tests/parse_examples.c b/tests/parse_examples.c index 8232e8a..7562c45 100644 --- a/tests/parse_examples.c +++ b/tests/parse_examples.c @@ -26,63 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.h" - -static char *read_file(const char *filename) -{ - FILE *file = NULL; - long length = 0; - char *content = NULL; - size_t read_chars = 0; - - /* open in read binary mode */ - file = fopen(filename, "rb"); - if (file == NULL) - { - goto cleanup; - } - - /* get the length */ - if (fseek(file, 0, SEEK_END) != 0) - { - goto cleanup; - } - length = ftell(file); - if (length < 0) - { - goto cleanup; - } - if (fseek(file, 0, SEEK_SET) != 0) - { - goto cleanup; - } - - /* allocate content buffer */ - content = (char*)malloc((size_t)length + sizeof('\0')); - if (content == NULL) - { - goto cleanup; - } - - /* read the file into memory */ - read_chars = fread(content, sizeof(char), (size_t)length, file); - if ((long)read_chars != length) - { - free(content); - content = NULL; - goto cleanup; - } - content[read_chars] = '\0'; - - -cleanup: - if (file != NULL) - { - fclose(file); - } - - return content; -} +#include "common.h" static cJSON *parse_file(const char *filename) { diff --git a/tests/parse_hex4.c b/tests/parse_hex4.c index 286157f..59b07ee 100644 --- a/tests/parse_hex4.c +++ b/tests/parse_hex4.c @@ -26,7 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.c" +#include "common.h" static void parse_hex4_should_parse_all_combinations(void) { diff --git a/tests/parse_number.c b/tests/parse_number.c index bfd0914..4419d40 100644 --- a/tests/parse_number.c +++ b/tests/parse_number.c @@ -26,7 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.c" +#include "common.h" static cJSON item[1]; diff --git a/tests/parse_object.c b/tests/parse_object.c index 3031851..93b543c 100644 --- a/tests/parse_object.c +++ b/tests/parse_object.c @@ -26,7 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.c" +#include "common.h" static cJSON item[1]; @@ -64,15 +64,6 @@ static void assert_parse_object(const char *json) assert_is_object(item); } -static void reset(void) -{ - if (item->child != NULL) - { - cJSON_Delete(item->child); - } - memset(item, 0, sizeof(cJSON)); -} - static void parse_object_should_parse_empty_objects(void) { assert_parse_object("{}"); @@ -86,19 +77,19 @@ static void parse_array_should_parse_arrays_with_one_element(void) assert_parse_object("{\"one\":1}"); assert_is_child(item->child, "one", cJSON_Number); - reset(); + reset(item); assert_parse_object("{\"hello\":\"world!\"}"); assert_is_child(item->child, "hello", cJSON_String); - reset(); + reset(item); assert_parse_object("{\"array\":[]}"); assert_is_child(item->child, "array", cJSON_Array); - reset(); + reset(item); assert_parse_object("{\"null\":null}"); assert_is_child(item->child, "null", cJSON_NULL); - reset(); + reset(item); } static void parse_object_should_parse_objects_with_multiple_elements(void) @@ -107,7 +98,7 @@ static void parse_object_should_parse_objects_with_multiple_elements(void) assert_is_child(item->child, "one", cJSON_Number); assert_is_child(item->child->next, "two", cJSON_Number); assert_is_child(item->child->next->next, "three", cJSON_Number); - reset(); + reset(item); { size_t i = 0; @@ -144,7 +135,7 @@ static void parse_object_should_parse_objects_with_multiple_elements(void) assert_is_child(node, expected_names[i], expected_types[i]); } TEST_ASSERT_EQUAL_INT(i, 7); - reset(); + reset(item); } } diff --git a/tests/parse_string.c b/tests/parse_string.c index b121eff..8fd6920 100644 --- a/tests/parse_string.c +++ b/tests/parse_string.c @@ -26,7 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.c" +#include "common.h" static cJSON item[1]; diff --git a/tests/parse_value.c b/tests/parse_value.c index f0c8b66..7528df7 100644 --- a/tests/parse_value.c +++ b/tests/parse_value.c @@ -26,7 +26,7 @@ #include "unity/examples/unity_config.h" #include "unity/src/unity.h" -#include "../cJSON.c" +#include "common.h" static cJSON item[1]; const unsigned char *error_pointer = NULL; @@ -49,61 +49,48 @@ static void assert_parse_value(const char *string, int type) assert_is_value(item, type); } -static void reset(void) -{ - if (item->child != NULL) - { - cJSON_Delete(item->child); - } - if (item->valuestring != NULL) - { - cJSON_free(item->valuestring); - } - memset(item, 0, sizeof(cJSON)); -} - static void parse_value_should_parse_null(void) { assert_parse_value("null", cJSON_NULL); - reset(); + reset(item); } static void parse_value_should_parse_true(void) { assert_parse_value("true", cJSON_True); - reset(); + reset(item); } static void parse_value_should_parse_false(void) { assert_parse_value("false", cJSON_False); - reset(); + reset(item); } static void parse_value_should_parse_number(void) { assert_parse_value("1.5", cJSON_Number); - reset(); + reset(item); } static void parse_value_should_parse_string(void) { assert_parse_value("\"\"", cJSON_String); - reset(); + reset(item); assert_parse_value("\"hello\"", cJSON_String); - reset(); + reset(item); } static void parse_value_should_parse_array(void) { assert_parse_value("[]", cJSON_Array); - reset(); + reset(item); } static void parse_value_should_parse_object(void) { assert_parse_value("{}", cJSON_Object); - reset(); + reset(item); } int main(void)