Add test source file

This commit is contained in:
rexy712
2020-03-09 14:38:33 -07:00
parent 637f20e6b7
commit c2f4131b63

121
src/test/test.c Normal file
View File

@@ -0,0 +1,121 @@
#include "rjp.h"
#include "rjp_internal.h"
#include "rjp_string.h"
#include <stdio.h>
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);
}