blob: 9f879e7b01ea64d649c23751bb84df1ed869fff2 [file] [log] [blame]
Phil Nashd8026002010-11-09 23:24:00 +00001/*
2 * ConditionTests.cpp
3 * Catch - Test
4 *
5 * Created by Phil on 08/11/2010.
6 * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
7 *
8 * Distributed under the Boost Software License, Version 1.0. (See accompanying
9 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 *
11 */
12
Phil Nash7e8cfa72010-11-12 08:12:01 +000013#include "../catch.hpp"
Phil Nashd8026002010-11-09 23:24:00 +000014
15#include <string>
Phil Nash1e156692011-03-15 22:41:27 +000016#include <limits>
Phil Nashd8026002010-11-09 23:24:00 +000017
18struct TestData
19{
20 TestData()
21 : int_seven( 7 ),
22 str_hello( "hello" ),
23 float_nine_point_one( 9.1f ),
24 double_pi( 3.1415926535 )
25 {}
26
27 int int_seven;
28 std::string str_hello;
29 float float_nine_point_one;
30 double double_pi;
31};
32
Phil Nasha2d20952010-12-14 09:00:09 +000033// The "failing" tests all use the CHECK macro, which continues if the specific test fails.
34// This allows us to see all results, even if an earlier check fails
Phil Nashd8026002010-11-09 23:24:00 +000035
36// Equality tests
Phil Nash4e58d7c2011-04-01 08:15:45 +010037TEST_CASE( "./succeeding/conditions/equality",
38 "Equality checks that should succeed" )
Phil Nashd8026002010-11-09 23:24:00 +000039{
40 TestData data;
41
Phil Nasha2d20952010-12-14 09:00:09 +000042 REQUIRE( data.int_seven == 7 );
43 REQUIRE( data.float_nine_point_one == Approx( 9.1f ) );
44 REQUIRE( data.double_pi == Approx( 3.1415926535 ) );
45 REQUIRE( data.str_hello == "hello" );
46 REQUIRE( data.str_hello.size() == 5 );
Phil Nashd8026002010-11-09 23:24:00 +000047
48 double x = 1.1 + 0.1 + 0.1;
Phil Nasha2d20952010-12-14 09:00:09 +000049 REQUIRE( x == Approx( 1.3 ) );
Phil Nashd8026002010-11-09 23:24:00 +000050}
51
Phil Nash4e58d7c2011-04-01 08:15:45 +010052TEST_CASE( "./failing/conditions/equality",
53 "Equality checks that should fail" )
Phil Nashd8026002010-11-09 23:24:00 +000054{
55 TestData data;
56
57 CHECK( data.int_seven == 6 );
58 CHECK( data.int_seven == 8 );
59 CHECK( data.int_seven == 0 );
60 CHECK( data.float_nine_point_one == Approx( 9.11f ) );
61 CHECK( data.float_nine_point_one == Approx( 9.0f ) );
Phil Nashdb378d82011-01-31 10:10:20 +000062 CHECK( data.float_nine_point_one == Approx( 1 ) );
63 CHECK( data.float_nine_point_one == Approx( 0 ) );
Phil Nashd8026002010-11-09 23:24:00 +000064 CHECK( data.double_pi == Approx( 3.1415 ) );
65 CHECK( data.str_hello == "goodbye" );
66 CHECK( data.str_hello == "hell" );
67 CHECK( data.str_hello == "hello1" );
68 CHECK( data.str_hello.size() == 6 );
69
70 double x = 1.1 + 0.1 + 0.1;
71 CHECK( x == Approx( 1.301 ) );
72}
73
Phil Nash4e58d7c2011-04-01 08:15:45 +010074TEST_CASE( "./succeeding/conditions/inequality",
75 "Inequality checks that should succeed" )
Phil Nashd8026002010-11-09 23:24:00 +000076{
77 TestData data;
78
Phil Nasha2d20952010-12-14 09:00:09 +000079 REQUIRE( data.int_seven != 6 );
80 REQUIRE( data.int_seven != 8 );
81 REQUIRE( data.float_nine_point_one != Approx( 9.11f ) );
82 REQUIRE( data.float_nine_point_one != Approx( 9.0f ) );
Phil Nashdb378d82011-01-31 10:10:20 +000083 REQUIRE( data.float_nine_point_one != Approx( 1 ) );
84 REQUIRE( data.float_nine_point_one != Approx( 0 ) );
Phil Nasha2d20952010-12-14 09:00:09 +000085 REQUIRE( data.double_pi != Approx( 3.1415 ) );
86 REQUIRE( data.str_hello != "goodbye" );
87 REQUIRE( data.str_hello != "hell" );
88 REQUIRE( data.str_hello != "hello1" );
89 REQUIRE( data.str_hello.size() != 6 );
Phil Nashd8026002010-11-09 23:24:00 +000090}
91
Phil Nash4e58d7c2011-04-01 08:15:45 +010092TEST_CASE( "./failing/conditions/inequality",
93 "Inequality checks that should fails" )
Phil Nashd8026002010-11-09 23:24:00 +000094{
95 TestData data;
96
97 CHECK( data.int_seven != 7 );
98 CHECK( data.float_nine_point_one != Approx( 9.1f ) );
99 CHECK( data.double_pi != Approx( 3.1415926535 ) );
100 CHECK( data.str_hello != "hello" );
101 CHECK( data.str_hello.size() != 5 );
102}
103
104// Ordering comparison tests
Phil Nash4e58d7c2011-04-01 08:15:45 +0100105TEST_CASE( "./succeeding/conditions/ordered",
106 "Ordering comparison checks that should succeed" )
Phil Nashd8026002010-11-09 23:24:00 +0000107{
108 TestData data;
109
Phil Nasha2d20952010-12-14 09:00:09 +0000110 REQUIRE( data.int_seven < 8 );
111 REQUIRE( data.int_seven > 6 );
112 REQUIRE( data.int_seven > 0 );
113 REQUIRE( data.int_seven > -1 );
Phil Nashd8026002010-11-09 23:24:00 +0000114
Phil Nasha2d20952010-12-14 09:00:09 +0000115 REQUIRE( data.int_seven >= 7 );
116 REQUIRE( data.int_seven >= 6 );
117 REQUIRE( data.int_seven <= 7 );
118 REQUIRE( data.int_seven <= 8 );
Phil Nashd8026002010-11-09 23:24:00 +0000119
Phil Nasha2d20952010-12-14 09:00:09 +0000120 REQUIRE( data.float_nine_point_one > 9 );
121 REQUIRE( data.float_nine_point_one < 10 );
122 REQUIRE( data.float_nine_point_one < 9.2 );
Phil Nashd8026002010-11-09 23:24:00 +0000123
Phil Nasha2d20952010-12-14 09:00:09 +0000124 REQUIRE( data.str_hello <= "hello" );
125 REQUIRE( data.str_hello >= "hello" );
Phil Nashd8026002010-11-09 23:24:00 +0000126
Phil Nasha2d20952010-12-14 09:00:09 +0000127 REQUIRE( data.str_hello < "hellp" );
128 REQUIRE( data.str_hello < "zebra" );
129 REQUIRE( data.str_hello > "hellm" );
130 REQUIRE( data.str_hello > "a" );
Phil Nashd8026002010-11-09 23:24:00 +0000131}
132
Phil Nash4e58d7c2011-04-01 08:15:45 +0100133TEST_CASE( "./failing/conditions/ordered",
134 "Ordering comparison checks that should fail" )
Phil Nashd8026002010-11-09 23:24:00 +0000135{
136 TestData data;
137
138 CHECK( data.int_seven > 7 );
139 CHECK( data.int_seven < 7 );
140 CHECK( data.int_seven > 8 );
141 CHECK( data.int_seven < 6 );
142 CHECK( data.int_seven < 0 );
143 CHECK( data.int_seven < -1 );
144
145 CHECK( data.int_seven >= 8 );
146 CHECK( data.int_seven <= 6 );
147
148 CHECK( data.float_nine_point_one < 9 );
149 CHECK( data.float_nine_point_one > 10 );
150 CHECK( data.float_nine_point_one > 9.2 );
151
152 CHECK( data.str_hello > "hello" );
153 CHECK( data.str_hello < "hello" );
154 CHECK( data.str_hello > "hellp" );
155 CHECK( data.str_hello > "z" );
156 CHECK( data.str_hello < "hellm" );
157 CHECK( data.str_hello < "a" );
158
159 CHECK( data.str_hello >= "z" );
160 CHECK( data.str_hello <= "a" );
161}
162
Phil Nashb7087892011-03-09 19:45:05 +0000163// Comparisons with int literals
Phil Nash4e58d7c2011-04-01 08:15:45 +0100164TEST_CASE( "./succeeding/conditions/int literals",
165 "Comparisons with int literals don't warn when mixing signed/ unsigned" )
Phil Nashb7087892011-03-09 19:45:05 +0000166{
167 int i = 1;
168 unsigned int ui = 2;
169 long l = 3;
170 unsigned long ul = 4;
171 char c = 5;
172 unsigned char uc = 6;
173
174 REQUIRE( i == 1 );
175 REQUIRE( ui == 2 );
176 REQUIRE( l == 3 );
177 REQUIRE( ul == 4 );
178 REQUIRE( c == 5 );
179 REQUIRE( uc == 6 );
180
181 REQUIRE( 1 == i );
182 REQUIRE( 2 == ui );
183 REQUIRE( 3 == l );
184 REQUIRE( 4 == ul );
185 REQUIRE( 5 == c );
186 REQUIRE( 6 == uc );
187
Phil Nash18553e02011-03-21 08:09:47 +0000188 REQUIRE( (std::numeric_limits<unsigned long>::max)() > ul );
Phil Nashb7087892011-03-09 19:45:05 +0000189}
190
Phil Nash4e58d7c2011-04-01 08:15:45 +0100191// These are not built normally to avoid warnings about signed/ unsigned
192#ifdef ALLOW_TESTS_THAT_WARN
193TEST_CASE( "succeeding/conditions/negative ints",
194 "Comparisons between unsigned ints and negative signed ints match c++ standard behaviour" )
Phil Nashf6758c22011-03-15 22:22:19 +0000195{
Phil Nash4e58d7c2011-04-01 08:15:45 +0100196 CHECK( ( -1 > 2u ) );
197 CHECK( -1 > 2u );
Phil Nashf6758c22011-03-15 22:22:19 +0000198
Phil Nash4e58d7c2011-04-01 08:15:45 +0100199 CHECK( ( 2u < -1 ) );
200 CHECK( 2u < -1 );
201
202 const int minInt = (std::numeric_limits<int>::min)();
203 CHECK( ( minInt > 2u ) );
204 CHECK( minInt > 2u );
Phil Nashf6758c22011-03-15 22:22:19 +0000205}
Phil Nash4e58d7c2011-04-01 08:15:45 +0100206#endif
Phil Nashf6758c22011-03-15 22:22:19 +0000207
Phil Nash4e58d7c2011-04-01 08:15:45 +0100208TEST_CASE( "./succeeding/conditions/ptr",
209 "Pointers can be compared to null" )
Phil Nashef611c62011-03-18 14:39:58 +0000210{
211 TestData* p = NULL;
212 TestData* pNULL = NULL;
213
214 REQUIRE( p == NULL );
215 REQUIRE( p == pNULL );
216
217 TestData data;
218 p = &data;
Phil Nash7dfd8302011-03-18 19:08:33 +0000219
Phil Nashef611c62011-03-18 14:39:58 +0000220 REQUIRE( p != NULL );
Phil Nash7dfd8302011-03-18 19:08:33 +0000221
222 const TestData* cp = p;
223 REQUIRE( cp != NULL );
224
225 const TestData* const cpc = p;
226 REQUIRE( cpc != NULL );
227
228// REQUIRE( NULL != p ); // gives warning, but should compile and run ok
Phil Nashef611c62011-03-18 14:39:58 +0000229}
230
Phil Nashd8026002010-11-09 23:24:00 +0000231// Not (!) tests
Phil Nash16a6dd52010-11-10 07:59:07 +0000232// The problem with the ! operator is that it has right-to-left associativity.
Phil Nashaae41832010-12-14 09:05:51 +0000233// This means we can't isolate it when we decompose. The simple REQUIRE( !false ) form, therefore,
Phil Nash16a6dd52010-11-10 07:59:07 +0000234// cannot have the operand value extracted. The test will work correctly, and the situation
235// is detected and a warning issued.
Phil Nasha2d20952010-12-14 09:00:09 +0000236// An alternative form of the macros (CHECK_FALSE and REQUIRE_FALSE) can be used instead to capture
Phil Nash16a6dd52010-11-10 07:59:07 +0000237// the operand value.
Phil Nash4e58d7c2011-04-01 08:15:45 +0100238TEST_CASE( "./succeeding/conditions/not",
239 "'Not' checks that should succeed" )
Phil Nashd8026002010-11-09 23:24:00 +0000240{
241 bool falseValue = false;
242
Phil Nasha2d20952010-12-14 09:00:09 +0000243 REQUIRE( !false );
244 REQUIRE_FALSE( false );
Phil Nashd8026002010-11-09 23:24:00 +0000245
Phil Nasha2d20952010-12-14 09:00:09 +0000246 REQUIRE( !falseValue );
247 REQUIRE_FALSE( falseValue );
Phil Nashd8026002010-11-09 23:24:00 +0000248
Phil Nasha2d20952010-12-14 09:00:09 +0000249 REQUIRE( !(1 == 2) );
250 REQUIRE_FALSE( 1 == 2 );
Phil Nashd8026002010-11-09 23:24:00 +0000251}
252
Phil Nash4e58d7c2011-04-01 08:15:45 +0100253TEST_CASE( "./failing/conditions/not",
254 "'Not' checks that should fail" )
Phil Nashd8026002010-11-09 23:24:00 +0000255{
256 bool trueValue = true;
257
258 CHECK( !true );
Phil Nasha2d20952010-12-14 09:00:09 +0000259 CHECK_FALSE( true );
Phil Nashd8026002010-11-09 23:24:00 +0000260
261 CHECK( !trueValue );
Phil Nasha2d20952010-12-14 09:00:09 +0000262 CHECK_FALSE( trueValue );
Phil Nashd8026002010-11-09 23:24:00 +0000263
264 CHECK( !(1 == 1) );
Phil Nasha2d20952010-12-14 09:00:09 +0000265 CHECK_FALSE( 1 == 1 );
Phil Nashd8026002010-11-09 23:24:00 +0000266}
267