Przeglądaj źródła

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.

Tibor Nagy 6 lat temu
rodzic
commit
efd6947d8e

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

+ 44 - 46
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;
-
-  // 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 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 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");
+    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);
+
+    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 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);
+
+    printf("PASS bsearch\n");
 }

+ 30 - 27
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 */

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

+ 3 - 2
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:");

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

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