summaryrefslogtreecommitdiff
path: root/source/base/base_test.h
blob: 072bec3360993459b90e42cf1311e85e7dd2525d (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
// TODO(nasr): metaprogram that takes an expected output and generates a test for that specified 
// function
/* base library testing framework */
#ifndef BASE_TEST_H
#define BASE_TEST_H

// helper macro
#define show \
    do \
    { \
        write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \
        write(STDOUT_FILENO, ":", 1); \
        write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \
        write(STDOUT_FILENO, ":", 1); \
        write_int(__LINE__); \
        write(STDOUT_FILENO, "\n", 1); \
    } while (0)

#define test(expr) \
    { \
        if ((expr) != 0) \
        { \
            write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \
            show; \
            _exit(1); \
        } \
    }

#define verify(expr) \
    { \
        if ((expr) != 0) \
        { \
            write(STDERR_FILENO, Red "[ERROR] ", Len(Red "[ERROR] ")); \
            show; \
            write(STDERR_FILENO, Reset, Len(Reset)); \
            _exit(1); \
        } \
        else \
        { \
            write(STDERR_FILENO, Green "[SUCCESS] ", Len(Green "[SUCCESS] ")); \
            show; \
            write(STDERR_FILENO, Reset, Len(Reset)); \
        } \
    }

#endif /* BASE_TEST_H */