1 /*
2 * Created by Phil on 08/11/2010.
3 * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
4 *
5 * Distributed under the Boost Software License, Version 1.0. (See accompanying
6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #ifdef __clang__
9 # pragma clang diagnostic ignored "-Wpadded"
10 # pragma clang diagnostic ignored "-Wc++98-compat"
11 #endif
12
13 #include "catch.hpp"
14
15 #include <string>
16 #include <limits>
17
18 struct TestData {
TestDataTestData19 TestData()
20 : int_seven( 7 ),
21 str_hello( "hello" ),
22 float_nine_point_one( 9.1f ),
23 double_pi( 3.1415926535 )
24 {}
25
26 int int_seven;
27 std::string str_hello;
28 float float_nine_point_one;
29 double double_pi;
30 };
31
32
33 struct TestDef {
operator +TestDef34 TestDef& operator + ( const std::string& ) {
35 return *this;
36 }
operator []TestDef37 TestDef& operator[]( const std::string& ) {
38 return *this;
39 }
40
41 };
42
43 // The "failing" tests all use the CHECK macro, which continues if the specific test fails.
44 // This allows us to see all results, even if an earlier check fails
45
46 // Equality tests
47 TEST_CASE( "Equality checks that should succeed", "" )
48 {
49
50 TestDef td;
51 td + "hello" + "hello";
52
53 TestData data;
54
55 REQUIRE( data.int_seven == 7 );
56 REQUIRE( data.float_nine_point_one == Approx( 9.1f ) );
57 REQUIRE( data.double_pi == Approx( 3.1415926535 ) );
58 REQUIRE( data.str_hello == "hello" );
59 REQUIRE( "hello" == data.str_hello );
60 REQUIRE( data.str_hello.size() == 5 );
61
62 double x = 1.1 + 0.1 + 0.1;
63 REQUIRE( x == Approx( 1.3 ) );
64 }
65
66 TEST_CASE( "Equality checks that should fail", "[.][failing][!mayfail]" )
67 {
68 TestData data;
69
70 CHECK( data.int_seven == 6 );
71 CHECK( data.int_seven == 8 );
72 CHECK( data.int_seven == 0 );
73 CHECK( data.float_nine_point_one == Approx( 9.11f ) );
74 CHECK( data.float_nine_point_one == Approx( 9.0f ) );
75 CHECK( data.float_nine_point_one == Approx( 1 ) );
76 CHECK( data.float_nine_point_one == Approx( 0 ) );
77 CHECK( data.double_pi == Approx( 3.1415 ) );
78 CHECK( data.str_hello == "goodbye" );
79 CHECK( data.str_hello == "hell" );
80 CHECK( data.str_hello == "hello1" );
81 CHECK( data.str_hello.size() == 6 );
82
83 double x = 1.1 + 0.1 + 0.1;
84 CHECK( x == Approx( 1.301 ) );
85 }
86
87 TEST_CASE( "Inequality checks that should succeed", "" )
88 {
89 TestData data;
90
91 REQUIRE( data.int_seven != 6 );
92 REQUIRE( data.int_seven != 8 );
93 REQUIRE( data.float_nine_point_one != Approx( 9.11f ) );
94 REQUIRE( data.float_nine_point_one != Approx( 9.0f ) );
95 REQUIRE( data.float_nine_point_one != Approx( 1 ) );
96 REQUIRE( data.float_nine_point_one != Approx( 0 ) );
97 REQUIRE( data.double_pi != Approx( 3.1415 ) );
98 REQUIRE( data.str_hello != "goodbye" );
99 REQUIRE( data.str_hello != "hell" );
100 REQUIRE( data.str_hello != "hello1" );
101 REQUIRE( data.str_hello.size() != 6 );
102 }
103
104 TEST_CASE( "Inequality checks that should fail", "[.][failing][!shouldfail]" )
105 {
106 TestData data;
107
108 CHECK( data.int_seven != 7 );
109 CHECK( data.float_nine_point_one != Approx( 9.1f ) );
110 CHECK( data.double_pi != Approx( 3.1415926535 ) );
111 CHECK( data.str_hello != "hello" );
112 CHECK( data.str_hello.size() != 5 );
113 }
114
115 // Ordering comparison tests
116 TEST_CASE( "Ordering comparison checks that should succeed", "" )
117 {
118 TestData data;
119
120 REQUIRE( data.int_seven < 8 );
121 REQUIRE( data.int_seven > 6 );
122 REQUIRE( data.int_seven > 0 );
123 REQUIRE( data.int_seven > -1 );
124
125 REQUIRE( data.int_seven >= 7 );
126 REQUIRE( data.int_seven >= 6 );
127 REQUIRE( data.int_seven <= 7 );
128 REQUIRE( data.int_seven <= 8 );
129
130 REQUIRE( data.float_nine_point_one > 9 );
131 REQUIRE( data.float_nine_point_one < 10 );
132 REQUIRE( data.float_nine_point_one < 9.2 );
133
134 REQUIRE( data.str_hello <= "hello" );
135 REQUIRE( data.str_hello >= "hello" );
136
137 REQUIRE( data.str_hello < "hellp" );
138 REQUIRE( data.str_hello < "zebra" );
139 REQUIRE( data.str_hello > "hellm" );
140 REQUIRE( data.str_hello > "a" );
141 }
142
143 TEST_CASE( "Ordering comparison checks that should fail", "[.][failing]" )
144 {
145 TestData data;
146
147 CHECK( data.int_seven > 7 );
148 CHECK( data.int_seven < 7 );
149 CHECK( data.int_seven > 8 );
150 CHECK( data.int_seven < 6 );
151 CHECK( data.int_seven < 0 );
152 CHECK( data.int_seven < -1 );
153
154 CHECK( data.int_seven >= 8 );
155 CHECK( data.int_seven <= 6 );
156
157 CHECK( data.float_nine_point_one < 9 );
158 CHECK( data.float_nine_point_one > 10 );
159 CHECK( data.float_nine_point_one > 9.2 );
160
161 CHECK( data.str_hello > "hello" );
162 CHECK( data.str_hello < "hello" );
163 CHECK( data.str_hello > "hellp" );
164 CHECK( data.str_hello > "z" );
165 CHECK( data.str_hello < "hellm" );
166 CHECK( data.str_hello < "a" );
167
168 CHECK( data.str_hello >= "z" );
169 CHECK( data.str_hello <= "a" );
170 }
171
172 // Comparisons with int literals
173 TEST_CASE( "Comparisons with int literals don't warn when mixing signed/ unsigned", "" )
174 {
175 int i = 1;
176 unsigned int ui = 2;
177 long l = 3;
178 unsigned long ul = 4;
179 char c = 5;
180 unsigned char uc = 6;
181
182 REQUIRE( i == 1 );
183 REQUIRE( ui == 2 );
184 REQUIRE( l == 3 );
185 REQUIRE( ul == 4 );
186 REQUIRE( c == 5 );
187 REQUIRE( uc == 6 );
188
189 REQUIRE( 1 == i );
190 REQUIRE( 2 == ui );
191 REQUIRE( 3 == l );
192 REQUIRE( 4 == ul );
193 REQUIRE( 5 == c );
194 REQUIRE( 6 == uc );
195
196 REQUIRE( (std::numeric_limits<unsigned long>::max)() > ul );
197 }
198
199 // Disable warnings about sign conversions for the next two tests
200 // (as we are deliberately invoking them)
201 // - Currently only disabled for GCC/ LLVM. Should add VC++ too
202 #ifdef __GNUC__
203 #pragma GCC diagnostic push
204 #pragma GCC diagnostic ignored "-Wsign-compare"
205 #pragma GCC diagnostic ignored "-Wsign-conversion"
206 #endif
207 #ifdef _MSC_VER
208 #pragma warning(disable:4389) // '==' : signed/unsigned mismatch
209 #endif
210
211 TEST_CASE( "comparisons between int variables", "" )
212 {
213 long long_var = 1L;
214 unsigned char unsigned_char_var = 1;
215 unsigned short unsigned_short_var = 1;
216 unsigned int unsigned_int_var = 1;
217 unsigned long unsigned_long_var = 1L;
218
219 REQUIRE( long_var == unsigned_char_var );
220 REQUIRE( long_var == unsigned_short_var );
221 REQUIRE( long_var == unsigned_int_var );
222 REQUIRE( long_var == unsigned_long_var );
223 }
224
225 TEST_CASE( "comparisons between const int variables", "" )
226 {
227 const unsigned char unsigned_char_var = 1;
228 const unsigned short unsigned_short_var = 1;
229 const unsigned int unsigned_int_var = 1;
230 const unsigned long unsigned_long_var = 1L;
231
232 REQUIRE( unsigned_char_var == 1 );
233 REQUIRE( unsigned_short_var == 1 );
234 REQUIRE( unsigned_int_var == 1 );
235 REQUIRE( unsigned_long_var == 1 );
236 }
237
238 TEST_CASE( "Comparisons between unsigned ints and negative signed ints match c++ standard behaviour", "" )
239 {
240 CHECK( ( -1 > 2u ) );
241 CHECK( -1 > 2u );
242
243 CHECK( ( 2u < -1 ) );
244 CHECK( 2u < -1 );
245
246 const int minInt = (std::numeric_limits<int>::min)();
247 CHECK( ( minInt > 2u ) );
248 CHECK( minInt > 2u );
249 }
250
251 template<typename T>
252 struct Ex
253 {
ExEx254 Ex( T ){}
255
operator ==Ex256 bool operator == ( const T& ) const { return true; }
operator *Ex257 T operator * ( const T& ) const { return T(); }
258 };
259
260 TEST_CASE( "Comparisons between ints where one side is computed", "" )
261 {
262 CHECK( 54 == 6*9 );
263 }
264
265 #ifdef __GNUC__
266 #pragma GCC diagnostic pop
267 #endif
268
returnsConstNull()269 inline const char* returnsConstNull(){ return CATCH_NULL; }
returnsNull()270 inline char* returnsNull(){ return CATCH_NULL; }
271
272 TEST_CASE( "Pointers can be compared to null", "" )
273 {
274 TestData* p = CATCH_NULL;
275 TestData* pNULL = CATCH_NULL;
276
277 REQUIRE( p == CATCH_NULL );
278 REQUIRE( p == pNULL );
279
280 TestData data;
281 p = &data;
282
283 REQUIRE( p != CATCH_NULL );
284
285 const TestData* cp = p;
286 REQUIRE( cp != CATCH_NULL );
287
288 const TestData* const cpc = p;
289 REQUIRE( cpc != CATCH_NULL );
290
291 REQUIRE( returnsNull() == CATCH_NULL );
292 REQUIRE( returnsConstNull() == CATCH_NULL );
293
294 REQUIRE( CATCH_NULL != p );
295 }
296
297 // Not (!) tests
298 // The problem with the ! operator is that it has right-to-left associativity.
299 // This means we can't isolate it when we decompose. The simple REQUIRE( !false ) form, therefore,
300 // cannot have the operand value extracted. The test will work correctly, and the situation
301 // is detected and a warning issued.
302 // An alternative form of the macros (CHECK_FALSE and REQUIRE_FALSE) can be used instead to capture
303 // the operand value.
304 TEST_CASE( "'Not' checks that should succeed", "" )
305 {
306 bool falseValue = false;
307
308 REQUIRE( false == false );
309 REQUIRE( true == true );
310 REQUIRE( !false );
311 REQUIRE_FALSE( false );
312
313 REQUIRE( !falseValue );
314 REQUIRE_FALSE( falseValue );
315
316 REQUIRE( !(1 == 2) );
317 REQUIRE_FALSE( 1 == 2 );
318 }
319
320 TEST_CASE( "'Not' checks that should fail", "[.][failing]" )
321 {
322 bool trueValue = true;
323
324 CHECK( false != false );
325 CHECK( true != true );
326 CHECK( !true );
327 CHECK_FALSE( true );
328
329 CHECK( !trueValue );
330 CHECK_FALSE( trueValue );
331
332 CHECK( !(1 == 1) );
333 CHECK_FALSE( 1 == 1 );
334 }
335
336