| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#ifdef __clang__ |
| 10 |
# pragma clang diagnostic push |
| 11 |
# pragma clang diagnostic ignored "-Wpadded" |
| 12 |
// Wdouble-promotion is not supported until 3.8 |
| 13 |
# if (__clang_major__ > 3) || (__clang_major__ == 3 && __clang_minor__ > 7) |
| 14 |
# pragma clang diagnostic ignored "-Wdouble-promotion" |
| 15 |
# endif |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <catch2/catch_approx.hpp> |
| 19 |
#include <catch2/catch_test_macros.hpp> |
| 20 |
|
| 21 |
using Catch::Approx; |
| 22 |
|
| 23 |
#include <string> |
| 24 |
#include <limits> |
| 25 |
#include <cstdint> |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
struct TestData { |
| 30 |
int int_seven = 7; |
| 31 |
std::string str_hello = "hello"; |
| 32 |
float float_nine_point_one = 9.1f; |
| 33 |
double double_pi = 3.1415926535; |
| 34 |
}; |
| 35 |
|
| 36 |
static const char* returnsConstNull() { return nullptr; } |
| 37 |
static char* returnsNull() { return nullptr; } |
| 38 |
|
| 39 |
} // end unnamed namespace |
| 40 |
|
| 41 |
// The "failing" tests all use the CHECK macro, which continues if the specific test fails. |
| 42 |
// This allows us to see all results, even if an earlier check fails |
| 43 |
|
| 44 |
// Equality tests |
| 45 |
TEST_CASE( "Equality checks that should succeed" ) |
| 46 |
{ |
| 47 |
TestData data; |
| 48 |
|
| 49 |
REQUIRE( data.int_seven == 7 ); |
| 50 |
REQUIRE( data.float_nine_point_one == Approx( 9.1f ) ); |
| 51 |
REQUIRE( data.double_pi == Approx( 3.1415926535 ) ); |
| 52 |
REQUIRE( data.str_hello == "hello" ); |
| 53 |
REQUIRE( "hello" == data.str_hello ); |
| 54 |
REQUIRE( data.str_hello.size() == 5 ); |
| 55 |
|
| 56 |
double x = 1.1 + 0.1 + 0.1; |
| 57 |
REQUIRE( x == Approx( 1.3 ) ); |
| 58 |
} |
| 59 |
|
| 60 |
TEST_CASE( "Equality checks that should fail", "[.][failing][!mayfail]" ) |
| 61 |
{ |
| 62 |
TestData data; |
| 63 |
|
| 64 |
CHECK( data.int_seven == 6 ); |
| 65 |
CHECK( data.int_seven == 8 ); |
| 66 |
CHECK( data.int_seven == 0 ); |
| 67 |
CHECK( data.float_nine_point_one == Approx( 9.11f ) ); |
| 68 |
CHECK( data.float_nine_point_one == Approx( 9.0f ) ); |
| 69 |
CHECK( data.float_nine_point_one == Approx( 1 ) ); |
| 70 |
CHECK( data.float_nine_point_one == Approx( 0 ) ); |
| 71 |
CHECK( data.double_pi == Approx( 3.1415 ) ); |
| 72 |
CHECK( data.str_hello == "goodbye" ); |
| 73 |
CHECK( data.str_hello == "hell" ); |
| 74 |
CHECK( data.str_hello == "hello1" ); |
| 75 |
CHECK( data.str_hello.size() == 6 ); |
| 76 |
|
| 77 |
double x = 1.1 + 0.1 + 0.1; |
| 78 |
CHECK( x == Approx( 1.301 ) ); |
| 79 |
} |
| 80 |
|
| 81 |
// Needed to test junit reporter's handling of mayfail test cases and sections |
| 82 |
TEST_CASE("Mayfail test case with nested sections", "[!mayfail]") { |
| 83 |
SECTION("A") { |
| 84 |
SECTION("1") { FAIL(); } |
| 85 |
SECTION("2") { FAIL(); } |
| 86 |
} |
| 87 |
SECTION("B") { |
| 88 |
SECTION("1") { FAIL(); } |
| 89 |
SECTION("2") { FAIL(); } |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
TEST_CASE( "Inequality checks that should succeed" ) |
| 95 |
{ |
| 96 |
TestData data; |
| 97 |
|
| 98 |
REQUIRE( data.int_seven != 6 ); |
| 99 |
REQUIRE( data.int_seven != 8 ); |
| 100 |
REQUIRE( data.float_nine_point_one != Approx( 9.11f ) ); |
| 101 |
REQUIRE( data.float_nine_point_one != Approx( 9.0f ) ); |
| 102 |
REQUIRE( data.float_nine_point_one != Approx( 1 ) ); |
| 103 |
REQUIRE( data.float_nine_point_one != Approx( 0 ) ); |
| 104 |
REQUIRE( data.double_pi != Approx( 3.1415 ) ); |
| 105 |
REQUIRE( data.str_hello != "goodbye" ); |
| 106 |
REQUIRE( data.str_hello != "hell" ); |
| 107 |
REQUIRE( data.str_hello != "hello1" ); |
| 108 |
REQUIRE( data.str_hello.size() != 6 ); |
| 109 |
} |
| 110 |
|
| 111 |
TEST_CASE( "Inequality checks that should fail", "[.][failing][!shouldfail]" ) |
| 112 |
{ |
| 113 |
TestData data; |
| 114 |
|
| 115 |
CHECK( data.int_seven != 7 ); |
| 116 |
CHECK( data.float_nine_point_one != Approx( 9.1f ) ); |
| 117 |
CHECK( data.double_pi != Approx( 3.1415926535 ) ); |
| 118 |
CHECK( data.str_hello != "hello" ); |
| 119 |
CHECK( data.str_hello.size() != 5 ); |
| 120 |
} |
| 121 |
|
| 122 |
// Ordering comparison tests |
| 123 |
TEST_CASE( "Ordering comparison checks that should succeed" ) |
| 124 |
{ |
| 125 |
TestData data; |
| 126 |
|
| 127 |
REQUIRE( data.int_seven < 8 ); |
| 128 |
REQUIRE( data.int_seven > 6 ); |
| 129 |
REQUIRE( data.int_seven > 0 ); |
| 130 |
REQUIRE( data.int_seven > -1 ); |
| 131 |
|
| 132 |
REQUIRE( data.int_seven >= 7 ); |
| 133 |
REQUIRE( data.int_seven >= 6 ); |
| 134 |
REQUIRE( data.int_seven <= 7 ); |
| 135 |
REQUIRE( data.int_seven <= 8 ); |
| 136 |
|
| 137 |
REQUIRE( data.float_nine_point_one > 9 ); |
| 138 |
REQUIRE( data.float_nine_point_one < 10 ); |
| 139 |
REQUIRE( data.float_nine_point_one < 9.2 ); |
| 140 |
|
| 141 |
REQUIRE( data.str_hello <= "hello" ); |
| 142 |
REQUIRE( data.str_hello >= "hello" ); |
| 143 |
|
| 144 |
REQUIRE( data.str_hello < "hellp" ); |
| 145 |
REQUIRE( data.str_hello < "zebra" ); |
| 146 |
REQUIRE( data.str_hello > "hellm" ); |
| 147 |
REQUIRE( data.str_hello > "a" ); |
| 148 |
} |
| 149 |
|
| 150 |
TEST_CASE( "Ordering comparison checks that should fail", "[.][failing]" ) |
| 151 |
{ |
| 152 |
TestData data; |
| 153 |
|
| 154 |
CHECK( data.int_seven > 7 ); |
| 155 |
CHECK( data.int_seven < 7 ); |
| 156 |
CHECK( data.int_seven > 8 ); |
| 157 |
CHECK( data.int_seven < 6 ); |
| 158 |
CHECK( data.int_seven < 0 ); |
| 159 |
CHECK( data.int_seven < -1 ); |
| 160 |
|
| 161 |
CHECK( data.int_seven >= 8 ); |
| 162 |
CHECK( data.int_seven <= 6 ); |
| 163 |
|
| 164 |
CHECK( data.float_nine_point_one < 9 ); |
| 165 |
CHECK( data.float_nine_point_one > 10 ); |
| 166 |
CHECK( data.float_nine_point_one > 9.2 ); |
| 167 |
|
| 168 |
CHECK( data.str_hello > "hello" ); |
| 169 |
CHECK( data.str_hello < "hello" ); |
| 170 |
CHECK( data.str_hello > "hellp" ); |
| 171 |
CHECK( data.str_hello > "z" ); |
| 172 |
CHECK( data.str_hello < "hellm" ); |
| 173 |
CHECK( data.str_hello < "a" ); |
| 174 |
|
| 175 |
CHECK( data.str_hello >= "z" ); |
| 176 |
CHECK( data.str_hello <= "a" ); |
| 177 |
} |
| 178 |
|
| 179 |
#ifdef __clang__ |
| 180 |
# pragma clang diagnostic pop |
| 181 |
#endif |
| 182 |
|
| 183 |
|
| 184 |
// Comparisons with int literals |
| 185 |
TEST_CASE( "Comparisons with int literals don't warn when mixing signed/ unsigned" ) |
| 186 |
{ |
| 187 |
int i = 1; |
| 188 |
unsigned int ui = 2; |
| 189 |
long l = 3; |
| 190 |
unsigned long ul = 4; |
| 191 |
char c = 5; |
| 192 |
unsigned char uc = 6; |
| 193 |
|
| 194 |
REQUIRE( i == 1 ); |
| 195 |
REQUIRE( ui == 2 ); |
| 196 |
REQUIRE( l == 3 ); |
| 197 |
REQUIRE( ul == 4 ); |
| 198 |
REQUIRE( c == 5 ); |
| 199 |
REQUIRE( uc == 6 ); |
| 200 |
|
| 201 |
REQUIRE( 1 == i ); |
| 202 |
REQUIRE( 2 == ui ); |
| 203 |
REQUIRE( 3 == l ); |
| 204 |
REQUIRE( 4 == ul ); |
| 205 |
REQUIRE( 5 == c ); |
| 206 |
REQUIRE( 6 == uc ); |
| 207 |
|
| 208 |
REQUIRE( (std::numeric_limits<uint32_t>::max)() > ul ); |
| 209 |
} |
| 210 |
|
| 211 |
// Disable warnings about sign conversions for the next two tests |
| 212 |
// (as we are deliberately invoking them) |
| 213 |
// - Currently only disabled for GCC/ LLVM. Should add VC++ too |
| 214 |
#ifdef __GNUC__ |
| 215 |
#pragma GCC diagnostic push |
| 216 |
#pragma GCC diagnostic ignored "-Wsign-compare" |
| 217 |
#pragma GCC diagnostic ignored "-Wsign-conversion" |
| 218 |
#endif |
| 219 |
#ifdef _MSC_VER |
| 220 |
#pragma warning(disable:4389) // '==' : signed/unsigned mismatch |
| 221 |
#endif |
| 222 |
|
| 223 |
TEST_CASE( "comparisons between int variables" ) |
| 224 |
{ |
| 225 |
long long_var = 1L; |
| 226 |
unsigned char unsigned_char_var = 1; |
| 227 |
unsigned short unsigned_short_var = 1; |
| 228 |
unsigned int unsigned_int_var = 1; |
| 229 |
unsigned long unsigned_long_var = 1L; |
| 230 |
|
| 231 |
REQUIRE( long_var == unsigned_char_var ); |
| 232 |
REQUIRE( long_var == unsigned_short_var ); |
| 233 |
REQUIRE( long_var == unsigned_int_var ); |
| 234 |
REQUIRE( long_var == unsigned_long_var ); |
| 235 |
} |
| 236 |
|
| 237 |
TEST_CASE( "comparisons between const int variables" ) |
| 238 |
{ |
| 239 |
const unsigned char unsigned_char_var = 1; |
| 240 |
const unsigned short unsigned_short_var = 1; |
| 241 |
const unsigned int unsigned_int_var = 1; |
| 242 |
const unsigned long unsigned_long_var = 1L; |
| 243 |
|
| 244 |
REQUIRE( unsigned_char_var == 1 ); |
| 245 |
REQUIRE( unsigned_short_var == 1 ); |
| 246 |
REQUIRE( unsigned_int_var == 1 ); |
| 247 |
REQUIRE( unsigned_long_var == 1 ); |
| 248 |
} |
| 249 |
|
| 250 |
TEST_CASE( "Comparisons between unsigned ints and negative signed ints match c++ standard behaviour" ) |
| 251 |
{ |
| 252 |
CHECK( ( -1 > 2u ) ); |
| 253 |
CHECK( -1 > 2u ); |
| 254 |
|
| 255 |
CHECK( ( 2u < -1 ) ); |
| 256 |
CHECK( 2u < -1 ); |
| 257 |
|
| 258 |
const int minInt = (std::numeric_limits<int>::min)(); |
| 259 |
CHECK( ( minInt > 2u ) ); |
| 260 |
CHECK( minInt > 2u ); |
| 261 |
} |
| 262 |
|
| 263 |
TEST_CASE( "Comparisons between ints where one side is computed" ) |
| 264 |
{ |
| 265 |
CHECK( 54 == 6*9 ); |
| 266 |
} |
| 267 |
|
| 268 |
#ifdef __GNUC__ |
| 269 |
#pragma GCC diagnostic pop |
| 270 |
#endif |
| 271 |
|
| 272 |
TEST_CASE( "Pointers can be compared to null" ) |
| 273 |
{ |
| 274 |
TestData* p = nullptr; |
| 275 |
TestData* pNULL = nullptr; |
| 276 |
|
| 277 |
REQUIRE( p == nullptr ); |
| 278 |
REQUIRE( p == pNULL ); |
| 279 |
|
| 280 |
TestData data; |
| 281 |
p = &data; |
| 282 |
|
| 283 |
REQUIRE( p != nullptr ); |
| 284 |
|
| 285 |
const TestData* cp = p; |
| 286 |
REQUIRE( cp != nullptr ); |
| 287 |
|
| 288 |
const TestData* const cpc = p; |
| 289 |
REQUIRE( cpc != nullptr ); |
| 290 |
|
| 291 |
REQUIRE( returnsNull() == nullptr ); |
| 292 |
REQUIRE( returnsConstNull() == nullptr ); |
| 293 |
|
| 294 |
REQUIRE( nullptr != 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 |
} |