tests: unit: add jit tests

This commit is contained in:
Bruno BELANYI 2020-09-30 01:33:22 +02:00
parent c2b5117875
commit 476efeb3b9
2 changed files with 140 additions and 0 deletions

View file

@ -27,6 +27,7 @@ testsuite_SOURCES = \
%D%/unit/common.c \ %D%/unit/common.c \
%D%/unit/common.h \ %D%/unit/common.h \
%D%/unit/eval.c \ %D%/unit/eval.c \
%D%/unit/jit.c \
%D%/unit/parse.c \ %D%/unit/parse.c \
$(NULL) $(NULL)
@ -34,6 +35,7 @@ testsuite_SOURCES = \
testsuite_LDADD = \ testsuite_LDADD = \
libast.a \ libast.a \
libeval.a \ libeval.a \
libjit.a \
libparse.a \ libparse.a \
$(NULL) $(NULL)

138
tests/unit/jit.c Normal file
View file

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