00001 #ifndef UNITTEST_CHECKMACROS_H
00002 #define UNITTEST_CHECKMACROS_H
00003
00004 #include "Checks.h"
00005 #include "AssertException.h"
00006 #include "MemoryOutStream.h"
00007 #include "TestDetails.h"
00008 #include "CurrentTest.h"
00009
00010 #ifdef CHECK
00011 #error UnitTest++ redefines CHECK
00012 #endif
00013
00014 #ifdef CHECK_EQUAL
00015 #error UnitTest++ redefines CHECK_EQUAL
00016 #endif
00017
00018 #ifdef CHECK_CLOSE
00019 #error UnitTest++ redefines CHECK_CLOSE
00020 #endif
00021
00022 #ifdef CHECK_ARRAY_EQUAL
00023 #error UnitTest++ redefines CHECK_ARRAY_EQUAL
00024 #endif
00025
00026 #ifdef CHECK_ARRAY_CLOSE
00027 #error UnitTest++ redefines CHECK_ARRAY_CLOSE
00028 #endif
00029
00030 #ifdef CHECK_ARRAY2D_CLOSE
00031 #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE
00032 #endif
00033
00034 #define CHECK(value) \
00035 do \
00036 { \
00037 try { \
00038 if (!UnitTest::Check(value)) \
00039 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
00040 } \
00041 catch (...) { \
00042 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
00043 "Unhandled exception in CHECK(" #value ")"); \
00044 } \
00045 } while (0)
00046
00047 #define CHECK_EQUAL(expected, actual) \
00048 do \
00049 { \
00050 try { \
00051 UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
00052 } \
00053 catch (...) { \
00054 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
00055 "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
00056 } \
00057 } while (0)
00058
00059 #define CHECK_CLOSE(expected, actual, tolerance) \
00060 do \
00061 { \
00062 try { \
00063 UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
00064 } \
00065 catch (...) { \
00066 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
00067 "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
00068 } \
00069 } while (0)
00070
00071 #define CHECK_ARRAY_EQUAL(expected, actual, count) \
00072 do \
00073 { \
00074 try { \
00075 UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
00076 } \
00077 catch (...) { \
00078 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
00079 "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
00080 } \
00081 } while (0)
00082
00083 #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \
00084 do \
00085 { \
00086 try { \
00087 UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
00088 } \
00089 catch (...) { \
00090 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
00091 "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
00092 } \
00093 } while (0)
00094
00095 #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \
00096 do \
00097 { \
00098 try { \
00099 UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
00100 } \
00101 catch (...) { \
00102 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
00103 "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
00104 } \
00105 } while (0)
00106
00107
00108 #define CHECK_THROW(expression, ExpectedExceptionType) \
00109 do \
00110 { \
00111 bool caught_ = false; \
00112 try { expression; } \
00113 catch (ExpectedExceptionType const&) { caught_ = true; } \
00114 catch (...) {} \
00115 if (!caught_) \
00116 UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
00117 } while(0)
00118
00119 #define CHECK_ASSERT(expression) \
00120 CHECK_THROW(expression, UnitTest::AssertException);
00121
00122 #endif