tests: unit: add evaluation tests

This commit is contained in:
Bruno BELANYI 2020-09-29 20:36:09 +02:00
parent 41550b1291
commit de6e195f6b
3 changed files with 137 additions and 0 deletions

View file

@ -26,12 +26,14 @@ AM_TESTS_ENVIRONMENT = \
testsuite_SOURCES = \ testsuite_SOURCES = \
%D%/unit/common.c \ %D%/unit/common.c \
%D%/unit/common.h \ %D%/unit/common.h \
%D%/unit/eval.c \
%D%/unit/parse.c \ %D%/unit/parse.c \
$(NULL) $(NULL)
# Libraries being tested # Libraries being tested
testsuite_LDADD = \ testsuite_LDADD = \
libast.a \ libast.a \
libeval.a \
libparse.a \ libparse.a \
$(NULL) $(NULL)

133
tests/unit/eval.c Normal file
View file

@ -0,0 +1,133 @@
#include <criterion/criterion.h>
#include <criterion/redirect.h>
#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);
}

2
tests/unit/leak.suppr Normal file
View file

@ -0,0 +1,2 @@
# We do not care about criterion leaks
leak:criterion_handle_args