From 86de4d2bb34e5db91364cce7d8a600e259263176 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Tue, 24 Mar 2020 14:53:02 -0700 Subject: [PATCH] Add testing targets to cmake. Improve output test --- CMakeLists.txt | 6 + tests/CMakeLists.txt | 4 +- tests/output.c | 289 ++++++++++++++++++++++++++++++++++++------- tests/parse.c | 3 + 4 files changed, 253 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec0393d..36ee0e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,12 @@ if(ENABLE_C++) endif() if(BUILD_TESTS) + enable_testing() + add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} + --force-new-ctest-process + --verbose + --output-on-failure + ) add_subdirectory(tests) endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 09ff0de..5ea43e8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.0.2) project(rjp_tests) set(INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../include) include_directories("${INCLUDE_PATH}") - add_compile_options(-Wall -Wextra -pedantic) link_libraries(rjp) @@ -13,3 +12,6 @@ endif() add_executable(parse "parse.c") add_executable(output "output.c") + +add_test(NAME parse-test COMMAND parse) +add_test(NAME output-test COMMAND output) diff --git a/tests/output.c b/tests/output.c index a29a336..9dfc933 100644 --- a/tests/output.c +++ b/tests/output.c @@ -1,60 +1,253 @@ -#include "rjp.h" +#include "rjp_internal.h" +#include "rjp_value.h" #include +#include -RJP_value* do_object(void){ - RJP_value* obj = rjp_new_object(); - RJP_value* mem = rjp_new_member_key_copy(obj, "key", 0); - rjp_set_int(mem, 10); - return obj; +typedef struct{ + RJP_value* (*create)(void); + const char* res; + int fmt; +}test_pair; + +//Basic values excluding float because precision bs and stuff +RJP_value* case_1(void){ + return rjp_new_null(); } -RJP_value* do_ordered_object(void){ - RJP_value* obj = rjp_new_ordered_object(); - RJP_value* mem = rjp_new_member_key_copy(obj, "key", 0); - rjp_set_int(mem, 10); - return obj; -} -RJP_value* do_array(void){ - RJP_value* arr = rjp_new_array(); - RJP_value* elm = rjp_new_element(arr); - rjp_set_int(elm, 10); - return arr; -} -RJP_value* do_string(void){ - RJP_value* str = rjp_new_string_copy("str", 0); - return str; -} -RJP_value* do_integer(void){ +RJP_value* case_2(void){ return rjp_new_int(5); } -RJP_value* do_dfloat(void){ - return rjp_new_float(5); -} -RJP_value* do_boolean(void){ +RJP_value* case_3(void){ return rjp_new_bool(1); } -RJP_value* do_null(void){ - return rjp_new_null(); +RJP_value* case_4(void){ + return rjp_new_bool(0); +} +RJP_value* case_5(void){ + return rjp_new_object(); +} +RJP_value* case_6(void){ + return rjp_new_array(); +} +RJP_value* case_7(void){ + return rjp_new_ordered_object(); +} +//handle object with member +RJP_value* case_8(void){ + RJP_value* obj = rjp_new_object(); + rjp_add_member_key_copy(obj, "key", 0, rjp_new_int(7)); + return obj; +} +//handle object with subobject +RJP_value* case_9(void){ + RJP_value* obj = rjp_new_object(); + RJP_value* sub = rjp_new_member_key_copy(obj, "key", 0); + rjp_set_object(sub); + rjp_add_member_key_copy(sub, "subkey", 0, rjp_new_bool(0)); + return obj; +} +//handle object with multiple members +RJP_value* case_10(void){ + RJP_value* obj = rjp_new_object(); + RJP_value* sub = rjp_new_member_key_copy(obj, "key", 0); + rjp_set_object(sub); + rjp_add_member_key_copy(sub, "subkey", 0, rjp_new_bool(0)); + rjp_add_member_key_copy(sub, "subkey2", 0, rjp_new_bool(1)); + return obj; +} +//handle object member ordering +RJP_value* case_11(void){ + RJP_value* obj = rjp_new_object(); + RJP_value* sub = rjp_new_member_key_copy(obj, "key", 0); + rjp_set_object(sub); + rjp_add_member_key_copy(sub, "subkey2", 0, rjp_new_bool(1)); + rjp_add_member_key_copy(sub, "subkey", 0, rjp_new_bool(0)); + return obj; +} +//handle orderedobject member ordering +RJP_value* case_12(void){ + RJP_value* obj = rjp_new_object(); + RJP_value* sub = rjp_new_member_key_copy(obj, "key", 0); + rjp_set_ordered_object(sub); + rjp_add_member_key_copy(sub, "subkey", 0, rjp_new_bool(0)); + rjp_add_member_key_copy(sub, "subkey2", 0, rjp_new_bool(1)); + return obj; +} +//handle orderedobject member ordering pt2 +RJP_value* case_13(void){ + RJP_value* obj = rjp_new_object(); + RJP_value* sub = rjp_new_member_key_copy(obj, "key", 0); + rjp_set_ordered_object(sub); + rjp_add_member_key_copy(sub, "subkey2", 0, rjp_new_bool(1)); + rjp_add_member_key_copy(sub, "subkey", 0, rjp_new_bool(0)); + return obj; +} +//handle array with element +RJP_value* case_14(void){ + RJP_value* arr = rjp_new_array(); + rjp_add_element(arr, rjp_new_int(5)); + return arr; +} +//handle array with subarray +RJP_value* case_15(void){ + RJP_value* arr = rjp_new_array(); + RJP_value* sub = rjp_new_element(arr); + rjp_set_array(sub); + rjp_add_element(sub, rjp_new_bool(0)); + return arr; +} +//handle array with multiple elements +RJP_value* case_16(void){ + RJP_value* arr = rjp_new_array(); + RJP_value* sub = rjp_new_element(arr); + rjp_set_array(sub); + rjp_add_element(sub, rjp_new_bool(0)); + rjp_add_element(sub, rjp_new_bool(1)); + return arr; +} +//handle array with multiple elements and subarray +RJP_value* case_17(void){ + RJP_value* arr = rjp_new_array(); + rjp_add_element(arr, rjp_new_int(5)); + RJP_value* sub = rjp_new_element(arr); + rjp_set_array(sub); + rjp_add_element(sub, rjp_new_bool(0)); + rjp_add_element(sub, rjp_new_bool(1)); + return arr; +} +//handle array with subobject with subarray +RJP_value* case_18(void){ + RJP_value* arr = rjp_new_array(); + rjp_add_element(arr, rjp_new_int(5)); + RJP_value* subobj = rjp_new_element(arr); + rjp_set_object(subobj); + RJP_value* subarr = rjp_new_member_key_copy(subobj, "key", 0); + rjp_set_array(subarr); + rjp_add_element(subarr, rjp_new_bool(0)); + return arr; +} +//handle object with many members +RJP_value* case_19(void){ + char c[] = "key0"; + RJP_value* arr = rjp_new_array(); + rjp_add_element(arr, rjp_new_int(5)); + RJP_value* subobj = rjp_new_element(arr); + rjp_set_object(subobj); + for(int i = 0;i < 10;++i){ + RJP_value* newmem = rjp_new_member_key_copy(subobj, c, 0); + c[3] += 1; + if(i % 2 == 0) + rjp_set_bool(newmem, 1); + else + rjp_set_bool(newmem, 0); + } + return arr; +} +//handle orderedobject with many members as array element +RJP_value* case_20(void){ + char c[] = "key9"; + RJP_value* arr = rjp_new_array(); + rjp_add_element(arr, rjp_new_int(5)); + RJP_value* subobj = rjp_new_element(arr); + rjp_set_ordered_object(subobj); + for(int i = 0;i < 10;++i){ + RJP_value* newmem = rjp_new_member_key_copy(subobj, c, 0); + c[3] -= 1; + if(i % 2 == 0) + rjp_set_bool(newmem, 1); + else + rjp_set_bool(newmem, 0); + } + return arr; +} +//handle array with many element as object member +RJP_value* case_21(void){ + RJP_value* obj = rjp_new_object(); + RJP_value* arr = rjp_new_member_key_copy(obj, "arr", 0); + rjp_set_array(arr); + for(int i = 0;i < 10;++i) + rjp_set_int(rjp_new_element(arr), i); + return obj; +} + +static test_pair tests[] = { + {case_1, "null", RJP_FORMAT_NONE}, + {case_2, "5", RJP_FORMAT_NONE}, + {case_3, "true", RJP_FORMAT_NONE}, + {case_4, "false", RJP_FORMAT_NONE}, + {case_5, "{}", RJP_FORMAT_NONE}, + {case_6, "[]", RJP_FORMAT_NONE}, + {case_7, "{}", RJP_FORMAT_NONE}, + {case_8, "{\"key\":7}", RJP_FORMAT_NONE}, + {case_9, "{\"key\":{\"subkey\":false}}", RJP_FORMAT_NONE}, + {case_10, "{\"key\":{\"subkey\":false,\"subkey2\":true}}", RJP_FORMAT_NONE}, + {case_11, "{\"key\":{\"subkey\":false,\"subkey2\":true}}", RJP_FORMAT_NONE}, + {case_12, "{\"key\":{\"subkey\":false,\"subkey2\":true}}", RJP_FORMAT_NONE}, + {case_13, "{\"key\":{\"subkey2\":true,\"subkey\":false}}", RJP_FORMAT_NONE}, + {case_14, "[5]", RJP_FORMAT_NONE}, + {case_15, "[[false]]", RJP_FORMAT_NONE}, + {case_16, "[[false,true]]", RJP_FORMAT_NONE}, + {case_17, "[5,[false,true]]", RJP_FORMAT_NONE}, + {case_18, "[5,{\"key\":[false]}]", RJP_FORMAT_NONE}, + {case_19, "[5,{\"key0\":true,\"key1\":false,\"key2\":true,\"key3\":false,\"key4\":true,\"key5\":false,\"key6\":true,\"key7\":false,\"key8\":true,\"key9\":false}]", RJP_FORMAT_NONE}, + {case_20, "[5,{\"key9\":true,\"key8\":false,\"key7\":true,\"key6\":false,\"key5\":true,\"key4\":false,\"key3\":true,\"key2\":false,\"key1\":true,\"key0\":false}]", RJP_FORMAT_NONE}, + {case_21, "{\"arr\":[0,1,2,3,4,5,6,7,8,9]}", RJP_FORMAT_NONE}, + + {case_1, "null", RJP_FORMAT_PRETTY}, + {case_2, "5", RJP_FORMAT_PRETTY}, + {case_3, "true", RJP_FORMAT_PRETTY}, + {case_4, "false", RJP_FORMAT_PRETTY}, + {case_5, "{}", RJP_FORMAT_PRETTY}, + {case_6, "[]", RJP_FORMAT_PRETTY}, + {case_7, "{}", RJP_FORMAT_PRETTY}, + {case_8, "{\n\t\"key\": 7\n}", RJP_FORMAT_PRETTY}, + {case_9, "{\n\t\"key\": {\n\t\t\"subkey\": false\n\t}\n}", RJP_FORMAT_PRETTY}, + {case_10, "{\n\t\"key\": {\n\t\t\"subkey\": false,\n\t\t\"subkey2\": true\n\t}\n}", RJP_FORMAT_PRETTY}, + {case_11, "{\n\t\"key\": {\n\t\t\"subkey\": false,\n\t\t\"subkey2\": true\n\t}\n}", RJP_FORMAT_PRETTY}, + {case_12, "{\n\t\"key\": {\n\t\t\"subkey\": false,\n\t\t\"subkey2\": true\n\t}\n}", RJP_FORMAT_PRETTY}, + {case_13, "{\n\t\"key\": {\n\t\t\"subkey2\": true,\n\t\t\"subkey\": false\n\t}\n}", RJP_FORMAT_PRETTY}, + {case_14, "[\n\t5\n]", RJP_FORMAT_PRETTY}, + {case_15, "[\n\t[\n\t\tfalse\n\t]\n]", RJP_FORMAT_PRETTY}, + {case_16, "[\n\t[\n\t\tfalse,\n\t\ttrue\n\t]\n]", RJP_FORMAT_PRETTY}, + {case_17, "[\n\t5,\n\t[\n\t\tfalse,\n\t\ttrue\n\t]\n]", RJP_FORMAT_PRETTY}, + {case_18, "[\n\t5,\n\t{\n\t\t\"key\": [\n\t\t\tfalse\n\t\t]\n\t}\n]", RJP_FORMAT_PRETTY}, + {case_19, "[\n\t5,\n\t{\n\t\t\"key0\": true,\n\t\t\"key1\": false,\n\t\t\"key2\": true,\n\t\t\"key3\": false,\n\t\t\"key4\": true,\n\t\t\"key5\": false,\n\t\t\"key6\": true,\n\t\t\"key7\": false,\n\t\t\"key8\": true,\n\t\t\"key9\": false\n\t}\n]", RJP_FORMAT_PRETTY}, + {case_20, "[\n\t5,\n\t{\n\t\t\"key9\": true,\n\t\t\"key8\": false,\n\t\t\"key7\": true,\n\t\t\"key6\": false,\n\t\t\"key5\": true,\n\t\t\"key4\": false,\n\t\t\"key3\": true,\n\t\t\"key2\": false,\n\t\t\"key1\": true,\n\t\t\"key0\": false\n\t}\n]", RJP_FORMAT_PRETTY}, + {case_21, "{\n\t\"arr\": [\n\t\t0,\n\t\t1,\n\t\t2,\n\t\t3,\n\t\t4,\n\t\t5,\n\t\t6,\n\t\t7,\n\t\t8,\n\t\t9\n\t]\n}", RJP_FORMAT_PRETTY}, +}; + +int run_test(test_pair* p){ + RJP_value* test = p->create(); + char* buf = rjp_to_json(test, p->fmt); + if(!buf){ + if(!p->res) + return 1; + return 0; + } + int retval = !strcmp(buf, p->res); + if(retval){ + fprintf(stderr, "Success\n"); + }else{ + fprintf(stderr, "Failure\n"); + fprintf(stderr, "Expected: '%s'\nGot: '%s'\n", p->res, buf); + } + rjp_free(buf); + rjp_free_value(test); + return retval; } int main(){ - RJP_value* obj = do_object(); - RJP_value* arr = rjp_add_member_key_copy(obj, "key2", 0, do_array()); - rjp_add_element(arr, do_string()); - rjp_add_element(arr, do_integer()); - rjp_add_element(arr, do_dfloat()); - rjp_add_element(arr, do_boolean()); - rjp_add_member_key_copy(obj, "an-in-order", 0, do_integer()); - - char* buf = rjp_to_json(obj, RJP_FORMAT_PRETTY); - printf("%s\n", buf); - rjp_free(buf); - - rjp_object_to_ordered(obj); - rjp_add_member_key_copy(obj, "an-out-of-order", 0, do_integer()); - buf = rjp_to_json(obj, RJP_FORMAT_PRETTY); - printf("%s\n", buf); - rjp_free(buf); - - rjp_free_value(obj); + const int num_tests = sizeof(tests) / sizeof(tests[0]); + int passed = 0; + fprintf(stderr, "Running %d tests that should succeed...\n", num_tests); + for(int i = 0;i < num_tests;++i){ + fprintf(stderr, "%8d) ", i+1); + if(run_test(&tests[i])){ + ++passed; + } + } + fprintf(stderr, "\nResults: %d/%d tests passed\n", passed, num_tests); + if(passed != num_tests) + return 1; + return 0; } diff --git a/tests/parse.c b/tests/parse.c index ad6815b..ec3857f 100644 --- a/tests/parse.c +++ b/tests/parse.c @@ -199,4 +199,7 @@ int main(){ fprintf(stderr, "\nResults: %d/%d normal tests passed\n", normal_passed, total_tests); fprintf(stderr, "Results: %d/%d callback tests passed\n", cback_passed, total_tests); fprintf(stderr, "Results: %d/%d tests passed\n", total_passed, total_tests*2); + if(total_passed != (total_tests*2)) + return 1; + return 0; }