Add testing targets to cmake. Improve output test

This commit is contained in:
rexy712 2020-03-24 14:53:02 -07:00
parent 437d6f7bd0
commit 86de4d2bb3
4 changed files with 253 additions and 49 deletions

View File

@ -49,6 +49,12 @@ if(ENABLE_C++)
endif() endif()
if(BUILD_TESTS) if(BUILD_TESTS)
enable_testing()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
--force-new-ctest-process
--verbose
--output-on-failure
)
add_subdirectory(tests) add_subdirectory(tests)
endif() endif()

View File

@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.0.2)
project(rjp_tests) project(rjp_tests)
set(INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../include) set(INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../include)
include_directories("${INCLUDE_PATH}") include_directories("${INCLUDE_PATH}")
add_compile_options(-Wall -Wextra -pedantic) add_compile_options(-Wall -Wextra -pedantic)
link_libraries(rjp) link_libraries(rjp)
@ -13,3 +12,6 @@ endif()
add_executable(parse "parse.c") add_executable(parse "parse.c")
add_executable(output "output.c") add_executable(output "output.c")
add_test(NAME parse-test COMMAND parse)
add_test(NAME output-test COMMAND output)

View File

@ -1,60 +1,253 @@
#include "rjp.h" #include "rjp_internal.h"
#include "rjp_value.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
RJP_value* do_object(void){ typedef struct{
RJP_value* obj = rjp_new_object(); RJP_value* (*create)(void);
RJP_value* mem = rjp_new_member_key_copy(obj, "key", 0); const char* res;
rjp_set_int(mem, 10); int fmt;
return obj; }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* case_2(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){
return rjp_new_int(5); return rjp_new_int(5);
} }
RJP_value* do_dfloat(void){ RJP_value* case_3(void){
return rjp_new_float(5);
}
RJP_value* do_boolean(void){
return rjp_new_bool(1); return rjp_new_bool(1);
} }
RJP_value* do_null(void){ RJP_value* case_4(void){
return rjp_new_null(); 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(){ int main(){
RJP_value* obj = do_object(); const int num_tests = sizeof(tests) / sizeof(tests[0]);
RJP_value* arr = rjp_add_member_key_copy(obj, "key2", 0, do_array()); int passed = 0;
rjp_add_element(arr, do_string()); fprintf(stderr, "Running %d tests that should succeed...\n", num_tests);
rjp_add_element(arr, do_integer()); for(int i = 0;i < num_tests;++i){
rjp_add_element(arr, do_dfloat()); fprintf(stderr, "%8d) ", i+1);
rjp_add_element(arr, do_boolean()); if(run_test(&tests[i])){
rjp_add_member_key_copy(obj, "an-in-order", 0, do_integer()); ++passed;
}
char* buf = rjp_to_json(obj, RJP_FORMAT_PRETTY); }
printf("%s\n", buf); fprintf(stderr, "\nResults: %d/%d tests passed\n", passed, num_tests);
rjp_free(buf); if(passed != num_tests)
return 1;
rjp_object_to_ordered(obj); return 0;
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);
} }

View File

@ -199,4 +199,7 @@ int main(){
fprintf(stderr, "\nResults: %d/%d normal tests passed\n", normal_passed, total_tests); 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 callback tests passed\n", cback_passed, total_tests);
fprintf(stderr, "Results: %d/%d tests passed\n", total_passed, total_tests*2); fprintf(stderr, "Results: %d/%d tests passed\n", total_passed, total_tests*2);
if(total_passed != (total_tests*2))
return 1;
return 0;
} }