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>
8 #include <stdio.h>
9 #include <float.h> // For struct assertion_traits<double>
10 
11 // Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T
12 // is an enum type:
13 #if defined __GNUC__ && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
14 #pragma GCC system_header
15 #endif
16 
17 
19 
20 
44 template <class T>
46 {
47  static bool equal( const T& x, const T& y )
48  {
49  return x == y;
50  }
51 
52  static std::string toString( const T& x )
53  {
54  OStringStream ost;
55 // Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T
56 // is an enum type:
57 #if defined __GNUC__ && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
58 #pragma GCC diagnostic push
59 #pragma GCC diagnostic ignored "-Wsign-promo"
60 #endif
61  ost << x;
62 #if defined __GNUC__ && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
63 #pragma GCC diagnostic pop
64 #endif
65  return ost.str();
66  }
67 };
68 
69 
78 template <>
79 struct assertion_traits<double>
80 {
81  static bool equal( double x, double y )
82  {
83  return x == y;
84  }
85 
86  static std::string toString( double x )
87  {
88 #ifdef DBL_DIG
89  const int precision = DBL_DIG;
90 #else
91  const int precision = 15;
92 #endif // #ifdef DBL_DIG
93  char buffer[128];
94 #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
95  sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
96 #else
97  sprintf(buffer, "%.*g", precision, x);
98 #endif
99  return buffer;
100  }
101 };
102 
103 
108 template <class T>
109 void assertEquals( const T& expected,
110  const T& actual,
111  SourceLine sourceLine,
112  const std::string &message )
113 {
114  if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
115  {
118  sourceLine,
119  message );
120  }
121 }
122 
123 
129 void CPPUNIT_API assertDoubleEquals( double expected,
130  double actual,
131  double delta,
132  SourceLine sourceLine,
133  const std::string &message );
134 
135 
136 /* A set of macros which allow us to get the line number
137  * and file name at the point of an error.
138  * Just goes to show that preprocessors do have some
139  * redeeming qualities.
140  */
141 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
142 
145 #define CPPUNIT_ASSERT(condition) \
146  ( CPPUNIT_NS::Asserter::failIf( !(condition), \
147  CPPUNIT_NS::Message( "assertion failed", \
148  "Expression: " #condition), \
149  CPPUNIT_SOURCELINE() ) )
150 #else
151 #define CPPUNIT_ASSERT(condition) \
152  ( CPPUNIT_NS::Asserter::failIf( !(condition), \
153  CPPUNIT_NS::Message( "assertion failed" ), \
154  CPPUNIT_SOURCELINE() ) )
155 #endif
156 
164 #define CPPUNIT_ASSERT_MESSAGE(message,condition) \
165  ( CPPUNIT_NS::Asserter::failIf( !(condition), \
166  CPPUNIT_NS::Message( "assertion failed", \
167  "Expression: " \
168  #condition, \
169  message ), \
170  CPPUNIT_SOURCELINE() ) )
171 
176 #define CPPUNIT_FAIL( message ) \
177  ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
178  message ), \
179  CPPUNIT_SOURCELINE() ) )
180 
181 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
182 
183 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
184  ( CPPUNIT_NS::assertEquals( (expected), \
185  (actual), \
186  __LINE__, __FILE__ ) )
187 #else
188 
204 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
205  ( CPPUNIT_NS::assertEquals( (expected), \
206  (actual), \
207  CPPUNIT_SOURCELINE(), \
208  "" ) )
209 
228 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
229  ( CPPUNIT_NS::assertEquals( (expected), \
230  (actual), \
231  CPPUNIT_SOURCELINE(), \
232  (message) ) )
233 #endif
234 
245 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
246  ( CPPUNIT_NS::assertDoubleEquals( (expected), \
247  (actual), \
248  (delta), \
249  CPPUNIT_SOURCELINE(), \
250  "" ) )
251 
252 
258 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
259  ( CPPUNIT_NS::assertDoubleEquals( (expected), \
260  (actual), \
261  (delta), \
262  CPPUNIT_SOURCELINE(), \
263  (message) ) )
264 
265 
274 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
275  CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
276  expression, \
277  ExceptionType )
278 
279 
280 // implementation detail
281 #if CPPUNIT_USE_TYPEINFO_NAME
282 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
283  CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
284 #else
285 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
286  std::string( no_rtti_message )
287 #endif // CPPUNIT_USE_TYPEINFO_NAME
288 
289 // implementation detail
290 #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
291 
301 # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
302  do { \
303  bool cpputCorrectExceptionThrown_ = false; \
304  CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
305  cpputMsg_.addDetail( message ); \
306  cpputMsg_.addDetail( "Expected: " \
307  CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
308  \
309  try { \
310  expression; \
311  } catch ( const ExceptionType & ) { \
312  cpputCorrectExceptionThrown_ = true; \
313  } catch ( const std::exception &e) { \
314  cpputMsg_.addDetail( "Actual : " + \
315  CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
316  "std::exception or derived") ); \
317  cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
318  } catch ( ... ) { \
319  cpputMsg_.addDetail( "Actual : unknown."); \
320  } \
321  \
322  if ( cpputCorrectExceptionThrown_ ) \
323  break; \
324  \
325  CPPUNIT_NS::Asserter::fail( cpputMsg_, \
326  CPPUNIT_SOURCELINE() ); \
327  } while ( false )
328 
329 
339 # define CPPUNIT_ASSERT_NO_THROW( expression ) \
340  CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
341  expression )
342 
343 
354 # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
355  do { \
356  CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
357  cpputMsg_.addDetail( message ); \
358  \
359  try { \
360  expression; \
361  } catch ( const std::exception &e ) { \
362  cpputMsg_.addDetail( "Caught: " + \
363  CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
364  "std::exception or derived" ) ); \
365  cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
366  CPPUNIT_NS::Asserter::fail( cpputMsg_, \
367  CPPUNIT_SOURCELINE() ); \
368  } catch ( ... ) { \
369  cpputMsg_.addDetail( "Caught: unknown." ); \
370  CPPUNIT_NS::Asserter::fail( cpputMsg_, \
371  CPPUNIT_SOURCELINE() ); \
372  } \
373  } while ( false )
374 
375 
384 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
385  CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
386 
387 
397 # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
398  CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
399 
400 
409 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
410  CPPUNIT_ASSERT_NO_THROW( assertion )
411 
412 
422 # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
423  CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
424 
425 
426 
427 
428 // Backwards compatibility
429 
430 #if CPPUNIT_ENABLE_NAKED_ASSERT
431 
432 #undef assert
433 #define assert(c) CPPUNIT_ASSERT(c)
434 #define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
435 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
436 #define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
437 
438 #endif
439 
440 
442 
443 #endif // CPPUNIT_TESTASSERT_H

Send comments to:
CppUnit Developers