From 476efeb3b9fe89506cf5ddd26a5ea7730e162d25 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 30 Sep 2020 01:33:22 +0200 Subject: [PATCH] tests: unit: add jit tests --- tests/local.am | 2 + tests/unit/jit.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 tests/unit/jit.c diff --git a/tests/local.am b/tests/local.am index ecf9b35..7e1e3d8 100644 --- a/tests/local.am +++ b/tests/local.am @@ -27,6 +27,7 @@ testsuite_SOURCES = \ %D%/unit/common.c \ %D%/unit/common.h \ %D%/unit/eval.c \ + %D%/unit/jit.c \ %D%/unit/parse.c \ $(NULL) @@ -34,6 +35,7 @@ testsuite_SOURCES = \ testsuite_LDADD = \ libast.a \ libeval.a \ + libjit.a \ libparse.a \ $(NULL) diff --git a/tests/unit/jit.c b/tests/unit/jit.c new file mode 100644 index 0000000..e10a347 --- /dev/null +++ b/tests/unit/jit.c @@ -0,0 +1,138 @@ +#include +#include + +#include "common.h" + +#include "ast/ast.h" +#include "eval/evaluator.h" +#include "jit/jitter.h" +#include "parse/parse-jitters.h" + +TestSuite(jit, .init = redirect_streams); + +#define ERR_STRING "Input '%s' = %d, JIT got %d\n" +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 jit = jit_eval_ast(ast); + + cr_expect_eq(eval, jit, ERR_STRING, input, eval, jit); + + destroy_ast(ast); +} + +Test(jit, one) +{ + do_compare("1"); +} + +Test(jit, the_answer) +{ + do_compare("42"); +} + +Test(jit, int_max) +{ + do_compare("2147483647"); +} + +Test(jit, int_max_plus_one) +{ + do_compare("2147483647 + 1"); +} + +Test(jit, whitespace) +{ + do_compare(" 1 "); +} + +Test(jit, more_whitespace) +{ + do_compare(" 1 + 2 "); +} + +Test(jit, one_plus_one) +{ + do_compare("1+1"); +} + +Test(jit, one_minus_one) +{ + do_compare("1-1"); +} + +Test(jit, additions) +{ + do_compare("1+1+1+1+1"); +} + +Test(jit, substractions) +{ + do_compare("1-1-1-1-1"); +} + +Test(jit, multiplication) +{ + do_compare("2 * 3"); +} + +Test(jit, multiplications) +{ + do_compare("1 * 2 * 3 * 4"); +} + +Test(jit, division) +{ + do_compare("12 / 3"); +} + +Test(jit, divisions) +{ + do_compare("24 / 4 / 3 / 2"); +} + +Test(jit, simple_priority) +{ + do_compare("1 + 2 * 3"); +} + +Test(jit, more_priorities) +{ + do_compare("1 + 6 / 3 + 4 * 6 + 14 / 7"); +} + +Test(jit, simple_parenthesis) +{ + do_compare("(1 + 2) * 3"); +} + +Test(jit, more_parenthesis) +{ + do_compare("(1 + 2) * (3 - 4)"); +} + +Test(jit, unary_minus) +{ + do_compare("-1"); +} + +Test(jit, unary_plus) +{ + do_compare("+1"); +} + +Test(jit, unary_torture) +{ + do_compare("--+++--+-+-+-1"); +} + +Test(jit, altogether) +{ + do_compare(" - 3 + - 4 * 8 / 2 + + 3 -- 2 + ((-1) + 1) * 2 "); +}