Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/SelfTest/UsageTests/Matchers.tests.cpp
diff options
context:
space:
mode:
authorschallerr2020-04-16 15:36:54 +0200
committerMartin Hořeňovský2020-05-03 07:40:04 +0200
commit2a93a65bc24c21102e46c5e102f0905f97a10ee5 (patch)
treea111cb59be0d4a3f9a3e9559dfde86623f11d04f /tests/SelfTest/UsageTests/Matchers.tests.cpp
parentdd35430a2b03265343846735593fdc519cd64418 (diff)
downloadcatch3-2a93a65bc24c21102e46c5e102f0905f97a10ee5.tar.gz
Support custom allocators in vector Matchers (#1909)
-- Combined with f4fc2dab2c5bb8cab11b0a0dde8a8421076c30f9 during cherry-picking.
Diffstat (limited to 'tests/SelfTest/UsageTests/Matchers.tests.cpp')
-rw-r--r--tests/SelfTest/UsageTests/Matchers.tests.cpp66
1 files changed, 66 insertions, 0 deletions
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));
+ 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") {