aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: bc2fbb1d3f371439003cfc9ce307d882c12a4f7a (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <string.h>
#include "../include/compiler.h"
#include "../include/std/hash_map.h"
#include "../include/std/hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define REQUIRE_EQ_INT(expected, actual) do{\
    if((expected) != (actual)) {\
        fprintf(stderr, "%s:%d: Assert failed (%s vs %s).\nExpected %d, got %d.\n", __FILE__, __LINE__, #expected, #actual, (expected), (actual));\
        exit(1);\
    }\
}while(0)

static CHECK_RESULT int test_hash_map() {
    ScopedAllocator scoped_allocator;
    HashMap hash_map;
    int value;
    bool has_key;
    unsigned char i;

    return_if_error(scoped_allocator_init(&scoped_allocator));
    cleanup_if_error(hash_map_init(&hash_map, &scoped_allocator, sizeof(int), hash_compare_string, amal_hash_string));

    value = 34;
    return_if_error(hash_map_insert(&hash_map, create_buffer_view("hello", 5), &value));
    value = 50;
    for(i = 0; i < 128; ++i)
        return_if_error(hash_map_insert(&hash_map, create_buffer_view((const char*)&i, 1), &value));
    return_if_error(hash_map_insert(&hash_map, create_buffer_view("hellp", 5), &value));
    has_key = hash_map_get(&hash_map, create_buffer_view("hello", 5), &value);
    if(!has_key) {
        fprintf(stderr, "Missing value for key \"hello\" in hash map\n");
        exit(1);
    }
    REQUIRE_EQ_INT(34, value);

    cleanup:
    scoped_allocator_deinit(&scoped_allocator);
    return 0;
}

#define FAIL_TEST(name) do { fprintf(stderr, "Test failed: %s\n", (name)); exit(1); } while(0)

typedef struct {
    char *filepath;
    char *expected_error;
    bool got_expected_error;
} ErrorExpectedData;

static void error_callback_assert(const char *err_msg, int err_msg_len, void *userdata) {
    ErrorExpectedData *expected_data;
    expected_data = userdata;
    int expected_err_msg_len;
    expected_err_msg_len = strlen(expected_data->expected_error);

    if(expected_data->got_expected_error) {
        fprintf(stderr, "We got the error we expected but also got additional error:\n%.*s\n", err_msg_len, err_msg);
        FAIL_TEST(expected_data->filepath);
    }

    if(err_msg_len != expected_err_msg_len || strncmp(err_msg, expected_data->expected_error, expected_err_msg_len) != 0) {
        fprintf(stderr, "Expected error message:\n%.*s\n", expected_err_msg_len, expected_data->expected_error);
        fprintf(stderr, "Actual error message:\n%.*s\n", err_msg_len, err_msg);
        fprintf(stderr, "a: %d, b: %d\n", expected_err_msg_len, err_msg_len);
        FAIL_TEST(expected_data->filepath);
    }

    expected_data->got_expected_error = bool_true;
}

static char* get_full_path(const char *filepath) {
    #define PATH_LEN 4096
    char *buf;
    int len;
    int filepath_len;

    buf = malloc(PATH_LEN);
    buf[PATH_LEN - 1] = '\0';
    getcwd(buf, PATH_LEN);

    len = strlen(buf);
    filepath_len = strlen(filepath);

    buf[len++] = '/';
    memcpy(buf + len, filepath, filepath_len);
    buf[len + filepath_len] = '\0';
    return buf;
}

static char* join_str(const char *str1, const char *str2, char delimiter) {
    char *buf;
    int len1;
    int len2;

    len1 = strlen(str1);
    len2 = strlen(str2);
    buf = malloc(len1 + 1 + len2 + 1);

    memcpy(buf, str1, len1);
    buf[len1] = delimiter;
    memcpy(buf + len1 + 1, str2, len2);
    buf[len1 + 1 + len2] = '\0';
    return buf;
}

static void test_load_error(const char *filepath, const char *expected_error) {
    amal_compiler compiler;
    amal_compiler_options options;
    int result;

    amal_compiler_options_init(&options);
    options.error_callback = error_callback_assert;
    ErrorExpectedData expected_data;
    expected_data.filepath = get_full_path(filepath);
    expected_data.expected_error = join_str(expected_data.filepath, expected_error, ':');
    expected_data.got_expected_error = bool_false;
    options.error_callback_userdata = &expected_data;

    result = amal_compiler_init(&compiler, &options);
    if(result != AMAL_COMPILER_OK) {
        fprintf(stderr, "Failed to initialize compiler, error code: %d\n", result);
        FAIL_TEST(expected_data.filepath);
    }

    result = amal_compiler_load_file(&compiler, filepath);
    if(result == AMAL_COMPILER_OK) {
        fprintf(stderr, "Expected to fail loading file\n");
        FAIL_TEST(expected_data.filepath);
    }

    if(amal_compiler_deinit(&compiler) != 0) {
        fprintf(stderr, "Failed to deinitialize compiler.\n");
        FAIL_TEST(expected_data.filepath);
    }

    if(!expected_data.got_expected_error) {
        fprintf(stderr, "Didn't get expected error message:\n%s\n", expected_error);
        FAIL_TEST(expected_data.filepath);
    }
    free(expected_data.filepath);
    free(expected_data.expected_error);
}

/* TODO: Restrict variables in global scope to const */
int main() {
    /*amal_compiler compiler;
    int result;*/

    return_if_error(test_hash_map());

    /*
    result = amal_compiler_init(&compiler, NULL);
    if(result != AMAL_COMPILER_OK) {
        fprintf(stderr, "Failed to initialize compiler, error code: %d\n", result);
        return 1;
    }

    result = amal_compiler_load_file(&compiler, "tests/main.amal");
    if(result != AMAL_COMPILER_OK) {
        fprintf(stderr, "Failed to load file, error code: %d\n", result);
        return 1;
    }

    return amal_compiler_deinit(&compiler);
    */
    test_load_error("tests/errors/duplicate_declaration.amal", 
        "2:7: error: Variable with the name main was declared twice in the same scope\n"
        "const main = fn {}\n"
        "      ^\n");
    test_load_error("tests/errors/pub_in_closure.amal", 
        "2:5: error: Only declarations in structs can be public\n"
        "    pub const num = 45;\n"
        "    ^\n");
    test_load_error("tests/errors/closure_no_lhs.amal", 
        "1:1: error: Expected variable declaration, string, variable or function call\n"
        "fn {}\n"
        "^\n");
    return 0;
}