CppUnit project page FAQ

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>
9#include <stdio.h>
10#include <float.h> // For struct assertion_traits<double>
11
12// Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T
13// is an enum type:
14#if defined __GNUC__ && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
15#pragma GCC system_header
16#endif
17
18
20
54template <class T>
56{
57 static bool equal( const T& x, const T& y )
58 {
59 return x == y;
60 }
61
62 static bool less( const T& x, const T& y )
63 {
64 return x < y;
65 }
66
67 static bool lessEqual( const T& x, const T& y )
68 {
69 return x <= y;
70 }
71
72 static std::string toString( const T& x )
73 {
74 return CPPUNIT_NS::StringHelper::toString(x);
75 }
76};
77
86template <>
87struct assertion_traits<double>
88{
89 static bool equal( double x, double y )
90 {
91 return x == y;
92 }
93
94 static bool less( double x, double y )
95 {
96 return x < y;
97 }
98
99 static bool lessEqual( double x, double y )
100 {
101 return x <= y;
102 }
103
104 static std::string toString( double x )
105 {
106#ifdef DBL_DIG
107 const int precision = DBL_DIG;
108#else
109 const int precision = 15;
110#endif // #ifdef DBL_DIG
111 char buffer[128];
112#ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
113 sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
114#else
115 sprintf(buffer, "%.*g", precision, x);
116#endif
117 return buffer;
118 }
119};
120
121
126template <class T>
127void assertEquals( const T& expected,
128 const T& actual,
129 SourceLine sourceLine,
130 const std::string &message )
131{
132 if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
133 {
136 sourceLine,
137 message );
138 }
139}
140
141
147void CPPUNIT_API assertDoubleEquals( double expected,
148 double actual,
149 double delta,
150 SourceLine sourceLine,
151 const std::string &message );
152
153
158template <class T>
159void assertLess( const T& expected,
160 const T& actual,
161 SourceLine sourceLine,
162 const std::string& message )
163{
164 if ( !assertion_traits<T>::less(actual,expected) )
165 {
168 sourceLine,
169 message );
170 }
171}
172
173
178template <class T>
179void assertGreater( const T& expected,
180 const T& actual,
181 SourceLine sourceLine,
182 const std::string& message )
183{
184 if ( !assertion_traits<T>::less(expected,actual) )
185 {
188 sourceLine,
189 message );
190 }
191}
192
197template <class T>
198void assertLessEqual( const T& expected,
199 const T& actual,
200 SourceLine sourceLine,
201 const std::string& message )
202{
203 if ( !assertion_traits<T>::lessEqual(actual,expected) )
204 {
207 sourceLine,
208 message );
209 }
210}
211
216template <class T>
217void assertGreaterEqual( const T& expected,
218 const T& actual,
219 SourceLine sourceLine,
220 const std::string& message )
221{
222 if ( !assertion_traits<T>::lessEqual(expected,actual) )
223 {
226 sourceLine,
227 message );
228 }
229}
230/* A set of macros which allow us to get the line number
231 * and file name at the point of an error.
232 * Just goes to show that preprocessors do have some
233 * redeeming qualities.
234 */
235#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
239#define CPPUNIT_ASSERT(condition) \
240 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
241 CPPUNIT_NS::Message( "assertion failed", \
242 "Expression: " #condition), \
243 CPPUNIT_SOURCELINE() ) )
244#else
245#define CPPUNIT_ASSERT(condition) \
246 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
247 CPPUNIT_NS::Message( "assertion failed" ), \
248 CPPUNIT_SOURCELINE() ) )
249#endif
250
258#define CPPUNIT_ASSERT_MESSAGE(message,condition) \
259 ( CPPUNIT_NS::Asserter::failIf( !(condition), \
260 CPPUNIT_NS::Message( "assertion failed", \
261 "Expression: " \
262 #condition, \
263 message ), \
264 CPPUNIT_SOURCELINE() ) )
265
270#define CPPUNIT_FAIL( message ) \
271 ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
272 message ), \
273 CPPUNIT_SOURCELINE() ) )
274
275#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
277#define CPPUNIT_ASSERT_EQUAL(expected,actual) \
278 ( CPPUNIT_NS::assertEquals( (expected), \
279 (actual), \
280 __LINE__, __FILE__ ) )
281#else
298#define CPPUNIT_ASSERT_EQUAL(expected,actual) \
299 ( CPPUNIT_NS::assertEquals( (expected), \
300 (actual), \
301 CPPUNIT_SOURCELINE(), \
302 "" ) )
303
322#define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
323 ( CPPUNIT_NS::assertEquals( (expected), \
324 (actual), \
325 CPPUNIT_SOURCELINE(), \
326 (message) ) )
327#endif
328
349#define CPPUNIT_ASSERT_LESS(expected, actual) \
350 ( CPPUNIT_NS::assertLess( (expected), \
351 (actual), \
352 CPPUNIT_SOURCELINE(), \
353 "" ) )
354
375#define CPPUNIT_ASSERT_GREATER(expected, actual) \
376 ( CPPUNIT_NS::assertGreater( (expected), \
377 (actual), \
378 CPPUNIT_SOURCELINE(), \
379 "" ) )
380
401#define CPPUNIT_ASSERT_LESSEQUAL(expected, actual) \
402 ( CPPUNIT_NS::assertLessEqual( (expected), \
403 (actual), \
404 CPPUNIT_SOURCELINE(), \
405 "" ) )
406
427#define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual) \
428 ( CPPUNIT_NS::assertGreaterEqual( (expected), \
429 (actual), \
430 CPPUNIT_SOURCELINE(), \
431 "" ) )
442#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
443 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
444 (actual), \
445 (delta), \
446 CPPUNIT_SOURCELINE(), \
447 "" ) )
448
449
455#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
456 ( CPPUNIT_NS::assertDoubleEquals( (expected), \
457 (actual), \
458 (delta), \
459 CPPUNIT_SOURCELINE(), \
460 (message) ) )
461
462
471# define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
472 CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
473 expression, \
474 ExceptionType )
475
476
477// implementation detail
478#if defined(CPPUNIT_USE_TYPEINFO_NAME)
479#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
480 CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
481#else
482#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
483 std::string( no_rtti_message )
484#endif // CPPUNIT_USE_TYPEINFO_NAME
485
486// implementation detail
487#define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
488
498# define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
499 do { \
500 bool cpputCorrectExceptionThrown_ = false; \
501 CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
502 cpputMsg_.addDetail( message ); \
503 cpputMsg_.addDetail( "Expected: " \
504 CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
505 \
506 try { \
507 expression; \
508 } catch ( const ExceptionType & ) { \
509 cpputCorrectExceptionThrown_ = true; \
510 } catch ( const std::exception &e) { \
511 cpputMsg_.addDetail( "Actual : " + \
512 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
513 "std::exception or derived") ); \
514 cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
515 } catch ( ... ) { \
516 cpputMsg_.addDetail( "Actual : unknown."); \
517 } \
518 \
519 if ( cpputCorrectExceptionThrown_ ) \
520 break; \
521 \
522 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
523 CPPUNIT_SOURCELINE() ); \
524 } while ( false )
525
526
536# define CPPUNIT_ASSERT_NO_THROW( expression ) \
537 CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
538 expression )
539
540
551# define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
552 do { \
553 CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
554 cpputMsg_.addDetail( message ); \
555 \
556 try { \
557 expression; \
558 } catch ( const std::exception &e ) { \
559 cpputMsg_.addDetail( "Caught: " + \
560 CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
561 "std::exception or derived" ) ); \
562 cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
563 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
564 CPPUNIT_SOURCELINE() ); \
565 } catch ( ... ) { \
566 cpputMsg_.addDetail( "Caught: unknown." ); \
567 CPPUNIT_NS::Asserter::fail( cpputMsg_, \
568 CPPUNIT_SOURCELINE() ); \
569 } \
570 } while ( false )
571
572
581# define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
582 CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
583
584
594# define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
595 CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
596
597
606# define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
607 CPPUNIT_ASSERT_NO_THROW( assertion )
608
609
619# define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
620 CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
621
622
623
624
625// Backwards compatibility
626
627#if CPPUNIT_ENABLE_NAKED_ASSERT
628
629#undef assert
630#define assert(c) CPPUNIT_ASSERT(c)
631#define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
632#define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
633#define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a)
634
635#endif
636
637
639
640#endif // CPPUNIT_TESTASSERT_H
#define CPPUNIT_API
Definition CppUnitApi.h:27
#define CPPUNIT_NS_END
Definition Portability.h:106
#define CPPUNIT_NS_BEGIN
Definition Portability.h:105
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:127
void assertLess(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that an object is less than another one of the same type Use CPPUNIT_ASSERT_...
Definition TestAssert.h:159
void assertGreater(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that an object is less than another one of the same type Use CPPUNIT_ASSERT_...
Definition TestAssert.h:179
void assertGreaterEqual(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_LESSEQUAL,...
Definition TestAssert.h:217
void assertLessEqual(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_LESSEQUAL,...
Definition TestAssert.h:198
Represents a source line location.
Definition SourceLine.h:31
static void CPPUNIT_API failNotLess(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="less assertion failed")
Throws an Exception with the specified message and location.
Definition Asserter.cpp:128
static void CPPUNIT_API failNotGreaterEqual(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="greater equal assertion failed")
Throws an Exception with the specified message and location.
Definition Asserter.cpp:171
static NORETURN 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:113
static void CPPUNIT_API failNotGreater(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="greater assertion failed")
Throws an Exception with the specified message and location.
Definition Asserter.cpp:143
static void CPPUNIT_API failNotLessEqual(std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="less equal assertion failed")
Throws an Exception with the specified message and location.
Definition Asserter.cpp:157
static std::string toString(double x)
Definition TestAssert.h:104
static bool equal(double x, double y)
Definition TestAssert.h:89
static bool lessEqual(double x, double y)
Definition TestAssert.h:99
static bool less(double x, double y)
Definition TestAssert.h:94
Traits used by CPPUNIT_ASSERT* macros.
Definition TestAssert.h:56
static std::string toString(const T &x)
Definition TestAssert.h:72
static bool equal(const T &x, const T &y)
Definition TestAssert.h:57
static bool lessEqual(const T &x, const T &y)
Definition TestAssert.h:67
static bool less(const T &x, const T &y)
Definition TestAssert.h:62

Send comments to:
CppUnit Developers