From 61efb96fe21005ad0422d630f1b30f66c9481d52 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 29 Aug 2022 12:23:48 +1000 Subject: [PATCH] Don't get testy with me --- source/scope.h | 1 + test/test_literal_dictionary.c | 38 ++++++++++ test/test_node.c | 21 ++++++ test/test_scope.c | 124 +++++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 test/test_literal_dictionary.c create mode 100644 test/test_node.c create mode 100644 test/test_scope.c diff --git a/source/scope.h b/source/scope.h index 153d837..f069401 100644 --- a/source/scope.h +++ b/source/scope.h @@ -23,3 +23,4 @@ bool setScopeVariable(Scope* scope, Literal key, Literal value, bool constCheck) bool getScopeVariable(Scope* scope, Literal key, Literal* value); Literal getScopeType(Scope* scope, Literal key); + diff --git a/test/test_literal_dictionary.c b/test/test_literal_dictionary.c new file mode 100644 index 0000000..9f2c789 --- /dev/null +++ b/test/test_literal_dictionary.c @@ -0,0 +1,38 @@ +#include "literal_dictionary.h" + +#include "memory.h" +#include "console_colors.h" + +#include + +int main() { + { + //test init & cleanup + LiteralDictionary dictionary; + initLiteralDictionary(&dictionary); + freeLiteralDictionary(&dictionary); + } + + { + //test insertion and deletion + char* idn_raw = "foobar"; + char* str_raw = "hello world"; + + Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw)); + Literal string = TO_STRING_LITERAL(copyString(str_raw, strlen(str_raw)), strlen(str_raw)); + + LiteralDictionary dictionary; + initLiteralDictionary(&dictionary); + + setLiteralDictionary(&dictionary, identifier, string); + + freeLiteral(identifier); + freeLiteral(string); + + freeLiteralDictionary(&dictionary); + } + + printf(NOTICE "All good\n" RESET); + return 0; +} + diff --git a/test/test_node.c b/test/test_node.c new file mode 100644 index 0000000..18b7d68 --- /dev/null +++ b/test/test_node.c @@ -0,0 +1,21 @@ +#include "node.h" + +#include "memory.h" +#include "console_colors.h" + +#include + +int main() { + { + //test literals + char* str = "foobar"; + + Node* node; + emitNodeLiteral(&node, TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str)) ); + freeNode(node); + } + + printf(NOTICE "All good\n" RESET); + return 0; +} + diff --git a/test/test_scope.c b/test/test_scope.c new file mode 100644 index 0000000..765afbb --- /dev/null +++ b/test/test_scope.c @@ -0,0 +1,124 @@ +#include "scope.h" + +#include "memory.h" +#include "console_colors.h" + +#include + +int main() { + { + //test init & quit + Scope* scope = pushScope(NULL); + scope = popScope(scope); + } + + { + //prerequisites + char* idn_raw = "foobar"; + + Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw)); + Literal value = TO_INTEGER_LITERAL(42); + Literal type = TO_TYPE_LITERAL(value.type, false); + + //test declarations & assignments + Scope* scope = pushScope(NULL); + + //declare & assign + if (!declareScopeVariable(scope, identifier, type)) { + printf(ERROR "Failed to declare scope variable" RESET); + return -1; + } + + if (!setScopeVariable(scope, identifier, value, true)) { + printf(ERROR "Failed to sete scope variable" RESET); + return -1; + } + + //cleanup + scope = popScope(scope); + + freeLiteral(identifier); + freeLiteral(value); + freeLiteral(type); + } + + { + //prerequisites + char* idn_raw = "foobar"; + + Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw)); + Literal type = TO_TYPE_LITERAL(LITERAL_INTEGER, false); + + //test declarations & assignments + Scope* scope = pushScope(NULL); + + //declare & assign + if (!declareScopeVariable(scope, identifier, type)) { + printf(ERROR "Failed to declare the scope variable" RESET); + return -1; + } + + if (!setScopeVariable(scope, identifier, TO_INTEGER_LITERAL(42), true)) { + printf(ERROR "Failed to set the scope variable" RESET); + return -1; + } + + //deeper scope + scope = pushScope(scope); + + //test shadowing + Literal ref; + if (!getScopeVariable(scope, identifier, &ref)) { + printf(ERROR "Failed to get the scope variable" RESET); + return -1; + } + + if (AS_INTEGER(ref) != 42) { + printf(ERROR "Failed to retreive the correct variable value" RESET); + return -1; + } + + if (!declareScopeVariable(scope, identifier, type)) { + printf(ERROR "Failed to declare the scope variable (shadowing)" RESET); + return -1; + } + + if (!setScopeVariable(scope, identifier, TO_INTEGER_LITERAL(69), true)) { + printf(ERROR "Failed to set the scope variable (shadowing)" RESET); + return -1; + } + + if (!getScopeVariable(scope, identifier, &ref)) { + printf(ERROR "Failed to get the scope variable (shadowing)" RESET); + return -1; + } + + if (AS_INTEGER(ref) != 69) { + printf(ERROR "Failed to retreive the correct variable value (shadowing)" RESET); + return -1; + } + + //unwind + scope = popScope(scope); + + if (!getScopeVariable(scope, identifier, &ref)) { + printf(ERROR "Failed to get the scope variable" RESET); + return -1; + } + + if (AS_INTEGER(ref) != 42) { + printf(ERROR "Failed to retreive the correct variable value" RESET); + return -1; + } + + //cleanup + scope = popScope(scope); + + freeLiteral(identifier); + freeLiteral(type); + } + + printf(NOTICE "All good\n" RESET); + return 0; +} +