diff options
Diffstat (limited to 'test/main.c')
| -rw-r--r-- | test/main.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..2854d1c --- /dev/null +++ b/test/main.c @@ -0,0 +1,73 @@ +#define _POSIX_C_SOURCE 200809L +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include "ts.h" + +#include "main_generated.c" + +int main(void) +{ + struct sigaction sa = {0}; + sa.sa_handler = SIG_IGN; + sigaction(SIGALRM, &sa, NULL); + + size_t passed = 0; + size_t failed = 0; + size_t crashed = 0; + size_t skipped = 0; + + for (size_t i = 0; i < NUM_TESTS; i++) + { + pid_t pid = fork(); + if (pid < 0) { + fprintf(stderr, " SKIP %s (fork failed: %s)\n", tests[i].name, strerror(errno)); + skipped++; + continue; + } + if (pid == 0) { + bool ok = tests[i].func(); + _exit(ok ? 0 : 1); + } + int status = 0; + alarm(30); + pid_t result = waitpid(pid, &status, 0); + alarm(0); + if (result < 0) { + if (errno == EINTR) { + kill(pid, SIGKILL); + waitpid(pid, &status, 0); + fprintf(stderr, " CRASH %s (timeout)\n", tests[i].name); + crashed++; + } else { + fprintf(stderr, " CRASH %s (waitpid failed: %s)\n", tests[i].name, strerror(errno)); + crashed++; + } + continue; + } + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { + printf(" PASS %s\n", tests[i].name); + passed++; + } else if (WIFEXITED(status)) { + fprintf(stderr, " FAIL %s\n", tests[i].name); + failed++; + } else if (WIFSIGNALED(status)) { + fprintf(stderr, " CRASH %s (signal %d)\n", tests[i].name, WTERMSIG(status)); + crashed++; + } else { + fprintf(stderr, " CRASH %s\n", tests[i].name); + crashed++; + } + } + + printf("\n--- %zu tests: %zu passed, %zu failed, %zu crashed, %zu skipped ---\n", + NUM_TESTS, passed, failed, crashed, skipped); + + return (failed > 0 || crashed > 0) ? 1 : 0; +} |