diff --git a/src/catch2/matchers/catch_matchers_vector.hpp b/src/catch2/matchers/catch_matchers_vector.hpp index 4f3918cb..71634813 100644 --- a/src/catch2/matchers/catch_matchers_vector.hpp +++ b/src/catch2/matchers/catch_matchers_vector.hpp @@ -16,14 +16,14 @@ namespace Catch { namespace Matchers { - template - struct ContainsElementMatcher final : MatcherBase> { + template + struct ContainsElementMatcher final : 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; @@ -39,14 +39,14 @@ namespace Matchers { T const& m_comparator; }; - template - struct ContainsMatcher final : MatcherBase> { + template + struct ContainsMatcher final : 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; @@ -68,15 +68,17 @@ namespace Matchers { return "Contains: " + ::Catch::Detail::stringify( m_comparator ); } - std::vector const& m_comparator; + std::vector const& m_comparator; }; - template - struct EqualsMatcher final : MatcherBase> { + template + struct EqualsMatcher final : 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 @@ -91,15 +93,17 @@ 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 final : MatcherBase> { + template + struct ApproxMatcher final : 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) @@ -126,14 +130,16 @@ namespace Matchers { return *this; } - std::vector const& m_comparator; + std::vector const& m_comparator; mutable Catch::Approx approx = Catch::Approx::custom(); }; - template - struct UnorderedEqualsMatcher final : MatcherBase> { - UnorderedEqualsMatcher(std::vector const& target) : m_target(target) {} - bool match(std::vector const& vec) const override { + template + struct UnorderedEqualsMatcher final : 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()) { @@ -146,7 +152,7 @@ namespace Matchers { return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target); } private: - std::vector const& m_target; + std::vector const& m_target; }; @@ -155,33 +161,33 @@ namespace Matchers { //! Creates a matcher that matches vectors that contain all elements in `comparator` - template - ContainsMatcher Contains( std::vector const& comparator ) { - return ContainsMatcher( comparator ); + template, typename AllocMatch = AllocComp> + ContainsMatcher Contains( std::vector const& comparator ) { + return ContainsMatcher(comparator); } //! Creates a matcher that matches vectors that contain `comparator` as an element - template - ContainsElementMatcher VectorContains( T const& comparator ) { - return ContainsElementMatcher( comparator ); + template> + ContainsElementMatcher VectorContains( T const& comparator ) { + return ContainsElementMatcher(comparator); } //! Creates a matcher that matches vectors that are exactly equal to `comparator` - template - EqualsMatcher Equals( std::vector const& comparator ) { - return EqualsMatcher( comparator ); + template, typename AllocMatch = AllocComp> + EqualsMatcher Equals( std::vector const& comparator ) { + return EqualsMatcher(comparator); } //! Creates a matcher that matches vectors that `comparator` as an element - template - ApproxMatcher Approx( std::vector const& comparator ) { - return ApproxMatcher( comparator ); + template, typename AllocMatch = AllocComp> + ApproxMatcher Approx( std::vector const& comparator ) { + return ApproxMatcher(comparator); } //! Creates a matcher that matches vectors that is equal to `target` modulo permutation - template - UnorderedEqualsMatcher UnorderedEquals(std::vector const& target) { - return UnorderedEqualsMatcher(target); + template, typename AllocMatch = AllocComp> + UnorderedEqualsMatcher UnorderedEquals(std::vector const& target) { + return UnorderedEqualsMatcher(target); } } // namespace Matchers diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index 2a11bf78..7c126fdf 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1603,6 +1603,7 @@ Approx.tests.cpp:: passed: approx( d ) != 1.25 for: Approx( 1.23 ) VariadicMacros.tests.cpp:: passed: with 1 message: 'no assertions' Matchers.tests.cpp:: passed: empty, Approx(empty) for: { } is approx: { } Matchers.tests.cpp:: passed: v1, Approx(v1) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } +Matchers.tests.cpp:: passed: v1, Approx({ 1., 2., 3. }) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } Matchers.tests.cpp:: passed: v1, !Approx(temp) for: { 1.0, 2.0, 3.0 } not is approx: { 1.0, 2.0, 3.0, 4.0 } Matchers.tests.cpp:: passed: v1, !Approx(v2) for: { 1.0, 2.0, 3.0 } not is approx: { 1.5, 2.5, 3.5 } Matchers.tests.cpp:: passed: v1, Approx(v2).margin(0.5) for: { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } @@ -1612,18 +1613,29 @@ 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: v, Contains({ 1, 2 }) 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({ 1, 2, 3 }) for: { 1, 2, 3 } Equals: { 1, 2, 3 } 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: v, UnorderedEquals({ 3, 2, 1 }) for: { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } 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/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index a0823854..1780de5a 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== test cases: 334 | 260 passed | 70 failed | 4 failed as expected -assertions: 1880 | 1728 passed | 131 failed | 21 failed as expected +assertions: 1892 | 1740 passed | 131 failed | 21 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 0c022ebb..dd96bbdd 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -11676,6 +11676,11 @@ Matchers.tests.cpp:: PASSED: with expansion: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, Approx({ 1., 2., 3. }) ) +with expansion: + { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } + ------------------------------------------------------------------------------- Vector Approx matcher Vectors with elements @@ -11758,6 +11763,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) @@ -11770,6 +11780,16 @@ Matchers.tests.cpp:: PASSED: with expansion: { 1, 2, 3 } Contains: { 1, 2 } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v, Contains({ 1, 2 }) ) +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: @@ -11785,6 +11805,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 @@ -11814,11 +11844,26 @@ Matchers.tests.cpp:: PASSED: with expansion: { } Equals: { } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v, Equals({ 1, 2, 3 }) ) +with expansion: + { 1, 2, 3 } Equals: { 1, 2, 3 } + Matchers.tests.cpp:: PASSED: CHECK_THAT( v, Equals(v2) ) 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 @@ -11831,6 +11876,11 @@ Matchers.tests.cpp:: PASSED: with expansion: { 1, 2, 3 } UnorderedEquals: { 1, 2, 3 } +Matchers.tests.cpp:: PASSED: + CHECK_THAT( v, UnorderedEquals({ 3, 2, 1 }) ) +with expansion: + { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } + Matchers.tests.cpp:: PASSED: CHECK_THAT( empty, UnorderedEquals(empty) ) with expansion: @@ -11846,6 +11896,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) @@ -14714,5 +14774,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== test cases: 334 | 244 passed | 86 failed | 4 failed as expected -assertions: 1897 | 1728 passed | 148 failed | 21 failed as expected +assertions: 1909 | 1740 passed | 148 failed | 21 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index ca65203f..264f7d95 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index a97aeb77..6e45900c 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -3054,6 +3054,8 @@ ok {test-number} - empty, Approx(empty) for: { } is approx: { } # Vector Approx matcher ok {test-number} - v1, Approx(v1) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } # Vector Approx matcher +ok {test-number} - v1, Approx({ 1., 2., 3. }) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } +# Vector Approx matcher ok {test-number} - v1, !Approx(temp) for: { 1.0, 2.0, 3.0 } not is approx: { 1.0, 2.0, 3.0, 4.0 } # Vector Approx matcher ok {test-number} - v1, !Approx(v2) for: { 1.0, 2.0, 3.0 } not is approx: { 1.5, 2.5, 3.5 } @@ -3072,29 +3074,51 @@ ok {test-number} - v, VectorContains(1) for: { 1, 2, 3 } Contains: 1 # Vector matchers ok {test-number} - v, VectorContains(2) for: { 1, 2, 3 } Contains: 2 # Vector matchers +ok {test-number} - v5, (VectorContains>(2)) for: { 1, 2, 3 } Contains: 2 +# Vector matchers ok {test-number} - v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2 } # Vector matchers +ok {test-number} - v, Contains({ 1, 2 }) for: { 1, 2, 3 } Contains: { 1, 2 } +# Vector matchers +ok {test-number} - v5, (Contains, CustomAllocator>(v2)) for: { 1, 2, 3 } Contains: { 1, 2 } +# Vector matchers ok {test-number} - v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2, 3 } # Vector matchers ok {test-number} - v, Contains(empty) for: { 1, 2, 3 } Contains: { } # Vector matchers ok {test-number} - empty, Contains(empty) for: { } Contains: { } # Vector matchers +ok {test-number} - v5, (Contains, CustomAllocator>(v2)) for: { 1, 2, 3 } Contains: { 1, 2, 3 } +# Vector matchers +ok {test-number} - v5, Contains(v6) for: { 1, 2, 3 } Contains: { 1, 2 } +# Vector matchers ok {test-number} - v, VectorContains(1) && VectorContains(2) for: { 1, 2, 3 } ( Contains: 1 and Contains: 2 ) # Vector matchers ok {test-number} - v, Equals(v) for: { 1, 2, 3 } Equals: { 1, 2, 3 } # Vector matchers ok {test-number} - empty, Equals(empty) for: { } Equals: { } # Vector matchers +ok {test-number} - v, Equals({ 1, 2, 3 }) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +# Vector matchers ok {test-number} - v, Equals(v2) for: { 1, 2, 3 } Equals: { 1, 2, 3 } # Vector matchers +ok {test-number} - v5, (Equals, CustomAllocator>(v2)) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +# Vector matchers +ok {test-number} - v5, Equals(v6) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +# Vector matchers ok {test-number} - v, UnorderedEquals(v) for: { 1, 2, 3 } UnorderedEquals: { 1, 2, 3 } # Vector matchers +ok {test-number} - v, UnorderedEquals({ 3, 2, 1 }) for: { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } +# Vector matchers ok {test-number} - empty, UnorderedEquals(empty) for: { } UnorderedEquals: { } # Vector matchers ok {test-number} - permuted, UnorderedEquals(v) for: { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } # Vector matchers ok {test-number} - permuted, UnorderedEquals(v) for: { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } +# Vector matchers +ok {test-number} - v5, (UnorderedEquals, CustomAllocator>(permuted)) for: { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } +# Vector matchers +ok {test-number} - v5_permuted, UnorderedEquals(v5) for: { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } # Vector matchers that fail not ok {test-number} - v, VectorContains(-1) for: { 1, 2, 3 } Contains: -1 # Vector matchers that fail @@ -3786,5 +3810,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..1889 +1..1901 diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index fbba5620..2bc11f3b 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -14099,9 +14099,17 @@ There is no extra whitespace here { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } - + + + v1, Approx<double>({ 1., 2., 3. }) + + + { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } + + + - +
@@ -14200,7 +14208,15 @@ There is no extra whitespace here { 1, 2, 3 } Contains: 2 - + + + v5, (VectorContains<int, CustomAllocator<int>>(2)) + + + { 1, 2, 3 } Contains: 2 + + +
@@ -14211,6 +14227,22 @@ There is no extra whitespace here { 1, 2, 3 } Contains: { 1, 2 } + + + v, Contains<int>({ 1, 2 }) + + + { 1, 2, 3 } Contains: { 1, 2 } + + + + + v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) + + + { 1, 2, 3 } Contains: { 1, 2 } + + v, Contains(v2) @@ -14235,7 +14267,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 } + + +
@@ -14265,6 +14313,14 @@ There is no extra whitespace here { } Equals: { } + + + v, Equals<int>({ 1, 2, 3 }) + + + { 1, 2, 3 } Equals: { 1, 2, 3 } + + v, Equals(v2) @@ -14273,7 +14329,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 } + + +
@@ -14284,6 +14356,14 @@ There is no extra whitespace here { 1, 2, 3 } UnorderedEquals: { 1, 2, 3 } + + + v, UnorderedEquals<int>({ 3, 2, 1 }) + + + { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } + + empty, UnorderedEquals(empty) @@ -14308,7 +14388,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 } + + +
@@ -17636,7 +17732,7 @@ loose text artifact
- + - + diff --git a/tests/SelfTest/UsageTests/Matchers.tests.cpp b/tests/SelfTest/UsageTests/Matchers.tests.cpp index 2efc4397..4736b563 100644 --- a/tests/SelfTest/UsageTests/Matchers.tests.cpp +++ b/tests/SelfTest/UsageTests/Matchers.tests.cpp @@ -217,6 +217,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); @@ -237,19 +273,35 @@ 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(v, Contains({ 1, 2 })); + 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)); @@ -263,11 +315,18 @@ namespace { namespace MatchersTests { CHECK_THAT(empty, Equals(empty)); // Different vector with same elements + CHECK_THAT(v, Equals({ 1, 2, 3 })); 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)); + CHECK_THAT(v, UnorderedEquals({ 3, 2, 1 })); CHECK_THAT(empty, UnorderedEquals(empty)); auto permuted = v; @@ -276,6 +335,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)); } } @@ -521,6 +586,7 @@ namespace { namespace MatchersTests { std::vector v1({1., 2., 3.}); SECTION("A vector is approx equal to itself") { REQUIRE_THAT(v1, Approx(v1)); + REQUIRE_THAT(v1, Approx({ 1., 2., 3. })); } std::vector v2({1.5, 2.5, 3.5}); SECTION("Different length") {