aboutsummaryrefslogtreecommitdiff
path: root/test/test.c
blob: 02a87303310854a09730ca0b100d12e68cd3df25 (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

#include "kpl_test.h"

static int32_t kpl_test_tree_compare(const void *node_a, const void *node_b) {
    const kpl_test *test_a = node_a, *test_b = node_b;
    return strcmp(test_a->name, test_b->name);
}

static kpl_tree test_tree = KPL_TREE_INIT_STATIC(kpl_test_tree_compare, NULL);

static kpl_test *test_head = NULL, *test_tail = NULL, *error_head = NULL, *error_tail = NULL;

void _kpl_test_add(kpl_test *_test) {
    if (!test_head)
        test_head = test_tail = _test;
    else
       test_tail = test_tail->list_next = _test;
    kpl_tree_insert(&test_tree, _test);
}

static void kpl_test_report(const kpl_test *_test) {
    printf(KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_CYAN) "%s" KPL_COLOR(RESET),
        _test->source_file, _test->name);
    if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT) {
        printf(KPL_COLOR(LIGHT_GREEN) " PASS\n"  KPL_COLOR(RESET));
        return;
    }
    printf("::" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) KPL_COLOR(LIGHT_RED) " %s\n" KPL_COLOR(RESET),
        _test->error_line_number, _test->error_message);
}

static int32_t kpl_test_report_error(void) {
    if (!error_head) {
        printf(KPL_COLOR2(BOLD, GREEN) "PASSED\n" KPL_COLOR(RESET));
        return KPL_EXIT_CODE_SUCCESS;
    }
    printf(KPL_COLOR2(BOLD, RED) "FAILED\n" KPL_COLOR(RESET));
    while (error_head) {
        kpl_test *_test = error_head;
        error_head = error_head->list_next;
        kpl_test_report(_test);
    }
    return KPL_EXIT_CODE_ERROR;
}

static void kpl_test_run_one(kpl_test *_test) {
    _test->list_next = NULL;
    _test->test_fn(_test);
    kpl_test_report(_test);
    if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT)
        return;
    if (!error_head)
        error_head = error_tail = _test;
    else
       error_tail = error_tail->list_next = _test;
}

static int32_t kpl_test_run_all(void) {
    while (test_head) {
        kpl_test *_test = test_head;
        test_head = test_head->list_next;
        kpl_test_run_one(_test);
    }
    return kpl_test_report_error();
}

int32_t main(int32_t argc, char *argv[]) {
    if (argc < 2)
        return kpl_test_run_all();
    kpl_test find = {};
    for (int32_t arg_index = 1; arg_index < argc; arg_index++) {
        find.name = argv[arg_index];
        kpl_test *found = kpl_tree_find(&test_tree, &find);
        if (!found) {
            printf(KPL_COLOR2(BOLD, RED) "TEST: " KPL_COLOR(RESET) KPL_COLOR2(BOLD, WHITE) "%s " KPL_COLOR(RESET)
                KPL_COLOR2(BOLD, RED) "NOT FOUND\n" KPL_COLOR(RESET), find.name);
            return KPL_EXIT_CODE_ERROR;
        }
        kpl_test *duplicates = found->tree_duplicates;
        while (duplicates) {
            kpl_test_run_one(duplicates);
            duplicates = duplicates->tree_duplicates;
        }
        kpl_test_run_one(found);
    }
    return kpl_test_report_error();
}

KPL_TEST(pass_by_default) {
    // pass, uncomment below to fail
    // KPL_FAIL("example fail");
}