From d50b85591d2dcb8999951626dfb050dd697041f0 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 26 Feb 2017 14:41:25 +0100 Subject: [PATCH] Refactor testcase API. --- regress/test-fmemopen.c | 15 ++++++------ regress/test-pipe-one-byte.c | 6 ++--- regress/test-pthread-argv.c | 12 +++------- regress/test-pthread-basic.c | 12 ++++------ regress/test-pthread-main-exit.c | 4 +--- regress/test-pthread-main-join.c | 13 ++++------- regress/test-pthread-once.c | 10 +++----- regress/test-pthread-self.c | 16 +++++-------- regress/test-pthread-tls.c | 14 ++++------- regress/test-signal-raise.c | 7 +++--- regress/test-unix-socket-shutdown.c | 15 ++++++------ regress/test.h | 36 ++++++++++++++++++++++++----- 12 files changed, 76 insertions(+), 84 deletions(-) diff --git a/regress/test-fmemopen.c b/regress/test-fmemopen.c index 8aad3d9c..886934a4 100644 --- a/regress/test-fmemopen.c +++ b/regress/test-fmemopen.c @@ -33,24 +33,23 @@ int main(void) unsigned char* buffer = (unsigned char*) string; size_t buffer_size = string_length; - if ( !(fp = fmemopen(buffer, buffer_size, "r")) ) - test_error(errno, "fmemopen"); + test_assert((fp = fmemopen(buffer, buffer_size, "r"))); for ( size_t i = 0; i < buffer_size; i++ ) { c = fgetc(fp); - test_assert(!(c == EOF && feof(fp))); + test_assertx(!(c == EOF && feof(fp))); test_assert(!(c == EOF && ferror(fp))); - test_assert(c != EOF); - test_assert((unsigned char) c == buffer[i]); - test_assert(!feof(fp)); + test_assertx(c != EOF); + test_assertx((unsigned char) c == buffer[i]); + test_assertx(!feof(fp)); test_assert(!ferror(fp)); } c = fgetc(fp); - test_assert(c == EOF); + test_assertx(c == EOF); test_assert(!ferror(fp)); - test_assert(feof(fp)); + test_assertx(feof(fp)); fclose(fp); diff --git a/regress/test-pipe-one-byte.c b/regress/test-pipe-one-byte.c index d2399122..32b7e1fb 100644 --- a/regress/test-pipe-one-byte.c +++ b/regress/test-pipe-one-byte.c @@ -30,8 +30,8 @@ int main(void) { int fds[2]; pipe(fds); - pid_t pid = fork(); - test_assert(0 <= pid); + pid_t pid; + test_assert(0 <= (pid = fork())); if ( pid == 0 ) { close(fds[0]); @@ -43,7 +43,7 @@ int main(void) close(fds[1]); char c; test_assert(read(fds[0], &c, 1) == 1); - test_assert(c == 'X'); + test_assertx(c == 'X'); kill(pid, SIGKILL); int status; waitpid(pid, &status, 0); diff --git a/regress/test-pthread-argv.c b/regress/test-pthread-argv.c index ac2f99c7..20355e6c 100644 --- a/regress/test-pthread-argv.c +++ b/regress/test-pthread-argv.c @@ -32,10 +32,7 @@ void* thread_routine(void* ctx) { (void) ctx; - int errnum; - - if ( (errnum = pthread_join(main_thread, NULL)) ) - test_error(errnum, "pthread_join"); + test_assertp(pthread_join(main_thread, NULL)); size_t recount = 0; for ( int i = 0; global_argv[i]; i++ ) @@ -43,7 +40,7 @@ void* thread_routine(void* ctx) for ( int i = 0; global_envp[i]; i++ ) recount += strlen(global_envp[i]); - test_assert(answer == recount); + test_assertx(answer == recount); exit(0); } @@ -52,8 +49,6 @@ int main(int argc, char* argv[], char* envp[]) { (void) argc; - int errnum; - for ( int i = 0; argv[i]; i++ ) answer += strlen(argv[i]); for ( int i = 0; envp[i]; i++ ) @@ -65,8 +60,7 @@ int main(int argc, char* argv[], char* envp[]) main_thread = pthread_self(); pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL)); pthread_exit(NULL); } diff --git a/regress/test-pthread-basic.c b/regress/test-pthread-basic.c index 5d32785f..6025abba 100644 --- a/regress/test-pthread-basic.c +++ b/regress/test-pthread-basic.c @@ -30,20 +30,16 @@ void* thread_routine(void* cookie) int main(void) { - int errnum; - int test_failure = 1; pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, &test_failure)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, &test_failure)); void* thread_result; - if ( (errnum = pthread_join(thread, &thread_result)) ) - test_error(errnum, "pthread_join"); + test_assertp(pthread_join(thread, &thread_result)); - test_assert(test_failure == 0); - test_assert(thread_result == &test_failure); + test_assertx(test_failure == 0); + test_assertx(thread_result == &test_failure); return 0; } diff --git a/regress/test-pthread-main-exit.c b/regress/test-pthread-main-exit.c index aff53ff9..13c8cc0b 100644 --- a/regress/test-pthread-main-exit.c +++ b/regress/test-pthread-main-exit.c @@ -32,10 +32,8 @@ void* thread_routine(void* ctx) int main(void) { - int errnum; pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL)); sched_yield(); exit(0); } diff --git a/regress/test-pthread-main-join.c b/regress/test-pthread-main-join.c index c7b33322..ea89645a 100644 --- a/regress/test-pthread-main-join.c +++ b/regress/test-pthread-main-join.c @@ -25,28 +25,23 @@ pthread_t main_thread; void* thread_routine(void* expected_result) { - int errnum; - void* main_thread_result; - if ( (errnum = pthread_join(main_thread, &main_thread_result)) ) - test_error(errnum, "pthread_join"); + test_assertp(pthread_join(main_thread, &main_thread_result)); - test_assert(expected_result == &main_thread); + test_assertx(expected_result == &main_thread); exit(0); } int main(void) { - int errnum; - main_thread = pthread_self(); void* expected_result = &main_thread; pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, expected_result)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, + expected_result)); pthread_exit(expected_result); } diff --git a/regress/test-pthread-once.c b/regress/test-pthread-once.c index 8b19e04b..844e0894 100644 --- a/regress/test-pthread-once.c +++ b/regress/test-pthread-once.c @@ -40,18 +40,14 @@ void* thread_routine(void* ctx) int main(void) { - int errnum; - pthread_once(&init_counter_once, init_counter_increase); pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL)); - if ( (errnum = pthread_join(thread, NULL)) ) - test_error(errnum, "pthread_join"); + test_assertp(pthread_join(thread, NULL)); - test_assert(init_counter == 1); + test_assertx(init_counter == 1); return 0; } diff --git a/regress/test-pthread-self.c b/regress/test-pthread-self.c index d5e42d9d..df490468 100644 --- a/regress/test-pthread-self.c +++ b/regress/test-pthread-self.c @@ -27,7 +27,7 @@ void* thread_routine(void* main_thread_ptr) { pthread_t main_thread = *(pthread_t*) main_thread_ptr; - test_assert(!pthread_equal(main_thread, pthread_self())); + test_assertx(!pthread_equal(main_thread, pthread_self())); child_thread_self = pthread_self(); @@ -36,20 +36,16 @@ void* thread_routine(void* main_thread_ptr) int main(void) { - int errnum; - pthread_t main_thread = pthread_self(); pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, &main_thread)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, &main_thread)); - if ( (errnum = pthread_join(thread, NULL)) ) - test_error(errnum, "pthread_join"); + test_assertp(pthread_join(thread, NULL)); - test_assert(!pthread_equal(thread, pthread_self())); - test_assert(pthread_equal(thread, child_thread_self)); - test_assert(!pthread_equal(pthread_self(), child_thread_self)); + test_assertx(!pthread_equal(thread, pthread_self())); + test_assertx(pthread_equal(thread, child_thread_self)); + test_assertx(!pthread_equal(pthread_self(), child_thread_self)); return 0; } diff --git a/regress/test-pthread-tls.c b/regress/test-pthread-tls.c index 8ee1b547..d30155fe 100644 --- a/regress/test-pthread-tls.c +++ b/regress/test-pthread-tls.c @@ -27,7 +27,7 @@ void* thread_routine(void* ctx) { (void) ctx; - test_assert(tls_variable == 42); + test_assertx(tls_variable == 42); tls_variable = 9001; @@ -36,20 +36,16 @@ void* thread_routine(void* ctx) int main(void) { - int errnum; - - test_assert(tls_variable == 42); + test_assertx(tls_variable == 42); tls_variable = 1337; pthread_t thread; - if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) - test_error(errnum, "pthread_create"); + test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL)); - if ( (errnum = pthread_join(thread, NULL)) ) - test_error(errnum, "pthread_join"); + test_assertp(pthread_join(thread, NULL)); - test_assert(tls_variable == 1337); + test_assertx(tls_variable == 1337); return 0; } diff --git a/regress/test-signal-raise.c b/regress/test-signal-raise.c index 5eab83db..00842068 100644 --- a/regress/test-signal-raise.c +++ b/regress/test-signal-raise.c @@ -29,7 +29,7 @@ void signal_handler(int signum, siginfo_t* siginfo, void* ucontext_ptr) (void) siginfo; (void) ucontext_ptr; - test_assert(signum == SIGUSR1); + test_assertx(signum == SIGUSR1); signal_counter++; } @@ -41,12 +41,11 @@ int main(void) sa.sa_sigaction = signal_handler; sa.sa_flags = SA_SIGINFO; - if ( sigaction(SIGUSR1, &sa, NULL) ) - test_error(errno, "sigaction(USR1)"); + test_assert(sigaction(SIGUSR1, &sa, NULL) == 0); raise(SIGUSR1); - test_assert(signal_counter == 1); + test_assertx(signal_counter == 1); return 0; } diff --git a/regress/test-unix-socket-shutdown.c b/regress/test-unix-socket-shutdown.c index 2686b5db..74b388cc 100644 --- a/regress/test-unix-socket-shutdown.c +++ b/regress/test-unix-socket-shutdown.c @@ -50,10 +50,9 @@ static void test(bool test_read, char c; if ( test_read ) { - ssize_t amount = read(fds[0], &c, 1); - if ( amount < 0 ) - test_error(errno, "read"); - test_assert(amount == 0); + ssize_t amount; + test_assert(0 <= (amount = read(fds[0], &c, 1))); + test_assertx(amount == 0); _exit(0); } else @@ -64,8 +63,8 @@ static void test(bool test_read, ssize_t amount = write(fds[0], &c, 1); if ( !sigpipe && amount == -1 && errno == EPIPE ) _exit(0); - if ( amount != 1 ) - test_error(errno, "write"); + test_assert(0 <= amount); + test_assertx(amount == 1); } } } @@ -82,9 +81,9 @@ static void test(bool test_read, int status; test_assert(waitpid(child_pid, &status, 0) == child_pid); if ( test_read || !sigpipe ) - test_assert(WIFEXITED(status) && WEXITSTATUS(status) == 0); + test_assertx(WIFEXITED(status) && WEXITSTATUS(status) == 0); else - test_assert(WIFSIGNALED(status) && WTERMSIG(status) == SIGPIPE); + test_assertx(WIFSIGNALED(status) && WTERMSIG(status) == SIGPIPE); if ( parent_shutdown || child_shutdown ) close(fds[1]); } diff --git a/regress/test.h b/regress/test.h index 32db492d..5776a0d5 100644 --- a/regress/test.h +++ b/regress/test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2014, 2017 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,17 +20,14 @@ #ifndef TEST_H #define TEST_H -#undef NDEBUG -#include #include #include +#include #include #include #include -#define test_assert(x) assert(x) - -__attribute__((noreturn)) +__attribute((noreturn, unused)) static inline void test_error(int errnum, const char* format, ...) { fprintf(stderr, "%s: ", program_invocation_name); @@ -47,4 +44,31 @@ void test_error(int errnum, const char* format, ...) exit(1); } +__attribute((unused)) +static inline void test_assertion(bool assertion, + const char* file, + unsigned int line, + const char* assertion_string, + int errnum) +{ + if ( !assertion ) + test_error(errnum, "assertion failure: %s:%u: %s", file, line, + assertion_string); +} + +__attribute((unused)) +static inline void test_assertionp(int errnum, + const char* file, + unsigned int line, + const char* assertion_string) +{ + test_assertion(errnum == 0, file, line, assertion_string, errnum); +} + +#define test_assert(x) test_assertion((x), __FILE__, __LINE__, #x, errno) +#define test_assertc(x, errnum) \ + test_assertion((x), __FILE__, __LINE__, #x, errnum) +#define test_assertp(x) test_assertionp((x), __FILE__, __LINE__, #x) +#define test_assertx(x) test_assertion((x), __FILE__, __LINE__, #x, 0) + #endif