aboutsummaryrefslogtreecommitdiff
path: root/tests/test_as_tokenizer.c
blob: 7d8209923d45aa60c0eb89e193106be93c0a50c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <string.h> 
#include <stdlib.h>
#include <assert.h>

#include "as_tokenizer.h"
#include "utils.h"

char *input_buffer = 
    "start:\n"
    "    add 1\n"
    "    sub start\n"
    "    div\n"
    "    eq\n";

char *expected_output = 
    "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(input_buffer, strlen(input_buffer), "r");
    struct allocator * alct = new_allocator();
    struct token_stream * ts = new_token_stream(alct, fp);
    
    char *output_buffer = malloc(10240);
    // redirect stdout to a file
    FILE *out = fmemopen(output_buffer, 10240, "w");
    FILE *origin_stdout = stdout;
    stdout = out;
    while (peek_token(alct, ts)->type != TK_ENDOFFILE) {
        print_token(peek_token(alct, ts));
        next_token(alct, ts);
    }
    fclose(out);
    stdout = origin_stdout;

    assert(strcmp(output_buffer, expected_output) == 0);
    printf("[PASS] assembler tokenizer\n");
    free(output_buffer);
    delete_allocator(alct);
    return 0;
}