cppunitex.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #ifndef _CPPUNITEX_FBASICS_H
  7. #define _CPPUNITEX_FBASICS_H
  8. #ifdef NICE_USELIB_CPPUNIT
  9. #include <cppunit/extensions/HelperMacros.h>
  10. #include <core/basics/numerictools.h>
  11. /**
  12. * This is similar to CppUnit's built in CPPUNIT_ASSERT_MESSAGE,
  13. * but \c _STREAMABLE_ can be anything streamable to a std::stringstream
  14. * (including expressions like the following:
  15. * <code>"test failed with error " << error << "."</code>)
  16. */
  17. #define CPPUNIT_ASSERT_STREAMABLE(_CONDITION_,_STREAMABLE_) \
  18. if(!(_CONDITION_)) { \
  19. std::stringstream _MSG_; \
  20. _MSG_<<_STREAMABLE_<<std::endl; \
  21. CPPUNIT_NS::Asserter::fail(_MSG_.str() , CPPUNIT_SOURCELINE() ); }
  22. /**
  23. * CppUnit macro asserting that \c b is NOT NaN (Not a Number)
  24. */
  25. #define CPPUNIT_ASSERT_NOT_NAN(b) \
  26. CPPUNIT_ASSERT_MESSAGE("Value is NaN", !NICE::isNaN(b));
  27. /**
  28. * CppUnit macro asserting that \c b IS NaN (Not a Number)
  29. */
  30. #define CPPUNIT_ASSERT_IS_NAN(b) \
  31. CPPUNIT_ASSERT_MESSAGE("Value is not NaN", NICE::isNaN(b));
  32. /**
  33. * CppUnit macro asserting equality of doubles.
  34. */
  35. #define CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN_STREAMABLE(a,b,c,s) \
  36. { \
  37. bool uaieuaieOK = true; \
  38. if (!isZero(double(a) - double(b), double(c)) || NICE::isNaN(b)) { \
  39. uaieuaieOK = false; \
  40. } \
  41. CPPUNIT_ASSERT_STREAMABLE(uaieuaieOK, \
  42. "equality assertion failed. " << s << std::endl \
  43. << "- Expected: " << (a) << std::endl \
  44. << "- Actual : " << (b) << std::endl); \
  45. }
  46. /**
  47. * CppUnit macro asserting equality of doubles.
  48. */
  49. #define CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(a,b,c) \
  50. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN_STREAMABLE(a,b,c,"")
  51. /**
  52. * CppUnit macro asserting equality of double values.
  53. * This is like CppUnit's built in CPPUNIT_ASSERT_DOUBLES_EQUAL
  54. * extended by an explicit check that \c b is NOT NaN
  55. * (CPPUNIT_ASSERT_DOUBLES_EQUAL will NOT fail if \c b is NaN,
  56. * but most of the time, this IS an error).
  57. */
  58. /*
  59. #define CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(a,b,c) \
  60. CPPUNIT_ASSERT_NOT_NAN((b)); \
  61. CPPUNIT_ASSERT_DOUBLES_EQUAL((a),(b),(c));
  62. */
  63. /**
  64. * Like CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN, but here \c c is \b relative
  65. * to \c a.
  66. */
  67. #define CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN_REL(a,b,c) \
  68. CPPUNIT_ASSERT_NOT_NAN((b)); \
  69. CPPUNIT_ASSERT_DOUBLES_EQUAL((a),(b),fabs(a*c));
  70. /**
  71. * This is similar to CppUnit's built in CPPUNIT_ASSERT_EQUAL,
  72. * but streams the two parameters and compares the resulting strings.
  73. * Hence, any streamable objects can be compared.
  74. */
  75. #define CPPUNIT_ASSERT_EQUAL_STREAMED(a,b) \
  76. { \
  77. std::ostringstream str_a, str_b; \
  78. str_a << a; \
  79. str_b << b; \
  80. CPPUNIT_ASSERT_EQUAL(str_a.str(), str_b.str()); \
  81. }
  82. /**
  83. * This is similar to CppUnit's built in CPPUNIT_ASSERT_MESSAGE,
  84. * but \c _STREAMABLE_ can be anything streamable to a std::stringstream
  85. * (including expressions like the following:
  86. * <code>"test failed with error " << error << "."</code>)
  87. * @deprecated Use CPPUNIT_ASSERT_STREAMABLE instead
  88. */
  89. #define CPPUNIT_COND_STREAM(_COND_,_STREAM_) \
  90. CPPUNIT_ASSERT_STREAMABLE(_COND_,_STREAM_)
  91. #endif // NICE_USELIB_CPPUNIT
  92. #endif // _CPPUNITEX_FBASICS_H