Support custom allocators in vector Matchers (#1909)

-- Combined with f4fc2dab2c
   during cherry-picking.
This commit is contained in:
schallerr 2020-04-16 15:36:54 +02:00 committed by Martin Hořeňovský
parent dd35430a2b
commit 2a93a65bc2
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
8 changed files with 315 additions and 51 deletions

View File

@ -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):
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

View File

@ -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 }

View File

@ -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

View File

@ -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)
@ -11770,6 +11780,16 @@ Matchers.tests.cpp:<line number>: PASSED:
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:
@ -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
@ -11814,11 +11844,26 @@ Matchers.tests.cpp:<line number>: PASSED:
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
@ -11831,6 +11876,11 @@ Matchers.tests.cpp:<line number>: PASSED:
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:
@ -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

View File

@ -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"/>

View File

@ -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

View File

@ -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&lt;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&lt;int, CustomAllocator&lt;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" >
@ -14211,6 +14227,22 @@ There is no extra whitespace here
{ 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&lt;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&lt;int, std::allocator&lt;int>, CustomAllocator&lt;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)
@ -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&lt;int, std::allocator&lt;int>, CustomAllocator&lt;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" >
@ -14265,6 +14313,14 @@ There is no extra whitespace here
{ } Equals: { }
</Expanded>
</Expression>
<Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
<Original>
v, Equals&lt;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)
@ -14273,7 +14329,23 @@ There is no extra whitespace here
{ 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&lt;int, std::allocator&lt;int>, CustomAllocator&lt;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" >
@ -14284,6 +14356,14 @@ There is no extra whitespace here
{ 1, 2, 3 } UnorderedEquals: { 1, 2, 3 }
</Expanded>
</Expression>
<Expression success="true" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
<Original>
v, UnorderedEquals&lt;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)
@ -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&lt;int, std::allocator&lt;int>, CustomAllocator&lt;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>

View File

@ -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));
CHECK_THAT(v, Contains<int>({ 1, 2 }));
CHECK_THAT(v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(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<int, std::allocator<int>, CustomAllocator<int>>(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<int>({ 1, 2, 3 }));
v2.push_back(3);
CHECK_THAT(v, Equals(v2));
CHECK_THAT(v5, (Equals<int, std::allocator<int>, CustomAllocator<int>>(v2)));
v6.push_back(3);
CHECK_THAT(v5, Equals(v6));
}
SECTION("UnorderedEquals") {
CHECK_THAT(v, UnorderedEquals(v));
CHECK_THAT(v, UnorderedEquals<int>({ 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<int, std::allocator<int>, CustomAllocator<int>>(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<double> v1({1., 2., 3.});
SECTION("A vector is approx equal to itself") {
REQUIRE_THAT(v1, Approx(v1));
REQUIRE_THAT(v1, Approx<double>({ 1., 2., 3. }));
}
std::vector<double> v2({1.5, 2.5, 3.5});
SECTION("Different length") {