tests: extract common functionality to common.c

This commit is contained in:
Max Bruckner 2017-02-15 18:41:55 +01:00
parent 3facca4792
commit 4f58695ed3
10 changed files with 158 additions and 115 deletions

View File

@ -16,6 +16,8 @@ if(ENABLE_CJSON_TEST)
parse_value parse_value
) )
add_library(test-common common.c)
option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.") option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")
if (ENABLE_VALGRIND) if (ENABLE_VALGRIND)
find_program(MEMORYCHECK_COMMAND valgrind) find_program(MEMORYCHECK_COMMAND valgrind)
@ -29,7 +31,7 @@ if(ENABLE_CJSON_TEST)
foreach(unity_test ${unity_tests}) foreach(unity_test ${unity_tests})
add_executable("${unity_test}" "${unity_test}.c") 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) if(MEMORYCHECK_COMMAND)
add_test(NAME "${unity_test}" add_test(NAME "${unity_test}"
COMMAND "${MEMORYCHECK_COMMAND}" ${MEMORYCHECK_COMMAND_OPTIONS} "./${unity_test}") COMMAND "${MEMORYCHECK_COMMAND}" ${MEMORYCHECK_COMMAND_OPTIONS} "./${unity_test}")

97
tests/common.c Normal file
View File

@ -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;
}

31
tests/common.h Normal file
View File

@ -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

View File

@ -26,7 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.c" #include "common.h"
static cJSON item[1]; static cJSON item[1];
@ -56,15 +56,6 @@ static void assert_parse_array(const char *json)
assert_is_array(item); 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) static void parse_array_should_parse_empty_arrays(void)
{ {
assert_parse_array("[]"); assert_parse_array("[]");
@ -80,24 +71,24 @@ static void parse_array_should_parse_arrays_with_one_element(void)
assert_parse_array("[1]"); assert_parse_array("[1]");
TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_NOT_NULL(item->child);
TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->type); TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->type);
reset(); reset(item);
assert_parse_array("[\"hello!\"]"); assert_parse_array("[\"hello!\"]");
TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_NOT_NULL(item->child);
TEST_ASSERT_BITS(0xFF, cJSON_String, item->child->type); TEST_ASSERT_BITS(0xFF, cJSON_String, item->child->type);
TEST_ASSERT_EQUAL_STRING("hello!", item->child->valuestring); TEST_ASSERT_EQUAL_STRING("hello!", item->child->valuestring);
reset(); reset(item);
assert_parse_array("[[]]"); assert_parse_array("[[]]");
TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_NOT_NULL(item->child);
assert_is_array(item->child); assert_is_array(item->child);
TEST_ASSERT_NULL(item->child->child); TEST_ASSERT_NULL(item->child->child);
reset(); reset(item);
assert_parse_array("[null]"); assert_parse_array("[null]");
TEST_ASSERT_NOT_NULL(item->child); TEST_ASSERT_NOT_NULL(item->child);
TEST_ASSERT_BITS(0xFF, cJSON_NULL, item->child->type); TEST_ASSERT_BITS(0xFF, cJSON_NULL, item->child->type);
reset(); reset(item);
} }
static void parse_array_should_parse_arrays_with_multiple_elements(void) 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->type);
TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->type); TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->type);
TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->next->type); TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->next->type);
reset(); reset(item);
{ {
size_t i = 0; 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_BITS(0xFF, expected_types[i], node->type);
} }
TEST_ASSERT_EQUAL_INT(i, 7); TEST_ASSERT_EQUAL_INT(i, 7);
reset(); reset(item);
} }
} }

View File

@ -26,63 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.h" #include "common.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;
}
static cJSON *parse_file(const char *filename) static cJSON *parse_file(const char *filename)
{ {

View File

@ -26,7 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.c" #include "common.h"
static void parse_hex4_should_parse_all_combinations(void) static void parse_hex4_should_parse_all_combinations(void)
{ {

View File

@ -26,7 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.c" #include "common.h"
static cJSON item[1]; static cJSON item[1];

View File

@ -26,7 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.c" #include "common.h"
static cJSON item[1]; static cJSON item[1];
@ -64,15 +64,6 @@ static void assert_parse_object(const char *json)
assert_is_object(item); 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) static void parse_object_should_parse_empty_objects(void)
{ {
assert_parse_object("{}"); assert_parse_object("{}");
@ -86,19 +77,19 @@ static void parse_array_should_parse_arrays_with_one_element(void)
assert_parse_object("{\"one\":1}"); assert_parse_object("{\"one\":1}");
assert_is_child(item->child, "one", cJSON_Number); assert_is_child(item->child, "one", cJSON_Number);
reset(); reset(item);
assert_parse_object("{\"hello\":\"world!\"}"); assert_parse_object("{\"hello\":\"world!\"}");
assert_is_child(item->child, "hello", cJSON_String); assert_is_child(item->child, "hello", cJSON_String);
reset(); reset(item);
assert_parse_object("{\"array\":[]}"); assert_parse_object("{\"array\":[]}");
assert_is_child(item->child, "array", cJSON_Array); assert_is_child(item->child, "array", cJSON_Array);
reset(); reset(item);
assert_parse_object("{\"null\":null}"); assert_parse_object("{\"null\":null}");
assert_is_child(item->child, "null", cJSON_NULL); assert_is_child(item->child, "null", cJSON_NULL);
reset(); reset(item);
} }
static void parse_object_should_parse_objects_with_multiple_elements(void) 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, "one", cJSON_Number);
assert_is_child(item->child->next, "two", cJSON_Number); assert_is_child(item->child->next, "two", cJSON_Number);
assert_is_child(item->child->next->next, "three", cJSON_Number); assert_is_child(item->child->next->next, "three", cJSON_Number);
reset(); reset(item);
{ {
size_t i = 0; 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]); assert_is_child(node, expected_names[i], expected_types[i]);
} }
TEST_ASSERT_EQUAL_INT(i, 7); TEST_ASSERT_EQUAL_INT(i, 7);
reset(); reset(item);
} }
} }

View File

@ -26,7 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.c" #include "common.h"
static cJSON item[1]; static cJSON item[1];

View File

@ -26,7 +26,7 @@
#include "unity/examples/unity_config.h" #include "unity/examples/unity_config.h"
#include "unity/src/unity.h" #include "unity/src/unity.h"
#include "../cJSON.c" #include "common.h"
static cJSON item[1]; static cJSON item[1];
const unsigned char *error_pointer = NULL; 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); 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) static void parse_value_should_parse_null(void)
{ {
assert_parse_value("null", cJSON_NULL); assert_parse_value("null", cJSON_NULL);
reset(); reset(item);
} }
static void parse_value_should_parse_true(void) static void parse_value_should_parse_true(void)
{ {
assert_parse_value("true", cJSON_True); assert_parse_value("true", cJSON_True);
reset(); reset(item);
} }
static void parse_value_should_parse_false(void) static void parse_value_should_parse_false(void)
{ {
assert_parse_value("false", cJSON_False); assert_parse_value("false", cJSON_False);
reset(); reset(item);
} }
static void parse_value_should_parse_number(void) static void parse_value_should_parse_number(void)
{ {
assert_parse_value("1.5", cJSON_Number); assert_parse_value("1.5", cJSON_Number);
reset(); reset(item);
} }
static void parse_value_should_parse_string(void) static void parse_value_should_parse_string(void)
{ {
assert_parse_value("\"\"", cJSON_String); assert_parse_value("\"\"", cJSON_String);
reset(); reset(item);
assert_parse_value("\"hello\"", cJSON_String); assert_parse_value("\"hello\"", cJSON_String);
reset(); reset(item);
} }
static void parse_value_should_parse_array(void) static void parse_value_should_parse_array(void)
{ {
assert_parse_value("[]", cJSON_Array); assert_parse_value("[]", cJSON_Array);
reset(); reset(item);
} }
static void parse_value_should_parse_object(void) static void parse_value_should_parse_object(void)
{ {
assert_parse_value("{}", cJSON_Object); assert_parse_value("{}", cJSON_Object);
reset(); reset(item);
} }
int main(void) int main(void)