1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#include "CoreSuite.h"
#include "TestAssertTest.h"
#include <algorithm>
/*
Note:
- tests need to be added to test asserEquals() template function and
use of assertion traits. Some check may need to be added to check
the message content in Exception.
- code need to be refactored with the use of a test caller that expect
an exception.
*/
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TestAssertTest,
coreSuiteName() );
TestAssertTest::TestAssertTest()
{
}
TestAssertTest::~TestAssertTest()
{
}
void
TestAssertTest::setUp()
{
}
void
TestAssertTest::tearDown()
{
}
void
TestAssertTest::testAssertThrow()
{
CPPUNIT_ASSERT_THROW( throw std::string(), std::string );
try
{
int x;
CPPUNIT_ASSERT_THROW( x = 1234, std::string );
}
catch ( CPPUNIT_NS::Exception & )
{
return;
}
throw std::exception();
}
void
TestAssertTest::testAssertNoThrow()
{
int x;
CPPUNIT_ASSERT_NO_THROW( x = 1234 );
try
{
CPPUNIT_ASSERT_NO_THROW( throw std::exception() );
}
catch ( CPPUNIT_NS::Exception & )
{
return;
}
throw std::exception();
}
void
TestAssertTest::testAssertAssertionFail()
{
CPPUNIT_ASSERT_ASSERTION_FAIL( throw CPPUNIT_NS::Exception() );
try
{
int x;
CPPUNIT_ASSERT_ASSERTION_FAIL( x = 1234 );
}
catch ( CPPUNIT_NS::Exception & )
{
return;
}
throw std::exception();
}
void
TestAssertTest::testAssertAssertionPass()
{
int x;
CPPUNIT_ASSERT_ASSERTION_PASS( x = 1234 );
try
{
CPPUNIT_ASSERT_ASSERTION_PASS( throw CPPUNIT_NS::Exception() );
}
catch ( CPPUNIT_NS::Exception & )
{
return;
}
throw std::exception();
}
void
TestAssertTest::testAssert()
{
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( true ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( false ) );
}
static int foo() { return 1; }
void
TestAssertTest::testAssertEqual()
{
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, 1 ) );
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, foo() ) );
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 12345678, 12345678 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( 1, 2 ) );
}
void
TestAssertTest::testAssertMessageTrue()
{
CPPUNIT_ASSERT_ASSERTION_PASS(
CPPUNIT_ASSERT_MESSAGE( "This test should not failed", true ) );
}
void
TestAssertTest::testAssertMessageFalse()
{
bool exceptionCaught = false;
std::string message( "This test message should not be seen" );
try
{
CPPUNIT_ASSERT_MESSAGE( message, 2==3 );
}
catch( CPPUNIT_NS::Exception &e )
{
exceptionCaught = true; // ok, we were expecting an exception.
checkMessageContains( &e, message );
}
CPPUNIT_ASSERT( exceptionCaught );
}
void
TestAssertTest::testAssertDoubleEquals()
{
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.101 ) );
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.101 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ) );
double inf = std::numeric_limits<double>::infinity();
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( inf, 0.0, 1.0 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, inf, 1.0 ) );
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( inf, inf, 1.0 ) );
double nan = std::numeric_limits<double>::quiet_NaN();
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( nan, 0.0, 1.0 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( nan, nan, 1.0 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( nan, inf, 1.0 ) );
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( inf, nan, 1.0 ) );
}
void
TestAssertTest::testFail()
{
bool exceptionCaught = false;
std::string failure( "FailureMessage" );
try
{
CPPUNIT_FAIL( failure );
}
catch( CPPUNIT_NS::Exception &e )
{
exceptionCaught = true;
checkMessageContains( &e, failure );
}
CPPUNIT_ASSERT( exceptionCaught );
}
void
TestAssertTest::checkMessageContains( CPPUNIT_NS::Exception *e,
std::string expected )
{
std::string actual = e->what();
CPPUNIT_ASSERT_MESSAGE( "Expected message not found: " + expected +
", was: " + actual,
std::search( actual.begin(), actual.end(),
expected.begin(), expected.end() ) != actual.end() );
}
|