Refactor testcase API.

This commit is contained in:
Jonas 'Sortie' Termansen 2017-02-26 14:41:25 +01:00
parent 590fd835d5
commit d50b85591d
12 changed files with 76 additions and 84 deletions

View File

@ -33,24 +33,23 @@ int main(void)
unsigned char* buffer = (unsigned char*) string; unsigned char* buffer = (unsigned char*) string;
size_t buffer_size = string_length; size_t buffer_size = string_length;
if ( !(fp = fmemopen(buffer, buffer_size, "r")) ) test_assert((fp = fmemopen(buffer, buffer_size, "r")));
test_error(errno, "fmemopen");
for ( size_t i = 0; i < buffer_size; i++ ) for ( size_t i = 0; i < buffer_size; i++ )
{ {
c = fgetc(fp); 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 && ferror(fp)));
test_assert(c != EOF); test_assertx(c != EOF);
test_assert((unsigned char) c == buffer[i]); test_assertx((unsigned char) c == buffer[i]);
test_assert(!feof(fp)); test_assertx(!feof(fp));
test_assert(!ferror(fp)); test_assert(!ferror(fp));
} }
c = fgetc(fp); c = fgetc(fp);
test_assert(c == EOF); test_assertx(c == EOF);
test_assert(!ferror(fp)); test_assert(!ferror(fp));
test_assert(feof(fp)); test_assertx(feof(fp));
fclose(fp); fclose(fp);

View File

@ -30,8 +30,8 @@ int main(void)
{ {
int fds[2]; int fds[2];
pipe(fds); pipe(fds);
pid_t pid = fork(); pid_t pid;
test_assert(0 <= pid); test_assert(0 <= (pid = fork()));
if ( pid == 0 ) if ( pid == 0 )
{ {
close(fds[0]); close(fds[0]);
@ -43,7 +43,7 @@ int main(void)
close(fds[1]); close(fds[1]);
char c; char c;
test_assert(read(fds[0], &c, 1) == 1); test_assert(read(fds[0], &c, 1) == 1);
test_assert(c == 'X'); test_assertx(c == 'X');
kill(pid, SIGKILL); kill(pid, SIGKILL);
int status; int status;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);

View File

@ -32,10 +32,7 @@ void* thread_routine(void* ctx)
{ {
(void) ctx; (void) ctx;
int errnum; test_assertp(pthread_join(main_thread, NULL));
if ( (errnum = pthread_join(main_thread, NULL)) )
test_error(errnum, "pthread_join");
size_t recount = 0; size_t recount = 0;
for ( int i = 0; global_argv[i]; i++ ) 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++ ) for ( int i = 0; global_envp[i]; i++ )
recount += strlen(global_envp[i]); recount += strlen(global_envp[i]);
test_assert(answer == recount); test_assertx(answer == recount);
exit(0); exit(0);
} }
@ -52,8 +49,6 @@ int main(int argc, char* argv[], char* envp[])
{ {
(void) argc; (void) argc;
int errnum;
for ( int i = 0; argv[i]; i++ ) for ( int i = 0; argv[i]; i++ )
answer += strlen(argv[i]); answer += strlen(argv[i]);
for ( int i = 0; envp[i]; i++ ) for ( int i = 0; envp[i]; i++ )
@ -65,8 +60,7 @@ int main(int argc, char* argv[], char* envp[])
main_thread = pthread_self(); main_thread = pthread_self();
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL));
test_error(errnum, "pthread_create");
pthread_exit(NULL); pthread_exit(NULL);
} }

View File

@ -30,20 +30,16 @@ void* thread_routine(void* cookie)
int main(void) int main(void)
{ {
int errnum;
int test_failure = 1; int test_failure = 1;
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, &test_failure)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine, &test_failure));
test_error(errnum, "pthread_create");
void* thread_result; void* thread_result;
if ( (errnum = pthread_join(thread, &thread_result)) ) test_assertp(pthread_join(thread, &thread_result));
test_error(errnum, "pthread_join");
test_assert(test_failure == 0); test_assertx(test_failure == 0);
test_assert(thread_result == &test_failure); test_assertx(thread_result == &test_failure);
return 0; return 0;
} }

View File

@ -32,10 +32,8 @@ void* thread_routine(void* ctx)
int main(void) int main(void)
{ {
int errnum;
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL));
test_error(errnum, "pthread_create");
sched_yield(); sched_yield();
exit(0); exit(0);
} }

View File

@ -25,28 +25,23 @@ pthread_t main_thread;
void* thread_routine(void* expected_result) void* thread_routine(void* expected_result)
{ {
int errnum;
void* main_thread_result; void* main_thread_result;
if ( (errnum = pthread_join(main_thread, &main_thread_result)) ) test_assertp(pthread_join(main_thread, &main_thread_result));
test_error(errnum, "pthread_join");
test_assert(expected_result == &main_thread); test_assertx(expected_result == &main_thread);
exit(0); exit(0);
} }
int main(void) int main(void)
{ {
int errnum;
main_thread = pthread_self(); main_thread = pthread_self();
void* expected_result = &main_thread; void* expected_result = &main_thread;
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, expected_result)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine,
test_error(errnum, "pthread_create"); expected_result));
pthread_exit(expected_result); pthread_exit(expected_result);
} }

View File

@ -40,18 +40,14 @@ void* thread_routine(void* ctx)
int main(void) int main(void)
{ {
int errnum;
pthread_once(&init_counter_once, init_counter_increase); pthread_once(&init_counter_once, init_counter_increase);
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL));
test_error(errnum, "pthread_create");
if ( (errnum = pthread_join(thread, NULL)) ) test_assertp(pthread_join(thread, NULL));
test_error(errnum, "pthread_join");
test_assert(init_counter == 1); test_assertx(init_counter == 1);
return 0; return 0;
} }

View File

@ -27,7 +27,7 @@ void* thread_routine(void* main_thread_ptr)
{ {
pthread_t main_thread = *(pthread_t*) 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(); child_thread_self = pthread_self();
@ -36,20 +36,16 @@ void* thread_routine(void* main_thread_ptr)
int main(void) int main(void)
{ {
int errnum;
pthread_t main_thread = pthread_self(); pthread_t main_thread = pthread_self();
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, &main_thread)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine, &main_thread));
test_error(errnum, "pthread_create");
if ( (errnum = pthread_join(thread, NULL)) ) test_assertp(pthread_join(thread, NULL));
test_error(errnum, "pthread_join");
test_assert(!pthread_equal(thread, pthread_self())); test_assertx(!pthread_equal(thread, pthread_self()));
test_assert(pthread_equal(thread, child_thread_self)); test_assertx(pthread_equal(thread, child_thread_self));
test_assert(!pthread_equal(pthread_self(), child_thread_self)); test_assertx(!pthread_equal(pthread_self(), child_thread_self));
return 0; return 0;
} }

View File

@ -27,7 +27,7 @@ void* thread_routine(void* ctx)
{ {
(void) ctx; (void) ctx;
test_assert(tls_variable == 42); test_assertx(tls_variable == 42);
tls_variable = 9001; tls_variable = 9001;
@ -36,20 +36,16 @@ void* thread_routine(void* ctx)
int main(void) int main(void)
{ {
int errnum; test_assertx(tls_variable == 42);
test_assert(tls_variable == 42);
tls_variable = 1337; tls_variable = 1337;
pthread_t thread; pthread_t thread;
if ( (errnum = pthread_create(&thread, NULL, &thread_routine, NULL)) ) test_assertp(pthread_create(&thread, NULL, &thread_routine, NULL));
test_error(errnum, "pthread_create");
if ( (errnum = pthread_join(thread, NULL)) ) test_assertp(pthread_join(thread, NULL));
test_error(errnum, "pthread_join");
test_assert(tls_variable == 1337); test_assertx(tls_variable == 1337);
return 0; return 0;
} }

View File

@ -29,7 +29,7 @@ void signal_handler(int signum, siginfo_t* siginfo, void* ucontext_ptr)
(void) siginfo; (void) siginfo;
(void) ucontext_ptr; (void) ucontext_ptr;
test_assert(signum == SIGUSR1); test_assertx(signum == SIGUSR1);
signal_counter++; signal_counter++;
} }
@ -41,12 +41,11 @@ int main(void)
sa.sa_sigaction = signal_handler; sa.sa_sigaction = signal_handler;
sa.sa_flags = SA_SIGINFO; sa.sa_flags = SA_SIGINFO;
if ( sigaction(SIGUSR1, &sa, NULL) ) test_assert(sigaction(SIGUSR1, &sa, NULL) == 0);
test_error(errno, "sigaction(USR1)");
raise(SIGUSR1); raise(SIGUSR1);
test_assert(signal_counter == 1); test_assertx(signal_counter == 1);
return 0; return 0;
} }

View File

@ -50,10 +50,9 @@ static void test(bool test_read,
char c; char c;
if ( test_read ) if ( test_read )
{ {
ssize_t amount = read(fds[0], &c, 1); ssize_t amount;
if ( amount < 0 ) test_assert(0 <= (amount = read(fds[0], &c, 1)));
test_error(errno, "read"); test_assertx(amount == 0);
test_assert(amount == 0);
_exit(0); _exit(0);
} }
else else
@ -64,8 +63,8 @@ static void test(bool test_read,
ssize_t amount = write(fds[0], &c, 1); ssize_t amount = write(fds[0], &c, 1);
if ( !sigpipe && amount == -1 && errno == EPIPE ) if ( !sigpipe && amount == -1 && errno == EPIPE )
_exit(0); _exit(0);
if ( amount != 1 ) test_assert(0 <= amount);
test_error(errno, "write"); test_assertx(amount == 1);
} }
} }
} }
@ -82,9 +81,9 @@ static void test(bool test_read,
int status; int status;
test_assert(waitpid(child_pid, &status, 0) == child_pid); test_assert(waitpid(child_pid, &status, 0) == child_pid);
if ( test_read || !sigpipe ) if ( test_read || !sigpipe )
test_assert(WIFEXITED(status) && WEXITSTATUS(status) == 0); test_assertx(WIFEXITED(status) && WEXITSTATUS(status) == 0);
else else
test_assert(WIFSIGNALED(status) && WTERMSIG(status) == SIGPIPE); test_assertx(WIFSIGNALED(status) && WTERMSIG(status) == SIGPIPE);
if ( parent_shutdown || child_shutdown ) if ( parent_shutdown || child_shutdown )
close(fds[1]); close(fds[1]);
} }

View File

@ -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 * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -20,17 +20,14 @@
#ifndef TEST_H #ifndef TEST_H
#define TEST_H #define TEST_H
#undef NDEBUG
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define test_assert(x) assert(x) __attribute((noreturn, unused)) static inline
__attribute__((noreturn))
void test_error(int errnum, const char* format, ...) void test_error(int errnum, const char* format, ...)
{ {
fprintf(stderr, "%s: ", program_invocation_name); fprintf(stderr, "%s: ", program_invocation_name);
@ -47,4 +44,31 @@ void test_error(int errnum, const char* format, ...)
exit(1); 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 #endif