Refactor testcase API.
This commit is contained in:
parent
590fd835d5
commit
d50b85591d
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
@ -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 <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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
|
||||
|
|
Loading…
Reference in New Issue