From fa403b3fb6ff14dbed73d815684d6935c3c19717 Mon Sep 17 00:00:00 2001 From: s-over-4 Date: Mon, 10 Jul 2023 00:59:08 -0400 Subject: made a test framework (HLKT) --- src/include/hlkt.h | 29 +++++++++++++++++++++++++++++ src/include/util.h | 1 + src/main.c | 5 +++-- src/parser.c | 2 +- src/tree.c | 1 - src/util.c | 16 ++++++++++++++-- 6 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/include/hlkt.h (limited to 'src') diff --git a/src/include/hlkt.h b/src/include/hlkt.h new file mode 100644 index 0000000..295e5f4 --- /dev/null +++ b/src/include/hlkt.h @@ -0,0 +1,29 @@ +#ifndef HLKT_H +#define HLKT_H + +#include + +#include "util.h" + +static int hlkt_run = 0; /* number of tests run */ +static int hlkt_failed = 0; /* number of tests that have failed */ + +#define HLKT_HIDE(stuff) do { stuff } while (0) + +#define HLKT_LOG() HLKT_HIDE( \ + if ((hlkt_run > 0) && (hlkt_failed > 0)) { \ + log_war("HLKT: %d/%d tests failed", hlkt_failed, hlkt_run); \ + } else { \ + log_inf("HLKT: all %d tests passed", hlkt_run); \ + } \ +) + +#define HLKT_ASS(pred) HLKT_HIDE( \ + hlkt_run ++; \ + if (! pred) { \ + hlkt_failed ++; \ + log_err("HLKT: test [%s] failed: %s:%s:%d", #pred, __FILE__, __func__, __LINE__);\ + } \ +) + +#endif diff --git a/src/include/util.h b/src/include/util.h index 63ee8bb..a7879e9 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -8,6 +8,7 @@ void die(const char* fmt, ...); +void log_err(const char* fmt, ...); void log_inf(const char* fmt, ...); void log_raw(const char* fmt, ...); void log_war(const char* fmt, ...); diff --git a/src/main.c b/src/main.c index 1efc797..75ee98f 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "include/token.h" #include "include/lexer.h" #include "include/source.h" +#include "include/hlkt.h" int main(int argc, char* argv[]) { char* source; @@ -14,6 +15,7 @@ int main(int argc, char* argv[]) { source = source_get(argv[1]); lexer = lexer_init(source); + HLKT_ASS(lexer); log_inf("lexer created"); log_inf("BEGIN INPUT"); @@ -37,10 +39,9 @@ int main(int argc, char* argv[]) { // clean up lexer_destroy(lexer); - //fclose(fsource); free(source); - log_inf("source file closed"); + HLKT_LOG(); return 0; } diff --git a/src/parser.c b/src/parser.c index ab51b5b..4233138 100644 --- a/src/parser.c +++ b/src/parser.c @@ -99,7 +99,7 @@ tree_t* parser_parse_def(parser_t* parser) { def->data.def.name = parser->token->value; - def->data.def. +// def->data.def. while (parser->token->type == TOKEN_DEF_TAG) { diff --git a/src/tree.c b/src/tree.c index aadfc3f..0ed0697 100644 --- a/src/tree.c +++ b/src/tree.c @@ -12,7 +12,6 @@ tree_t* tree_init(int type) { tree->data.comp.size = 0; break; case TREE_DEF: - tree->data.def.mutability = 0; tree->data.def.name = (void*) 0; tree->data.def.value = (void*) 0; break; diff --git a/src/util.c b/src/util.c index 15ac0d4..ba99bda 100644 --- a/src/util.c +++ b/src/util.c @@ -3,7 +3,7 @@ void die(const char* fmt, ...) { va_list ap; - fprintf(stderr, "[\e[31m==\e[0m] ERROR "); + fprintf(stderr, "[\e[31;1m==\e[0m] FATAL ERROR "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); @@ -13,6 +13,18 @@ void die(const char* fmt, ...) { exit(1); } +void log_err(const char* fmt, ...) { + va_list ap; + + fprintf(stderr, "[\e[31m==\e[0m] ERROR "); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fprintf(stderr, "\n"); +} + void log_inf(const char* fmt, ...) { va_list ap; @@ -35,7 +47,7 @@ void log_raw(const char* fmt, ...) { void log_war(const char* fmt, ...) { va_list ap; - fprintf(stderr, "[\e[33m==\e[0m] WARNING"); + fprintf(stderr, "[\e[33m==\e[0m] WARNING "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); -- cgit v1.2.3