Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Nash | 7e8cfa7 | 2010-11-12 08:12:01 +0000 | [diff] [blame] | 13 | #include "../catch.hpp" |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 14 | |
| 15 | #include <string> |
Phil Nash | 1e15669 | 2011-03-15 22:41:27 +0000 | [diff] [blame] | 16 | #include <limits> |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 17 | |
| 18 | struct 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 Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 33 | // 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 Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 35 | |
| 36 | // Equality tests |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 37 | TEST_CASE( "./succeeding/conditions/equality", |
| 38 | "Equality checks that should succeed" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 39 | { |
| 40 | TestData data; |
| 41 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 42 | 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 Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 47 | |
| 48 | double x = 1.1 + 0.1 + 0.1; |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 49 | REQUIRE( x == Approx( 1.3 ) ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 52 | TEST_CASE( "./failing/conditions/equality", |
| 53 | "Equality checks that should fail" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 54 | { |
| 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 Nash | db378d8 | 2011-01-31 10:10:20 +0000 | [diff] [blame] | 62 | CHECK( data.float_nine_point_one == Approx( 1 ) ); |
| 63 | CHECK( data.float_nine_point_one == Approx( 0 ) ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 64 | 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 Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 74 | TEST_CASE( "./succeeding/conditions/inequality", |
| 75 | "Inequality checks that should succeed" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 76 | { |
| 77 | TestData data; |
| 78 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 79 | 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 Nash | db378d8 | 2011-01-31 10:10:20 +0000 | [diff] [blame] | 83 | REQUIRE( data.float_nine_point_one != Approx( 1 ) ); |
| 84 | REQUIRE( data.float_nine_point_one != Approx( 0 ) ); |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 85 | 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 Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 92 | TEST_CASE( "./failing/conditions/inequality", |
| 93 | "Inequality checks that should fails" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 94 | { |
| 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 Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 105 | TEST_CASE( "./succeeding/conditions/ordered", |
| 106 | "Ordering comparison checks that should succeed" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 107 | { |
| 108 | TestData data; |
| 109 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 110 | REQUIRE( data.int_seven < 8 ); |
| 111 | REQUIRE( data.int_seven > 6 ); |
| 112 | REQUIRE( data.int_seven > 0 ); |
| 113 | REQUIRE( data.int_seven > -1 ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 114 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 115 | REQUIRE( data.int_seven >= 7 ); |
| 116 | REQUIRE( data.int_seven >= 6 ); |
| 117 | REQUIRE( data.int_seven <= 7 ); |
| 118 | REQUIRE( data.int_seven <= 8 ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 119 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 120 | 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 Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 123 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 124 | REQUIRE( data.str_hello <= "hello" ); |
| 125 | REQUIRE( data.str_hello >= "hello" ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 126 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 127 | REQUIRE( data.str_hello < "hellp" ); |
| 128 | REQUIRE( data.str_hello < "zebra" ); |
| 129 | REQUIRE( data.str_hello > "hellm" ); |
| 130 | REQUIRE( data.str_hello > "a" ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 133 | TEST_CASE( "./failing/conditions/ordered", |
| 134 | "Ordering comparison checks that should fail" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 135 | { |
| 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 Nash | b708789 | 2011-03-09 19:45:05 +0000 | [diff] [blame] | 163 | // Comparisons with int literals |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 164 | TEST_CASE( "./succeeding/conditions/int literals", |
| 165 | "Comparisons with int literals don't warn when mixing signed/ unsigned" ) |
Phil Nash | b708789 | 2011-03-09 19:45:05 +0000 | [diff] [blame] | 166 | { |
| 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 Nash | 18553e0 | 2011-03-21 08:09:47 +0000 | [diff] [blame] | 188 | REQUIRE( (std::numeric_limits<unsigned long>::max)() > ul ); |
Phil Nash | b708789 | 2011-03-09 19:45:05 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 191 | // These are not built normally to avoid warnings about signed/ unsigned |
| 192 | #ifdef ALLOW_TESTS_THAT_WARN |
| 193 | TEST_CASE( "succeeding/conditions/negative ints", |
| 194 | "Comparisons between unsigned ints and negative signed ints match c++ standard behaviour" ) |
Phil Nash | f6758c2 | 2011-03-15 22:22:19 +0000 | [diff] [blame] | 195 | { |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 196 | CHECK( ( -1 > 2u ) ); |
| 197 | CHECK( -1 > 2u ); |
Phil Nash | f6758c2 | 2011-03-15 22:22:19 +0000 | [diff] [blame] | 198 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 199 | 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 Nash | f6758c2 | 2011-03-15 22:22:19 +0000 | [diff] [blame] | 205 | } |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 206 | #endif |
Phil Nash | f6758c2 | 2011-03-15 22:22:19 +0000 | [diff] [blame] | 207 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 208 | TEST_CASE( "./succeeding/conditions/ptr", |
| 209 | "Pointers can be compared to null" ) |
Phil Nash | ef611c6 | 2011-03-18 14:39:58 +0000 | [diff] [blame] | 210 | { |
| 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 Nash | 7dfd830 | 2011-03-18 19:08:33 +0000 | [diff] [blame] | 219 | |
Phil Nash | ef611c6 | 2011-03-18 14:39:58 +0000 | [diff] [blame] | 220 | REQUIRE( p != NULL ); |
Phil Nash | 7dfd830 | 2011-03-18 19:08:33 +0000 | [diff] [blame] | 221 | |
| 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 Nash | ef611c6 | 2011-03-18 14:39:58 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 231 | // Not (!) tests |
Phil Nash | 16a6dd5 | 2010-11-10 07:59:07 +0000 | [diff] [blame] | 232 | // The problem with the ! operator is that it has right-to-left associativity. |
Phil Nash | aae4183 | 2010-12-14 09:05:51 +0000 | [diff] [blame] | 233 | // This means we can't isolate it when we decompose. The simple REQUIRE( !false ) form, therefore, |
Phil Nash | 16a6dd5 | 2010-11-10 07:59:07 +0000 | [diff] [blame] | 234 | // cannot have the operand value extracted. The test will work correctly, and the situation |
| 235 | // is detected and a warning issued. |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 236 | // An alternative form of the macros (CHECK_FALSE and REQUIRE_FALSE) can be used instead to capture |
Phil Nash | 16a6dd5 | 2010-11-10 07:59:07 +0000 | [diff] [blame] | 237 | // the operand value. |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 238 | TEST_CASE( "./succeeding/conditions/not", |
| 239 | "'Not' checks that should succeed" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 240 | { |
| 241 | bool falseValue = false; |
| 242 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 243 | REQUIRE( !false ); |
| 244 | REQUIRE_FALSE( false ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 245 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 246 | REQUIRE( !falseValue ); |
| 247 | REQUIRE_FALSE( falseValue ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 248 | |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 249 | REQUIRE( !(1 == 2) ); |
| 250 | REQUIRE_FALSE( 1 == 2 ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Phil Nash | 4e58d7c | 2011-04-01 08:15:45 +0100 | [diff] [blame^] | 253 | TEST_CASE( "./failing/conditions/not", |
| 254 | "'Not' checks that should fail" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 255 | { |
| 256 | bool trueValue = true; |
| 257 | |
| 258 | CHECK( !true ); |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 259 | CHECK_FALSE( true ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 260 | |
| 261 | CHECK( !trueValue ); |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 262 | CHECK_FALSE( trueValue ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 263 | |
| 264 | CHECK( !(1 == 1) ); |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 265 | CHECK_FALSE( 1 == 1 ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 266 | } |
| 267 | |