AGX Dynamics  2.37.2.0
UnitMacros.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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("--isFileSanityUnitTest") != -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("--isFileSanityUnitTest");
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 isDeterminismTest()
93  {
94  return agxUnit::UnitTestManager::instance()->getIsDeterminismTest();
95  }
96 
97 
99  bool inline shouldOnlyFastUnittestsBeRun()
100  {
101  return agxUnit::UnitTestManager::instance()->getDoOnlyFastTests();
102  }
103 }
104 
105 #define AGXUNIT_POS_ARGS __LINE__,__FILE__, __FUNCTION__
106 #define AGXUNIT_SOURCECODEINFO() agxUnit::SourceCodeInfo( AGXUNIT_POS_ARGS )
107 
108 
109 #define AGXUNIT_BEGIN_SCOPE() if (1)
110 #define AGXUNIT_END_SCOPE() endif
111 
112 #define AGXUNIT_PRINT_TEST_NAME() LOGGER_WARNING() << LOGGER_STATE(agx::Notify::PRINT_NONE) << "Running test " << __FUNCTION__ << "." << std::endl << LOGGER_END()
113 
114 
115 #define AGXUNIT_ASSERT(condition) \
116  ( agxUnit::Assert::failIf( !(condition), \
117  agxUnit::AssertMessage( "agxAssertion failed", \
118  "Expression: " #condition), \
119  AGXUNIT_SOURCECODEINFO() ) )
120 
121 
129 #define AGXUNIT_ASSERT_MESSAGE(message,condition) \
130  ( agxUnit::Assert::failIf( !(condition), \
131  agxUnit::AssertMessage( "agxAssertion failed", \
132  "Expression: " \
133 #condition, \
134  message ), \
135  AGXUNIT_SOURCECODEINFO() ) )
136 
141 #define AGXUNIT_FAIL( message ) \
142  ( agxUnit::Assert::fail( agxUnit::AssertMessage( "forced failure", \
143  message ), \
144  AGXUNIT_SOURCECODEINFO() ) )
145 
146 
163 #define AGXUNIT_ASSERT_EQUAL(expected,actual) \
164  ( agxUnit::agxAssertEquals( (expected), \
165  (actual), \
166  AGXUNIT_SOURCECODEINFO() ) )
167 
186 #define AGXUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
187  ( agxUnit::agxAssertEquals( (expected), \
188  (actual), \
189  AGXUNIT_SOURCECODEINFO(), \
190  (message) ) )
191 
192 
203 #define AGXUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
204  ( agxUnit::agxAssertDoubleEquals( (expected), \
205  (actual), \
206  (delta), \
207  AGXUNIT_SOURCECODEINFO(), \
208  "" ) )
209 
210 
216 #define AGXUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
217  ( agxUnit::agxAssertDoubleEquals( (expected), \
218  (actual), \
219  (delta), \
220  AGXUNIT_SOURCECODEINFO(), \
221  (message) ) )
222 
228 #define AGXUNIT_ASSERT_DOUBLES_NOT_EQUAL_MESSAGE(message,expected,actual,delta) \
229  ( agxUnit::agxAssertDoubleNotEquals( (expected), \
230  (actual), \
231  (delta), \
232  AGXUNIT_SOURCECODEINFO(), \
233  (message) ) )
234 
235 
236 #define AGXUNIT_ASSERT_VEC3S_EQUAL_MESSAGE(message, expected, actual, delta) \
237  (agxUnit::agxAssertVec3sEqual((expected), (actual), (delta), AGXUNIT_SOURCECODEINFO(), (message)))
238 
239 #define AGXUNIT_ASSERT_VEC3S_NOT_EQUAL_MESSAGE(message, expected, actual, delta) \
240  (agxUnit::agxAssertVec3sNotEqual((expected), (actual), (delta), AGXUNIT_SOURCECODEINFO(), (message)))
241 
242 
243 #define AGXUNIT_TRY_END(X) } \
244  catch (agx::InterruptException&) \
245  { \
246  agx::shutdown(); \
247  exit(1); \
248  } \
249  catch (agxUnit::Error& e) \
250  { \
251  agxUnit::AssertMessage msg("Error while running tests:", "" ); \
252  msg.addMessage( agx::String("What: ") + e.what() ); \
253  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
254  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
255  agxThrow e; \
256  return X;\
257  } \
258  catch (agxUnit::Exception& e) \
259  { \
260  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
261  agxThrow e; \
262  return X;\
263  } \
264  catch (std::exception& e) { \
265  agxUnit::AssertMessage msg("Caught std::exception", typeid(e).name() ); \
266  msg.addMessage( agx::String("What: ") + e.what() ); \
267  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
268  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
269  agxThrow e; \
270  return X; \
271  }\
272 catch (...) { \
273  agxUnit::AssertMessage msg("Caught undefined exception", "" ); \
274  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
275  if (agxUnit::UnitTestManager::instance()->getPassOnExceptions()) \
276  agxThrow std::exception(); \
277  return X; \
278  }
279 
280 
281 #define AGXUNIT_TRY_AND_CONTINUE_END() } \
282  catch (agxUnit::Error& e) \
283  { \
284  agxUnit::AssertMessage msg("Error while running tests:", "" ); \
285  msg.addMessage( agx::String("What: ") + e.what() ); \
286  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
287  \
288  } \
289  catch (agxUnit::Exception& ) \
290  { \
291  \
292  } \
293  catch (std::exception& e) { \
294  agxUnit::AssertMessage msg("Caught std::exception", typeid(e).name() ); \
295  msg.addMessage( agx::String("What: ") + e.what() ); \
296  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
297  \
298  }\
299  catch (...) { \
300  agxUnit::AssertMessage msg("Caught undefined exception", "" ); \
301  agxUnit::UnitTestManager::instance()->reportError( msg, AGXUNIT_SOURCECODEINFO() ); \
302  \
303  }
304 
305 #define AGXUNIT_TRY_BEGIN() try {
306 
307 #define AGXUNIT_BEGIN_TEST_GROUP(X)
308 
309 #define AGXUNIT_END_TEST_GROUP(X)
310 
311 #define AGXUNIT_BEGIN_TEST(X)
312 #define AGXUNIT_END_TEST(X)
313 
314 
315 #else // AGX_USE_UNIT_TESTS()
316 
317 namespace agxUnit {
319  void inline initUnittestFrameWork(agxIO::ArgumentParser* /*arguments*/)
320  {
321  }
322 
323 
325  void inline initUnittestFrameWork(int /*argc*/, char** /*argv*/)
326  {
327  }
328 
329 
331  bool inline isUnittestingEnabled()
332  {
333  return false;
334  }
335 
336 
339  {
340  return false;
341  }
342 }
343 
345 #define AGXUNIT_IF_HACK #if
346 #define AGXUNIT_ENDIF_HACK #endif
347 #define AGXUNIT_BEGIN_SCOPE() AGXUNIT_IF_HACK 0
348 #define AGXUNIT_END_SCOPE() AGXUNIT_ENDIF_HACK
349 
351 #define AGXUNIT_TRY_END(X)
352 
353 #define AGXUNIT_TRY_AND_CONTINUE_END()
354 
356 #define AGXUNIT_TRY_BEGIN()
357 
359 #define AGXUNIT_BEGIN_TEST_GROUP(X)
360 
362 #define AGXUNIT_END_TEST_GROUP(X)
363 
365 #define AGXUNIT_BEGIN_TEST(X)
366 
371 #define AGXUNIT_END_TEST(X)
372 
374 #define AGXUNIT_ASSERT(condition)
375 
376 #define AGXUNIT_ASSERT_MESSAGE(message,condition)
377 
378 #define AGXUNIT_FAIL( message )
379 
380 #define AGXUNIT_ASSERT_EQUAL(expected,actual)
381 
382 #define AGXUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)
383 
384 #define AGXUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)
385 
386 #define AGXUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta)
387 
388 #define AGXUNIT_EMPTY_APPLICATION() \
389  int main() \
390 { \
391 \
392  return 1; \
393 }
394 
395 
396 #endif // AGX_USE_UNIT_TESTS()
397 #endif // AGXUNIT_UNITMACROS_H
bool shouldOnlyFastUnittestsBeRun()
Should only fast unit tests be run?
Definition: UnitMacros.h:338
bool isUnittestingEnabled()
Is unit testing enabled? (Has to be both built and initialized).
Definition: UnitMacros.h:331
void initUnittestFrameWork(agxIO::ArgumentParser *)
Initializes the unit testing framework from an argument parser.
Definition: UnitMacros.h:319
A small argument parser class.
int find(const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
Finds the position of an argument.
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
bool haveUnittestingArguments(agxIO::ArgumentParser *arguments)
Definition: UnitMacros.h:30