CppUnit project page FAQ

TestAssert.h
Go to the documentation of this file.
1 #ifndef CPPUNIT_TESTASSERT_H
2 #define CPPUNIT_TESTASSERT_H
3 
4 #include <cppunit/Portability.h>
5 #include <cppunit/Exception.h>
6 #include <cppunit/Asserter.h>
9 #include <stdio.h>
10 #include <float.h> // For struct assertion_traits<double>
11 
12 // Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T
13 // is an enum type:
14 #if defined __GNUC__ && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
15 #pragma GCC system_header
16 #endif
17 
18 
20 
54 template <class T>
56 {
57  static bool equal( const T& x, const T& y )
58  {
59  return x == y;
60  }
61 
62  static bool less( const T& x, const T& y )
63  {
64  return x < y;
65  }
66 
67  static bool lessEqual( const T& x, const T& y )
68  {
69  return x <= y;
70  }
71 
72  static std::string toString( const T& x )
73  {
75  }
76 };
77 
86 template <>
87 struct assertion_traits<double>
88 {
89  static bool equal( double x, double y )
90  {
91  return x == y;
92  }
93 
94  static bool less( double x, double y )
95  {
96  return x < y;
97  }
98 
99  static bool lessEqual( double x, double y )
100  {
101  return x <= y;
102  }
103 
104  static std::string toString( double x )
105  {
106 #ifdef DBL_DIG
107  const int precision = DBL_DIG;
108 #else
109  const int precision = 15;
110 #endif // #ifdef DBL_DIG
111  char buffer[128];
112 #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
113  sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
114 #else
115  sprintf(buffer, "%.*g", precision, x);
116 #endif
117  return buffer;
118  }
119 };
120 
121 
135 inline std::string message_to_string( const std::string& s )
136 {
137  return s;
138 }
139 inline std::string message_to_string( const OStream& out )
140 {
141  OStringStream ost;
142  ost << out.rdbuf();
143  return ost.str();
144 }
147 {
148  return msg;
149 }
151 inline std::string message_to_string( const char * s )
152 {
153  return s;
154 }
155 
160 template <class T>
161 void assertEquals( const T& expected,
162  const T& actual,
163  SourceLine sourceLine,
164  const std::string &message )
165 {
166  if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
167  {
170  sourceLine,
171  message );
172  }
173 }
174 
175 
181 void CPPUNIT_API assertDoubleEquals( double expected,
182  double actual,
183  double delta,
184  SourceLine sourceLine,
185  const std::string &message );
186 
187 
192 template <class T>
193 void assertLess( const T& expected,
194  const T& actual,
195  SourceLine sourceLine,
196  const std::string& message )
197 {
198  if ( !assertion_traits<T>::less(actual,expected) )
199  {
202  sourceLine,
203  message );
204  }
205 }
206 
207 
212 template <class T>
213 void assertGreater( const T& expected,
214  const T& actual,
215  SourceLine sourceLine,
216  const std::string& message )
217 {
218  if ( !assertion_traits<T>::less(expected,actual) )
219  {
222  sourceLine,
223  message );
224  }
225 }
226 
231 template <class T>
232 void assertLessEqual( const T& expected,
233  const T& actual,
234  SourceLine sourceLine,
235  const std::string& message )
236 {
237  if ( !assertion_traits<T>::lessEqual(actual,expected) )
238  {
241  sourceLine,
242  message );
243  }
244 }
245 
250 template <class T>
251 void assertGreaterEqual( const T& expected,
252  const T& actual,
253  SourceLine sourceLine,
254  const std::string& message )
255 {
256  if ( !assertion_traits<T>::lessEqual(expected,actual) )
257  {
260  sourceLine,
261  message );
262  }
263 }
264 /* A set of macros which allow us to get the line number
265  * and file name at the point of an error.
266  * Just goes to show that preprocessors do have some
267  * redeeming qualities.
268  */
269 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
273 #define CPPUNIT_ASSERT(condition) \
274  ( CPPUNIT_NS::Asserter::failIf( !(condition), \
275  CPPUNIT_NS::Message( "assertion failed", \
276  "Expression: " #condition), \
277  CPPUNIT_SOURCELINE() ) )
278 #else
279 #define CPPUNIT_ASSERT(condition) \
280  ( CPPUNIT_NS::Asserter::failIf( !(condition), \
281  CPPUNIT_NS::Message( "assertion failed" ), \
282  CPPUNIT_SOURCELINE() ) )
283 #endif
284 
292 #define CPPUNIT_ASSERT_MESSAGE(message,condition) \
293  ( CPPUNIT_NS::Asserter::failIf( !(condition), \
294  CPPUNIT_NS::Message( "assertion failed", \
295  "Expression: " \
296  #condition, \
297  CPPUNIT_NS::message_to_string(message) ), \
298  CPPUNIT_SOURCELINE() ) )
299 
304 #define CPPUNIT_FAIL( message ) \
305  ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
306  CPPUNIT_NS::message_to_string(message) ), \
307  CPPUNIT_SOURCELINE() ) )
308 
309 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
311 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
312  ( CPPUNIT_NS::assertEquals( (expected), \
313  (actual), \
314  __LINE__, __FILE__ ) )
315 #else
332 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
333  ( CPPUNIT_NS::assertEquals( (expected), \
334  (actual), \
335  CPPUNIT_SOURCELINE(), \
336  "" ) )
337 
356 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
357  ( CPPUNIT_NS::assertEquals( (expected), \
358  (actual), \
359  CPPUNIT_SOURCELINE(), \
360  CPPUNIT_NS::message_to_string(message) ) )
361 #endif
362 
383 #define CPPUNIT_ASSERT_LESS(expected, actual) \
384  ( CPPUNIT_NS::assertLess( (expected), \
385  (actual), \
386  CPPUNIT_SOURCELINE(), \
387  "" ) )
388 
409 #define CPPUNIT_ASSERT_GREATER(expected, actual) \
410  ( CPPUNIT_NS::assertGreater( (expected), \
411  (actual), \
412  CPPUNIT_SOURCELINE(), \
413  "" ) )
414 
435 #define CPPUNIT_ASSERT_LESSEQUAL(expected, actual) \
436  ( CPPUNIT_NS::assertLessEqual( (expected), \
437  (actual), \
438  CPPUNIT_SOURCELINE(), \
439  "" ) )
440 
461 #define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual) \
462  ( CPPUNIT_NS::assertGreaterEqual( (expected), \
463  (actual), \
464  CPPUNIT_SOURCELINE(), \
465  "" ) )
476 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
477  ( CPPUNIT_NS::assertDoubleEquals( (expected), \
478  (actual), \
479  (delta), \
480  CPPUNIT_SOURCELINE(), \
481  "" ) )
482 
483 
489 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
490  ( CPPUNIT_NS::assertDoubleEquals( (expected), \
491  (actual), \
492  (delta), \
493  CPPUNIT_SOURCELINE(), \
494  CPPUNIT_NS::message_to_string(message) ) )
495 
496 
505 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
506  CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
507  expression, \
508  ExceptionType )
509 
510 
511 // implementation detail
512 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
513 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
514  CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
515 #else
516 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
517  std::string( no_rtti_message )
518 #endif // CPPUNIT_USE_TYPEINFO_NAME
519 
520 // implementation detail
521 #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
522 
532 # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
533  do { \
534  bool cpputCorrectExceptionThrown_ = false; \
535  CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
536  cpputMsg_.addDetail( CPPUNIT_NS::message_to_string(message) ); \
537  cpputMsg_.addDetail( "Expected: " \
538  CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
539  \
540  try { \
541  expression; \
542  } catch ( const ExceptionType & ) { \
543  cpputCorrectExceptionThrown_ = true; \
544  } catch ( const std::exception &e) { \
545  cpputMsg_.addDetail( "Actual : " + \
546  CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
547  "std::exception or derived") ); \
548  cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
549  } catch ( ... ) { \
550  cpputMsg_.addDetail( "Actual : unknown."); \
551  } \
552  \
553  if ( cpputCorrectExceptionThrown_ ) \
554  { break; } \
555  \
556  CPPUNIT_NS::Asserter::fail( cpputMsg_, \
557  CPPUNIT_SOURCELINE() ); \
558  } while ( false )
559 
560 
570 # define CPPUNIT_ASSERT_NO_THROW( expression ) \
571  CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
572  expression )
573 
574 
585 # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
586  do { \
587  CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
588  cpputMsg_.addDetail( CPPUNIT_NS::message_to_string(message) ); \
589  \
590  try { \
591  expression; \
592  } catch ( const std::exception &e ) { \
593  cpputMsg_.addDetail( "Caught: " + \
594  CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
595  "std::exception or derived" ) ); \
596  cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
597  CPPUNIT_NS::Asserter::fail( cpputMsg_, \
598  CPPUNIT_SOURCELINE() ); \
599  } catch ( ... ) { \
600  cpputMsg_.addDetail( "Caught: unknown." ); \
601  CPPUNIT_NS::Asserter::fail( cpputMsg_, \
602  CPPUNIT_SOURCELINE() ); \
603  } \
604  } while ( false )
605 
606 
615 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
616  CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
617 
618 
628 # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
629  CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
630 
631 
640 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
641  CPPUNIT_ASSERT_NO_THROW( assertion )
642 
643 
653 # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
654  CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
655 
656 
657 
658 
659 // Backwards compatibility
660 
661 #if CPPUNIT_ENABLE_NAKED_ASSERT
662 
663 #undef assert
664 #define assert(c) CPPUNIT_ASSERT(c)
665 #define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
666 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
667 #define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
668 
669 #endif
670 
671 
673 
674 #endif // CPPUNIT_TESTASSERT_H
#define CPPUNIT_API
Definition: CppUnitApi.h:27
#define CPPUNIT_NS_END
Definition: Portability.h:106
#define CPPUNIT_NS_BEGIN
Definition: Portability.h:105
CPPUNIT_NS_BEGIN typedef std::ostream OStream
Definition: Stream.h:330
std::string message_to_string(const std::string &s)
Message traits used by CPPUNIT_ASSERT* macros.
Definition: TestAssert.h:135
void CPPUNIT_API assertDoubleEquals(double expected, double actual, double delta, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two double are equals given a tolerance. Use CPPUNIT_ASSERT_DOUBLES_EQU...
Definition: TestAssert.cpp:8
void assertEquals(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_EQUAL inste...
Definition: TestAssert.h:161
void assertLess(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that an object is less than another one of the same type Use CPPUNIT_ASSERT_...
Definition: TestAssert.h:193
void assertGreater(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that an object is less than another one of the same type Use CPPUNIT_ASSERT_...
Definition: TestAssert.h:213
void assertGreaterEqual(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_LESSEQUAL,...
Definition: TestAssert.h:251
void assertLessEqual(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_LESSEQUAL,...
Definition: TestAssert.h:232
An additional Message for assertions.
Definition: AdditionalMessage.h:40
Represents a source line location.
Definition: SourceLine.h:31
std::enable_if<!std::is_enum< T >::value, std::string >::type toString(const T &x)
Definition: StringHelper.h:22
static void CPPUNIT_API failNotLess(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="less assertion failed")
Throws an Exception with the specified message and location.
Definition: Asserter.cpp:128
static void CPPUNIT_API failNotGreaterEqual(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="greater equal assertion failed")
Throws an Exception with the specified message and location.
Definition: Asserter.cpp:171
static void CPPUNIT_API failNotEqual(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="equality assertion failed")
Throws an Exception with the specified message and location.
Definition: Asserter.cpp:113
static void CPPUNIT_API failNotGreater(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="greater assertion failed")
Throws an Exception with the specified message and location.
Definition: Asserter.cpp:143
static void CPPUNIT_API failNotLessEqual(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="less equal assertion failed")
Throws an Exception with the specified message and location.
Definition: Asserter.cpp:157
static std::string toString(double x)
Definition: TestAssert.h:104
static bool equal(double x, double y)
Definition: TestAssert.h:89
static bool lessEqual(double x, double y)
Definition: TestAssert.h:99
static bool less(double x, double y)
Definition: TestAssert.h:94
Traits used by CPPUNIT_ASSERT* macros.
Definition: TestAssert.h:56
static std::string toString(const T &x)
Definition: TestAssert.h:72
static bool equal(const T &x, const T &y)
Definition: TestAssert.h:57
static bool lessEqual(const T &x, const T &y)
Definition: TestAssert.h:67
static bool less(const T &x, const T &y)
Definition: TestAssert.h:62

Send comments to:
CppUnit Developers