| | 29 | #if (UNIT_TESTS == 1) |
| | 30 | |
| | 31 | # if defined(DEVEL_RELEASE) |
| | 32 | # error Unit testing not allowed in release |
| | 33 | # endif |
| | 34 | |
| | 35 | # ifdef __cplusplus |
| | 36 | |
| | 37 | # define SET_ASSERTION_FAILED_FLAG() arb_test::test_data().assertion_failed = true |
| | 38 | # define PRINT_ASSERTION_FAILED_MSG(cond) arb_test::GlobalTestData::assertfailmsg(__FILE__, __LINE__, #cond) |
| | 39 | |
| | 40 | # else |
| | 41 | # define SET_ASSERTION_FAILED_FLAG() // impossible in C (assertions in C code will be handled like normal SEGV) |
| | 42 | # define PRINT_ASSERTION_FAILED_MSG(cond) \ |
| | 43 | do { \ |
| | 44 | fflush(stdout); \ |
| | 45 | fflush(stderr); \ |
| | 46 | fprintf(stderr, "%s:%i: Assertion '%s' failed [C]\n", \ |
| | 47 | __FILE__, __LINE__, #cond); \ |
| | 48 | fflush(stderr); \ |
| | 49 | } while(0) |
| | 50 | # endif |
| | 51 | |
| | 52 | # define TRIGGER_ASSERTION() \ |
| | 53 | do { \ |
| | 54 | SET_ASSERTION_FAILED_FLAG(); \ |
| | 55 | ARB_SIGSEGV(0); \ |
| | 56 | } while(0) |
| 57 | | |
| 58 | | class FlushedOutput { |
| 59 | | inline void flushall() { fflush(stdout); fflush(stderr); } |
| 60 | | public: |
| 61 | | FlushedOutput() { flushall(); } |
| 62 | | ~FlushedOutput() { flushall(); } |
| 63 | | |
| 64 | | #define VPRINTFORMAT(format) do { va_list parg; va_start(parg, format); vfprintf(stderr, format, parg); va_end(parg); } while(0) |
| 65 | | |
| 66 | | static void printf(const char *format, ...) __attribute__((format(printf, 1, 2))) { |
| 67 | | FlushedOutput yes; |
| 68 | | VPRINTFORMAT(format); |
| 69 | | } |
| 70 | | static void messagef(const char *filename, int lineno, const char *format, ...) __attribute__((format(printf, 3, 4))) { |
| 71 | | FlushedOutput yes; |
| 72 | | fprintf(stderr, "%s:%i: ", filename, lineno); |
| 73 | | fputc('\n', stderr); |
| 74 | | VPRINTFORMAT(format); |
| 75 | | } |
| 76 | | static void warningf(const char *filename, int lineno, const char *format, ...) __attribute__((format(printf, 3, 4))) { |
| 77 | | GlobalTestData& global = test_data(); |
| 78 | | if (global.show_warnings) { |
| 79 | | FlushedOutput yes; |
| 80 | | fprintf(stderr, "%s:%i: Warning: ", filename, lineno); |
| 81 | | VPRINTFORMAT(format); |
| 82 | | fputc('\n', stderr); |
| 83 | | global.warnings++; |
| 84 | | } |
| 85 | | } |
| 86 | | static void errorf(const char *filename, int lineno, const char *format, ...) __attribute__((format(printf, 3, 4))) { |
| 87 | | FlushedOutput yes; |
| 88 | | fprintf(stderr, "%s:%i: Error: ", filename, lineno); |
| 89 | | VPRINTFORMAT(format); |
| 90 | | fputc('\n', stderr); |
| 91 | | } |
| 92 | | #undef VPRINTFORMAT |
| 93 | | }; |
| | 107 | // -------------------------------------------------------------------------------- |