diff --git a/tests/alloca.c b/tests/alloca.c index 3d774c5fcc..0e4a2d4902 100644 --- a/tests/alloca.c +++ b/tests/alloca.c @@ -2,6 +2,8 @@ #include #include +#include "test_helpers.h" + int main(void) { char *str = (char *) alloca(17); diff --git a/tests/args.c b/tests/args.c index c868d78d2e..6dc49d1325 100644 --- a/tests/args.c +++ b/tests/args.c @@ -1,6 +1,8 @@ #include #include +#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 cae9921b54..4b9f4a4067 100644 --- a/tests/arpainet.c +++ b/tests/arpainet.c @@ -3,6 +3,8 @@ #include #include +#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 0ab46cafa2..6049a38e17 100644 --- a/tests/assert.c +++ b/tests/assert.c @@ -2,6 +2,8 @@ #include #include +#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 38fc58cbaf..ddde65029f 100644 --- a/tests/constructor.c +++ b/tests/constructor.c @@ -1,4 +1,5 @@ #include +#include "test_helpers.h" __attribute__((constructor)) void constructor_no_priority(void) { diff --git a/tests/ctype.c b/tests/ctype.c index 1dd70680f2..de7ebb59d1 100644 --- a/tests/ctype.c +++ b/tests/ctype.c @@ -2,6 +2,8 @@ #include #include +#include "test_helpers.h" + struct test_case { int c; int isalnum; diff --git a/tests/destructor.c b/tests/destructor.c index 40840ccde6..234cf2ed42 100644 --- a/tests/destructor.c +++ b/tests/destructor.c @@ -1,4 +1,5 @@ #include +#include "test_helpers.h" __attribute__((destructor)) void destructor_no_priority(void) { diff --git a/tests/dirent/main.c b/tests/dirent/main.c index e9f943526d..0a8e265d38 100644 --- a/tests/dirent/main.c +++ b/tests/dirent/main.c @@ -3,6 +3,8 @@ #include #include +#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 a3102e5e10..a4d5954250 100644 --- a/tests/dirent/scandir.c +++ b/tests/dirent/scandir.c @@ -3,17 +3,18 @@ #include #include +#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 09756286f8..0ae8de70db 100644 --- a/tests/error.c +++ b/tests/error.c @@ -3,6 +3,8 @@ #include #include +#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 a5b6ffe30c..1d7dce6e80 100644 --- a/tests/fcntl/create.c +++ b/tests/fcntl/create.c @@ -2,14 +2,13 @@ #include #include +#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 3ec16a84d1..689502f2b5 100644 --- a/tests/fcntl/fcntl.c +++ b/tests/fcntl/fcntl.c @@ -2,12 +2,25 @@ #include #include +#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 9fa673f539..a126cbd152 100644 --- a/tests/fnmatch.c +++ b/tests/fnmatch.c @@ -1,6 +1,8 @@ #include #include +#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 91b18a4f3c..460e710fcb 100644 --- a/tests/libgen.c +++ b/tests/libgen.c @@ -3,6 +3,8 @@ #include #include +#include "test_helpers.h" + typedef struct { char * in; char * expected_out; diff --git a/tests/locale.c b/tests/locale.c index dde3364da4..bb80cf7d0f 100644 --- a/tests/locale.c +++ b/tests/locale.c @@ -2,6 +2,8 @@ #include #include +#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 9f485b1b0e..8ff0a72911 100644 --- a/tests/math.c +++ b/tests/math.c @@ -1,6 +1,8 @@ #include #include +#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 b57c1bb2c5..daecf8390d 100644 --- a/tests/netdb/getaddrinfo.c +++ b/tests/netdb/getaddrinfo.c @@ -8,6 +8,8 @@ #include #include +#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 d81f08d632..a7808df22f 100644 --- a/tests/netdb/netdb.c +++ b/tests/netdb/netdb.c @@ -35,6 +35,8 @@ #include #include +#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 59acfdef66..6e51c6c130 100644 --- a/tests/pwd.c +++ b/tests/pwd.c @@ -3,6 +3,8 @@ #include #include +#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 d59989e758..894781e447 100644 --- a/tests/regex.c +++ b/tests/regex.c @@ -2,6 +2,8 @@ #include #include +#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 e2384c8d5c..4a5c51e39e 100644 --- a/tests/resource/getrusage.c +++ b/tests/resource/getrusage.c @@ -3,6 +3,8 @@ #include #include +#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 596a589a83..a367b4ce16 100644 --- a/tests/select.c +++ b/tests/select.c @@ -3,6 +3,8 @@ #include #include +#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 fe117e4dec..3abc56f804 100644 --- a/tests/setjmp.c +++ b/tests/setjmp.c @@ -1,6 +1,8 @@ #include #include +#include "test_helpers.h" + int main(void) { jmp_buf buf; if (setjmp(buf)) { diff --git a/tests/signal.c b/tests/signal.c index 3c1ceab8d8..57d1323e2c 100644 --- a/tests/signal.c +++ b/tests/signal.c @@ -4,6 +4,8 @@ #include #include +#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 d1e393e307..2ccc35eb41 100644 --- a/tests/stdio/all.c +++ b/tests/stdio/all.c @@ -1,6 +1,8 @@ #include #include +#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 50b236f148..69efe8ebac 100644 --- a/tests/stdio/buffer.c +++ b/tests/stdio/buffer.c @@ -1,5 +1,7 @@ #include +#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 28254debef..6e2b715bf4 100644 --- a/tests/stdio/fgets.c +++ b/tests/stdio/fgets.c @@ -1,6 +1,8 @@ #include #include +#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 52aef7da68..7d76d12ab1 100644 --- a/tests/stdio/fputs.c +++ b/tests/stdio/fputs.c @@ -1,6 +1,8 @@ #include #include +#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 e69b6f1700..ea14368ebf 100644 --- a/tests/stdio/fread.c +++ b/tests/stdio/fread.c @@ -2,6 +2,8 @@ #include #include +#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 e7ec2c271d..f6b4dbed91 100644 --- a/tests/stdio/freopen.c +++ b/tests/stdio/freopen.c @@ -1,5 +1,7 @@ #include +#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 ddbcfb6778..c07cf79b58 100644 --- a/tests/stdio/fseek.c +++ b/tests/stdio/fseek.c @@ -1,6 +1,8 @@ #include #include +#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 c858cca174..24c7040fb7 100644 --- a/tests/stdio/fwrite.c +++ b/tests/stdio/fwrite.c @@ -2,6 +2,8 @@ #include #include +#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 fded82577f..77351c40f1 100644 --- a/tests/stdio/getc_unget.c +++ b/tests/stdio/getc_unget.c @@ -1,6 +1,8 @@ #include #include +#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 229e3f56ec..aae45caa12 100644 --- a/tests/stdio/mutex.c +++ b/tests/stdio/mutex.c @@ -1,6 +1,8 @@ #include #include +#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 b534fb9f5e..967ad78ace 100644 --- a/tests/stdio/popen.c +++ b/tests/stdio/popen.c @@ -1,6 +1,8 @@ #include #include +#include "test_helpers.h" + int main(void) { FILE *fp; int status; diff --git a/tests/stdio/printf.c b/tests/stdio/printf.c index ea7b8dde02..f36cc970c7 100644 --- a/tests/stdio/printf.c +++ b/tests/stdio/printf.c @@ -1,5 +1,7 @@ #include +#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 31cf8f252d..5c0dc89de6 100644 --- a/tests/stdio/rename.c +++ b/tests/stdio/rename.c @@ -4,6 +4,8 @@ #include #include +#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 a19daaf3bb..8978928cea 100644 --- a/tests/stdio/scanf.c +++ b/tests/stdio/scanf.c @@ -1,6 +1,8 @@ #include #include +#include "test_helpers.h" + struct params { short sa; int ia; diff --git a/tests/stdio/setvbuf.c b/tests/stdio/setvbuf.c index 2b0356bf15..2e79590aba 100644 --- a/tests/stdio/setvbuf.c +++ b/tests/stdio/setvbuf.c @@ -1,6 +1,8 @@ #include #include +#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 2de35e4508..0c29fe291b 100644 --- a/tests/stdio/sprintf.c +++ b/tests/stdio/sprintf.c @@ -2,6 +2,8 @@ #include #include +#include "test_helpers.h" + int main(void) { char buffer[72]; diff --git a/tests/stdlib/a64l.c b/tests/stdlib/a64l.c index ddf7d88526..24937f5206 100644 --- a/tests/stdlib/a64l.c +++ b/tests/stdlib/a64l.c @@ -1,6 +1,8 @@ #include #include +#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 b21dfadb18..29bc41ebe7 100644 --- a/tests/stdlib/alloc.c +++ b/tests/stdlib/alloc.c @@ -2,6 +2,8 @@ #include #include +#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 19f6e25c77..ec945ef697 100644 --- a/tests/stdlib/atof.c +++ b/tests/stdlib/atof.c @@ -1,6 +1,8 @@ #include #include +#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 3e06f29c89..26497f27f4 100644 --- a/tests/stdlib/atoi.c +++ b/tests/stdlib/atoi.c @@ -1,6 +1,8 @@ #include #include +#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 678e8a7e83..fe55cfce70 100644 --- a/tests/stdlib/bsearch.c +++ b/tests/stdlib/bsearch.c @@ -1,6 +1,8 @@ #include #include +#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 394e842378..5a768b8ed8 100644 --- a/tests/stdlib/div.c +++ b/tests/stdlib/div.c @@ -1,20 +1,23 @@ #include - 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 5bdda7f3d5..b5524067b1 100644 --- a/tests/stdlib/env.c +++ b/tests/stdlib/env.c @@ -2,6 +2,8 @@ #include #include +#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 aa544e9aa7..370ffd009a 100644 --- a/tests/stdlib/mkostemps.c +++ b/tests/stdlib/mkostemps.c @@ -2,6 +2,8 @@ #include #include +#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 0a23e6ed25..7d43078e4b 100644 --- a/tests/stdlib/mktemp.c +++ b/tests/stdlib/mktemp.c @@ -2,6 +2,8 @@ #include #include +#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 f3883c3fdb..a665e2a466 100644 --- a/tests/stdlib/rand.c +++ b/tests/stdlib/rand.c @@ -1,6 +1,8 @@ #include #include +#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 c5df66f9dd..a1583f864d 100644 --- a/tests/stdlib/realpath.c +++ b/tests/stdlib/realpath.c @@ -4,6 +4,8 @@ #include #include +#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 ae88af4db2..d6826f6761 100644 --- a/tests/stdlib/strtod.c +++ b/tests/stdlib/strtod.c @@ -1,6 +1,8 @@ #include #include +#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 2977d64747..17c4155de6 100644 --- a/tests/stdlib/strtol.c +++ b/tests/stdlib/strtol.c @@ -2,6 +2,8 @@ #include #include +#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 afb903cb38..365f268c91 100644 --- a/tests/stdlib/strtoul.c +++ b/tests/stdlib/strtoul.c @@ -2,6 +2,8 @@ #include #include +#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 5e9d662cd6..8b7d02201a 100644 --- a/tests/stdlib/system.c +++ b/tests/stdlib/system.c @@ -1,5 +1,8 @@ #include +#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 305d643904..5b1b497b40 100644 --- a/tests/string/mem.c +++ b/tests/string/mem.c @@ -2,6 +2,8 @@ #include #include +#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 0653b7f480..cbfe52218b 100644 --- a/tests/string/strcat.c +++ b/tests/string/strcat.c @@ -1,6 +1,8 @@ #include #include +#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 fb2e07e056..ebe973a51a 100644 --- a/tests/string/strchr.c +++ b/tests/string/strchr.c @@ -1,6 +1,8 @@ #include #include +#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 69159d738e..d78d26e51b 100644 --- a/tests/string/strcpy.c +++ b/tests/string/strcpy.c @@ -1,6 +1,8 @@ #include #include +#include "test_helpers.h" + int main(void) { char dst[20]; diff --git a/tests/string/strcspn.c b/tests/string/strcspn.c index 7370558c91..969d6ec663 100644 --- a/tests/string/strcspn.c +++ b/tests/string/strcspn.c @@ -1,6 +1,8 @@ #include #include +#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 fd4033c8e6..d42b3e39a5 100644 --- a/tests/string/strncmp.c +++ b/tests/string/strncmp.c @@ -1,6 +1,8 @@ #include #include +#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 bb5ca56ee0..88e62f7463 100644 --- a/tests/string/strpbrk.c +++ b/tests/string/strpbrk.c @@ -1,6 +1,8 @@ #include #include +#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 a8ae06c024..379d023d6a 100644 --- a/tests/string/strrchr.c +++ b/tests/string/strrchr.c @@ -2,6 +2,8 @@ #include #include +#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 c198a20bf0..2f5ae33e9d 100644 --- a/tests/string/strspn.c +++ b/tests/string/strspn.c @@ -1,6 +1,8 @@ #include #include +#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 d36b4d915a..aa097864ca 100644 --- a/tests/string/strstr.c +++ b/tests/string/strstr.c @@ -1,6 +1,8 @@ #include #include +#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 d2e066acb3..4781968e82 100644 --- a/tests/string/strtok.c +++ b/tests/string/strtok.c @@ -1,6 +1,8 @@ #include #include +#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 8873635c69..1c188c76b1 100644 --- a/tests/string/strtok_r.c +++ b/tests/string/strtok_r.c @@ -1,6 +1,8 @@ #include #include +#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 11cdb48feb..6d3c4742cb 100644 --- a/tests/strings.c +++ b/tests/strings.c @@ -3,6 +3,8 @@ #include #include +#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 8e094df416..836fef71fd 100644 --- a/tests/sys_utsname/uname.c +++ b/tests/sys_utsname/uname.c @@ -1,19 +1,19 @@ #include #include +#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 2905ab0818..6db31e2d7a 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -1,27 +1,40 @@ #ifndef _TEST_HELPERS #define _TEST_HELPERS +#include +#include #include #include -#include +#include // 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 9f7e2b5494..480de464a7 100644 --- a/tests/time/asctime.c +++ b/tests/time/asctime.c @@ -3,6 +3,8 @@ #include #include +#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 a17e2aedf5..e43b38dc86 100644 --- a/tests/time/gettimeofday.c +++ b/tests/time/gettimeofday.c @@ -1,8 +1,14 @@ #include #include +#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 a0af0ab6be..a77d19fb9a 100644 --- a/tests/time/gmtime.c +++ b/tests/time/gmtime.c @@ -3,6 +3,8 @@ #include #include +#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 5f96b3ea23..df8d2d550f 100644 --- a/tests/time/localtime.c +++ b/tests/time/localtime.c @@ -1,6 +1,8 @@ #include #include +#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 737dab831f..60e2f96e7c 100644 --- a/tests/time/macros.c +++ b/tests/time/macros.c @@ -1,6 +1,8 @@ #include #include +#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 f3010de6e7..79e5e3cf67 100644 --- a/tests/time/mktime.c +++ b/tests/time/mktime.c @@ -3,6 +3,8 @@ #include #include +#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 99da578bfd..c9299e0b21 100644 --- a/tests/time/strftime.c +++ b/tests/time/strftime.c @@ -2,6 +2,8 @@ #include #include +#include "test_helpers.h" + void print(time_t timestamp, char* fmt) { char* out = malloc(50); size_t n = strftime(out, 50, fmt, localtime(×tamp)); diff --git a/tests/time/time.c b/tests/time/time.c index 8ee969ed2f..84b818ddb6 100644 --- a/tests/time/time.c +++ b/tests/time/time.c @@ -2,24 +2,17 @@ #include #include +#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 91829b4cad..018f9423e7 100644 --- a/tests/time/times.c +++ b/tests/time/times.c @@ -2,9 +2,13 @@ #include #include +#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 f7a5ecf6ae..87ee7c9fbc 100644 --- a/tests/unistd/access.c +++ b/tests/unistd/access.c @@ -2,6 +2,8 @@ #include #include +#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 0c7ff8ecb9..ca879d6ed6 100644 --- a/tests/unistd/dup.c +++ b/tests/unistd/dup.c @@ -2,15 +2,42 @@ #include #include +#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 22074dd709..1df201e4f9 100644 --- a/tests/unistd/getcwd.c +++ b/tests/unistd/getcwd.c @@ -4,6 +4,8 @@ #include #include +#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 d2a2bf579c..fdd8eb285d 100644 --- a/tests/unistd/getid.c +++ b/tests/unistd/getid.c @@ -1,6 +1,8 @@ #include #include +#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 b1eb262556..d6f33fe078 100644 --- a/tests/unistd/getopt.c +++ b/tests/unistd/getopt.c @@ -1,6 +1,8 @@ #include #include +#include "test_helpers.h" + #define RUN(...) \ { \ optind = 1; \ diff --git a/tests/unistd/getopt_long.c b/tests/unistd/getopt_long.c index 3c947a7d54..9afca067ed 100644 --- a/tests/unistd/getopt_long.c +++ b/tests/unistd/getopt_long.c @@ -1,6 +1,8 @@ #include #include +#include "test_helpers.h" + #define RUN(...) { \ optind = 1; \ optarg = NULL; \ diff --git a/tests/unistd/isatty.c b/tests/unistd/isatty.c index 25248c5fff..5f7edfe886 100644 --- a/tests/unistd/isatty.c +++ b/tests/unistd/isatty.c @@ -3,6 +3,8 @@ #include #include +#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 19cf0810e7..a1a949fd48 100644 --- a/tests/unistd/link.c +++ b/tests/unistd/link.c @@ -4,6 +4,8 @@ #include #include +#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 5aeaee8b67..fd2e6dcd1a 100644 --- a/tests/unistd/pathconf.c +++ b/tests/unistd/pathconf.c @@ -2,6 +2,8 @@ #include #include +#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 ba1b1c9906..88403c40ad 100644 --- a/tests/unistd/pipe.c +++ b/tests/unistd/pipe.c @@ -4,6 +4,8 @@ #include #include +#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 5a35bbb54f..c5184bddd2 100644 --- a/tests/unistd/setid.c +++ b/tests/unistd/setid.c @@ -5,20 +5,24 @@ #include #include -int main(void) { - if( setpgid( getpid(), 0 ) == -1 ) { - perror( "setpgid" ); - } - printf( "%d belongs to process group %d\n", - getpid(), getpgrp() ); +#include "test_helpers.h" + +int main(void) { + 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 7aa46108c9..5f4005eb12 100644 --- a/tests/unistd/sleep.c +++ b/tests/unistd/sleep.c @@ -2,6 +2,8 @@ #include #include +#include "test_helpers.h" + int main(void) { sleep(2); perror("sleep"); diff --git a/tests/unistd/stat.c b/tests/unistd/stat.c index 00997ca43b..be856646bd 100644 --- a/tests/unistd/stat.c +++ b/tests/unistd/stat.c @@ -4,15 +4,16 @@ #include #include +#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 be64b647c6..7d9031366e 100644 --- a/tests/unistd/sysconf.c +++ b/tests/unistd/sysconf.c @@ -2,6 +2,8 @@ #include #include +#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 47cc4e0213..5dde94c727 100644 --- a/tests/unistd/write.c +++ b/tests/unistd/write.c @@ -1,5 +1,7 @@ #include +#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 6cf019d072..e0b77829f4 100644 --- a/tests/waitpid.c +++ b/tests/waitpid.c @@ -2,6 +2,8 @@ #include #include +#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 f05f70313a..0a1824429a 100644 --- a/tests/wchar/mbrtowc.c +++ b/tests/wchar/mbrtowc.c @@ -2,6 +2,8 @@ #include #include +#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 15dff0b7e8..6db79da51a 100644 --- a/tests/wchar/mbsrtowcs.c +++ b/tests/wchar/mbsrtowcs.c @@ -2,6 +2,8 @@ #include #include +#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 2f39a8ea10..7b11264f7d 100644 --- a/tests/wchar/putwchar.c +++ b/tests/wchar/putwchar.c @@ -3,6 +3,8 @@ #include #include +#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 233496ed37..9978da5ab4 100644 --- a/tests/wchar/wcrtomb.c +++ b/tests/wchar/wcrtomb.c @@ -3,6 +3,8 @@ #include #include +#include "test_helpers.h" + int main(void) { mbstate_t state; memset(&state, 0, sizeof state);