1 #ifndef CPPUNIT_TESTASSERT_H
2 #define CPPUNIT_TESTASSERT_H
41 static bool equal(
const T& x,
const T& y )
66 static bool equal(
double x,
double y )
74 const int precision = DBL_DIG;
76 const int precision = 15;
77 #endif // #ifdef DBL_DIG
79 #ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
80 sprintf_s(buffer,
sizeof(buffer),
"%.*g", precision, x);
82 sprintf(buffer,
"%.*g", precision, x);
97 const std::string &message )
117 const std::string &message );
125 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
129 #define CPPUNIT_ASSERT(condition) \
130 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
131 CPPUNIT_NS::Message( "assertion failed", \
132 "Expression: " #condition), \
133 CPPUNIT_SOURCELINE() ) )
135 #define CPPUNIT_ASSERT(condition) \
136 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
137 CPPUNIT_NS::Message( "assertion failed" ), \
138 CPPUNIT_SOURCELINE() ) )
148 #define CPPUNIT_ASSERT_MESSAGE(message,condition) \
149 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
150 CPPUNIT_NS::Message( "assertion failed", \
154 CPPUNIT_SOURCELINE() ) )
160 #define CPPUNIT_FAIL( message ) \
161 ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
163 CPPUNIT_SOURCELINE() ) )
165 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
166 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
168 ( CPPUNIT_NS::assertEquals( (expected), \
170 __LINE__, __FILE__ ) )
188 #define CPPUNIT_ASSERT_EQUAL(expected,actual) \
189 ( CPPUNIT_NS::assertEquals( (expected), \
191 CPPUNIT_SOURCELINE(), \
212 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
213 ( CPPUNIT_NS::assertEquals( (expected), \
215 CPPUNIT_SOURCELINE(), \
222 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
223 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
226 CPPUNIT_SOURCELINE(), \
234 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
235 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
238 CPPUNIT_SOURCELINE(), \
250 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
251 CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
257 #if CPPUNIT_USE_TYPEINFO_NAME
258 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
259 CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
261 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
262 std::string( no_rtti_message )
263 #endif // CPPUNIT_USE_TYPEINFO_NAME
266 #define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
277 # define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
279 bool cpputCorrectExceptionThrown_ = false; \
280 CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
281 cpputMsg_.addDetail( message ); \
282 cpputMsg_.addDetail( "Expected: " \
283 CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
287 } catch ( const ExceptionType & ) { \
288 cpputCorrectExceptionThrown_ = true; \
289 } catch ( const std::exception &e) { \
290 cpputMsg_.addDetail( "Actual : " + \
291 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
292 "std::exception or derived") ); \
293 cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
295 cpputMsg_.addDetail( "Actual : unknown."); \
298 if ( cpputCorrectExceptionThrown_ ) \
301 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
302 CPPUNIT_SOURCELINE() ); \
315 # define CPPUNIT_ASSERT_NO_THROW( expression ) \
316 CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
330 # define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
332 CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
333 cpputMsg_.addDetail( message ); \
337 } catch ( const std::exception &e ) { \
338 cpputMsg_.addDetail( "Caught: " + \
339 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
340 "std::exception or derived" ) ); \
341 cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
342 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
343 CPPUNIT_SOURCELINE() ); \
345 cpputMsg_.addDetail( "Caught: unknown." ); \
346 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
347 CPPUNIT_SOURCELINE() ); \
360 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
361 CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
373 # define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
374 CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
385 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
386 CPPUNIT_ASSERT_NO_THROW( assertion )
398 # define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
399 CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
406 #if CPPUNIT_ENABLE_NAKED_ASSERT
409 #define assert(c) CPPUNIT_ASSERT(c)
410 #define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
411 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
412 #define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
419 #endif // CPPUNIT_TESTASSERT_H
static bool equal(double x, double y)
Definition: TestAssert.h:66
static bool equal(const T &x, const T &y)
Definition: TestAssert.h:41
void assertEquals(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_EQUAL inste...
Definition: TestAssert.h:94
Traits used by CPPUNIT_ASSERT_EQUAL().
Definition: TestAssert.h:39
void CPPUNIT_API assertDoubleEquals(double expected, double actual, double delta, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two double are equals given a tolerance. Use CPPUNIT_ASSERT_DOUBLES_EQU...
Definition: TestAssert.cpp:14
static std::string toString(double x)
Definition: TestAssert.h:71
Represents a source line location.Used to capture the failure location in assertion.
Definition: SourceLine.h:30
static void CPPUNIT_API failNotEqual(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="equality assertion failed")
Throws an Exception with the specified message and location.
Definition: Asserter.cpp:74
#define CPPUNIT_NS_END
Definition: Portability.h:120
#define CPPUNIT_NS_BEGIN
Definition: Portability.h:119
static std::string toString(const T &x)
Definition: TestAssert.h:46
#define CPPUNIT_API
Definition: CppUnitApi.h:27