| 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/matchers/catch_matchers_container_properties.hpp> |
| 11 |
#include <catch2/matchers/catch_matchers_contains.hpp> |
| 12 |
#include <catch2/matchers/catch_matchers_range_equals.hpp> |
| 13 |
#include <catch2/matchers/catch_matchers_floating_point.hpp> |
| 14 |
#include <catch2/matchers/catch_matchers_quantifiers.hpp> |
| 15 |
#include <catch2/matchers/catch_matchers_predicate.hpp> |
| 16 |
#include <catch2/matchers/catch_matchers_string.hpp> |
| 17 |
|
| 18 |
#include <helpers/range_test_helpers.hpp> |
| 19 |
|
| 20 |
#include <cmath> |
| 21 |
#include <list> |
| 22 |
#include <map> |
| 23 |
#include <type_traits> |
| 24 |
#include <vector> |
| 25 |
#include <memory> |
| 26 |
|
| 27 |
struct MoveOnlyTestElement { |
| 28 |
int num = 0; |
| 29 |
MoveOnlyTestElement(int n) :num(n) {} |
| 30 |
|
| 31 |
MoveOnlyTestElement(MoveOnlyTestElement&& rhs) = default; |
| 32 |
MoveOnlyTestElement& operator=(MoveOnlyTestElement&& rhs) = default; |
| 33 |
|
| 34 |
friend bool operator==(MoveOnlyTestElement const& lhs, MoveOnlyTestElement const& rhs) { |
| 35 |
return lhs.num == rhs.num; |
| 36 |
} |
| 37 |
|
| 38 |
friend std::ostream& operator<<(std::ostream& out, MoveOnlyTestElement const& elem) { |
| 39 |
out << elem.num; |
| 40 |
return out; |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
TEST_CASE("Basic use of the Contains range matcher", "[matchers][templated][contains]") { |
| 45 |
using Catch::Matchers::Contains; |
| 46 |
|
| 47 |
SECTION("Different argument ranges, same element type, default comparison") { |
| 48 |
std::array<int, 3> a{ { 1,2,3 } }; |
| 49 |
std::vector<int> b{ 0,1,2 }; |
| 50 |
std::list<int> c{ 4,5,6 }; |
| 51 |
|
| 52 |
// A contains 1 |
| 53 |
REQUIRE_THAT(a, Contains(1)); |
| 54 |
// B contains 1 |
| 55 |
REQUIRE_THAT(b, Contains(1)); |
| 56 |
// C does not contain 1 |
| 57 |
REQUIRE_THAT(c, !Contains(1)); |
| 58 |
} |
| 59 |
|
| 60 |
SECTION("Different argument ranges, same element type, custom comparison") { |
| 61 |
std::array<int, 3> a{ { 1,2,3 } }; |
| 62 |
std::vector<int> b{ 0,1,2 }; |
| 63 |
std::list<int> c{ 4,5,6 }; |
| 64 |
|
| 65 |
auto close_enough = [](int lhs, int rhs) { return std::abs(lhs - rhs) <= 1; }; |
| 66 |
|
| 67 |
// A contains 1, which is "close enough" to 0 |
| 68 |
REQUIRE_THAT(a, Contains(0, close_enough)); |
| 69 |
// B contains 0 directly |
| 70 |
REQUIRE_THAT(b, Contains(0, close_enough)); |
| 71 |
// C does not contain anything "close enough" to 0 |
| 72 |
REQUIRE_THAT(c, !Contains(0, close_enough)); |
| 73 |
} |
| 74 |
|
| 75 |
SECTION("Different element type, custom comparisons") { |
| 76 |
std::array<std::string, 3> a{ { "abc", "abcd" , "abcde" } }; |
| 77 |
|
| 78 |
REQUIRE_THAT(a, Contains(4, [](auto&& lhs, size_t sz) { |
| 79 |
return lhs.size() == sz; |
| 80 |
})); |
| 81 |
} |
| 82 |
|
| 83 |
SECTION("Can handle type that requires ADL-found free function begin and end") { |
| 84 |
unrelated::needs_ADL_begin<int> in{1, 2, 3, 4, 5}; |
| 85 |
|
| 86 |
REQUIRE_THAT(in, Contains(1)); |
| 87 |
REQUIRE_THAT(in, !Contains(8)); |
| 88 |
} |
| 89 |
|
| 90 |
SECTION("Initialization with move only types") { |
| 91 |
std::array<MoveOnlyTestElement, 3> in{ { MoveOnlyTestElement{ 1 }, MoveOnlyTestElement{ 2 }, MoveOnlyTestElement{ 3 } } }; |
| 92 |
|
| 93 |
REQUIRE_THAT(in, Contains(MoveOnlyTestElement{ 2 })); |
| 94 |
REQUIRE_THAT(in, !Contains(MoveOnlyTestElement{ 9 })); |
| 95 |
} |
| 96 |
|
| 97 |
SECTION("Matching using matcher") { |
| 98 |
std::array<double, 4> in{ {1, 2, 3} }; |
| 99 |
|
| 100 |
REQUIRE_THAT(in, Contains(Catch::Matchers::WithinAbs(0.5, 0.5))); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
namespace { |
| 105 |
|
| 106 |
struct has_empty { |
| 107 |
bool empty() const { return false; } |
| 108 |
}; |
| 109 |
|
| 110 |
} // end unnamed namespace |
| 111 |
|
| 112 |
TEST_CASE("Basic use of the Empty range matcher", "[matchers][templated][empty]") { |
| 113 |
using Catch::Matchers::IsEmpty; |
| 114 |
SECTION("Simple, std-provided containers") { |
| 115 |
std::array<int, 0> empty_array{}; |
| 116 |
std::array<double, 1> non_empty_array{}; |
| 117 |
REQUIRE_THAT(empty_array, IsEmpty()); |
| 118 |
REQUIRE_THAT(non_empty_array, !IsEmpty()); |
| 119 |
|
| 120 |
std::vector<std::string> empty_vec; |
| 121 |
std::vector<char> non_empty_vec{ 'a', 'b', 'c' }; |
| 122 |
REQUIRE_THAT(empty_vec, IsEmpty()); |
| 123 |
REQUIRE_THAT(non_empty_vec, !IsEmpty()); |
| 124 |
|
| 125 |
std::list<std::list<std::list<int>>> inner_lists_are_empty; |
| 126 |
inner_lists_are_empty.push_back({}); |
| 127 |
REQUIRE_THAT(inner_lists_are_empty, !IsEmpty()); |
| 128 |
REQUIRE_THAT(inner_lists_are_empty.front(), IsEmpty()); |
| 129 |
} |
| 130 |
SECTION("Type with empty") { |
| 131 |
REQUIRE_THAT(has_empty{}, !IsEmpty()); |
| 132 |
} |
| 133 |
SECTION("Type requires ADL found empty free function") { |
| 134 |
REQUIRE_THAT(unrelated::ADL_empty{}, IsEmpty()); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
namespace { |
| 139 |
class LessThanMatcher final : public Catch::Matchers::MatcherBase<size_t> { |
| 140 |
size_t m_target; |
| 141 |
public: |
| 142 |
explicit LessThanMatcher(size_t target): |
| 143 |
m_target(target) |
| 144 |
{} |
| 145 |
|
| 146 |
bool match(size_t const& size) const override { |
| 147 |
return size < m_target; |
| 148 |
} |
| 149 |
|
| 150 |
std::string describe() const override { |
| 151 |
return "is less than " + std::to_string(m_target); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
LessThanMatcher Lt(size_t sz) { |
| 156 |
return LessThanMatcher{ sz }; |
| 157 |
} |
| 158 |
|
| 159 |
struct has_size { |
| 160 |
size_t size() const { |
| 161 |
return 13; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
} // end unnamed namespace |
| 166 |
|
| 167 |
TEST_CASE("Usage of the SizeIs range matcher", "[matchers][templated][size]") { |
| 168 |
using Catch::Matchers::SizeIs; |
| 169 |
SECTION("Some with stdlib containers") { |
| 170 |
std::vector<int> empty_vec; |
| 171 |
REQUIRE_THAT(empty_vec, SizeIs(0)); |
| 172 |
REQUIRE_THAT(empty_vec, !SizeIs(2)); |
| 173 |
REQUIRE_THAT(empty_vec, SizeIs(Lt(2))); |
| 174 |
|
| 175 |
std::array<int, 2> arr{}; |
| 176 |
REQUIRE_THAT(arr, SizeIs(2)); |
| 177 |
REQUIRE_THAT(arr, SizeIs( Lt(3))); |
| 178 |
REQUIRE_THAT(arr, !SizeIs(!Lt(3))); |
| 179 |
|
| 180 |
std::map<int, int> map{ {1, 1}, {2, 2}, {3, 3} }; |
| 181 |
REQUIRE_THAT(map, SizeIs(3)); |
| 182 |
} |
| 183 |
SECTION("Type requires ADL found size free function") { |
| 184 |
REQUIRE_THAT(unrelated::ADL_size{}, SizeIs(12)); |
| 185 |
} |
| 186 |
SECTION("Type has size member") { |
| 187 |
REQUIRE_THAT(has_size{}, SizeIs(13)); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
TEST_CASE("Usage of AllMatch range matcher", "[matchers][templated][quantifiers]") { |
| 193 |
using Catch::Matchers::AllMatch; |
| 194 |
using Catch::Matchers::Predicate; |
| 195 |
|
| 196 |
SECTION("Basic usage") { |
| 197 |
using Catch::Matchers::Contains; |
| 198 |
using Catch::Matchers::SizeIs; |
| 199 |
|
| 200 |
std::array<std::array<int, 5>, 5> data{{ |
| 201 |
{{ 0, 1, 2, 3, 5 }}, |
| 202 |
{{ 4,-3,-2, 5, 0 }}, |
| 203 |
{{ 0, 0, 0, 5, 0 }}, |
| 204 |
{{ 0,-5, 0, 5, 0 }}, |
| 205 |
{{ 1, 0, 0,-1, 5 }} |
| 206 |
}}; |
| 207 |
|
| 208 |
REQUIRE_THAT(data, AllMatch(SizeIs(5))); |
| 209 |
REQUIRE_THAT(data, !AllMatch(Contains(0) && Contains(1))); |
| 210 |
} |
| 211 |
|
| 212 |
SECTION("Type requires ADL found begin and end") { |
| 213 |
unrelated::needs_ADL_begin<int> needs_adl{ 1, 2, 3, 4, 5 }; |
| 214 |
REQUIRE_THAT( needs_adl, AllMatch( Predicate<int>( []( int elem ) { |
| 215 |
return elem < 6; |
| 216 |
} ) ) ); |
| 217 |
} |
| 218 |
|
| 219 |
SECTION("Shortcircuiting") { |
| 220 |
with_mocked_iterator_access<int> mocked{ 1, 2, 3, 4, 5 }; |
| 221 |
SECTION("All are read") { |
| 222 |
auto allMatch = AllMatch(Predicate<int>([](int elem) { |
| 223 |
return elem < 10; |
| 224 |
})); |
| 225 |
REQUIRE_THAT(mocked, allMatch); |
| 226 |
REQUIRE(mocked.m_derefed[0]); |
| 227 |
REQUIRE(mocked.m_derefed[1]); |
| 228 |
REQUIRE(mocked.m_derefed[2]); |
| 229 |
REQUIRE(mocked.m_derefed[3]); |
| 230 |
REQUIRE(mocked.m_derefed[4]); |
| 231 |
} |
| 232 |
SECTION("Short-circuited") { |
| 233 |
auto allMatch = AllMatch(Predicate<int>([](int elem) { |
| 234 |
return elem < 3; |
| 235 |
})); |
| 236 |
REQUIRE_THAT(mocked, !allMatch); |
| 237 |
REQUIRE(mocked.m_derefed[0]); |
| 238 |
REQUIRE(mocked.m_derefed[1]); |
| 239 |
REQUIRE(mocked.m_derefed[2]); |
| 240 |
REQUIRE_FALSE(mocked.m_derefed[3]); |
| 241 |
REQUIRE_FALSE(mocked.m_derefed[4]); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
TEST_CASE("Usage of AnyMatch range matcher", "[matchers][templated][quantifiers]") { |
| 247 |
using Catch::Matchers::AnyMatch; |
| 248 |
using Catch::Matchers::Predicate; |
| 249 |
|
| 250 |
SECTION("Basic usage") { |
| 251 |
using Catch::Matchers::Contains; |
| 252 |
using Catch::Matchers::SizeIs; |
| 253 |
|
| 254 |
std::array<std::array<int, 5>, 5> data{ { |
| 255 |
{{ 0, 1, 2, 3, 5 }}, |
| 256 |
{{ 4,-3,-2, 5, 0 }}, |
| 257 |
{{ 0, 0, 0, 5, 0 }}, |
| 258 |
{{ 0,-5, 0, 5, 0 }}, |
| 259 |
{{ 1, 0, 0,-1, 5 }} |
| 260 |
} }; |
| 261 |
|
| 262 |
REQUIRE_THAT(data, AnyMatch(SizeIs(5))); |
| 263 |
REQUIRE_THAT(data, !AnyMatch(Contains(0) && Contains(10))); |
| 264 |
} |
| 265 |
|
| 266 |
SECTION( "Type requires ADL found begin and end" ) { |
| 267 |
unrelated::needs_ADL_begin<int> needs_adl{ 1, 2, 3, 4, 5 }; |
| 268 |
REQUIRE_THAT( needs_adl, AnyMatch( Predicate<int>( []( int elem ) { |
| 269 |
return elem < 3; |
| 270 |
} ) ) ); |
| 271 |
} |
| 272 |
|
| 273 |
SECTION("Shortcircuiting") { |
| 274 |
with_mocked_iterator_access<int> mocked{ 1, 2, 3, 4, 5 }; |
| 275 |
SECTION("All are read") { |
| 276 |
auto anyMatch = AnyMatch( |
| 277 |
Predicate<int>( []( int elem ) { return elem > 10; } ) ); |
| 278 |
REQUIRE_THAT( mocked, !anyMatch ); |
| 279 |
REQUIRE( mocked.m_derefed[0] ); |
| 280 |
REQUIRE( mocked.m_derefed[1] ); |
| 281 |
REQUIRE( mocked.m_derefed[2] ); |
| 282 |
REQUIRE( mocked.m_derefed[3] ); |
| 283 |
REQUIRE( mocked.m_derefed[4] ); |
| 284 |
} |
| 285 |
SECTION("Short-circuited") { |
| 286 |
auto anyMatch = AnyMatch( |
| 287 |
Predicate<int>( []( int elem ) { return elem < 3; } ) ); |
| 288 |
REQUIRE_THAT( mocked, anyMatch ); |
| 289 |
REQUIRE( mocked.m_derefed[0] ); |
| 290 |
REQUIRE_FALSE( mocked.m_derefed[1] ); |
| 291 |
REQUIRE_FALSE( mocked.m_derefed[2] ); |
| 292 |
REQUIRE_FALSE( mocked.m_derefed[3] ); |
| 293 |
REQUIRE_FALSE( mocked.m_derefed[4] ); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
TEST_CASE("Usage of NoneMatch range matcher", "[matchers][templated][quantifiers]") { |
| 299 |
using Catch::Matchers::NoneMatch; |
| 300 |
using Catch::Matchers::Predicate; |
| 301 |
|
| 302 |
SECTION("Basic usage") { |
| 303 |
using Catch::Matchers::Contains; |
| 304 |
using Catch::Matchers::SizeIs; |
| 305 |
|
| 306 |
std::array<std::array<int, 5>, 5> data{ { |
| 307 |
{{ 0, 1, 2, 3, 5 }}, |
| 308 |
{{ 4,-3,-2, 5, 0 }}, |
| 309 |
{{ 0, 0, 0, 5, 0 }}, |
| 310 |
{{ 0,-5, 0, 5, 0 }}, |
| 311 |
{{ 1, 0, 0,-1, 5 }} |
| 312 |
} }; |
| 313 |
|
| 314 |
REQUIRE_THAT(data, NoneMatch(SizeIs(6))); |
| 315 |
REQUIRE_THAT(data, !NoneMatch(Contains(0) && Contains(1))); |
| 316 |
} |
| 317 |
|
| 318 |
SECTION( "Type requires ADL found begin and end" ) { |
| 319 |
unrelated::needs_ADL_begin<int> needs_adl{ 1, 2, 3, 4, 5 }; |
| 320 |
REQUIRE_THAT( needs_adl, NoneMatch( Predicate<int>( []( int elem ) { |
| 321 |
return elem > 6; |
| 322 |
} ) ) ); |
| 323 |
} |
| 324 |
|
| 325 |
SECTION("Shortcircuiting") { |
| 326 |
with_mocked_iterator_access<int> mocked{ 1, 2, 3, 4, 5 }; |
| 327 |
SECTION("All are read") { |
| 328 |
auto noneMatch = NoneMatch( |
| 329 |
Predicate<int>([](int elem) { return elem > 10; })); |
| 330 |
REQUIRE_THAT(mocked, noneMatch); |
| 331 |
REQUIRE(mocked.m_derefed[0]); |
| 332 |
REQUIRE(mocked.m_derefed[1]); |
| 333 |
REQUIRE(mocked.m_derefed[2]); |
| 334 |
REQUIRE(mocked.m_derefed[3]); |
| 335 |
REQUIRE(mocked.m_derefed[4]); |
| 336 |
} |
| 337 |
SECTION("Short-circuited") { |
| 338 |
auto noneMatch = NoneMatch( |
| 339 |
Predicate<int>([](int elem) { return elem < 3; })); |
| 340 |
REQUIRE_THAT(mocked, !noneMatch); |
| 341 |
REQUIRE(mocked.m_derefed[0]); |
| 342 |
REQUIRE_FALSE(mocked.m_derefed[1]); |
| 343 |
REQUIRE_FALSE(mocked.m_derefed[2]); |
| 344 |
REQUIRE_FALSE(mocked.m_derefed[3]); |
| 345 |
REQUIRE_FALSE(mocked.m_derefed[4]); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
namespace { |
| 351 |
struct ConvertibleToBool |
| 352 |
{ |
| 353 |
bool v; |
| 354 |
|
| 355 |
explicit operator bool() const |
| 356 |
{ |
| 357 |
return v; |
| 358 |
} |
| 359 |
}; |
| 360 |
} |
| 361 |
|
| 362 |
namespace Catch { |
| 363 |
template <> |
| 364 |
struct StringMaker<ConvertibleToBool> { |
| 365 |
static std::string |
| 366 |
convert( ConvertibleToBool const& convertible_to_bool ) { |
| 367 |
return ::Catch::Detail::stringify( convertible_to_bool.v ); |
| 368 |
} |
| 369 |
}; |
| 370 |
} // namespace Catch |
| 371 |
|
| 372 |
TEST_CASE("Usage of AllTrue range matcher", "[matchers][templated][quantifiers]") { |
| 373 |
using Catch::Matchers::AllTrue; |
| 374 |
|
| 375 |
SECTION( "Basic usage" ) { |
| 376 |
SECTION( "All true evaluates to true" ) { |
| 377 |
std::array<bool, 5> const data{ { true, true, true, true, true } }; |
| 378 |
REQUIRE_THAT( data, AllTrue() ); |
| 379 |
} |
| 380 |
SECTION( "Empty evaluates to true" ) { |
| 381 |
std::array<bool, 0> const data{}; |
| 382 |
REQUIRE_THAT( data, AllTrue() ); |
| 383 |
} |
| 384 |
SECTION( "One false evaluates to false" ) { |
| 385 |
std::array<bool, 5> const data{ { true, true, false, true, true } }; |
| 386 |
REQUIRE_THAT( data, !AllTrue() ); |
| 387 |
} |
| 388 |
SECTION( "All false evaluates to false" ) { |
| 389 |
std::array<bool, 5> const data{ |
| 390 |
{ false, false, false, false, false } }; |
| 391 |
REQUIRE_THAT( data, !AllTrue() ); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
SECTION( "Contained type is convertible to bool" ) { |
| 396 |
SECTION( "All true evaluates to true" ) { |
| 397 |
std::array<ConvertibleToBool, 5> const data{ |
| 398 |
{ { true }, { true }, { true }, { true }, { true } } }; |
| 399 |
REQUIRE_THAT( data, AllTrue() ); |
| 400 |
} |
| 401 |
SECTION( "One false evaluates to false" ) { |
| 402 |
std::array<ConvertibleToBool, 5> const data{ |
| 403 |
{ { true }, { true }, { false }, { true }, { true } } }; |
| 404 |
REQUIRE_THAT( data, !AllTrue() ); |
| 405 |
} |
| 406 |
SECTION( "All false evaluates to false" ) { |
| 407 |
std::array<ConvertibleToBool, 5> const data{ |
| 408 |
{ { false }, { false }, { false }, { false }, { false } } }; |
| 409 |
REQUIRE_THAT( data, !AllTrue() ); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
SECTION( "Shortcircuiting" ) { |
| 414 |
SECTION( "All are read" ) { |
| 415 |
with_mocked_iterator_access<bool> const mocked{ |
| 416 |
true, true, true, true, true }; |
| 417 |
REQUIRE_THAT( mocked, AllTrue() ); |
| 418 |
REQUIRE( mocked.m_derefed[0] ); |
| 419 |
REQUIRE( mocked.m_derefed[1] ); |
| 420 |
REQUIRE( mocked.m_derefed[2] ); |
| 421 |
REQUIRE( mocked.m_derefed[3] ); |
| 422 |
REQUIRE( mocked.m_derefed[4] ); |
| 423 |
} |
| 424 |
SECTION( "Short-circuited" ) { |
| 425 |
with_mocked_iterator_access<bool> const mocked{ |
| 426 |
true, true, false, true, true }; |
| 427 |
REQUIRE_THAT( mocked, !AllTrue() ); |
| 428 |
REQUIRE( mocked.m_derefed[0] ); |
| 429 |
REQUIRE( mocked.m_derefed[1] ); |
| 430 |
REQUIRE( mocked.m_derefed[2] ); |
| 431 |
REQUIRE_FALSE( mocked.m_derefed[3] ); |
| 432 |
REQUIRE_FALSE( mocked.m_derefed[4] ); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
TEST_CASE( "Usage of NoneTrue range matcher", "[matchers][templated][quantifiers]" ) { |
| 438 |
using Catch::Matchers::NoneTrue; |
| 439 |
|
| 440 |
SECTION( "Basic usage" ) { |
| 441 |
SECTION( "All true evaluates to false" ) { |
| 442 |
std::array<bool, 5> const data{ { true, true, true, true, true } }; |
| 443 |
REQUIRE_THAT( data, !NoneTrue() ); |
| 444 |
} |
| 445 |
SECTION( "Empty evaluates to true" ) { |
| 446 |
std::array<bool, 0> const data{}; |
| 447 |
REQUIRE_THAT( data, NoneTrue() ); |
| 448 |
} |
| 449 |
SECTION( "One true evaluates to false" ) { |
| 450 |
std::array<bool, 5> const data{ |
| 451 |
{ false, false, true, false, false } }; |
| 452 |
REQUIRE_THAT( data, !NoneTrue() ); |
| 453 |
} |
| 454 |
SECTION( "All false evaluates to true" ) { |
| 455 |
std::array<bool, 5> const data{ |
| 456 |
{ false, false, false, false, false } }; |
| 457 |
REQUIRE_THAT( data, NoneTrue() ); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
SECTION( "Contained type is convertible to bool" ) { |
| 462 |
SECTION( "All true evaluates to false" ) { |
| 463 |
std::array<ConvertibleToBool, 5> const data{ |
| 464 |
{ { true }, { true }, { true }, { true }, { true } } }; |
| 465 |
REQUIRE_THAT( data, !NoneTrue() ); |
| 466 |
} |
| 467 |
SECTION( "One true evaluates to false" ) { |
| 468 |
std::array<ConvertibleToBool, 5> const data{ |
| 469 |
{ { false }, { false }, { true }, { false }, { false } } }; |
| 470 |
REQUIRE_THAT( data, !NoneTrue() ); |
| 471 |
} |
| 472 |
SECTION( "All false evaluates to true" ) { |
| 473 |
std::array<ConvertibleToBool, 5> const data{ |
| 474 |
{ { false }, { false }, { false }, { false }, { false } } }; |
| 475 |
REQUIRE_THAT( data, NoneTrue() ); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
SECTION( "Shortcircuiting" ) { |
| 480 |
SECTION( "All are read" ) { |
| 481 |
with_mocked_iterator_access<bool> const mocked{ |
| 482 |
false, false, false, false, false }; |
| 483 |
REQUIRE_THAT( mocked, NoneTrue() ); |
| 484 |
REQUIRE( mocked.m_derefed[0] ); |
| 485 |
REQUIRE( mocked.m_derefed[1] ); |
| 486 |
REQUIRE( mocked.m_derefed[2] ); |
| 487 |
REQUIRE( mocked.m_derefed[3] ); |
| 488 |
REQUIRE( mocked.m_derefed[4] ); |
| 489 |
} |
| 490 |
SECTION( "Short-circuited" ) { |
| 491 |
with_mocked_iterator_access<bool> const mocked{ |
| 492 |
false, false, true, true, true }; |
| 493 |
REQUIRE_THAT( mocked, !NoneTrue() ); |
| 494 |
REQUIRE( mocked.m_derefed[0] ); |
| 495 |
REQUIRE( mocked.m_derefed[1] ); |
| 496 |
REQUIRE( mocked.m_derefed[2] ); |
| 497 |
REQUIRE_FALSE( mocked.m_derefed[3] ); |
| 498 |
REQUIRE_FALSE( mocked.m_derefed[4] ); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
TEST_CASE( "Usage of AnyTrue range matcher", "[matchers][templated][quantifiers]" ) { |
| 504 |
using Catch::Matchers::AnyTrue; |
| 505 |
|
| 506 |
SECTION( "Basic usage" ) { |
| 507 |
SECTION( "All true evaluates to true" ) { |
| 508 |
std::array<bool, 5> const data{ { true, true, true, true, true } }; |
| 509 |
REQUIRE_THAT( data, AnyTrue() ); |
| 510 |
} |
| 511 |
SECTION( "Empty evaluates to false" ) { |
| 512 |
std::array<bool, 0> const data{}; |
| 513 |
REQUIRE_THAT( data, !AnyTrue() ); |
| 514 |
} |
| 515 |
SECTION( "One true evaluates to true" ) { |
| 516 |
std::array<bool, 5> const data{ |
| 517 |
{ false, false, true, false, false } }; |
| 518 |
REQUIRE_THAT( data, AnyTrue() ); |
| 519 |
} |
| 520 |
SECTION( "All false evaluates to false" ) { |
| 521 |
std::array<bool, 5> const data{ |
| 522 |
{ false, false, false, false, false } }; |
| 523 |
REQUIRE_THAT( data, !AnyTrue() ); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
SECTION( "Contained type is convertible to bool" ) { |
| 528 |
SECTION( "All true evaluates to true" ) { |
| 529 |
std::array<ConvertibleToBool, 5> const data{ |
| 530 |
{ { true }, { true }, { true }, { true }, { true } } }; |
| 531 |
REQUIRE_THAT( data, AnyTrue() ); |
| 532 |
} |
| 533 |
SECTION( "One true evaluates to true" ) { |
| 534 |
std::array<ConvertibleToBool, 5> const data{ |
| 535 |
{ { false }, { false }, { true }, { false }, { false } } }; |
| 536 |
REQUIRE_THAT( data, AnyTrue() ); |
| 537 |
} |
| 538 |
SECTION( "All false evaluates to false" ) { |
| 539 |
std::array<ConvertibleToBool, 5> const data{ |
| 540 |
{ { false }, { false }, { false }, { false }, { false } } }; |
| 541 |
REQUIRE_THAT( data, !AnyTrue() ); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
SECTION( "Shortcircuiting" ) { |
| 546 |
SECTION( "All are read" ) { |
| 547 |
with_mocked_iterator_access<bool> const mocked{ |
| 548 |
false, false, false, false, true }; |
| 549 |
REQUIRE_THAT( mocked, AnyTrue() ); |
| 550 |
REQUIRE( mocked.m_derefed[0] ); |
| 551 |
REQUIRE( mocked.m_derefed[1] ); |
| 552 |
REQUIRE( mocked.m_derefed[2] ); |
| 553 |
REQUIRE( mocked.m_derefed[3] ); |
| 554 |
REQUIRE( mocked.m_derefed[4] ); |
| 555 |
} |
| 556 |
SECTION( "Short-circuited" ) { |
| 557 |
with_mocked_iterator_access<bool> const mocked{ |
| 558 |
false, false, true, true, true }; |
| 559 |
REQUIRE_THAT( mocked, AnyTrue() ); |
| 560 |
REQUIRE( mocked.m_derefed[0] ); |
| 561 |
REQUIRE( mocked.m_derefed[1] ); |
| 562 |
REQUIRE( mocked.m_derefed[2] ); |
| 563 |
REQUIRE_FALSE( mocked.m_derefed[3] ); |
| 564 |
REQUIRE_FALSE( mocked.m_derefed[4] ); |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
TEST_CASE("All/Any/None True matchers support types with ADL begin", |
| 570 |
"[approvals][matchers][quantifiers][templated]") { |
| 571 |
using Catch::Matchers::AllTrue; |
| 572 |
using Catch::Matchers::NoneTrue; |
| 573 |
using Catch::Matchers::AnyTrue; |
| 574 |
|
| 575 |
|
| 576 |
SECTION( "Type requires ADL found begin and end" ) { |
| 577 |
unrelated::needs_ADL_begin<bool> const needs_adl{ |
| 578 |
true, true, true, true, true }; |
| 579 |
REQUIRE_THAT( needs_adl, AllTrue() ); |
| 580 |
} |
| 581 |
|
| 582 |
SECTION( "Type requires ADL found begin and end" ) { |
| 583 |
unrelated::needs_ADL_begin<bool> const needs_adl{ |
| 584 |
false, false, false, false, false }; |
| 585 |
REQUIRE_THAT( needs_adl, NoneTrue() ); |
| 586 |
} |
| 587 |
|
| 588 |
SECTION( "Type requires ADL found begin and end" ) { |
| 589 |
unrelated::needs_ADL_begin<bool> const needs_adl{ |
| 590 |
false, false, true, false, false }; |
| 591 |
REQUIRE_THAT( needs_adl, AnyTrue() ); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
// Range loop iterating over range with different types for begin and end is a |
| 596 |
// C++17 feature, and GCC refuses to compile such code unless the lang mode is |
| 597 |
// set to C++17 or later. |
| 598 |
#if defined(CATCH_CPP17_OR_GREATER) |
| 599 |
|
| 600 |
TEST_CASE( "The quantifier range matchers support types with different types returned from begin and end", |
| 601 |
"[matchers][templated][quantifiers][approvals]" ) { |
| 602 |
using Catch::Matchers::AllMatch; |
| 603 |
using Catch::Matchers::AllTrue; |
| 604 |
using Catch::Matchers::AnyMatch; |
| 605 |
using Catch::Matchers::AnyTrue; |
| 606 |
using Catch::Matchers::NoneMatch; |
| 607 |
using Catch::Matchers::NoneTrue; |
| 608 |
|
| 609 |
using Catch::Matchers::Predicate; |
| 610 |
|
| 611 |
SECTION( "AllAnyNoneMatch" ) { |
| 612 |
has_different_begin_end_types<int> diff_types{ 1, 2, 3, 4, 5 }; |
| 613 |
REQUIRE_THAT( diff_types, !AllMatch( Predicate<int>( []( int elem ) { |
| 614 |
return elem < 3; |
| 615 |
} ) ) ); |
| 616 |
|
| 617 |
REQUIRE_THAT( diff_types, AnyMatch( Predicate<int>( []( int elem ) { |
| 618 |
return elem < 2; |
| 619 |
} ) ) ); |
| 620 |
|
| 621 |
REQUIRE_THAT( diff_types, !NoneMatch( Predicate<int>( []( int elem ) { |
| 622 |
return elem < 3; |
| 623 |
} ) ) ); |
| 624 |
} |
| 625 |
SECTION( "AllAnyNoneTrue" ) { |
| 626 |
has_different_begin_end_types<bool> diff_types{ false, false, true, false, false }; |
| 627 |
|
| 628 |
REQUIRE_THAT( diff_types, !AllTrue() ); |
| 629 |
REQUIRE_THAT( diff_types, AnyTrue() ); |
| 630 |
REQUIRE_THAT( diff_types, !NoneTrue() ); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
TEST_CASE( "RangeEquals supports ranges with different types returned from begin and end", |
| 635 |
"[matchers][templated][range][approvals] ") { |
| 636 |
using Catch::Matchers::RangeEquals; |
| 637 |
using Catch::Matchers::UnorderedRangeEquals; |
| 638 |
|
| 639 |
has_different_begin_end_types<int> diff_types{ 1, 2, 3, 4, 5 }; |
| 640 |
std::array<int, 5> arr1{ { 1, 2, 3, 4, 5 } }, arr2{ { 2, 3, 4, 5, 6 } }; |
| 641 |
|
| 642 |
REQUIRE_THAT( diff_types, RangeEquals( arr1 ) ); |
| 643 |
REQUIRE_THAT( diff_types, RangeEquals( arr2, []( int l, int r ) { |
| 644 |
return l + 1 == r; |
| 645 |
} ) ); |
| 646 |
REQUIRE_THAT( diff_types, UnorderedRangeEquals( diff_types ) ); |
| 647 |
} |
| 648 |
|
| 649 |
TEST_CASE( "RangeContains supports ranges with different types returned from " |
| 650 |
"begin and end", |
| 651 |
"[matchers][templated][range][approvals]" ) { |
| 652 |
using Catch::Matchers::Contains; |
| 653 |
|
| 654 |
has_different_begin_end_types<size_t> diff_types{ 1, 2, 3, 4, 5 }; |
| 655 |
REQUIRE_THAT( diff_types, Contains( size_t( 3 ) ) ); |
| 656 |
REQUIRE_THAT( diff_types, Contains( LessThanMatcher( size_t( 4 ) ) ) ); |
| 657 |
} |
| 658 |
|
| 659 |
#endif |
| 660 |
|
| 661 |
TEST_CASE( "Usage of RangeEquals range matcher", "[matchers][templated][quantifiers]" ) { |
| 662 |
using Catch::Matchers::RangeEquals; |
| 663 |
|
| 664 |
// In these tests, the types are always the same - type conversion is in the next section |
| 665 |
SECTION( "Basic usage" ) { |
| 666 |
SECTION( "Empty container matches empty container" ) { |
| 667 |
const std::vector<int> empty_vector; |
| 668 |
CHECK_THAT( empty_vector, RangeEquals( empty_vector ) ); |
| 669 |
} |
| 670 |
SECTION( "Empty container does not match non-empty container" ) { |
| 671 |
const std::vector<int> empty_vector; |
| 672 |
const std::vector<int> non_empty_vector{ 1 }; |
| 673 |
CHECK_THAT( empty_vector, !RangeEquals( non_empty_vector ) ); |
| 674 |
// ...and in reverse |
| 675 |
CHECK_THAT( non_empty_vector, !RangeEquals( empty_vector ) ); |
| 676 |
} |
| 677 |
SECTION( "Two equal 1-length non-empty containers" ) { |
| 678 |
const std::array<int, 1> non_empty_array{ { 1 } }; |
| 679 |
CHECK_THAT( non_empty_array, RangeEquals( non_empty_array ) ); |
| 680 |
} |
| 681 |
SECTION( "Two equal-sized, equal, non-empty containers" ) { |
| 682 |
const std::array<int, 3> array_a{ { 1, 2, 3 } }; |
| 683 |
CHECK_THAT( array_a, RangeEquals( array_a ) ); |
| 684 |
} |
| 685 |
SECTION( "Two equal-sized, non-equal, non-empty containers" ) { |
| 686 |
const std::array<int, 3> array_a{ { 1, 2, 3 } }; |
| 687 |
const std::array<int, 3> array_b{ { 2, 2, 3 } }; |
| 688 |
const std::array<int, 3> array_c{ { 1, 2, 2 } }; |
| 689 |
CHECK_THAT( array_a, !RangeEquals( array_b ) ); |
| 690 |
CHECK_THAT( array_a, !RangeEquals( array_c ) ); |
| 691 |
} |
| 692 |
SECTION( "Two non-equal-sized, non-empty containers (with same first " |
| 693 |
"elements)" ) { |
| 694 |
const std::vector<int> vector_a{ 1, 2, 3 }; |
| 695 |
const std::vector<int> vector_b{ 1, 2, 3, 4 }; |
| 696 |
CHECK_THAT( vector_a, !RangeEquals( vector_b ) ); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
SECTION( "Custom predicate" ) { |
| 701 |
|
| 702 |
auto close_enough = []( int lhs, int rhs ) { |
| 703 |
return std::abs( lhs - rhs ) <= 1; |
| 704 |
}; |
| 705 |
|
| 706 |
SECTION( "Two equal non-empty containers (close enough)" ) { |
| 707 |
const std::vector<int> vector_a{ { 1, 2, 3 } }; |
| 708 |
const std::vector<int> vector_a_plus_1{ { 2, 3, 4 } }; |
| 709 |
CHECK_THAT( vector_a, RangeEquals( vector_a_plus_1, close_enough ) ); |
| 710 |
} |
| 711 |
SECTION( "Two non-equal non-empty containers (close enough)" ) { |
| 712 |
const std::vector<int> vector_a{ { 1, 2, 3 } }; |
| 713 |
const std::vector<int> vector_b{ { 3, 3, 4 } }; |
| 714 |
CHECK_THAT( vector_a, !RangeEquals( vector_b, close_enough ) ); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
SECTION( "Ranges that need ADL begin/end" ) { |
| 719 |
unrelated::needs_ADL_begin<int> const |
| 720 |
needs_adl1{ 1, 2, 3, 4, 5 }, |
| 721 |
needs_adl2{ 1, 2, 3, 4, 5 }, |
| 722 |
needs_adl3{ 2, 3, 4, 5, 6 }; |
| 723 |
|
| 724 |
REQUIRE_THAT( needs_adl1, RangeEquals( needs_adl2 ) ); |
| 725 |
REQUIRE_THAT( needs_adl1, RangeEquals( needs_adl3, []( int l, int r ) { |
| 726 |
return l + 1 == r; |
| 727 |
} ) ); |
| 728 |
} |
| 729 |
|
| 730 |
SECTION( "Compare against std::initializer_list" ) { |
| 731 |
const std::array<int, 3> array_a{ { 1, 2, 3 } }; |
| 732 |
|
| 733 |
REQUIRE_THAT( array_a, RangeEquals( { 1, 2, 3 } ) ); |
| 734 |
REQUIRE_THAT( array_a, RangeEquals( { 2, 4, 6 }, []( int l, int r ) { |
| 735 |
return l * 2 == r; |
| 736 |
} ) ); |
| 737 |
} |
| 738 |
|
| 739 |
SECTION("Check short-circuiting behaviour") { |
| 740 |
with_mocked_iterator_access<int> const mocked1{ 1, 2, 3, 4 }; |
| 741 |
|
| 742 |
SECTION( "Check short-circuits on failure" ) { |
| 743 |
std::array<int, 4> arr{ { 1, 2, 4, 4 } }; |
| 744 |
|
| 745 |
REQUIRE_THAT( mocked1, !RangeEquals( arr ) ); |
| 746 |
REQUIRE( mocked1.m_derefed[0] ); |
| 747 |
REQUIRE( mocked1.m_derefed[1] ); |
| 748 |
REQUIRE( mocked1.m_derefed[2] ); |
| 749 |
REQUIRE_FALSE( mocked1.m_derefed[3] ); |
| 750 |
} |
| 751 |
SECTION("All elements are checked on success") { |
| 752 |
std::array<int, 4> arr{ { 1, 2, 3, 4 } }; |
| 753 |
|
| 754 |
REQUIRE_THAT( mocked1, RangeEquals( arr ) ); |
| 755 |
REQUIRE( mocked1.m_derefed[0] ); |
| 756 |
REQUIRE( mocked1.m_derefed[1] ); |
| 757 |
REQUIRE( mocked1.m_derefed[2] ); |
| 758 |
REQUIRE( mocked1.m_derefed[3] ); |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
TEST_CASE( "Usage of UnorderedRangeEquals range matcher", |
| 764 |
"[matchers][templated][quantifiers]" ) { |
| 765 |
using Catch::Matchers::UnorderedRangeEquals; |
| 766 |
|
| 767 |
// In these tests, the types are always the same - type conversion is in the |
| 768 |
// next section |
| 769 |
SECTION( "Basic usage" ) { |
| 770 |
SECTION( "Empty container matches empty container" ) { |
| 771 |
const std::vector<int> empty_vector; |
| 772 |
CHECK_THAT( empty_vector, UnorderedRangeEquals( empty_vector ) ); |
| 773 |
} |
| 774 |
SECTION( "Empty container does not match non-empty container" ) { |
| 775 |
const std::vector<int> empty_vector; |
| 776 |
const std::vector<int> non_empty_vector{ 1 }; |
| 777 |
CHECK_THAT( empty_vector, |
| 778 |
!UnorderedRangeEquals( non_empty_vector ) ); |
| 779 |
// ...and in reverse |
| 780 |
CHECK_THAT( non_empty_vector, |
| 781 |
!UnorderedRangeEquals( empty_vector ) ); |
| 782 |
} |
| 783 |
SECTION( "Two equal 1-length non-empty containers" ) { |
| 784 |
const std::array<int, 1> non_empty_array{ { 1 } }; |
| 785 |
CHECK_THAT( non_empty_array, |
| 786 |
UnorderedRangeEquals( non_empty_array ) ); |
| 787 |
} |
| 788 |
SECTION( "Two equal-sized, equal, non-empty containers" ) { |
| 789 |
const std::array<int, 3> array_a{ { 1, 2, 3 } }; |
| 790 |
CHECK_THAT( array_a, UnorderedRangeEquals( array_a ) ); |
| 791 |
} |
| 792 |
SECTION( "Two equal-sized, non-equal, non-empty containers" ) { |
| 793 |
const std::array<int, 3> array_a{ { 1, 2, 3 } }; |
| 794 |
const std::array<int, 3> array_b{ { 2, 2, 3 } }; |
| 795 |
CHECK_THAT( array_a, !UnorderedRangeEquals( array_b ) ); |
| 796 |
} |
| 797 |
SECTION( "Two non-equal-sized, non-empty containers" ) { |
| 798 |
const std::vector<int> vector_a{ 1, 2, 3 }; |
| 799 |
const std::vector<int> vector_b{ 1, 2, 3, 4 }; |
| 800 |
CHECK_THAT( vector_a, !UnorderedRangeEquals( vector_b ) ); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
SECTION( "Custom predicate" ) { |
| 805 |
|
| 806 |
auto close_enough = []( int lhs, int rhs ) { |
| 807 |
return std::abs( lhs - rhs ) <= 1; |
| 808 |
}; |
| 809 |
|
| 810 |
SECTION( "Two equal non-empty containers (close enough)" ) { |
| 811 |
const std::vector<int> vector_a{ { 1, 10, 20 } }; |
| 812 |
const std::vector<int> vector_a_plus_1{ { 11, 21, 2 } }; |
| 813 |
CHECK_THAT( vector_a, |
| 814 |
UnorderedRangeEquals( vector_a_plus_1, close_enough ) ); |
| 815 |
} |
| 816 |
SECTION( "Two non-equal non-empty containers (close enough)" ) { |
| 817 |
const std::vector<int> vector_a{ { 1, 10, 21 } }; |
| 818 |
const std::vector<int> vector_b{ { 11, 21, 3 } }; |
| 819 |
CHECK_THAT( vector_a, |
| 820 |
!UnorderedRangeEquals( vector_b, close_enough ) ); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
SECTION( "Ranges that need ADL begin/end" ) { |
| 826 |
unrelated::needs_ADL_begin<int> const |
| 827 |
needs_adl1{ 1, 2, 3, 4, 5 }, |
| 828 |
needs_adl2{ 1, 2, 3, 4, 5 }; |
| 829 |
|
| 830 |
REQUIRE_THAT( needs_adl1, UnorderedRangeEquals( needs_adl2 ) ); |
| 831 |
} |
| 832 |
|
| 833 |
SECTION( "Compare against std::initializer_list" ) { |
| 834 |
const std::array<int, 3> array_a{ { 1, 10, 20 } }; |
| 835 |
|
| 836 |
REQUIRE_THAT( array_a, UnorderedRangeEquals( { 10, 20, 1 } ) ); |
| 837 |
REQUIRE_THAT( array_a, |
| 838 |
UnorderedRangeEquals( { 11, 21, 2 }, []( int l, int r ) { |
| 839 |
return std::abs( l - r ) <= 1; |
| 840 |
} ) ); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* Return true if the type given has a random access iterator type. |
| 846 |
*/ |
| 847 |
template <typename Container> |
| 848 |
static constexpr bool ContainerIsRandomAccess( const Container& ) { |
| 849 |
using array_iter_category = typename std::iterator_traits< |
| 850 |
typename Container::iterator>::iterator_category; |
| 851 |
|
| 852 |
return std::is_base_of<std::random_access_iterator_tag, |
| 853 |
array_iter_category>::value; |
| 854 |
} |
| 855 |
|
| 856 |
TEST_CASE( "Type conversions of RangeEquals and similar", |
| 857 |
"[matchers][templated][quantifiers]" ) { |
| 858 |
using Catch::Matchers::RangeEquals; |
| 859 |
using Catch::Matchers::UnorderedRangeEquals; |
| 860 |
|
| 861 |
// In these test, we can always test RangeEquals and |
| 862 |
// UnorderedRangeEquals in the same way, since we're mostly |
| 863 |
// testing the template type deductions (and RangeEquals |
| 864 |
// implies UnorderedRangeEquals) |
| 865 |
|
| 866 |
SECTION( "Container conversions" ) { |
| 867 |
SECTION( "Two equal containers of different container types" ) { |
| 868 |
const std::array<int, 3> array_int_a{ { 1, 2, 3 } }; |
| 869 |
const int c_array[3] = { 1, 2, 3 }; |
| 870 |
CHECK_THAT( array_int_a, RangeEquals( c_array ) ); |
| 871 |
CHECK_THAT( array_int_a, UnorderedRangeEquals( c_array ) ); |
| 872 |
} |
| 873 |
SECTION( "Two equal containers of different container types " |
| 874 |
"(differ in array N)" ) { |
| 875 |
const std::array<int, 3> array_int_3{ { 1, 2, 3 } }; |
| 876 |
const std::array<int, 4> array_int_4{ { 1, 2, 3, 4 } }; |
| 877 |
CHECK_THAT( array_int_3, !RangeEquals( array_int_4 ) ); |
| 878 |
CHECK_THAT( array_int_3, !UnorderedRangeEquals( array_int_4 ) ); |
| 879 |
} |
| 880 |
SECTION( "Two equal containers of different container types and value " |
| 881 |
"types" ) { |
| 882 |
const std::array<int, 3> array_int_a{ { 1, 2, 3 } }; |
| 883 |
const std::vector<int> vector_char_a{ 1, 2, 3 }; |
| 884 |
CHECK_THAT( array_int_a, RangeEquals( vector_char_a ) ); |
| 885 |
CHECK_THAT( array_int_a, UnorderedRangeEquals( vector_char_a ) ); |
| 886 |
} |
| 887 |
SECTION( "Two equal containers, one random access, one not" ) { |
| 888 |
const std::array<int, 3> array_int_a{ { 1, 2, 3 } }; |
| 889 |
const std::list<int> list_char_a{ 1, 2, 3 }; |
| 890 |
|
| 891 |
// Verify these types really are different in random access nature |
| 892 |
STATIC_REQUIRE( ContainerIsRandomAccess( array_int_a ) != |
| 893 |
ContainerIsRandomAccess( list_char_a ) ); |
| 894 |
|
| 895 |
CHECK_THAT( array_int_a, RangeEquals( list_char_a ) ); |
| 896 |
CHECK_THAT( array_int_a, UnorderedRangeEquals( list_char_a ) ); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
SECTION( "Value type" ) { |
| 901 |
SECTION( "Two equal containers of different value types" ) { |
| 902 |
const std::vector<int> vector_int_a{ 1, 2, 3 }; |
| 903 |
const std::vector<char> vector_char_a{ 1, 2, 3 }; |
| 904 |
CHECK_THAT( vector_int_a, RangeEquals( vector_char_a ) ); |
| 905 |
CHECK_THAT( vector_int_a, UnorderedRangeEquals( vector_char_a ) ); |
| 906 |
} |
| 907 |
SECTION( "Two non-equal containers of different value types" ) { |
| 908 |
const std::vector<int> vector_int_a{ 1, 2, 3 }; |
| 909 |
const std::vector<char> vector_char_b{ 1, 2, 2 }; |
| 910 |
CHECK_THAT( vector_int_a, !RangeEquals( vector_char_b ) ); |
| 911 |
CHECK_THAT( vector_int_a, !UnorderedRangeEquals( vector_char_b ) ); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
SECTION( "Ranges with begin that needs ADL" ) { |
| 916 |
unrelated::needs_ADL_begin<int> a{ 1, 2, 3 }, b{ 3, 2, 1 }; |
| 917 |
REQUIRE_THAT( a, !RangeEquals( b ) ); |
| 918 |
REQUIRE_THAT( a, UnorderedRangeEquals( b ) ); |
| 919 |
} |
| 920 |
|
| 921 |
SECTION( "Custom predicate" ) { |
| 922 |
|
| 923 |
auto close_enough = []( int lhs, int rhs ) { |
| 924 |
return std::abs( lhs - rhs ) <= 1; |
| 925 |
}; |
| 926 |
|
| 927 |
SECTION( "Two equal non-empty containers (close enough)" ) { |
| 928 |
const std::vector<int> vector_a{ { 1, 2, 3 } }; |
| 929 |
const std::array<char, 3> array_a_plus_1{ { 2, 3, 4 } }; |
| 930 |
CHECK_THAT( vector_a, |
| 931 |
RangeEquals( array_a_plus_1, close_enough ) ); |
| 932 |
CHECK_THAT( vector_a, |
| 933 |
UnorderedRangeEquals( array_a_plus_1, close_enough ) ); |
| 934 |
} |
| 935 |
} |
| 936 |
} |