From c2f4131b6348053ebd68234e802e603e9637cd59 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 9 Mar 2020 14:38:33 -0700 Subject: [PATCH] Add test source file --- src/test/test.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/test/test.c diff --git a/src/test/test.c b/src/test/test.c new file mode 100644 index 0000000..7c5988c --- /dev/null +++ b/src/test/test.c @@ -0,0 +1,121 @@ +#include "rjp.h" +#include "rjp_internal.h" +#include "rjp_string.h" + +#include + +int test(const char* str){ + RJP_value* res; + res = rjp_parse(str); + if(res){ + fprintf(stderr, "Accepted\n"); + } +#ifndef RJP_ENABLE_DIAGNOSTICS + else{ + fprintf(stderr, "Failed\n"); + } +#endif + int failed = res == NULL; + rjp_free_value(res); + return failed; +} + +int main(){ + const char* should_pass_strings[] = { + "{}", + "[]", + "\"s\"", + "\"\\n\"", + "\"\\\"\"", + "\"str\\nstr\"", + "true", + "false", + "null", + "5", + "-5", + "+5", + "5.5", + "-5.5", + "+5.5", + "5.5e6", + "-5.5e6", + "+5.5e6", + "5.5e+6", + "-5.5e+6", + "+5.5e+6", + "5.5e-6", + "-5.5e-6", + "+5.5e-6", + " {}", + "\n{}\n", + " {\t }\n", + "5.5 ", + "{\"key\":5}", + "{\"key\":{}}", + "{\"key\":{\"key\":5}}", + "{\"key\":{\"key\":5,\"key2\":6}}", + "{\"key\":{\"key\":5},\"key2\":6}", + "[5, 6, 7, 8, 9, \"10\"]", + "[[5,6],[7,8],[9,\"10\"]]", + "{\"arr\":[5,6,6]}", + "[{\"arr\":[5,6,6]}]", + "[{\"arr\":[5,6,6]}, 6]", + "[5,6,6,6,6.6]", +#ifdef RJP_ENABLE_TRAILING_COMMA + "[6,7,]", + "{\"1\":1,\"2\":2,}", + "[6,]", + "{\"1\":1,}", +#endif +#ifdef RJP_ENABLE_COMMENTS + "//comment\n{}", +#endif + }; + const int should_pass_cnt = sizeof(should_pass_strings)/sizeof(should_pass_strings[0]); + const char* should_fail_strings[] = { + "{", + "}", + "[", + "]", + "6.", + "6.6e", + "6.6e+", + "{6}", + "[\"key\":5]", + "[3 4]", + "{\"key\":1 \"key2\":2}", + "{\"key\" 1}", + "6, 7", + "[,]", + "{,}", + "[1, 2],", +#ifndef RJP_ENABLE_TRAILING_COMMA + "[6,7,]", + "{\"1\":1,\"2\":2,}", + "[6,]", + "{\"1\":1,}", +#endif +#ifndef RJP_ENABLE_COMMENTS + "//comment\n{}", +#endif + }; + const int should_fail_cnt = sizeof(should_fail_strings)/sizeof(should_fail_strings[0]); + const int total_tests = should_pass_cnt + should_fail_cnt; + + int passed = 0; + + fprintf(stderr, "Running %d tests that should pass...\n", should_pass_cnt); + for(unsigned i = 0;i < sizeof(should_pass_strings)/sizeof(should_pass_strings[0]);++i){ + fprintf(stderr, "%8d) ", i+1); + if(!test(should_pass_strings[i])) + ++passed; + } + fprintf(stderr, "\n"); + printf("Running %d tests that should fail...\n", should_fail_cnt); + for(unsigned i = 0;i < sizeof(should_fail_strings)/sizeof(should_fail_strings[0]);++i){ + fprintf(stderr, "%8d) ", i+1); + if(test(should_fail_strings[i])) + ++passed; + } + fprintf(stderr, "\nResults: %d/%d tests passed\n", passed, total_tests); +}