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 
00008 
00009 CPPUNIT_NS_BEGIN
00010 
00011 
00035 template <class T>
00036 struct assertion_traits 
00037 {  
00038     static bool equal( const T& x, const T& y )
00039     {
00040         return x == y;
00041     }
00042 
00043     static std::string toString( const T& x )
00044     {
00045         OStringStream ost;
00046         ost << x;
00047         return ost.str();
00048     }
00049 };
00050 
00051 
00056 template <class T>
00057 void assertEquals( const T& expected,
00058                    const T& actual,
00059                    SourceLine sourceLine,
00060                    const std::string &message )
00061 {
00062   if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
00063   {
00064     Asserter::failNotEqual( assertion_traits<T>::toString(expected),
00065                             assertion_traits<T>::toString(actual),
00066                             sourceLine,
00067                             message );
00068   }
00069 }
00070 
00075 void CPPUNIT_API assertDoubleEquals( double expected,
00076                                      double actual,
00077                                      double delta,
00078                                      SourceLine sourceLine );
00079 
00080 
00081 /* A set of macros which allow us to get the line number
00082  * and file name at the point of an error.
00083  * Just goes to show that preprocessors do have some
00084  * redeeming qualities.
00085  */
00086 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00087 
00090 #define CPPUNIT_ASSERT(condition)                                                 \
00091   ( CPPUNIT_NS::Asserter::failIf( !(condition),                                   \
00092                                  CPPUNIT_NS::Message( "assertion failed",         \
00093                                                       "Expression: " #condition), \
00094                                  CPPUNIT_SOURCELINE() ) )
00095 #else
00096 #define CPPUNIT_ASSERT(condition)                                            \
00097   ( CPPUNIT_NS::Asserter::failIf( !(condition),                              \
00098                                   CPPUNIT_NS::Message( "assertion failed" ), \
00099                                   CPPUNIT_SOURCELINE() ) )
00100 #endif
00101 
00109 #define CPPUNIT_ASSERT_MESSAGE(message,condition)          \
00110   ( CPPUNIT_NS::Asserter::failIf( !(condition),            \
00111                                   (message),               \
00112                                   CPPUNIT_SOURCELINE() ) )
00113 
00118 #define CPPUNIT_FAIL( message )                                         \
00119   ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure",  \
00120                                                      message ),         \
00121                                 CPPUNIT_SOURCELINE() ) )
00122 
00123 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00124 
00125 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                     \
00126   ( CPPUNIT_NS::assertEquals( (expected),             \
00127                               (actual),               \
00128                               __LINE__, __FILE__ ) )
00129 #else
00130 
00146 #define CPPUNIT_ASSERT_EQUAL(expected,actual)          \
00147   ( CPPUNIT_NS::assertEquals( (expected),              \
00148                               (actual),                \
00149                               CPPUNIT_SOURCELINE(),    \
00150                               "" ) )
00151 
00170 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)      \
00171   ( CPPUNIT_NS::assertEquals( (expected),              \
00172                               (actual),                \
00173                               CPPUNIT_SOURCELINE(),    \
00174                               (message) ) )
00175 #endif
00176 
00180 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)        \
00181   ( CPPUNIT_NS::assertDoubleEquals( (expected),        \
00182                                     (actual),          \
00183                                     (delta),           \
00184                                     CPPUNIT_SOURCELINE() ) )
00185 
00186 // Backwards compatibility
00187 
00188 #if CPPUNIT_ENABLE_NAKED_ASSERT
00189 
00190 #undef assert
00191 #define assert(c)                 CPPUNIT_ASSERT(c)
00192 #define assertEqual(e,a)          CPPUNIT_ASSERT_EQUAL(e,a)
00193 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00194 #define assertLongsEqual(e,a)     CPPUNIT_ASSERT_EQUAL(e,a)
00195 
00196 #endif
00197 
00198 
00199 CPPUNIT_NS_END
00200 
00201 #endif  // CPPUNIT_TESTASSERT_H

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