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 <cppunit/tools/StringHelper.h>
00009 #include <stdio.h>
00010 #include <float.h>
00011
00012
00013
00014 #if defined __GNUC__ && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
00015 #pragma GCC system_header
00016 #endif
00017
00018
00019 CPPUNIT_NS_BEGIN
00020
00054 template <class T>
00055 struct assertion_traits
00056 {
00057 static bool equal( const T& x, const T& y )
00058 {
00059 return x == y;
00060 }
00061
00062 static bool less( const T& x, const T& y )
00063 {
00064 return x < y;
00065 }
00066
00067 static bool lessEqual( const T& x, const T& y )
00068 {
00069 return x <= y;
00070 }
00071
00072 static std::string toString( const T& x )
00073 {
00074 return CPPUNIT_NS::StringHelper::toString(x);
00075 }
00076 };
00077
00086 template <>
00087 struct assertion_traits<double>
00088 {
00089 static bool equal( double x, double y )
00090 {
00091 return x == y;
00092 }
00093
00094 static bool less( double x, double y )
00095 {
00096 return x < y;
00097 }
00098
00099 static bool lessEqual( double x, double y )
00100 {
00101 return x <= y;
00102 }
00103
00104 static std::string toString( double x )
00105 {
00106 #ifdef DBL_DIG
00107 const int precision = DBL_DIG;
00108 #else
00109 const int precision = 15;
00110 #endif // #ifdef DBL_DIG
00111 char buffer[128];
00112 #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
00113 sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
00114 #else
00115 sprintf(buffer, "%.*g", precision, x);
00116 #endif
00117 return buffer;
00118 }
00119 };
00120
00121
00126 template <class T>
00127 void assertEquals( const T& expected,
00128 const T& actual,
00129 SourceLine sourceLine,
00130 const std::string &message )
00131 {
00132 if ( !assertion_traits<T>::equal(expected,actual) )
00133 {
00134 Asserter::failNotEqual( assertion_traits<T>::toString(expected),
00135 assertion_traits<T>::toString(actual),
00136 sourceLine,
00137 message );
00138 }
00139 }
00140
00141
00147 void CPPUNIT_API assertDoubleEquals( double expected,
00148 double actual,
00149 double delta,
00150 SourceLine sourceLine,
00151 const std::string &message );
00152
00153
00158 template <class T>
00159 void assertLess( const T& expected,
00160 const T& actual,
00161 SourceLine sourceLine,
00162 const std::string& message )
00163 {
00164 if ( !assertion_traits<T>::less(actual,expected) )
00165 {
00166 Asserter::failNotLess( assertion_traits<T>::toString(expected),
00167 assertion_traits<T>::toString(actual),
00168 sourceLine,
00169 message );
00170 }
00171 }
00172
00173
00178 template <class T>
00179 void assertGreater( const T& expected,
00180 const T& actual,
00181 SourceLine sourceLine,
00182 const std::string& message )
00183 {
00184 if ( !assertion_traits<T>::less(expected,actual) )
00185 {
00186 Asserter::failNotGreater( assertion_traits<T>::toString(expected),
00187 assertion_traits<T>::toString(actual),
00188 sourceLine,
00189 message );
00190 }
00191 }
00192
00197 template <class T>
00198 void assertLessEqual( const T& expected,
00199 const T& actual,
00200 SourceLine sourceLine,
00201 const std::string& message )
00202 {
00203 if ( !assertion_traits<T>::lessEqual(actual,expected) )
00204 {
00205 Asserter::failNotLessEqual( assertion_traits<T>::toString(expected),
00206 assertion_traits<T>::toString(actual),
00207 sourceLine,
00208 message );
00209 }
00210 }
00211
00216 template <class T>
00217 void assertGreaterEqual( const T& expected,
00218 const T& actual,
00219 SourceLine sourceLine,
00220 const std::string& message )
00221 {
00222 if ( !assertion_traits<T>::lessEqual(expected,actual) )
00223 {
00224 Asserter::failNotGreaterEqual( assertion_traits<T>::toString(expected),
00225 assertion_traits<T>::toString(actual),
00226 sourceLine,
00227 message );
00228 }
00229 }
00230
00231
00232
00233
00234
00235 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00236
00239 #define CPPUNIT_ASSERT(condition) \
00240 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
00241 CPPUNIT_NS::Message( "assertion failed", \
00242 "Expression: " #condition), \
00243 CPPUNIT_SOURCELINE() ) )
00244 #else
00245 #define CPPUNIT_ASSERT(condition) \
00246 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
00247 CPPUNIT_NS::Message( "assertion failed" ), \
00248 CPPUNIT_SOURCELINE() ) )
00249 #endif
00250
00258 #define CPPUNIT_ASSERT_MESSAGE(message,condition) \
00259 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
00260 CPPUNIT_NS::Message( "assertion failed", \
00261 "Expression: " \
00262 #condition, \
00263 message ), \
00264 CPPUNIT_SOURCELINE() ) )
00265
00270 #define CPPUNIT_FAIL( message ) \
00271 ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
00272 message ), \
00273 CPPUNIT_SOURCELINE() ) )
00274
00275 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00277 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
00278 ( CPPUNIT_NS::assertEquals( (expected), \
00279 (actual), \
00280 __LINE__, __FILE__ ) )
00281 #else
00282
00298 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
00299 ( CPPUNIT_NS::assertEquals( (expected), \
00300 (actual), \
00301 CPPUNIT_SOURCELINE(), \
00302 "" ) )
00303
00322 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
00323 ( CPPUNIT_NS::assertEquals( (expected), \
00324 (actual), \
00325 CPPUNIT_SOURCELINE(), \
00326 (message) ) )
00327 #endif
00328
00349 #define CPPUNIT_ASSERT_LESS(expected, actual) \
00350 ( CPPUNIT_NS::assertLess( (expected), \
00351 (actual), \
00352 CPPUNIT_SOURCELINE(), \
00353 "" ) )
00354
00375 #define CPPUNIT_ASSERT_GREATER(expected, actual) \
00376 ( CPPUNIT_NS::assertGreater( (expected), \
00377 (actual), \
00378 CPPUNIT_SOURCELINE(), \
00379 "" ) )
00380
00401 #define CPPUNIT_ASSERT_LESSEQUAL(expected, actual) \
00402 ( CPPUNIT_NS::assertLessEqual( (expected), \
00403 (actual), \
00404 CPPUNIT_SOURCELINE(), \
00405 "" ) )
00406
00427 #define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual) \
00428 ( CPPUNIT_NS::assertGreaterEqual( (expected), \
00429 (actual), \
00430 CPPUNIT_SOURCELINE(), \
00431 "" ) )
00432
00442 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
00443 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
00444 (actual), \
00445 (delta), \
00446 CPPUNIT_SOURCELINE(), \
00447 "" ) )
00448
00449
00455 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
00456 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
00457 (actual), \
00458 (delta), \
00459 CPPUNIT_SOURCELINE(), \
00460 (message) ) )
00461
00462
00471 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
00472 CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
00473 expression, \
00474 ExceptionType )
00475
00476
00477
00478 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
00479 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00480 CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
00481 #else
00482 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00483 std::string( no_rtti_message )
00484 #endif // CPPUNIT_USE_TYPEINFO_NAME
00485
00486
00487 #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
00488
00498 # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
00499 do { \
00500 bool cpputCorrectExceptionThrown_ = false; \
00501 CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
00502 cpputMsg_.addDetail( message ); \
00503 cpputMsg_.addDetail( "Expected: " \
00504 CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
00505 \
00506 try { \
00507 expression; \
00508 } catch ( const ExceptionType & ) { \
00509 cpputCorrectExceptionThrown_ = true; \
00510 } catch ( const std::exception &e) { \
00511 cpputMsg_.addDetail( "Actual : " + \
00512 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
00513 "std::exception or derived") ); \
00514 cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
00515 } catch ( ... ) { \
00516 cpputMsg_.addDetail( "Actual : unknown."); \
00517 } \
00518 \
00519 if ( cpputCorrectExceptionThrown_ ) \
00520 break; \
00521 \
00522 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
00523 CPPUNIT_SOURCELINE() ); \
00524 } while ( false )
00525
00526
00536 # define CPPUNIT_ASSERT_NO_THROW( expression ) \
00537 CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
00538 expression )
00539
00540
00551 # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
00552 do { \
00553 CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
00554 cpputMsg_.addDetail( message ); \
00555 \
00556 try { \
00557 expression; \
00558 } catch ( const std::exception &e ) { \
00559 cpputMsg_.addDetail( "Caught: " + \
00560 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
00561 "std::exception or derived" ) ); \
00562 cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
00563 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
00564 CPPUNIT_SOURCELINE() ); \
00565 } catch ( ... ) { \
00566 cpputMsg_.addDetail( "Caught: unknown." ); \
00567 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
00568 CPPUNIT_SOURCELINE() ); \
00569 } \
00570 } while ( false )
00571
00572
00581 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
00582 CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
00583
00584
00594 # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
00595 CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
00596
00597
00606 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
00607 CPPUNIT_ASSERT_NO_THROW( assertion )
00608
00609
00619 # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
00620 CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
00621
00622
00623
00624
00625
00626
00627 #if CPPUNIT_ENABLE_NAKED_ASSERT
00628
00629 #undef assert
00630 #define assert(c) CPPUNIT_ASSERT(c)
00631 #define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
00632 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00633 #define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
00634
00635 #endif
00636
00637
00638 CPPUNIT_NS_END
00639
00640 #endif // CPPUNIT_TESTASSERT_H