aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/codegen_test.c37
-rw-r--r--tests/main.c1
-rw-r--r--tests/tests.h1
3 files changed, 39 insertions, 0 deletions
diff --git a/tests/codegen_test.c b/tests/codegen_test.c
new file mode 100644
index 0000000..64421ea
--- /dev/null
+++ b/tests/codegen_test.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "parser/parser.h"
+#include "codegen/nasm.h"
+#include "tests.h"
+
+static void test_generate_consts_and_data(struct TestContext* context)
+{
+ struct Lexer lexer = create_lexer("const N = 5\ndata msg = \"hi\"\n");
+ struct Program program;
+ check(context, parse_program(&lexer, &program));
+
+ FILE* out = tmpfile();
+ generate_nasm(&program, out);
+ fflush(out);
+ rewind(out);
+
+ char buffer[1024];
+ size_t read = fread(buffer, 1, sizeof(buffer) - 1, out);
+ buffer[read] = '\0';
+ fclose(out);
+
+ check(context, strstr(buffer, "N equ 5") != NULL);
+ check(context, strstr(buffer, "section .data") != NULL);
+ check(context, strstr(buffer, "msg: db `hi`") != NULL);
+ check(context, strstr(buffer, ".len equ $ - msg") != NULL);
+ check(context, strstr(buffer, "section .text") != NULL);
+ check(context, strstr(buffer, "global _start") != NULL);
+
+ free_program(&program);
+}
+
+void run_codegen_tests(struct TestContext* context)
+{
+ test_generate_consts_and_data(context);
+}
diff --git a/tests/main.c b/tests/main.c
index b69bb3e..f0caf52 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -8,6 +8,7 @@ int main(void)
run_lexer_tests(&context);
run_parser_tests(&context);
+ run_codegen_tests(&context);
printf("\n%d checks, %d failure(s)\n", context.checks, context.failures);
return context.failures == 0 ? 0 : 1;
diff --git a/tests/tests.h b/tests/tests.h
index 9136910..32ee4a6 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -4,3 +4,4 @@
void run_lexer_tests(struct TestContext* context);
void run_parser_tests(struct TestContext* context);
+void run_codegen_tests(struct TestContext* context);