1 #ifndef UNITTEST_CHECKMACROS_H
2 #define UNITTEST_CHECKMACROS_H
3 
4 #include "HelperMacros.h"
5 #include "ExceptionMacros.h"
6 #include "Checks.h"
7 #include "AssertException.h"
8 #include "MemoryOutStream.h"
9 #include "TestDetails.h"
10 #include "CurrentTest.h"
11 #include "ReportAssertImpl.h"
12 
13 #ifdef CHECK
14     #error UnitTest++ redefines CHECK
15 #endif
16 
17 #ifdef CHECK_EQUAL
18 	#error UnitTest++ redefines CHECK_EQUAL
19 #endif
20 
21 #ifdef CHECK_CLOSE
22 	#error UnitTest++ redefines CHECK_CLOSE
23 #endif
24 
25 #ifdef CHECK_ARRAY_EQUAL
26 	#error UnitTest++ redefines CHECK_ARRAY_EQUAL
27 #endif
28 
29 #ifdef CHECK_ARRAY_CLOSE
30 	#error UnitTest++ redefines CHECK_ARRAY_CLOSE
31 #endif
32 
33 #ifdef CHECK_ARRAY2D_CLOSE
34 	#error UnitTest++ redefines CHECK_ARRAY2D_CLOSE
35 #endif
36 
37 #define CHECK(value) \
38 	UNITTEST_MULTILINE_MACRO_BEGIN \
39 	UT_TRY \
40 		({ \
41 			if (!UnitTest::Check(value)) \
42 				UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
43 		}) \
44 		UT_CATCH (std::exception, e, \
45 		{ \
46 			UnitTest::MemoryOutStream message; \
47 			message << "Unhandled exception (" << e.what() << ") in CHECK(" #value ")"; \
48 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
49 				message.GetText()); \
50 		}) \
51 		UT_CATCH_ALL \
52 		({ \
53 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
54 				"Unhandled exception in CHECK(" #value ")"); \
55 		}) \
56 	UNITTEST_MULTILINE_MACRO_END
57 
58 #define CHECK_EQUAL(expected, actual) \
59 	UNITTEST_MULTILINE_MACRO_BEGIN \
60         UT_TRY \
61 		({ \
62             UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
63         }) \
64 		UT_CATCH (std::exception, e, \
65 		{ \
66 			UnitTest::MemoryOutStream message; \
67 			message << "Unhandled exception (" << e.what() << ") in CHECK_EQUAL(" #expected ", " #actual ")"; \
68 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
69 				message.GetText()); \
70 		}) \
71         UT_CATCH_ALL \
72 		({ \
73             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
74                     "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
75         }) \
76 	UNITTEST_MULTILINE_MACRO_END
77 
78 #define CHECK_CLOSE(expected, actual, tolerance) \
79 	UNITTEST_MULTILINE_MACRO_BEGIN \
80         UT_TRY \
81 		({ \
82             UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
83         }) \
84 		UT_CATCH (std::exception, e, \
85 		{ \
86 			UnitTest::MemoryOutStream message; \
87 			message << "Unhandled exception (" << e.what() << ") in CHECK_CLOSE(" #expected ", " #actual ")"; \
88 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
89 				message.GetText()); \
90 		}) \
91         UT_CATCH_ALL \
92 		({ \
93             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
94                     "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
95         }) \
96 	UNITTEST_MULTILINE_MACRO_END
97 
98 #define CHECK_ARRAY_EQUAL(expected, actual, count) \
99 	UNITTEST_MULTILINE_MACRO_BEGIN \
100         UT_TRY \
101 		({ \
102             UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
103         }) \
104  		UT_CATCH (std::exception, e, \
105 		{ \
106 			UnitTest::MemoryOutStream message; \
107 			message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"; \
108 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
109 				message.GetText()); \
110 		}) \
111         UT_CATCH_ALL \
112 		({ \
113             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
114                     "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
115         }) \
116 	UNITTEST_MULTILINE_MACRO_END
117 
118 #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \
119 	UNITTEST_MULTILINE_MACRO_BEGIN \
120         UT_TRY \
121 		({ \
122             UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
123         }) \
124  		UT_CATCH (std::exception, e, \
125 		{ \
126 			UnitTest::MemoryOutStream message; \
127 			message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"; \
128 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
129 				message.GetText()); \
130 		}) \
131         UT_CATCH_ALL \
132 		({ \
133             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
134                     "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
135         }) \
136 	UNITTEST_MULTILINE_MACRO_END
137 
138 #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \
139 	UNITTEST_MULTILINE_MACRO_BEGIN \
140         UT_TRY \
141 		({ \
142             UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
143         }) \
144  		UT_CATCH (std::exception, e, \
145 		{ \
146 			UnitTest::MemoryOutStream message; \
147 			message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"; \
148 			UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
149 				message.GetText()); \
150 		}) \
151         UT_CATCH_ALL \
152 		({ \
153             UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
154                     "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"); \
155         }) \
156 	UNITTEST_MULTILINE_MACRO_END
157 
158 
159 // CHECK_THROW and CHECK_ASSERT only exist when UNITTEST_NO_EXCEPTIONS isn't defined (see config.h)
160 #ifndef UNITTEST_NO_EXCEPTIONS
161 #define CHECK_THROW(expression, ExpectedExceptionType) \
162 	UNITTEST_MULTILINE_MACRO_BEGIN \
163         bool caught_ = false; \
164         try { expression; } \
165         catch (ExpectedExceptionType const&) { caught_ = true; } \
166 		catch (...) {} \
167         if (!caught_) \
168 	        UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
169 	UNITTEST_MULTILINE_MACRO_END
170 
171 
172 #define CHECK_ASSERT(expression) \
173 	UNITTEST_MULTILINE_MACRO_BEGIN \
174 		UnitTest::Detail::ExpectAssert(true); \
175 		CHECK_THROW(expression, UnitTest::AssertException); \
176 		UnitTest::Detail::ExpectAssert(false); \
177 	UNITTEST_MULTILINE_MACRO_END
178 #endif
179 #endif
180