| 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 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/catch_template_test_macros.hpp> |
| 11 | #include <catch2/matchers/catch_matchers_exception.hpp> |
| 12 | #include <catch2/matchers/catch_matchers_floating_point.hpp> |
| 13 | #include <catch2/matchers/catch_matchers_predicate.hpp> |
| 14 | #include <catch2/matchers/catch_matchers_string.hpp> |
| 15 | #include <catch2/matchers/catch_matchers_vector.hpp> |
| 16 | #include <catch2/matchers/catch_matchers_templated.hpp> |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <exception> |
| 20 | #include <cmath> |
| 21 | #include <list> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #ifdef __clang__ |
| 25 | # pragma clang diagnostic push |
| 26 | # pragma clang diagnostic ignored "-Wweak-vtables" |
| 27 | # pragma clang diagnostic ignored "-Wpadded" |
| 28 | #endif |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | static const char* testStringForMatching() { |
| 33 | return "this string contains 'abc' as a substring"; |
| 34 | } |
| 35 | |
| 36 | static const char* testStringForMatching2() { |
| 37 | return "some completely different text that contains one common word"; |
| 38 | } |
| 39 | |
| 40 | static bool alwaysTrue( int ) { return true; } |
| 41 | static bool alwaysFalse( int ) { return false; } |
| 42 | |
| 43 | #ifdef _MSC_VER |
| 44 | # pragma warning( disable : 4702 ) // Unreachable code -- MSVC 19 (VS 2015) |
| 45 | // sees right through the indirection |
| 46 | #endif |
| 47 | |
| 48 | struct SpecialException : std::exception { |
| 49 | SpecialException( int i_ ): i( i_ ) {} |
| 50 | |
| 51 | char const* what() const noexcept override { |
| 52 | return "SpecialException::what"; |
| 53 | } |
| 54 | |
| 55 | int i; |
| 56 | }; |
| 57 | |
| 58 | struct DerivedException : std::exception { |
| 59 | char const* what() const noexcept override { |
| 60 | return "DerivedException::what"; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | static void doesNotThrow() {} |
| 65 | |
| 66 | [[noreturn]] static void throwsSpecialException( int i ) { |
| 67 | throw SpecialException{ i }; |
| 68 | } |
| 69 | |
| 70 | [[noreturn]] static void throwsAsInt( int i ) { throw i; } |
| 71 | |
| 72 | [[noreturn]] static void throwsDerivedException() { |
| 73 | throw DerivedException{}; |
| 74 | } |
| 75 | |
| 76 | class ExceptionMatcher |
| 77 | : public Catch::Matchers::MatcherBase<SpecialException> { |
| 78 | int m_expected; |
| 79 | |
| 80 | public: |
| 81 | ExceptionMatcher( int i ): m_expected( i ) {} |
| 82 | |
| 83 | bool match( SpecialException const& se ) const override { |
| 84 | return se.i == m_expected; |
| 85 | } |
| 86 | |
| 87 | std::string describe() const override { |
| 88 | std::ostringstream ss; |
| 89 | ss << "special exception has value of " << m_expected; |
| 90 | return ss.str(); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | using namespace Catch::Matchers; |
| 95 | |
| 96 | #ifdef __DJGPP__ |
| 97 | static float nextafter( float from, float to ) { |
| 98 | return ::nextafterf( from, to ); |
| 99 | } |
| 100 | |
| 101 | static double nextafter( double from, double to ) { |
| 102 | return ::nextafter( from, to ); |
| 103 | } |
| 104 | #else |
| 105 | using std::nextafter; |
| 106 | #endif |
| 107 | |
| 108 | } // end unnamed namespace |
| 109 | |
| 110 | TEST_CASE( "String matchers", "[matchers]" ) { |
| 111 | REQUIRE_THAT( testStringForMatching(), ContainsSubstring( "string" ) ); |
| 112 | REQUIRE_THAT( testStringForMatching(), |
| 113 | ContainsSubstring( "string", Catch::CaseSensitive::No ) ); |
| 114 | CHECK_THAT( testStringForMatching(), ContainsSubstring( "abc" ) ); |
| 115 | CHECK_THAT( testStringForMatching(), |
| 116 | ContainsSubstring( "aBC", Catch::CaseSensitive::No ) ); |
| 117 | |
| 118 | CHECK_THAT( testStringForMatching(), StartsWith( "this" ) ); |
| 119 | CHECK_THAT( testStringForMatching(), |
| 120 | StartsWith( "THIS", Catch::CaseSensitive::No ) ); |
| 121 | CHECK_THAT( testStringForMatching(), EndsWith( "substring" ) ); |
| 122 | CHECK_THAT( testStringForMatching(), |
| 123 | EndsWith( " SuBsTrInG", Catch::CaseSensitive::No ) ); |
| 124 | } |
| 125 | |
| 126 | TEST_CASE( "Contains string matcher", "[.][failing][matchers]" ) { |
| 127 | CHECK_THAT( testStringForMatching(), |
| 128 | ContainsSubstring( "not there", Catch::CaseSensitive::No ) ); |
| 129 | CHECK_THAT( testStringForMatching(), ContainsSubstring( "STRING" ) ); |
| 130 | } |
| 131 | |
| 132 | TEST_CASE( "StartsWith string matcher", "[.][failing][matchers]" ) { |
| 133 | CHECK_THAT( testStringForMatching(), StartsWith( "This String" ) ); |
| 134 | CHECK_THAT( testStringForMatching(), |
| 135 | StartsWith( "string", Catch::CaseSensitive::No ) ); |
| 136 | } |
| 137 | |
| 138 | TEST_CASE( "EndsWith string matcher", "[.][failing][matchers]" ) { |
| 139 | CHECK_THAT( testStringForMatching(), EndsWith( "Substring" ) ); |
| 140 | CHECK_THAT( testStringForMatching(), |
| 141 | EndsWith( "this", Catch::CaseSensitive::No ) ); |
| 142 | } |
| 143 | |
| 144 | TEST_CASE( "Equals string matcher", "[.][failing][matchers]" ) { |
| 145 | CHECK_THAT( testStringForMatching(), |
| 146 | Equals( "this string contains 'ABC' as a substring" ) ); |
| 147 | CHECK_THAT( testStringForMatching(), |
| 148 | Equals( "something else", Catch::CaseSensitive::No ) ); |
| 149 | } |
| 150 | |
| 151 | TEST_CASE( "Equals", "[matchers]" ) { |
| 152 | CHECK_THAT( testStringForMatching(), |
| 153 | Equals( "this string contains 'abc' as a substring" ) ); |
| 154 | CHECK_THAT( testStringForMatching(), |
| 155 | Equals( "this string contains 'ABC' as a substring", |
| 156 | Catch::CaseSensitive::No ) ); |
| 157 | } |
| 158 | |
| 159 | TEST_CASE( "Regex string matcher -- libstdc++-4.8 workaround", |
| 160 | "[matchers][approvals]" ) { |
| 161 | // DJGPP has similar problem with its regex support as libstdc++ 4.8 |
| 162 | #ifndef __DJGPP__ |
| 163 | REQUIRE_THAT( testStringForMatching(), |
| 164 | Matches( "this string contains 'abc' as a substring" ) ); |
| 165 | REQUIRE_THAT( testStringForMatching(), |
| 166 | Matches( "this string CONTAINS 'abc' as a substring", |
| 167 | Catch::CaseSensitive::No ) ); |
| 168 | REQUIRE_THAT( testStringForMatching(), |
| 169 | Matches( "^this string contains 'abc' as a substring$" ) ); |
| 170 | REQUIRE_THAT( testStringForMatching(), Matches( "^.* 'abc' .*$" ) ); |
| 171 | REQUIRE_THAT( testStringForMatching(), |
| 172 | Matches( "^.* 'ABC' .*$", Catch::CaseSensitive::No ) ); |
| 173 | #endif |
| 174 | |
| 175 | REQUIRE_THAT( testStringForMatching2(), |
| 176 | !Matches( "this string contains 'abc' as a substring" ) ); |
| 177 | } |
| 178 | |
| 179 | TEST_CASE( "Regex string matcher", "[matchers][.failing]" ) { |
| 180 | CHECK_THAT( testStringForMatching(), |
| 181 | Matches( "this STRING contains 'abc' as a substring" ) ); |
| 182 | CHECK_THAT( testStringForMatching(), |
| 183 | Matches( "contains 'abc' as a substring" ) ); |
| 184 | CHECK_THAT( testStringForMatching(), |
| 185 | Matches( "this string contains 'abc' as a" ) ); |
| 186 | } |
| 187 | |
| 188 | TEST_CASE( "Matchers can be (AllOf) composed with the && operator", |
| 189 | "[matchers][operators][operator&&]" ) { |
| 190 | CHECK_THAT( testStringForMatching(), |
| 191 | ContainsSubstring( "string" ) && ContainsSubstring( "abc" ) && |
| 192 | ContainsSubstring( "substring" ) && ContainsSubstring( "contains" ) ); |
| 193 | } |
| 194 | |
| 195 | TEST_CASE( "Matchers can be (AnyOf) composed with the || operator", |
| 196 | "[matchers][operators][operator||]" ) { |
| 197 | CHECK_THAT( testStringForMatching(), |
| 198 | ContainsSubstring( "string" ) || ContainsSubstring( "different" ) || |
| 199 | ContainsSubstring( "random" ) ); |
| 200 | CHECK_THAT( testStringForMatching2(), |
| 201 | ContainsSubstring( "string" ) || ContainsSubstring( "different" ) || |
| 202 | ContainsSubstring( "random" ) ); |
| 203 | } |
| 204 | |
| 205 | TEST_CASE( "Matchers can be composed with both && and ||", |
| 206 | "[matchers][operators][operator||][operator&&]" ) { |
| 207 | CHECK_THAT( testStringForMatching(), |
| 208 | ( ContainsSubstring( "string" ) || ContainsSubstring( "different" ) ) && |
| 209 | ContainsSubstring( "substring" ) ); |
| 210 | } |
| 211 | |
| 212 | TEST_CASE( "Matchers can be composed with both && and || - failing", |
| 213 | "[matchers][operators][operator||][operator&&][.failing]" ) { |
| 214 | CHECK_THAT( testStringForMatching(), |
| 215 | ( ContainsSubstring( "string" ) || ContainsSubstring( "different" ) ) && |
| 216 | ContainsSubstring( "random" ) ); |
| 217 | } |
| 218 | |
| 219 | TEST_CASE( "Matchers can be negated (Not) with the ! operator", |
| 220 | "[matchers][operators][not]" ) { |
| 221 | CHECK_THAT( testStringForMatching(), !ContainsSubstring( "different" ) ); |
| 222 | } |
| 223 | |
| 224 | TEST_CASE( "Matchers can be negated (Not) with the ! operator - failing", |
| 225 | "[matchers][operators][not][.failing]" ) { |
| 226 | CHECK_THAT( testStringForMatching(), !ContainsSubstring( "substring" ) ); |
| 227 | } |
| 228 | |
| 229 | template <typename T> struct CustomAllocator : private std::allocator<T> { |
| 230 | using size_type = size_t; |
| 231 | using difference_type = ptrdiff_t; |
| 232 | using pointer = T*; |
| 233 | using const_pointer = const T*; |
| 234 | using reference = T&; |
| 235 | using const_reference = const T&; |
| 236 | using value_type = T; |
| 237 | |
| 238 | template <typename U> struct rebind { using other = CustomAllocator<U>; }; |
| 239 | |
| 240 | using propagate_on_container_move_assignment = std::true_type; |
| 241 | using is_always_equal = std::true_type; |
| 242 | |
| 243 | CustomAllocator() = default; |
| 244 | |
| 245 | CustomAllocator( const CustomAllocator& other ): |
| 246 | std::allocator<T>( other ) {} |
| 247 | |
| 248 | template <typename U> CustomAllocator( const CustomAllocator<U>& ) {} |
| 249 | |
| 250 | ~CustomAllocator() = default; |
| 251 | |
| 252 | using std::allocator<T>::allocate; |
| 253 | using std::allocator<T>::deallocate; |
| 254 | }; |
| 255 | |
| 256 | TEST_CASE( "Vector matchers", "[matchers][vector]" ) { |
| 257 | std::vector<int> v; |
| 258 | v.push_back( 1 ); |
| 259 | v.push_back( 2 ); |
| 260 | v.push_back( 3 ); |
| 261 | |
| 262 | std::vector<int> v2; |
| 263 | v2.push_back( 1 ); |
| 264 | v2.push_back( 2 ); |
| 265 | |
| 266 | std::vector<double> v3; |
| 267 | v3.push_back( 1 ); |
| 268 | v3.push_back( 2 ); |
| 269 | v3.push_back( 3 ); |
| 270 | |
| 271 | std::vector<double> v4; |
| 272 | v4.push_back( 1 + 1e-8 ); |
| 273 | v4.push_back( 2 + 1e-8 ); |
| 274 | v4.push_back( 3 + 1e-8 ); |
| 275 | |
| 276 | std::vector<int, CustomAllocator<int>> v5; |
| 277 | v5.push_back( 1 ); |
| 278 | v5.push_back( 2 ); |
| 279 | v5.push_back( 3 ); |
| 280 | |
| 281 | std::vector<int, CustomAllocator<int>> v6; |
| 282 | v6.push_back( 1 ); |
| 283 | v6.push_back( 2 ); |
| 284 | |
| 285 | std::vector<int> empty; |
| 286 | |
| 287 | SECTION( "Contains (element)" ) { |
| 288 | CHECK_THAT( v, VectorContains( 1 ) ); |
| 289 | CHECK_THAT( v, VectorContains( 2 ) ); |
| 290 | CHECK_THAT( v5, ( VectorContains<int, CustomAllocator<int>>( 2 ) ) ); |
| 291 | } |
| 292 | SECTION( "Contains (vector)" ) { |
| 293 | CHECK_THAT( v, Contains( v2 ) ); |
| 294 | CHECK_THAT( v, Contains<int>( { 1, 2 } ) ); |
| 295 | CHECK_THAT( v5, |
| 296 | ( Contains<int, std::allocator<int>, CustomAllocator<int>>( |
| 297 | v2 ) ) ); |
| 298 | |
| 299 | v2.push_back( 3 ); // now exactly matches |
| 300 | CHECK_THAT( v, Contains( v2 ) ); |
| 301 | |
| 302 | CHECK_THAT( v, Contains( empty ) ); |
| 303 | CHECK_THAT( empty, Contains( empty ) ); |
| 304 | |
| 305 | CHECK_THAT( v5, |
| 306 | ( Contains<int, std::allocator<int>, CustomAllocator<int>>( |
| 307 | v2 ) ) ); |
| 308 | CHECK_THAT( v5, Contains( v6 ) ); |
| 309 | } |
| 310 | SECTION( "Contains (element), composed" ) { |
| 311 | CHECK_THAT( v, VectorContains( 1 ) && VectorContains( 2 ) ); |
| 312 | } |
| 313 | |
| 314 | SECTION( "Equals" ) { |
| 315 | |
| 316 | // Same vector |
| 317 | CHECK_THAT( v, Equals( v ) ); |
| 318 | |
| 319 | CHECK_THAT( empty, Equals( empty ) ); |
| 320 | |
| 321 | // Different vector with same elements |
| 322 | CHECK_THAT( v, Equals<int>( { 1, 2, 3 } ) ); |
| 323 | v2.push_back( 3 ); |
| 324 | CHECK_THAT( v, Equals( v2 ) ); |
| 325 | |
| 326 | CHECK_THAT( |
| 327 | v5, |
| 328 | ( Equals<int, std::allocator<int>, CustomAllocator<int>>( v2 ) ) ); |
| 329 | |
| 330 | v6.push_back( 3 ); |
| 331 | CHECK_THAT( v5, Equals( v6 ) ); |
| 332 | } |
| 333 | SECTION( "UnorderedEquals" ) { |
| 334 | CHECK_THAT( v, UnorderedEquals( v ) ); |
| 335 | CHECK_THAT( v, UnorderedEquals<int>( { 3, 2, 1 } ) ); |
| 336 | CHECK_THAT( empty, UnorderedEquals( empty ) ); |
| 337 | |
| 338 | auto permuted = v; |
| 339 | std::next_permutation( begin( permuted ), end( permuted ) ); |
| 340 | REQUIRE_THAT( permuted, UnorderedEquals( v ) ); |
| 341 | |
| 342 | std::reverse( begin( permuted ), end( permuted ) ); |
| 343 | REQUIRE_THAT( permuted, UnorderedEquals( v ) ); |
| 344 | |
| 345 | CHECK_THAT( |
| 346 | v5, |
| 347 | ( UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>( |
| 348 | permuted ) ) ); |
| 349 | |
| 350 | auto v5_permuted = v5; |
| 351 | std::next_permutation( begin( v5_permuted ), end( v5_permuted ) ); |
| 352 | CHECK_THAT( v5_permuted, UnorderedEquals( v5 ) ); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | TEST_CASE( "Vector matchers that fail", "[matchers][vector][.][failing]" ) { |
| 357 | std::vector<int> v; |
| 358 | v.push_back( 1 ); |
| 359 | v.push_back( 2 ); |
| 360 | v.push_back( 3 ); |
| 361 | |
| 362 | std::vector<int> v2; |
| 363 | v2.push_back( 1 ); |
| 364 | v2.push_back( 2 ); |
| 365 | |
| 366 | std::vector<double> v3; |
| 367 | v3.push_back( 1 ); |
| 368 | v3.push_back( 2 ); |
| 369 | v3.push_back( 3 ); |
| 370 | |
| 371 | std::vector<double> v4; |
| 372 | v4.push_back( 1.1 ); |
| 373 | v4.push_back( 2.1 ); |
| 374 | v4.push_back( 3.1 ); |
| 375 | |
| 376 | std::vector<int> empty; |
| 377 | |
| 378 | SECTION( "Contains (element)" ) { |
| 379 | CHECK_THAT( v, VectorContains( -1 ) ); |
| 380 | CHECK_THAT( empty, VectorContains( 1 ) ); |
| 381 | } |
| 382 | SECTION( "Contains (vector)" ) { |
| 383 | CHECK_THAT( empty, Contains( v ) ); |
| 384 | v2.push_back( 4 ); |
| 385 | CHECK_THAT( v, Contains( v2 ) ); |
| 386 | } |
| 387 | |
| 388 | SECTION( "Equals" ) { |
| 389 | |
| 390 | CHECK_THAT( v, Equals( v2 ) ); |
| 391 | CHECK_THAT( v2, Equals( v ) ); |
| 392 | CHECK_THAT( empty, Equals( v ) ); |
| 393 | CHECK_THAT( v, Equals( empty ) ); |
| 394 | } |
| 395 | SECTION( "UnorderedEquals" ) { |
| 396 | CHECK_THAT( v, UnorderedEquals( empty ) ); |
| 397 | CHECK_THAT( empty, UnorderedEquals( v ) ); |
| 398 | |
| 399 | auto permuted = v; |
| 400 | std::next_permutation( begin( permuted ), end( permuted ) ); |
| 401 | permuted.pop_back(); |
| 402 | CHECK_THAT( permuted, UnorderedEquals( v ) ); |
| 403 | |
| 404 | std::reverse( begin( permuted ), end( permuted ) ); |
| 405 | CHECK_THAT( permuted, UnorderedEquals( v ) ); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | namespace { |
| 410 | struct SomeType { |
| 411 | int i; |
| 412 | friend bool operator==( SomeType lhs, SomeType rhs ) { |
| 413 | return lhs.i == rhs.i; |
| 414 | } |
| 415 | }; |
| 416 | } // end anonymous namespace |
| 417 | |
| 418 | TEST_CASE( "Vector matcher with elements without !=", "[matchers][vector][approvals]" ) { |
| 419 | std::vector<SomeType> lhs, rhs; |
| 420 | lhs.push_back( { 1 } ); |
| 421 | lhs.push_back( { 2 } ); |
| 422 | rhs.push_back( { 1 } ); |
| 423 | rhs.push_back( { 1 } ); |
| 424 | |
| 425 | REQUIRE_THAT( lhs, !Equals(rhs) ); |
| 426 | } |
| 427 | |
| 428 | TEST_CASE( "Exception matchers that succeed", |
| 429 | "[matchers][exceptions][!throws]" ) { |
| 430 | CHECK_THROWS_MATCHES( |
| 431 | throwsSpecialException( 1 ), SpecialException, ExceptionMatcher{ 1 } ); |
| 432 | REQUIRE_THROWS_MATCHES( |
| 433 | throwsSpecialException( 2 ), SpecialException, ExceptionMatcher{ 2 } ); |
| 434 | } |
| 435 | |
| 436 | TEST_CASE( "Exception matchers that fail", |
| 437 | "[matchers][exceptions][!throws][.failing]" ) { |
| 438 | SECTION( "No exception" ) { |
| 439 | CHECK_THROWS_MATCHES( |
| 440 | doesNotThrow(), SpecialException, ExceptionMatcher{ 1 } ); |
| 441 | REQUIRE_THROWS_MATCHES( |
| 442 | doesNotThrow(), SpecialException, ExceptionMatcher{ 1 } ); |
| 443 | } |
| 444 | SECTION( "Type mismatch" ) { |
| 445 | CHECK_THROWS_MATCHES( |
| 446 | throwsAsInt( 1 ), SpecialException, ExceptionMatcher{ 1 } ); |
| 447 | REQUIRE_THROWS_MATCHES( |
| 448 | throwsAsInt( 1 ), SpecialException, ExceptionMatcher{ 1 } ); |
| 449 | } |
| 450 | SECTION( "Contents are wrong" ) { |
| 451 | CHECK_THROWS_MATCHES( throwsSpecialException( 3 ), |
| 452 | SpecialException, |
| 453 | ExceptionMatcher{ 1 } ); |
| 454 | REQUIRE_THROWS_MATCHES( throwsSpecialException( 4 ), |
| 455 | SpecialException, |
| 456 | ExceptionMatcher{ 1 } ); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | TEST_CASE( "Floating point matchers: float", "[matchers][floating-point]" ) { |
| 461 | SECTION( "Relative" ) { |
| 462 | REQUIRE_THAT( 10.f, WithinRel( 11.1f, 0.1f ) ); |
| 463 | REQUIRE_THAT( 10.f, !WithinRel( 11.2f, 0.1f ) ); |
| 464 | REQUIRE_THAT( 1.f, !WithinRel( 0.f, 0.99f ) ); |
| 465 | REQUIRE_THAT( -0.f, WithinRel( 0.f ) ); |
| 466 | SECTION( "Some subnormal values" ) { |
| 467 | auto v1 = std::numeric_limits<float>::min(); |
| 468 | auto v2 = v1; |
| 469 | for ( int i = 0; i < 5; ++i ) { |
| 470 | v2 = std::nextafter( v1, 0.f ); |
| 471 | } |
| 472 | REQUIRE_THAT( v1, WithinRel( v2 ) ); |
| 473 | } |
| 474 | } |
| 475 | SECTION( "Margin" ) { |
| 476 | REQUIRE_THAT( 1.f, WithinAbs( 1.f, 0 ) ); |
| 477 | REQUIRE_THAT( 0.f, WithinAbs( 1.f, 1 ) ); |
| 478 | |
| 479 | REQUIRE_THAT( 0.f, !WithinAbs( 1.f, 0.99f ) ); |
| 480 | REQUIRE_THAT( 0.f, !WithinAbs( 1.f, 0.99f ) ); |
| 481 | |
| 482 | REQUIRE_THAT( 0.f, WithinAbs( -0.f, 0 ) ); |
| 483 | |
| 484 | REQUIRE_THAT( 11.f, !WithinAbs( 10.f, 0.5f ) ); |
| 485 | REQUIRE_THAT( 10.f, !WithinAbs( 11.f, 0.5f ) ); |
| 486 | REQUIRE_THAT( -10.f, WithinAbs( -10.f, 0.5f ) ); |
| 487 | REQUIRE_THAT( -10.f, WithinAbs( -9.6f, 0.5f ) ); |
| 488 | } |
| 489 | SECTION( "ULPs" ) { |
| 490 | REQUIRE_THAT( 1.f, WithinULP( 1.f, 0 ) ); |
| 491 | REQUIRE_THAT(-1.f, WithinULP( -1.f, 0 ) ); |
| 492 | |
| 493 | REQUIRE_THAT( nextafter( 1.f, 2.f ), WithinULP( 1.f, 1 ) ); |
| 494 | REQUIRE_THAT( 0.f, WithinULP( nextafter( 0.f, 1.f ), 1 ) ); |
| 495 | REQUIRE_THAT( 1.f, WithinULP( nextafter( 1.f, 0.f ), 1 ) ); |
| 496 | REQUIRE_THAT( 1.f, !WithinULP( nextafter( 1.f, 2.f ), 0 ) ); |
| 497 | |
| 498 | REQUIRE_THAT( 1.f, WithinULP( 1.f, 0 ) ); |
| 499 | REQUIRE_THAT( -0.f, WithinULP( 0.f, 0 ) ); |
| 500 | } |
| 501 | SECTION( "Composed" ) { |
| 502 | REQUIRE_THAT( 1.f, WithinAbs( 1.f, 0.5 ) || WithinULP( 1.f, 1 ) ); |
| 503 | REQUIRE_THAT( 1.f, WithinAbs( 2.f, 0.5 ) || WithinULP( 1.f, 0 ) ); |
| 504 | REQUIRE_THAT( 0.0001f, |
| 505 | WithinAbs( 0.f, 0.001f ) || WithinRel( 0.f, 0.1f ) ); |
| 506 | } |
| 507 | SECTION( "Constructor validation" ) { |
| 508 | REQUIRE_NOTHROW( WithinAbs( 1.f, 0.f ) ); |
| 509 | REQUIRE_THROWS_AS( WithinAbs( 1.f, -1.f ), std::domain_error ); |
| 510 | |
| 511 | REQUIRE_NOTHROW( WithinULP( 1.f, 0 ) ); |
| 512 | REQUIRE_THROWS_AS( WithinULP( 1.f, static_cast<uint64_t>( -1 ) ), |
| 513 | std::domain_error ); |
| 514 | |
| 515 | REQUIRE_NOTHROW( WithinRel( 1.f, 0.f ) ); |
| 516 | REQUIRE_THROWS_AS( WithinRel( 1.f, -0.2f ), std::domain_error ); |
| 517 | REQUIRE_THROWS_AS( WithinRel( 1.f, 1.f ), std::domain_error ); |
| 518 | } |
| 519 | SECTION( "IsNaN" ) { |
| 520 | REQUIRE_THAT( 1., !IsNaN() ); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | TEST_CASE( "Floating point matchers: double", "[matchers][floating-point]" ) { |
| 525 | SECTION( "Relative" ) { |
| 526 | REQUIRE_THAT( 10., WithinRel( 11.1, 0.1 ) ); |
| 527 | REQUIRE_THAT( 10., !WithinRel( 11.2, 0.1 ) ); |
| 528 | REQUIRE_THAT( 1., !WithinRel( 0., 0.99 ) ); |
| 529 | REQUIRE_THAT( -0., WithinRel( 0. ) ); |
| 530 | SECTION( "Some subnormal values" ) { |
| 531 | auto v1 = std::numeric_limits<double>::min(); |
| 532 | auto v2 = v1; |
| 533 | for ( int i = 0; i < 5; ++i ) { |
| 534 | v2 = std::nextafter( v1, 0 ); |
| 535 | } |
| 536 | REQUIRE_THAT( v1, WithinRel( v2 ) ); |
| 537 | } |
| 538 | } |
| 539 | SECTION( "Margin" ) { |
| 540 | REQUIRE_THAT( 1., WithinAbs( 1., 0 ) ); |
| 541 | REQUIRE_THAT( 0., WithinAbs( 1., 1 ) ); |
| 542 | |
| 543 | REQUIRE_THAT( 0., !WithinAbs( 1., 0.99 ) ); |
| 544 | REQUIRE_THAT( 0., !WithinAbs( 1., 0.99 ) ); |
| 545 | |
| 546 | REQUIRE_THAT( 11., !WithinAbs( 10., 0.5 ) ); |
| 547 | REQUIRE_THAT( 10., !WithinAbs( 11., 0.5 ) ); |
| 548 | REQUIRE_THAT( -10., WithinAbs( -10., 0.5 ) ); |
| 549 | REQUIRE_THAT( -10., WithinAbs( -9.6, 0.5 ) ); |
| 550 | } |
| 551 | SECTION( "ULPs" ) { |
| 552 | REQUIRE_THAT( 1., WithinULP( 1., 0 ) ); |
| 553 | |
| 554 | REQUIRE_THAT( nextafter( 1., 2. ), WithinULP( 1., 1 ) ); |
| 555 | REQUIRE_THAT( 0., WithinULP( nextafter( 0., 1. ), 1 ) ); |
| 556 | REQUIRE_THAT( 1., WithinULP( nextafter( 1., 0. ), 1 ) ); |
| 557 | REQUIRE_THAT( 1., !WithinULP( nextafter( 1., 2. ), 0 ) ); |
| 558 | |
| 559 | REQUIRE_THAT( 1., WithinULP( 1., 0 ) ); |
| 560 | REQUIRE_THAT( -0., WithinULP( 0., 0 ) ); |
| 561 | } |
| 562 | SECTION( "Composed" ) { |
| 563 | REQUIRE_THAT( 1., WithinAbs( 1., 0.5 ) || WithinULP( 2., 1 ) ); |
| 564 | REQUIRE_THAT( 1., WithinAbs( 2., 0.5 ) || WithinULP( 1., 0 ) ); |
| 565 | REQUIRE_THAT( 0.0001, WithinAbs( 0., 0.001 ) || WithinRel( 0., 0.1 ) ); |
| 566 | } |
| 567 | SECTION( "Constructor validation" ) { |
| 568 | REQUIRE_NOTHROW( WithinAbs( 1., 0. ) ); |
| 569 | REQUIRE_THROWS_AS( WithinAbs( 1., -1. ), std::domain_error ); |
| 570 | |
| 571 | REQUIRE_NOTHROW( WithinULP( 1., 0 ) ); |
| 572 | |
| 573 | REQUIRE_NOTHROW( WithinRel( 1., 0. ) ); |
| 574 | REQUIRE_THROWS_AS( WithinRel( 1., -0.2 ), std::domain_error ); |
| 575 | REQUIRE_THROWS_AS( WithinRel( 1., 1. ), std::domain_error ); |
| 576 | } |
| 577 | SECTION("IsNaN") { |
| 578 | REQUIRE_THAT( 1., !IsNaN() ); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | TEST_CASE( "Floating point matchers that are problematic in approvals", |
| 583 | "[approvals][matchers][floating-point]" ) { |
| 584 | REQUIRE_THAT( NAN, !WithinAbs( NAN, 0 ) ); |
| 585 | REQUIRE_THAT( NAN, !( WithinAbs( NAN, 100 ) || WithinULP( NAN, 123 ) ) ); |
| 586 | REQUIRE_THAT( NAN, !WithinULP( NAN, 123 ) ); |
| 587 | REQUIRE_THAT( INFINITY, WithinRel( INFINITY ) ); |
| 588 | REQUIRE_THAT( -INFINITY, !WithinRel( INFINITY ) ); |
| 589 | REQUIRE_THAT( 1., !WithinRel( INFINITY ) ); |
| 590 | REQUIRE_THAT( INFINITY, !WithinRel( 1. ) ); |
| 591 | REQUIRE_THAT( NAN, !WithinRel( NAN ) ); |
| 592 | REQUIRE_THAT( 1., !WithinRel( NAN ) ); |
| 593 | REQUIRE_THAT( NAN, !WithinRel( 1. ) ); |
| 594 | REQUIRE_THAT( NAN, IsNaN() ); |
| 595 | REQUIRE_THAT( static_cast<double>(NAN), IsNaN() ); |
| 596 | } |
| 597 | |
| 598 | TEST_CASE( "Arbitrary predicate matcher", "[matchers][generic]" ) { |
| 599 | SECTION( "Function pointer" ) { |
| 600 | REQUIRE_THAT( 1, Predicate<int>( alwaysTrue, "always true" ) ); |
| 601 | REQUIRE_THAT( 1, !Predicate<int>( alwaysFalse, "always false" ) ); |
| 602 | } |
| 603 | SECTION( "Lambdas + different type" ) { |
| 604 | REQUIRE_THAT( "Hello olleH", |
| 605 | Predicate<std::string>( |
| 606 | []( std::string const& str ) -> bool { |
| 607 | return str.front() == str.back(); |
| 608 | }, |
| 609 | "First and last character should be equal" ) ); |
| 610 | |
| 611 | REQUIRE_THAT( |
| 612 | "This wouldn't pass", |
| 613 | !Predicate<std::string>( []( std::string const& str ) -> bool { |
| 614 | return str.front() == str.back(); |
| 615 | } ) ); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | TEST_CASE( "Regression test #1", "[matchers][vector]" ) { |
| 620 | // At some point, UnorderedEqualsMatcher skipped |
| 621 | // mismatched prefixed before doing the comparison itself |
| 622 | std::vector<char> actual = { 'a', 'b' }; |
| 623 | std::vector<char> expected = { 'c', 'b' }; |
| 624 | |
| 625 | CHECK_THAT( actual, !UnorderedEquals( expected ) ); |
| 626 | } |
| 627 | |
| 628 | TEST_CASE( "Predicate matcher can accept const char*", |
| 629 | "[matchers][compilation]" ) { |
| 630 | REQUIRE_THAT( "foo", Predicate<const char*>( []( const char* const& ) { |
| 631 | return true; |
| 632 | } ) ); |
| 633 | } |
| 634 | |
| 635 | TEST_CASE( "Vector Approx matcher", "[matchers][approx][vector]" ) { |
| 636 | using Catch::Matchers::Approx; |
| 637 | SECTION( "Empty vector is roughly equal to an empty vector" ) { |
| 638 | std::vector<double> empty; |
| 639 | REQUIRE_THAT( empty, Approx( empty ) ); |
| 640 | } |
| 641 | SECTION( "Vectors with elements" ) { |
| 642 | std::vector<double> v1( { 1., 2., 3. } ); |
| 643 | SECTION( "A vector is approx equal to itself" ) { |
| 644 | REQUIRE_THAT( v1, Approx( v1 ) ); |
| 645 | REQUIRE_THAT( v1, Approx<double>( { 1., 2., 3. } ) ); |
| 646 | } |
| 647 | std::vector<double> v2( { 1.5, 2.5, 3.5 } ); |
| 648 | SECTION( "Different length" ) { |
| 649 | auto temp( v1 ); |
| 650 | temp.push_back( 4 ); |
| 651 | REQUIRE_THAT( v1, !Approx( temp ) ); |
| 652 | } |
| 653 | SECTION( "Same length, different elements" ) { |
| 654 | REQUIRE_THAT( v1, !Approx( v2 ) ); |
| 655 | REQUIRE_THAT( v1, Approx( v2 ).margin( 0.5 ) ); |
| 656 | REQUIRE_THAT( v1, Approx( v2 ).epsilon( 0.5 ) ); |
| 657 | REQUIRE_THAT( v1, Approx( v2 ).epsilon( 0.1 ).scale( 500 ) ); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | TEST_CASE( "Vector Approx matcher -- failing", |
| 663 | "[matchers][approx][vector][.failing]" ) { |
| 664 | using Catch::Matchers::Approx; |
| 665 | SECTION( "Empty and non empty vectors are not approx equal" ) { |
| 666 | std::vector<double> empty, t1( { 1, 2 } ); |
| 667 | CHECK_THAT( empty, Approx( t1 ) ); |
| 668 | } |
| 669 | SECTION( "Just different vectors" ) { |
| 670 | std::vector<double> v1( { 2., 4., 6. } ), v2( { 1., 3., 5. } ); |
| 671 | CHECK_THAT( v1, Approx( v2 ) ); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | TEST_CASE( "Exceptions matchers", "[matchers][exceptions][!throws]" ) { |
| 676 | REQUIRE_THROWS_MATCHES( throwsDerivedException(), |
| 677 | DerivedException, |
| 678 | Message( "DerivedException::what" ) ); |
| 679 | REQUIRE_THROWS_MATCHES( throwsDerivedException(), |
| 680 | DerivedException, |
| 681 | !Message( "derivedexception::what" ) ); |
| 682 | REQUIRE_THROWS_MATCHES( throwsSpecialException( 2 ), |
| 683 | SpecialException, |
| 684 | !Message( "DerivedException::what" ) ); |
| 685 | REQUIRE_THROWS_MATCHES( throwsSpecialException( 2 ), |
| 686 | SpecialException, |
| 687 | Message( "SpecialException::what" ) ); |
| 688 | } |
| 689 | |
| 690 | TEST_CASE( "Exception message can be matched", "[matchers][exceptions][!throws]" ) { |
| 691 | REQUIRE_THROWS_MATCHES( throwsDerivedException(), |
| 692 | DerivedException, |
| 693 | MessageMatches( StartsWith( "Derived" ) ) ); |
| 694 | REQUIRE_THROWS_MATCHES( throwsDerivedException(), |
| 695 | DerivedException, |
| 696 | MessageMatches( EndsWith( "::what" ) ) ); |
| 697 | REQUIRE_THROWS_MATCHES( throwsDerivedException(), |
| 698 | DerivedException, |
| 699 | MessageMatches( !StartsWith( "::what" ) ) ); |
| 700 | REQUIRE_THROWS_MATCHES( throwsSpecialException( 2 ), |
| 701 | SpecialException, |
| 702 | MessageMatches( StartsWith( "Special" ) ) ); |
| 703 | } |
| 704 | |
| 705 | struct CheckedTestingMatcher : Catch::Matchers::MatcherBase<int> { |
| 706 | mutable bool matchCalled = false; |
| 707 | bool matchSucceeds = false; |
| 708 | |
| 709 | bool match( int const& ) const override { |
| 710 | matchCalled = true; |
| 711 | return matchSucceeds; |
| 712 | } |
| 713 | std::string describe() const override { |
| 714 | return "CheckedTestingMatcher set to " + |
| 715 | ( matchSucceeds ? std::string( "succeed" ) |
| 716 | : std::string( "fail" ) ); |
| 717 | } |
| 718 | }; |
| 719 | |
| 720 | TEST_CASE( "Composed matchers shortcircuit", "[matchers][composed]" ) { |
| 721 | // Check that if first returns false, second is not touched |
| 722 | CheckedTestingMatcher first, second; |
| 723 | SECTION( "MatchAllOf" ) { |
| 724 | first.matchSucceeds = false; |
| 725 | |
| 726 | Detail::MatchAllOf<int> matcher = |
| 727 | Detail::MatchAllOf<int>{} && first && second; |
| 728 | CHECK_FALSE( matcher.match( 1 ) ); |
| 729 | |
| 730 | // These two assertions are the important ones |
| 731 | REQUIRE( first.matchCalled ); |
| 732 | REQUIRE( !second.matchCalled ); |
| 733 | } |
| 734 | // Check that if first returns true, second is not touched |
| 735 | SECTION( "MatchAnyOf" ) { |
| 736 | first.matchSucceeds = true; |
| 737 | |
| 738 | Detail::MatchAnyOf<int> matcher = |
| 739 | Detail::MatchAnyOf<int>{} || first || second; |
| 740 | CHECK( matcher.match( 1 ) ); |
| 741 | |
| 742 | // These two assertions are the important ones |
| 743 | REQUIRE( first.matchCalled ); |
| 744 | REQUIRE( !second.matchCalled ); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | struct CheckedTestingGenericMatcher : Catch::Matchers::MatcherGenericBase { |
| 749 | mutable bool matchCalled = false; |
| 750 | bool matchSucceeds = false; |
| 751 | |
| 752 | bool match( int const& ) const { |
| 753 | matchCalled = true; |
| 754 | return matchSucceeds; |
| 755 | } |
| 756 | std::string describe() const override { |
| 757 | return "CheckedTestingGenericMatcher set to " + |
| 758 | ( matchSucceeds ? std::string( "succeed" ) |
| 759 | : std::string( "fail" ) ); |
| 760 | } |
| 761 | }; |
| 762 | |
| 763 | TEST_CASE( "Composed generic matchers shortcircuit", |
| 764 | "[matchers][composed][generic]" ) { |
| 765 | // Check that if first returns false, second is not touched |
| 766 | CheckedTestingGenericMatcher first, second; |
| 767 | SECTION( "MatchAllOf" ) { |
| 768 | first.matchSucceeds = false; |
| 769 | |
| 770 | Detail::MatchAllOfGeneric<CheckedTestingGenericMatcher, |
| 771 | CheckedTestingGenericMatcher> |
| 772 | matcher{ first, second }; |
| 773 | |
| 774 | CHECK_FALSE( matcher.match( 1 ) ); |
| 775 | |
| 776 | // These two assertions are the important ones |
| 777 | REQUIRE( first.matchCalled ); |
| 778 | REQUIRE( !second.matchCalled ); |
| 779 | } |
| 780 | // Check that if first returns true, second is not touched |
| 781 | SECTION( "MatchAnyOf" ) { |
| 782 | first.matchSucceeds = true; |
| 783 | |
| 784 | Detail::MatchAnyOfGeneric<CheckedTestingGenericMatcher, |
| 785 | CheckedTestingGenericMatcher> |
| 786 | matcher{ first, second }; |
| 787 | CHECK( matcher.match( 1 ) ); |
| 788 | |
| 789 | // These two assertions are the important ones |
| 790 | REQUIRE( first.matchCalled ); |
| 791 | REQUIRE( !second.matchCalled ); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | template <typename Range> |
| 796 | struct EqualsRangeMatcher : Catch::Matchers::MatcherGenericBase { |
| 797 | |
| 798 | EqualsRangeMatcher( Range const& range ): m_range{ range } {} |
| 799 | |
| 800 | template <typename OtherRange> bool match( OtherRange const& other ) const { |
| 801 | using std::begin; |
| 802 | using std::end; |
| 803 | |
| 804 | return std::equal( |
| 805 | begin( m_range ), end( m_range ), begin( other ), end( other ) ); |
| 806 | } |
| 807 | |
| 808 | std::string describe() const override { |
| 809 | return "Equals: " + Catch::rangeToString( m_range ); |
| 810 | } |
| 811 | |
| 812 | private: |
| 813 | Range const& m_range; |
| 814 | }; |
| 815 | |
| 816 | template <typename Range> |
| 817 | auto EqualsRange( const Range& range ) -> EqualsRangeMatcher<Range> { |
| 818 | return EqualsRangeMatcher<Range>{ range }; |
| 819 | } |
| 820 | |
| 821 | TEST_CASE( "Combining templated matchers", "[matchers][templated]" ) { |
| 822 | std::array<int, 3> container{ { 1, 2, 3 } }; |
| 823 | |
| 824 | std::array<int, 3> a{ { 1, 2, 3 } }; |
| 825 | std::vector<int> b{ 0, 1, 2 }; |
| 826 | std::list<int> c{ 4, 5, 6 }; |
| 827 | |
| 828 | REQUIRE_THAT( container, |
| 829 | EqualsRange( a ) || EqualsRange( b ) || EqualsRange( c ) ); |
| 830 | } |
| 831 | |
| 832 | TEST_CASE( "Combining templated and concrete matchers", |
| 833 | "[matchers][templated]" ) { |
| 834 | std::vector<int> vec{ 1, 3, 5 }; |
| 835 | |
| 836 | std::array<int, 3> a{ { 5, 3, 1 } }; |
| 837 | |
| 838 | REQUIRE_THAT( vec, |
| 839 | Predicate<std::vector<int>>( |
| 840 | []( auto const& v ) { |
| 841 | return std::all_of( |
| 842 | v.begin(), v.end(), []( int elem ) { |
| 843 | return elem % 2 == 1; |
| 844 | } ); |
| 845 | }, |
| 846 | "All elements are odd" ) && |
| 847 | !EqualsRange( a ) ); |
| 848 | |
| 849 | const std::string str = "foobar"; |
| 850 | const std::array<char, 6> arr{ { 'f', 'o', 'o', 'b', 'a', 'r' } }; |
| 851 | const std::array<char, 6> bad_arr{ { 'o', 'o', 'f', 'b', 'a', 'r' } }; |
| 852 | |
| 853 | using Catch::Matchers::EndsWith; |
| 854 | using Catch::Matchers::StartsWith; |
| 855 | |
| 856 | REQUIRE_THAT( |
| 857 | str, StartsWith( "foo" ) && EqualsRange( arr ) && EndsWith( "bar" ) ); |
| 858 | REQUIRE_THAT( str, |
| 859 | StartsWith( "foo" ) && !EqualsRange( bad_arr ) && |
| 860 | EndsWith( "bar" ) ); |
| 861 | |
| 862 | REQUIRE_THAT( |
| 863 | str, EqualsRange( arr ) && StartsWith( "foo" ) && EndsWith( "bar" ) ); |
| 864 | REQUIRE_THAT( str, |
| 865 | !EqualsRange( bad_arr ) && StartsWith( "foo" ) && |
| 866 | EndsWith( "bar" ) ); |
| 867 | |
| 868 | REQUIRE_THAT( str, |
| 869 | EqualsRange( bad_arr ) || |
| 870 | ( StartsWith( "foo" ) && EndsWith( "bar" ) ) ); |
| 871 | REQUIRE_THAT( str, |
| 872 | ( StartsWith( "foo" ) && EndsWith( "bar" ) ) || |
| 873 | EqualsRange( bad_arr ) ); |
| 874 | } |
| 875 | |
| 876 | TEST_CASE( "Combining concrete matchers does not use templated matchers", |
| 877 | "[matchers][templated]" ) { |
| 878 | using Catch::Matchers::EndsWith; |
| 879 | using Catch::Matchers::StartsWith; |
| 880 | |
| 881 | STATIC_REQUIRE( |
| 882 | std::is_same<decltype( StartsWith( "foo" ) || |
| 883 | ( StartsWith( "bar" ) && EndsWith( "bar" ) && |
| 884 | !EndsWith( "foo" ) ) ), |
| 885 | Catch::Matchers::Detail::MatchAnyOf<std::string>>::value ); |
| 886 | } |
| 887 | |
| 888 | struct MatcherA : Catch::Matchers::MatcherGenericBase { |
| 889 | std::string describe() const override { |
| 890 | return "equals: (int) 1 or (string) \"1\""; |
| 891 | } |
| 892 | bool match( int i ) const { return i == 1; } |
| 893 | bool match( std::string const& s ) const { return s == "1"; } |
| 894 | }; |
| 895 | |
| 896 | struct MatcherB : Catch::Matchers::MatcherGenericBase { |
| 897 | std::string describe() const override { return "equals: (long long) 1"; } |
| 898 | bool match( long long l ) const { return l == 1ll; } |
| 899 | }; |
| 900 | |
| 901 | struct MatcherC : Catch::Matchers::MatcherGenericBase { |
| 902 | std::string describe() const override { return "equals: (T) 1"; } |
| 903 | template <typename T> bool match( T t ) const { return t == T{ 1 }; } |
| 904 | }; |
| 905 | |
| 906 | struct MatcherD : Catch::Matchers::MatcherGenericBase { |
| 907 | std::string describe() const override { return "equals: true"; } |
| 908 | bool match( bool b ) const { return b == true; } |
| 909 | }; |
| 910 | |
| 911 | TEST_CASE( "Combining only templated matchers", "[matchers][templated]" ) { |
| 912 | STATIC_REQUIRE( |
| 913 | std::is_same<decltype( MatcherA() || MatcherB() ), |
| 914 | Catch::Matchers::Detail:: |
| 915 | MatchAnyOfGeneric<MatcherA, MatcherB>>::value ); |
| 916 | |
| 917 | REQUIRE_THAT( 1, MatcherA() || MatcherB() ); |
| 918 | |
| 919 | STATIC_REQUIRE( |
| 920 | std::is_same<decltype( MatcherA() && MatcherB() ), |
| 921 | Catch::Matchers::Detail:: |
| 922 | MatchAllOfGeneric<MatcherA, MatcherB>>::value ); |
| 923 | |
| 924 | REQUIRE_THAT( 1, MatcherA() && MatcherB() ); |
| 925 | |
| 926 | STATIC_REQUIRE( |
| 927 | std::is_same< |
| 928 | decltype( MatcherA() || !MatcherB() ), |
| 929 | Catch::Matchers::Detail::MatchAnyOfGeneric< |
| 930 | MatcherA, |
| 931 | Catch::Matchers::Detail::MatchNotOfGeneric<MatcherB>>>::value ); |
| 932 | |
| 933 | REQUIRE_THAT( 1, MatcherA() || !MatcherB() ); |
| 934 | } |
| 935 | |
| 936 | TEST_CASE( "Combining MatchAnyOfGeneric does not nest", |
| 937 | "[matchers][templated]" ) { |
| 938 | // MatchAnyOfGeneric LHS + some matcher RHS |
| 939 | STATIC_REQUIRE( |
| 940 | std::is_same< |
| 941 | decltype( ( MatcherA() || MatcherB() ) || MatcherC() ), |
| 942 | Catch::Matchers::Detail:: |
| 943 | MatchAnyOfGeneric<MatcherA, MatcherB, MatcherC>>::value ); |
| 944 | |
| 945 | REQUIRE_THAT( 1, ( MatcherA() || MatcherB() ) || MatcherC() ); |
| 946 | |
| 947 | // some matcher LHS + MatchAnyOfGeneric RHS |
| 948 | STATIC_REQUIRE( |
| 949 | std::is_same< |
| 950 | decltype( MatcherA() || ( MatcherB() || MatcherC() ) ), |
| 951 | Catch::Matchers::Detail:: |
| 952 | MatchAnyOfGeneric<MatcherA, MatcherB, MatcherC>>::value ); |
| 953 | |
| 954 | REQUIRE_THAT( 1, MatcherA() || ( MatcherB() || MatcherC() ) ); |
| 955 | |
| 956 | // MatchAnyOfGeneric LHS + MatchAnyOfGeneric RHS |
| 957 | STATIC_REQUIRE( |
| 958 | std::is_same< |
| 959 | decltype( ( MatcherA() || MatcherB() ) || |
| 960 | ( MatcherC() || MatcherD() ) ), |
| 961 | Catch::Matchers::Detail:: |
| 962 | MatchAnyOfGeneric<MatcherA, MatcherB, MatcherC, MatcherD>>:: |
| 963 | value ); |
| 964 | |
| 965 | REQUIRE_THAT( |
| 966 | 1, ( MatcherA() || MatcherB() ) || ( MatcherC() || MatcherD() ) ); |
| 967 | } |
| 968 | |
| 969 | TEST_CASE( "Combining MatchAllOfGeneric does not nest", |
| 970 | "[matchers][templated]" ) { |
| 971 | // MatchAllOfGeneric lhs + some matcher RHS |
| 972 | STATIC_REQUIRE( |
| 973 | std::is_same< |
| 974 | decltype( ( MatcherA() && MatcherB() ) && MatcherC() ), |
| 975 | Catch::Matchers::Detail:: |
| 976 | MatchAllOfGeneric<MatcherA, MatcherB, MatcherC>>::value ); |
| 977 | |
| 978 | REQUIRE_THAT( 1, ( MatcherA() && MatcherB() ) && MatcherC() ); |
| 979 | |
| 980 | // some matcher LHS + MatchAllOfGeneric RSH |
| 981 | STATIC_REQUIRE( |
| 982 | std::is_same< |
| 983 | decltype( MatcherA() && ( MatcherB() && MatcherC() ) ), |
| 984 | Catch::Matchers::Detail:: |
| 985 | MatchAllOfGeneric<MatcherA, MatcherB, MatcherC>>::value ); |
| 986 | |
| 987 | REQUIRE_THAT( 1, MatcherA() && ( MatcherB() && MatcherC() ) ); |
| 988 | |
| 989 | // MatchAllOfGeneric LHS + MatchAllOfGeneric RHS |
| 990 | STATIC_REQUIRE( |
| 991 | std::is_same< |
| 992 | decltype( ( MatcherA() && MatcherB() ) && |
| 993 | ( MatcherC() && MatcherD() ) ), |
| 994 | Catch::Matchers::Detail:: |
| 995 | MatchAllOfGeneric<MatcherA, MatcherB, MatcherC, MatcherD>>:: |
| 996 | value ); |
| 997 | |
| 998 | REQUIRE_THAT( |
| 999 | 1, ( MatcherA() && MatcherB() ) && ( MatcherC() && MatcherD() ) ); |
| 1000 | } |
| 1001 | |
| 1002 | TEST_CASE( "Combining MatchNotOfGeneric does not nest", |
| 1003 | "[matchers][templated]" ) { |
| 1004 | STATIC_REQUIRE( |
| 1005 | std::is_same< |
| 1006 | decltype( !MatcherA() ), |
| 1007 | Catch::Matchers::Detail::MatchNotOfGeneric<MatcherA>>::value ); |
| 1008 | |
| 1009 | REQUIRE_THAT( 0, !MatcherA() ); |
| 1010 | |
| 1011 | STATIC_REQUIRE( |
| 1012 | std::is_same<decltype( !!MatcherA() ), MatcherA const&>::value ); |
| 1013 | |
| 1014 | REQUIRE_THAT( 1, !!MatcherA() ); |
| 1015 | |
| 1016 | STATIC_REQUIRE( |
| 1017 | std::is_same< |
| 1018 | decltype( !!!MatcherA() ), |
| 1019 | Catch::Matchers::Detail::MatchNotOfGeneric<MatcherA>>::value ); |
| 1020 | |
| 1021 | REQUIRE_THAT( 0, !!!MatcherA() ); |
| 1022 | |
| 1023 | STATIC_REQUIRE( |
| 1024 | std::is_same<decltype( !!!!MatcherA() ), MatcherA const&>::value ); |
| 1025 | |
| 1026 | REQUIRE_THAT( 1, !!!!MatcherA() ); |
| 1027 | } |
| 1028 | |
| 1029 | struct EvilAddressOfOperatorUsed : std::exception { |
| 1030 | EvilAddressOfOperatorUsed() {} |
| 1031 | const char* what() const noexcept override { |
| 1032 | return "overloaded address-of operator of matcher was used instead of " |
| 1033 | "std::addressof"; |
| 1034 | } |
| 1035 | }; |
| 1036 | |
| 1037 | struct EvilCommaOperatorUsed : std::exception { |
| 1038 | EvilCommaOperatorUsed() {} |
| 1039 | const char* what() const noexcept override { |
| 1040 | return "overloaded comma operator of matcher was used"; |
| 1041 | } |
| 1042 | }; |
| 1043 | |
| 1044 | struct EvilMatcher : Catch::Matchers::MatcherGenericBase { |
| 1045 | std::string describe() const override { return "equals: 45"; } |
| 1046 | |
| 1047 | bool match( int i ) const { return i == 45; } |
| 1048 | |
| 1049 | EvilMatcher const* operator&() const { throw EvilAddressOfOperatorUsed(); } |
| 1050 | |
| 1051 | int operator,( EvilMatcher const& ) const { throw EvilCommaOperatorUsed(); } |
| 1052 | }; |
| 1053 | |
| 1054 | TEST_CASE( "Overloaded comma or address-of operators are not used", |
| 1055 | "[matchers][templated]" ) { |
| 1056 | REQUIRE_THROWS_AS( ( EvilMatcher(), EvilMatcher() ), |
| 1057 | EvilCommaOperatorUsed ); |
| 1058 | REQUIRE_THROWS_AS( &EvilMatcher(), EvilAddressOfOperatorUsed ); |
| 1059 | REQUIRE_NOTHROW( EvilMatcher() || ( EvilMatcher() && !EvilMatcher() ) ); |
| 1060 | REQUIRE_NOTHROW( ( EvilMatcher() && EvilMatcher() ) || !EvilMatcher() ); |
| 1061 | } |
| 1062 | |
| 1063 | struct ImmovableMatcher : Catch::Matchers::MatcherGenericBase { |
| 1064 | ImmovableMatcher() = default; |
| 1065 | ImmovableMatcher( ImmovableMatcher const& ) = delete; |
| 1066 | ImmovableMatcher( ImmovableMatcher&& ) = delete; |
| 1067 | ImmovableMatcher& operator=( ImmovableMatcher const& ) = delete; |
| 1068 | ImmovableMatcher& operator=( ImmovableMatcher&& ) = delete; |
| 1069 | |
| 1070 | std::string describe() const override { return "always false"; } |
| 1071 | |
| 1072 | template <typename T> bool match( T&& ) const { return false; } |
| 1073 | }; |
| 1074 | |
| 1075 | struct MatcherWasMovedOrCopied : std::exception { |
| 1076 | MatcherWasMovedOrCopied() {} |
| 1077 | const char* what() const noexcept override { |
| 1078 | return "attempted to copy or move a matcher"; |
| 1079 | } |
| 1080 | }; |
| 1081 | |
| 1082 | struct ThrowOnCopyOrMoveMatcher : Catch::Matchers::MatcherGenericBase { |
| 1083 | ThrowOnCopyOrMoveMatcher() = default; |
| 1084 | [[noreturn]] ThrowOnCopyOrMoveMatcher( ThrowOnCopyOrMoveMatcher const& ): |
| 1085 | Catch::Matchers::MatcherGenericBase() { |
| 1086 | throw MatcherWasMovedOrCopied(); |
| 1087 | } |
| 1088 | [[noreturn]] ThrowOnCopyOrMoveMatcher( ThrowOnCopyOrMoveMatcher&& ): |
| 1089 | Catch::Matchers::MatcherGenericBase() { |
| 1090 | throw MatcherWasMovedOrCopied(); |
| 1091 | } |
| 1092 | ThrowOnCopyOrMoveMatcher& operator=( ThrowOnCopyOrMoveMatcher const& ) { |
| 1093 | throw MatcherWasMovedOrCopied(); |
| 1094 | } |
| 1095 | ThrowOnCopyOrMoveMatcher& operator=( ThrowOnCopyOrMoveMatcher&& ) { |
| 1096 | throw MatcherWasMovedOrCopied(); |
| 1097 | } |
| 1098 | |
| 1099 | std::string describe() const override { return "always false"; } |
| 1100 | |
| 1101 | template <typename T> bool match( T&& ) const { return false; } |
| 1102 | }; |
| 1103 | |
| 1104 | TEST_CASE( "Matchers are not moved or copied", |
| 1105 | "[matchers][templated][approvals]" ) { |
| 1106 | REQUIRE_NOTHROW( |
| 1107 | ( ThrowOnCopyOrMoveMatcher() && ThrowOnCopyOrMoveMatcher() ) || |
| 1108 | !ThrowOnCopyOrMoveMatcher() ); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_CASE( "Immovable matchers can be used", |
| 1112 | "[matchers][templated][approvals]" ) { |
| 1113 | REQUIRE_THAT( 123, |
| 1114 | ( ImmovableMatcher() && ImmovableMatcher() ) || |
| 1115 | !ImmovableMatcher() ); |
| 1116 | } |
| 1117 | |
| 1118 | struct ReferencingMatcher : Catch::Matchers::MatcherGenericBase { |
| 1119 | std::string describe() const override { return "takes reference"; } |
| 1120 | bool match( int& i ) const { return i == 22; } |
| 1121 | }; |
| 1122 | |
| 1123 | TEST_CASE( "Matchers can take references", |
| 1124 | "[matchers][templated][approvals]" ) { |
| 1125 | REQUIRE_THAT( 22, ReferencingMatcher{} ); |
| 1126 | } |
| 1127 | |
| 1128 | #ifdef __clang__ |
| 1129 | # pragma clang diagnostic pop |
| 1130 | #endif |
| 1131 | |
| 1132 | TEMPLATE_TEST_CASE( |
| 1133 | "#2152 - ULP checks between differently signed values were wrong", |
| 1134 | "[matchers][floating-point][ulp]", |
| 1135 | float, |
| 1136 | double ) { |
| 1137 | using Catch::Matchers::WithinULP; |
| 1138 | |
| 1139 | static constexpr TestType smallest_non_zero = |
| 1140 | std::numeric_limits<TestType>::denorm_min(); |
| 1141 | |
| 1142 | CHECK_THAT( smallest_non_zero, WithinULP( -smallest_non_zero, 2 ) ); |
| 1143 | CHECK_THAT( smallest_non_zero, !WithinULP( -smallest_non_zero, 1 ) ); |
| 1144 | } |