diff --git a/include/internal/catch_matchers_vector.h b/include/internal/catch_matchers_vector.h index 2dc8a715..084130f5 100644 --- a/include/internal/catch_matchers_vector.h +++ b/include/internal/catch_matchers_vector.h @@ -17,12 +17,12 @@ namespace Catch { namespace Matchers { namespace Vector { - template - struct ContainsElementMatcher : MatcherBase> { + template + struct ContainsElementMatcher : MatcherBase> { ContainsElementMatcher(T const &comparator) : m_comparator( comparator) {} - bool match(std::vector const &v) const override { + bool match(std::vector const &v) const override { for (auto const& el : v) { if (el == m_comparator) { return true; @@ -38,12 +38,12 @@ namespace Matchers { T const& m_comparator; }; - template - struct ContainsMatcher : MatcherBase> { + template + struct ContainsMatcher : MatcherBase> { - ContainsMatcher(std::vector const &comparator) : m_comparator( comparator ) {} + ContainsMatcher(std::vector const &comparator) : m_comparator( comparator ) {} - bool match(std::vector const &v) const override { + bool match(std::vector const &v) const override { // !TBD: see note in EqualsMatcher if (m_comparator.size() > v.size()) return false; @@ -65,18 +65,18 @@ namespace Matchers { return "Contains: " + ::Catch::Detail::stringify( m_comparator ); } - std::vector const& m_comparator; + std::vector const& m_comparator; }; - template - struct EqualsMatcher : MatcherBase> { + template + struct EqualsMatcher : MatcherBase> { - EqualsMatcher(std::vector const &comparator) : m_comparator( comparator ) {} + EqualsMatcher(std::vector const &comparator) : m_comparator( comparator ) {} - bool match(std::vector const &v) const override { + bool match(std::vector const &v) const override { // !TBD: This currently works if all elements can be compared using != // - a more general approach would be via a compare template that defaults - // to using !=. but could be specialised for, e.g. std::vector etc + // to using !=. but could be specialised for, e.g. std::vector etc // - then just call that directly if (m_comparator.size() != v.size()) return false; @@ -88,15 +88,15 @@ namespace Matchers { std::string describe() const override { return "Equals: " + ::Catch::Detail::stringify( m_comparator ); } - std::vector const& m_comparator; + std::vector const& m_comparator; }; - template - struct ApproxMatcher : MatcherBase> { + template + struct ApproxMatcher : MatcherBase> { - ApproxMatcher(std::vector const& comparator) : m_comparator( comparator ) {} + ApproxMatcher(std::vector const& comparator) : m_comparator( comparator ) {} - bool match(std::vector const &v) const override { + bool match(std::vector const &v) const override { if (m_comparator.size() != v.size()) return false; for (std::size_t i = 0; i < v.size(); ++i) @@ -123,14 +123,14 @@ namespace Matchers { return *this; } - std::vector const& m_comparator; + std::vector const& m_comparator; mutable Catch::Detail::Approx approx = Catch::Detail::Approx::custom(); }; - template - struct UnorderedEqualsMatcher : MatcherBase> { - UnorderedEqualsMatcher(std::vector const& target) : m_target(target) {} - bool match(std::vector const& vec) const override { + template + struct UnorderedEqualsMatcher : MatcherBase> { + UnorderedEqualsMatcher(std::vector const& target) : m_target(target) {} + bool match(std::vector const& vec) const override { // Note: This is a reimplementation of std::is_permutation, // because I don't want to include inside the common path if (m_target.size() != vec.size()) { @@ -143,7 +143,7 @@ namespace Matchers { return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target); } private: - std::vector const& m_target; + std::vector const& m_target; }; } // namespace Vector @@ -151,29 +151,29 @@ namespace Matchers { // The following functions create the actual matcher objects. // This allows the types to be inferred - template - Vector::ContainsMatcher Contains( std::vector const& comparator ) { - return Vector::ContainsMatcher( comparator ); + template + Vector::ContainsMatcher Contains( std::vector const& comparator ) { + return Vector::ContainsMatcher( comparator ); } - template - Vector::ContainsElementMatcher VectorContains( T const& comparator ) { - return Vector::ContainsElementMatcher( comparator ); + template> + Vector::ContainsElementMatcher VectorContains( T const& comparator ) { + return Vector::ContainsElementMatcher( comparator ); } - template - Vector::EqualsMatcher Equals( std::vector const& comparator ) { - return Vector::EqualsMatcher( comparator ); + template + Vector::EqualsMatcher Equals( std::vector const& comparator ) { + return Vector::EqualsMatcher( comparator ); } - template - Vector::ApproxMatcher Approx( std::vector const& comparator ) { - return Vector::ApproxMatcher( comparator ); + template + Vector::ApproxMatcher Approx( std::vector const& comparator ) { + return Vector::ApproxMatcher( comparator ); } - template - Vector::UnorderedEqualsMatcher UnorderedEquals(std::vector const& target) { - return Vector::UnorderedEqualsMatcher(target); + template + Vector::UnorderedEqualsMatcher UnorderedEquals(std::vector const& target) { + return Vector::UnorderedEqualsMatcher( target ); } } // namespace Matchers diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 10fcad66..280f697a 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -1531,18 +1531,26 @@ Matchers.tests.cpp:: failed: empty, Approx(t1) for: { } is approx: Matchers.tests.cpp:: failed: v1, Approx(v2) for: { 2.0, 4.0, 6.0 } is approx: { 1.0, 3.0, 5.0 } Matchers.tests.cpp:: passed: v, VectorContains(1) for: { 1, 2, 3 } Contains: 1 Matchers.tests.cpp:: passed: v, VectorContains(2) for: { 1, 2, 3 } Contains: 2 +Matchers.tests.cpp:: passed: v5, (VectorContains>(2)) for: { 1, 2, 3 } Contains: 2 Matchers.tests.cpp:: passed: v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2 } +Matchers.tests.cpp:: passed: v5, (Contains, CustomAllocator>(v2)) for: { 1, 2, 3 } Contains: { 1, 2 } Matchers.tests.cpp:: passed: v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2, 3 } Matchers.tests.cpp:: passed: v, Contains(empty) for: { 1, 2, 3 } Contains: { } Matchers.tests.cpp:: passed: empty, Contains(empty) for: { } Contains: { } +Matchers.tests.cpp:: passed: v5, (Contains, CustomAllocator>(v2)) for: { 1, 2, 3 } Contains: { 1, 2, 3 } +Matchers.tests.cpp:: passed: v5, Contains(v6) for: { 1, 2, 3 } Contains: { 1, 2 } Matchers.tests.cpp:: passed: v, VectorContains(1) && VectorContains(2) for: { 1, 2, 3 } ( Contains: 1 and Contains: 2 ) Matchers.tests.cpp:: passed: v, Equals(v) for: { 1, 2, 3 } Equals: { 1, 2, 3 } Matchers.tests.cpp:: passed: empty, Equals(empty) for: { } Equals: { } Matchers.tests.cpp:: passed: v, Equals(v2) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +Matchers.tests.cpp:: passed: v5, (Equals, CustomAllocator>(v2)) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +Matchers.tests.cpp:: passed: v5, Equals(v6) for: { 1, 2, 3 } Equals: { 1, 2, 3 } Matchers.tests.cpp:: passed: v, UnorderedEquals(v) for: { 1, 2, 3 } UnorderedEquals: { 1, 2, 3 } Matchers.tests.cpp:: passed: empty, UnorderedEquals(empty) for: { } UnorderedEquals: { } Matchers.tests.cpp:: passed: permuted, UnorderedEquals(v) for: { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } Matchers.tests.cpp:: passed: permuted, UnorderedEquals(v) for: { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } +Matchers.tests.cpp:: passed: v5, (UnorderedEquals, CustomAllocator>(permuted)) for: { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } +Matchers.tests.cpp:: passed: v5_permuted, UnorderedEquals(v5) for: { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } Matchers.tests.cpp:: failed: v, VectorContains(-1) for: { 1, 2, 3 } Contains: -1 Matchers.tests.cpp:: failed: empty, VectorContains(1) for: { } Contains: 1 Matchers.tests.cpp:: failed: empty, Contains(v) for: { } Contains: { 1, 2, 3 } diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index d3a733d2..6b29a98c 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== test cases: 308 | 234 passed | 70 failed | 4 failed as expected -assertions: 1680 | 1528 passed | 131 failed | 21 failed as expected +assertions: 1688 | 1536 passed | 131 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 9a5b7eb3..fcb7a802 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -11204,6 +11204,11 @@ Matchers.tests.cpp:: PASSED: with expansion: { 1, 2, 3 } Contains: 2 +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, (VectorContains>(2)) ) +with expansion: + { 1, 2, 3 } Contains: 2 + ------------------------------------------------------------------------------- Vector matchers Contains (vector) @@ -11216,6 +11221,11 @@ Matchers.tests.cpp:: PASSED: with expansion: { 1, 2, 3 } Contains: { 1, 2 } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, (Contains, CustomAllocator>(v2)) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2 } + Matchers.tests.cpp:: PASSED: CHECK_THAT( v, Contains(v2) ) with expansion: @@ -11231,6 +11241,16 @@ Matchers.tests.cpp:: PASSED: with expansion: { } Contains: { } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, (Contains, CustomAllocator>(v2)) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2, 3 } + +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, Contains(v6) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2 } + ------------------------------------------------------------------------------- Vector matchers Contains (element), composed @@ -11265,6 +11285,16 @@ Matchers.tests.cpp:: PASSED: with expansion: { 1, 2, 3 } Equals: { 1, 2, 3 } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, (Equals, CustomAllocator>(v2)) ) +with expansion: + { 1, 2, 3 } Equals: { 1, 2, 3 } + +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, Equals(v6) ) +with expansion: + { 1, 2, 3 } Equals: { 1, 2, 3 } + ------------------------------------------------------------------------------- Vector matchers UnorderedEquals @@ -11292,6 +11322,16 @@ Matchers.tests.cpp:: PASSED: with expansion: { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5, (UnorderedEquals, CustomAllocator>(permuted)) ) +with expansion: + { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } + +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v5_permuted, UnorderedEquals(v5) ) +with expansion: + { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } + ------------------------------------------------------------------------------- Vector matchers that fail Contains (element) @@ -13449,5 +13489,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== test cases: 308 | 218 passed | 86 failed | 4 failed as expected -assertions: 1697 | 1528 passed | 148 failed | 21 failed as expected +assertions: 1705 | 1536 passed | 148 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 8ac1236d..76284a2f 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index fff44abd..807a946e 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -13657,7 +13657,15 @@ There is no extra whitespace here { 1, 2, 3 } Contains: 2 - + + + v5, (VectorContains<int, CustomAllocator<int>>(2)) + + + { 1, 2, 3 } Contains: 2 + + +
@@ -13668,6 +13676,14 @@ There is no extra whitespace here { 1, 2, 3 } Contains: { 1, 2 } + + + v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) + + + { 1, 2, 3 } Contains: { 1, 2 } + + v, Contains(v2) @@ -13692,7 +13708,23 @@ There is no extra whitespace here { } Contains: { } - + + + v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) + + + { 1, 2, 3 } Contains: { 1, 2, 3 } + + + + + v5, Contains(v6) + + + { 1, 2, 3 } Contains: { 1, 2 } + + +
@@ -13730,7 +13762,23 @@ There is no extra whitespace here { 1, 2, 3 } Equals: { 1, 2, 3 } - + + + v5, (Equals<int, std::allocator<int>, CustomAllocator<int>>(v2)) + + + { 1, 2, 3 } Equals: { 1, 2, 3 } + + + + + v5, Equals(v6) + + + { 1, 2, 3 } Equals: { 1, 2, 3 } + + +
@@ -13765,7 +13813,23 @@ There is no extra whitespace here { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } - + + + v5, (UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>(permuted)) + + + { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } + + + + + v5_permuted, UnorderedEquals(v5) + + + { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } + + +
@@ -16075,7 +16139,7 @@ loose text artifact - + - + diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp index 52f2912d..e7a1d542 100644 --- a/projects/SelfTest/UsageTests/Matchers.tests.cpp +++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp @@ -214,6 +214,42 @@ namespace { namespace MatchersTests { CHECK_THAT(testStringForMatching(), !Contains("substring")); } + template + struct CustomAllocator : private std::allocator + { + using size_type = size_t; + using difference_type = ptrdiff_t; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using value_type = T; + + template + struct rebind + { using other = CustomAllocator; }; + + using propagate_on_container_move_assignment = std::true_type; + using is_always_equal = std::true_type; + + CustomAllocator() = default; + + CustomAllocator(const CustomAllocator& other) + : std::allocator(other) { } + + template + CustomAllocator(const CustomAllocator&) { } + + ~CustomAllocator() = default; + + using std::allocator::address; + using std::allocator::allocate; + using std::allocator::construct; + using std::allocator::deallocate; + using std::allocator::max_size; + using std::allocator::destroy; + }; + TEST_CASE("Vector matchers", "[matchers][vector]") { std::vector v; v.push_back(1); @@ -234,19 +270,34 @@ namespace { namespace MatchersTests { v4.push_back(2 + 1e-8); v4.push_back(3 + 1e-8); + std::vector> v5; + v5.push_back(1); + v5.push_back(2); + v5.push_back(3); + + std::vector> v6; + v6.push_back(1); + v6.push_back(2); + std::vector empty; SECTION("Contains (element)") { CHECK_THAT(v, VectorContains(1)); CHECK_THAT(v, VectorContains(2)); + CHECK_THAT(v5, (VectorContains>(2))); } SECTION("Contains (vector)") { CHECK_THAT(v, Contains(v2)); + CHECK_THAT(v5, (Contains, CustomAllocator>(v2))); + v2.push_back(3); // now exactly matches CHECK_THAT(v, Contains(v2)); CHECK_THAT(v, Contains(empty)); CHECK_THAT(empty, Contains(empty)); + + CHECK_THAT(v5, (Contains, CustomAllocator>(v2))); + CHECK_THAT(v5, Contains(v6)); } SECTION("Contains (element), composed") { CHECK_THAT(v, VectorContains(1) && VectorContains(2)); @@ -262,6 +313,11 @@ namespace { namespace MatchersTests { // Different vector with same elements v2.push_back(3); CHECK_THAT(v, Equals(v2)); + + CHECK_THAT(v5, (Equals, CustomAllocator>(v2))); + + v6.push_back(3); + CHECK_THAT(v5, Equals(v6)); } SECTION("UnorderedEquals") { CHECK_THAT(v, UnorderedEquals(v)); @@ -273,6 +329,12 @@ namespace { namespace MatchersTests { std::reverse(begin(permuted), end(permuted)); REQUIRE_THAT(permuted, UnorderedEquals(v)); + + CHECK_THAT(v5, (UnorderedEquals, CustomAllocator>(permuted))); + + auto v5_permuted = v5; + std::next_permutation(begin(v5_permuted), end(v5_permuted)); + CHECK_THAT(v5_permuted, UnorderedEquals(v5)); } }