00001 #ifndef CPPUNIT_TESTASSERT_H
00002 #define CPPUNIT_TESTASSERT_H
00003
00004 #include <cppunit/Portability.h>
00005 #include <cppunit/Exception.h>
00006 #include <cppunit/Asserter.h>
00007 #include <cppunit/portability/Stream.h>
00008 #include <stdio.h>
00009 #include <float.h>
00010
00011
00012
00013 #if defined __GNUC__ && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
00014 #pragma GCC system_header
00015 #endif
00016
00017
00018 CPPUNIT_NS_BEGIN
00019
00020
00044 template <class T>
00045 struct assertion_traits
00046 {
00047 static bool equal( const T& x, const T& y )
00048 {
00049 return x == y;
00050 }
00051
00052 static std::string toString( const T& x )
00053 {
00054 OStringStream ost;
00055
00056
00057 #if defined __GNUC__ && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
00058 #pragma GCC diagnostic push
00059 #pragma GCC diagnostic ignored "-Wsign-promo"
00060 #endif
00061 ost << x;
00062 #if defined __GNUC__ && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
00063 #pragma GCC diagnostic pop
00064 #endif
00065 return ost.str();
00066 }
00067 };
00068
00069
00078 template <>
00079 struct assertion_traits<double>
00080 {
00081 static bool equal( double x, double y )
00082 {
00083 return x == y;
00084 }
00085
00086 static std::string toString( double x )
00087 {
00088 #ifdef DBL_DIG
00089 const int precision = DBL_DIG;
00090 #else
00091 const int precision = 15;
00092 #endif // #ifdef DBL_DIG
00093 char buffer[128];
00094 #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
00095 sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
00096 #else
00097 sprintf(buffer, "%.*g", precision, x);
00098 #endif
00099 return buffer;
00100 }
00101 };
00102
00103
00108 template <class T>
00109 void assertEquals( const T& expected,
00110 const T& actual,
00111 SourceLine sourceLine,
00112 const std::string &message )
00113 {
00114 if ( !assertion_traits<T>::equal(expected,actual) )
00115 {
00116 Asserter::failNotEqual( assertion_traits<T>::toString(expected),
00117 assertion_traits<T>::toString(actual),
00118 sourceLine,
00119 message );
00120 }
00121 }
00122
00123
00129 void CPPUNIT_API assertDoubleEquals( double expected,
00130 double actual,
00131 double delta,
00132 SourceLine sourceLine,
00133 const std::string &message );
00134
00135
00136
00137
00138
00139
00140
00141 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00142
00145 #define CPPUNIT_ASSERT(condition) \
00146 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
00147 CPPUNIT_NS::Message( "assertion failed", \
00148 "Expression: " #condition), \
00149 CPPUNIT_SOURCELINE() ) )
00150 #else
00151 #define CPPUNIT_ASSERT(condition) \
00152 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
00153 CPPUNIT_NS::Message( "assertion failed" ), \
00154 CPPUNIT_SOURCELINE() ) )
00155 #endif
00156
00164 #define CPPUNIT_ASSERT_MESSAGE(message,condition) \
00165 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
00166 CPPUNIT_NS::Message( "assertion failed", \
00167 "Expression: " \
00168 #condition, \
00169 message ), \
00170 CPPUNIT_SOURCELINE() ) )
00171
00176 #define CPPUNIT_FAIL( message ) \
00177 ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
00178 message ), \
00179 CPPUNIT_SOURCELINE() ) )
00180
00181 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00183 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
00184 ( CPPUNIT_NS::assertEquals( (expected), \
00185 (actual), \
00186 __LINE__, __FILE__ ) )
00187 #else
00188
00204 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
00205 ( CPPUNIT_NS::assertEquals( (expected), \
00206 (actual), \
00207 CPPUNIT_SOURCELINE(), \
00208 "" ) )
00209
00228 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
00229 ( CPPUNIT_NS::assertEquals( (expected), \
00230 (actual), \
00231 CPPUNIT_SOURCELINE(), \
00232 (message) ) )
00233 #endif
00234
00245 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
00246 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
00247 (actual), \
00248 (delta), \
00249 CPPUNIT_SOURCELINE(), \
00250 "" ) )
00251
00252
00258 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
00259 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
00260 (actual), \
00261 (delta), \
00262 CPPUNIT_SOURCELINE(), \
00263 (message) ) )
00264
00265
00274 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
00275 CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
00276 expression, \
00277 ExceptionType )
00278
00279
00280
00281 #if CPPUNIT_USE_TYPEINFO_NAME
00282 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00283 CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
00284 #else
00285 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00286 std::string( no_rtti_message )
00287 #endif // CPPUNIT_USE_TYPEINFO_NAME
00288
00289
00290 #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
00291
00301 # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
00302 do { \
00303 bool cpputCorrectExceptionThrown_ = false; \
00304 CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
00305 cpputMsg_.addDetail( message ); \
00306 cpputMsg_.addDetail( "Expected: " \
00307 CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
00308 \
00309 try { \
00310 expression; \
00311 } catch ( const ExceptionType & ) { \
00312 cpputCorrectExceptionThrown_ = true; \
00313 } catch ( const std::exception &e) { \
00314 cpputMsg_.addDetail( "Actual : " + \
00315 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
00316 "std::exception or derived") ); \
00317 cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
00318 } catch ( ... ) { \
00319 cpputMsg_.addDetail( "Actual : unknown."); \
00320 } \
00321 \
00322 if ( cpputCorrectExceptionThrown_ ) \
00323 break; \
00324 \
00325 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
00326 CPPUNIT_SOURCELINE() ); \
00327 } while ( false )
00328
00329
00339 # define CPPUNIT_ASSERT_NO_THROW( expression ) \
00340 CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
00341 expression )
00342
00343
00354 # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
00355 do { \
00356 CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
00357 cpputMsg_.addDetail( message ); \
00358 \
00359 try { \
00360 expression; \
00361 } catch ( const std::exception &e ) { \
00362 cpputMsg_.addDetail( "Caught: " + \
00363 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
00364 "std::exception or derived" ) ); \
00365 cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
00366 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
00367 CPPUNIT_SOURCELINE() ); \
00368 } catch ( ... ) { \
00369 cpputMsg_.addDetail( "Caught: unknown." ); \
00370 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
00371 CPPUNIT_SOURCELINE() ); \
00372 } \
00373 } while ( false )
00374
00375
00384 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
00385 CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
00386
00387
00397 # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
00398 CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
00399
00400
00409 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
00410 CPPUNIT_ASSERT_NO_THROW( assertion )
00411
00412
00422 # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
00423 CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
00424
00425
00426
00427
00428
00429
00430 #if CPPUNIT_ENABLE_NAKED_ASSERT
00431
00432 #undef assert
00433 #define assert(c) CPPUNIT_ASSERT(c)
00434 #define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
00435 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00436 #define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
00437
00438 #endif
00439
00440
00441 CPPUNIT_NS_END
00442
00443 #endif // CPPUNIT_TESTASSERT_H