diff --git a/tests/Makefile b/tests/Makefile index 30efeddaeca20c4250866f61a7d37ab6115f8b96..15d0eca8a80869c6d56b8b4246700e0a2d99cb75 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -159,7 +159,8 @@ CFLAGS=\ -g \ -nostdinc \ -nostdlib \ - -isystem ../sysroot/include + -isystem ../sysroot/include \ + -I . HEADLIBS=\ ../sysroot/lib/crt0.o \ diff --git a/tests/test_helpers.h b/tests/test_helpers.h new file mode 100644 index 0000000000000000000000000000000000000000..2905ab0818494fabe9298351118fc4fb1f610eda --- /dev/null +++ b/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 */ diff --git a/tests/unistd/brk.c b/tests/unistd/brk.c index d480006a946c1fa0e5fe1d17d51e77ac4e17e4ff..9680861c19a413e44dba94115445e9956bd5f950 100644 --- a/tests/unistd/brk.c +++ b/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); } diff --git a/tests/unistd/chdir.c b/tests/unistd/chdir.c index eb341b1bb2e75d1cafa645af6768e6ad21772577..70e5881afb9945d76cd52eab1d17922fdc1906bc 100644 --- a/tests/unistd/chdir.c +++ b/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); } diff --git a/tests/unistd/exec.c b/tests/unistd/exec.c index cc7bfe50ef4a30e98861fa79064455dff03e2a34..00d13b3a8490392a0c2852831f6142c6ab28c781 100644 --- a/tests/unistd/exec.c +++ b/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); } diff --git a/tests/unistd/fchdir.c b/tests/unistd/fchdir.c index b04bcdf565b6693455714893a4f52d5e0523d458..cae965337627663578e8c8301d6c5b0d13b6debf 100644 --- a/tests/unistd/fchdir.c +++ b/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); } diff --git a/tests/unistd/fsync.c b/tests/unistd/fsync.c index 47b894e3ebd810ba5b58fce970b1c3decbf01b6e..554edce55f0bcedfb1061b07b0b18026a0f40141 100644 --- a/tests/unistd/fsync.c +++ b/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); } diff --git a/tests/unistd/ftruncate.c b/tests/unistd/ftruncate.c index b71f833e4c6c8272c05a002580ae22d6704234f3..63b159abb6f5a2428c6c65f8f7f2071c4173d0c9 100644 --- a/tests/unistd/ftruncate.c +++ b/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); } diff --git a/tests/unistd/gethostname.c b/tests/unistd/gethostname.c index 889108be31e10d82a407b3fcfbc3fd7e2eee4802..aa13ed6b83af8fa34f87bcf5e4855f89322288ac 100644 --- a/tests/unistd/gethostname.c +++ b/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); } diff --git a/tests/unistd/rmdir.c b/tests/unistd/rmdir.c index 212ad48dab8a7894ace0a398eed9cf94ea611632..be9e7be9197402dbb26ca02161f27d9a7f2d4acc 100644 --- a/tests/unistd/rmdir.c +++ b/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); }