diff --git a/tests/local.am b/tests/local.am index c417cce..c013f2b 100644 --- a/tests/local.am +++ b/tests/local.am @@ -31,6 +31,7 @@ testsuite_SOURCES = \ %D%/unit/eval.c \ %D%/unit/jit.c \ %D%/unit/parse.c \ + %D%/unit/vm.c \ $(NULL) # Libraries being tested @@ -39,6 +40,7 @@ testsuite_LDADD = \ libeval.a \ libjit.a \ libparse.a \ + libvm.a \ $(NULL) # Needed flags diff --git a/tests/unit/eval.c b/tests/unit/eval.c index 8863e6e..80ee4f0 100644 --- a/tests/unit/eval.c +++ b/tests/unit/eval.c @@ -9,7 +9,7 @@ TestSuite(evaluate, .init = redirect_streams); -void do_eval(const char *input, int expected) +static void do_eval(const char *input, int expected) { write_to_stdin(input); struct ast_node *ast = NULL; diff --git a/tests/unit/jit.c b/tests/unit/jit.c index e10a347..e118c2d 100644 --- a/tests/unit/jit.c +++ b/tests/unit/jit.c @@ -11,7 +11,7 @@ TestSuite(jit, .init = redirect_streams); #define ERR_STRING "Input '%s' = %d, JIT got %d\n" -void do_compare(const char *input) +static void do_compare(const char *input) { write_to_stdin(input); struct ast_node *ast = NULL; diff --git a/tests/unit/parse.c b/tests/unit/parse.c index 7cc3911..d1a0a40 100644 --- a/tests/unit/parse.c +++ b/tests/unit/parse.c @@ -8,7 +8,7 @@ TestSuite(parser, .init = redirect_streams); -void do_correct(const char *input) +static void do_correct(const char *input) { write_to_stdin(input); struct ast_node *ast = NULL; @@ -19,7 +19,7 @@ void do_correct(const char *input) destroy_ast(ast); } -void do_incorrect(const char *input) +static void do_incorrect(const char *input) { write_to_stdin(input); struct ast_node *ast = NULL; diff --git a/tests/unit/vm.c b/tests/unit/vm.c new file mode 100644 index 0000000..400699f --- /dev/null +++ b/tests/unit/vm.c @@ -0,0 +1,138 @@ +#include +#include + +#include "common.h" + +#include "ast/ast.h" +#include "eval/evaluator.h" +#include "parse/parse-jitters.h" +#include "vm/vm.h" + +TestSuite(vm, .init = redirect_streams); + +#define ERR_STRING "Input '%s' = %d, VM got %d\n" +static void do_compare(const char *input) +{ + write_to_stdin(input); + struct ast_node *ast = NULL; + + cr_assert_eq(yyparse(&ast), 0); + cr_assert_not_null(ast); + + int eval = evaluate_ast(ast); + int vm = bytecompile_eval_ast(ast); + + cr_expect_eq(eval, vm, ERR_STRING, input, eval, vm); + + destroy_ast(ast); +} + +Test(vm, one) +{ + do_compare("1"); +} + +Test(vm, the_answer) +{ + do_compare("42"); +} + +Test(vm, int_max) +{ + do_compare("2147483647"); +} + +Test(vm, int_max_plus_one) +{ + do_compare("2147483647 + 1"); +} + +Test(vm, whitespace) +{ + do_compare(" 1 "); +} + +Test(vm, more_whitespace) +{ + do_compare(" 1 + 2 "); +} + +Test(vm, one_plus_one) +{ + do_compare("1+1"); +} + +Test(vm, one_minus_one) +{ + do_compare("1-1"); +} + +Test(vm, additions) +{ + do_compare("1+1+1+1+1"); +} + +Test(vm, substractions) +{ + do_compare("1-1-1-1-1"); +} + +Test(vm, multiplication) +{ + do_compare("2 * 3"); +} + +Test(vm, multiplications) +{ + do_compare("1 * 2 * 3 * 4"); +} + +Test(vm, division) +{ + do_compare("12 / 3"); +} + +Test(vm, divisions) +{ + do_compare("24 / 4 / 3 / 2"); +} + +Test(vm, simple_priority) +{ + do_compare("1 + 2 * 3"); +} + +Test(vm, more_priorities) +{ + do_compare("1 + 6 / 3 + 4 * 6 + 14 / 7"); +} + +Test(vm, simple_parenthesis) +{ + do_compare("(1 + 2) * 3"); +} + +Test(vm, more_parenthesis) +{ + do_compare("(1 + 2) * (3 - 4)"); +} + +Test(vm, unary_minus) +{ + do_compare("-1"); +} + +Test(vm, unary_plus) +{ + do_compare("+1"); +} + +Test(vm, unary_torture) +{ + do_compare("--+++--+-+-+-1"); +} + +Test(vm, altogether) +{ + do_compare(" - 3 + - 4 * 8 / 2 + + 3 -- 2 + ((-1) + 1) * 2 "); +}