20 #error Need cstdarg included
23 #if !defined(_STDIO_H) && !defined(_STDIO_H_)
24 #error Need cstdio included
26 #if !defined(_STDLIB_H) && !defined(_STDLIB_H_)
27 #error Need cstdlib included
29 #if !defined(_ERRNO_H) && !defined(_SYS_ERRNO_H_)
30 #error Need cerrno included
32 #if !defined(_STRING_H) && !defined(_STRING_H_)
33 #error Need cstring included
35 #if !defined(CXXFORWARD_H)
36 #error Need cxxforward.h included
41 # if defined(DEVEL_RELEASE)
42 # error Unit testing not allowed in release
47 # define SET_ASSERTION_FAILED_FLAG() arb_test::test_data().assertion_failed = true
48 # define PRINT_ASSERTION_FAILED_MSG(cond) arb_test::GlobalTestData::assertfailmsg(__FILE__, __LINE__, #cond)
51 # define SET_ASSERTION_FAILED_FLAG() // impossible in C (assertions in C code will be handled like normal SEGV)
52 # define PRINT_ASSERTION_FAILED_MSG(cond) \
56 fprintf(stderr, "%s:%i: Assertion '%s' failed [C]\n", \
57 __FILE__, __LINE__, #cond); \
62 #define SEGV_INSIDE_TEST_STOP_OTHERWISE(backtrace) \
64 if (RUNNING_TEST()) { \
65 ARB_SIGSEGV(backtrace); \
68 ARB_STOP(backtrace); \
72 # define TRIGGER_ASSERTION(backtrace) \
74 SET_ASSERTION_FAILED_FLAG(); \
75 SEGV_INSIDE_TEST_STOP_OTHERWISE(backtrace); \
79 typedef enum { UNITTEST_MOCK } UnittestMock;
82 class FlushedOutputNoLF {
83 inline void flushall() {
fflush(stdout);
fflush(stderr); }
85 FlushedOutputNoLF() { flushall(); }
86 ~FlushedOutputNoLF() { flushall(); }
88 struct FlushedOutput :
public FlushedOutputNoLF {
89 ~FlushedOutput() {
fputc(
'\n', stderr); }
97 inline const char *fakeenv(
const char *var) {
99 if (strcmp(var,
"HOME") == 0)
return "./homefake";
104 class GlobalTestData {
105 typedef bool (*FlagCallback)(FlagAction action,
const char *name);
107 FlagCallback flag_cb;
114 assertion_failed(
false),
116 entered_mutex_loop(
false),
122 GlobalTestData(
const GlobalTestData&);
123 GlobalTestData& operator=(
const GlobalTestData&);
125 static GlobalTestData *instance(
bool erase) {
126 static GlobalTestData *data =
NULp;
132 static int allocation_count = 0;
136 data =
new GlobalTestData;
142 unsigned retry_count;
151 bool assertion_failed;
153 bool entered_mutex_loop;
158 void raiseLocalFlag(
const char *name)
const {
160 flag_cb(FLAG_RAISE, name);
163 fputs(
"cannot raise local flag (called from outside test-code?)\n", stderr);
166 void init(FlagCallback fc) { flag_cb = fc; }
168 bool not_covered_by_test()
const {
return !running_test; }
170 static GlobalTestData& get_instance() {
return *instance(
false); }
171 static void erase_instance() { instance(
true); }
173 void allow_retry(
unsigned count) {
174 if (retry_count == 0) {
178 unsigned allowed_retries()
const {
182 void annotate(
const char *annotation_) {
184 annotation = annotation_ ? strdup(annotation_) :
NULp;
186 const char *get_annotation()
const {
return annotation; }
188 static void print_annotation() {
189 char*& annotation = get_instance().annotation;
190 if (annotation) fprintf(stderr,
" (%s)", annotation);
193 static void assertfailmsg(
const char *filename,
int lineno,
const char *condition) {
195 fprintf(stderr,
"\n%s:%i: Assertion '%s' failed", filename, lineno, condition);
200 inline GlobalTestData& test_data() {
return GlobalTestData::get_instance(); }
205 #define TEST_ANNOTATE(annotation) arb_test::test_data().annotate(annotation)
206 #define TEST_ALLOW_RETRY(count) arb_test::test_data().allow_retry(count)
207 #define RUNNING_TEST() arb_test::test_data().running_test
210 #define test_assert(cond,backtrace) \
213 PRINT_ASSERTION_FAILED_MSG(cond); \
214 TRIGGER_ASSERTION(backtrace); \
224 # if defined(ASSERTION_USED)
226 # define arb_assert(cond) test_assert(cond, true)
229 #define UNCOVERED() test_assert(arb_test::test_data().not_covered_by_test(), false)
233 #error test_global.h may only be included if UNIT_TESTS is defined
237 #error test_global.h included twice
238 #endif // TEST_GLOBAL_H
void show_warnings(const string &helpfile)
fputs(TRACE_PREFIX, stderr)
static list< LineAttachedMessage > warnings
static ARB_init_perl_interface init