Browse Source

Merge pull request #112 from DaveGamble/print-tests

Add tests for print functions
pull/116/head
Max Bruckner 8 years ago
committed by GitHub
parent
commit
1bc1a9748a
  1. 2
      cJSON.h
  2. 5
      tests/CMakeLists.txt
  3. 4
      tests/parse_object.c
  4. 92
      tests/print_array.c
  5. 103
      tests/print_number.c
  6. 92
      tests/print_object.c
  7. 76
      tests/print_string.c
  8. 102
      tests/print_value.c

2
cJSON.h

@ -173,7 +173,7 @@ extern void cJSON_Minify(char *json);
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) #define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */ /* helper for the cJSON_SetNumberValue macro */
extern double cJSON_SetNumberHelper(cJSON *object, double number); extern double cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object) ? cJSON_SetNumberHelper(object, (double)number) : (number)) #define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
/* Macro for iterating over an array */ /* Macro for iterating over an array */
#define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next) #define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)

5
tests/CMakeLists.txt

@ -14,6 +14,11 @@ if(ENABLE_CJSON_TEST)
parse_array parse_array
parse_object parse_object
parse_value parse_value
print_string
print_number
print_array
print_object
print_value
) )
add_library(test-common common.c) add_library(test-common common.c)

4
tests/parse_object.c

@ -76,7 +76,7 @@ static void parse_object_should_parse_empty_objects(void)
reset(item); reset(item);
} }
static void parse_array_should_parse_arrays_with_one_element(void) static void parse_object_should_parse_objects_with_one_element(void)
{ {
assert_parse_object("{\"one\":1}"); assert_parse_object("{\"one\":1}");
@ -163,6 +163,6 @@ int main(void)
RUN_TEST(parse_object_should_parse_empty_objects); RUN_TEST(parse_object_should_parse_empty_objects);
RUN_TEST(parse_object_should_not_parse_non_objects); RUN_TEST(parse_object_should_not_parse_non_objects);
RUN_TEST(parse_object_should_parse_objects_with_multiple_elements); RUN_TEST(parse_object_should_parse_objects_with_multiple_elements);
RUN_TEST(parse_array_should_parse_arrays_with_one_element); RUN_TEST(parse_object_should_parse_objects_with_one_element);
return UNITY_END(); return UNITY_END();
} }

92
tests/print_array.c

@ -0,0 +1,92 @@
/*
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 "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"
static void assert_print_array(const char * const expected, const char * const input)
{
unsigned char printed_unformatted[1024];
unsigned char printed_formatted[1024];
const unsigned char *error_pointer;
cJSON item[1];
printbuffer formatted_buffer;
printbuffer unformatted_buffer;
/* buffer for formatted printing */
formatted_buffer.buffer = printed_formatted;
formatted_buffer.length = sizeof(printed_formatted);
formatted_buffer.offset = 0;
formatted_buffer.noalloc = true;
/* buffer for unformatted printing */
unformatted_buffer.buffer = printed_unformatted;
unformatted_buffer.length = sizeof(printed_unformatted);
unformatted_buffer.offset = 0;
unformatted_buffer.noalloc = true;
memset(item, 0, sizeof(item));
TEST_ASSERT_NOT_NULL_MESSAGE(parse_array(item, (const unsigned char*)input, &error_pointer), "Failed to parse array.");
TEST_ASSERT_NOT_NULL_MESSAGE(print_array(item, 0, false, &unformatted_buffer), "Failed to print unformatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct.");
TEST_ASSERT_NOT_NULL_MESSAGE(print_array(item, 0, true, &formatted_buffer), "Failed to print formatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct.");
reset(item);
}
static void print_array_should_print_empty_arrays(void)
{
assert_print_array("[]", "[]");
}
static void print_array_should_print_arrays_with_one_element(void)
{
assert_print_array("[1]", "[1]");
assert_print_array("[\"hello!\"]", "[\"hello!\"]");
assert_print_array("[[]]", "[[]]");
assert_print_array("[null]", "[null]");
}
static void print_array_should_print_arrays_with_multiple_elements(void)
{
assert_print_array("[1, 2, 3]", "[1,2,3]");
assert_print_array("[1, null, true, false, [], \"hello\", {\n\t}]", "[1,null,true,false,[],\"hello\",{}]");
}
int main(void)
{
/* initialize cJSON item */
UNITY_BEGIN();
RUN_TEST(print_array_should_print_empty_arrays);
RUN_TEST(print_array_should_print_arrays_with_one_element);
RUN_TEST(print_array_should_print_arrays_with_multiple_elements);
return UNITY_END();
}

103
tests/print_number.c

@ -0,0 +1,103 @@
/*
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 "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"
static void assert_print_number(const char *expected, double input)
{
unsigned char printed[1024];
cJSON item[1];
printbuffer buffer;
buffer.buffer = printed;
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
memset(item, 0, sizeof(item));
cJSON_SetNumberValue(item, input);
TEST_ASSERT_NOT_NULL_MESSAGE(print_number(item, &buffer), "Failed to print number.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected.");
}
static void print_number_should_print_zero(void)
{
assert_print_number("0", 0);
}
static void print_number_should_print_negative_integers(void)
{
assert_print_number("-1", -1);
assert_print_number("-32768", -32768);
assert_print_number("-2147483648", -2147483648);
}
static void print_number_should_print_positive_integers(void)
{
assert_print_number("1", 1);
assert_print_number("32767", 32767);
assert_print_number("2147483647", 2147483647);
}
static void print_number_should_print_positive_reals(void)
{
assert_print_number("0.123000", 0.123);
assert_print_number("1.000000e-09", 10e-10);
assert_print_number("1000000000000", 10e11);
assert_print_number("1.230000e+129", 123e+127);
assert_print_number("0", 123e-128); /* TODO: Maybe this shouldn't be 0 */
}
static void print_number_should_print_negative_reals(void)
{
assert_print_number("-0.012300", -0.0123);
assert_print_number("-1.000000e-09", -10e-10);
assert_print_number("-1000000000000000000000", -10e20);
assert_print_number("-1.230000e+129", -123e+127);
assert_print_number("-1.230000e-126", -123e-128);
}
static void print_number_should_print_non_number(void)
{
TEST_IGNORE();
/* FIXME: Cannot test this easily in C89! */
/* assert_print_number("null", NaN); */
/* assert_print_number("null", INFTY); */
/* assert_print_number("null", -INFTY); */
}
int main(void)
{
/* initialize cJSON item */
UNITY_BEGIN();
RUN_TEST(print_number_should_print_zero);
RUN_TEST(print_number_should_print_negative_integers);
RUN_TEST(print_number_should_print_positive_integers);
RUN_TEST(print_number_should_print_positive_reals);
RUN_TEST(print_number_should_print_negative_reals);
RUN_TEST(print_number_should_print_non_number);
return UNITY_END();
}

92
tests/print_object.c

@ -0,0 +1,92 @@
/*
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 "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"
static void assert_print_object(const char * const expected, const char * const input)
{
unsigned char printed_unformatted[1024];
unsigned char printed_formatted[1024];
const unsigned char *error_pointer;
cJSON item[1];
printbuffer formatted_buffer;
printbuffer unformatted_buffer;
/* buffer for formatted printing */
formatted_buffer.buffer = printed_formatted;
formatted_buffer.length = sizeof(printed_formatted);
formatted_buffer.offset = 0;
formatted_buffer.noalloc = true;
/* buffer for unformatted printing */
unformatted_buffer.buffer = printed_unformatted;
unformatted_buffer.length = sizeof(printed_unformatted);
unformatted_buffer.offset = 0;
unformatted_buffer.noalloc = true;
memset(item, 0, sizeof(item));
TEST_ASSERT_NOT_NULL_MESSAGE(parse_object(item, (const unsigned char*)input, &error_pointer), "Failed to parse object.");
TEST_ASSERT_NOT_NULL_MESSAGE(print_object(item, 0, false, &unformatted_buffer), "Failed to print unformatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct.");
TEST_ASSERT_NOT_NULL_MESSAGE(print_object(item, 0, true, &formatted_buffer), "Failed to print formatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct.");
reset(item);
}
static void print_object_should_print_empty_objects(void)
{
assert_print_object("{\n}", "{}");
}
static void print_object_should_print_objects_with_one_element(void)
{
assert_print_object("{\n\t\"one\":\t1\n}", "{\"one\":1}");
assert_print_object("{\n\t\"hello\":\t\"world!\"\n}", "{\"hello\":\"world!\"}");
assert_print_object("{\n\t\"array\":\t[]\n}", "{\"array\":[]}");
assert_print_object("{\n\t\"null\":\tnull\n}", "{\"null\":null}");
}
static void print_object_should_print_objects_with_multiple_elements(void)
{
assert_print_object("{\n\t\"one\":\t1,\n\t\"two\":\t2,\n\t\"three\":\t3\n}", "{\"one\":1,\"two\":2,\"three\":3}");
assert_print_object("{\n\t\"one\":\t1,\n\t\"NULL\":\tnull,\n\t\"TRUE\":\ttrue,\n\t\"FALSE\":\tfalse,\n\t\"array\":\t[],\n\t\"world\":\t\"hello\",\n\t\"object\":\t{\n\t}\n}", "{\"one\":1,\"NULL\":null,\"TRUE\":true,\"FALSE\":false,\"array\":[],\"world\":\"hello\",\"object\":{}}");
}
int main(void)
{
/* initialize cJSON item */
UNITY_BEGIN();
RUN_TEST(print_object_should_print_empty_objects);
RUN_TEST(print_object_should_print_objects_with_one_element);
RUN_TEST(print_object_should_print_objects_with_multiple_elements);
return UNITY_END();
}

76
tests/print_string.c

@ -0,0 +1,76 @@
/*
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 "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"
static void assert_print_string(const char *expected, const char *input)
{
unsigned char printed[1024];
printbuffer buffer;
buffer.buffer = printed;
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
TEST_ASSERT_NOT_NULL_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");
}
static void print_string_should_print_empty_strings(void)
{
assert_print_string("\"\"", "");
}
static void print_string_should_print_ascii(void)
{
char ascii[0x7F];
size_t i = 1;
/* create ascii table */
for (i = 1; i < 0x7F; i++)
{
ascii[i-1] = (char)i;
}
ascii[0x7F-1] = '\0';
assert_print_string("\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\"",
ascii);
}
static void print_string_should_print_utf8(void)
{
assert_print_string("\"ü猫慕\"", "ü猫慕");
}
int main(void)
{
/* initialize cJSON item */
UNITY_BEGIN();
RUN_TEST(print_string_should_print_empty_strings);
RUN_TEST(print_string_should_print_ascii);
RUN_TEST(print_string_should_print_utf8);
return UNITY_END();
}

102
tests/print_value.c

@ -0,0 +1,102 @@
/*
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"
static void assert_print_value(const char *input)
{
unsigned char printed[1024];
const unsigned char *error_pointer = NULL;
cJSON item[1];
printbuffer buffer;
buffer.buffer = printed;
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
memset(item, 0, sizeof(item));
TEST_ASSERT_NOT_NULL_MESSAGE(parse_value(item, (const unsigned char*)input, &error_pointer), "Failed to parse value.");
TEST_ASSERT_NOT_NULL_MESSAGE(print_value(item, 0, false, &buffer), "Failed to print value.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, buffer.buffer, "Printed value is not as expected.");
reset(item);
}
static void print_value_should_print_null(void)
{
assert_print_value("null");
}
static void print_value_should_print_true(void)
{
assert_print_value("true");
}
static void print_value_should_print_false(void)
{
assert_print_value("false");
}
static void print_value_should_print_number(void)
{
assert_print_value("1.500000");
}
static void print_value_should_print_string(void)
{
assert_print_value("\"\"");
assert_print_value("\"hello\"");
}
static void print_value_should_print_array(void)
{
assert_print_value("[]");
}
static void print_value_should_print_object(void)
{
assert_print_value("{}");
}
int main(void)
{
/* initialize cJSON item */
UNITY_BEGIN();
RUN_TEST(print_value_should_print_null);
RUN_TEST(print_value_should_print_true);
RUN_TEST(print_value_should_print_false);
RUN_TEST(print_value_should_print_number);
RUN_TEST(print_value_should_print_string);
RUN_TEST(print_value_should_print_array);
RUN_TEST(print_value_should_print_object);
return UNITY_END();
}
Loading…
Cancel
Save