From 2d027f07718cb476d6fbd1755e75d1c00a3cc623 Mon Sep 17 00:00:00 2001 From: Tibor Nagy <xnagytibor@gmail.com> Date: Sun, 24 Feb 2019 00:46:26 +0100 Subject: [PATCH] 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 0a8e265d3..f1c92ed50 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 86ea6812e..60210f3e9 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 fdeaa3bc7..836469707 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 1d7dce6e8..c365bbcbd 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 689502f2b..df8442b8e 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 f6b4dbed9..6c658435f 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 8bc948799..c370cc4e6 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 3d314d52a..56a2ab104 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 5c0dc89de..62592a5e2 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 a665e2a46..52908fae7 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); + int rand_seed259_1 = rand(); + printf("%d\n", rand_seed259_1); + srand(259); - printf("%d\n", rand()); + 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 379d023d6..1013e7610 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 d3559a4e5..0007a552c 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 a1a949fd4..5c09c1260 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 5dde94c72..6810af385 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); } -- GitLab