diff options
author | c | 2024-01-27 22:58:23 -0500 |
---|---|---|
committer | c | 2024-01-27 22:58:23 -0500 |
commit | 87e0d4b3d23a7eb38f0e196eac333c318fef27ea (patch) | |
tree | ce3b2cee903b7022e57a558f4725fed2b58e2813 /test | |
parent | f48c3a6cb320c897ca7477af17f8e5eb7447fd72 (diff) |
Added string parsing, and tests for more blocks.
cool bug on window rn
Diffstat (limited to 'test')
-rw-r--r-- | test/include/test.h | 8 | ||||
-rw-r--r-- | test/parser.c | 173 | ||||
-rw-r--r-- | test/tree.c | 16 |
3 files changed, 184 insertions, 13 deletions
diff --git a/test/include/test.h b/test/include/test.h index 02c3d18..3193c52 100644 --- a/test/include/test.h +++ b/test/include/test.h @@ -3,6 +3,10 @@ #include "../../src/include/util.h" +/* + This is probably how it's supposed to work. In what other cases would you + need `extern`? +*/ extern unsigned int TESTS_RUN; extern unsigned int TESTS_PASSED; @@ -11,8 +15,8 @@ extern unsigned int TESTS_PASSED; #define ASSERT(EXPR) \ TESTS_RUN++; \ (EXPR && ++TESTS_PASSED) ? \ - log_inf("%s:%s:%d: Assertion passed!", __func__, __FILE__, __LINE__) : \ - log_err("%s:%s:%d: Assertion failed:\n\t%s", __func__, __FILE__, __LINE__, #EXPR); + log_inf("%s:%s:%d: Assertion passed!", __FILE__, __func__, __LINE__) : \ + log_err("%s:%s:%d: Assertion failed:\n\t%s", __FILE__, __func__, __LINE__, #EXPR); #define TEST_REPORT \ (TESTS_RUN == TESTS_PASSED) ? \ diff --git a/test/parser.c b/test/parser.c index 2007e93..feb9c70 100644 --- a/test/parser.c +++ b/test/parser.c @@ -6,13 +6,17 @@ TEST_INIT -void test_simple_empty() { +void empty() { + /* + Hmm… I feel as though this setup/cleanup process could be wrapped in a + macro. Tentative TODO? + */ tree_t* tree; pp_t* pp; lexer_t* lexer; parser_t* parser; - char src[] = " "; + char src[] = ""; /* @@ -32,6 +36,7 @@ void test_simple_empty() { ASSERT(tree_cmp(parser->tree, tree) == 1); + /* Probably not cleaning up fully here. Whatever. */ token_destroy(lexer->tokenl); lexer_destroy(lexer); pp_destroy(pp); @@ -39,7 +44,7 @@ void test_simple_empty() { parser_destroy(parser); } -void test_single_lint() { +void single_lint() { tree_t* tree; pp_t* pp; lexer_t* lexer; @@ -82,7 +87,7 @@ void test_single_lint() { parser_destroy(parser); } -void test_double_lint() { +void double_lint() { tree_t* tree; pp_t* pp; lexer_t* lexer; @@ -134,10 +139,164 @@ void test_double_lint() { parser_destroy(parser); } +void single_lstr() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "\"Hello, World.\""; + + /* + + [block] + val: + [lstr] + val: + "Hello, World." + len: + 13 + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0lstr = tree->data.block.val = tree_init(TREE_TYPE_LSTR); + tree0lstr->data.lstr.val = "Hello, World."; + tree0lstr->data.lstr.len = 13; + tree->data.block.nxt = NULL; + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); +} + +void double_lstr() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "\"foo\";\"bar\""; + + /* + + [block] + val: + [lstr] + val: + "foo" + len: + 3 + nxt: + [block] + val: + [lstr] + val: + "bar" + len: + 3 + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0lstr = tree->data.block.val = tree_init(TREE_TYPE_LSTR); + tree0lstr->data.lstr.val = "foo"; + tree0lstr->data.lstr.len = 3; + tree_t* tree0block = tree->data.block.nxt = tree_init(TREE_TYPE_BLOCK); + tree_t* tree1lstr = tree0block->data.block.val = tree_init(TREE_TYPE_LSTR); + tree1lstr->data.lstr.val = "bar"; + tree1lstr->data.lstr.len = 3; + tree0block->data.block.nxt = NULL; + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); +} + +void single_block() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "{}"; + + /* + + [block] + val: + [block] + val: + NULL + nxt: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0block = tree->data.block.val = tree_init(TREE_TYPE_BLOCK); + tree0block->data.block.val = NULL; + tree0block->data.block.nxt = NULL; + tree->data.block.nxt = NULL; + + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); +} + int main(int argc, char** argv) { - test_simple_empty(); - test_single_lint(); - test_double_lint(); + empty(); + single_lint(); + double_lint(); + single_lstr(); + double_lstr(); + single_block(); TEST_REPORT; diff --git a/test/tree.c b/test/tree.c index 1b2742c..b9ef210 100644 --- a/test/tree.c +++ b/test/tree.c @@ -3,12 +3,17 @@ #include "../src/include/lexer.h" #include "../src/include/tree.h" -unsigned int TESTS_RUN = 0, TESTS_PASSED = 0; +TEST_INIT -int main(int argc, char** argv) { +/* + I wrote these before I started putting tests in their own functions, so + this'll have to do for now. +*/ +void test_things() { tree_t* tree_0; tree_t* tree_1; + /* Not refactoring this. */ tree_t* tree_lint_0; tree_t* tree_lint_1; tree_t* tree_lstr_0; @@ -240,7 +245,10 @@ int main(int argc, char** argv) { ASSERT(tree_cmp(tree_block_0, tree_block_0) == 1); ASSERT(tree_cmp(tree_block_0, tree_block_1) == 0); +} + +int main() { + test_things(); - TEST_REPORT; - return 0; + TEST_REPORT } |