TestAssert.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #ifndef CPPUNIT_TESTASSERT_H
  2. #define CPPUNIT_TESTASSERT_H
  3. #include <cppunit/Portability.h>
  4. #include <cppunit/Exception.h>
  5. #include <cppunit/Asserter.h>
  6. #include <cppunit/portability/Stream.h>
  7. #include <stdio.h>
  8. #include <float.h> // For struct assertion_traits<double>
  9. CPPUNIT_NS_BEGIN
  10. /*! \brief Traits used by CPPUNIT_ASSERT_EQUAL().
  11. *
  12. * Here is an example of specialising these traits:
  13. *
  14. * \code
  15. * template<>
  16. * struct assertion_traits<std::string> // specialization for the std::string type
  17. * {
  18. * static bool equal( const std::string& x, const std::string& y )
  19. * {
  20. * return x == y;
  21. * }
  22. *
  23. * static std::string toString( const std::string& x )
  24. * {
  25. * std::string text = '"' + x + '"'; // adds quote around the string to see whitespace
  26. * OStringStream ost;
  27. * ost << text;
  28. * return ost.str();
  29. * }
  30. * };
  31. * \endcode
  32. */
  33. template <class T>
  34. struct assertion_traits
  35. {
  36. static bool equal( const T& x, const T& y )
  37. {
  38. return x == y;
  39. }
  40. static std::string toString( const T& x )
  41. {
  42. OStringStream ost;
  43. ost << x;
  44. return ost.str();
  45. }
  46. };
  47. /*! \brief Traits used by CPPUNIT_ASSERT_DOUBLES_EQUAL().
  48. *
  49. * This specialisation from @c struct @c assertion_traits<> ensures that
  50. * doubles are converted in full, instead of being rounded to the default
  51. * 6 digits of precision. Use the system defined ISO C99 macro DBL_DIG
  52. * within float.h is available to define the maximum precision, otherwise
  53. * use the hard-coded maximum precision of 15.
  54. */
  55. template <>
  56. struct assertion_traits<double>
  57. {
  58. static bool equal( double x, double y )
  59. {
  60. return x == y;
  61. }
  62. static std::string toString( double x )
  63. {
  64. #ifdef DBL_DIG
  65. const int precision = DBL_DIG;
  66. #else
  67. const int precision = 15;
  68. #endif // #ifdef DBL_DIG
  69. char buffer[128];
  70. #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
  71. sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
  72. #else
  73. sprintf(buffer, "%.*g", precision, x);
  74. #endif
  75. return buffer;
  76. }
  77. };
  78. /*! \brief (Implementation) Asserts that two objects of the same type are equals.
  79. * Use CPPUNIT_ASSERT_EQUAL instead of this function.
  80. * \sa assertion_traits, Asserter::failNotEqual().
  81. */
  82. template <class T>
  83. void assertEquals( const T& expected,
  84. const T& actual,
  85. SourceLine sourceLine,
  86. const std::string &message )
  87. {
  88. if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
  89. {
  90. Asserter::failNotEqual( assertion_traits<T>::toString(expected),
  91. assertion_traits<T>::toString(actual),
  92. sourceLine,
  93. message );
  94. }
  95. }
  96. /*! \brief (Implementation) Asserts that two double are equals given a tolerance.
  97. * Use CPPUNIT_ASSERT_DOUBLES_EQUAL instead of this function.
  98. * \sa Asserter::failNotEqual().
  99. * \sa CPPUNIT_ASSERT_DOUBLES_EQUAL for detailed semantic of the assertion.
  100. */
  101. void CPPUNIT_API assertDoubleEquals( double expected,
  102. double actual,
  103. double delta,
  104. SourceLine sourceLine,
  105. const std::string &message );
  106. /* A set of macros which allow us to get the line number
  107. * and file name at the point of an error.
  108. * Just goes to show that preprocessors do have some
  109. * redeeming qualities.
  110. */
  111. #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
  112. /** Assertions that a condition is \c true.
  113. * \ingroup Assertions
  114. */
  115. #define CPPUNIT_ASSERT(condition) \
  116. ( CPPUNIT_NS::Asserter::failIf( !(condition), \
  117. CPPUNIT_NS::Message( "assertion failed", \
  118. "Expression: " #condition), \
  119. CPPUNIT_SOURCELINE() ) )
  120. #else
  121. #define CPPUNIT_ASSERT(condition) \
  122. ( CPPUNIT_NS::Asserter::failIf( !(condition), \
  123. CPPUNIT_NS::Message( "assertion failed" ), \
  124. CPPUNIT_SOURCELINE() ) )
  125. #endif
  126. /** Assertion with a user specified message.
  127. * \ingroup Assertions
  128. * \param message Message reported in diagnostic if \a condition evaluates
  129. * to \c false.
  130. * \param condition If this condition evaluates to \c false then the
  131. * test failed.
  132. */
  133. #define CPPUNIT_ASSERT_MESSAGE(message,condition) \
  134. ( CPPUNIT_NS::Asserter::failIf( !(condition), \
  135. CPPUNIT_NS::Message( "assertion failed", \
  136. "Expression: " \
  137. #condition, \
  138. message ), \
  139. CPPUNIT_SOURCELINE() ) )
  140. /** Fails with the specified message.
  141. * \ingroup Assertions
  142. * \param message Message reported in diagnostic.
  143. */
  144. #define CPPUNIT_FAIL( message ) \
  145. ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
  146. message ), \
  147. CPPUNIT_SOURCELINE() ) )
  148. #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
  149. /// Generalized macro for primitive value comparisons
  150. #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
  151. ( CPPUNIT_NS::assertEquals( (expected), \
  152. (actual), \
  153. __LINE__, __FILE__ ) )
  154. #else
  155. /** Asserts that two values are equals.
  156. * \ingroup Assertions
  157. *
  158. * Equality and string representation can be defined with
  159. * an appropriate CppUnit::assertion_traits class.
  160. *
  161. * A diagnostic is printed if actual and expected values disagree.
  162. *
  163. * Requirement for \a expected and \a actual parameters:
  164. * - They are exactly of the same type
  165. * - They are serializable into a std::strstream using operator <<.
  166. * - They can be compared using operator ==.
  167. *
  168. * The last two requirements (serialization and comparison) can be
  169. * removed by specializing the CppUnit::assertion_traits.
  170. */
  171. #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
  172. ( CPPUNIT_NS::assertEquals( (expected), \
  173. (actual), \
  174. CPPUNIT_SOURCELINE(), \
  175. "" ) )
  176. /** Asserts that two values are equals, provides additional message on failure.
  177. * \ingroup Assertions
  178. *
  179. * Equality and string representation can be defined with
  180. * an appropriate assertion_traits class.
  181. *
  182. * A diagnostic is printed if actual and expected values disagree.
  183. * The message is printed in addition to the expected and actual value
  184. * to provide additional information.
  185. *
  186. * Requirement for \a expected and \a actual parameters:
  187. * - They are exactly of the same type
  188. * - They are serializable into a std::strstream using operator <<.
  189. * - They can be compared using operator ==.
  190. *
  191. * The last two requirements (serialization and comparison) can be
  192. * removed by specializing the CppUnit::assertion_traits.
  193. */
  194. #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
  195. ( CPPUNIT_NS::assertEquals( (expected), \
  196. (actual), \
  197. CPPUNIT_SOURCELINE(), \
  198. (message) ) )
  199. #endif
  200. /*! \brief Macro for primitive double value comparisons.
  201. * \ingroup Assertions
  202. *
  203. * The assertion pass if both expected and actual are finite and
  204. * \c fabs( \c expected - \c actual ) <= \c delta.
  205. * If either \c expected or actual are infinite (+/- inf), the
  206. * assertion pass if \c expected == \c actual.
  207. * If either \c expected or \c actual is a NaN (not a number), then
  208. * the assertion fails.
  209. */
  210. #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
  211. ( CPPUNIT_NS::assertDoubleEquals( (expected), \
  212. (actual), \
  213. (delta), \
  214. CPPUNIT_SOURCELINE(), \
  215. "" ) )
  216. /*! \brief Macro for primitive double value comparisons, setting a
  217. * user-supplied message in case of failure.
  218. * \ingroup Assertions
  219. * \sa CPPUNIT_ASSERT_DOUBLES_EQUAL for detailed semantic of the assertion.
  220. */
  221. #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
  222. ( CPPUNIT_NS::assertDoubleEquals( (expected), \
  223. (actual), \
  224. (delta), \
  225. CPPUNIT_SOURCELINE(), \
  226. (message) ) )
  227. /** Asserts that the given expression throws an exception of the specified type.
  228. * \ingroup Assertions
  229. * Example of usage:
  230. * \code
  231. * std::vector<int> v;
  232. * CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range );
  233. * \endcode
  234. */
  235. # define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
  236. CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
  237. expression, \
  238. ExceptionType )
  239. // implementation detail
  240. #if CPPUNIT_USE_TYPEINFO_NAME
  241. #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
  242. CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
  243. #else
  244. #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
  245. std::string( no_rtti_message )
  246. #endif // CPPUNIT_USE_TYPEINFO_NAME
  247. // implementation detail
  248. #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
  249. /** Asserts that the given expression throws an exception of the specified type,
  250. * setting a user supplied message in case of failure.
  251. * \ingroup Assertions
  252. * Example of usage:
  253. * \code
  254. * std::vector<int> v;
  255. * CPPUNIT_ASSERT_THROW_MESSAGE( "- std::vector<int> v;", v.at( 50 ), std::out_of_range );
  256. * \endcode
  257. */
  258. # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
  259. do { \
  260. bool cpputCorrectExceptionThrown_ = false; \
  261. CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
  262. cpputMsg_.addDetail( message ); \
  263. cpputMsg_.addDetail( "Expected: " \
  264. CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
  265. \
  266. try { \
  267. expression; \
  268. } catch ( const ExceptionType & ) { \
  269. cpputCorrectExceptionThrown_ = true; \
  270. } catch ( const std::exception &e) { \
  271. cpputMsg_.addDetail( "Actual : " + \
  272. CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
  273. "std::exception or derived") ); \
  274. cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
  275. } catch ( ... ) { \
  276. cpputMsg_.addDetail( "Actual : unknown."); \
  277. } \
  278. \
  279. if ( cpputCorrectExceptionThrown_ ) \
  280. break; \
  281. \
  282. CPPUNIT_NS::Asserter::fail( cpputMsg_, \
  283. CPPUNIT_SOURCELINE() ); \
  284. } while ( false )
  285. /** Asserts that the given expression does not throw any exceptions.
  286. * \ingroup Assertions
  287. * Example of usage:
  288. * \code
  289. * std::vector<int> v;
  290. * v.push_back( 10 );
  291. * CPPUNIT_ASSERT_NO_THROW( v.at( 0 ) );
  292. * \endcode
  293. */
  294. # define CPPUNIT_ASSERT_NO_THROW( expression ) \
  295. CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
  296. expression )
  297. /** Asserts that the given expression does not throw any exceptions,
  298. * setting a user supplied message in case of failure.
  299. * \ingroup Assertions
  300. * Example of usage:
  301. * \code
  302. * std::vector<int> v;
  303. * v.push_back( 10 );
  304. * CPPUNIT_ASSERT_NO_THROW( "std::vector<int> v;", v.at( 0 ) );
  305. * \endcode
  306. */
  307. # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
  308. do { \
  309. CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
  310. cpputMsg_.addDetail( message ); \
  311. \
  312. try { \
  313. expression; \
  314. } catch ( const std::exception &e ) { \
  315. cpputMsg_.addDetail( "Caught: " + \
  316. CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
  317. "std::exception or derived" ) ); \
  318. cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
  319. CPPUNIT_NS::Asserter::fail( cpputMsg_, \
  320. CPPUNIT_SOURCELINE() ); \
  321. } catch ( ... ) { \
  322. cpputMsg_.addDetail( "Caught: unknown." ); \
  323. CPPUNIT_NS::Asserter::fail( cpputMsg_, \
  324. CPPUNIT_SOURCELINE() ); \
  325. } \
  326. } while ( false )
  327. /** Asserts that an assertion fail.
  328. * \ingroup Assertions
  329. * Use to test assertions.
  330. * Example of usage:
  331. * \code
  332. * CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( 1 == 2 ) );
  333. * \endcode
  334. */
  335. # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
  336. CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
  337. /** Asserts that an assertion fail, with a user-supplied message in
  338. * case of error.
  339. * \ingroup Assertions
  340. * Use to test assertions.
  341. * Example of usage:
  342. * \code
  343. * CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( "1 == 2", CPPUNIT_ASSERT( 1 == 2 ) );
  344. * \endcode
  345. */
  346. # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
  347. CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
  348. /** Asserts that an assertion pass.
  349. * \ingroup Assertions
  350. * Use to test assertions.
  351. * Example of usage:
  352. * \code
  353. * CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( 1 == 1 ) );
  354. * \endcode
  355. */
  356. # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
  357. CPPUNIT_ASSERT_NO_THROW( assertion )
  358. /** Asserts that an assertion pass, with a user-supplied message in
  359. * case of failure.
  360. * \ingroup Assertions
  361. * Use to test assertions.
  362. * Example of usage:
  363. * \code
  364. * CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( "1 != 1", CPPUNIT_ASSERT( 1 == 1 ) );
  365. * \endcode
  366. */
  367. # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
  368. CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
  369. // Backwards compatibility
  370. #if CPPUNIT_ENABLE_NAKED_ASSERT
  371. #undef assert
  372. #define assert(c) CPPUNIT_ASSERT(c)
  373. #define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
  374. #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
  375. #define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
  376. #endif
  377. CPPUNIT_NS_END
  378. #endif // CPPUNIT_TESTASSERT_H