CppUnit project page
FAQ
CppUnit home page
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
include
cppunit
TestAssert.h
Go to the documentation of this file.
1
#ifndef CPPUNIT_TESTASSERT_H
2
#define CPPUNIT_TESTASSERT_H
3
4
#include <
cppunit/Portability.h
>
5
#include <
cppunit/Exception.h
>
6
#include <
cppunit/Asserter.h
>
7
#include <
cppunit/portability/Stream.h
>
8
#include <stdio.h>
9
#include <float.h>
// For struct assertion_traits<double>
10
11
12
CPPUNIT_NS_BEGIN
13
14
38
template
<
class
T>
39
struct
assertion_traits
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
63
template
<>
64
struct
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
93
template
<
class
T>
94
void
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
{
101
Asserter::failNotEqual
(
assertion_traits<T>::toString
(expected),
102
assertion_traits<T>::toString
(actual),
103
sourceLine,
104
message );
105
}
106
}
107
108
113
void
CPPUNIT_API
assertDoubleEquals
(
double
expected,
114
double
actual,
115
double
delta,
116
SourceLine
sourceLine,
117
const
std::string &message );
118
119
120
/* A set of macros which allow us to get the line number
121
* and file name at the point of an error.
122
* Just goes to show that preprocessors do have some
123
* redeeming qualities.
124
*/
125
#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
126
129
#define CPPUNIT_ASSERT(condition) \
130
( CPPUNIT_NS::Asserter::failIf( !(condition), \
131
CPPUNIT_NS::Message( "assertion failed", \
132
"Expression: " #condition), \
133
CPPUNIT_SOURCELINE() ) )
134
#else
135
#define CPPUNIT_ASSERT(condition) \
136
( CPPUNIT_NS::Asserter::failIf( !(condition), \
137
CPPUNIT_NS::Message( "assertion failed" ), \
138
CPPUNIT_SOURCELINE() ) )
139
#endif
140
148
#define CPPUNIT_ASSERT_MESSAGE(message,condition) \
149
( CPPUNIT_NS::Asserter::failIf( !(condition), \
150
CPPUNIT_NS::Message( "assertion failed", \
151
"Expression: " \
152
#condition, \
153
message ), \
154
CPPUNIT_SOURCELINE() ) )
155
160
#define CPPUNIT_FAIL( message ) \
161
( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
162
message ), \
163
CPPUNIT_SOURCELINE() ) )
164
165
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
166
167
#define CPPUNIT_ASSERT_EQUAL(expected,actual) \
168
( CPPUNIT_NS::assertEquals( (expected), \
169
(actual), \
170
__LINE__, __FILE__ ) )
171
#else
172
188
#define CPPUNIT_ASSERT_EQUAL(expected,actual) \
189
( CPPUNIT_NS::assertEquals( (expected), \
190
(actual), \
191
CPPUNIT_SOURCELINE(), \
192
"" ) )
193
212
#define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual) \
213
( CPPUNIT_NS::assertEquals( (expected), \
214
(actual), \
215
CPPUNIT_SOURCELINE(), \
216
(message) ) )
217
#endif
218
222
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
223
( CPPUNIT_NS::assertDoubleEquals( (expected), \
224
(actual), \
225
(delta), \
226
CPPUNIT_SOURCELINE(), \
227
"" ) )
228
229
234
#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
235
( CPPUNIT_NS::assertDoubleEquals( (expected), \
236
(actual), \
237
(delta), \
238
CPPUNIT_SOURCELINE(), \
239
(message) ) )
240
241
250
# define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
251
CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
252
expression, \
253
ExceptionType )
254
255
256
// implementation detail
257
#if CPPUNIT_USE_TYPEINFO_NAME
258
#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
259
CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
260
#else
261
#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
262
std::string( no_rtti_message )
263
#endif // CPPUNIT_USE_TYPEINFO_NAME
264
265
// implementation detail
266
#define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
267
277
# define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
278
do { \
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 ) ); \
284
\
285
try { \
286
expression; \
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() ); \
294
} catch ( ... ) { \
295
cpputMsg_.addDetail( "Actual : unknown."); \
296
} \
297
\
298
if ( cpputCorrectExceptionThrown_ ) \
299
break; \
300
\
301
CPPUNIT_NS::Asserter::fail( cpputMsg_, \
302
CPPUNIT_SOURCELINE() ); \
303
} while ( false )
304
305
315
# define CPPUNIT_ASSERT_NO_THROW( expression ) \
316
CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
317
expression )
318
319
330
# define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
331
do { \
332
CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
333
cpputMsg_.addDetail( message ); \
334
\
335
try { \
336
expression; \
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() ); \
344
} catch ( ... ) { \
345
cpputMsg_.addDetail( "Caught: unknown." ); \
346
CPPUNIT_NS::Asserter::fail( cpputMsg_, \
347
CPPUNIT_SOURCELINE() ); \
348
} \
349
} while ( false )
350
351
360
# define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \
361
CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
362
363
373
# define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
374
CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
375
376
385
# define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \
386
CPPUNIT_ASSERT_NO_THROW( assertion )
387
388
398
# define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
399
CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
400
401
402
403
404
// Backwards compatibility
405
406
#if CPPUNIT_ENABLE_NAKED_ASSERT
407
408
#undef 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)
413
414
#endif
415
416
417
CPPUNIT_NS_END
418
419
#endif // CPPUNIT_TESTASSERT_H
hosts this site.
Send comments to:
CppUnit Developers