From d1a424c00218b5ec6f767f74cb8d7b517bfb5cf7 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Thu, 21 Feb 2019 11:51:58 +0100 Subject: [PATCH 01/12] tests: Replace returns with exits in the main functions This will allow us to redefine the exit function. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For example: ``` #define exit(code) { \ fprintf(stderr, "%s:%d: exit(%s) in function ā€˜%s’\n", __FILE__, __LINE__, #code, __func__); \ _exit(code); \ } ``` --- tests/dirent/main.c | 2 +- tests/dirent/scandir.c | 2 +- tests/fcntl/create.c | 4 ++-- tests/netdb/getaddrinfo.c | 2 +- tests/pwd.c | 16 ++++++++-------- tests/regex.c | 4 ++-- tests/resource/getrusage.c | 2 +- tests/signal.c | 4 ++-- tests/stdio/fgets.c | 2 +- tests/stdio/fread.c | 2 +- tests/stdio/fseek.c | 2 +- tests/stdio/fwrite.c | 6 +++--- tests/stdio/getc_unget.c | 4 ++-- tests/stdio/mutex.c | 6 +++--- tests/stdio/popen.c | 4 ++-- tests/stdio/rename.c | 4 ++-- tests/stdio/sprintf.c | 4 ++-- tests/stdlib/a64l.c | 4 ++-- tests/stdlib/bsearch.c | 2 +- tests/stdlib/env.c | 2 +- tests/stdlib/realpath.c | 4 ++-- tests/string/mem.c | 12 ++++++------ tests/string/strrchr.c | 4 ++-- tests/time/asctime.c | 2 +- tests/time/gmtime.c | 4 ++-- tests/time/mktime.c | 6 +++--- tests/time/time.c | 6 +++--- tests/unistd/access.c | 4 ++-- tests/unistd/getcwd.c | 2 +- tests/unistd/link.c | 8 ++++---- tests/unistd/pipe.c | 16 ++++++++-------- tests/unistd/stat.c | 2 +- tests/waitpid.c | 2 +- tests/wchar/putwchar.c | 2 +- 34 files changed, 76 insertions(+), 76 deletions(-) diff --git a/tests/dirent/main.c b/tests/dirent/main.c index 92a4fe2467..e9f943526d 100644 --- a/tests/dirent/main.c +++ b/tests/dirent/main.c @@ -10,7 +10,7 @@ int main(void) { if (dir == NULL) { perror("opendir"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } struct dirent* entry; diff --git a/tests/dirent/scandir.c b/tests/dirent/scandir.c index 8bbee0d13f..a3102e5e10 100644 --- a/tests/dirent/scandir.c +++ b/tests/dirent/scandir.c @@ -12,7 +12,7 @@ int main(void) { int len = scandir("example_dir/", &array, filter, alphasort); if (len < 0) { perror("scandir"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } for(int i = 0; i < len; i += 1) { diff --git a/tests/fcntl/create.c b/tests/fcntl/create.c index c881270c2f..a5b6ffe30c 100644 --- a/tests/fcntl/create.c +++ b/tests/fcntl/create.c @@ -7,9 +7,9 @@ int main(void) { if (fd >= 0) { write(fd, "Hello World!\n", 13); close(fd); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else { write(STDERR_FILENO, "creat failed\n", 13); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/netdb/getaddrinfo.c b/tests/netdb/getaddrinfo.c index 1c92a86307..b57c1bb2c5 100644 --- a/tests/netdb/getaddrinfo.c +++ b/tests/netdb/getaddrinfo.c @@ -22,7 +22,7 @@ int main(void) { errcode = getaddrinfo("www.redox-os.org", NULL, &hints, &res); if (errcode != 0) { perror("getaddrinfo"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } while (res) { diff --git a/tests/pwd.c b/tests/pwd.c index 029972c7b7..59acfdef66 100644 --- a/tests/pwd.c +++ b/tests/pwd.c @@ -19,7 +19,7 @@ int main(void) { struct passwd *pwd = getpwuid(0); if (errno != 0) { perror("getpwuid"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (pwd != NULL) { print(pwd); @@ -30,7 +30,7 @@ int main(void) { pwd = getpwnam("nobody"); if (errno != 0) { perror("getpwnam"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (pwd != NULL) { print(pwd); @@ -43,12 +43,12 @@ int main(void) { if (getpwuid_r(0, &pwd2, buf, 100, &result) < 0) { perror("getpwuid_r"); free(buf); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (result != NULL) { if (result != &pwd2) { free(buf); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } print(&pwd2); } @@ -57,12 +57,12 @@ int main(void) { if (getpwnam_r("nobody", &pwd2, buf, 100, &result) < 0) { perror("getpwuid_r"); free(buf); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (result != NULL) { if (result != &pwd2) { free(buf); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } print(&pwd2); } @@ -72,11 +72,11 @@ int main(void) { char buf2[1]; if (getpwuid_r(0, &pwd2, buf2, 1, &result) == 0) { puts("This shouldn't have succeeded, but did!"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (errno != ERANGE) { perror("getpwuid_r"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts("Returned ERANGE because the buffer was too small šŸ‘"); } diff --git a/tests/regex.c b/tests/regex.c index e920b601d3..d59989e758 100644 --- a/tests/regex.c +++ b/tests/regex.c @@ -11,7 +11,7 @@ int main(void) { regerror(error, ®ex, error_buf, 255); error_buf[255] = 0; printf("regcomp error: %d = %s\n", error, error_buf); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } regmatch_t matches[3] = {{0}}; @@ -23,7 +23,7 @@ int main(void) { if (error) { regerror(error, ®ex, error_buf, 255); printf("regexec error: %d = %s\n", error, error_buf); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } for (int group = 0; group < 3; group += 1) { diff --git a/tests/resource/getrusage.c b/tests/resource/getrusage.c index 1650dd6fc5..f924b5ae34 100644 --- a/tests/resource/getrusage.c +++ b/tests/resource/getrusage.c @@ -10,7 +10,7 @@ int main(void) { struct rusage r_usage; if (getrusage(RUSAGE_SELF, &r_usage) < 0) { perror("getrusage"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("ru_utime:"); ptimeval(&r_usage.ru_utime); diff --git a/tests/signal.c b/tests/signal.c index 1a48004507..3c1ceab8d8 100644 --- a/tests/signal.c +++ b/tests/signal.c @@ -12,14 +12,14 @@ int main(void) { if (signal(SIGUSR1, &handler) == SIG_ERR) { puts("Signal error!"); printf("%d\n", errno); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts("Raising..."); if (raise(SIGUSR1)) { puts("Raise error!"); printf("%d\n", errno); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts("Raised."); } diff --git a/tests/stdio/fgets.c b/tests/stdio/fgets.c index e48247b98f..28254debef 100644 --- a/tests/stdio/fgets.c +++ b/tests/stdio/fgets.c @@ -13,7 +13,7 @@ int main(void) { puts("EOF"); if (!feof(f)) { puts("feof() not updated!"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } break; } diff --git a/tests/stdio/fread.c b/tests/stdio/fread.c index e5e13b28cb..e69b6f1700 100644 --- a/tests/stdio/fread.c +++ b/tests/stdio/fread.c @@ -9,7 +9,7 @@ int main(void) { for (int i = 1; i <= 32; ++i) { if (fread(buf, 1, i, fp) < 0) { perror("fread"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } buf[i] = 0; diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c index 6f166928f3..ddbcfb6778 100644 --- a/tests/stdio/fseek.c +++ b/tests/stdio/fseek.c @@ -5,7 +5,7 @@ int main(void) { FILE *f = fopen("stdio/stdio.in", "r"); if (fseek(f, 14, SEEK_CUR) < 0) { puts("fseek error"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } char buffer[256]; printf("%s", fgets(buffer, 256, f)); diff --git a/tests/stdio/fwrite.c b/tests/stdio/fwrite.c index 9132420bff..c858cca174 100644 --- a/tests/stdio/fwrite.c +++ b/tests/stdio/fwrite.c @@ -7,15 +7,15 @@ int main(void) { const char ptr[] = "Hello World!"; if (fwrite(ptr, 0, 17, f)) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (fwrite(ptr, 7, 0, f)) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (fwrite(ptr, 0, 0, f)) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } fwrite(ptr, sizeof(ptr), 1, f); diff --git a/tests/stdio/getc_unget.c b/tests/stdio/getc_unget.c index 55eb6bbb3e..fded82577f 100644 --- a/tests/stdio/getc_unget.c +++ b/tests/stdio/getc_unget.c @@ -6,8 +6,8 @@ int main(void) { char c; if ((c = getchar()) == 'h') { printf("Worked!\n"); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } printf("failed :( %c\n", c); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } diff --git a/tests/stdio/mutex.c b/tests/stdio/mutex.c index d318ea3e2b..229e3f56ec 100644 --- a/tests/stdio/mutex.c +++ b/tests/stdio/mutex.c @@ -11,17 +11,17 @@ int main(void) { if (!ftrylockfile(f)) { puts("Mutex unlocked but it shouldn't be"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } funlockfile(f); if (ftrylockfile(f)) { puts("Mutex locked but it shouldn't be"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (!ftrylockfile(f)) { puts("Mutex unlocked but it shouldn't be"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } funlockfile(f); } diff --git a/tests/stdio/popen.c b/tests/stdio/popen.c index afa83fab0a..b534fb9f5e 100644 --- a/tests/stdio/popen.c +++ b/tests/stdio/popen.c @@ -10,7 +10,7 @@ int main(void) { fp = popen("ls -1 example_dir", "r"); if (fp == NULL) { perror("popen"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } while (fgets(path, 256, fp) != NULL) { @@ -21,7 +21,7 @@ int main(void) { status = pclose(fp); if (status == -1) { perror("pclose"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } else { printf("status %x\n", status); } diff --git a/tests/stdio/rename.c b/tests/stdio/rename.c index df88c5f54f..31cf8f252d 100644 --- a/tests/stdio/rename.c +++ b/tests/stdio/rename.c @@ -21,8 +21,8 @@ int main(void) { close(fd); remove(newpath); if (strcmp(str, buf) == 0) { - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/stdio/sprintf.c b/tests/stdio/sprintf.c index 829ce59989..2de35e4508 100644 --- a/tests/stdio/sprintf.c +++ b/tests/stdio/sprintf.c @@ -12,7 +12,7 @@ int main(void) { ); if (ret != 68) { printf("Failed! Return value was %d\n", ret); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } memset(buffer, 0, sizeof(buffer)); @@ -25,7 +25,7 @@ int main(void) { ); if (ret != 86) { printf("Failed! Return value was %d\n", ret); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts(buffer); diff --git a/tests/stdlib/a64l.c b/tests/stdlib/a64l.c index 7c44bb84ed..ddf7d88526 100644 --- a/tests/stdlib/a64l.c +++ b/tests/stdlib/a64l.c @@ -6,7 +6,7 @@ int main(void) { long l = a64l(s); if (l != 194301926) { printf("Invalid result: a64l(%s) = %ld\n", s, l); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("Correct a64l: %s = %ld\n", s, l); @@ -15,7 +15,7 @@ int main(void) { l = a64l(s); if (l != 53222) { printf("Invalid result: a64l(%s) = %ld\n", s, l); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("Correct a64l: %s = %ld\n", s, l); } diff --git a/tests/stdlib/bsearch.c b/tests/stdlib/bsearch.c index a56756a77a..678e8a7e83 100644 --- a/tests/stdlib/bsearch.c +++ b/tests/stdlib/bsearch.c @@ -13,7 +13,7 @@ int int_cmp(const void* a, const void* b) { size_t i = 0; \ for (; i < len; ++i) printf("%d,", arr[i]); \ printf("] expected %p but got %p\n", (void*) expect, res); \ - return EXIT_FAILURE; \ + exit(EXIT_FAILURE); \ } \ } while (0); diff --git a/tests/stdlib/env.c b/tests/stdlib/env.c index 83cd0b44fc..5bdda7f3d5 100644 --- a/tests/stdlib/env.c +++ b/tests/stdlib/env.c @@ -34,7 +34,7 @@ int main(void) { if (env) { puts("This should be null, but isn't"); puts(env); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } else { puts("Value deleted successfully!"); } diff --git a/tests/stdlib/realpath.c b/tests/stdlib/realpath.c index 5bcea42717..c5df66f9dd 100644 --- a/tests/stdlib/realpath.c +++ b/tests/stdlib/realpath.c @@ -8,7 +8,7 @@ int main(void) { char* path = realpath("stdlib/realpath.c", NULL); if (!path) { perror("realpath"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts(path); @@ -21,7 +21,7 @@ int main(void) { if (!path) { perror("realpath"); free(path); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts(path); diff --git a/tests/string/mem.c b/tests/string/mem.c index 2b717d6c75..305d643904 100644 --- a/tests/string/mem.c +++ b/tests/string/mem.c @@ -9,7 +9,7 @@ int main(void) { arr[50] = 1; if ((size_t)memchr((void *)arr, 1, 100) - (size_t)arr != 50) { puts("Incorrect memchr"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts("Correct memchr"); char arr2[51]; @@ -17,25 +17,25 @@ int main(void) { memccpy((void *)arr2, (void *)arr, 1, 100); if (arr[50] != 1) { puts("Incorrect memccpy"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts("Correct memccpy"); int res; if ((res = memcmp("hello world", "hello world", 11))) { printf("Incorrect memcmp (1), expected 0 found %d\n", res); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if ((res = memcmp("hello world", "hello worlt", 11)) >= 0) { printf("Incorrect memcmp (2), expected -, found %d\n", res); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if ((res = memcmp("hello world", "hallo world", 5)) <= 0) { printf("Incorrect memcmp (3), expected +, found %d\n", res); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if ((res = memcmp("hello world", "henlo world", 5)) >= 0) { printf("Incorrect memcmp (4), expected -, found %d\n", res); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } puts("Correct memcmp"); } diff --git a/tests/string/strrchr.c b/tests/string/strrchr.c index d32c244ff1..a8ae06c024 100644 --- a/tests/string/strrchr.c +++ b/tests/string/strrchr.c @@ -8,14 +8,14 @@ int main(void) { if (ptr != &s0[10]) { printf("%p != %p\n", ptr, &s0[10]); printf("strrchr FAIL , exit with status code %d\n", 1); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } char s1[] = ""; ptr = strrchr(s1, 'a'); if (ptr != NULL) { printf("%p != 0\n", ptr); printf("strrchr FAIL, exit with status code %d\n", 1); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("strrch PASS, exiting with status code %d\n", 0); } diff --git a/tests/time/asctime.c b/tests/time/asctime.c index f2e57da1a6..9f7e2b5494 100644 --- a/tests/time/asctime.c +++ b/tests/time/asctime.c @@ -10,6 +10,6 @@ int main(void) { char *time_string = asctime(time_info); if (time_string == NULL || strcmp(time_string, "Thu Jan 1 00:00:00 1970\n") != 0) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/time/gmtime.c b/tests/time/gmtime.c index 09bc97e5ef..a0af0ab6be 100644 --- a/tests/time/gmtime.c +++ b/tests/time/gmtime.c @@ -14,7 +14,7 @@ int main(void) { info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday || info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst || info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min || @@ -22,6 +22,6 @@ int main(void) { info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday || info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst || info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/time/mktime.c b/tests/time/mktime.c index eba83363c0..f3010de6e7 100644 --- a/tests/time/mktime.c +++ b/tests/time/mktime.c @@ -14,9 +14,9 @@ int check(time_t input) { t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec ); puts("Failed!"); - return EXIT_FAILURE; + return 1; } - return EXIT_SUCCESS; + return 0; } int main(void) { @@ -31,7 +31,7 @@ int main(void) { time_t inputs[] = { -(day * 33), -day, -500, 0, 1531454950 }; for (int i = 0; i < 5; i += 1) { if (check(inputs[i])) { - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/time/time.c b/tests/time/time.c index 24d7b28d34..8ee969ed2f 100644 --- a/tests/time/time.c +++ b/tests/time/time.c @@ -8,18 +8,18 @@ int main(void) { int cgt = clock_gettime(CLOCK_REALTIME, &tm); if (cgt == -1) { perror("clock_gettime"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } time_t t = time(NULL); if (t == (time_t)-1) { perror("time"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } clock_t c = clock(); if (c == (clock_t)-1) { perror("clock"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/unistd/access.c b/tests/unistd/access.c index 9de843b6d1..f7a5ecf6ae 100644 --- a/tests/unistd/access.c +++ b/tests/unistd/access.c @@ -5,11 +5,11 @@ int main(void) { if (access("example_dir/1-never-gonna-give-you-up", R_OK | W_OK)) { perror("access"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (!access("example_dir/1-never-gonna-give-you-up", X_OK)) { puts("Accessing a file with X_OK worked even though it... probably... shouldn't?"); puts("Please run `chmod 644 example_dir/*` and try again."); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/unistd/getcwd.c b/tests/unistd/getcwd.c index f1e088f8ca..2f9442e2a4 100644 --- a/tests/unistd/getcwd.c +++ b/tests/unistd/getcwd.c @@ -15,7 +15,7 @@ int main(void) { if (strcmp(first, second)) { puts("Not matching"); free(second); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } free(second); diff --git a/tests/unistd/link.c b/tests/unistd/link.c index 7c2af4565e..19cf0810e7 100644 --- a/tests/unistd/link.c +++ b/tests/unistd/link.c @@ -12,7 +12,7 @@ int main(void) { // Stat for the inode if (stat("unistd/link.c", &buf)) { perror("stat"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } unsigned long inode = buf.st_ino; printf("%ld\n", inode); @@ -20,7 +20,7 @@ int main(void) { // Create the link if (link("unistd/link.c", "link.out")) { perror("link"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } // Make sure inodes match @@ -38,10 +38,10 @@ int main(void) { // Remove link if (unlink("link.out")) { perror("unlink"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } if (!stat("link.out", &buf) || errno != ENOENT) { perror("stat"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } diff --git a/tests/unistd/pipe.c b/tests/unistd/pipe.c index 90dfa64641..ba1b1c9906 100644 --- a/tests/unistd/pipe.c +++ b/tests/unistd/pipe.c @@ -11,7 +11,7 @@ int main(void) { if (pipe(pip) < 0) { perror("pipe"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } pid = fork(); @@ -29,13 +29,13 @@ int main(void) { /* check result */ if (bytes < 0) { perror("pipe write"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } else if (bytes != strlen(outstring)) { fprintf(stderr, "pipe write: %d != %ld\n", bytes, strlen(outstring)); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else /* parent : receives message from child */ { @@ -54,17 +54,17 @@ int main(void) { /* check result */ if (bytes < 0) { perror("pipe read"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } else if (bytes != strlen(outstring)) { fprintf(stderr, "pipe read: %d != %ld\n", bytes, strlen(outstring)); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } else if (memcmp(instring, outstring, strlen(outstring)) != 0) { fprintf(stderr, "pipe read does not match pipe write\n"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } else { printf("%s\n", instring); } - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } } diff --git a/tests/unistd/stat.c b/tests/unistd/stat.c index 7287c0018e..00997ca43b 100644 --- a/tests/unistd/stat.c +++ b/tests/unistd/stat.c @@ -11,7 +11,7 @@ int main(void) { if (stat("unistd/stat.c", &buf)) { perror("stat"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("st_size: %lu\n", buf.st_size); diff --git a/tests/waitpid.c b/tests/waitpid.c index e25011d89a..6cf019d072 100644 --- a/tests/waitpid.c +++ b/tests/waitpid.c @@ -7,7 +7,7 @@ int main(void) { if (pid == 0) { // child sleep(1); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else { // parent int stat_loc; diff --git a/tests/wchar/putwchar.c b/tests/wchar/putwchar.c index db5e276acc..2f39a8ea10 100644 --- a/tests/wchar/putwchar.c +++ b/tests/wchar/putwchar.c @@ -12,7 +12,7 @@ int main(void) { if (0xFFFFFFFFu == putwchar(wcs[i])) { printf("Unable to putwchar() the wide character.\n"); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } } From f60c95d2cad6bc50e3bbc2849027b44c5a85d0fc Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Thu, 21 Feb 2019 16:15:49 +0100 Subject: [PATCH 02/12] tests: Work on more thorough error handling --- tests/args.c | 3 +-- tests/resource/getrusage.c | 7 ++++++- tests/unistd/brk.c | 10 ++++++++- tests/unistd/chdir.c | 43 ++++++++++++++++++++++++++++++-------- tests/unistd/exec.c | 9 ++++++-- tests/unistd/fchdir.c | 30 ++++++++++++++++++++++---- tests/unistd/fsync.c | 30 ++++++++++++++++++++++---- tests/unistd/ftruncate.c | 30 ++++++++++++++++++++++---- tests/unistd/getcwd.c | 2 +- tests/unistd/gethostname.c | 12 ++++++++--- tests/unistd/isatty.c | 19 ++++++++++++++--- 11 files changed, 161 insertions(+), 34 deletions(-) diff --git a/tests/args.c b/tests/args.c index 49e7ae0ff4..c868d78d2e 100644 --- a/tests/args.c +++ b/tests/args.c @@ -2,8 +2,7 @@ #include int main(int argc, char *argv[]) { - int i; - for(i = 0; i < argc; i++) { + for(int i = 0; i < argc; i++) { write(STDOUT_FILENO, argv[i], strlen(argv[i])); write(STDOUT_FILENO, " ", 1); } diff --git a/tests/resource/getrusage.c b/tests/resource/getrusage.c index f924b5ae34..e2384c8d5c 100644 --- a/tests/resource/getrusage.c +++ b/tests/resource/getrusage.c @@ -1,5 +1,6 @@ #include #include +#include #include void ptimeval(struct timeval* val) { @@ -8,14 +9,18 @@ void ptimeval(struct timeval* val) { int main(void) { struct rusage r_usage; - if (getrusage(RUSAGE_SELF, &r_usage) < 0) { + + if (getrusage(RUSAGE_SELF, &r_usage) == -1) { perror("getrusage"); exit(EXIT_FAILURE); } + printf("ru_utime:"); ptimeval(&r_usage.ru_utime); + printf("ru_stime:"); ptimeval(&r_usage.ru_utime); + printf("ru_maxrss: %ld\n", r_usage.ru_maxrss); printf("ru_ixrss: %ld\n", r_usage.ru_ixrss); printf("ru_idrss: %ld\n", r_usage.ru_idrss); diff --git a/tests/unistd/brk.c b/tests/unistd/brk.c index 811644e28d..d480006a94 100644 --- a/tests/unistd/brk.c +++ b/tests/unistd/brk.c @@ -1,7 +1,15 @@ #include #include +#include int main(void) { int status = brk((void*)100); - printf("brk exited with status code %d\n", status); + + if (status == -1) { + perror("brk"); + exit(EXIT_FAILURE); + } else if (status != 0) { + printf("brk returned %d, unexpected result\n", status); + exit(EXIT_FAILURE); + } } diff --git a/tests/unistd/chdir.c b/tests/unistd/chdir.c index 28178922e8..eb341b1bb2 100644 --- a/tests/unistd/chdir.c +++ b/tests/unistd/chdir.c @@ -1,15 +1,40 @@ +#include #include #include #include int main(void) { - char* cwd1 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char)); - getcwd(cwd1, 4096); - printf("initial cwd: %s\n", cwd1); - free(cwd1); - chdir(".."); - char* cwd2 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char)); - getcwd(cwd2, 4096); - printf("final cwd: %s\n", cwd2); - free(cwd2); + 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); + } + + 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); + } + + 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); + } + + printf("getcwd after chdir: %s\n", cwd); } diff --git a/tests/unistd/exec.c b/tests/unistd/exec.c index 43b10ddccb..cc7bfe50ef 100644 --- a/tests/unistd/exec.c +++ b/tests/unistd/exec.c @@ -1,8 +1,13 @@ #include #include +#include int main(void) { char* args[] = {"sh", "-c", "echo 'exec works :D'", NULL}; - execv("/bin/sh", args); - perror("execv"); + + int status = execv("/bin/sh", args); + if (status == -1) { + perror("execv"); + exit(EXIT_FAILURE); + } } diff --git a/tests/unistd/fchdir.c b/tests/unistd/fchdir.c index 9e3ab29b7e..b04bcdf565 100644 --- a/tests/unistd/fchdir.c +++ b/tests/unistd/fchdir.c @@ -1,11 +1,33 @@ #include #include #include +#include int main(void) { int fd = open("..", 0, 0); - int status; - status = fchdir(fd); - printf("fchdir exited with status code %d\n", status); - close(fd); + if (fd == -1) { + perror("open"); + exit(EXIT_FAILURE); + } else if (fd < 0) { + printf("open returned %d, unexpected result\n", fd); + exit(EXIT_FAILURE); + } + + 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); + } + + 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); + } } diff --git a/tests/unistd/fsync.c b/tests/unistd/fsync.c index 7f337f1caa..47b894e3eb 100644 --- a/tests/unistd/fsync.c +++ b/tests/unistd/fsync.c @@ -1,11 +1,33 @@ #include #include #include +#include int main(void) { int fd = open(".", 0, 0); - int status; - status = fsync(fd); - printf("fsync exited with status code %d\n", status); - close(fd); + if (fd == -1) { + perror("open"); + exit(EXIT_FAILURE); + } else if (fd < 0) { + printf("open returned %d, unexpected result\n", fd); + exit(EXIT_FAILURE); + } + + 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); + } + + 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); + } } diff --git a/tests/unistd/ftruncate.c b/tests/unistd/ftruncate.c index 7eb4f9822d..b71f833e4c 100644 --- a/tests/unistd/ftruncate.c +++ b/tests/unistd/ftruncate.c @@ -1,11 +1,33 @@ #include #include #include +#include int main(void) { int fd = creat("ftruncate.out", 0777); - int status; - status = ftruncate(fd, 100); - printf("ftruncate exited with status code %d\n", status); - close(fd); + if (fd == -1) { + perror("creat"); + exit(EXIT_FAILURE); + } else if (fd < 0) { + printf("creat returned %d, unexpected result\n", fd); + exit(EXIT_FAILURE); + } + + 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); + } + + 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); + } } diff --git a/tests/unistd/getcwd.c b/tests/unistd/getcwd.c index 2f9442e2a4..22074dd709 100644 --- a/tests/unistd/getcwd.c +++ b/tests/unistd/getcwd.c @@ -5,7 +5,7 @@ #include int main(void) { - char first[PATH_MAX]; + char first[PATH_MAX] = { 0 }; getcwd(first, PATH_MAX); puts(first); diff --git a/tests/unistd/gethostname.c b/tests/unistd/gethostname.c index 85119c32f5..889108be31 100644 --- a/tests/unistd/gethostname.c +++ b/tests/unistd/gethostname.c @@ -3,10 +3,16 @@ #include int main(void) { - char* hostname = malloc(256); - if (gethostname(hostname, 256) == 0) { + 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 { - puts("error getting hostname"); + printf("gethostname returned %d, unexpected result\n", status); + exit(EXIT_FAILURE); } } diff --git a/tests/unistd/isatty.c b/tests/unistd/isatty.c index 33855fbaf9..25248c5fff 100644 --- a/tests/unistd/isatty.c +++ b/tests/unistd/isatty.c @@ -1,11 +1,24 @@ +#include #include +#include #include int main(void) { - // 1 is stdout - if (isatty(1)) { + int status = isatty(STDOUT_FILENO); + + if (status == 1) { puts("'Tis a tty :D"); + } else if (status == 0) { + if (errno == ENOTTY) { + // I wouldn't consider stdout not being a TTY an error + // (CI runners, etc.) + puts("Whatever a tty is, it's not me"); + } else { + perror("isatty"); + exit(EXIT_FAILURE); + } } else { - puts("Whatever a tty is, it's not me"); + printf("isatty returned %d, unexpected result\n", status); + exit(EXIT_FAILURE); } } From 64acf45c40e2fd856cec5b9a723af6a2cecaac6a Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Thu, 21 Feb 2019 17:35:24 +0100 Subject: [PATCH 03/12] tests: Add helper macros for easier error handling and reporting --- tests/Makefile | 3 ++- tests/test_helpers.h | 27 +++++++++++++++++++++++++++ tests/unistd/brk.c | 12 ++++-------- tests/unistd/chdir.c | 29 ++++++++--------------------- tests/unistd/exec.c | 7 +++---- tests/unistd/fchdir.c | 29 ++++++++--------------------- tests/unistd/fsync.c | 29 ++++++++--------------------- tests/unistd/ftruncate.c | 29 ++++++++--------------------- tests/unistd/gethostname.c | 15 ++++++--------- tests/unistd/rmdir.c | 12 +++++++++--- 10 files changed, 83 insertions(+), 109 deletions(-) create mode 100644 tests/test_helpers.h diff --git a/tests/Makefile b/tests/Makefile index 30efeddaec..15d0eca8a8 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 0000000000..2905ab0818 --- /dev/null +++ b/tests/test_helpers.h @@ -0,0 +1,27 @@ +#ifndef _TEST_HELPERS +#define _TEST_HELPERS + +#include +#include +#include + +// 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 d480006a94..9680861c19 100644 --- a/tests/unistd/brk.c +++ b/tests/unistd/brk.c @@ -2,14 +2,10 @@ #include #include +#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 eb341b1bb2..70e5881afb 100644 --- a/tests/unistd/chdir.c +++ b/tests/unistd/chdir.c @@ -3,38 +3,25 @@ #include #include +#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 cc7bfe50ef..00d13b3a84 100644 --- a/tests/unistd/exec.c +++ b/tests/unistd/exec.c @@ -2,12 +2,11 @@ #include #include +#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 b04bcdf565..cae9653376 100644 --- a/tests/unistd/fchdir.c +++ b/tests/unistd/fchdir.c @@ -3,31 +3,18 @@ #include #include +#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 47b894e3eb..554edce55f 100644 --- a/tests/unistd/fsync.c +++ b/tests/unistd/fsync.c @@ -3,31 +3,18 @@ #include #include +#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 b71f833e4c..63b159abb6 100644 --- a/tests/unistd/ftruncate.c +++ b/tests/unistd/ftruncate.c @@ -3,31 +3,18 @@ #include #include +#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 889108be31..aa13ed6b83 100644 --- a/tests/unistd/gethostname.c +++ b/tests/unistd/gethostname.c @@ -2,17 +2,14 @@ #include #include +#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 212ad48dab..be9e7be919 100644 --- a/tests/unistd/rmdir.c +++ b/tests/unistd/rmdir.c @@ -2,8 +2,14 @@ #include #include +#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); } From 6266d29242cfca36e1e90b5422ff8b6d53021a42 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Thu, 21 Feb 2019 17:46:18 +0100 Subject: [PATCH 04/12] tests: Fix expected outputs --- tests/expected/unistd/brk.stdout | 1 - tests/expected/unistd/fchdir.stdout | 1 - tests/expected/unistd/fsync.stdout | 1 - tests/expected/unistd/ftruncate.stdout | 1 - tests/expected/unistd/rmdir.stdout | 1 - 5 files changed, 5 deletions(-) diff --git a/tests/expected/unistd/brk.stdout b/tests/expected/unistd/brk.stdout index c7e87198e0..e69de29bb2 100644 --- a/tests/expected/unistd/brk.stdout +++ b/tests/expected/unistd/brk.stdout @@ -1 +0,0 @@ -brk exited with status code 0 diff --git a/tests/expected/unistd/fchdir.stdout b/tests/expected/unistd/fchdir.stdout index 4c110c289b..e69de29bb2 100644 --- a/tests/expected/unistd/fchdir.stdout +++ b/tests/expected/unistd/fchdir.stdout @@ -1 +0,0 @@ -fchdir exited with status code 0 diff --git a/tests/expected/unistd/fsync.stdout b/tests/expected/unistd/fsync.stdout index 0b3deee6ec..e69de29bb2 100644 --- a/tests/expected/unistd/fsync.stdout +++ b/tests/expected/unistd/fsync.stdout @@ -1 +0,0 @@ -fsync exited with status code 0 diff --git a/tests/expected/unistd/ftruncate.stdout b/tests/expected/unistd/ftruncate.stdout index 5f0c8d7054..e69de29bb2 100644 --- a/tests/expected/unistd/ftruncate.stdout +++ b/tests/expected/unistd/ftruncate.stdout @@ -1 +0,0 @@ -ftruncate exited with status code 0 diff --git a/tests/expected/unistd/rmdir.stdout b/tests/expected/unistd/rmdir.stdout index be0153a5a5..e69de29bb2 100644 --- a/tests/expected/unistd/rmdir.stdout +++ b/tests/expected/unistd/rmdir.stdout @@ -1 +0,0 @@ -rmdir exited with status code 0 From 9a0ea6ff345e49ef4e8fcf96654a1c960d301f91 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Thu, 21 Feb 2019 21:13:28 +0100 Subject: [PATCH 05/12] 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 | 28 ++++++++++++++++------------ 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, 323 insertions(+), 88 deletions(-) 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); From 513f4ba53c39fde05d5c651ddb074024ea00b938 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Fri, 22 Feb 2019 13:19:38 +0100 Subject: [PATCH 06/12] tests: Documentation for test_helpers.h, more refactoring --- tests/signal.c | 16 +++++-------- tests/stdio/all.c | 29 +++++++++++++++++------ tests/stdio/fgets.c | 3 ++- tests/stdio/fputs.c | 16 +++++++++---- tests/stdio/fread.c | 1 + tests/stdio/fseek.c | 4 +++- tests/stdio/fwrite.c | 28 ++++++++++++---------- tests/stdio/mutex.c | 3 ++- tests/stdio/popen.c | 23 ++++-------------- tests/test_helpers.h | 56 ++++++++++++++++++++++++++++++++++++++------ tests/waitpid.c | 5 +++- 11 files changed, 121 insertions(+), 63 deletions(-) diff --git a/tests/signal.c b/tests/signal.c index 57d1323e2c..b192ad397d 100644 --- a/tests/signal.c +++ b/tests/signal.c @@ -11,17 +11,13 @@ void handler(int sig) { } int main(void) { - if (signal(SIGUSR1, &handler) == SIG_ERR) { - puts("Signal error!"); - printf("%d\n", errno); - exit(EXIT_FAILURE); - } + void (*signal_status)(int) = signal(SIGUSR1, &handler); + ERROR_IF(signal, signal_status, == SIG_ERR); puts("Raising..."); - if (raise(SIGUSR1)) { - puts("Raise error!"); - printf("%d\n", errno); - exit(EXIT_FAILURE); - } + + int raise_status = raise(SIGUSR1); + ERROR_IF(raise, raise_status, < 0); + puts("Raised."); } diff --git a/tests/stdio/all.c b/tests/stdio/all.c index 2ccc35eb41..de7c60ab63 100644 --- a/tests/stdio/all.c +++ b/tests/stdio/all.c @@ -4,11 +4,26 @@ #include "test_helpers.h" int main(void) { - FILE *f = fopen("stdio/stdio.in", "r"); - printf("%c\n", fgetc(f)); - ungetc('H', f); - char *in = malloc(30); - printf("%s\n", fgets(in, 30, f)); - setvbuf(stdout, 0, _IONBF, 0); - printf("Hello\n"); + FILE *f = fopen("stdio/stdio.in", "r"); + ERROR_IF(fopen, f, == NULL); + + int c = fgetc(f); + ERROR_IF(fgetc, c, == EOF); + UNEXP_IF(fgetc, c, < 0); + UNEXP_IF(fgetc, c, > 255); + printf("%c\n", c); + + int u = ungetc('J', f); + ERROR_IF(ungetc, u, == EOF); + + char in[30] = { 0 }; + char *s = fgets(in, 30, f); + ERROR_IF(fgets, s, == NULL); + printf("%s\n", in); + + int vb = setvbuf(stdout, 0, _IONBF, 0); + //ERROR_IF(setvbuf, vb, > 0); // TODO: Cannot use this, doesn't set errno + //UNEXP_IF(setvbuf, vb, != 0); + + printf("Hello\n"); } diff --git a/tests/stdio/fgets.c b/tests/stdio/fgets.c index 6e2b715bf4..ed02999cde 100644 --- a/tests/stdio/fgets.c +++ b/tests/stdio/fgets.c @@ -4,8 +4,9 @@ #include "test_helpers.h" int main(void) { - //FILE *f = fopen("/etc/ssl/certs/ca-certificates.crt", "r"); FILE *f = fopen("stdio/stdio.in", "r"); + ERROR_IF(fopen, f, == NULL); + char line[256]; while (1) { diff --git a/tests/stdio/fputs.c b/tests/stdio/fputs.c index 7d76d12ab1..e377bfe8db 100644 --- a/tests/stdio/fputs.c +++ b/tests/stdio/fputs.c @@ -4,8 +4,16 @@ #include "test_helpers.h" int main(void) { - FILE *f = fopen("stdio/fputs.out", "w"); - char *in = "Hello World!"; - fputs(in, f); // calls fwrite, helpers::fwritex, internal::to_write and internal::stdio_write - fclose(f); + FILE *f = fopen("stdio/fputs.out", "w"); + ERROR_IF(fopen, f, == NULL); + + char *in = "Hello World!"; + + int p = fputs(in, f); + ERROR_IF(fputs, p, == EOF); + UNEXP_IF(fputs, p, < 0); + + int c = fclose(f); + ERROR_IF(fclose, c, == EOF); + UNEXP_IF(fclose, c, != 0); } diff --git a/tests/stdio/fread.c b/tests/stdio/fread.c index ea14368ebf..75eb7ff8f6 100644 --- a/tests/stdio/fread.c +++ b/tests/stdio/fread.c @@ -6,6 +6,7 @@ int main(void) { FILE *fp = fopen("stdio/fread.in", "rb"); + ERROR_IF(fopen, fp, == NULL); char buf[33] = { 0 }; for (int i = 1; i <= 32; ++i) { diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c index c07cf79b58..8bc948799d 100644 --- a/tests/stdio/fseek.c +++ b/tests/stdio/fseek.c @@ -4,7 +4,9 @@ #include "test_helpers.h" int main(void) { - FILE *f = fopen("stdio/stdio.in", "r"); + FILE *f = fopen("stdio/stdio.in", "r"); + ERROR_IF(fopen, f, == NULL); + if (fseek(f, 14, SEEK_CUR) < 0) { puts("fseek error"); exit(EXIT_FAILURE); diff --git a/tests/stdio/fwrite.c b/tests/stdio/fwrite.c index 24c7040fb7..308c9c296a 100644 --- a/tests/stdio/fwrite.c +++ b/tests/stdio/fwrite.c @@ -5,21 +5,23 @@ #include "test_helpers.h" int main(void) { - FILE *f = fopen("stdio/fwrite.out", "w"); - const char ptr[] = "Hello World!"; + FILE *f = fopen("stdio/fwrite.out", "w"); + ERROR_IF(fopen, f, == NULL); - if (fwrite(ptr, 0, 17, f)) { - exit(EXIT_FAILURE); - } + const char ptr[] = "Hello World!"; - if (fwrite(ptr, 7, 0, f)) { - exit(EXIT_FAILURE); - } + if (fwrite(ptr, 0, 17, f)) { + exit(EXIT_FAILURE); + } - if (fwrite(ptr, 0, 0, f)) { - exit(EXIT_FAILURE); - } + if (fwrite(ptr, 7, 0, f)) { + exit(EXIT_FAILURE); + } - fwrite(ptr, sizeof(ptr), 1, f); - fclose(f); + if (fwrite(ptr, 0, 0, f)) { + exit(EXIT_FAILURE); + } + + fwrite(ptr, sizeof(ptr), 1, f); + fclose(f); } diff --git a/tests/stdio/mutex.c b/tests/stdio/mutex.c index aae45caa12..3d9be9d8db 100644 --- a/tests/stdio/mutex.c +++ b/tests/stdio/mutex.c @@ -4,7 +4,8 @@ #include "test_helpers.h" int main(void) { - FILE* f = fopen("stdio/stdio.in", "r"); + FILE *f = fopen("stdio/stdio.in", "r"); + ERROR_IF(fopen, f, == NULL); flockfile(f); diff --git a/tests/stdio/popen.c b/tests/stdio/popen.c index 967ad78ace..3d314d52a1 100644 --- a/tests/stdio/popen.c +++ b/tests/stdio/popen.c @@ -4,27 +4,14 @@ #include "test_helpers.h" int main(void) { - FILE *fp; - int status; - char path[256]; - - - fp = popen("ls -1 example_dir", "r"); - if (fp == NULL) { - perror("popen"); - exit(EXIT_FAILURE); - } + FILE *fp = popen("ls -1 example_dir", "r"); + ERROR_IF(fopen, fp, == NULL); + char path[256] = { 0 }; while (fgets(path, 256, fp) != NULL) { printf("%s", path); } - - status = pclose(fp); - if (status == -1) { - perror("pclose"); - exit(EXIT_FAILURE); - } else { - printf("status %x\n", status); - } + int status = pclose(fp); + ERROR_IF(pclose, status, == -1); } diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 6db31e2d7a..d3559a4e53 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -7,25 +7,67 @@ #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. +// Throws errors on a well-defined API error values. +// +// Only use with API functions that sets the errno variable. +// Do not pass functions as the status or condition arguments, they might be +// evaluated multiple times. +// +// Usage example: +// +// > Upon successful completion, fclose() returns 0. +// > Otherwise, it returns EOF and sets errno to indicate the error. +// +// int status = fclose(fp); +// ERROR_IF(fclose, status, == EOF); +// +// Use it only for checking the API error values. +// Do not use it for checking the correctness of the results. If you need to +// do that, print the values to the standard output and use the expected outputs +// directory. +// +// For example: +// +// int c = fgetc(f); // !!! DO NOT USE THIS WAY !!! +// ERROR_IF(fgetc, c, != 'H'); // !!! DO NOT USE THIS WAY !!! +// +// Correct usage: +// +// int c = fgetc(f); // OK +// ERROR_IF(fgetc, c, == EOF); // OK +// printf("result: %c\n", c); // OK +// #define ERROR_IF(func, status, condition) { \ if (status condition) { \ 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. +// Throws errors on API return values not defined by the standards. +// +// Do not pass functions as the status or condition arguments, they might be +// evaluated multiple times. +// +// Use it only for detecting return values that should have never been returned +// in any case by the API functions. +// +// Usage example: +// +// > The fgetc() function obtains the next byte as an unsigned char +// > converted to an int. +// +// int c = fgetc(f); +// UNEXP_IF(fgetc, c, < 0); +// UNEXP_IF(fgetc, c, > 255); +// #define UNEXP_IF(func, status, condition) { \ if (status condition) { \ 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. diff --git a/tests/waitpid.c b/tests/waitpid.c index e0b77829f4..ec4c6dedc6 100644 --- a/tests/waitpid.c +++ b/tests/waitpid.c @@ -6,6 +6,8 @@ int main(void) { pid_t pid = fork(); + ERROR_IF(fork, pid, == -1); + if (pid == 0) { // child sleep(1); @@ -13,6 +15,7 @@ int main(void) { } else { // parent int stat_loc; - waitpid(pid, &stat_loc, 0); + pid_t wid = waitpid(pid, &stat_loc, 0); + ERROR_IF(waitpid, wid, == -1); } } From 0c539d6e4ed6c224ff0401b7fedee5de4adc1061 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Fri, 22 Feb 2019 13:28:18 +0100 Subject: [PATCH 07/12] tests: Fix expected outputs --- tests/expected/stdio/all.stdout | 2 +- tests/expected/stdio/popen.stdout | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/expected/stdio/all.stdout b/tests/expected/stdio/all.stdout index ebda98d01a..92881132c2 100644 --- a/tests/expected/stdio/all.stdout +++ b/tests/expected/stdio/all.stdout @@ -1,4 +1,4 @@ H -Hello World! +Jello World! Hello diff --git a/tests/expected/stdio/popen.stdout b/tests/expected/stdio/popen.stdout index dac3c14ede..988b6f4a43 100644 --- a/tests/expected/stdio/popen.stdout +++ b/tests/expected/stdio/popen.stdout @@ -6,4 +6,3 @@ 6-never-gonna-say-goodbye 7-never-gonna-tell-a-lie 8-and-hurt-you -status 0 From 2d027f07718cb476d6fbd1755e75d1c00a3cc623 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Sun, 24 Feb 2019 00:46:26 +0100 Subject: [PATCH 08/12] tests: More work on error handling --- tests/dirent/main.c | 10 +++---- tests/expected/stdlib/rand.stdout | 2 ++ tests/expected/string/strrchr.stdout | 2 +- tests/fcntl/create.c | 8 +++-- tests/fcntl/fcntl.c | 9 ++++-- tests/stdio/freopen.c | 10 ++++--- tests/stdio/fseek.c | 13 ++++---- tests/stdio/popen.c | 2 +- tests/stdio/rename.c | 45 ++++++++++++++++++++++------ tests/stdlib/rand.c | 28 +++++++++++++++-- tests/string/strrchr.c | 32 ++++++++++---------- tests/test_helpers.h | 12 ++++++-- tests/unistd/link.c | 2 +- tests/unistd/write.c | 3 +- 14 files changed, 126 insertions(+), 52 deletions(-) diff --git a/tests/dirent/main.c b/tests/dirent/main.c index 0a8e265d38..f1c92ed506 100644 --- a/tests/dirent/main.c +++ b/tests/dirent/main.c @@ -9,11 +9,7 @@ int main(void) { printf("%lu\n", sizeof(struct dirent)); DIR* dir = opendir("example_dir/"); - - if (dir == NULL) { - perror("opendir"); - exit(EXIT_FAILURE); - } + ERROR_IF(opendir, dir, == NULL); struct dirent* entry; @@ -39,5 +35,7 @@ int main(void) { // entry = readdir(dir); // puts(entry->d_name); - closedir(dir); + int c = closedir(dir); + ERROR_IF(closedir, c, == -1); + UNEXP_IF(closedir, c, != 0); } diff --git a/tests/expected/stdlib/rand.stdout b/tests/expected/stdlib/rand.stdout index 86ea6812ed..60210f3e9d 100644 --- a/tests/expected/stdlib/rand.stdout +++ b/tests/expected/stdlib/rand.stdout @@ -1,2 +1,4 @@ 67141780 201425341 +201425341 +67141780 diff --git a/tests/expected/string/strrchr.stdout b/tests/expected/string/strrchr.stdout index fdeaa3bc75..836469707d 100644 --- a/tests/expected/string/strrchr.stdout +++ b/tests/expected/string/strrchr.stdout @@ -1 +1 @@ -strrch PASS, exiting with status code 0 +strrch PASS diff --git a/tests/fcntl/create.c b/tests/fcntl/create.c index 1d7dce6e80..c365bbcbd7 100644 --- a/tests/fcntl/create.c +++ b/tests/fcntl/create.c @@ -9,6 +9,10 @@ int main(void) { ERROR_IF(creat, fd, == -1); UNEXP_IF(creat, fd, < 0); - write(fd, "Hello World!\n", 13); - close(fd); + int written = write(fd, "Hello World!\n", 13); + ERROR_IF(write, written, == -1); + + int c = close(fd); + ERROR_IF(close, c, == -1); + UNEXP_IF(close, c, != 0); } diff --git a/tests/fcntl/fcntl.c b/tests/fcntl/fcntl.c index 689502f2b5..df8442b8e6 100644 --- a/tests/fcntl/fcntl.c +++ b/tests/fcntl/fcntl.c @@ -21,6 +21,11 @@ int main(void) { printf("fd %d duped into fd %d\n", newfd, newfd2); - close(newfd); - close(newfd2); + int c1 = close(newfd); + ERROR_IF(close, c1, == -1); + UNEXP_IF(close, c1, != 0); + + int c2 = close(newfd2); + ERROR_IF(close, c2, == -1); + UNEXP_IF(close, c2, != 0); } diff --git a/tests/stdio/freopen.c b/tests/stdio/freopen.c index f6b4dbed91..6c658435f6 100644 --- a/tests/stdio/freopen.c +++ b/tests/stdio/freopen.c @@ -3,8 +3,10 @@ #include "test_helpers.h" int main(void) { - freopen("stdio/stdio.in", "r", stdin); - char in[6]; - fgets(in, 6, stdin); - printf("%s\n", in); // should print Hello + FILE *f = freopen("stdio/stdio.in", "r", stdin); + ERROR_IF(freopen, f, == NULL); + + char in[6]; + fgets(in, 6, stdin); + printf("%s\n", in); // should print Hello } diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c index 8bc948799d..c370cc4e6d 100644 --- a/tests/stdio/fseek.c +++ b/tests/stdio/fseek.c @@ -7,11 +7,14 @@ int main(void) { FILE *f = fopen("stdio/stdio.in", "r"); ERROR_IF(fopen, f, == NULL); - if (fseek(f, 14, SEEK_CUR) < 0) { - puts("fseek error"); - exit(EXIT_FAILURE); - } + int status = fseek(f, 14, SEEK_CUR); + ERROR_IF(fseek, status, == -1); + UNEXP_IF(fseek, status, != 0); + char buffer[256]; printf("%s", fgets(buffer, 256, f)); - printf("ftell: %ld\n", ftello(f)); + + off_t pos = ftello(f); + ERROR_IF(ftello, pos, == -1); + printf("ftell: %ld\n", pos); } diff --git a/tests/stdio/popen.c b/tests/stdio/popen.c index 3d314d52a1..56a2ab1043 100644 --- a/tests/stdio/popen.c +++ b/tests/stdio/popen.c @@ -5,7 +5,7 @@ int main(void) { FILE *fp = popen("ls -1 example_dir", "r"); - ERROR_IF(fopen, fp, == NULL); + ERROR_IF(popen, fp, == NULL); char path[256] = { 0 }; while (fgets(path, 256, fp) != NULL) { diff --git a/tests/stdio/rename.c b/tests/stdio/rename.c index 5c0dc89de6..62592a5e28 100644 --- a/tests/stdio/rename.c +++ b/tests/stdio/rename.c @@ -9,19 +9,46 @@ static char oldpath[] = "old-name.out"; static char newpath[] = "new-name.out"; static char str[] = "Hello, World!"; -int str_len = 13; int main(void) { - char buf[14]; - buf[13] = 0x00; + char buf[14] = { 0 }; + + // Create old file int fd = creat(oldpath, 0777); - write(fd, str, str_len); - close(fd); - rename(oldpath, newpath); + ERROR_IF(creat, fd, == -1); + UNEXP_IF(creat, fd, < 0); + + int written_bytes = write(fd, str, strlen(str)); + ERROR_IF(write, written_bytes, == -1); + + int c1 = close(fd); + ERROR_IF(close, c1, == -1); + UNEXP_IF(close, c1, != 0); + + // Rename old file to new file + int rn_status = rename(oldpath, newpath); + ERROR_IF(rename, rn_status, == -1); + UNEXP_IF(rename, rn_status, != 0); + + // Read new file fd = open(newpath, O_RDONLY); - read(fd, buf, str_len); - close(fd); - remove(newpath); + ERROR_IF(open, fd, == -1); + UNEXP_IF(open, fd, < 0); + + int read_bytes = read(fd, buf, strlen(str)); + ERROR_IF(read, read_bytes, == -1); + UNEXP_IF(read, read_bytes, < 0); + + int c2 = close(fd); + ERROR_IF(close, c2, == -1); + UNEXP_IF(close, c2, != 0); + + // Remove new file + int rm_status = remove(newpath); + ERROR_IF(remove, rm_status, == -1); + UNEXP_IF(remove, rm_status, != 0); + + // Compare file contents if (strcmp(str, buf) == 0) { exit(EXIT_SUCCESS); } else { diff --git a/tests/stdlib/rand.c b/tests/stdlib/rand.c index a665e2a466..52908fae70 100644 --- a/tests/stdlib/rand.c +++ b/tests/stdlib/rand.c @@ -4,7 +4,31 @@ #include "test_helpers.h" int main(void) { - printf("%d\n", rand()); + // Uninitialized generator + int rand_uninit = rand(); + printf("%d\n", rand_uninit); + + // Testing the reproducibility of values srand(259); - printf("%d\n", rand()); + int rand_seed259_1 = rand(); + printf("%d\n", rand_seed259_1); + + srand(259); + int rand_seed259_2 = rand(); + printf("%d\n", rand_seed259_2); + + if (rand_seed259_1 != rand_seed259_2) { + puts("rand() doesn't return reproducible values"); + exit(EXIT_FAILURE); + } + + // Seed value 1 should return the same values as the ininitialized generator + srand(1); + int rand_seed1 = rand(); + printf("%d\n", rand_seed1); + + if (rand_uninit != rand_seed1) { + puts("srand(1) doesn't work"); + exit(EXIT_FAILURE); + } } diff --git a/tests/string/strrchr.c b/tests/string/strrchr.c index 379d023d6a..1013e76100 100644 --- a/tests/string/strrchr.c +++ b/tests/string/strrchr.c @@ -5,19 +5,21 @@ #include "test_helpers.h" int main(void) { - char s0[] = "hello, world"; - char* ptr = strrchr(s0, 'l'); - if (ptr != &s0[10]) { - printf("%p != %p\n", ptr, &s0[10]); - printf("strrchr FAIL , exit with status code %d\n", 1); - exit(EXIT_FAILURE); - } - char s1[] = ""; - ptr = strrchr(s1, 'a'); - if (ptr != NULL) { - printf("%p != 0\n", ptr); - printf("strrchr FAIL, exit with status code %d\n", 1); - exit(EXIT_FAILURE); - } - printf("strrch PASS, exiting with status code %d\n", 0); + char s0[] = "hello, world"; + char *ptr = strrchr(s0, 'l'); + if (ptr != &s0[10]) { + printf("%p != %p\n", ptr, &s0[10]); + puts("strrchr FAIL"); + exit(EXIT_FAILURE); + } + + char s1[] = ""; + ptr = strrchr(s1, 'a'); + if (ptr != NULL) { + printf("%p != 0\n", ptr); + puts("strrchr FAIL"); + exit(EXIT_FAILURE); + } + + puts("strrch PASS"); } diff --git a/tests/test_helpers.h b/tests/test_helpers.h index d3559a4e53..0007a552c2 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -39,7 +39,7 @@ // #define ERROR_IF(func, status, condition) { \ if (status condition) { \ - fprintf(stderr, "%s:%s:%d: '%s' returned an error: %s (%d)\n", \ + fprintf(stderr, "%s:%s:%d: '%s' failed: %s (%d)\n", \ __FILE__, __func__, __LINE__, #func, strerror(errno), errno); \ _exit(EXIT_FAILURE); \ } \ @@ -64,8 +64,14 @@ // #define UNEXP_IF(func, status, condition) { \ if (status condition) { \ - fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: %d\n", \ - __FILE__, __func__, __LINE__, #func, status); \ + fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: ", \ + __FILE__, __func__, __LINE__, #func); \ + fprintf(stderr, _Generic((status), \ + char *: "char*(%p) = \"%1$s\"", \ + void *: "void*(%p)", \ + default: "%i" \ + ), status); \ + fprintf(stderr, "\n"); \ _exit(EXIT_FAILURE); \ } \ } diff --git a/tests/unistd/link.c b/tests/unistd/link.c index a1a949fd48..5c09c12606 100644 --- a/tests/unistd/link.c +++ b/tests/unistd/link.c @@ -7,7 +7,7 @@ #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; diff --git a/tests/unistd/write.c b/tests/unistd/write.c index 5dde94c727..6810af3855 100644 --- a/tests/unistd/write.c +++ b/tests/unistd/write.c @@ -3,5 +3,6 @@ #include "test_helpers.h" int main(void) { - write(STDOUT_FILENO, "Hello World!\n", 13); + int written = write(STDOUT_FILENO, "Hello World!\n", 13); + ERROR_IF(write, written, == -1); } From a92be000fb06dc40ffd58c889e150c54682af6ed Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Sun, 24 Feb 2019 22:02:11 +0100 Subject: [PATCH 09/12] tests: Even more work on error handling realpath: Fixing undefined behaviour in the second test. If the call fails the resolved_name argument cannot be used for error checking because its state is undefined by SUSv2. pipe: Changing the order of close and write error handling code. Errors in close could overwrite errno after write errors, returning incorrect error messages. gmtime: Removed duplicate checks Other fixes for fseek, rename, mktime, putwchar --- tests/stdio/fseek.c | 2 +- tests/stdio/rename.c | 5 ++- tests/stdlib/realpath.c | 29 +++++----------- tests/time/gmtime.c | 8 ----- tests/time/mktime.c | 17 ++++++--- tests/unistd/pipe.c | 76 ++++++++++++++++++++++------------------- tests/wchar/putwchar.c | 11 ++---- 7 files changed, 68 insertions(+), 80 deletions(-) diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c index c370cc4e6d..9b8bc1b046 100644 --- a/tests/stdio/fseek.c +++ b/tests/stdio/fseek.c @@ -16,5 +16,5 @@ int main(void) { off_t pos = ftello(f); ERROR_IF(ftello, pos, == -1); - printf("ftell: %ld\n", pos); + printf("ftello: %ld\n", pos); } diff --git a/tests/stdio/rename.c b/tests/stdio/rename.c index 62592a5e28..840916c901 100644 --- a/tests/stdio/rename.c +++ b/tests/stdio/rename.c @@ -49,9 +49,8 @@ int main(void) { UNEXP_IF(remove, rm_status, != 0); // Compare file contents - if (strcmp(str, buf) == 0) { - exit(EXIT_SUCCESS); - } else { + if (strcmp(str, buf) != 0) { + puts("Comparison failed!"); exit(EXIT_FAILURE); } } diff --git a/tests/stdlib/realpath.c b/tests/stdlib/realpath.c index a1583f864d..d6406dd52e 100644 --- a/tests/stdlib/realpath.c +++ b/tests/stdlib/realpath.c @@ -7,25 +7,14 @@ #include "test_helpers.h" int main(void) { - char* path = realpath("stdlib/realpath.c", NULL); - if (!path) { - perror("realpath"); - exit(EXIT_FAILURE); - } - puts(path); + char *path_res = realpath("stdlib/realpath.c", NULL); + ERROR_IF(realpath, path_res, == NULL); + puts(path_res); + free(path_res); - free(path); - - path = malloc(PATH_MAX); - memset(path, 0, PATH_MAX); - - realpath("stdlib/realpath.c", path); - if (!path) { - perror("realpath"); - free(path); - exit(EXIT_FAILURE); - } - puts(path); - - free(path); + char path_arg[PATH_MAX] = { 0 }; + char *res = realpath("stdlib/realpath.c", path_arg); + ERROR_IF(realpath, res, == NULL); + puts(path_arg); + free(path_arg); } diff --git a/tests/time/gmtime.c b/tests/time/gmtime.c index a77d19fb9a..f5a16f2af3 100644 --- a/tests/time/gmtime.c +++ b/tests/time/gmtime.c @@ -18,12 +18,4 @@ int main(void) { info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { exit(EXIT_FAILURE); } - - if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min || - info->tm_hour != expected.tm_hour || info->tm_mday != expected.tm_mday || - info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday || - info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst || - info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { - exit(EXIT_FAILURE); - } } diff --git a/tests/time/mktime.c b/tests/time/mktime.c index 79e5e3cf67..bc8c9c755d 100644 --- a/tests/time/mktime.c +++ b/tests/time/mktime.c @@ -6,11 +6,15 @@ #include "test_helpers.h" int check(time_t input) { - struct tm* t = localtime(&input); + struct tm *t = localtime(&input); + ERROR_IF(localtime, t, == NULL); - printf("%ld = %ld\n", input, mktime(t)); + time_t output = mktime(t); + ERROR_IF(mktime, output, == (time_t)-1); - if (input != mktime(t)) { + printf("%ld = %ld\n", input, output); + + if (input != output) { printf( "Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n", t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec @@ -41,8 +45,13 @@ int main(void) { for (int i = 0; i < 10; i += 1) { time_t input = (time_t) rand(); - struct tm* time = localtime(&input); + + struct tm *time = localtime(&input); + ERROR_IF(localtime, time, == NULL); + time_t output = mktime(time); + ERROR_IF(mktime, output, == (time_t)-1); + if (input != output) { // asctime has newline printf("Comparison %ld == %ld failed. Time: %s", input, output, asctime(time)); diff --git a/tests/unistd/pipe.c b/tests/unistd/pipe.c index 88403c40ad..85e6db5abf 100644 --- a/tests/unistd/pipe.c +++ b/tests/unistd/pipe.c @@ -7,57 +7,56 @@ #include "test_helpers.h" int main(void) { - int pid, pip[2]; + int pip[2]; char instring[20]; - char * outstring = "Hello World!"; + char *outstring = "Hello World!"; - if (pipe(pip) < 0) { - perror("pipe"); - exit(EXIT_FAILURE); - } + int pipe_status = pipe(pip); + ERROR_IF(pipe, pipe_status, == -1); + UNEXP_IF(pipe, pipe_status, != 0); - pid = fork(); - if (pid == 0) /* child : sends message to parent*/ - { - /* close read end */ - close(pip[0]); + int pid = fork(); + ERROR_IF(fork, pid, == -1); - /* send 7 characters in the string, including end-of-string */ + if (pid == 0) { + // child: sends message to parent + // close read end + int cr = close(pip[0]); + ERROR_IF(close, cr, == -1); + UNEXP_IF(close, cr, != 0); + + // send 7 characters in the string, including end-of-string int bytes = write(pip[1], outstring, strlen(outstring)); + ERROR_IF(write, bytes, == -1); - /* close write end */ - close(pip[1]); - - /* check result */ - if (bytes < 0) { - perror("pipe write"); - exit(EXIT_FAILURE); - } else if (bytes != strlen(outstring)) { + // check result + if (bytes != strlen(outstring)) { fprintf(stderr, "pipe write: %d != %ld\n", bytes, strlen(outstring)); exit(EXIT_FAILURE); } - exit(EXIT_SUCCESS); - } - else /* parent : receives message from child */ - { - /* close write end */ - close(pip[1]); + // close write end + int cw = close(pip[1]); + ERROR_IF(close, cw, == -1); + UNEXP_IF(close, cw, != 0); - /* clear memory */ + exit(EXIT_SUCCESS); + } else { + // parent: receives message from child + // close write end + int cw = close(pip[1]); + ERROR_IF(close, cw, == -1); + UNEXP_IF(close, cw, != 0); + + // clear memory memset(instring, 0, sizeof(instring)); - /* read from the pipe */ + // read from the pipe int bytes = read(pip[0], instring, sizeof(instring) - 1); + ERROR_IF(read, bytes, == -1); - /* close read end */ - close(pip[0]); - - /* check result */ - if (bytes < 0) { - perror("pipe read"); - exit(EXIT_FAILURE); - } else if (bytes != strlen(outstring)) { + // check result + if (bytes != strlen(outstring)) { fprintf(stderr, "pipe read: %d != %ld\n", bytes, strlen(outstring)); exit(EXIT_FAILURE); } else if (memcmp(instring, outstring, strlen(outstring)) != 0) { @@ -67,6 +66,11 @@ int main(void) { printf("%s\n", instring); } + // close read end + int cr = close(pip[0]); + ERROR_IF(close, cr, == -1); + UNEXP_IF(close, cr, != 0); + exit(EXIT_SUCCESS); } } diff --git a/tests/wchar/putwchar.c b/tests/wchar/putwchar.c index 7b11264f7d..fc25911468 100644 --- a/tests/wchar/putwchar.c +++ b/tests/wchar/putwchar.c @@ -8,13 +8,8 @@ int main(void) { wchar_t *wcs = L"zĆŸę°“šŸŒ"; - int i; - for (i = 0; wcs[i] != L'\0'; i++) - { - if (0xFFFFFFFFu == putwchar(wcs[i])) - { - printf("Unable to putwchar() the wide character.\n"); - exit(EXIT_FAILURE); - } + for (int i = 0; wcs[i] != L'\0'; i++) { + wint_t status = putwchar(wcs[i]); + ERROR_IF(putwchar, status, == WEOF); } } From 96182ce8ad7aaacd2145963e8ca3bcfa889bfe80 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Sun, 24 Feb 2019 22:19:07 +0100 Subject: [PATCH 10/12] tests: Fix expected outputs --- tests/expected/stdio/fseek.stdout | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/expected/stdio/fseek.stdout b/tests/expected/stdio/fseek.stdout index 574d89ae9e..b6163b2663 100644 --- a/tests/expected/stdio/fseek.stdout +++ b/tests/expected/stdio/fseek.stdout @@ -1,2 +1,2 @@ Line 2 -ftell: 21 +ftello: 21 From efd6947d8ece3bac91363e11ff513d3ed1391b56 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Mon, 25 Feb 2019 14:13:02 +0100 Subject: [PATCH 11/12] tests: Fix function-like macros Turns the results of these macros from compound to regular statements using the old `do { ... } while(0)` trick. Must have for function-like macros. --- tests/ctype.c | 10 +++-- tests/stdlib/bsearch.c | 78 +++++++++++++++++++------------------- tests/test_helpers.h | 57 +++++++++++++++------------- tests/unistd/getopt.c | 4 +- tests/unistd/getopt_long.c | 5 ++- tests/unistd/pathconf.c | 51 +++++++++++++------------ tests/unistd/sysconf.c | 37 +++++++++--------- 7 files changed, 124 insertions(+), 118 deletions(-) diff --git a/tests/ctype.c b/tests/ctype.c index de7ebb59d1..049ac99a70 100644 --- a/tests/ctype.c +++ b/tests/ctype.c @@ -291,10 +291,12 @@ struct test_case { size_t num_test_cases = sizeof(test_cases) / sizeof(struct test_case); #define CHECK_TEST(tc, fn, retval) \ - if (fn(tc.c) != tc.fn) { \ - retval = EXIT_FAILURE; \ - printf("Unexpected result: " #fn "('%c') != %d // Char value: %d\n", tc.c, tc.fn, tc.c); \ - } + do { \ + if (fn(tc.c) != tc.fn) { \ + retval = EXIT_FAILURE; \ + printf("Unexpected result: " #fn "('%c') != %d // Char value: %d\n", tc.c, tc.fn, tc.c); \ + } \ + } while (0) int main(void) { int retval = EXIT_SUCCESS; diff --git a/tests/stdlib/bsearch.c b/tests/stdlib/bsearch.c index fe55cfce70..139cb53d65 100644 --- a/tests/stdlib/bsearch.c +++ b/tests/stdlib/bsearch.c @@ -4,55 +4,53 @@ #include "test_helpers.h" int int_cmp(const void* a, const void* b) { - return *(const int*) a - *(const int*) b; + return *(const int*) a - *(const int*) b; } #define BSEARCH_TEST_INT(key, arr, len, expect) \ - do { \ - void* res = bsearch((const void*) &key, (void*) arr, len, sizeof(int), int_cmp); \ - if (res != expect) { \ - printf("FAIL bsearch for %d in [", key); \ - size_t i = 0; \ - for (; i < len; ++i) printf("%d,", arr[i]); \ - printf("] expected %p but got %p\n", (void*) expect, res); \ - exit(EXIT_FAILURE); \ - } \ - } while (0); - - + do { \ + void* res = bsearch((const void*) &key, (void*) arr, len, sizeof(int), int_cmp); \ + if (res != expect) { \ + printf("FAIL bsearch for %d in [", key); \ + size_t i = 0; \ + for (; i < len; ++i) printf("%d,", arr[i]); \ + printf("] expected %p but got %p\n", (void*) expect, res); \ + exit(EXIT_FAILURE); \ + } \ + } while (0) int main(void) { - int x = 0; - int y = 1024; + int x = 0; + int y = 1024; - // TODO: Zero sized arrays are a non-standard GNU extension - //int empty[] = {}; - //BSEARCH_TEST_INT(x, empty, 0, NULL); + // TODO: Zero sized arrays are a non-standard GNU extension + //int empty[] = {}; + //BSEARCH_TEST_INT(x, empty, 0, NULL); - int singleton[] = {42}; - printf("%p\n%p\n", singleton, &singleton[1]); - BSEARCH_TEST_INT(x, singleton, 1, NULL); - BSEARCH_TEST_INT(singleton[0], singleton, 1, &singleton[0]); - BSEARCH_TEST_INT(y, singleton, 1, NULL); + int singleton[] = {42}; + printf("%p\n%p\n", singleton, &singleton[1]); + BSEARCH_TEST_INT(x, singleton, 1, NULL); + BSEARCH_TEST_INT(singleton[0], singleton, 1, &singleton[0]); + BSEARCH_TEST_INT(y, singleton, 1, NULL); - int two[] = {14, 42}; - BSEARCH_TEST_INT(x, two, 2, NULL); - BSEARCH_TEST_INT(y, two, 2, NULL); - BSEARCH_TEST_INT(two[0], two, 2, &two[0]); - BSEARCH_TEST_INT(two[0], two, 1, &two[0]); - BSEARCH_TEST_INT(two[1], two, 2, &two[1]); - BSEARCH_TEST_INT(two[1], two, 1, NULL); + int two[] = {14, 42}; + BSEARCH_TEST_INT(x, two, 2, NULL); + BSEARCH_TEST_INT(y, two, 2, NULL); + BSEARCH_TEST_INT(two[0], two, 2, &two[0]); + BSEARCH_TEST_INT(two[0], two, 1, &two[0]); + BSEARCH_TEST_INT(two[1], two, 2, &two[1]); + BSEARCH_TEST_INT(two[1], two, 1, NULL); - int three[] = {-5, -1, 4}; - BSEARCH_TEST_INT(three[0], three, 3, &three[0]); - BSEARCH_TEST_INT(three[1], three, 3, &three[1]); - BSEARCH_TEST_INT(three[2], three, 3, &three[2]); + int three[] = {-5, -1, 4}; + BSEARCH_TEST_INT(three[0], three, 3, &three[0]); + BSEARCH_TEST_INT(three[1], three, 3, &three[1]); + BSEARCH_TEST_INT(three[2], three, 3, &three[2]); - int big[] = {-19, -13, -7, -3, 2, 5, 11}; - BSEARCH_TEST_INT(big[0], big, 7, big); - BSEARCH_TEST_INT(big[6], big, 7, &big[6]); - BSEARCH_TEST_INT(big[3], big, 7, &big[3]); - BSEARCH_TEST_INT(x, big, 7, NULL); + int big[] = {-19, -13, -7, -3, 2, 5, 11}; + BSEARCH_TEST_INT(big[0], big, 7, big); + BSEARCH_TEST_INT(big[6], big, 7, &big[6]); + BSEARCH_TEST_INT(big[3], big, 7, &big[3]); + BSEARCH_TEST_INT(x, big, 7, NULL); - printf("PASS bsearch\n"); + printf("PASS bsearch\n"); } diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 0007a552c2..68705d8673 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -37,13 +37,14 @@ // ERROR_IF(fgetc, c, == EOF); // OK // printf("result: %c\n", c); // OK // -#define ERROR_IF(func, status, condition) { \ - if (status condition) { \ - fprintf(stderr, "%s:%s:%d: '%s' failed: %s (%d)\n", \ - __FILE__, __func__, __LINE__, #func, strerror(errno), errno); \ - _exit(EXIT_FAILURE); \ - } \ -} +#define ERROR_IF(func, status, condition) \ + do { \ + if (status condition) { \ + fprintf(stderr, "%s:%s:%d: '%s' failed: %s (%d)\n", \ + __FILE__, __func__, __LINE__, #func, strerror(errno), errno); \ + _exit(EXIT_FAILURE); \ + } \ + } while(0) // Throws errors on API return values not defined by the standards. // @@ -62,27 +63,29 @@ // UNEXP_IF(fgetc, c, < 0); // UNEXP_IF(fgetc, c, > 255); // -#define UNEXP_IF(func, status, condition) { \ - if (status condition) { \ - fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: ", \ - __FILE__, __func__, __LINE__, #func); \ - fprintf(stderr, _Generic((status), \ - char *: "char*(%p) = \"%1$s\"", \ - void *: "void*(%p)", \ - default: "%i" \ - ), status); \ - fprintf(stderr, "\n"); \ - _exit(EXIT_FAILURE); \ - } \ -} +#define UNEXP_IF(func, status, condition) \ + do { \ + if (status condition) { \ + fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: ", \ + __FILE__, __func__, __LINE__, #func); \ + fprintf(stderr, _Generic((status), \ + char *: "char*(%p) = \"%1$s\"", \ + void *: "void*(%p)", \ + default: "%i" \ + ), status); \ + fprintf(stderr, "\n"); \ + _exit(EXIT_FAILURE); \ + } \ + } while (0) // 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); \ -} +#define exit(code) \ + do { \ + if (code != EXIT_SUCCESS) { \ + fprintf(stderr, "%s:%s:%d: Test failed with exit(%s)\n", \ + __FILE__, __func__, __LINE__, #code); \ + } \ + _exit(code); \ + } while(0) #endif /* _TEST_HELPERS */ diff --git a/tests/unistd/getopt.c b/tests/unistd/getopt.c index d6f33fe078..ff41b517aa 100644 --- a/tests/unistd/getopt.c +++ b/tests/unistd/getopt.c @@ -4,14 +4,14 @@ #include "test_helpers.h" #define RUN(...) \ - { \ + do { \ optind = 1; \ optarg = NULL; \ opterr = 1; \ optopt = -1; \ char *args_arr[] = { __VA_ARGS__ }; \ printf("result: %d\n", runner(sizeof(args_arr) / sizeof(args_arr[0]), args_arr)); \ - } + } while (0) int runner(int argc, char *argv[]) { int c; diff --git a/tests/unistd/getopt_long.c b/tests/unistd/getopt_long.c index 9afca067ed..9af233cf50 100644 --- a/tests/unistd/getopt_long.c +++ b/tests/unistd/getopt_long.c @@ -3,14 +3,15 @@ #include "test_helpers.h" -#define RUN(...) { \ +#define RUN(...) \ + do { \ optind = 1; \ optarg = NULL; \ opterr = 1; \ optopt = -1; \ char *args_arr[] = { __VA_ARGS__ }; \ runner(sizeof(args_arr) / sizeof(char*), args_arr); \ - } + } while (0) void runner(int argc, char *argv[]) { printf("--- Running:"); diff --git a/tests/unistd/pathconf.c b/tests/unistd/pathconf.c index fd2e6dcd1a..bbbf922b79 100644 --- a/tests/unistd/pathconf.c +++ b/tests/unistd/pathconf.c @@ -4,31 +4,32 @@ #include "test_helpers.h" -#define PC(N) { \ - errno = 0; \ - printf("%s (%d): %ld (%d)\n", #N, _PC_ ## N, fpathconf(0, _PC_ ## N), errno); \ -} +#define PC(N) \ + do { \ + errno = 0; \ + printf("%s (%d): %ld (%d)\n", #N, _PC_ ## N, fpathconf(0, _PC_ ## N), errno); \ + } while (0) int main(void) { - PC(LINK_MAX); - PC(MAX_CANON); - PC(MAX_INPUT); - PC(NAME_MAX); - PC(PATH_MAX); - PC(PIPE_BUF); - PC(CHOWN_RESTRICTED); - PC(NO_TRUNC); - PC(VDISABLE); - PC(SYNC_IO); - PC(ASYNC_IO); - PC(PRIO_IO); - PC(SOCK_MAXBUF); - PC(FILESIZEBITS); - PC(REC_INCR_XFER_SIZE); - PC(REC_MAX_XFER_SIZE); - PC(REC_MIN_XFER_SIZE); - PC(REC_XFER_ALIGN); - PC(ALLOC_SIZE_MIN); - PC(SYMLINK_MAX); - PC(2_SYMLINKS); + PC(LINK_MAX); + PC(MAX_CANON); + PC(MAX_INPUT); + PC(NAME_MAX); + PC(PATH_MAX); + PC(PIPE_BUF); + PC(CHOWN_RESTRICTED); + PC(NO_TRUNC); + PC(VDISABLE); + PC(SYNC_IO); + PC(ASYNC_IO); + PC(PRIO_IO); + PC(SOCK_MAXBUF); + PC(FILESIZEBITS); + PC(REC_INCR_XFER_SIZE); + PC(REC_MAX_XFER_SIZE); + PC(REC_MIN_XFER_SIZE); + PC(REC_XFER_ALIGN); + PC(ALLOC_SIZE_MIN); + PC(SYMLINK_MAX); + PC(2_SYMLINKS); } diff --git a/tests/unistd/sysconf.c b/tests/unistd/sysconf.c index 7d9031366e..ecb20a53aa 100644 --- a/tests/unistd/sysconf.c +++ b/tests/unistd/sysconf.c @@ -4,24 +4,25 @@ #include "test_helpers.h" -#define SC(N) { \ - errno = 0; \ - printf("%s (%d): %ld (%d)\n", #N, _SC_ ## N, sysconf(_SC_ ## N), errno); \ -} +#define SC(N) \ + do { \ + errno = 0; \ + printf("%s (%d): %ld (%d)\n", #N, _SC_ ## N, sysconf(_SC_ ## N), errno); \ + } while (0) int main(void) { - SC(ARG_MAX); - SC(CHILD_MAX); - SC(CLK_TCK); - SC(NGROUPS_MAX); - SC(OPEN_MAX); - SC(STREAM_MAX); - SC(TZNAME_MAX); - SC(VERSION); - SC(PAGESIZE); - SC(RE_DUP_MAX); - SC(LOGIN_NAME_MAX); - SC(TTY_NAME_MAX); - SC(SYMLOOP_MAX); - SC(HOST_NAME_MAX); + SC(ARG_MAX); + SC(CHILD_MAX); + SC(CLK_TCK); + SC(NGROUPS_MAX); + SC(OPEN_MAX); + SC(STREAM_MAX); + SC(TZNAME_MAX); + SC(VERSION); + SC(PAGESIZE); + SC(RE_DUP_MAX); + SC(LOGIN_NAME_MAX); + SC(TTY_NAME_MAX); + SC(SYMLOOP_MAX); + SC(HOST_NAME_MAX); } From fa2c6d29db499c216ac00053ca40973643a6959e Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Mon, 25 Feb 2019 19:32:20 +0100 Subject: [PATCH 12/12] tests: Rewrite libgen tests based on ctype (nice table layout), fix error handling of sleep tests --- tests/expected/libgen.stdout | 39 ++++--- tests/expected/unistd/sleep.stderr | 3 - tests/expected/unistd/sleep.stdout | 1 + tests/libgen.c | 177 ++++++++++++++++++----------- tests/unistd/sleep.c | 17 ++- 5 files changed, 144 insertions(+), 93 deletions(-) diff --git a/tests/expected/libgen.stdout b/tests/expected/libgen.stdout index c19d73cc8b..6e32d2008d 100644 --- a/tests/expected/libgen.stdout +++ b/tests/expected/libgen.stdout @@ -1,18 +1,21 @@ -Testing libgen.h -OK on basename(/usr/lib), expected: 'lib', got: 'lib' -OK on basename(//usr//lib//), expected: 'lib', got: 'lib' -OK on basename(/usr/), expected: 'usr', got: 'usr' -OK on basename(), expected: '.', got: '.' -OK on basename(/), expected: '/', got: '/' -OK on basename(///), expected: '/', got: '/' -OK on basename(NULL), expected: '.', got: '.' -OK on dirname(/usr/lib), expected: '/usr', got: '/usr' -OK on dirname(//usr//lib//), expected: '//usr', got: '//usr' -OK on dirname(/usr), expected: '/', got: '/' -OK on dirname(usr), expected: '.', got: '.' -OK on dirname(/), expected: '/', got: '/' -OK on dirname(///), expected: '/', got: '/' -OK on dirname(.), expected: '.', got: '.' -OK on dirname(..), expected: '.', got: '.' -OK on dirname(), expected: '.', got: '.' -OK on dirname(NULL), expected: '.', got: '.' +dirname("") == "." +basename("") == "." +dirname(".") == "." +basename(".") == "." +dirname("..") == "." +basename("..") == ".." +dirname("/") == "/" +basename("/") == "/" +dirname("///") == "/" +basename("///") == "/" +dirname("//usr//lib//") == "//usr" +basename("//usr//lib//") == "lib" +dirname("/usr") == "/" +basename("/usr") == "usr" +dirname("/usr/") == "/" +basename("/usr/") == "usr" +dirname("/usr/lib") == "/usr" +basename("/usr/lib") == "lib" +dirname(NULL) == "." +basename(NULL) == "." +Success: 0 diff --git a/tests/expected/unistd/sleep.stderr b/tests/expected/unistd/sleep.stderr index df935fdb39..e69de29bb2 100644 --- a/tests/expected/unistd/sleep.stderr +++ b/tests/expected/unistd/sleep.stderr @@ -1,3 +0,0 @@ -sleep: Success -usleep: Success -nanosleep: Success diff --git a/tests/expected/unistd/sleep.stdout b/tests/expected/unistd/sleep.stdout index e69de29bb2..25867863a2 100644 --- a/tests/expected/unistd/sleep.stdout +++ b/tests/expected/unistd/sleep.stdout @@ -0,0 +1 @@ +unslept: 0 diff --git a/tests/libgen.c b/tests/libgen.c index 460e710fcb..0cdf8b8ade 100644 --- a/tests/libgen.c +++ b/tests/libgen.c @@ -5,78 +5,123 @@ #include "test_helpers.h" -typedef struct { - char * in; - char * expected_out; -} test_case; +#define TODO NULL -// API for basename and dirname allow the passed in string to -// be modified. This means we have to pass a modifiable copy. -char * get_mutable_string(char *str) { - if (str == NULL) - return NULL; - char * copy = malloc(sizeof(char) * (strlen(str) + 1)); - copy = strcpy(copy, str); - return copy; -} +// TODO: Tests for Redox schemes +struct test_case { + char *path; + char *dirname; + char *basename; +} test_cases[] = { + // Classic UNIX + // path dirname basename + { "", ".", "." }, + { ".", ".", "." }, + { "..", ".", ".." }, + { "/", "/", "/" }, + { "///", "/", "/" }, + { "//usr//lib//", "//usr", "lib" }, + { "/usr", "/", "usr" }, + { "/usr/", "/", "usr" }, + { "/usr/lib", "/usr", "lib" }, + { NULL, ".", "." } + // Root scheme + // path dirname basename + //{ ":", TODO, TODO }, + //{ ":/", TODO, TODO }, + //{ ":/scheme", TODO, TODO }, + // Regular scheme + // path dirname basename + //{ "file:", TODO, TODO }, + //{ "file:usr", TODO, TODO }, + //{ "file:usr/", TODO, TODO }, + //{ "file:usr/lib", TODO, TODO }, + //{ "file:/", TODO, TODO }, + //{ "file:/usr", TODO, TODO }, + //{ "file:/usr/", TODO, TODO }, + //{ "file:/usr/lib", TODO, TODO }, + //{ "file:///", TODO, TODO }, + //{ "file://usr", TODO, TODO }, + //{ "file://usr//", TODO, TODO }, + // Hierarchical scheme + // path dirname basename + //{ "disk/0:", TODO, TODO }, + //{ "disk/0:/", TODO, TODO }, + //{ "disk/0:///", TODO, TODO }, + //{ "disk/0:/usr", TODO, TODO }, + //{ "disk/0:/usr/", TODO, TODO }, + //{ "disk/0:/usr/lib", TODO, TODO }, + // Malformed + // path dirname basename + //{ "/file:/sys:/usr", TODO, TODO }, + //{ "/file:/usr", TODO, TODO }, + //{ "/file:sys:/usr", TODO, TODO }, + //{ "/file:usr", TODO, TODO }, + //{ ":file/usr", TODO, TODO }, + //{ "file:/sys:/usr", TODO, TODO }, + //{ "file::/", TODO, TODO }, + //{ "file::/usr/lib", TODO, TODO } +}; -void test_basename(void) { - test_case test_cases[] = - { {"/usr/lib", "lib"}, - {"//usr//lib//", "lib"}, - {"/usr/", "usr"}, - {"", "."}, - {"/", "/"}, - {"///","/"}, - {NULL, "."} - }; - for (int i = 0;i < sizeof(test_cases)/sizeof(test_case);i++) { - char * in = get_mutable_string(test_cases[i].in); - char * out = basename(in); - if (!out) { - printf("Error on basename(%s), expected: '%s', got NULL\n", test_cases[i].in != 0 ? test_cases[i].in : "NULL", test_cases[i].expected_out); - } else if (strcmp(out, test_cases[i].expected_out) != 0) { - printf("Error on basename(%s), expected: '%s', got: '%s'\n", test_cases[i].in != 0 ? test_cases[i].in : "NULL", test_cases[i].expected_out, out); +size_t num_test_cases = sizeof(test_cases) / sizeof(struct test_case); + +int safe_strcmp(char *s1, char *s2) { + if (s1 == NULL && s2 == NULL) { + return 0; + } else if (s1 == NULL && s2 != NULL) { + return 1; + } else if (s1 != NULL && s2 == NULL) { + return -1; } else { - printf("OK on basename(%s), expected: '%s', got: '%s'\n", test_cases[i].in != 0 ? test_cases[i].in : "NULL", test_cases[i].expected_out, out); + return strcmp(s1, s2); } - if (!in) - free(in); - } - return; } -void test_dirname(void) { - test_case test_cases[] = - { {"/usr/lib", "/usr"}, - {"//usr//lib//", "//usr"}, - {"/usr", "/"}, - {"usr", "."}, - {"/", "/"}, - {"///","/"}, - {".", "."}, - {"..", "."}, - {"", "."}, - {NULL, "."} - }; - for (int i = 0;i < sizeof(test_cases)/sizeof(test_case);i++) { - char * in = get_mutable_string(test_cases[i].in); - char * out = dirname(in); - if (!out) { - printf("Error on dirname(%s), expected: '%s', got NULL\n", test_cases[i].in != 0 ? test_cases[i].in : "NULL", test_cases[i].expected_out); - } else if (strcmp(out, test_cases[i].expected_out) != 0) { - printf("Error on dirname(%s), expected: '%s', got: '%s'\n", test_cases[i].in != 0 ? test_cases[i].in : "NULL", test_cases[i].expected_out, out); - } else { - printf("OK on dirname(%s), expected: '%s', got: '%s'\n", test_cases[i].in != 0 ? test_cases[i].in : "NULL", test_cases[i].expected_out, out); - } - if (!in) - free(in); - } - return; -} +#define CHECK_TEST(tc, fn, retval) \ + do { \ + /* API for basename and dirname allow the passed in string to */ \ + /* be modified. This means we have to pass a modifiable copy. */ \ + char *path = NULL; \ + if (tc.path != NULL) \ + path = strdup(tc.path); \ + \ + char *output = fn(path); \ + \ + /* Printing NULLs with printf("%s") is undefined behaviour, */ \ + /* that's why they are handled here this way. */ \ + char display_path[64] = "NULL"; \ + char display_output[64] = "NULL"; \ + char display_expected[64] = "NULL"; \ + if (tc.path != NULL) sprintf(display_path, "\"%s\"", tc.path); \ + if (output != NULL) sprintf(display_output, "\"%s\"", output); \ + if (tc.fn != NULL) sprintf(display_expected, "\"%s\"", tc.fn); \ + \ + if (safe_strcmp(output, tc.fn) != 0) { \ + retval = EXIT_FAILURE; \ + printf("%s(%s) != %s, expected: %s\n", \ + #fn, display_path, display_output, display_expected); \ + } else { \ + printf("%s(%s) == %s\n", \ + #fn, display_path, display_output); \ + } \ + \ + free(path); \ + } while (0) int main(void) { - printf("Testing libgen.h\n"); - test_basename(); - test_dirname(); + int retval = EXIT_SUCCESS; + + for(int i = 0; i < num_test_cases; ++i) { + struct test_case tc = test_cases[i]; + CHECK_TEST(tc, dirname, retval); + CHECK_TEST(tc, basename, retval); + } + + if (retval == EXIT_SUCCESS) { + printf("Success: %d\n", retval); + } else { + printf("Failure: %d\n", retval); + } + + return retval; } diff --git a/tests/unistd/sleep.c b/tests/unistd/sleep.c index 5f4005eb12..2d7457e2d2 100644 --- a/tests/unistd/sleep.c +++ b/tests/unistd/sleep.c @@ -5,11 +5,16 @@ #include "test_helpers.h" int main(void) { - sleep(2); - perror("sleep"); - usleep(1000); - perror("usleep"); + // sleep has no error codes and doesn't set errno + unsigned int unslept = sleep(2); + printf("unslept: %u\n", unslept); + + int us_status = usleep(1000); + ERROR_IF(usleep, us_status, == -1); + UNEXP_IF(usleep, us_status, != 0); + struct timespec tm = {0, 10000}; - nanosleep(&tm, NULL); - perror("nanosleep"); + int ns_status = nanosleep(&tm, NULL); + ERROR_IF(nanosleep, ns_status, == -1); + UNEXP_IF(nanosleep, ns_status, != 0); }