1 #include "CoreSuite.h"
2 #include "TestAssertTest.h"
3 #include <cppunit/portability/FloatingPoint.h>
4 #include <algorithm>
5 #include <limits>
6 
7 /*
8  Note:
9  - tests need to be added to test asserEquals() template function and
10  use of assertion traits. Some check may need to be added to check
11  the message content in Exception.
12  - code need to be refactored with the use of a test caller that expect
13  an exception.
14  */
15 
16 enum class EnumClass
17 {
18     VALUE1,
19     VALUE2
20 };
21 
22 
23 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TestAssertTest,
24                                        coreSuiteName() );
25 
26 
TestAssertTest()27 TestAssertTest::TestAssertTest()
28 {
29 }
30 
31 
~TestAssertTest()32 TestAssertTest::~TestAssertTest()
33 {
34 }
35 
36 
37 void
setUp()38 TestAssertTest::setUp()
39 {
40 }
41 
42 
43 void
tearDown()44 TestAssertTest::tearDown()
45 {
46 }
47 
48 
49 void
testAssertThrow()50 TestAssertTest::testAssertThrow()
51 {
52    CPPUNIT_ASSERT_THROW( throw std::string(), std::string );
53 
54    try
55    {
56       int x;
57       CPPUNIT_ASSERT_THROW( x = 1234, std::string );
58       (void) x;
59    }
60    catch ( CPPUNIT_NS::Exception & )
61    {
62       return;
63    }
64 
65    throw std::exception();
66 }
67 
68 
69 void
testAssertNoThrow()70 TestAssertTest::testAssertNoThrow()
71 {
72    int x;
73    CPPUNIT_ASSERT_NO_THROW( x = 1234 );
74    (void)x;
75 
76    try
77    {
78       CPPUNIT_ASSERT_NO_THROW( throw std::exception() );
79    }
80    catch ( CPPUNIT_NS::Exception & )
81    {
82       return;
83    }
84    throw std::exception();
85 }
86 
87 
88 void
testAssertAssertionFail()89 TestAssertTest::testAssertAssertionFail()
90 {
91    CPPUNIT_ASSERT_ASSERTION_FAIL( throw CPPUNIT_NS::Exception() );
92 
93    try
94    {
95       int x;
96       CPPUNIT_ASSERT_ASSERTION_FAIL( x = 1234 );
97       (void)x;
98    }
99    catch ( CPPUNIT_NS::Exception & )
100    {
101       return;
102    }
103 
104    throw std::exception();
105 }
106 
107 
108 void
testAssertAssertionPass()109 TestAssertTest::testAssertAssertionPass()
110 {
111    int x;
112    CPPUNIT_ASSERT_ASSERTION_PASS( x = 1234 );
113    (void)x;
114 
115    try
116    {
117       CPPUNIT_ASSERT_ASSERTION_PASS( throw CPPUNIT_NS::Exception() );
118    }
119    catch ( CPPUNIT_NS::Exception & )
120    {
121       return;
122    }
123 
124    throw std::exception();
125 }
126 
127 
128 void
testAssert()129 TestAssertTest::testAssert()
130 {
131   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( true ) );
132 
133   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( false ) );
134 }
135 
136 
foo()137 static int foo() { return 1; }
138 
139 
140 void
testAssertEqual()141 TestAssertTest::testAssertEqual()
142 {
143   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, 1 ) );
144   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, foo() ) );
145   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 12345678, 12345678 ) );
146   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( EnumClass::VALUE1, EnumClass::VALUE1 ) );
147 
148   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( 1, 2 ) );
149   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( EnumClass::VALUE1, EnumClass::VALUE2 ) );
150 }
151 
152 
153 void
testAssertLess()154 TestAssertTest::testAssertLess()
155 {
156     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESS( 2, 1 ) );
157     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESS( 12345679, 12345678 ) );
158 
159     CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_LESS( 1, 2 ) );
160 }
161 
162 
163 void
testAssertGreater()164 TestAssertTest::testAssertGreater()
165 {
166     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATER( 1, 2 ) );
167     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATER( 12345678, 12345679 ));
168 
169     CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_GREATER( 2, 1 ) );
170     CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_GREATER( 2, 2 ) );
171 }
172 
173 
174 void
testAssertLessEqual()175 TestAssertTest::testAssertLessEqual()
176 {
177     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESSEQUAL( 2, 1 ) );
178     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESSEQUAL( 12345679, 12345678 ));
179     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESSEQUAL( 2, 2 ) );
180 
181     CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_LESSEQUAL( 1, 2 ) );
182 }
183 
184 void
testAssertGreaterEqual()185 TestAssertTest::testAssertGreaterEqual()
186 {
187     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 1, 2 ) );
188     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 12345678, 12345679 ));
189     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 12345678, 12345678 ));
190     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 2, 2 ) );
191 
192     CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_GREATEREQUAL( 2, 1 ) );
193 }
194 
195 
196 
197 void
testAssertMessageTrue()198 TestAssertTest::testAssertMessageTrue()
199 {
200   CPPUNIT_ASSERT_ASSERTION_PASS(
201      CPPUNIT_ASSERT_MESSAGE( "This test should not failed", true ) );
202 }
203 
204 
205 void
testAssertMessageFalse()206 TestAssertTest::testAssertMessageFalse()
207 {
208   bool exceptionCaught = false;
209   std::string message( "This test message should not be seen" );
210   try
211   {
212     CPPUNIT_ASSERT_MESSAGE( message, 2==3 );
213   }
214   catch( CPPUNIT_NS::Exception &e )
215   {
216     exceptionCaught = true; // ok, we were expecting an exception.
217     checkMessageContains( &e, message );
218   }
219 
220   CPPUNIT_ASSERT( exceptionCaught );
221 }
222 
223 
224 void
testAssertDoubleEquals()225 TestAssertTest::testAssertDoubleEquals()
226 {
227   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.101 ) );
228   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.101 ) );
229 
230   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ) );
231   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ) );
232 }
233 
234 /*
235  * Test that the error message from CPPUNIT_ASSERT_DOUBLES_EQUAL()
236  * has more than the default 6 digits of precision.
237  */
238 void
testAssertDoubleEqualsPrecision()239 TestAssertTest::testAssertDoubleEqualsPrecision()
240 {
241   std::string failure( "2.000000001" );
242   try
243   {
244     CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, 2.000000001, 1 );
245   }
246   catch( CPPUNIT_NS::Exception &e )
247   {
248     checkMessageContains( &e, failure );
249     return;
250   }
251   CPPUNIT_FAIL( "Expected assertion failure" );
252 }
253 
254 
255 void
testAssertDoubleNonFinite()256 TestAssertTest::testAssertDoubleNonFinite()
257 {
258   double inf = std::numeric_limits<double>::infinity();
259   double nan = std::numeric_limits<double>::quiet_NaN();
260   // test our portable floating-point primitives that detect NaN values
261   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsUnordered( nan ) );
262   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( inf ) );
263   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( -inf ) );
264   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( 1.0 ) );
265   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( 1.5 ) );
266   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( 2.0 ) );
267   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( 2.5 ) );
268   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( 0.0 ) );
269   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( -1.0 ) );
270   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsUnordered( -2.0 ) );
271   // test our portable floating-point primitives that detect finite values
272   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( 0.0 ) );
273   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( 0.5 ) );
274   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( 1.0 ) );
275   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( 1.5 ) );
276   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( 2.0 ) );
277   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( 2.5 ) );
278   CPPUNIT_ASSERT( CPPUNIT_NS::floatingPointIsFinite( -1.5 ) );
279   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsFinite( nan ) );
280   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsFinite( inf ) );
281   CPPUNIT_ASSERT( !CPPUNIT_NS::floatingPointIsFinite( -inf ) );
282   // Infinity tests
283   CPPUNIT_ASSERT( inf == inf );
284   CPPUNIT_ASSERT( -inf == -inf );
285   CPPUNIT_ASSERT( -inf != inf );
286   CPPUNIT_ASSERT( -inf < inf );
287   CPPUNIT_ASSERT( inf > -inf );
288   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( inf, 0.0, 1.0 ) );
289   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, inf, 1.0 ) );
290   CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( inf, inf, 1.0 ) );
291   // NaN tests
292   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( nan, 0.0, 1.0 ) );
293   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( nan, nan, 1.0 ) );
294   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( nan, inf, 1.0 ) );
295   CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( inf, nan, 1.0 ) );
296 }
297 
298 
299 void
testFail()300 TestAssertTest::testFail()
301 {
302   bool exceptionCaught = false;
303   std::string failure( "FailureMessage" );
304   try
305   {
306     CPPUNIT_FAIL( failure );
307   }
308   catch( CPPUNIT_NS::Exception &e )
309   {
310     exceptionCaught = true;
311     checkMessageContains( &e, failure );
312   }
313   CPPUNIT_ASSERT( exceptionCaught );
314 }
315 
316 
317 void
checkMessageContains(CPPUNIT_NS::Exception * e,std::string expected)318 TestAssertTest::checkMessageContains( CPPUNIT_NS::Exception *e,
319                                       std::string expected )
320 {
321   std::string actual = e->what();
322   CPPUNIT_ASSERT_MESSAGE( "Expected message not found: " + expected +
323                           ", was: " + actual,
324       std::search( actual.begin(), actual.end(),
325                    expected.begin(), expected.end() ) != actual.end() );
326 }
327