mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 21:05:39 +02:00
Support custom allocators in vector Matchers (#1909)
This commit is contained in:
@@ -214,6 +214,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);
|
||||
@@ -234,19 +270,34 @@ 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(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));
|
||||
@@ -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<int, std::allocator<int>, CustomAllocator<int>>(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<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));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user