CppUnit project page FAQ CppUnit home page

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

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 <string>
00007 
00008 
00009 namespace CppUnit {
00010 
00011   template <class T>
00012   struct assertion_traits 
00013   {  
00014       static bool equal( const T& x, const T& y )
00015       {
00016           return x == y;
00017       }
00018 
00019       static std::string toString( const T& x )
00020       {
00021           OStringStream ost;
00022           ost << x;
00023           return ost.str();
00024       }
00025   };
00026 
00027 
00028   namespace TestAssert
00029   {
00030     void assertImplementation( bool         condition, 
00031                                std::string  conditionExpression = "",
00032                                long lineNumber = Exception::UNKNOWNLINENUMBER,
00033                                std::string  fileName = Exception::UNKNOWNFILENAME );
00034 
00035     void assertNotEqualImplementation( std::string expected,
00036                                        std::string actual,
00037                                        long lineNumber = Exception::UNKNOWNLINENUMBER,
00038                                        std::string fileName = Exception::UNKNOWNFILENAME );
00039       
00040 
00041     template <class T>
00042     void assertEquals( const T& expected,
00043                        const T& actual,
00044                        long lineNumber = Exception::UNKNOWNLINENUMBER,
00045                        std::string fileName = Exception::UNKNOWNFILENAME )
00046     {
00047       if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
00048       {
00049         assertNotEqualImplementation( assertion_traits<T>::toString(expected),
00050                                       assertion_traits<T>::toString(actual),
00051                                       lineNumber, 
00052                                       fileName );
00053       }
00054     }
00055 
00056     void assertEquals( double expected, 
00057                        double actual, 
00058                        double delta, 
00059                        long lineNumber = Exception::UNKNOWNLINENUMBER,
00060                        std::string fileName = Exception::UNKNOWNFILENAME);
00061   }
00062 
00063 
00069 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00070 
00071 #  define CPPUNIT_ASSERT(condition)\
00072     (CppUnit::TestAssert::assertImplementation ((condition),(#condition),\
00073         __LINE__, __FILE__))
00074 
00075 #else
00076 
00077 #  define CPPUNIT_ASSERT(condition)\
00078     (CppUnit::TestAssert::assertImplementation ((condition),"",\
00079         __LINE__, __FILE__))
00080 
00081 #endif
00082 
00089 #define CPPUNIT_ASSERT_MESSAGE(message,condition)\
00090   (CppUnit::TestAssert::assertImplementation( condition, \
00091                                               message, \
00092                                               __LINE__, \
00093                                               __FILE__ ) )
00094 
00096 
00100 #define CPPUNIT_ASSERT_EQUAL(expected,actual)\
00101   (CppUnit::TestAssert::assertEquals ((expected),\
00102     (actual),__LINE__,__FILE__))
00103 
00105 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)\
00106   (CppUnit::TestAssert::assertEquals ((expected),\
00107     (actual),(delta),__LINE__,__FILE__))
00108 
00109 
00110 // Backwards compatibility
00111 
00112 #if CPPUNIT_ENABLE_NAKED_ASSERT
00113 
00114 #undef assert
00115 #define assert(c)                 CPPUNIT_ASSERT(c)
00116 #define assertEqual(e,a)          CPPUNIT_ASSERT_EQUAL(e,a)
00117 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00118 #define assertLongsEqual(e,a)     CPPUNIT_ASSERT_EQUAL(e,a)
00119 
00120 #endif
00121 
00122 
00123 } // namespace CppUnit
00124 
00125 #endif  // CPPUNIT_TESTASSERT_H

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