From 41550b1291c0d5d0e15fd4ac0a0c95472c741976 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 29 Sep 2020 20:35:51 +0200 Subject: [PATCH] tests: unit: add parsing tests --- tests/local.am | 37 ++++++++++++++ tests/unit/common.c | 15 ++++++ tests/unit/common.h | 7 +++ tests/unit/parse.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 tests/unit/common.c create mode 100644 tests/unit/common.h create mode 100644 tests/unit/parse.c diff --git a/tests/local.am b/tests/local.am index 51b3cb4..2fa8033 100644 --- a/tests/local.am +++ b/tests/local.am @@ -3,3 +3,40 @@ TEST_LOG_DRIVER = \ env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/build-aux/tap-driver.sh TESTS = + +# Distribute the leak suppression file +EXTRA_DIST += %D%/unit/leak.suppr + +if USING_CRITERION +# Add our testsuite to `make check` only if Criterion is detected +check_PROGRAMS = testsuite + +# Add our testsuite to the TAP driver +TESTS += $(check_PROGRAMS) + +# Setup Criterion TAP output, and suppress leak from Criterion +AM_TESTS_ENVIRONMENT = \ + CRITERION_OUTPUTS='tap:-'; \ + export CRITERION_OUTPUTS; \ + LSAN_OPTIONS=suppressions=%D%/unit/leak.suppr; \ + export LSAN_OPTIONS; \ + $(NULL) + +# Unit-test sources +testsuite_SOURCES = \ + %D%/unit/common.c \ + %D%/unit/common.h \ + %D%/unit/parse.c \ + $(NULL) + +# Libraries being tested +testsuite_LDADD = \ + libast.a \ + libparse.a \ + $(NULL) + +# Needed flags +testsuite_CFLAGS = $(AM_CFLAGS) $(CRITERION_CFLAGS) +testsuite_CPPFLAGS = $(AM_CPPFLAGS) +testsuite_LDFLAGS = $(AM_LDFLAGS) $(CRITERION_LIBS) +endif diff --git a/tests/unit/common.c b/tests/unit/common.c new file mode 100644 index 0000000..425c6f8 --- /dev/null +++ b/tests/unit/common.c @@ -0,0 +1,15 @@ +#include + +void redirect_streams(void) +{ + cr_redirect_stdin(); + cr_redirect_stdout(); + cr_redirect_stderr(); +} + +void write_to_stdin(const char *input) +{ + FILE *f = cr_get_redirected_stdin(); + fputs(input, f); + fclose(f); // Make sure to send EOF when parsing +} diff --git a/tests/unit/common.h b/tests/unit/common.h new file mode 100644 index 0000000..ecc4c04 --- /dev/null +++ b/tests/unit/common.h @@ -0,0 +1,7 @@ +#ifndef COMMON_H +#define COMMON_H + +void redirect_streams(void); +void write_to_stdin(const char *input); + +#endif /* !COMMON_H */ diff --git a/tests/unit/parse.c b/tests/unit/parse.c new file mode 100644 index 0000000..7cc3911 --- /dev/null +++ b/tests/unit/parse.c @@ -0,0 +1,116 @@ +#include +#include + +#include "common.h" + +#include "ast/ast.h" +#include "parse/parse-jitters.h" + +TestSuite(parser, .init = redirect_streams); + +void do_correct(const char *input) +{ + write_to_stdin(input); + struct ast_node *ast = NULL; + + cr_assert_eq(yyparse(&ast), 0); + cr_assert_not_null(ast); + + destroy_ast(ast); +} + +void do_incorrect(const char *input) +{ + write_to_stdin(input); + struct ast_node *ast = NULL; + + cr_expect_neq(yyparse(&ast), 0); + cr_expect_null(ast); + + destroy_ast(ast); +} + +Test(parser, empty) +{ + do_incorrect(""); +} + +Test(parser, trailing_operator) +{ + do_incorrect("1 +"); +} + +Test(parser, trailing_expression) +{ + do_incorrect("1 1"); +} + +Test(parser, double_operator) +{ + do_incorrect("1 * * 1"); +} + +Test(parser, one) +{ + do_correct("1"); +} + +Test(parser, the_answer) +{ + do_correct("42"); +} + +Test(parser, int_max) +{ + do_correct("2147483647"); +} + +Test(parser, whitespace) +{ + do_correct(" 1 "); +} + +Test(parser, more_whitespace) +{ + do_correct(" 1 + 2 "); +} + +Test(parser, one_plus_one) +{ + do_correct("1+1"); +} + +Test(parser, one_minus_one) +{ + do_correct("1-1"); +} + +Test(parser, additions) +{ + do_correct("1+1+1+1+1"); +} + +Test(parser, substractions) +{ + do_correct("1-1-1-1-1"); +} + +Test(parser, multiplication) +{ + do_correct("2 * 3"); +} + +Test(parser, multiplications) +{ + do_correct("1 * 2 * 3 * 4"); +} + +Test(parser, division) +{ + do_correct("12 / 3"); +} + +Test(parser, divisions) +{ + do_correct("24 / 4 / 3 / 2"); +}