1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include <stdio.h>
#include "compile.h"
#include "parser.tab.h"
#include "builtin.h"
#include "jv.h"
block compile(const char* str);
void jq_init(struct bytecode* bc, jv value);
jv jq_next();
void jq_teardown();
void run_program(struct bytecode* 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;
block program = compile(buf);
block_append(&program, gen_op_simple(YIELD));
block_append(&program, gen_op_simple(BACKTRACK));
program = gen_cbinding(&builtins, program);
struct bytecode* bc = block_compile(program);
block_free(program);
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));
printf(", but got ");
jv_dump(jv_copy(actual));
printf("\n");
pass = 0;
}
jv_free(expected);
jv_free(actual);
}
if (pass) {
jv extra = jq_next();
if (jv_is_valid(extra)) {
printf("Superfluous result: ");
jv_dump(extra);
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);
}
int main(int argc, char* argv[]) {
if (argc == 1) { run_tests(); return 0; }
block blk = compile(argv[1]);
block_append(&blk, block_join(gen_op_simple(YIELD), gen_op_simple(BACKTRACK)));
blk = gen_cbinding(&builtins, blk);
struct bytecode* bc = block_compile(blk);
block_free(blk);
run_program(bc);
bytecode_free(bc);
}
|