Browse Source

tests: Add helper macros for easier error handling and reporting

Tibor Nagy 6 years ago
parent
commit
64acf45c40

+ 2 - 1
tests/Makefile

@@ -159,7 +159,8 @@ CFLAGS=\
 	-g \
 	-nostdinc \
 	-nostdlib \
-	-isystem ../sysroot/include
+	-isystem ../sysroot/include \
+	-I .
 
 HEADLIBS=\
 	../sysroot/lib/crt0.o \

+ 27 - 0
tests/test_helpers.h

@@ -0,0 +1,27 @@
+#ifndef _TEST_HELPERS
+#define _TEST_HELPERS
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+// Throws an error on a well-defined error value.
+#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); \
+    }\
+}
+
+// Throws an error on an return value not defined by the standards.
+// Used for sanity checking the return values.
+#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); \
+    }\
+}
+
+#endif /* _TEST_HELPERS */

+ 4 - 8
tests/unistd/brk.c

@@ -2,14 +2,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int status = brk((void*)100);
-
-    if (status == -1) {
-        perror("brk");
-        exit(EXIT_FAILURE);
-    } else if (status != 0) {
-        printf("brk returned %d, unexpected result\n", status);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(brk, status, == -1);
+    UNEXP_IF(brk, status, != 0);
 }

+ 8 - 21
tests/unistd/chdir.c

@@ -3,38 +3,25 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char cwd[PATH_MAX] = { 0 };
     char *cwd_result = NULL;
 
     cwd_result = getcwd(cwd, PATH_MAX);
-    if (cwd_result == NULL) {
-        perror("getcwd");
-        exit(EXIT_FAILURE);
-    } else if (cwd_result != cwd) {
-        puts("getcwd returned something else than the buf argument");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(getcwd, cwd_result, == NULL);
+    UNEXP_IF(getcwd, cwd_result, != cwd);
 
     printf("getcwd before chdir: %s\n", cwd);
 
     int status = chdir("..");
-    if (status == -1) {
-        perror("chdir");
-        exit(EXIT_FAILURE);
-    } else if (status != 0) {
-        printf("chdir returned %d, unexpected result\n", status);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(chdir, status, == -1);
+    UNEXP_IF(chdir, status, != 0);
 
     cwd_result = getcwd(cwd, PATH_MAX);
-    if (cwd_result == NULL) {
-        perror("getcwd");
-        exit(EXIT_FAILURE);
-    } else if (cwd_result != cwd) {
-        puts("getcwd returned something else than the buf argument");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(getcwd, cwd_result, == NULL);
+    UNEXP_IF(getcwd, cwd_result, != cwd);
 
     printf("getcwd after chdir: %s\n", cwd);
 }

+ 3 - 4
tests/unistd/exec.c

@@ -2,12 +2,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* args[] = {"sh", "-c", "echo 'exec works :D'", NULL};
 
     int status = execv("/bin/sh", args);
-    if (status == -1) {
-        perror("execv");
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(execv, status, == -1);
 }

+ 8 - 21
tests/unistd/fchdir.c

@@ -3,31 +3,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int fd = open("..", 0, 0);
-    if (fd == -1) {
-        perror("open");
-        exit(EXIT_FAILURE);
-    } else if (fd < 0) {
-        printf("open returned %d, unexpected result\n", fd);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(open, fd, == -1);
+    UNEXP_IF(open, fd, < 0);
 
     int status = fchdir(fd);
-    if (status == -1) {
-        perror("fchdir");
-        exit(EXIT_FAILURE);
-    } else if (status != 0) {
-        printf("fchdir returned %d, unexpected result\n", status);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(fchdir, status, == -1);
+    UNEXP_IF(fchdir, status, != 0);
 
     int c = close(fd);
-    if (c == -1) {
-        perror("close");
-        exit(EXIT_FAILURE);
-    } else if (c != 0) {
-        printf("close returned %d, unexpected result\n", c);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(close, c, == -1);
+    UNEXP_IF(close, c, != 0);
 }

+ 8 - 21
tests/unistd/fsync.c

@@ -3,31 +3,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int fd = open(".", 0, 0);
-    if (fd == -1) {
-        perror("open");
-        exit(EXIT_FAILURE);
-    } else if (fd < 0) {
-        printf("open returned %d, unexpected result\n", fd);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(open, fd, == -1);
+    UNEXP_IF(open, fd, < 0);
 
     int status = fsync(fd);
-    if (status == -1) {
-        perror("fsync");
-        exit(EXIT_FAILURE);
-    } else if (status != 0) {
-        printf("fsync returned %d, unexpected result\n", status);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(fsync, status, == -1);
+    UNEXP_IF(fsync, status, != 0);
 
     int c = close(fd);
-    if (c == -1) {
-        perror("close");
-        exit(EXIT_FAILURE);
-    } else if (c != 0) {
-        printf("close returned %d, unexpected result\n", c);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(close, c, == -1);
+    UNEXP_IF(close, c, != 0);
 }

+ 8 - 21
tests/unistd/ftruncate.c

@@ -3,31 +3,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int fd = creat("ftruncate.out", 0777);
-    if (fd == -1) {
-        perror("creat");
-        exit(EXIT_FAILURE);
-    } else if (fd < 0) {
-        printf("creat returned %d, unexpected result\n", fd);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(creat, fd, == -1);
+    UNEXP_IF(creat, fd, < 0);
 
     int status = ftruncate(fd, 100);
-    if (status == -1) {
-        perror("ftruncate");
-        exit(EXIT_FAILURE);
-    } else if (status != 0) {
-        printf("ftruncate returned %d, unexpected result\n", status);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(ftruncate, status, == -1);
+    UNEXP_IF(ftruncate, status, != 0);
 
     int c = close(fd);
-    if (c == -1) {
-        perror("close");
-        exit(EXIT_FAILURE);
-    } else if (c != 0) {
-        printf("close returned %d, unexpected result\n", c);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(close, c, == -1);
+    UNEXP_IF(close, c, != 0);
 }

+ 6 - 9
tests/unistd/gethostname.c

@@ -2,17 +2,14 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char hostname[256] = { 0 };
 
     int status = gethostname(hostname, 256);
-    if (status == 0) {
-        printf("Hostname: %s\n", hostname);
-    } else if (status == -1) {
-        perror("gethostname");
-        exit(EXIT_FAILURE);
-    } else {
-        printf("gethostname returned %d, unexpected result\n", status);
-        exit(EXIT_FAILURE);
-    }
+    ERROR_IF(gethostname, status, == -1);
+    UNEXP_IF(gethostname, status, != 0);
+
+    printf("Hostname: %s\n", hostname);
 }

+ 9 - 3
tests/unistd/rmdir.c

@@ -2,8 +2,14 @@
 #include <sys/stat.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    mkdir("foo", 0);
-    int status = rmdir("foo");
-    printf("rmdir exited with status code %d\n", status);
+    int mk_status = mkdir("foo", 0);
+    ERROR_IF(mkdir, mk_status, == -1);
+    UNEXP_IF(mkdir, mk_status, != 0);
+
+    int rm_status = rmdir("foo");
+    ERROR_IF(rmdir, rm_status, == -1);
+    UNEXP_IF(rmdir, rm_status, != 0);
 }