AGX Dynamics  2.35.0.1
UnitMacros.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXUNIT_UNITMACROS_H
18 #define AGXUNIT_UNITMACROS_H
19 
20 #include <agx/config/AGX_USE_UNIT_TESTS.h>
21 #include <agx/Interrupt.h>
22 #include <agx/agxPhysics_export.h>
23 #include <agxIO/ArgumentParser.h>
24 
25 namespace agxUnit
26 {
27  // Query functions that can be used even when not building with unit tests enabled.
28 
31  {
32  const bool unitTest = arguments->find("--unittest") != -1;
33  const bool unitTestShort = arguments->find("-u") != -1;
34  const bool sanityTest = arguments->find("--isLuaFileSanityUnitTest") != -1;
35  return unitTest || unitTestShort || sanityTest;
36  }
37 
38 
39 
41  bool inline haveUnittestingArguments(int argc, char** argv)
42  {
43  auto haveArgument = [argc, argv](const char* key)
44  {
45  auto begin = argv;
46  auto end = argv + argc;
47  auto strequal = [key](const char* arg) { return strcmp(arg, key) == 0; };
48  auto it = std::find_if(begin, end, strequal);
49  return it != end;
50  };
51 
52  const bool unitTest = haveArgument("--unittest");
53  const bool unitTestShort = haveArgument("-u");
54  const bool sanityTest = haveArgument("--isLuaFileSanityUnitTest");
55  return unitTest || unitTestShort || sanityTest;
56  }
57 }
58 
59 
60 #if AGX_USE_UNIT_TESTS()
61 #include <agxUnit/agxUnit.h>
62 
63 
64 #ifdef _WIN32
65 #define AGXUNIT_FUNCTION __FUNCTION__
66 #else
67 #define AGXUNIT_FUNCTION __PRETTY_FUNCTION__
68 #endif
69 
70 namespace agxUnit {
72  void inline initUnittestFrameWork(agxIO::ArgumentParser* arguments)
73  {
74  agxUnit::UnitTestManager::instance()->parseArgs( arguments );
75  }
76 
77 
79  void inline initUnittestFrameWork(int argc, char** argv)
80  {
81  agxUnit::UnitTestManager::instance()->parseArgs( argc, argv );
82  }
83 
84 
86  bool inline isUnittestingEnabled()
87  {
88  return agxUnit::UnitTestManager::instance()->getEnable();
89  }
90 
92  bool inline shouldOnlyFastUnittestsBeRun()
93  {
94  return agxUnit::UnitTestManager::instance()->getDoOnlyFastTests();
95  }
96 }
97 
98 #define AGXUNIT_POS_ARGS __LINE__,__FILE__, __FUNCTION__
99 #define AGXUNIT_SOURCECODEINFO() agxUnit::SourceCodeInfo( AGXUNIT_POS_ARGS )
100 
101 
102 #define AGXUNIT_BEGIN_SCOPE() if (1)
103 #define AGXUNIT_END_SCOPE() endif
104 
105 #define AGXUNIT_PRINT_TEST_NAME() LOGGER_WARNING() << LOGGER_STATE(agx::Notify::PRINT_NONE) << "Running test " << __FUNCTION__ << "." << std::endl << LOGGER_END()
106 
107 
108 #define AGXUNIT_ASSERT(condition) \
109  ( agxUnit::Assert::failIf( !(condition), \
110  agxUnit::AssertMessage( "agxAssertion failed", \
111  "Expression: " #condition), \
112  AGXUNIT_SOURCECODEINFO() ) )
113 
114 
122 #define AGXUNIT_ASSERT_MESSAGE(message,condition) \
123  ( agxUnit::Assert::failIf( !(condition), \
124  agxUnit::AssertMessage( "agxAssertion failed", \
125  "Expression: " \
126 #condition, \
127  message ), \
128  AGXUNIT_SOURCECODEINFO() ) )
129 
134 #define AGXUNIT_FAIL( message ) \
135  ( agxUnit::Assert::fail( agxUnit::AssertMessage( "forced failure", \
136  message ), \
137  AGXUNIT_SOURCECODEINFO() ) )
138 
139 
156 #define AGXUNIT_ASSERT_EQUAL(expected,actual) \
157  ( agxUnit::agxAssertEquals( (expected), \
158  (actual), \
159  AGXUNIT_SOURCECODEINFO() ) )
160 
179 #define AGXUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
180  ( agxUnit::agxAssertEquals( (expected), \
181  (actual), \
182  AGXUNIT_SOURCECODEINFO(), \
183  (message) ) )
184 
185 
196 #define AGXUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
197  ( agxUnit::agxAssertDoubleEquals( (expected), \
198  (actual), \
199  (delta), \
200  AGXUNIT_SOURCECODEINFO(), \
201  "" ) )
202 
203 
209 #define AGXUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
210  ( agxUnit::agxAssertDoubleEquals( (expected), \
211  (actual), \
212  (delta), \
213  AGXUNIT_SOURCECODEINFO(), \
214  (message) ) )
215 
221 #define AGXUNIT_ASSERT_DOUBLES_NOT_EQUAL_MESSAGE(message,expected,actual,delta) \
222  ( agxUnit::agxAssertDoubleNotEquals( (expected), \
223  (actual), \
224  (delta), \
225  AGXUNIT_SOURCECODEINFO(), \
226  (message) ) )
227 
228 #define AGXUNIT_TRY_END(X) } \
229  catch (agx::InterruptException&) \
230  { \
231  agx::shutdown(); \
232  exit(1); \
233  } \
234  catch (agxUnit::Error& e) \
235  { \
236  agxUnit::AssertMessage msg("Error while running tests:", "" ); \
237  msg.addMessage( agx::String("What: ") + e.what() ); \
238  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
239  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
240  agxThrow e; \
241  return X;\
242  } \
243  catch (agxUnit::Exception& e) \
244  { \
245  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
246  agxThrow e; \
247  return X;\
248  } \
249  catch (std::exception& e) { \
250  agxUnit::AssertMessage msg("Caught std::exception", typeid(e).name() ); \
251  msg.addMessage( agx::String("What: ") + e.what() ); \
252  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
253  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
254  agxThrow e; \
255  return X; \
256  }\
257 catch (...) { \
258  agxUnit::AssertMessage msg("Caught undefined exception", "" ); \
259  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
260  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
261  agxThrow std::exception(); \
262  return X; \
263  }
264 
265 
266 #define AGXUNIT_TRY_AND_CONTINUE_END() } \
267  catch (agxUnit::Error& e) \
268  { \
269  agxUnit::AssertMessage msg("Error while running tests:", "" ); \
270  msg.addMessage( agx::String("What: ") + e.what() ); \
271  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
272  \
273  } \
274  catch (agxUnit::Exception& ) \
275  { \
276  \
277  } \
278  catch (std::exception& e) { \
279  agxUnit::AssertMessage msg("Caught std::exception", typeid(e).name() ); \
280  msg.addMessage( agx::String("What: ") + e.what() ); \
281  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
282  \
283  }\
284  catch (...) { \
285  agxUnit::AssertMessage msg("Caught undefined exception", "" ); \
286  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
287  \
288  }
289 
290 #define AGXUNIT_TRY_BEGIN() try {
291 
292 #define AGXUNIT_BEGIN_TEST_GROUP(X)
293 
294 #define AGXUNIT_END_TEST_GROUP(X)
295 
296 #define AGXUNIT_BEGIN_TEST(X)
297 #define AGXUNIT_END_TEST(X)
298 
299 
300 #else // AGX_USE_UNIT_TESTS()
301 
302 namespace agxUnit {
304  void inline initUnittestFrameWork(agxIO::ArgumentParser* /*arguments*/)
305  {
306  }
307 
308 
310  void inline initUnittestFrameWork(int /*argc*/, char** /*argv*/)
311  {
312  }
313 
314 
316  bool inline isUnittestingEnabled()
317  {
318  return false;
319  }
320 
321 
324  {
325  return false;
326  }
327 }
328 
330 #define AGXUNIT_IF_HACK #if
331 #define AGXUNIT_ENDIF_HACK #endif
332 #define AGXUNIT_BEGIN_SCOPE() AGXUNIT_IF_HACK 0
333 #define AGXUNIT_END_SCOPE() AGXUNIT_ENDIF_HACK
334 
336 #define AGXUNIT_TRY_END(X)
337 
338 #define AGXUNIT_TRY_AND_CONTINUE_END()
339 
341 #define AGXUNIT_TRY_BEGIN()
342 
344 #define AGXUNIT_BEGIN_TEST_GROUP(X)
345 
347 #define AGXUNIT_END_TEST_GROUP(X)
348 
350 #define AGXUNIT_BEGIN_TEST(X)
351 
356 #define AGXUNIT_END_TEST(X)
357 
359 #define AGXUNIT_ASSERT(condition)
361 #define AGXUNIT_ASSERT_MESSAGE(message,condition)
363 #define AGXUNIT_FAIL( message )
365 #define AGXUNIT_ASSERT_EQUAL(expected,actual)
367 #define AGXUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)
369 #define AGXUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)
371 #define AGXUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta)
372 
373 #define AGXUNIT_EMPTY_APPLICATION() \
374  int main() \
375 { \
376 \
377  return 1; \
378 }
379 
380 
381 #endif // AGX_USE_UNIT_TESTS()
382 #endif // AGXUNIT_UNITMACROS_H
A small argument parser class.
int find(const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
Finds the position of an argument.
bool isUnittestingEnabled()
Is unit testing enabled? (Has to be both built and initialized).
Definition: UnitMacros.h:316
void initUnittestFrameWork(agxIO::ArgumentParser *)
Initializes the unit testing framework from an argument parser.
Definition: UnitMacros.h:304
bool haveUnittestingArguments(agxIO::ArgumentParser *arguments)
Definition: UnitMacros.h:30
bool shouldOnlyFastUnittestsBeRun()
Should only fast unit tests be run?
Definition: UnitMacros.h:323
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)