| 1 | #ifndef UNITTEST_CHECKMACROS_H | 
|---|
| 2 | #define UNITTEST_CHECKMACROS_H | 
|---|
| 3 |  | 
|---|
| 4 | #include "Checks.h" | 
|---|
| 5 | #include "AssertException.h" | 
|---|
| 6 | #include "MemoryOutStream.h" | 
|---|
| 7 | #include "TestDetails.h" | 
|---|
| 8 | #include "CurrentTest.h" | 
|---|
| 9 |  | 
|---|
| 10 | #ifdef CHECK | 
|---|
| 11 | #error UnitTest++ redefines CHECK | 
|---|
| 12 | #endif | 
|---|
| 13 |  | 
|---|
| 14 | #ifdef CHECK_EQUAL | 
|---|
| 15 | #error UnitTest++ redefines CHECK_EQUAL | 
|---|
| 16 | #endif | 
|---|
| 17 |  | 
|---|
| 18 | #ifdef CHECK_CLOSE | 
|---|
| 19 | #error UnitTest++ redefines CHECK_CLOSE | 
|---|
| 20 | #endif | 
|---|
| 21 |  | 
|---|
| 22 | #ifdef CHECK_ARRAY_EQUAL | 
|---|
| 23 | #error UnitTest++ redefines CHECK_ARRAY_EQUAL | 
|---|
| 24 | #endif | 
|---|
| 25 |  | 
|---|
| 26 | #ifdef CHECK_ARRAY_CLOSE | 
|---|
| 27 | #error UnitTest++ redefines CHECK_ARRAY_CLOSE | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 | #ifdef CHECK_ARRAY2D_CLOSE | 
|---|
| 31 | #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 34 | #define CHECK(value) \ | 
|---|
| 35 | do \ | 
|---|
| 36 | { \ | 
|---|
| 37 | try { \ | 
|---|
| 38 | if (!UnitTest::Check(value)) \ | 
|---|
| 39 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ | 
|---|
| 40 | } \ | 
|---|
| 41 | catch (...) { \ | 
|---|
| 42 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ | 
|---|
| 43 | "Unhandled exception in CHECK(" #value ")"); \ | 
|---|
| 44 | } \ | 
|---|
| 45 | } while (0) | 
|---|
| 46 |  | 
|---|
| 47 | #define CHECK_EQUAL(expected, actual) \ | 
|---|
| 48 | do \ | 
|---|
| 49 | { \ | 
|---|
| 50 | try { \ | 
|---|
| 51 | UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ | 
|---|
| 52 | } \ | 
|---|
| 53 | catch (...) { \ | 
|---|
| 54 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ | 
|---|
| 55 | "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \ | 
|---|
| 56 | } \ | 
|---|
| 57 | } while (0) | 
|---|
| 58 |  | 
|---|
| 59 | #define CHECK_CLOSE(expected, actual, tolerance) \ | 
|---|
| 60 | do \ | 
|---|
| 61 | { \ | 
|---|
| 62 | try { \ | 
|---|
| 63 | UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ | 
|---|
| 64 | } \ | 
|---|
| 65 | catch (...) { \ | 
|---|
| 66 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ | 
|---|
| 67 | "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ | 
|---|
| 68 | } \ | 
|---|
| 69 | } while (0) | 
|---|
| 70 |  | 
|---|
| 71 | #define CHECK_ARRAY_EQUAL(expected, actual, count) \ | 
|---|
| 72 | do \ | 
|---|
| 73 | { \ | 
|---|
| 74 | try { \ | 
|---|
| 75 | UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ | 
|---|
| 76 | } \ | 
|---|
| 77 | catch (...) { \ | 
|---|
| 78 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ | 
|---|
| 79 | "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \ | 
|---|
| 80 | } \ | 
|---|
| 81 | } while (0) | 
|---|
| 82 |  | 
|---|
| 83 | #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \ | 
|---|
| 84 | do \ | 
|---|
| 85 | { \ | 
|---|
| 86 | try { \ | 
|---|
| 87 | UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ | 
|---|
| 88 | } \ | 
|---|
| 89 | catch (...) { \ | 
|---|
| 90 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ | 
|---|
| 91 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ | 
|---|
| 92 | } \ | 
|---|
| 93 | } while (0) | 
|---|
| 94 |  | 
|---|
| 95 | #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \ | 
|---|
| 96 | do \ | 
|---|
| 97 | { \ | 
|---|
| 98 | try { \ | 
|---|
| 99 | UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ | 
|---|
| 100 | } \ | 
|---|
| 101 | catch (...) { \ | 
|---|
| 102 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ | 
|---|
| 103 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ | 
|---|
| 104 | } \ | 
|---|
| 105 | } while (0) | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | #define CHECK_THROW(expression, ExpectedExceptionType) \ | 
|---|
| 109 | do \ | 
|---|
| 110 | { \ | 
|---|
| 111 | bool caught_ = false; \ | 
|---|
| 112 | try { expression; } \ | 
|---|
| 113 | catch (ExpectedExceptionType const&) { caught_ = true; } \ | 
|---|
| 114 | catch (...) {} \ | 
|---|
| 115 | if (!caught_) \ | 
|---|
| 116 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \ | 
|---|
| 117 | } while(0) | 
|---|
| 118 |  | 
|---|
| 119 | #define CHECK_ASSERT(expression) \ | 
|---|
| 120 | CHECK_THROW(expression, UnitTest::AssertException); | 
|---|
| 121 |  | 
|---|
| 122 | #endif | 
|---|