aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_as_tokenizer.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/test_as_tokenizer.c b/tests/test_as_tokenizer.c
new file mode 100644
index 0000000..c32eddb
--- /dev/null
+++ b/tests/test_as_tokenizer.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "as_tokenizer.h"
+
+char *inputBuffer =
+ "start:\n"
+ " add 1\n"
+ " sub start\n"
+ " div\n"
+ " eq\n";
+
+char *expectedOutput =
+ "LABEL: start, line: 1, col: 1\n"
+ "COLON\n"
+ "NEWLINE\n"
+ "OP: add, line: 2, col: 5\n"
+ "ARG: 1, line: 2, col: 10\n"
+ "NEWLINE\n"
+ "OP: sub, line: 3, col: 5\n"
+ "LABEL: start, line: 3, col: 9\n"
+ "NEWLINE\n"
+ "OP: div, line: 4, col: 5\n"
+ "NEWLINE\n"
+ "OP: eq, line: 5, col: 5\n"
+ "NEWLINE\n";
+
+int main(int argc, char** argv) {
+ printf("[TEST] assembler tokenizer\n");
+ // make a memory buffer to FILE*
+ FILE *fp = fmemopen(inputBuffer, strlen(inputBuffer), "r");
+ TokenStream* ts = makeTokenStream(fp);
+
+ char *outputBuffer = malloc(10240);
+ // redirect stdout to a file
+ FILE *out = fmemopen(outputBuffer, 10240, "w");
+ FILE *origin_stdout = stdout;
+ stdout = out;
+ while (peekToken(ts)->type != ENDOFFILE) {
+ printToken(peekToken(ts));
+ nextToken(ts);
+ }
+ fclose(out);
+ stdout = origin_stdout;
+ // compare outputBuffer with expectedOutput
+ assert(strcmp(outputBuffer, expectedOutput) == 0);
+ printf("[PASS] assembler tokenizer\n");
+ return 0;
+}
+
+