diff --git a/tests/ctype.c b/tests/ctype.c index de7ebb59d1e7e6c4ded407a2fba193498be2a94e..049ac99a70a77174a3e9ad53dbaa6f992ed48e37 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 fe55cfce70c66a495103f29bf17ceb4f00da5438..139cb53d65ed041385352a16b356c2a6a7edc3ed 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; - - // 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"); } diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 0007a552c278043b9b44aa5cb81aa9a19f8140c3..68705d8673b0516751133e5ddb6a2803f1079bd6 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 d6f33fe0789a41f25d1b87b7883c344630296d95..ff41b517aa1d3798e5d7c78b4a4c96b89b9426ed 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 9afca067ed7951529099300f760278230ce69ce8..9af233cf508ccff72194cfece56dd0660c4b816b 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 fd2e6dcd1ae441f1cc3cd1abeacbf06a5207280b..bbbf922b79fb75cb27f962363a0db2b692e8ce3e 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 7d9031366e52c54e31735e08f3b64906f8a226e1..ecb20a53aab0d07d125feec73e47f654e5fdaf12 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); }