aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: 0dfd8781aa3ebedcf45f2f4a2bdce50bd9789046 (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
#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>

#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;
}

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

    return_if_error(test_hash_map());

    result = amal_compiler_init(&compiler);
    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", &file_scope);
    if(result != AMAL_COMPILER_OK) {
        fprintf(stderr, "Failed to load file, error code: %d\n", result);
        return 1;
    }

    return amal_compiler_deinit(&compiler);
}