From e39267c83a8af189a624891813ebd6014543b01a Mon Sep 17 00:00:00 2001 From: c Date: Sun, 24 Dec 2023 10:28:00 -0500 Subject: Proper tests. --- test/include/test.h | 22 ++++++++++++++++ test/parser.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/tree.c | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 test/include/test.h create mode 100644 test/parser.c create mode 100644 test/tree.c (limited to 'test') diff --git a/test/include/test.h b/test/include/test.h new file mode 100644 index 0000000..4638a1e --- /dev/null +++ b/test/include/test.h @@ -0,0 +1,22 @@ +#ifndef TEST_H +#define TEST_H + +#include "../../src/include/util.h" + +extern unsigned int TESTS_RUN; +extern unsigned int TESTS_PASSED; + +#define HIDE(THE) do { THE } while ( 0 ); + +#define ASSERT(EXPR) \ + TESTS_RUN++; \ + (EXPR && ++TESTS_PASSED) ? \ + log_inf("%s:%d: Assertion passed!", __FILE__, __LINE__) : \ + log_err("%s:%d: Assertion failed:\n\t%s", __FILE__, __LINE__, #EXPR); + +#define TEST_REPORT \ + (TESTS_RUN == TESTS_PASSED) ? \ + log_yay("%s: All %d tests passed!", __FILE__, TESTS_RUN) : \ + log_err("%d/%d tests failed.", TESTS_RUN - TESTS_PASSED, TESTS_RUN); + +#endif /* ifndef TEST_H */ diff --git a/test/parser.c b/test/parser.c new file mode 100644 index 0000000..fb1c0b2 --- /dev/null +++ b/test/parser.c @@ -0,0 +1,72 @@ +#include "include/test.h" +#include "../src/include/pp.h" +#include "../src/include/lexer.h" +#include "../src/include/tree.h" +#include "../src/include/parser.h" + +unsigned int TESTS_RUN = 0, TESTS_PASSED = 0; + +int main(int argc, char** argv) { + tree_t* tree_0; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + tree_0 = tree_init(TREE_TYPE_BLOCK); + tree_t* treep_00 = tree_0->data.block.val = tree_init(TREE_TYPE_EXPR); + tree_t* treep_01 = treep_00->data.expr.val = tree_init(TREE_TYPE_DEF); + tree_t* treep_02 = treep_01->data.def.tag = tree_init(TREE_TYPE_TAG); + treep_02->data.tag.val = "int"; + tree_t* treep_03 = treep_02->data.tag.nxt = tree_init(TREE_TYPE_TAG); + treep_03->data.tag.val = "f"; + treep_03->data.tag.nxt = NULL; + treep_01->data.def.arg = NULL; + tree_t* treep_04 = treep_01->data.def.val = tree_init(TREE_TYPE_EXPR); + tree_t* treep_05 = treep_04->data.expr.val = tree_init(TREE_TYPE_BLOCK); + tree_t* treep_06 = treep_05->data.block.val = tree_init(TREE_TYPE_EXPR); + tree_t* treep_07 = treep_06->data.expr.val = tree_init(TREE_TYPE_CALL); + treep_07->data.call.target = "a"; + tree_t* treep_08 = treep_07->data.call.arg = tree_init(TREE_TYPE_CARG); + tree_t* treep_09 = treep_08->data.carg.val = tree_init(TREE_TYPE_EXPR); + tree_t* treep_10 = treep_09->data.expr.val = tree_init(TREE_TYPE_CALL); + treep_10->data.call.target = "b"; + treep_10->data.call.arg = NULL; + treep_08->data.carg.nxt = NULL; + tree_t* treep_11 = treep_05->data.block.nxt = tree_init(TREE_TYPE_BLOCK); + tree_t* treep_12 = treep_11->data.block.val = tree_init(TREE_TYPE_EXPR); + tree_t* treep_13 = treep_12->data.expr.val = tree_init(TREE_TYPE_CALL); + treep_13->data.call.target = "c"; + tree_t* treep_14 = treep_13->data.call.arg = tree_init(TREE_TYPE_CARG); + tree_t* treep_15 = treep_14->data.carg.val = tree_init(TREE_TYPE_EXPR); + tree_t* treep_16 = treep_15->data.expr.val = tree_init(TREE_TYPE_CALL); + treep_16->data.call.target = "d"; + treep_16->data.call.arg = NULL; + treep_14->data.carg.nxt = NULL; + treep_11->data.block.nxt = NULL; + tree_0->data.block.nxt = NULL; + + char src_0[] = "" \ + ":int:f = {" \ + " a.b;" \ + " c.d;" \ + "}"; + + pp = pp_init(src_0); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(tree_0, parser->tree)); + + pp_destroy(pp); + parser_destroy(parser); + lexer_destroy(lexer); + + TEST_REPORT; + + return 0; +} diff --git a/test/tree.c b/test/tree.c new file mode 100644 index 0000000..4c32746 --- /dev/null +++ b/test/tree.c @@ -0,0 +1,54 @@ +#include "include/test.h" +#include "../src/include/pp.h" +#include "../src/include/lexer.h" +#include "../src/include/tree.h" + +unsigned int TESTS_RUN = 0, TESTS_PASSED = 0; + +int main(int argc, char** argv) { + tree_t* tree_0; + tree_t* tree_1; + + tree_t* tree_lint_0; + tree_t* tree_lint_1; + tree_t* tree_lstr_0; + tree_t* tree_lstr_1; + tree_t* tree_tag_0; + tree_t* tree_tag_1; + tree_t* tree_def_0; + tree_t* tree_def_1; + tree_t* tree_call_0; + tree_t* tree_call_1; + + tree_0 = tree_init(TREE_TYPE_EXPR); + tree_1 = tree_init(TREE_TYPE_BLOCK); + + tree_lint_0 = tree_init(TREE_TYPE_LINT); + tree_lint_1 = tree_init(TREE_TYPE_LINT); + tree_lstr_0 = tree_init(TREE_TYPE_LSTR); + tree_lstr_1 = tree_init(TREE_TYPE_LSTR); + tree_tag_0 = tree_init(TREE_TYPE_TAG); + tree_tag_1 = tree_init(TREE_TYPE_TAG); + tree_def_0 = tree_init(TREE_TYPE_DEF); + tree_def_1 = tree_init(TREE_TYPE_DEF); + tree_call_0 = tree_init(TREE_TYPE_CALL); + tree_call_1 = tree_init(TREE_TYPE_CALL); + + /* Simple cases with nonexistent trees. */ + ASSERT(tree_cmp(NULL, tree_0) == 0); + ASSERT(tree_cmp(tree_0, NULL) == 0); + ASSERT(tree_cmp(NULL, NULL) == 1); + + /* Test tree types. */ + ASSERT(tree_cmp(tree_0, tree_0) == 1); + ASSERT(tree_cmp(tree_0, tree_1) == 0); + + /* Test lints. */ + tree_lint_0->data.lint.val = 0; + tree_lint_1->data.lint.val = 1; + ASSERT(tree_cmp(tree_lint_0, tree_lint_0) == 1); + ASSERT(tree_cmp(tree_lint_0, tree_lint_1) == 0); + + TEST_REPORT; + return 0; +} -- cgit v1.2.3