aboutsummaryrefslogtreecommitdiff
path: root/include/kpl_test.h
blob: c1645e2b80eb04d3c404ec29cc285133e3dbd73b (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

#pragma once

// KPL testing framework

#include "kpl.h"

typedef struct _kpl_test kpl_test;

typedef void kpl_test_function(__attribute__((unused)) kpl_test *_test);

typedef struct _kpl_test {
    int32_t error_line_number; // -1 for no error
    char *file_name, *test_name, *error_message;
    kpl_test_function *test_function;
    struct _kpl_test *next_test;
} kpl_test;

void kpl_test_add(kpl_test *_test);

#define _KPL_TEST_FN_NAME(NAME) _kpl_test_function_ ## NAME

#define _KPL_TEST_FN(NAME) static void _KPL_TEST_FN_NAME(NAME)(__attribute__((unused)) kpl_test *_test)

#define KPL_TEST(NAME) _KPL_TEST_FN(NAME); \
static void __attribute__((constructor)) _kpl_test_constructor_ ## NAME(void) { \
    static kpl_test _test = { \
        .error_line_number = -1, \
        .file_name = __FILE__, .test_name = #NAME, .error_message = NULL, \
        .test_function = _KPL_TEST_FN_NAME(NAME), \
        .next_test = NULL \
    }; \
    kpl_test_add(&_test); \
} \
_KPL_TEST_FN(NAME)

#define KPL_FAIL(MESSAGE) do { \
    _test->error_line_number = __LINE__; \
    _test->error_message = MESSAGE; \
} while (0)

#define KPL_ASSERT(CONDITION, MESSAGE) do { \
    if (!(CONDITION)) \
        KPL_FAIL(#CONDITION "; " MESSAGE); \
} while (0)