CppUnit project page FAQ CppUnit home page

TestAssert.h
Go to the documentation of this file.
1#ifndef CPPUNIT_TESTASSERT_H
2#define CPPUNIT_TESTASSERT_H
3
5#include <cppunit/Exception.h>
6#include <cppunit/Asserter.h>
8#include <stdio.h>
9#include <float.h> // For struct assertion_traits<double>
10
11
13
14
38template <class T>
40{
41 static bool equal( const T& x, const T& y )
42 {
43 return x == y;
44 }
45
46 static std::string toString( const T& x )
47 {
48 OStringStream ost;
49 ost << x;
50 return ost.str();
51 }
52};
53
54
63template <>
64struct assertion_traits<double>
65{
66 static bool equal( double x, double y )
67 {
68 return x == y;
69 }
70
71 static std::string toString( double x )
72 {
73#ifdef DBL_DIG
74 const int precision = DBL_DIG;
75#else
76 const int precision = 15;
77#endif // #ifdef DBL_DIG
78 char buffer[128];
79#ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
80 sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
81#else
82 sprintf(buffer, "%.*g", precision, x);
83#endif
84 return buffer;
85 }
86};
87
88
93template <class T>
94void assertEquals( const T& expected,
95 const T& actual,
96 SourceLine sourceLine,
97 const std::string &message )
98{
99 if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
100 {
103 sourceLine,
104 message );
105 }
106}
107
108
114void CPPUNIT_API assertDoubleEquals( double expected,
115 double actual,
116 double delta,
117 SourceLine sourceLine,
118 const std::string &message );
119
120
121/* A set of macros which allow us to get the line number
122 * and file name at the point of an error.
123 * Just goes to show that preprocessors do have some
124 * redeeming qualities.
125 */
126#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
130#define CPPUNIT_ASSERT(condition) \
131 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
132 CPPUNIT_NS::Message( "assertion failed", \
133 "Expression: " #condition), \
134 CPPUNIT_SOURCELINE() ) )
135#else
136#define CPPUNIT_ASSERT(condition) \
137 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
138 CPPUNIT_NS::Message( "assertion failed" ), \
139 CPPUNIT_SOURCELINE() ) )
140#endif
141
149#define CPPUNIT_ASSERT_MESSAGE(message,condition) \
150 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
151 CPPUNIT_NS::Message( "assertion failed", \
152 "Expression: " \
153 #condition, \
154 message ), \
155 CPPUNIT_SOURCELINE() ) )
156
161#define CPPUNIT_FAIL( message ) \
162 ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
163 message ), \
164 CPPUNIT_SOURCELINE() ) )
165
166#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
168#define CPPUNIT_ASSERT_EQUAL(expected,actual) \
169 ( CPPUNIT_NS::assertEquals( (expected), \
170 (actual), \
171 __LINE__, __FILE__ ) )
172#else
189#define CPPUNIT_ASSERT_EQUAL(expected,actual) \
190 ( CPPUNIT_NS::assertEquals( (expected), \
191 (actual), \
192 CPPUNIT_SOURCELINE(), \
193 "" ) )
194
213#define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
214 ( CPPUNIT_NS::assertEquals( (expected), \
215 (actual), \
216 CPPUNIT_SOURCELINE(), \
217 (message) ) )
218#endif
219
230#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
231 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
232 (actual), \
233 (delta), \
234 CPPUNIT_SOURCELINE(), \
235 "" ) )
236
237
243#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
244 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
245 (actual), \
246 (delta), \
247 CPPUNIT_SOURCELINE(), \
248 (message) ) )
249
250
259# define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
260 CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
261 expression, \
262 ExceptionType )
263
264
265// implementation detail
266#if CPPUNIT_USE_TYPEINFO_NAME
267#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
268 CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
269#else
270#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
271 std::string( no_rtti_message )
272#endif // CPPUNIT_USE_TYPEINFO_NAME
273
274// implementation detail
275#define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
276
286# define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
287 do { \
288 bool cpputCorrectExceptionThrown_ = false; \
289 CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
290 cpputMsg_.addDetail( message ); \
291 cpputMsg_.addDetail( "Expected: " \
292 CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
293 \
294 try { \
295 expression; \
296 } catch ( const ExceptionType & ) { \
297 cpputCorrectExceptionThrown_ = true; \
298 } catch ( const std::exception &e) { \
299 cpputMsg_.addDetail( "Actual : " + \
300 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
301 "std::exception or derived") ); \
302 cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
303 } catch ( ... ) { \
304 cpputMsg_.addDetail( "Actual : unknown."); \
305 } \
306 \
307 if ( cpputCorrectExceptionThrown_ ) \
308 break; \
309 \
310 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
311 CPPUNIT_SOURCELINE() ); \
312 } while ( false )
313
314
324# define CPPUNIT_ASSERT_NO_THROW( expression ) \
325 CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
326 expression )
327
328
339# define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
340 do { \
341 CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
342 cpputMsg_.addDetail( message ); \
343 \
344 try { \
345 expression; \
346 } catch ( const std::exception &e ) { \
347 cpputMsg_.addDetail( "Caught: " + \
348 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
349 "std::exception or derived" ) ); \
350 cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
351 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
352 CPPUNIT_SOURCELINE() ); \
353 } catch ( ... ) { \
354 cpputMsg_.addDetail( "Caught: unknown." ); \
355 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
356 CPPUNIT_SOURCELINE() ); \
357 } \
358 } while ( false )
359
360
369# define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
370 CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
371
372
382# define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
383 CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
384
385
394# define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
395 CPPUNIT_ASSERT_NO_THROW( assertion )
396
397
407# define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
408 CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
409
410
411
412
413// Backwards compatibility
414
415#if CPPUNIT_ENABLE_NAKED_ASSERT
416
417#undef assert
418#define assert(c) CPPUNIT_ASSERT(c)
419#define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
420#define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
421#define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
422
423#endif
424
425
427
428#endif // CPPUNIT_TESTASSERT_H
#define CPPUNIT_API
Definition CppUnitApi.h:27
#define CPPUNIT_NS_END
Definition Portability.h:120
#define CPPUNIT_NS_BEGIN
Definition Portability.h:119
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:8
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
Represents a source line location.
Definition SourceLine.h:31
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
static std::string toString(double x)
Definition TestAssert.h:71
static bool equal(double x, double y)
Definition TestAssert.h:66
Traits used by CPPUNIT_ASSERT_EQUAL().
Definition TestAssert.h:40
static std::string toString(const T &x)
Definition TestAssert.h:46
static bool equal(const T &x, const T &y)
Definition TestAssert.h:41

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