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 namespace CppUnit {
00010 
00034   template <class T>
00035   struct assertion_traits 
00036   {  
00037       static bool equal( const T& x, const T& y )
00038       {
00039           return x == y;
00040       }
00041 
00042       static std::string toString( const T& x )
00043       {
00044           OStringStream ost;
00045           ost << x;
00046           return ost.str();
00047       }
00048   };
00049 
00050 
00051   namespace TestAssert
00052   {
00053 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00054     void CPPUNIT_API assertImplementation( bool         condition, 
00055                                            std::string  conditionExpression = "",
00056                                            long lineNumber,
00057                                            std::string  fileName );
00058 
00059     void CPPUNIT_API assertNotEqualImplementation( std::string expected,
00060                                                    std::string actual,
00061                                                    long lineNumber,
00062                                                    std::string fileName );
00063       
00064 
00065     template <class T>
00066     void assertEquals( const T& expected,
00067                        const T& actual,
00068                        long lineNumber,
00069                        std::string fileName )
00070     {
00071       if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
00072       {
00073         assertNotEqualImplementation( assertion_traits<T>::toString(expected),
00074                                       assertion_traits<T>::toString(actual),
00075                                       lineNumber, 
00076                                       fileName );
00077       }
00078     }
00079 
00080     void CPPUNIT_API assertEquals( double expected, 
00081                                    double actual, 
00082                                    double delta, 
00083                                    long lineNumber,
00084                                    std::string fileName );
00085 
00086 #else   //                  using SourceLine
00087 
00088     template <class T>
00089     void assertEquals( const T& expected,
00090                        const T& actual,
00091                        SourceLine sourceLine,
00092                        const std::string &message ="" )
00093     {
00094       if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
00095       {
00096         Asserter::failNotEqual( assertion_traits<T>::toString(expected),
00097                                 assertion_traits<T>::toString(actual),
00098                                 sourceLine,
00099                                 message );
00100       }
00101     }
00102 
00103     void CPPUNIT_API assertDoubleEquals( double expected,
00104                                          double actual,
00105                                          double delta,
00106                                          SourceLine sourceLine );
00107 
00108 #endif
00109   }
00110 
00111 
00112 /* A set of macros which allow us to get the line number
00113  * and file name at the point of an error.
00114  * Just goes to show that preprocessors do have some
00115  * redeeming qualities.
00116  */
00117 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00118 
00121 #define CPPUNIT_ASSERT(condition)                          \
00122   ( ::CppUnit::Asserter::failIf( !(condition),             \
00123                                  (#condition),             \
00124                                  CPPUNIT_SOURCELINE() ) )
00125 #else
00126 #define CPPUNIT_ASSERT(condition)                          \
00127   ( ::CppUnit::Asserter::failIf( !(condition),             \
00128                                  "",                       \
00129                                  CPPUNIT_SOURCELINE() ) )
00130 #endif
00131 
00139 #define CPPUNIT_ASSERT_MESSAGE(message,condition)          \
00140   ( ::CppUnit::Asserter::failIf( !(condition),             \
00141                                  (message),                \
00142                                  CPPUNIT_SOURCELINE() ) )
00143 
00148 #define CPPUNIT_FAIL( message )                            \
00149   ( ::CppUnit::Asserter::fail( message,                    \
00150                                CPPUNIT_SOURCELINE() ) )
00151 
00152 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00153 
00154 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                    \
00155   ( ::CppUnit::TestAssert::assertEquals( (expected),             \
00156                                          (actual),               \
00157                                          __LINE__, __FILE__ ) )
00158 #else
00159 
00175 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                     \
00176   ( ::CppUnit::TestAssert::assertEquals( (expected),              \
00177                                          (actual),                \
00178                                          CPPUNIT_SOURCELINE() ) )
00179 
00198 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)     \
00199   ( ::CppUnit::TestAssert::assertEquals( (expected),              \
00200                                          (actual),                \
00201                                          CPPUNIT_SOURCELINE(),    \
00202                                          (message) ) )
00203 #endif
00204 
00208 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)       \
00209   ( ::CppUnit::TestAssert::assertDoubleEquals( (expected),        \
00210                                                (actual),          \
00211                                                (delta),           \
00212                                                CPPUNIT_SOURCELINE() ) )
00213 
00214 // Backwards compatibility
00215 
00216 #if CPPUNIT_ENABLE_NAKED_ASSERT
00217 
00218 #undef assert
00219 #define assert(c)                 CPPUNIT_ASSERT(c)
00220 #define assertEqual(e,a)          CPPUNIT_ASSERT_EQUAL(e,a)
00221 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00222 #define assertLongsEqual(e,a)     CPPUNIT_ASSERT_EQUAL(e,a)
00223 
00224 #endif
00225 
00226 
00227 } // namespace CppUnit
00228 
00229 #endif  // CPPUNIT_TESTASSERT_H

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