aboutsummaryrefslogtreecommitdiff
path: root/tests/test.h
blob: 44fd7fb642b2e5cf2ceb536e75ac0d112af9e45c (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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#define START_TESTS int main() {
#define END_TESTS }
#define TEST(name) strcpy(current_test, #name);
#define assert_null(actual) _assert(actual == NULL, __FILE__, __LINE__, "invalid value: expected NULL, got %s", actual)
#define assert_str(actual, expected) _assert(actual != NULL && strcmp(actual, expected) == 0, __FILE__, __LINE__, "invalid string: expected \"%s\", got \"%s\"", expected, actual)
#define assert(assertion, format, ...) _assert(assertion, __FILE__, __LINE__, format, ##__VA_ARGS__)
#define ARRAY_SIZE(arr) sizeof arr / sizeof arr[0]

/* used to store the running test name */
char current_test[256] = {'\0'};

static void _assert(int assertion, const char filename[64], const int line, char *format, ...)
{
    if (assertion)
    {
        return;
    }

    va_list args;
    va_start(args, format);
    printf("%s:%d:%s failed: ", filename, line, current_test);
    vprintf(format, args);
    va_end(args);
    printf("\n");
    exit(1);
}