tests: unit: add vm tests

This commit is contained in:
Bruno BELANYI 2020-09-30 22:35:58 +02:00
parent bd7180cea4
commit 7ab8e8c6b6
5 changed files with 144 additions and 4 deletions

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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;

138
tests/unit/vm.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 "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 ");
}