CppUnit project page FAQ CppUnit home page

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

TestAssert.h

Go to the documentation of this file.
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> // For struct assertion_traits<double>
00010 
00011 
00012 CPPUNIT_NS_BEGIN
00013 
00014 
00038 template <class T>
00039 struct assertion_traits 
00040 {  
00041     static bool equal( const T& x, const T& y )
00042     {
00043         return x == y;
00044     }
00045 
00046     static std::string toString( const T& x )
00047     {
00048         OStringStream ost;
00049         ost << x;
00050         return ost.str();
00051     }
00052 };
00053 
00054 
00063 template <>
00064 struct assertion_traits<double>
00065 {  
00066     static bool equal( double x, double y )
00067     {
00068         return x == y;
00069     }
00070 
00071     static std::string toString( double x )
00072     {
00073 #ifdef DBL_DIG
00074        const int precision = DBL_DIG;
00075 #else
00076        const int precision = 15;
00077 #endif  // #ifdef DBL_DIG
00078        char buffer[128];
00079 #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
00080        sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x); 
00081 #else   
00082        sprintf(buffer, "%.*g", precision, x); 
00083 #endif
00084        return buffer;
00085     }
00086 };
00087 
00088 
00093 template <class T>
00094 void assertEquals( const T& expected,
00095                    const T& actual,
00096                    SourceLine sourceLine,
00097                    const std::string &message )
00098 {
00099   if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
00100   {
00101     Asserter::failNotEqual( assertion_traits<T>::toString(expected),
00102                             assertion_traits<T>::toString(actual),
00103                             sourceLine,
00104                             message );
00105   }
00106 }
00107 
00108 
00113 void CPPUNIT_API assertDoubleEquals( double expected,
00114                                      double actual,
00115                                      double delta,
00116                                      SourceLine sourceLine, 
00117                                      const std::string &message );
00118 
00119 
00120 /* A set of macros which allow us to get the line number
00121  * and file name at the point of an error.
00122  * Just goes to show that preprocessors do have some
00123  * redeeming qualities.
00124  */
00125 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00126 
00129 #define CPPUNIT_ASSERT(condition)                                                 \
00130   ( CPPUNIT_NS::Asserter::failIf( !(condition),                                   \
00131                                  CPPUNIT_NS::Message( "assertion failed",         \
00132                                                       "Expression: " #condition), \
00133                                  CPPUNIT_SOURCELINE() ) )
00134 #else
00135 #define CPPUNIT_ASSERT(condition)                                            \
00136   ( CPPUNIT_NS::Asserter::failIf( !(condition),                              \
00137                                   CPPUNIT_NS::Message( "assertion failed" ), \
00138                                   CPPUNIT_SOURCELINE() ) )
00139 #endif
00140 
00148 #define CPPUNIT_ASSERT_MESSAGE(message,condition)                          \
00149   ( CPPUNIT_NS::Asserter::failIf( !(condition),                            \
00150                                   CPPUNIT_NS::Message( "assertion failed", \
00151                                                        "Expression: "      \
00152                                                        #condition,         \
00153                                                        message ),          \
00154                                   CPPUNIT_SOURCELINE() ) )
00155 
00160 #define CPPUNIT_FAIL( message )                                         \
00161   ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure",  \
00162                                                      message ),         \
00163                                 CPPUNIT_SOURCELINE() ) )
00164 
00165 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00166 
00167 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                     \
00168   ( CPPUNIT_NS::assertEquals( (expected),             \
00169                               (actual),               \
00170                               __LINE__, __FILE__ ) )
00171 #else
00172 
00188 #define CPPUNIT_ASSERT_EQUAL(expected,actual)          \
00189   ( CPPUNIT_NS::assertEquals( (expected),              \
00190                               (actual),                \
00191                               CPPUNIT_SOURCELINE(),    \
00192                               "" ) )
00193 
00212 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)      \
00213   ( CPPUNIT_NS::assertEquals( (expected),              \
00214                               (actual),                \
00215                               CPPUNIT_SOURCELINE(),    \
00216                               (message) ) )
00217 #endif
00218 
00222 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)        \
00223   ( CPPUNIT_NS::assertDoubleEquals( (expected),            \
00224                                     (actual),              \
00225                                     (delta),               \
00226                                     CPPUNIT_SOURCELINE(),  \
00227                                     "" ) )
00228 
00229 
00234 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta)  \
00235   ( CPPUNIT_NS::assertDoubleEquals( (expected),            \
00236                                     (actual),              \
00237                                     (delta),               \
00238                                     CPPUNIT_SOURCELINE(),  \
00239                                     (message) ) )
00240 
00241 
00250 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType )              \
00251    CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(),       \
00252                                  expression,                            \
00253                                  ExceptionType )
00254 
00255 
00256 // implementation detail
00257 #if CPPUNIT_USE_TYPEINFO_NAME
00258 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00259    CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
00260 #else
00261 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00262    std::string( no_rtti_message )
00263 #endif // CPPUNIT_USE_TYPEINFO_NAME
00264 
00265 // implementation detail
00266 #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
00267 
00277 # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType )   \
00278    do {                                                                       \
00279       bool cpputCorrectExceptionThrown_ = false;                              \
00280       CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" );       \
00281       cpputMsg_.addDetail( message );                                         \
00282       cpputMsg_.addDetail( "Expected: "                                       \
00283                            CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) );   \
00284                                                                               \
00285       try {                                                                   \
00286          expression;                                                          \
00287       } catch ( const ExceptionType & ) {                                     \
00288          cpputCorrectExceptionThrown_ = true;                                 \
00289       } catch ( const std::exception &e) {                                    \
00290          cpputMsg_.addDetail( "Actual  : " +                                  \
00291                               CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e,             \
00292                                           "std::exception or derived") );     \
00293          cpputMsg_.addDetail( std::string("What()  : ") + e.what() );         \
00294       } catch ( ... ) {                                                       \
00295          cpputMsg_.addDetail( "Actual  : unknown.");                          \
00296       }                                                                       \
00297                                                                               \
00298       if ( cpputCorrectExceptionThrown_ )                                     \
00299          break;                                                               \
00300                                                                               \
00301       CPPUNIT_NS::Asserter::fail( cpputMsg_,                                  \
00302                                   CPPUNIT_SOURCELINE() );                     \
00303    } while ( false )
00304 
00305 
00315 # define CPPUNIT_ASSERT_NO_THROW( expression )                             \
00316    CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(),       \
00317                                     expression )
00318 
00319 
00330 # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression )               \
00331    do {                                                                       \
00332       CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" );         \
00333       cpputMsg_.addDetail( message );                                         \
00334                                                                               \
00335       try {                                                                   \
00336          expression;                                                          \
00337       } catch ( const std::exception &e ) {                                   \
00338          cpputMsg_.addDetail( "Caught: " +                                    \
00339                               CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e,             \
00340                                           "std::exception or derived" ) );    \
00341          cpputMsg_.addDetail( std::string("What(): ") + e.what() );           \
00342          CPPUNIT_NS::Asserter::fail( cpputMsg_,                               \
00343                                      CPPUNIT_SOURCELINE() );                  \
00344       } catch ( ... ) {                                                       \
00345          cpputMsg_.addDetail( "Caught: unknown." );                           \
00346          CPPUNIT_NS::Asserter::fail( cpputMsg_,                               \
00347                                      CPPUNIT_SOURCELINE() );                  \
00348       }                                                                       \
00349    } while ( false )
00350 
00351 
00360 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion )                 \
00361    CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
00362 
00363 
00373 # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion )    \
00374    CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
00375 
00376 
00385 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion )                 \
00386    CPPUNIT_ASSERT_NO_THROW( assertion )
00387 
00388 
00398 # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion )    \
00399    CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
00400 
00401 
00402 
00403 
00404 // Backwards compatibility
00405 
00406 #if CPPUNIT_ENABLE_NAKED_ASSERT
00407 
00408 #undef assert
00409 #define assert(c)                 CPPUNIT_ASSERT(c)
00410 #define assertEqual(e,a)          CPPUNIT_ASSERT_EQUAL(e,a)
00411 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00412 #define assertLongsEqual(e,a)     CPPUNIT_ASSERT_EQUAL(e,a)
00413 
00414 #endif
00415 
00416 
00417 CPPUNIT_NS_END
00418 
00419 #endif  // CPPUNIT_TESTASSERT_H

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers