diff --git a/tests/local.am b/tests/local.am index 2fa8033..ecf9b35 100644 --- a/tests/local.am +++ b/tests/local.am @@ -26,12 +26,14 @@ AM_TESTS_ENVIRONMENT = \ testsuite_SOURCES = \ %D%/unit/common.c \ %D%/unit/common.h \ + %D%/unit/eval.c \ %D%/unit/parse.c \ $(NULL) # Libraries being tested testsuite_LDADD = \ libast.a \ + libeval.a \ libparse.a \ $(NULL) diff --git a/tests/unit/eval.c b/tests/unit/eval.c new file mode 100644 index 0000000..8863e6e --- /dev/null +++ b/tests/unit/eval.c @@ -0,0 +1,133 @@ +#include +#include + +#include "common.h" + +#include "ast/ast.h" +#include "eval/evaluator.h" +#include "parse/parse-jitters.h" + +TestSuite(evaluate, .init = redirect_streams); + +void do_eval(const char *input, int expected) +{ + write_to_stdin(input); + struct ast_node *ast = NULL; + + cr_assert_eq(yyparse(&ast), 0); + cr_assert_not_null(ast); + + cr_expect_eq(evaluate_ast(ast), expected); + + destroy_ast(ast); +} + +Test(evaluate, one) +{ + do_eval("1", 1); +} + +Test(evaluate, the_answer) +{ + do_eval("42", 42); +} + +Test(evaluate, int_max) +{ + do_eval("2147483647", 2147483647); +} + +Test(evaluate, int_max_plus_one) +{ + do_eval("2147483647 + 1", -2147483648); +} + +Test(evaluate, whitespace) +{ + do_eval(" 1 ", 1); +} + +Test(evaluate, more_whitespace) +{ + do_eval(" 1 + 2 ", 3); +} + +Test(evaluate, one_plus_one) +{ + do_eval("1+1", 2); +} + +Test(evaluate, one_minus_one) +{ + do_eval("1-1", 0); +} + +Test(evaluate, additions) +{ + do_eval("1+1+1+1+1", 5); +} + +Test(evaluate, substractions) +{ + do_eval("1-1-1-1-1", -3); +} + +Test(evaluate, multiplication) +{ + do_eval("2 * 3", 6); +} + +Test(evaluate, multiplications) +{ + do_eval("1 * 2 * 3 * 4", 24); +} + +Test(evaluate, division) +{ + do_eval("12 / 3", 4); +} + +Test(evaluate, divisions) +{ + do_eval("24 / 4 / 3 / 2", 1); +} + +Test(evaluate, simple_priority) +{ + do_eval("1 + 2 * 3", 7); +} + +Test(evaluate, more_priorities) +{ + do_eval("1 + 6 / 3 + 4 * 6 + 14 / 7", 29); +} + +Test(evaluate, simple_parenthesis) +{ + do_eval("(1 + 2) * 3", 9); +} + +Test(evaluate, more_parenthesis) +{ + do_eval("(1 + 2) * (3 - 4)", -3); +} + +Test(evaluate, unary_minus) +{ + do_eval("-1", -1); +} + +Test(evaluate, unary_plus) +{ + do_eval("+1", 1); +} + +Test(evaluate, unary_torture) +{ + do_eval("--+++--+-+-+-1", -1); +} + +Test(evaluate, altogether) +{ + do_eval(" - 3 + - 4 * 8 / 2 + + 3 -- 2 + ((-1) + 1) * 2 ", -14); +} diff --git a/tests/unit/leak.suppr b/tests/unit/leak.suppr new file mode 100644 index 0000000..6f70eb0 --- /dev/null +++ b/tests/unit/leak.suppr @@ -0,0 +1,2 @@ +# We do not care about criterion leaks +leak:criterion_handle_args