diff --git a/tests/dirent/main.c b/tests/dirent/main.c index 0a8e265d38d30dde9fe3120015253cdb8c745f97..f1c92ed506f5daf01a5d39e63b43bc15f7b13fb6 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 86ea6812ed9ef9471aa24500e3386c2f48d1970c..60210f3e9df671359f5045cc4dcd64ebe2d6e5f9 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 fdeaa3bc75d219c3cc46ae1dac3ce5cb9adc8b25..836469707d6720ec706fdb3b0abb53dfe5bd674c 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 1d7dce6e8028c2bfec79d3d68efdba7e5a8483af..c365bbcbd721de95cb4593bee32ad094bae75cfa 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 689502f2b50426c55338d1a3903d2894ae8eb152..df8442b8e654cb038499e54df68fba6354c5cf87 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 f6b4dbed91413d22cfd10d883a160ac84b480fb5..6c658435f658d7cfd93a65e57694c65115e0f8f3 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 8bc948799d3e770d59d67fd58cf0a5174a6329b7..c370cc4e6dc43812850568a4445fd47e79cd2a5f 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 3d314d52a1d333c8ca26e414c6b2d763b137cd95..56a2ab1043100a541295c52ab9fc6a5f8d1357bc 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 5c0dc89de643ff2a0c49141625e558a264969bfe..62592a5e284abf8908be052d55e081ecde9dfa4b 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 a665e2a4661274de9861839b1e31fd96a8158708..52908fae709db4375790716445eb3c56b903a935 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 379d023d6afc19031b5d23332a12babcd50756c9..1013e76100ec7a0a7879cab90460163a41bc4065 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 d3559a4e53465df08c91e9d37bd51a5b2303c843..0007a552c278043b9b44aa5cb81aa9a19f8140c3 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 a1a949fd483c7f011946ac6a27f35ed1601165b0..5c09c126067a8937c23eddd5779f732058c3577d 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 5dde94c727f0e3b11854a66a0dc3a7dea26b2c2d..6810af3855b3246862d60fcd2f82826a61e05fe8 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); }