diff options
| author | schallerr | 2020-04-16 15:36:54 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2020-05-03 07:40:04 +0200 |
| commit | 2a93a65bc24c21102e46c5e102f0905f97a10ee5 (patch) | |
| tree | a111cb59be0d4a3f9a3e9559dfde86623f11d04f | |
| parent | dd35430a2b03265343846735593fdc519cd64418 (diff) | |
| download | catch3-2a93a65bc24c21102e46c5e102f0905f97a10ee5.tar.gz | |
Support custom allocators in vector Matchers (#1909)
-- Combined with f4fc2dab2c5bb8cab11b0a0dde8a8421076c30f9
during cherry-picking.
| -rw-r--r-- | src/catch2/matchers/catch_matchers_vector.hpp | 84 | ||||
| -rw-r--r-- | tests/SelfTest/Baselines/compact.sw.approved.txt | 12 | ||||
| -rw-r--r-- | tests/SelfTest/Baselines/console.std.approved.txt | 2 | ||||
| -rw-r--r-- | tests/SelfTest/Baselines/console.sw.approved.txt | 62 | ||||
| -rw-r--r-- | tests/SelfTest/Baselines/junit.sw.approved.txt | 2 | ||||
| -rw-r--r-- | tests/SelfTest/Baselines/tap.sw.approved.txt | 26 | ||||
| -rw-r--r-- | tests/SelfTest/Baselines/xml.sw.approved.txt | 112 | ||||
| -rw-r--r-- | tests/SelfTest/UsageTests/Matchers.tests.cpp | 66 |
8 files changed, 315 insertions, 51 deletions
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<typename T> - struct ContainsElementMatcher final : MatcherBase<std::vector<T>> { + template<typename T, typename Alloc> + struct ContainsElementMatcher final : MatcherBase<std::vector<T, Alloc>> { ContainsElementMatcher(T const& comparator): m_comparator(comparator) {} - bool match(std::vector<T> const& v) const override { + bool match(std::vector<T, Alloc> 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<typename T> - struct ContainsMatcher final : MatcherBase<std::vector<T>> { + template<typename T, typename AllocComp, typename AllocMatch> + struct ContainsMatcher final : MatcherBase<std::vector<T, AllocMatch>> { - ContainsMatcher(std::vector<T> const& comparator): - m_comparator(comparator) + ContainsMatcher(std::vector<T, AllocComp> const& comparator): + m_comparator( comparator ) {} - bool match(std::vector<T> const& v) const override { + bool match(std::vector<T, AllocMatch> 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<T> const& m_comparator; + std::vector<T, AllocComp> const& m_comparator; }; - template<typename T> - struct EqualsMatcher final : MatcherBase<std::vector<T>> { + template<typename T, typename AllocComp, typename AllocMatch> + struct EqualsMatcher final : MatcherBase<std::vector<T, AllocMatch>> { - EqualsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} + EqualsMatcher(std::vector<T, AllocComp> const& comparator): + m_comparator( comparator ) + {} - bool match(std::vector<T> const &v) const override { + bool match(std::vector<T, AllocMatch> 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<T> etc @@ -91,15 +93,17 @@ namespace Matchers { std::string describe() const override { return "Equals: " + ::Catch::Detail::stringify( m_comparator ); } - std::vector<T> const& m_comparator; + std::vector<T, AllocComp> const& m_comparator; }; - template<typename T> - struct ApproxMatcher final : MatcherBase<std::vector<T>> { + template<typename T, typename AllocComp, typename AllocMatch> + struct ApproxMatcher final : MatcherBase<std::vector<T, AllocMatch>> { - ApproxMatcher(std::vector<T> const& comparator) : m_comparator( comparator ) {} + ApproxMatcher(std::vector<T, AllocComp> const& comparator): + m_comparator( comparator ) + {} - bool match(std::vector<T> const &v) const override { + bool match(std::vector<T, AllocMatch> 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<T> const& m_comparator; + std::vector<T, AllocComp> const& m_comparator; mutable Catch::Approx approx = Catch::Approx::custom(); }; - template<typename T> - struct UnorderedEqualsMatcher final : MatcherBase<std::vector<T>> { - UnorderedEqualsMatcher(std::vector<T> const& target) : m_target(target) {} - bool match(std::vector<T> const& vec) const override { + template<typename T, typename AllocComp, typename AllocMatch> + struct UnorderedEqualsMatcher final : MatcherBase<std::vector<T, AllocMatch>> { + UnorderedEqualsMatcher(std::vector<T, AllocComp> const& target): + m_target(target) + {} + bool match(std::vector<T, AllocMatch> const& vec) const override { // Note: This is a reimplementation of std::is_permutation, // because I don't want to include <algorithm> 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<T> const& m_target; + std::vector<T, AllocComp> const& m_target; }; @@ -155,33 +161,33 @@ namespace Matchers { //! Creates a matcher that matches vectors that contain all elements in `comparator` - template<typename T> - ContainsMatcher<T> Contains( std::vector<T> const& comparator ) { - return ContainsMatcher<T>( comparator ); + template<typename T, typename AllocComp = std::allocator<T>, typename AllocMatch = AllocComp> + ContainsMatcher<T, AllocComp, AllocMatch> Contains( std::vector<T, AllocComp> const& comparator ) { + return ContainsMatcher<T, AllocComp, AllocMatch>(comparator); } //! Creates a matcher that matches vectors that contain `comparator` as an element - template<typename T> - ContainsElementMatcher<T> VectorContains( T const& comparator ) { - return ContainsElementMatcher<T>( comparator ); + template<typename T, typename Alloc = std::allocator<T>> + ContainsElementMatcher<T, Alloc> VectorContains( T const& comparator ) { + return ContainsElementMatcher<T, Alloc>(comparator); } //! Creates a matcher that matches vectors that are exactly equal to `comparator` - template<typename T> - EqualsMatcher<T> Equals( std::vector<T> const& comparator ) { - return EqualsMatcher<T>( comparator ); + template<typename T, typename AllocComp = std::allocator<T>, typename AllocMatch = AllocComp> + EqualsMatcher<T, AllocComp, AllocMatch> Equals( std::vector<T, AllocComp> const& comparator ) { + return EqualsMatcher<T, AllocComp, AllocMatch>(comparator); } //! Creates a matcher that matches vectors that `comparator` as an element - template<typename T> - ApproxMatcher<T> Approx( std::vector<T> const& comparator ) { - return ApproxMatcher<T>( comparator ); + template<typename T, typename AllocComp = std::allocator<T>, typename AllocMatch = AllocComp> + ApproxMatcher<T, AllocComp, AllocMatch> Approx( std::vector<T, AllocComp> const& comparator ) { + return ApproxMatcher<T, AllocComp, AllocMatch>(comparator); } //! Creates a matcher that matches vectors that is equal to `target` modulo permutation - template<typename T> - UnorderedEqualsMatcher<T> UnorderedEquals(std::vector<T> const& target) { - return UnorderedEqualsMatcher<T>(target); + template<typename T, typename AllocComp = std::allocator<T>, typename AllocMatch = AllocComp> + UnorderedEqualsMatcher<T, AllocComp, AllocMatch> UnorderedEquals(std::vector<T, AllocComp> const& target) { + return UnorderedEqualsMatcher<T, AllocComp, AllocMatch>(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:<line number>: passed: approx( d ) != 1.25 for: Approx( 1.23 ) VariadicMacros.tests.cpp:<line number>: passed: with 1 message: 'no assertions' Matchers.tests.cpp:<line number>: passed: empty, Approx(empty) for: { } is approx: { } Matchers.tests.cpp:<line number>: passed: v1, Approx(v1) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } +Matchers.tests.cpp:<line number>: passed: v1, Approx<double>({ 1., 2., 3. }) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } Matchers.tests.cpp:<line number>: 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:<line number>: passed: v1, !Approx(v2) for: { 1.0, 2.0, 3.0 } not is approx: { 1.5, 2.5, 3.5 } Matchers.tests.cpp:<line number>: 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:<line number>: failed: empty, Approx(t1) for: { } is approx: Matchers.tests.cpp:<line number>: failed: v1, Approx(v2) for: { 2.0, 4.0, 6.0 } is approx: { 1.0, 3.0, 5.0 } Matchers.tests.cpp:<line number>: passed: v, VectorContains(1) for: { 1, 2, 3 } Contains: 1 Matchers.tests.cpp:<line number>: passed: v, VectorContains(2) for: { 1, 2, 3 } Contains: 2 +Matchers.tests.cpp:<line number>: passed: v5, (VectorContains<int, CustomAllocator<int>>(2)) for: { 1, 2, 3 } Contains: 2 Matchers.tests.cpp:<line number>: passed: v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2 } +Matchers.tests.cpp:<line number>: passed: v, Contains<int>({ 1, 2 }) for: { 1, 2, 3 } Contains: { 1, 2 } +Matchers.tests.cpp:<line number>: passed: v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) for: { 1, 2, 3 } Contains: { 1, 2 } Matchers.tests.cpp:<line number>: passed: v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2, 3 } Matchers.tests.cpp:<line number>: passed: v, Contains(empty) for: { 1, 2, 3 } Contains: { } Matchers.tests.cpp:<line number>: passed: empty, Contains(empty) for: { } Contains: { } +Matchers.tests.cpp:<line number>: passed: v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) for: { 1, 2, 3 } Contains: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: passed: v5, Contains(v6) for: { 1, 2, 3 } Contains: { 1, 2 } Matchers.tests.cpp:<line number>: passed: v, VectorContains(1) && VectorContains(2) for: { 1, 2, 3 } ( Contains: 1 and Contains: 2 ) Matchers.tests.cpp:<line number>: passed: v, Equals(v) for: { 1, 2, 3 } Equals: { 1, 2, 3 } Matchers.tests.cpp:<line number>: passed: empty, Equals(empty) for: { } Equals: { } +Matchers.tests.cpp:<line number>: passed: v, Equals<int>({ 1, 2, 3 }) for: { 1, 2, 3 } Equals: { 1, 2, 3 } Matchers.tests.cpp:<line number>: passed: v, Equals(v2) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: passed: v5, (Equals<int, std::allocator<int>, CustomAllocator<int>>(v2)) for: { 1, 2, 3 } Equals: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: passed: v5, Equals(v6) for: { 1, 2, 3 } Equals: { 1, 2, 3 } Matchers.tests.cpp:<line number>: passed: v, UnorderedEquals(v) for: { 1, 2, 3 } UnorderedEquals: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: passed: v, UnorderedEquals<int>({ 3, 2, 1 }) for: { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } Matchers.tests.cpp:<line number>: passed: empty, UnorderedEquals(empty) for: { } UnorderedEquals: { } Matchers.tests.cpp:<line number>: passed: permuted, UnorderedEquals(v) for: { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } Matchers.tests.cpp:<line number>: passed: permuted, UnorderedEquals(v) for: { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: passed: v5, (UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>(permuted)) for: { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } +Matchers.tests.cpp:<line number>: passed: v5_permuted, UnorderedEquals(v5) for: { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } Matchers.tests.cpp:<line number>: failed: v, VectorContains(-1) for: { 1, 2, 3 } Contains: -1 Matchers.tests.cpp:<line number>: failed: empty, VectorContains(1) for: { } Contains: 1 Matchers.tests.cpp:<line number>: 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:<line number>: PASSED: with expansion: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } +Matchers.tests.cpp:<line number>: PASSED: + REQUIRE_THAT( v1, Approx<double>({ 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:<line number>: PASSED: with expansion: { 1, 2, 3 } Contains: 2 +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, (VectorContains<int, CustomAllocator<int>>(2)) ) +with expansion: + { 1, 2, 3 } Contains: 2 + ------------------------------------------------------------------------------- Vector matchers Contains (vector) @@ -11771,6 +11781,16 @@ with expansion: { 1, 2, 3 } Contains: { 1, 2 } Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v, Contains<int>({ 1, 2 }) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2 } + +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2 } + +Matchers.tests.cpp:<line number>: PASSED: CHECK_THAT( v, Contains(v2) ) with expansion: { 1, 2, 3 } Contains: { 1, 2, 3 } @@ -11785,6 +11805,16 @@ Matchers.tests.cpp:<line number>: PASSED: with expansion: { } Contains: { } +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2, 3 } + +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, Contains(v6) ) +with expansion: + { 1, 2, 3 } Contains: { 1, 2 } + ------------------------------------------------------------------------------- Vector matchers Contains (element), composed @@ -11815,10 +11845,25 @@ with expansion: { } Equals: { } Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v, Equals<int>({ 1, 2, 3 }) ) +with expansion: + { 1, 2, 3 } Equals: { 1, 2, 3 } + +Matchers.tests.cpp:<line number>: PASSED: CHECK_THAT( v, Equals(v2) ) with expansion: { 1, 2, 3 } Equals: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, (Equals<int, std::allocator<int>, CustomAllocator<int>>(v2)) ) +with expansion: + { 1, 2, 3 } Equals: { 1, 2, 3 } + +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, Equals(v6) ) +with expansion: + { 1, 2, 3 } Equals: { 1, 2, 3 } + ------------------------------------------------------------------------------- Vector matchers UnorderedEquals @@ -11832,6 +11877,11 @@ with expansion: { 1, 2, 3 } UnorderedEquals: { 1, 2, 3 } Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v, UnorderedEquals<int>({ 3, 2, 1 }) ) +with expansion: + { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } + +Matchers.tests.cpp:<line number>: PASSED: CHECK_THAT( empty, UnorderedEquals(empty) ) with expansion: { } UnorderedEquals: { } @@ -11846,6 +11896,16 @@ Matchers.tests.cpp:<line number>: PASSED: with expansion: { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } +Matchers.tests.cpp:<line number>: PASSED: + CHECK_THAT( v5, (UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>(permuted)) ) +with expansion: + { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } + +Matchers.tests.cpp:<line number>: 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:<line number>: 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 @@ <?xml version="1.0" encoding="UTF-8"?> <testsuitesloose text artifact > - <testsuite name="<exe-name>" errors="17" failures="132" tests="1898" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> + <testsuite name="<exe-name>" errors="17" failures="132" tests="1910" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <properties> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/> <property name="random-seed" value="1"/> 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<double>({ 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<int, CustomAllocator<int>>(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<int>({ 1, 2 }) for: { 1, 2, 3 } Contains: { 1, 2 } +# Vector matchers +ok {test-number} - v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(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<int, std::allocator<int>, CustomAllocator<int>>(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<int>({ 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<int, std::allocator<int>, CustomAllocator<int>>(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<int>({ 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<int, std::allocator<int>, CustomAllocator<int>>(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 } </Expanded> </Expression> - <OverallResults successes="1" failures="0" expectedFailures="0"/> + <Expression success="true" type="REQUIRE_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v1, Approx<double>({ 1., 2., 3. }) + </Original> + <Expanded> + { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } + </Expanded> + </Expression> + <OverallResults successes="2" failures="0" expectedFailures="0"/> </Section> - <OverallResults successes="1" failures="0" expectedFailures="0"/> + <OverallResults successes="2" failures="0" expectedFailures="0"/> </Section> <Section name="Vectors with elements" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Section name="Different length" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > @@ -14200,7 +14208,15 @@ There is no extra whitespace here { 1, 2, 3 } Contains: 2 </Expanded> </Expression> - <OverallResults successes="2" failures="0" expectedFailures="0"/> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, (VectorContains<int, CustomAllocator<int>>(2)) + </Original> + <Expanded> + { 1, 2, 3 } Contains: 2 + </Expanded> + </Expression> + <OverallResults successes="3" failures="0" expectedFailures="0"/> </Section> <Section name="Contains (vector)" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > @@ -14213,6 +14229,22 @@ There is no extra whitespace here </Expression> <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Original> + v, Contains<int>({ 1, 2 }) + </Original> + <Expanded> + { 1, 2, 3 } Contains: { 1, 2 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) + </Original> + <Expanded> + { 1, 2, 3 } Contains: { 1, 2 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> v, Contains(v2) </Original> <Expanded> @@ -14235,7 +14267,23 @@ There is no extra whitespace here { } Contains: { } </Expanded> </Expression> - <OverallResults successes="4" failures="0" expectedFailures="0"/> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)) + </Original> + <Expanded> + { 1, 2, 3 } Contains: { 1, 2, 3 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, Contains(v6) + </Original> + <Expanded> + { 1, 2, 3 } Contains: { 1, 2 } + </Expanded> + </Expression> + <OverallResults successes="8" failures="0" expectedFailures="0"/> </Section> <Section name="Contains (element), composed" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > @@ -14267,13 +14315,37 @@ There is no extra whitespace here </Expression> <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Original> + v, Equals<int>({ 1, 2, 3 }) + </Original> + <Expanded> + { 1, 2, 3 } Equals: { 1, 2, 3 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> v, Equals(v2) </Original> <Expanded> { 1, 2, 3 } Equals: { 1, 2, 3 } </Expanded> </Expression> - <OverallResults successes="3" failures="0" expectedFailures="0"/> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, (Equals<int, std::allocator<int>, CustomAllocator<int>>(v2)) + </Original> + <Expanded> + { 1, 2, 3 } Equals: { 1, 2, 3 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, Equals(v6) + </Original> + <Expanded> + { 1, 2, 3 } Equals: { 1, 2, 3 } + </Expanded> + </Expression> + <OverallResults successes="6" failures="0" expectedFailures="0"/> </Section> <Section name="UnorderedEquals" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > @@ -14286,6 +14358,14 @@ There is no extra whitespace here </Expression> <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > <Original> + v, UnorderedEquals<int>({ 3, 2, 1 }) + </Original> + <Expanded> + { 1, 2, 3 } UnorderedEquals: { 3, 2, 1 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> empty, UnorderedEquals(empty) </Original> <Expanded> @@ -14308,7 +14388,23 @@ There is no extra whitespace here { 2, 3, 1 } UnorderedEquals: { 1, 2, 3 } </Expanded> </Expression> - <OverallResults successes="4" failures="0" expectedFailures="0"/> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5, (UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>(permuted)) + </Original> + <Expanded> + { 1, 2, 3 } UnorderedEquals: { 2, 3, 1 } + </Expanded> + </Expression> + <Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + v5_permuted, UnorderedEquals(v5) + </Original> + <Expanded> + { 1, 3, 2 } UnorderedEquals: { 1, 2, 3 } + </Expanded> + </Expression> + <OverallResults successes="7" failures="0" expectedFailures="0"/> </Section> <OverallResult success="true"/> </TestCase> @@ -17636,7 +17732,7 @@ loose text artifact </Section> <OverallResult success="true"/> </TestCase> - <OverallResults successes="1728" failures="149" expectedFailures="21"/> + <OverallResults successes="1740" failures="149" expectedFailures="21"/> </Group> - <OverallResults successes="1728" failures="148" expectedFailures="21"/> + <OverallResults successes="1740" failures="148" expectedFailures="21"/> </Catch> 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<typename T> + struct CustomAllocator : private std::allocator<T> + { + 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<typename U> + struct rebind + { using other = CustomAllocator<U>; }; + + using propagate_on_container_move_assignment = std::true_type; + using is_always_equal = std::true_type; + + CustomAllocator() = default; + + CustomAllocator(const CustomAllocator& other) + : std::allocator<T>(other) { } + + template<typename U> + CustomAllocator(const CustomAllocator<U>&) { } + + ~CustomAllocator() = default; + + using std::allocator<T>::address; + using std::allocator<T>::allocate; + using std::allocator<T>::construct; + using std::allocator<T>::deallocate; + using std::allocator<T>::max_size; + using std::allocator<T>::destroy; + }; + TEST_CASE("Vector matchers", "[matchers][vector]") { std::vector<int> 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<int, CustomAllocator<int>> v5; + v5.push_back(1); + v5.push_back(2); + v5.push_back(3); + + std::vector<int, CustomAllocator<int>> v6; + v6.push_back(1); + v6.push_back(2); + std::vector<int> empty; SECTION("Contains (element)") { CHECK_THAT(v, VectorContains(1)); CHECK_THAT(v, VectorContains(2)); + CHECK_THAT(v5, (VectorContains<int, CustomAllocator<int>>(2))); } SECTION("Contains (vector)") { CHECK_THAT(v, Contains(v2)); + |