Compare commits
3 Commits
25b0e92ed0
...
1bd017adfc
Author | SHA1 | Date | |
---|---|---|---|
1bd017adfc | |||
238db54668 | |||
c1c3e87444 |
63
test.h
63
test.h
@ -1,7 +1,7 @@
|
||||
// include this file in all the test files
|
||||
|
||||
#ifndef _TEST_MDB_H
|
||||
#define _TEST_MDB_H
|
||||
#ifndef _TEST_H
|
||||
#define _TEST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -14,29 +14,84 @@
|
||||
#define COLOR_YELLOW "\x1b[33;1m"
|
||||
#define COLOR_RESET "\x1b[0m"
|
||||
|
||||
#define BEFORE_IDX 0
|
||||
#define AFTER_IDX 1
|
||||
#define SETUP_IDX 2
|
||||
#define TEARDOWN_IDX 3
|
||||
|
||||
#ifndef MAX_TEST_FUNCS
|
||||
#define MAX_TEST_FUNCS 256
|
||||
#endif
|
||||
|
||||
typedef int (*test_func_t)();
|
||||
typedef void (*special_func_t)();
|
||||
|
||||
int total = 0;
|
||||
int success = 0;
|
||||
static test_func_t test_functions[MAX_TEST_FUNCS];
|
||||
|
||||
// before, after, setup, teardown
|
||||
static special_func_t special_funcs[4] = {NULL};
|
||||
|
||||
#define TEST_INIT(...) \
|
||||
int main(void) { \
|
||||
printf("=========================================================================\n"); \
|
||||
printf("TEST %s\n", __FILE__); \
|
||||
__VA_ARGS__ \
|
||||
if (special_funcs[SETUP_IDX] != NULL) special_funcs[SETUP_IDX](); \
|
||||
for (int i = 0; i < total; i++) { \
|
||||
if (special_funcs[BEFORE_IDX] != NULL) special_funcs[BEFORE_IDX](); \
|
||||
if (test_functions[i]()) success++; \
|
||||
if (special_funcs[AFTER_IDX] != NULL) special_funcs[AFTER_IDX](); \
|
||||
} \
|
||||
if (special_funcs[TEARDOWN_IDX] != NULL) special_funcs[TEARDOWN_IDX](); \
|
||||
int failed = total - success; \
|
||||
printf("\n%s - (%d/%d) passed (%d) failed\n" COLOR_RESET, (failed == 0) ? COLOR_GREEN "PASSED" : COLOR_RED "FAILED", success, total, failed); \
|
||||
printf("=========================================================================\n"); \
|
||||
return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; \
|
||||
}
|
||||
|
||||
#define TEST(NAME, BLOCK) \
|
||||
// special ones :)
|
||||
|
||||
#define SETUP(...) \
|
||||
static void _test_setup(); \
|
||||
__attribute__((constructor)) static void register__test_setup() { \
|
||||
special_funcs[SETUP_IDX] = _test_setup; \
|
||||
} \
|
||||
static void _test_setup() { \
|
||||
__VA_ARGS__ \
|
||||
}
|
||||
|
||||
#define TEARDOWN(...) \
|
||||
static void _test_teardown(); \
|
||||
__attribute__((constructor)) static void register__test_teardown() { \
|
||||
special_funcs[TEARDOWN_IDX] = _test_teardown; \
|
||||
} \
|
||||
static void _test_teardown() { \
|
||||
__VA_ARGS__ \
|
||||
}
|
||||
|
||||
#define BEFORE(...) \
|
||||
static void _test_before(); \
|
||||
__attribute__((constructor)) static void register__test_before() { \
|
||||
special_funcs[BEFORE_IDX] = _test_before; \
|
||||
} \
|
||||
static void _test_before() { \
|
||||
__VA_ARGS__ \
|
||||
}
|
||||
|
||||
#define AFTER(...) \
|
||||
static void _test_after(); \
|
||||
__attribute__((constructor)) static void register__test_after() { \
|
||||
special_funcs[AFTER_IDX] = _test_after; \
|
||||
} \
|
||||
static void _test_after() { \
|
||||
__VA_ARGS__ \
|
||||
}
|
||||
|
||||
// test stuff
|
||||
|
||||
#define TEST(NAME, ...) \
|
||||
static int test_##NAME(); \
|
||||
__attribute__((constructor)) static void register_test_##NAME() { \
|
||||
if (total < MAX_TEST_FUNCS) { \
|
||||
@ -50,7 +105,7 @@ static test_func_t test_functions[MAX_TEST_FUNCS];
|
||||
printf("\ntest_" #NAME "\n"); \
|
||||
int total_assert = 0; \
|
||||
int total_success = 0; \
|
||||
BLOCK \
|
||||
__VA_ARGS__ \
|
||||
printf("\t(%d/%d) passed (%d) failed\n", total_success, total_assert, total_assert - total_success); \
|
||||
return (total_assert == total_success); \
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user