From efd6947d8ece3bac91363e11ff513d3ed1391b56 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Mon, 25 Feb 2019 14:13:02 +0100 Subject: [PATCH] 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. --- tests/ctype.c | 10 +++-- tests/stdlib/bsearch.c | 78 +++++++++++++++++++------------------- tests/test_helpers.h | 57 +++++++++++++++------------- tests/unistd/getopt.c | 4 +- tests/unistd/getopt_long.c | 5 ++- tests/unistd/pathconf.c | 51 +++++++++++++------------ tests/unistd/sysconf.c | 37 +++++++++--------- 7 files changed, 124 insertions(+), 118 deletions(-) diff --git a/tests/ctype.c b/tests/ctype.c index de7ebb59d1..049ac99a70 100644 --- a/tests/ctype.c +++ b/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; diff --git a/tests/stdlib/bsearch.c b/tests/stdlib/bsearch.c index fe55cfce70..139cb53d65 100644 --- a/tests/stdlib/bsearch.c +++ b/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; + 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); + // 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 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 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 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); + 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"); + printf("PASS bsearch\n"); } diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 0007a552c2..68705d8673 100644 --- a/tests/test_helpers.h +++ b/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 */ diff --git a/tests/unistd/getopt.c b/tests/unistd/getopt.c index d6f33fe078..ff41b517aa 100644 --- a/tests/unistd/getopt.c +++ b/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; diff --git a/tests/unistd/getopt_long.c b/tests/unistd/getopt_long.c index 9afca067ed..9af233cf50 100644 --- a/tests/unistd/getopt_long.c +++ b/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:"); diff --git a/tests/unistd/pathconf.c b/tests/unistd/pathconf.c index fd2e6dcd1a..bbbf922b79 100644 --- a/tests/unistd/pathconf.c +++ b/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); } diff --git a/tests/unistd/sysconf.c b/tests/unistd/sysconf.c index 7d9031366e..ecb20a53aa 100644 --- a/tests/unistd/sysconf.c +++ b/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); }