IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
UnitTestMacros.h
Go to the documentation of this file.
1 // UnitTestMacros.h:
3 // -----------------
4 //
14 
15 #ifndef __IPSDKUNITTESTTOOLS_UNITTESTMACROS_H__
16 #define __IPSDKUNITTESTTOOLS_UNITTESTMACROS_H__
17 
18 #include <IPSDKUtil/Tools/ProcessingResult.h>
20 
23 
26 #define IPSDK_UT_CHECK_BOOLRESULT(predicate) \
27  { \
28  BoolResult bRes = predicate; \
29  BOOST_CHECK_MESSAGE(bRes == true, bRes.getMsg()); \
30  }
31 
35 #define IPSDK_UT_CHECK_BOOLRESULT_FALSE(predicate) \
36  { \
37  BoolResult bRes = predicate; \
38  BOOST_CHECK_MESSAGE(bRes == false, bRes.getMsg()); \
39  }
40 
41 
44 #define IPSDK_UT_GET_LOG() \
45 boost::unit_test::unit_test_log_formatter* pLogFormatter = boost::unit_test::unit_test_log.get_formatter(boost::unit_test::OF_CUSTOM_LOGGER); \
46 ipsdk::unittest::UnitTestLog* pUnitTestLog = static_cast<ipsdk::unittest::UnitTestLog*>(pLogFormatter);
47 
50 #define IPSDK_UT_LOG_TEST_RES(strPrefix, boolRes) \
51 IPSDK_UT_GET_LOG() \
52 if(boolRes.getResult()) { \
53  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, strPrefix + " Success : " + boolRes.getMsg()); \
54 } \
55 else{ \
56  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, " Failure : " + boolRes.getMsg()); \
57 }
58 
63 #define IPSDK_UT_SUITE_START(UnitTestSuiteName) \
64 namespace { \
65  struct Fixture { \
66  Fixture() \
67  { \
68  boost::unit_test::unit_test_log_formatter* pLogFormatter = \
69  boost::unit_test::unit_test_log.get_formatter(boost::unit_test::OF_CUSTOM_LOGGER); \
70  pUnitTestLog = static_cast<ipsdk::unittest::UnitTestLog*>(pLogFormatter); \
71  } \
72  ~Fixture() {} \
73  ipsdk::unittest::UnitTestLog* pUnitTestLog; \
74  }; \
75 } \
76 BOOST_FIXTURE_TEST_SUITE(UnitTestSuiteName, Fixture) \
77 BOOST_AUTO_TEST_CASE(SuiteStart) \
78 { \
79  pUnitTestLog->notifyTestSuiteStart(BOOST_PP_STRINGIZE(UnitTestSuiteName)); \
80 }
81 
85 #define IPSDK_UT_SUITE_END() \
86 BOOST_AUTO_TEST_CASE(SuiteStop) \
87 { \
88  pUnitTestLog->notifyTestSuiteEnd(); \
89 } \
90 BOOST_AUTO_TEST_SUITE_END()
91 
94 #define IPSDK_UT_CASE_START(UnitTestCaseName) \
95 BOOST_AUTO_TEST_CASE(UnitTestCaseName) \
96 { \
97  pUnitTestLog->notifyTestCaseStart(BOOST_PP_STRINGIZE(UnitTestCaseName));
98 
101 #define IPSDK_UT_CASE_END() \
102  pUnitTestLog->notifyTestCaseEnd(); \
103 }
104 
107 #define IPSDK_UT_CHECK(P) \
108 do { \
109  IPSDK_UT_GET_LOG() \
110  if(P) { \
111  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : for ") + BOOST_PP_STRINGIZE(P)); \
112  } \
113  else { \
114  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : for " ) + BOOST_PP_STRINGIZE(P)); \
115  } \
116  BOOST_CHECK(P); \
117 } while(0)
118 
121 #define IPSDK_CHECK_MESSAGE(P, M) \
122 do { \
123  IPSDK_UT_GET_LOG() \
124  if(P) { \
125  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + M + " for " + BOOST_PP_STRINGIZE(P)); \
126  } \
127  else { \
128  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : ") + M + " for " + BOOST_PP_STRINGIZE(P)); \
129  } \
130  BOOST_CHECK_MESSAGE(P, M); \
131 } while(0)
132 
135 #define IPSDK_TEST_MESSAGE(M) \
136 do { \
137  IPSDK_UT_GET_LOG() \
138  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + M); \
139  BOOST_TEST_MESSAGE(M); \
140 } while(0)
141 
144 #define IPSDK_CHECK_EQUAL(V1, V2) \
145 do { \
146  IPSDK_UT_GET_LOG() \
147  if(V1 == V2) { \
148  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + BOOST_PP_STRINGIZE(V1 == V2)); \
149  } \
150  else { \
151  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : ") + BOOST_PP_STRINGIZE(V1 == V2)); \
152  } \
153  BOOST_CHECK_EQUAL(V1, V2); \
154 } while(0)
155 
158 #define IPSDK_CHECK_NE(V1, V2) \
159 do { \
160  IPSDK_UT_GET_LOG() \
161  if(V1 != V2) { \
162  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + \
163  std::to_string(V1) + " != " + std::to_string(V2)); \
164  } \
165  else { \
166  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : ") + \
167  std::to_string(V1) + " != " + std::to_string(V2)); \
168  } \
169  BOOST_CHECK_NE(V1, V2); \
170 } while(0)
171 
174 #define IPSDK_CHECK_NE_STR(V1, V2) \
175 do { \
176  IPSDK_UT_GET_LOG() \
177  if(V1 != V2) { \
178  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + V1 + " != " + V2); \
179  } \
180  else { \
181  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : ") + V1 + " != " + V2); \
182  } \
183  BOOST_CHECK_NE(V1, V2); \
184 } while(0)
185 
188 #define IPSDK_CHECK_LT(V1, V2) \
189 do { \
190  IPSDK_UT_GET_LOG() \
191  if(V1 < V2) { \
192  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + \
193  std::to_string(V1) + " < " + std::to_string(V2)); \
194  } \
195  else { \
196  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : ") + \
197  std::to_string(V1) + " < " + std::to_string(V2)); \
198  } \
199  BOOST_CHECK_LT(V1, V2); \
200 } while(0)
201 
204 #define IPSDK_CHECK_GE(V1, V2) \
205 do { \
206  IPSDK_UT_GET_LOG() \
207  if(V1 >= V2) { \
208  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + \
209  std::to_string(V1) + " >= " + std::to_string(V2)); \
210  } \
211  else { \
212  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : ") + \
213  std::to_string(V1) + " >= " + std::to_string(V2)); \
214  } \
215  BOOST_CHECK_GE(V1, V2); \
216 } while(0)
217 
221 #define IPSDK_CHECK_CLOSE(V1, V2, TOL) \
222 do { \
223  IPSDK_UT_GET_LOG() \
224  if((V1 != 0 ? std::abs((V1)-(V2)) / V1 * 100 : 0) < TOL) { \
225  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : Comparison between ") + \
226  BOOST_PP_STRINGIZE(V1) + "(" + std::to_string(V1) + ") and " + \
227  BOOST_PP_STRINGIZE(V2) + "(" + std::to_string(V2) + ") doesn't exceed " + \
228  std::to_string(TOL) + "%"); \
229  } \
230  else { \
231  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Failure : Comparison between ") + \
232  BOOST_PP_STRINGIZE(V1) + "(" + std::to_string(V1) + ") and " + \
233  BOOST_PP_STRINGIZE(V2) + "(" + std::to_string(V2) + ") doesn't exceed " + \
234  std::to_string(TOL) + "%"); \
235  } \
236  BOOST_CHECK_CLOSE(V1, V2, TOL); \
237 } while(0)
238 
241 #define IPSDK_CHECK_THROW(statement, exception) \
242 do { \
243  IPSDK_UT_GET_LOG() \
244  try { \
245  statement; \
246  } \
247  catch (...) { \
248  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, "Exception correctly caught"); \
249  BOOST_CHECK_THROW(statement, exception); \
250  } \
251 } while(0)
252 
255 #define IPSDK_CHECK_NO_THROW(statement) \
256 do { \
257  IPSDK_UT_GET_LOG() \
258  try { \
259  statement; \
260  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, "No Exception thrown"); \
261  } \
262  catch (const std::exception& e) { \
263  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string("Exception caught : ") + e.what()); \
264  BOOST_CHECK_NO_THROW(statement); \
265  } \
266 } while(0)
267 
270 #define IPSDK_REQUIRE(P) \
271 do { \
272  IPSDK_UT_GET_LOG() \
273  if(P) { \
274  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Check ") + BOOST_PP_STRINGIZE(P) + " has passed"); \
275  } \
276  else { \
277  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Check " ) + BOOST_PP_STRINGIZE(P) + " has passed"); \
278  } \
279  BOOST_REQUIRE(P); \
280 } while(0)
281 
284 #define IPSDK_REQUIRE_MESSAGE(P, M) \
285 do { \
286  IPSDK_UT_GET_LOG() \
287  if(P) { \
288  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Check ") + BOOST_PP_STRINGIZE(P) + " has passed :" + M); \
289  } \
290  else { \
291  pUnitTestLog->notifyTestFailure(__FILE__, __LINE__, std::string(" Check " ) + BOOST_PP_STRINGIZE(P) + " has passed:" + M); \
292  } \
293  BOOST_REQUIRE_MESSAGE(P, M); \
294 } while(0)
295 
296 
299 #define IPSDK_WARN_LT(V1, V2) \
300 do { \
301  IPSDK_UT_GET_LOG() \
302  if(V1 < V2) { \
303  pUnitTestLog->notifyTestSuccess(__FILE__, __LINE__, std::string(" Success : ") + \
304  std::to_string(V1) + " < " + std::to_string(V2)); \
305  } \
306  else { \
307  pUnitTestLog->notifyTestWarning(__FILE__, __LINE__, std::string(" Warning: ") + \
308  std::to_string(V1) + " < " + std::to_string(V2)); \
309  } \
310  BOOST_WARN_LT(V1, V2); \
311 } while(0)
312 
313 
316 
317 #endif // __IPSDKUNITTESTTOOLS_UNITTESTMACROS_H__
Predefined types for unit tests management.