| 182 | | # if defined(DEVEL_RELEASE) |
| 183 | | # error Unit testing not allowed in release |
| 184 | | # else |
| 185 | | |
| 186 | | # ifdef __cplusplus |
| 187 | | |
| 188 | | #ifndef _CPP_CSTDLIB |
| 189 | | #include <cstdlib> |
| 190 | | #endif |
| 191 | | |
| 192 | | namespace arb_test { |
| 193 | | class GlobalTestData { |
| 194 | | GlobalTestData() |
| 195 | | : show_warnings(true), |
| 196 | | assertion_failed(false), |
| 197 | | warnings(0) |
| 198 | | {} |
| 199 | | |
| 200 | | static GlobalTestData *instance(bool erase) { |
| 201 | | static GlobalTestData *data = 0; // singleton |
| 202 | | if (erase) { |
| 203 | | delete data; |
| 204 | | data = 0; |
| 205 | | } |
| 206 | | else { |
| 207 | | if (!data) data = new GlobalTestData; |
| 208 | | } |
| 209 | | return data; |
| 210 | | } |
| 211 | | |
| 212 | | public: |
| 213 | | bool show_warnings; |
| 214 | | bool assertion_failed; |
| 215 | | |
| 216 | | // counters |
| 217 | | size_t warnings; |
| 218 | | |
| 219 | | static GlobalTestData& get_instance() { return *instance(false); } |
| 220 | | static void erase_instance() { instance(true); } |
| 221 | | }; |
| 222 | | |
| 223 | | inline GlobalTestData& test_data() { return GlobalTestData::get_instance(); } |
| 224 | | }; |
| 225 | | |
| 226 | | # define ASSERTION_HAS_FAILED() arb_test::test_data().assertion_failed = true |
| 227 | | |
| 228 | | # else |
| 229 | | # define ASSERTION_HAS_FAILED() // impossible in C |
| 230 | | # endif |
| 231 | | |
| 232 | | # define test_assert(cond) \ |
| 233 | | do { \ |
| 234 | | if (!(cond)) { \ |
| 235 | | fflush(stdout); \ |
| 236 | | fflush(stderr); \ |
| 237 | | fprintf(stderr, "%s:%i: Assertion '%s' failed\n", \ |
| 238 | | __FILE__, __LINE__, #cond); \ |
| 239 | | fflush(stderr); \ |
| 240 | | ASSERTION_HAS_FAILED(); \ |
| 241 | | ARB_SIGSEGV(0); \ |
| 242 | | } \ |
| 243 | | } while(0) |
| 244 | | |
| 245 | | # if defined(ASSERTION_USED) |
| 246 | | # undef arb_assert |
| 247 | | # define arb_assert(cond) test_assert(cond) |
| 248 | | # endif |
| 249 | | |
| 250 | | # endif |
| | 182 | #ifndef TEST_GLOBAL_H |
| | 183 | #include <test_global.h> // overrides arb_assert()! |
| | 184 | #endif |