feat: added tests
This commit is contained in:
parent
a7b7cda496
commit
f3377656e4
6 changed files with 234 additions and 10 deletions
67
tests/test.h
Normal file
67
tests/test.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#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
|
||||
Loading…
Add table
Add a link
Reference in a new issue