From 9a0ea6ff345e49ef4e8fcf96654a1c960d301f91 Mon Sep 17 00:00:00 2001
From: Tibor Nagy <xnagytibor@gmail.com>
Date: Thu, 21 Feb 2019 21:13:28 +0100
Subject: [PATCH] tests: More refactoring, add helper header to every test,
 override exit for better error reporting

---
 tests/alloca.c             |  2 ++
 tests/args.c               |  2 ++
 tests/arpainet.c           |  3 ++-
 tests/assert.c             |  2 ++
 tests/constructor.c        |  1 +
 tests/ctype.c              |  2 ++
 tests/destructor.c         |  1 +
 tests/dirent/main.c        |  2 ++
 tests/dirent/scandir.c     |  9 +++++----
 tests/error.c              |  2 ++
 tests/fcntl/create.c       | 15 +++++++--------
 tests/fcntl/fcntl.c        | 17 +++++++++++++++--
 tests/fnmatch.c            |  2 ++
 tests/libgen.c             |  2 ++
 tests/locale.c             |  2 ++
 tests/math.c               |  2 ++
 tests/netdb/getaddrinfo.c  |  2 ++
 tests/netdb/netdb.c        |  2 ++
 tests/pwd.c                |  2 ++
 tests/regex.c              |  2 ++
 tests/resource/getrusage.c |  9 +++++----
 tests/select.c             |  2 ++
 tests/setjmp.c             |  2 ++
 tests/signal.c             |  2 ++
 tests/stdio/all.c          |  2 ++
 tests/stdio/buffer.c       |  2 ++
 tests/stdio/fgets.c        |  2 ++
 tests/stdio/fputs.c        |  2 ++
 tests/stdio/fread.c        |  2 ++
 tests/stdio/freopen.c      |  2 ++
 tests/stdio/fseek.c        |  2 ++
 tests/stdio/fwrite.c       |  2 ++
 tests/stdio/getc_unget.c   |  2 ++
 tests/stdio/mutex.c        |  2 ++
 tests/stdio/popen.c        |  2 ++
 tests/stdio/printf.c       |  2 ++
 tests/stdio/rename.c       |  2 ++
 tests/stdio/scanf.c        |  2 ++
 tests/stdio/setvbuf.c      |  2 ++
 tests/stdio/sprintf.c      |  2 ++
 tests/stdlib/a64l.c        |  2 ++
 tests/stdlib/alloc.c       |  2 ++
 tests/stdlib/atof.c        |  2 ++
 tests/stdlib/atoi.c        |  2 ++
 tests/stdlib/bsearch.c     |  2 ++
 tests/stdlib/div.c         | 33 ++++++++++++++++++---------------
 tests/stdlib/env.c         |  2 ++
 tests/stdlib/mkostemps.c   |  2 ++
 tests/stdlib/mktemp.c      |  2 ++
 tests/stdlib/rand.c        |  2 ++
 tests/stdlib/realpath.c    |  2 ++
 tests/stdlib/strtod.c      |  2 ++
 tests/stdlib/strtol.c      |  2 ++
 tests/stdlib/strtoul.c     |  2 ++
 tests/stdlib/system.c      |  5 ++++-
 tests/string/mem.c         |  2 ++
 tests/string/strcat.c      |  2 ++
 tests/string/strchr.c      |  2 ++
 tests/string/strcpy.c      |  2 ++
 tests/string/strcspn.c     |  2 ++
 tests/string/strncmp.c     |  2 ++
 tests/string/strpbrk.c     |  2 ++
 tests/string/strrchr.c     |  2 ++
 tests/string/strspn.c      |  2 ++
 tests/string/strstr.c      |  2 ++
 tests/string/strtok.c      |  2 ++
 tests/string/strtok_r.c    |  2 ++
 tests/strings.c            |  2 ++
 tests/sys_utsname/uname.c  | 20 ++++++++++----------
 tests/test_helpers.h       | 27 ++++++++++++++++++++-------
 tests/time/asctime.c       |  2 ++
 tests/time/gettimeofday.c  |  8 +++++++-
 tests/time/gmtime.c        |  2 ++
 tests/time/localtime.c     |  2 ++
 tests/time/macros.c        |  2 ++
 tests/time/mktime.c        |  2 ++
 tests/time/strftime.c      |  2 ++
 tests/time/time.c          | 17 +++++------------
 tests/time/times.c         |  6 +++++-
 tests/unistd/access.c      |  2 ++
 tests/unistd/dup.c         | 37 ++++++++++++++++++++++++++++++++-----
 tests/unistd/getcwd.c      |  2 ++
 tests/unistd/getid.c       |  2 ++
 tests/unistd/getopt.c      |  2 ++
 tests/unistd/getopt_long.c |  2 ++
 tests/unistd/isatty.c      |  2 ++
 tests/unistd/link.c        |  2 ++
 tests/unistd/pathconf.c    |  2 ++
 tests/unistd/pipe.c        |  2 ++
 tests/unistd/setid.c       | 26 +++++++++++++++-----------
 tests/unistd/sleep.c       |  2 ++
 tests/unistd/stat.c        | 11 ++++++-----
 tests/unistd/sysconf.c     |  2 ++
 tests/unistd/write.c       |  2 ++
 tests/waitpid.c            |  2 ++
 tests/wchar/mbrtowc.c      |  2 ++
 tests/wchar/mbsrtowcs.c    |  2 ++
 tests/wchar/putwchar.c     |  2 ++
 tests/wchar/wcrtomb.c      |  2 ++
 99 files changed, 322 insertions(+), 87 deletions(-)

diff --git a/tests/alloca.c b/tests/alloca.c
index 3d774c5fc..0e4a2d490 100644
--- a/tests/alloca.c
+++ b/tests/alloca.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char *str = (char *) alloca(17);
 
diff --git a/tests/args.c b/tests/args.c
index c868d78d2..6dc49d132 100644
--- a/tests/args.c
+++ b/tests/args.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(int argc, char *argv[]) {
     for(int i = 0; i < argc; i++) {
         write(STDOUT_FILENO, argv[i], strlen(argv[i]));
diff --git a/tests/arpainet.c b/tests/arpainet.c
index cae9921b5..4b9f4a406 100644
--- a/tests/arpainet.c
+++ b/tests/arpainet.c
@@ -3,6 +3,8 @@
 #include <string.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     uint32_t hl = 0xBADFACED;
     uint32_t nl = htonl(hl);
@@ -20,5 +22,4 @@ int main(void) {
     struct in_addr* addr = malloc(sizeof addr);
     inet_aton(addr_str, addr);
     assert(strcmp(inet_ntoa(*addr), addr_str) == 0);
-
 }
diff --git a/tests/assert.c b/tests/assert.c
index 0ab46cafa..6049a38e1 100644
--- a/tests/assert.c
+++ b/tests/assert.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     assert(1 == 1);
     assert(1 + 1 == 2);
diff --git a/tests/constructor.c b/tests/constructor.c
index 38fc58cba..ddde65029 100644
--- a/tests/constructor.c
+++ b/tests/constructor.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include "test_helpers.h"
 
 __attribute__((constructor))
 void constructor_no_priority(void) {
diff --git a/tests/ctype.c b/tests/ctype.c
index 1dd70680f..de7ebb59d 100644
--- a/tests/ctype.c
+++ b/tests/ctype.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 struct test_case {
     int c;
     int isalnum;
diff --git a/tests/destructor.c b/tests/destructor.c
index 40840ccde..234cf2ed4 100644
--- a/tests/destructor.c
+++ b/tests/destructor.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include "test_helpers.h"
 
 __attribute__((destructor))
 void destructor_no_priority(void) {
diff --git a/tests/dirent/main.c b/tests/dirent/main.c
index e9f943526..0a8e265d3 100644
--- a/tests/dirent/main.c
+++ b/tests/dirent/main.c
@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%lu\n", sizeof(struct dirent));
 
diff --git a/tests/dirent/scandir.c b/tests/dirent/scandir.c
index a3102e5e1..a4d595425 100644
--- a/tests/dirent/scandir.c
+++ b/tests/dirent/scandir.c
@@ -3,17 +3,18 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int filter(const struct dirent* dirent) {
     return strstr(dirent->d_name, "3") == NULL;
 }
 
 int main(void) {
     struct dirent** array;
+
     int len = scandir("example_dir/", &array, filter, alphasort);
-    if (len < 0) {
-        perror("scandir");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(scandir, len, == -1);
+    UNEXP_IF(scandir, len, < 0);
 
     for(int i = 0; i < len; i += 1) {
         puts(array[i]->d_name);
diff --git a/tests/error.c b/tests/error.c
index 09756286f..0ae8de70d 100644
--- a/tests/error.c
+++ b/tests/error.c
@@ -3,6 +3,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     chdir("nonexistent");
     printf("errno: %d = %s\n", errno, strerror(errno));
diff --git a/tests/fcntl/create.c b/tests/fcntl/create.c
index a5b6ffe30..1d7dce6e8 100644
--- a/tests/fcntl/create.c
+++ b/tests/fcntl/create.c
@@ -2,14 +2,13 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int fd = creat("create.out", 0755);
-    if (fd >= 0) {
-        write(fd, "Hello World!\n", 13);
-        close(fd);
-        exit(EXIT_SUCCESS);
-    } else {
-        write(STDERR_FILENO, "creat failed\n", 13);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(creat, fd, == -1);
+    UNEXP_IF(creat, fd, < 0);
+
+    write(fd, "Hello World!\n", 13);
+    close(fd);
 }
diff --git a/tests/fcntl/fcntl.c b/tests/fcntl/fcntl.c
index 3ec16a84d..689502f2b 100644
--- a/tests/fcntl/fcntl.c
+++ b/tests/fcntl/fcntl.c
@@ -2,12 +2,25 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    //Lose our fd and pull it again
-    creat("fcntl.out", 0777);
+    // Lose our fd and pull it again
+    {
+        int fd = creat("fcntl.out", 0777);
+        ERROR_IF(creat, fd, == -1);
+        UNEXP_IF(creat, fd, < 0);
+    }
+
     int newfd = open("fcntl.out", 0);
+    ERROR_IF(open, newfd, == -1);
+    UNEXP_IF(open, newfd, < 0);
+
     int newfd2 = fcntl(newfd, F_DUPFD, 0);
+    // TODO: The standard doesn't define errors for F_DUPFD
+
     printf("fd %d duped into fd %d\n", newfd, newfd2);
+
     close(newfd);
     close(newfd2);
 }
diff --git a/tests/fnmatch.c b/tests/fnmatch.c
index 9fa673f53..a126cbd15 100644
--- a/tests/fnmatch.c
+++ b/tests/fnmatch.c
@@ -1,6 +1,8 @@
 #include <fnmatch.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 void test(char* pattern, char* input, int flags) {
     if (!fnmatch(pattern, input, flags)) {
         printf("\"%s\" matches \"%s\"\n", pattern, input);
diff --git a/tests/libgen.c b/tests/libgen.c
index 91b18a4f3..460e710fc 100644
--- a/tests/libgen.c
+++ b/tests/libgen.c
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 typedef struct {
   char * in;
   char * expected_out;
diff --git a/tests/locale.c b/tests/locale.c
index dde3364da..bb80cf7d0 100644
--- a/tests/locale.c
+++ b/tests/locale.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     // TODO: Implement locale properly and test it here
     char* val = setlocale(LC_ALL, NULL);
diff --git a/tests/math.c b/tests/math.c
index 9f485b1b0..8ff0a7291 100644
--- a/tests/math.c
+++ b/tests/math.c
@@ -1,6 +1,8 @@
 #include <math.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     double pi = 3.14;
     float c = cos(pi);
diff --git a/tests/netdb/getaddrinfo.c b/tests/netdb/getaddrinfo.c
index b57c1bb2c..daecf8390 100644
--- a/tests/netdb/getaddrinfo.c
+++ b/tests/netdb/getaddrinfo.c
@@ -8,6 +8,8 @@
 #include <sys/socket.h>
 #include <arpa/inet.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct addrinfo hints, *res;
     int errcode;
diff --git a/tests/netdb/netdb.c b/tests/netdb/netdb.c
index d81f08d63..a7808df22 100644
--- a/tests/netdb/netdb.c
+++ b/tests/netdb/netdb.c
@@ -35,6 +35,8 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int error_count;
 static void
 output_servent (const char *call, struct servent *sptr)
diff --git a/tests/pwd.c b/tests/pwd.c
index 59acfdef6..6e51c6c13 100644
--- a/tests/pwd.c
+++ b/tests/pwd.c
@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 void print(struct passwd *pwd) {
     printf("pw_name: %s\n", pwd->pw_name);
     printf("pw_password: %s\n", pwd->pw_passwd);
diff --git a/tests/regex.c b/tests/regex.c
index d59989e75..894781e44 100644
--- a/tests/regex.c
+++ b/tests/regex.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     regex_t regex;
     char error_buf[256];
diff --git a/tests/resource/getrusage.c b/tests/resource/getrusage.c
index e2384c8d5..4a5c51e39 100644
--- a/tests/resource/getrusage.c
+++ b/tests/resource/getrusage.c
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <sys/resource.h>
 
+#include "test_helpers.h"
+
 void ptimeval(struct timeval* val) {
     printf("{ tv_sec: %ld, tv_usec: %ld }\n", val->tv_sec, val->tv_usec);
 }
@@ -10,10 +12,9 @@ void ptimeval(struct timeval* val) {
 int main(void) {
     struct rusage r_usage;
 
-    if (getrusage(RUSAGE_SELF, &r_usage) == -1) {
-        perror("getrusage");
-        exit(EXIT_FAILURE);
-    }
+    int status = getrusage(RUSAGE_SELF, &r_usage);
+    ERROR_IF(getrusage, status, == -1);
+    UNEXP_IF(getrusage, status, != 0);
 
     printf("ru_utime:");
     ptimeval(&r_usage.ru_utime);
diff --git a/tests/select.c b/tests/select.c
index 596a589a8..a367b4ce1 100644
--- a/tests/select.c
+++ b/tests/select.c
@@ -3,6 +3,8 @@
 #include <sys/select.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int fd = open("select.c", 0, 0);
 
diff --git a/tests/setjmp.c b/tests/setjmp.c
index fe117e4de..3abc56f80 100644
--- a/tests/setjmp.c
+++ b/tests/setjmp.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <setjmp.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     jmp_buf buf;
     if (setjmp(buf)) {
diff --git a/tests/signal.c b/tests/signal.c
index 3c1ceab8d..57d1323e2 100644
--- a/tests/signal.c
+++ b/tests/signal.c
@@ -4,6 +4,8 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 void handler(int sig) {
     puts("Signal handler called!");
 }
diff --git a/tests/stdio/all.c b/tests/stdio/all.c
index d1e393e30..2ccc35eb4 100644
--- a/tests/stdio/all.c
+++ b/tests/stdio/all.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/stdio.in", "r");
 	printf("%c\n", fgetc(f));
diff --git a/tests/stdio/buffer.c b/tests/stdio/buffer.c
index 50b236f14..69efe8eba 100644
--- a/tests/stdio/buffer.c
+++ b/tests/stdio/buffer.c
@@ -1,5 +1,7 @@
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     // Tests what used to be a bug with buffering
     fwrite("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1, 999, stdout);
diff --git a/tests/stdio/fgets.c b/tests/stdio/fgets.c
index 28254debe..6e2b715bf 100644
--- a/tests/stdio/fgets.c
+++ b/tests/stdio/fgets.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     //FILE *f = fopen("/etc/ssl/certs/ca-certificates.crt", "r");
     FILE *f = fopen("stdio/stdio.in", "r");
diff --git a/tests/stdio/fputs.c b/tests/stdio/fputs.c
index 52aef7da6..7d76d12ab 100644
--- a/tests/stdio/fputs.c
+++ b/tests/stdio/fputs.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/fputs.out", "w");
 	char *in = "Hello World!";
diff --git a/tests/stdio/fread.c b/tests/stdio/fread.c
index e69b6f170..ea14368eb 100644
--- a/tests/stdio/fread.c
+++ b/tests/stdio/fread.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     FILE *fp = fopen("stdio/fread.in", "rb");
 
diff --git a/tests/stdio/freopen.c b/tests/stdio/freopen.c
index e7ec2c271..f6b4dbed9 100644
--- a/tests/stdio/freopen.c
+++ b/tests/stdio/freopen.c
@@ -1,5 +1,7 @@
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	freopen("stdio/stdio.in", "r", stdin);
 	char in[6];
diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c
index ddbcfb677..c07cf79b5 100644
--- a/tests/stdio/fseek.c
+++ b/tests/stdio/fseek.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/stdio.in", "r");
     if (fseek(f, 14, SEEK_CUR) < 0) {
diff --git a/tests/stdio/fwrite.c b/tests/stdio/fwrite.c
index c858cca17..24c7040fb 100644
--- a/tests/stdio/fwrite.c
+++ b/tests/stdio/fwrite.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/fwrite.out", "w");
 	const char ptr[] = "Hello World!";
diff --git a/tests/stdio/getc_unget.c b/tests/stdio/getc_unget.c
index fded82577..77351c40f 100644
--- a/tests/stdio/getc_unget.c
+++ b/tests/stdio/getc_unget.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	ungetc('h', stdin);
 	char c;
diff --git a/tests/stdio/mutex.c b/tests/stdio/mutex.c
index 229e3f56e..aae45caa1 100644
--- a/tests/stdio/mutex.c
+++ b/tests/stdio/mutex.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     FILE* f = fopen("stdio/stdio.in", "r");
 
diff --git a/tests/stdio/popen.c b/tests/stdio/popen.c
index b534fb9f5..967ad78ac 100644
--- a/tests/stdio/popen.c
+++ b/tests/stdio/popen.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     FILE *fp;
     int status;
diff --git a/tests/stdio/printf.c b/tests/stdio/printf.c
index ea7b8dde0..f36cc970c 100644
--- a/tests/stdio/printf.c
+++ b/tests/stdio/printf.c
@@ -1,5 +1,7 @@
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int sofar = 0;
     int len = printf(
diff --git a/tests/stdio/rename.c b/tests/stdio/rename.c
index 31cf8f252..5c0dc89de 100644
--- a/tests/stdio/rename.c
+++ b/tests/stdio/rename.c
@@ -4,6 +4,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 static char oldpath[] = "old-name.out";
 static char newpath[] = "new-name.out";
 static char str[] = "Hello, World!";
diff --git a/tests/stdio/scanf.c b/tests/stdio/scanf.c
index a19daaf3b..8978928ce 100644
--- a/tests/stdio/scanf.c
+++ b/tests/stdio/scanf.c
@@ -1,6 +1,8 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 struct params {
     short sa;
     int ia;
diff --git a/tests/stdio/setvbuf.c b/tests/stdio/setvbuf.c
index 2b0356bf1..2e79590ab 100644
--- a/tests/stdio/setvbuf.c
+++ b/tests/stdio/setvbuf.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	setvbuf(stdout, 0, _IONBF, 0);
 	FILE *f = fopen("stdio/stdio.in", "r");
diff --git a/tests/stdio/sprintf.c b/tests/stdio/sprintf.c
index 2de35e450..0c29fe291 100644
--- a/tests/stdio/sprintf.c
+++ b/tests/stdio/sprintf.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char buffer[72];
 
diff --git a/tests/stdlib/a64l.c b/tests/stdlib/a64l.c
index ddf7d8852..24937f520 100644
--- a/tests/stdlib/a64l.c
+++ b/tests/stdlib/a64l.c
@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char * s = "azAZ9."; // test boundaries
     long l = a64l(s);
diff --git a/tests/stdlib/alloc.c b/tests/stdlib/alloc.c
index b21dfadb1..29bc41ebe 100644
--- a/tests/stdlib/alloc.c
+++ b/tests/stdlib/alloc.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char * ptr = (char *)malloc(256);
     printf("malloc %p\n", ptr);
diff --git a/tests/stdlib/atof.c b/tests/stdlib/atof.c
index 19f6e25c7..ec945ef69 100644
--- a/tests/stdlib/atof.c
+++ b/tests/stdlib/atof.c
@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     double d = atof("-3.14");
     printf("%f\n", d);
diff --git a/tests/stdlib/atoi.c b/tests/stdlib/atoi.c
index 3e06f29c8..26497f27f 100644
--- a/tests/stdlib/atoi.c
+++ b/tests/stdlib/atoi.c
@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%d\n", atoi("         -42"));
     printf("%d\n", atoi(" +555"));
diff --git a/tests/stdlib/bsearch.c b/tests/stdlib/bsearch.c
index 678e8a7e8..fe55cfce7 100644
--- a/tests/stdlib/bsearch.c
+++ b/tests/stdlib/bsearch.c
@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int int_cmp(const void* a, const void* b) {
   return *(const int*) a - *(const int*) b;
 }
diff --git a/tests/stdlib/div.c b/tests/stdlib/div.c
index 394e84237..5a768b8ed 100644
--- a/tests/stdlib/div.c
+++ b/tests/stdlib/div.c
@@ -1,20 +1,23 @@
 #include <stdlib.h>
-         volatile float f;
-         volatile long double ld;
-         volatile unsigned long long ll;
-         lldiv_t mydivt;
+
+#include "test_helpers.h"
+
+volatile float f;
+volatile long double ld;
+volatile unsigned long long ll;
+lldiv_t mydivt;
 
 int main(void) {
-         char* tmp;
-         f = strtof("gnu", &tmp);
-         ld = strtold("gnu", &tmp);
-         ll = strtoll("gnu", &tmp, 10);
-         ll = strtoull("gnu", &tmp, 10);
-         ll = llabs(10);
-         mydivt = lldiv(10,1);
-         ll = mydivt.quot;
-         ll = mydivt.rem;
-         ll = atoll("10");
-         _Exit(0);
+    char* tmp;
+    f = strtof("gnu", &tmp);
+    ld = strtold("gnu", &tmp);
+    ll = strtoll("gnu", &tmp, 10);
+    ll = strtoull("gnu", &tmp, 10);
+    ll = llabs(10);
+    mydivt = lldiv(10,1);
+    ll = mydivt.quot;
+    ll = mydivt.rem;
+    ll = atoll("10");
+    _Exit(0);
 }
 
diff --git a/tests/stdlib/env.c b/tests/stdlib/env.c
index 5bdda7f3d..b5524067b 100644
--- a/tests/stdlib/env.c
+++ b/tests/stdlib/env.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     //puts(getenv("SHELL"));
     //puts(getenv("CC"));
diff --git a/tests/stdlib/mkostemps.c b/tests/stdlib/mkostemps.c
index aa544e9aa..370ffd009 100644
--- a/tests/stdlib/mkostemps.c
+++ b/tests/stdlib/mkostemps.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* file_name = (char*) calloc(18, sizeof(char));
     strcpy(file_name, "tempXXXXXX.suffix");
diff --git a/tests/stdlib/mktemp.c b/tests/stdlib/mktemp.c
index 0a23e6ed2..7d43078e4 100644
--- a/tests/stdlib/mktemp.c
+++ b/tests/stdlib/mktemp.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* string = (char*) calloc(20, sizeof(char));
     strcpy(string, "tempXXXXXX");
diff --git a/tests/stdlib/rand.c b/tests/stdlib/rand.c
index f3883c3fd..a665e2a46 100644
--- a/tests/stdlib/rand.c
+++ b/tests/stdlib/rand.c
@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%d\n", rand());
     srand(259);
diff --git a/tests/stdlib/realpath.c b/tests/stdlib/realpath.c
index c5df66f9d..a1583f864 100644
--- a/tests/stdlib/realpath.c
+++ b/tests/stdlib/realpath.c
@@ -4,6 +4,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* path = realpath("stdlib/realpath.c", NULL);
     if (!path) {
diff --git a/tests/stdlib/strtod.c b/tests/stdlib/strtod.c
index ae88af4db..d6826f676 100644
--- a/tests/stdlib/strtod.c
+++ b/tests/stdlib/strtod.c
@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* endptr = 0;
     double d;
diff --git a/tests/stdlib/strtol.c b/tests/stdlib/strtol.c
index 2977d6474..17c4155de 100644
--- a/tests/stdlib/strtol.c
+++ b/tests/stdlib/strtol.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%ld\n", strtol("         -42", NULL, 0));
     printf("%ld\n", strtol(" +555", NULL, 0));
diff --git a/tests/stdlib/strtoul.c b/tests/stdlib/strtoul.c
index afb903cb3..365f268c9 100644
--- a/tests/stdlib/strtoul.c
+++ b/tests/stdlib/strtoul.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%ld\n", strtoul("         -42", NULL, 0));
     printf("%ld\n", strtoul(" +555", NULL, 0));
diff --git a/tests/stdlib/system.c b/tests/stdlib/system.c
index 5e9d662cd..8b7d02201 100644
--- a/tests/stdlib/system.c
+++ b/tests/stdlib/system.c
@@ -1,5 +1,8 @@
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    system("echo test of system");
+    int status = system("echo test of system");
+    ERROR_IF(system, status, == -1);
 }
diff --git a/tests/string/mem.c b/tests/string/mem.c
index 305d64390..5b1b497b4 100644
--- a/tests/string/mem.c
+++ b/tests/string/mem.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     puts("# mem #");
     char arr[100];
diff --git a/tests/string/strcat.c b/tests/string/strcat.c
index 0653b7f48..cbfe52218 100644
--- a/tests/string/strcat.c
+++ b/tests/string/strcat.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char dest1[12] = "hello";
 	  printf("%s\n", strcat(dest1, " world")); // should be hello world
diff --git a/tests/string/strchr.c b/tests/string/strchr.c
index fb2e07e05..ebe973a51 100644
--- a/tests/string/strchr.c
+++ b/tests/string/strchr.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	printf("%s\n", strchr("hello", 'e')); // should be ello
 	printf("%s\n", strchr("world", 'l')); // should be ld
diff --git a/tests/string/strcpy.c b/tests/string/strcpy.c
index 69159d738..d78d26e51 100644
--- a/tests/string/strcpy.c
+++ b/tests/string/strcpy.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char dst[20];
 
diff --git a/tests/string/strcspn.c b/tests/string/strcspn.c
index 7370558c9..969d6ec66 100644
--- a/tests/string/strcspn.c
+++ b/tests/string/strcspn.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	char *world = "world";
 	printf("%ld\n", strcspn("hello", world)); // should be 2
diff --git a/tests/string/strncmp.c b/tests/string/strncmp.c
index fd4033c8e..d42b3e39a 100644
--- a/tests/string/strncmp.c
+++ b/tests/string/strncmp.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%d\n", strncmp("a", "aa", 2));
     printf("%d\n", strncmp("a", "aä", 2));
diff --git a/tests/string/strpbrk.c b/tests/string/strpbrk.c
index bb5ca56ee..88e62f746 100644
--- a/tests/string/strpbrk.c
+++ b/tests/string/strpbrk.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* source = "The quick drawn fix jumps over the lazy bug";
 
diff --git a/tests/string/strrchr.c b/tests/string/strrchr.c
index a8ae06c02..379d023d6 100644
--- a/tests/string/strrchr.c
+++ b/tests/string/strrchr.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
   char s0[] = "hello, world";
   char* ptr = strrchr(s0, 'l');
diff --git a/tests/string/strspn.c b/tests/string/strspn.c
index c198a20bf..2f5ae33e9 100644
--- a/tests/string/strspn.c
+++ b/tests/string/strspn.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	char *hello = "hello";
 	char *world = "world";
diff --git a/tests/string/strstr.c b/tests/string/strstr.c
index d36b4d915..aa097864c 100644
--- a/tests/string/strstr.c
+++ b/tests/string/strstr.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%s\n", strstr("In relibc we trust", "rust"));
     printf("%s\n", strstr("In relibc we trust", "libc"));
diff --git a/tests/string/strtok.c b/tests/string/strtok.c
index d2e066acb..4781968e8 100644
--- a/tests/string/strtok.c
+++ b/tests/string/strtok.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char source[] = "I'd just like to interject for a moment.  What you're referring to as Linux, "
                     "is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.\n";
diff --git a/tests/string/strtok_r.c b/tests/string/strtok_r.c
index 8873635c6..1c188c76b 100644
--- a/tests/string/strtok_r.c
+++ b/tests/string/strtok_r.c
@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char source[] = "I'd just like to interject for a moment.  What you're referring to as Linux, "
                     "is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.\n";
diff --git a/tests/strings.c b/tests/strings.c
index 11cdb48fe..6d3c4742c 100644
--- a/tests/strings.c
+++ b/tests/strings.c
@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <strings.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     assert(!bcmp("hello", "hehe", 2));
     assert(bcmp("hello", "haha", 2));
diff --git a/tests/sys_utsname/uname.c b/tests/sys_utsname/uname.c
index 8e094df41..836fef71f 100644
--- a/tests/sys_utsname/uname.c
+++ b/tests/sys_utsname/uname.c
@@ -1,19 +1,19 @@
 #include <stdio.h>
 #include <sys/utsname.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct utsname system_info;
 
     int result = uname(&system_info);
+    ERROR_IF(uname, result, == -1);
+    UNEXP_IF(uname, result, < 0);
 
-    if (result < 0) {
-        perror("uname");
-    } else {
-        printf("sysname: '%s'\n", system_info.sysname);
-        printf("nodename: '%s'\n", system_info.nodename);
-        printf("release: '%s'\n", system_info.release);
-        printf("version: '%s'\n", system_info.version);
-        printf("machine: '%s'\n", system_info.machine);
-        //printf("domainname: '%s'\n", system_info.domainname);
-    }
+    printf("sysname: '%s'\n", system_info.sysname);
+    printf("nodename: '%s'\n", system_info.nodename);
+    printf("release: '%s'\n", system_info.release);
+    printf("version: '%s'\n", system_info.version);
+    printf("machine: '%s'\n", system_info.machine);
+    //printf("domainname: '%s'\n", system_info.domainname);
 }
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 2905ab081..6db31e2d7 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -1,27 +1,40 @@
 #ifndef _TEST_HELPERS
 #define _TEST_HELPERS
 
+#include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
+#include <unistd.h>
 
 // Throws an error on a well-defined error value.
+// Don't pass functions as status or condition, it might evaluate them multiple times.
 #define ERROR_IF(func, status, condition) { \
     if (status condition) { \
-        fprintf(stderr, "%s:%d: ‘%s‘ returned an error in function ‘%s’: %s (%d)\n", \
-            __FILE__, __LINE__, #func, __func__, strerror(errno), errno); \
-        exit(EXIT_FAILURE); \
+        fprintf(stderr, "%s:%s:%d: '%s' returned an error: %s (%d)\n", \
+            __FILE__, __func__, __LINE__, #func, strerror(errno), errno); \
+        _exit(EXIT_FAILURE); \
     }\
 }
 
 // Throws an error on an return value not defined by the standards.
 // Used for sanity checking the return values.
+// Don't pass functions as status or condition it might evaluate them multiple times.
 #define UNEXP_IF(func, status, condition) { \
     if (status condition) { \
-        fprintf(stderr, "%s:%d: ‘%s‘ returned a value not defined by the standards in function ‘%s’: %d\n", \
-            __FILE__, __LINE__, #func, __func__, status); \
-        exit(EXIT_FAILURE); \
+        fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: %d\n", \
+            __FILE__, __func__, __LINE__, #func, status); \
+        _exit(EXIT_FAILURE); \
     }\
 }
 
+// A convenience macro to show where the test fail.
+#define exit(code) { \
+    if (code != EXIT_SUCCESS) { \
+        fprintf(stderr, "%s:%s:%d: Test failed with exit(%s)\n", \
+            __FILE__, __func__, __LINE__, #code); \
+    } \
+    _exit(code); \
+}
+
 #endif /* _TEST_HELPERS */
diff --git a/tests/time/asctime.c b/tests/time/asctime.c
index 9f7e2b549..480de464a 100644
--- a/tests/time/asctime.c
+++ b/tests/time/asctime.c
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     time_t a = 0;
     struct tm *time_info = gmtime(&a);
diff --git a/tests/time/gettimeofday.c b/tests/time/gettimeofday.c
index a17e2aedf..e43b38dc8 100644
--- a/tests/time/gettimeofday.c
+++ b/tests/time/gettimeofday.c
@@ -1,8 +1,14 @@
 #include <sys/time.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct timeval tv;
-    gettimeofday(&tv, NULL);
+
+    // gettimeofday always returns 0, no errors are defined
+    int gtod = gettimeofday(&tv, NULL);
+    UNEXP_IF(gettimeofday, gtod, != 0);
+
     printf("%ld: %ld\n", tv.tv_sec, tv.tv_usec);
 }
diff --git a/tests/time/gmtime.c b/tests/time/gmtime.c
index a0af0ab6b..a77d19fb9 100644
--- a/tests/time/gmtime.c
+++ b/tests/time/gmtime.c
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     time_t a = 0;
     struct tm expected = { .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_year = 70,
diff --git a/tests/time/localtime.c b/tests/time/localtime.c
index 5f96b3ea2..df8d2d550 100644
--- a/tests/time/localtime.c
+++ b/tests/time/localtime.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <time.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int day = 60 * 60 * 24;
     time_t inputs[] = { -(day * 33), -day, -1, -500, 0, 1, 1531454950 };
diff --git a/tests/time/macros.c b/tests/time/macros.c
index 737dab831..60e2f96e7 100644
--- a/tests/time/macros.c
+++ b/tests/time/macros.c
@@ -1,6 +1,8 @@
 #include <assert.h>
 #include <sys/time.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct timeval x = { .tv_usec = 15 };
     struct timeval y = { 0 };
diff --git a/tests/time/mktime.c b/tests/time/mktime.c
index f3010de6e..79e5e3cf6 100644
--- a/tests/time/mktime.c
+++ b/tests/time/mktime.c
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <time.h>
 
+#include "test_helpers.h"
+
 int check(time_t input) {
     struct tm* t = localtime(&input);
 
diff --git a/tests/time/strftime.c b/tests/time/strftime.c
index 99da578bf..c9299e0b2 100644
--- a/tests/time/strftime.c
+++ b/tests/time/strftime.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <time.h>
 
+#include "test_helpers.h"
+
 void print(time_t timestamp, char* fmt) {
     char* out = malloc(50);
     size_t n = strftime(out, 50, fmt, localtime(&timestamp));
diff --git a/tests/time/time.c b/tests/time/time.c
index 8ee969ed2..84b818ddb 100644
--- a/tests/time/time.c
+++ b/tests/time/time.c
@@ -2,24 +2,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct timespec tm = {0, 0};
 
     int cgt = clock_gettime(CLOCK_REALTIME, &tm);
-    if (cgt == -1) {
-        perror("clock_gettime");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(clock_gettime, cgt, == -1);
 
     time_t t = time(NULL);
-    if (t == (time_t)-1) {
-        perror("time");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(time, t, == (time_t)-1);
 
     clock_t c = clock();
-    if (c == (clock_t)-1) {
-        perror("clock");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(clock, c, == (clock_t)-1);
 }
diff --git a/tests/time/times.c b/tests/time/times.c
index 91829b4ca..018f9423e 100644
--- a/tests/time/times.c
+++ b/tests/time/times.c
@@ -2,9 +2,13 @@
 #include <sys/times.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct tms tms;
-    printf("return: %ld\n", times(&tms));
+
+    int status = times(&tms);
+    ERROR_IF(times, status, == (time_t)-1);
 
     printf("tm_utime: %ld\n", tms.tms_utime);
     printf("tm_stime: %ld\n", tms.tms_stime);
diff --git a/tests/unistd/access.c b/tests/unistd/access.c
index f7a5ecf6a..87ee7c9fb 100644
--- a/tests/unistd/access.c
+++ b/tests/unistd/access.c
@@ -2,6 +2,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     if (access("example_dir/1-never-gonna-give-you-up", R_OK | W_OK)) {
         perror("access");
diff --git a/tests/unistd/dup.c b/tests/unistd/dup.c
index 0c7ff8ecb..ca879d6ed 100644
--- a/tests/unistd/dup.c
+++ b/tests/unistd/dup.c
@@ -2,15 +2,42 @@
 #include <fcntl.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    creat("dup.out", 0777);
+    int fd0 = creat("dup.out", 0777);
+    ERROR_IF(creat, fd0, == -1);
+    UNEXP_IF(creat, fd0, < 0);
+
     int fd1 = open("dup.out", 0);
+    ERROR_IF(open, fd1, == -1);
+    UNEXP_IF(open, fd1, < 0);
+
     int fd2 = dup(fd1);
+    ERROR_IF(dup, fd2, == -1);
+    UNEXP_IF(dup, fd2, < 0);
+
     printf("fd %d duped into fd %d\n", fd1, fd2);
-    close(fd1);
-    close(fd2);
+
+    int c1 = close(fd1);
+    ERROR_IF(close, c1, == -1);
+    UNEXP_IF(close, c1, != 0);
+
+    int c2 = close(fd2);
+    ERROR_IF(close, c2, == -1);
+    UNEXP_IF(close, c2, != 0);
+
     int fd3 = open("dup.out", 0x0002, 0x1000);
-    dup2(fd3, 1);
+    ERROR_IF(open, fd3, == -1);
+    UNEXP_IF(open, fd3, < 0);
+
+    int fd4 = dup2(fd3, 1);
+    ERROR_IF(dup2, fd4, == -1);
+    UNEXP_IF(dup2, fd4, < 0);
+
     printf("hello fd %d", fd3);
-    close(fd3);
+
+    int c3 = close(fd3);
+    ERROR_IF(close, c3, == -1);
+    UNEXP_IF(close, c3, != 0);
 }
diff --git a/tests/unistd/getcwd.c b/tests/unistd/getcwd.c
index 22074dd70..1df201e4f 100644
--- a/tests/unistd/getcwd.c
+++ b/tests/unistd/getcwd.c
@@ -4,6 +4,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char first[PATH_MAX] = { 0 };
     getcwd(first, PATH_MAX);
diff --git a/tests/unistd/getid.c b/tests/unistd/getid.c
index d2a2bf579..fdd8eb285 100644
--- a/tests/unistd/getid.c
+++ b/tests/unistd/getid.c
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     gid_t egid = getegid();
     uid_t euid = geteuid();
diff --git a/tests/unistd/getopt.c b/tests/unistd/getopt.c
index b1eb26255..d6f33fe07 100644
--- a/tests/unistd/getopt.c
+++ b/tests/unistd/getopt.c
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 #define RUN(...) \
     { \
         optind = 1; \
diff --git a/tests/unistd/getopt_long.c b/tests/unistd/getopt_long.c
index 3c947a7d5..9afca067e 100644
--- a/tests/unistd/getopt_long.c
+++ b/tests/unistd/getopt_long.c
@@ -1,6 +1,8 @@
 #include <getopt.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 #define RUN(...) { \
         optind = 1; \
         optarg = NULL; \
diff --git a/tests/unistd/isatty.c b/tests/unistd/isatty.c
index 25248c5ff..5f7edfe88 100644
--- a/tests/unistd/isatty.c
+++ b/tests/unistd/isatty.c
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int status = isatty(STDOUT_FILENO);
 
diff --git a/tests/unistd/link.c b/tests/unistd/link.c
index 19cf0810e..a1a949fd4 100644
--- a/tests/unistd/link.c
+++ b/tests/unistd/link.c
@@ -4,6 +4,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%ld\n", sizeof(struct stat));
 
diff --git a/tests/unistd/pathconf.c b/tests/unistd/pathconf.c
index 5aeaee8b6..fd2e6dcd1 100644
--- a/tests/unistd/pathconf.c
+++ b/tests/unistd/pathconf.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 #define PC(N) { \
 	errno = 0; \
 	printf("%s (%d): %ld (%d)\n", #N, _PC_ ## N, fpathconf(0, _PC_ ## N), errno); \
diff --git a/tests/unistd/pipe.c b/tests/unistd/pipe.c
index ba1b1c990..88403c40a 100644
--- a/tests/unistd/pipe.c
+++ b/tests/unistd/pipe.c
@@ -4,6 +4,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int pid, pip[2];
     char instring[20];
diff --git a/tests/unistd/setid.c b/tests/unistd/setid.c
index 5a35bbb54..c5184bddd 100644
--- a/tests/unistd/setid.c
+++ b/tests/unistd/setid.c
@@ -5,20 +5,24 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    if( setpgid( getpid(), 0 ) == -1 ) {
-        perror( "setpgid" );
-    }
-    printf( "%d belongs to process group %d\n",
-         getpid(), getpgrp() );
+    int pg_status = setpgid(getpid(), 0);
+    ERROR_IF(setpgid, pg_status, == -1);
+    UNEXP_IF(setpgid, pg_status, != 0);
+
+    printf("%d belongs to process group %d\n", getpid(), getpgrp());
+
+    int reg_status = setregid(-1, -1);
+    ERROR_IF(setregid, reg_status, == -1);
+    UNEXP_IF(setregid, reg_status, != 0);
 
-    if( setregid(-1, -1) == -1 ) {
-        perror( "setregid" );
-    }
     printf("%d has egid %d and gid %d\n", getpid(), getegid(), getgid());
 
-    if( setreuid(-1, -1) == -1 ) {
-        perror( "setreuid" );
-    }
+    int reu_status = setreuid(-1, -1);
+    ERROR_IF(setreuid, reu_status, == -1);
+    UNEXP_IF(setreuid, reu_status, != 0);
+
     printf("%d has euid %d and uid %d\n", getpid(), geteuid(), getuid());
 }
diff --git a/tests/unistd/sleep.c b/tests/unistd/sleep.c
index 7aa46108c..5f4005eb1 100644
--- a/tests/unistd/sleep.c
+++ b/tests/unistd/sleep.c
@@ -2,6 +2,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     sleep(2);
     perror("sleep");
diff --git a/tests/unistd/stat.c b/tests/unistd/stat.c
index 00997ca43..be856646b 100644
--- a/tests/unistd/stat.c
+++ b/tests/unistd/stat.c
@@ -4,15 +4,16 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    printf("%ld\n", sizeof(struct stat));
+    printf("sizeof(struct stat): %ld\n", sizeof(struct stat));
 
     struct stat buf;
 
-    if (stat("unistd/stat.c", &buf)) {
-        perror("stat");
-        exit(EXIT_FAILURE);
-    }
+    int stat_status = stat("unistd/stat.c", &buf);
+    ERROR_IF(stat, stat_status, == -1);
+    UNEXP_IF(stat, stat_status, != 0);
 
     printf("st_size: %lu\n", buf.st_size);
     printf("st_blksize: %lu\n", buf.st_blksize);
diff --git a/tests/unistd/sysconf.c b/tests/unistd/sysconf.c
index be64b647c..7d9031366 100644
--- a/tests/unistd/sysconf.c
+++ b/tests/unistd/sysconf.c
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 #define SC(N) { \
 	errno = 0; \
 	printf("%s (%d): %ld (%d)\n", #N, _SC_ ## N, sysconf(_SC_ ## N), errno); \
diff --git a/tests/unistd/write.c b/tests/unistd/write.c
index 47cc4e021..5dde94c72 100644
--- a/tests/unistd/write.c
+++ b/tests/unistd/write.c
@@ -1,5 +1,7 @@
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     write(STDOUT_FILENO, "Hello World!\n", 13);
 }
diff --git a/tests/waitpid.c b/tests/waitpid.c
index 6cf019d07..e0b77829f 100644
--- a/tests/waitpid.c
+++ b/tests/waitpid.c
@@ -2,6 +2,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     pid_t pid = fork();
     if (pid == 0) {
diff --git a/tests/wchar/mbrtowc.c b/tests/wchar/mbrtowc.c
index f05f70313..0a1824429 100644
--- a/tests/wchar/mbrtowc.c
+++ b/tests/wchar/mbrtowc.c
@@ -2,6 +2,8 @@
 #include <string.h>
 #include <wchar.h>
  
+#include "test_helpers.h"
+
 int main(void) {
     mbstate_t state;
     memset(&state, 0, sizeof state);
diff --git a/tests/wchar/mbsrtowcs.c b/tests/wchar/mbsrtowcs.c
index 15dff0b7e..6db79da51 100644
--- a/tests/wchar/mbsrtowcs.c
+++ b/tests/wchar/mbsrtowcs.c
@@ -2,6 +2,8 @@
 #include <string.h>
 #include <wchar.h>
 
+#include "test_helpers.h"
+
 void print_as_wide(const char* mbstr)
 {
     mbstate_t state;
diff --git a/tests/wchar/putwchar.c b/tests/wchar/putwchar.c
index 2f39a8ea1..7b11264f7 100644
--- a/tests/wchar/putwchar.c
+++ b/tests/wchar/putwchar.c
@@ -3,6 +3,8 @@
 #include <wchar.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     wchar_t *wcs = L"zß水🍌";
 
diff --git a/tests/wchar/wcrtomb.c b/tests/wchar/wcrtomb.c
index 233496ed3..9978da5ab 100644
--- a/tests/wchar/wcrtomb.c
+++ b/tests/wchar/wcrtomb.c
@@ -3,6 +3,8 @@
 #include <wchar.h> 
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     mbstate_t state;
     memset(&state, 0, sizeof state);
-- 
GitLab