summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorStephen Dolan <mu@netsoc.tcd.ie>2012-09-18 22:17:13 +0100
committerStephen Dolan <mu@netsoc.tcd.ie>2012-09-18 22:17:13 +0100
commit20e45f363c91ef4a305eff5709212f1b2fb43523 (patch)
tree5fad025d47663b801d15a7cb805562cf6264a044 /main.c
parent46af5238ce3e9327e0268d18373d07f67eed58b8 (diff)
Separate the tests and the main program.
Diffstat (limited to 'main.c')
-rw-r--r--main.c99
1 files changed, 1 insertions, 98 deletions
diff --git a/main.c b/main.c
index 0a9eefe8..776c9575 100644
--- a/main.c
+++ b/main.c
@@ -6,106 +6,9 @@
#include "jv_parse.h"
#include "locfile.h"
#include "parser.h"
-
-
-void jq_init(struct bytecode* bc, jv value);
-jv jq_next();
-void jq_teardown();
-
-struct bytecode* jq_compile(const char* str) {
- struct locfile locations;
- locfile_init(&locations, str, strlen(str));
- block program;
- struct bytecode* bc = 0;
- int nerrors = jq_parse(&locations, &program);
- if (nerrors == 0) {
- block_append(&program, block_join(gen_op_simple(YIELD), gen_op_simple(BACKTRACK)));
- program = builtins_bind(program);
- nerrors = block_compile(program, &locations, &bc);
- block_free(program);
- }
- if (nerrors) {
- fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
- }
- locfile_free(&locations);
- return bc;
-}
-
-int skipline(const char* buf) {
- int p = 0;
- while (buf[p] == ' ' || buf[p] == '\t') p++;
- if (buf[p] == '#' || buf[p] == '\n' || buf[p] == 0) return 1;
- return 0;
-}
-
-void run_tests() {
- FILE* testdata = fopen("testdata","r");
- char buf[4096];
- int tests = 0, passed = 0;
-
- while (1) {
- if (!fgets(buf, sizeof(buf), testdata)) break;
- if (skipline(buf)) continue;
- printf("Testing %s\n", buf);
- int pass = 1;
- struct bytecode* bc = jq_compile(buf);
- assert(bc);
- printf("Disassembly:\n");
- dump_disassembly(2, bc);
- printf("\n");
- fgets(buf, sizeof(buf), testdata);
- jv input = jv_parse(buf);
- assert(jv_is_valid(input));
- jq_init(bc, input);
-
- while (fgets(buf, sizeof(buf), testdata)) {
- if (skipline(buf)) break;
- jv expected = jv_parse(buf);
- assert(jv_is_valid(expected));
- jv actual = jq_next();
- if (!jv_is_valid(actual)) {
- jv_free(actual);
- printf("Insufficient results\n");
- pass = 0;
- break;
- } else if (!jv_equal(jv_copy(expected), jv_copy(actual))) {
- printf("Expected ");
- jv_dump(jv_copy(expected), 0);
- printf(", but got ");
- jv_dump(jv_copy(actual), 0);
- printf("\n");
- pass = 0;
- }
- jv as_string = jv_dump_string(jv_copy(expected), rand());
- jv reparsed = jv_parse_sized(jv_string_value(as_string), jv_string_length(jv_copy(as_string)));
- assert(jv_equal(jv_copy(expected), jv_copy(reparsed)));
- jv_free(as_string);
- jv_free(reparsed);
- jv_free(expected);
- jv_free(actual);
- }
- if (pass) {
- jv extra = jq_next();
- if (jv_is_valid(extra)) {
- printf("Superfluous result: ");
- jv_dump(extra, 0);
- printf("\n");
- pass = 0;
- } else {
- jv_free(extra);
- }
- }
- jq_teardown();
- bytecode_free(bc);
- tests++;
- passed+=pass;
- }
- fclose(testdata);
- printf("%d of %d tests passed\n", passed,tests);
-}
+#include "execute.h"
int main(int argc, char* argv[]) {
- if (argc == 1) { run_tests(); return 0; }
struct bytecode* bc = jq_compile(argv[1]);
if (!bc) return 1;