Browse Source

tests: More refactoring, add helper header to every test, override exit for better error reporting

Tibor Nagy 6 years ago
parent
commit
9a0ea6ff34
99 changed files with 322 additions and 87 deletions
  1. 2 0
      tests/alloca.c
  2. 2 0
      tests/args.c
  3. 2 1
      tests/arpainet.c
  4. 2 0
      tests/assert.c
  5. 1 0
      tests/constructor.c
  6. 2 0
      tests/ctype.c
  7. 1 0
      tests/destructor.c
  8. 2 0
      tests/dirent/main.c
  9. 5 4
      tests/dirent/scandir.c
  10. 2 0
      tests/error.c
  11. 7 8
      tests/fcntl/create.c
  12. 15 2
      tests/fcntl/fcntl.c
  13. 2 0
      tests/fnmatch.c
  14. 2 0
      tests/libgen.c
  15. 2 0
      tests/locale.c
  16. 2 0
      tests/math.c
  17. 2 0
      tests/netdb/getaddrinfo.c
  18. 2 0
      tests/netdb/netdb.c
  19. 2 0
      tests/pwd.c
  20. 2 0
      tests/regex.c
  21. 5 4
      tests/resource/getrusage.c
  22. 2 0
      tests/select.c
  23. 2 0
      tests/setjmp.c
  24. 2 0
      tests/signal.c
  25. 2 0
      tests/stdio/all.c
  26. 2 0
      tests/stdio/buffer.c
  27. 2 0
      tests/stdio/fgets.c
  28. 2 0
      tests/stdio/fputs.c
  29. 2 0
      tests/stdio/fread.c
  30. 2 0
      tests/stdio/freopen.c
  31. 2 0
      tests/stdio/fseek.c
  32. 2 0
      tests/stdio/fwrite.c
  33. 2 0
      tests/stdio/getc_unget.c
  34. 2 0
      tests/stdio/mutex.c
  35. 2 0
      tests/stdio/popen.c
  36. 2 0
      tests/stdio/printf.c
  37. 2 0
      tests/stdio/rename.c
  38. 2 0
      tests/stdio/scanf.c
  39. 2 0
      tests/stdio/setvbuf.c
  40. 2 0
      tests/stdio/sprintf.c
  41. 2 0
      tests/stdlib/a64l.c
  42. 2 0
      tests/stdlib/alloc.c
  43. 2 0
      tests/stdlib/atof.c
  44. 2 0
      tests/stdlib/atoi.c
  45. 2 0
      tests/stdlib/bsearch.c
  46. 18 15
      tests/stdlib/div.c
  47. 2 0
      tests/stdlib/env.c
  48. 2 0
      tests/stdlib/mkostemps.c
  49. 2 0
      tests/stdlib/mktemp.c
  50. 2 0
      tests/stdlib/rand.c
  51. 2 0
      tests/stdlib/realpath.c
  52. 2 0
      tests/stdlib/strtod.c
  53. 2 0
      tests/stdlib/strtol.c
  54. 2 0
      tests/stdlib/strtoul.c
  55. 4 1
      tests/stdlib/system.c
  56. 2 0
      tests/string/mem.c
  57. 2 0
      tests/string/strcat.c
  58. 2 0
      tests/string/strchr.c
  59. 2 0
      tests/string/strcpy.c
  60. 2 0
      tests/string/strcspn.c
  61. 2 0
      tests/string/strncmp.c
  62. 2 0
      tests/string/strpbrk.c
  63. 2 0
      tests/string/strrchr.c
  64. 2 0
      tests/string/strspn.c
  65. 2 0
      tests/string/strstr.c
  66. 2 0
      tests/string/strtok.c
  67. 2 0
      tests/string/strtok_r.c
  68. 2 0
      tests/strings.c
  69. 10 10
      tests/sys_utsname/uname.c
  70. 20 7
      tests/test_helpers.h
  71. 2 0
      tests/time/asctime.c
  72. 7 1
      tests/time/gettimeofday.c
  73. 2 0
      tests/time/gmtime.c
  74. 2 0
      tests/time/localtime.c
  75. 2 0
      tests/time/macros.c
  76. 2 0
      tests/time/mktime.c
  77. 2 0
      tests/time/strftime.c
  78. 5 12
      tests/time/time.c
  79. 5 1
      tests/time/times.c
  80. 2 0
      tests/unistd/access.c
  81. 32 5
      tests/unistd/dup.c
  82. 2 0
      tests/unistd/getcwd.c
  83. 2 0
      tests/unistd/getid.c
  84. 2 0
      tests/unistd/getopt.c
  85. 2 0
      tests/unistd/getopt_long.c
  86. 2 0
      tests/unistd/isatty.c
  87. 2 0
      tests/unistd/link.c
  88. 2 0
      tests/unistd/pathconf.c
  89. 2 0
      tests/unistd/pipe.c
  90. 15 11
      tests/unistd/setid.c
  91. 2 0
      tests/unistd/sleep.c
  92. 6 5
      tests/unistd/stat.c
  93. 2 0
      tests/unistd/sysconf.c
  94. 2 0
      tests/unistd/write.c
  95. 2 0
      tests/waitpid.c
  96. 2 0
      tests/wchar/mbrtowc.c
  97. 2 0
      tests/wchar/mbsrtowcs.c
  98. 2 0
      tests/wchar/putwchar.c
  99. 2 0
      tests/wchar/wcrtomb.c

+ 2 - 0
tests/alloca.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char *str = (char *) alloca(17);
 

+ 2 - 0
tests/args.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#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]));

+ 2 - 1
tests/arpainet.c

@@ -3,6 +3,8 @@
 #include <string.h>
 #include <stdlib.h>
 
+#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);
-
 }

+ 2 - 0
tests/assert.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     assert(1 == 1);
     assert(1 + 1 == 2);

+ 1 - 0
tests/constructor.c

@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include "test_helpers.h"
 
 __attribute__((constructor))
 void constructor_no_priority(void) {

+ 2 - 0
tests/ctype.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 struct test_case {
     int c;
     int isalnum;

+ 1 - 0
tests/destructor.c

@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include "test_helpers.h"
 
 __attribute__((destructor))
 void destructor_no_priority(void) {

+ 2 - 0
tests/dirent/main.c

@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%lu\n", sizeof(struct dirent));
 

+ 5 - 4
tests/dirent/scandir.c

@@ -3,17 +3,18 @@
 #include <stdlib.h>
 #include <string.h>
 
+#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);

+ 2 - 0
tests/error.c

@@ -3,6 +3,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     chdir("nonexistent");
     printf("errno: %d = %s\n", errno, strerror(errno));

+ 7 - 8
tests/fcntl/create.c

@@ -2,14 +2,13 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#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);
 }

+ 15 - 2
tests/fcntl/fcntl.c

@@ -2,12 +2,25 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#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);
 }

+ 2 - 0
tests/fnmatch.c

@@ -1,6 +1,8 @@
 #include <fnmatch.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 void test(char* pattern, char* input, int flags) {
     if (!fnmatch(pattern, input, flags)) {
         printf("\"%s\" matches \"%s\"\n", pattern, input);

+ 2 - 0
tests/libgen.c

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 typedef struct {
   char * in;
   char * expected_out;

+ 2 - 0
tests/locale.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     // TODO: Implement locale properly and test it here
     char* val = setlocale(LC_ALL, NULL);

+ 2 - 0
tests/math.c

@@ -1,6 +1,8 @@
 #include <math.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     double pi = 3.14;
     float c = cos(pi);

+ 2 - 0
tests/netdb/getaddrinfo.c

@@ -8,6 +8,8 @@
 #include <sys/socket.h>
 #include <arpa/inet.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct addrinfo hints, *res;
     int errcode;

+ 2 - 0
tests/netdb/netdb.c

@@ -35,6 +35,8 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int error_count;
 static void
 output_servent (const char *call, struct servent *sptr)

+ 2 - 0
tests/pwd.c

@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#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);

+ 2 - 0
tests/regex.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     regex_t regex;
     char error_buf[256];

+ 5 - 4
tests/resource/getrusage.c

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <sys/resource.h>
 
+#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);

+ 2 - 0
tests/select.c

@@ -3,6 +3,8 @@
 #include <sys/select.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int fd = open("select.c", 0, 0);
 

+ 2 - 0
tests/setjmp.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <setjmp.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     jmp_buf buf;
     if (setjmp(buf)) {

+ 2 - 0
tests/signal.c

@@ -4,6 +4,8 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 void handler(int sig) {
     puts("Signal handler called!");
 }

+ 2 - 0
tests/stdio/all.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/stdio.in", "r");
 	printf("%c\n", fgetc(f));

+ 2 - 0
tests/stdio/buffer.c

@@ -1,5 +1,7 @@
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     // Tests what used to be a bug with buffering
     fwrite("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1, 999, stdout);

+ 2 - 0
tests/stdio/fgets.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     //FILE *f = fopen("/etc/ssl/certs/ca-certificates.crt", "r");
     FILE *f = fopen("stdio/stdio.in", "r");

+ 2 - 0
tests/stdio/fputs.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/fputs.out", "w");
 	char *in = "Hello World!";

+ 2 - 0
tests/stdio/fread.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     FILE *fp = fopen("stdio/fread.in", "rb");
 

+ 2 - 0
tests/stdio/freopen.c

@@ -1,5 +1,7 @@
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	freopen("stdio/stdio.in", "r", stdin);
 	char in[6];

+ 2 - 0
tests/stdio/fseek.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/stdio.in", "r");
     if (fseek(f, 14, SEEK_CUR) < 0) {

+ 2 - 0
tests/stdio/fwrite.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <errno.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	FILE *f = fopen("stdio/fwrite.out", "w");
 	const char ptr[] = "Hello World!";

+ 2 - 0
tests/stdio/getc_unget.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	ungetc('h', stdin);
 	char c;

+ 2 - 0
tests/stdio/mutex.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     FILE* f = fopen("stdio/stdio.in", "r");
 

+ 2 - 0
tests/stdio/popen.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     FILE *fp;
     int status;

+ 2 - 0
tests/stdio/printf.c

@@ -1,5 +1,7 @@
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int sofar = 0;
     int len = printf(

+ 2 - 0
tests/stdio/rename.c

@@ -4,6 +4,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 static char oldpath[] = "old-name.out";
 static char newpath[] = "new-name.out";
 static char str[] = "Hello, World!";

+ 2 - 0
tests/stdio/scanf.c

@@ -1,6 +1,8 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 struct params {
     short sa;
     int ia;

+ 2 - 0
tests/stdio/setvbuf.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	setvbuf(stdout, 0, _IONBF, 0);
 	FILE *f = fopen("stdio/stdio.in", "r");

+ 2 - 0
tests/stdio/sprintf.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char buffer[72];
 

+ 2 - 0
tests/stdlib/a64l.c

@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char * s = "azAZ9."; // test boundaries
     long l = a64l(s);

+ 2 - 0
tests/stdlib/alloc.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char * ptr = (char *)malloc(256);
     printf("malloc %p\n", ptr);

+ 2 - 0
tests/stdlib/atof.c

@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     double d = atof("-3.14");
     printf("%f\n", d);

+ 2 - 0
tests/stdlib/atoi.c

@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%d\n", atoi("         -42"));
     printf("%d\n", atoi(" +555"));

+ 2 - 0
tests/stdlib/bsearch.c

@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int int_cmp(const void* a, const void* b) {
   return *(const int*) a - *(const int*) b;
 }

+ 18 - 15
tests/stdlib/div.c

@@ -1,20 +1,23 @@
 #include <stdlib.h>
-         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);
 }
 

+ 2 - 0
tests/stdlib/env.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     //puts(getenv("SHELL"));
     //puts(getenv("CC"));

+ 2 - 0
tests/stdlib/mkostemps.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* file_name = (char*) calloc(18, sizeof(char));
     strcpy(file_name, "tempXXXXXX.suffix");

+ 2 - 0
tests/stdlib/mktemp.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* string = (char*) calloc(20, sizeof(char));
     strcpy(string, "tempXXXXXX");

+ 2 - 0
tests/stdlib/rand.c

@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%d\n", rand());
     srand(259);

+ 2 - 0
tests/stdlib/realpath.c

@@ -4,6 +4,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* path = realpath("stdlib/realpath.c", NULL);
     if (!path) {

+ 2 - 0
tests/stdlib/strtod.c

@@ -1,6 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* endptr = 0;
     double d;

+ 2 - 0
tests/stdlib/strtol.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%ld\n", strtol("         -42", NULL, 0));
     printf("%ld\n", strtol(" +555", NULL, 0));

+ 2 - 0
tests/stdlib/strtoul.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%ld\n", strtoul("         -42", NULL, 0));
     printf("%ld\n", strtoul(" +555", NULL, 0));

+ 4 - 1
tests/stdlib/system.c

@@ -1,5 +1,8 @@
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    system("echo test of system");
+    int status = system("echo test of system");
+    ERROR_IF(system, status, == -1);
 }

+ 2 - 0
tests/string/mem.c

@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     puts("# mem #");
     char arr[100];

+ 2 - 0
tests/string/strcat.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char dest1[12] = "hello";
 	  printf("%s\n", strcat(dest1, " world")); // should be hello world

+ 2 - 0
tests/string/strchr.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#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

+ 2 - 0
tests/string/strcpy.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char dst[20];
 

+ 2 - 0
tests/string/strcspn.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	char *world = "world";
 	printf("%ld\n", strcspn("hello", world)); // should be 2

+ 2 - 0
tests/string/strncmp.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%d\n", strncmp("a", "aa", 2));
     printf("%d\n", strncmp("a", "aä", 2));

+ 2 - 0
tests/string/strpbrk.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char* source = "The quick drawn fix jumps over the lazy bug";
 

+ 2 - 0
tests/string/strrchr.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
   char s0[] = "hello, world";
   char* ptr = strrchr(s0, 'l');

+ 2 - 0
tests/string/strspn.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
 	char *hello = "hello";
 	char *world = "world";

+ 2 - 0
tests/string/strstr.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#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"));

+ 2 - 0
tests/string/strtok.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#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";

+ 2 - 0
tests/string/strtok_r.c

@@ -1,6 +1,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#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";

+ 2 - 0
tests/strings.c

@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <strings.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     assert(!bcmp("hello", "hehe", 2));
     assert(bcmp("hello", "haha", 2));

+ 10 - 10
tests/sys_utsname/uname.c

@@ -1,19 +1,19 @@
 #include <stdio.h>
 #include <sys/utsname.h>
 
+#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);
 }

+ 20 - 7
tests/test_helpers.h

@@ -1,27 +1,40 @@
 #ifndef _TEST_HELPERS
 #define _TEST_HELPERS
 
+#include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
+#include <unistd.h>
 
 // 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 */

+ 2 - 0
tests/time/asctime.c

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     time_t a = 0;
     struct tm *time_info = gmtime(&a);

+ 7 - 1
tests/time/gettimeofday.c

@@ -1,8 +1,14 @@
 #include <sys/time.h>
 #include <stdio.h>
 
+#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);
 }

+ 2 - 0
tests/time/gmtime.c

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#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,

+ 2 - 0
tests/time/localtime.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <time.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int day = 60 * 60 * 24;
     time_t inputs[] = { -(day * 33), -day, -1, -500, 0, 1, 1531454950 };

+ 2 - 0
tests/time/macros.c

@@ -1,6 +1,8 @@
 #include <assert.h>
 #include <sys/time.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     struct timeval x = { .tv_usec = 15 };
     struct timeval y = { 0 };

+ 2 - 0
tests/time/mktime.c

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <time.h>
 
+#include "test_helpers.h"
+
 int check(time_t input) {
     struct tm* t = localtime(&input);
 

+ 2 - 0
tests/time/strftime.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <time.h>
 
+#include "test_helpers.h"
+
 void print(time_t timestamp, char* fmt) {
     char* out = malloc(50);
     size_t n = strftime(out, 50, fmt, localtime(&timestamp));

+ 5 - 12
tests/time/time.c

@@ -2,24 +2,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#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);
 }

+ 5 - 1
tests/time/times.c

@@ -2,9 +2,13 @@
 #include <sys/times.h>
 #include <unistd.h>
 
+#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);

+ 2 - 0
tests/unistd/access.c

@@ -2,6 +2,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     if (access("example_dir/1-never-gonna-give-you-up", R_OK | W_OK)) {
         perror("access");

+ 32 - 5
tests/unistd/dup.c

@@ -2,15 +2,42 @@
 #include <fcntl.h>
 #include <stdio.h>
 
+#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);
 }

+ 2 - 0
tests/unistd/getcwd.c

@@ -4,6 +4,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     char first[PATH_MAX] = { 0 };
     getcwd(first, PATH_MAX);

+ 2 - 0
tests/unistd/getid.c

@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     gid_t egid = getegid();
     uid_t euid = geteuid();

+ 2 - 0
tests/unistd/getopt.c

@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 #define RUN(...) \
     { \
         optind = 1; \

+ 2 - 0
tests/unistd/getopt_long.c

@@ -1,6 +1,8 @@
 #include <getopt.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 #define RUN(...) { \
         optind = 1; \
         optarg = NULL; \

+ 2 - 0
tests/unistd/isatty.c

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int status = isatty(STDOUT_FILENO);
 

+ 2 - 0
tests/unistd/link.c

@@ -4,6 +4,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     printf("%ld\n", sizeof(struct stat));
 

+ 2 - 0
tests/unistd/pathconf.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 #define PC(N) { \
 	errno = 0; \
 	printf("%s (%d): %ld (%d)\n", #N, _PC_ ## N, fpathconf(0, _PC_ ## N), errno); \

+ 2 - 0
tests/unistd/pipe.c

@@ -4,6 +4,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     int pid, pip[2];
     char instring[20];

+ 15 - 11
tests/unistd/setid.c

@@ -5,20 +5,24 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
-    if( setpgid( getpid(), 0 ) == -1 ) {
-        perror( "setpgid" );
-    }
-    printf( "%d belongs to process group %d\n",
-         getpid(), getpgrp() );
+    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());
 }

+ 2 - 0
tests/unistd/sleep.c

@@ -2,6 +2,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     sleep(2);
     perror("sleep");

+ 6 - 5
tests/unistd/stat.c

@@ -4,15 +4,16 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#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);

+ 2 - 0
tests/unistd/sysconf.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 #define SC(N) { \
 	errno = 0; \
 	printf("%s (%d): %ld (%d)\n", #N, _SC_ ## N, sysconf(_SC_ ## N), errno); \

+ 2 - 0
tests/unistd/write.c

@@ -1,5 +1,7 @@
 #include <unistd.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     write(STDOUT_FILENO, "Hello World!\n", 13);
 }

+ 2 - 0
tests/waitpid.c

@@ -2,6 +2,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     pid_t pid = fork();
     if (pid == 0) {

+ 2 - 0
tests/wchar/mbrtowc.c

@@ -2,6 +2,8 @@
 #include <string.h>
 #include <wchar.h>
  
+#include "test_helpers.h"
+
 int main(void) {
     mbstate_t state;
     memset(&state, 0, sizeof state);

+ 2 - 0
tests/wchar/mbsrtowcs.c

@@ -2,6 +2,8 @@
 #include <string.h>
 #include <wchar.h>
 
+#include "test_helpers.h"
+
 void print_as_wide(const char* mbstr)
 {
     mbstate_t state;

+ 2 - 0
tests/wchar/putwchar.c

@@ -3,6 +3,8 @@
 #include <wchar.h>
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     wchar_t *wcs = L"zß水🍌";
 

+ 2 - 0
tests/wchar/wcrtomb.c

@@ -3,6 +3,8 @@
 #include <wchar.h> 
 #include <stdlib.h>
 
+#include "test_helpers.h"
+
 int main(void) {
     mbstate_t state;
     memset(&state, 0, sizeof state);