aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: 1394860cc73103c78a6adb38497e69de50897c0f (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <stdio.h>
#include <string.h>
#include "../include/compiler.h"
#include "../include/std/hash_map.h"
#include "../include/std/hash.h"
#include "../include/std/log.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static int num_threads = 0;
static int num_successful_tests = 0;
static int num_tests_run = 0;

#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_map_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)); return; } while(0)

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

typedef struct {
    char *filepath;
} ErrorUnexpectedData;

static void error_callback_assert(const char *err_msg, int err_msg_len, void *userdata) {
    ErrorExpectedData *expected_data;
    int expected_err_msg_len;
    expected_data = userdata;
    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;
}
#if 0
static void error_callback(const char *err_msg, int err_msg_len, void *userdata) {
    ErrorUnexpectedData *data;
    data = userdata;
    fprintf(stderr, "Test failed: %s with error: %.*s\n", data->filepath, err_msg_len, err_msg);
    exit(1);
}
#endif
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 CHECK_RESULT int get_thread_count_env_var(int *thread_count) {
    char *threads;
    threads = getenv("THREADS");
    if(!threads)
        return -1;
    *thread_count = atoi(threads);
    return 0;
}

static void test_load(const char *filepath) {
    amal_compiler_options options;
    amal_program program;
    char *full_path;
    int result;

    amal_compiler_options_init(&options);
    options.num_threads = num_threads;
    ++num_tests_run;
    full_path = get_full_path(filepath);

    if(amal_program_init(&program) != 0) {
        fprintf(stderr, "Failed to initialize amal program\n");
        FAIL_TEST(full_path);
    }
    result = amal_compiler_load_file(&options, &program, filepath);
    if(result != AMAL_COMPILER_OK) {
        fprintf(stderr, "Failed to load file %s, result: %d\n", full_path, result);
        FAIL_TEST(full_path);
    }

    result = amal_program_run(&program);
    if(result != 0) {
        fprintf(stderr, "Failed to run the program %s, result: %d\n", full_path, result);
        FAIL_TEST(full_path);
    }
    amal_program_deinit(&program);

    fprintf(stderr, "Test succeeded as expected: %s\n", full_path);
    ++num_successful_tests;
    free(full_path);
}

static void test_load_error(const char *filepath, const char *expected_error) {
    amal_compiler_options options;
    amal_program program;
    ErrorExpectedData expected_data;

    amal_compiler_options_init(&options);
    options.num_threads = num_threads;
    ++num_tests_run;
    options.error_callback = error_callback_assert;

    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;

    if(amal_program_init(&program) != 0) {
        fprintf(stderr, "Failed to initialize amal program\n");
        FAIL_TEST(expected_data.filepath);
    }
    if(amal_compiler_load_file(&options, &program, filepath) == AMAL_COMPILER_OK) {
        fprintf(stderr, "Expected to fail loading file\n");
        FAIL_TEST(expected_data.filepath);
    }
    amal_program_deinit(&program);

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

    fprintf(stderr, "Test failed as expected: %s\n", expected_data.filepath);
    ++num_successful_tests;
    free(expected_data.filepath);
    free(expected_data.expected_error);
}

/* TODO: Restrict variables in global scope to const */
int main(int argc, char **argv) {
    int result;
    result = get_thread_count_env_var(&num_threads);
    if(result != 0)
        num_threads = 0;

    if(num_threads < 0) {
        amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 0 (0 = use the number of available threads on the system)");
        exit(1);
    }

    return_if_error(test_hash_map());

    /* Run all tests */
    if(argc == 1) {
        test_load("tests/main.amal");
        test_load("tests/utf8bom.amal");
        test_load("tests/bytecode.amal");

        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 global structs can be public\n"
            "    pub const num = 45;\n"
            "    ^\n");
        test_load_error("tests/errors/closure_no_lhs.amal", 
            "1:1: error: Expected string, variable, closure, struct, function call or import\n"
            "fn {}\n"
            "^\n");
        test_load_error("tests/errors/const_assign.amal", 
            "3:5: error: Can't assign to a const value\n"
            "    value = 34;\n"
            "    ^\n");
        fprintf(stderr, "##### %d/%d tests succeeded #####\n", num_successful_tests, num_tests_run);
    } else if(argc == 2) {
        test_load(argv[1]);
        fprintf(stderr, "##### %d/%d tests succeeded #####\n", num_successful_tests, num_tests_run);
    } else {
        fprintf(stderr, "usage: test [test-file-path]\n");
        fprintf(stderr, "If you run test without any files then all tests will run.\n");
        fprintf(stderr, "examples:\n");
        fprintf(stderr, "  test\n");
        fprintf(stderr, "  test tests/main.amal\n");
        exit(1);
    }

    return 0;
}