Bläddra i källkod

tests: More work on error handling

Tibor Nagy 6 år sedan
förälder
incheckning
2d027f0771

+ 4 - 6
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);
 }

+ 2 - 0
tests/expected/stdlib/rand.stdout

@@ -1,2 +1,4 @@
 67141780
 201425341
+201425341
+67141780

+ 1 - 1
tests/expected/string/strrchr.stdout

@@ -1 +1 @@
-strrch PASS, exiting with status code 0
+strrch PASS

+ 6 - 2
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);
 }

+ 7 - 2
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);
 }

+ 6 - 4
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
 }

+ 8 - 5
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);
 }

+ 1 - 1
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) {

+ 36 - 9
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 {

+ 26 - 2
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);
+    }
 }

+ 17 - 15
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");
 }

+ 9 - 3
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); \
     } \
 }

+ 1 - 1
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;
 

+ 2 - 1
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);
 }