67 lines
1.3 KiB
C
67 lines
1.3 KiB
C
#ifndef TEST_H
|
|
#define TEST_H
|
|
|
|
#include <stdio.h>
|
|
#include <setjmp.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
static int _stdout_fd_backup = -1;
|
|
static int _devnull_fd = -1;
|
|
|
|
static void mute_stdout(void) {
|
|
fflush(stdout);
|
|
_stdout_fd_backup = dup(STDOUT_FILENO);
|
|
_devnull_fd = open("/dev/null", O_WRONLY);
|
|
dup2(_devnull_fd, STDOUT_FILENO);
|
|
}
|
|
|
|
static void unmute_stdout(void) {
|
|
fflush(stdout);
|
|
if (_stdout_fd_backup != -1) {
|
|
dup2(_stdout_fd_backup, STDOUT_FILENO);
|
|
close(_stdout_fd_backup);
|
|
_stdout_fd_backup = -1;
|
|
}
|
|
if (_devnull_fd != -1) {
|
|
close(_devnull_fd);
|
|
_devnull_fd = -1;
|
|
}
|
|
}
|
|
|
|
|
|
#define GREEN "\033[1;32m"
|
|
#define RED "\033[1;31m"
|
|
#define RESET "\033[0m"
|
|
|
|
#define TEST(name) void name(void)
|
|
#define RUN_TEST(test) do { \
|
|
mute_stdout(); \
|
|
int result = test_runner(test); \
|
|
unmute_stdout(); \
|
|
if (result) \
|
|
printf(" OK\t%s\n", #test); \
|
|
else \
|
|
printf(" FAIL\t%s\n", #test); \
|
|
} while (0)
|
|
|
|
static jmp_buf env;
|
|
|
|
static int test_runner(void (*test_func)(void)) {
|
|
int failed = 0;
|
|
if (setjmp(env) == 0) {
|
|
test_func();
|
|
} else {
|
|
failed = 1;
|
|
}
|
|
return !failed;
|
|
}
|
|
|
|
#define ASSERT(expr) do { \
|
|
if (!(expr)) { \
|
|
longjmp(env, 1); \
|
|
} \
|
|
} while (0)
|
|
|
|
#endif
|