From c92b6ce052ec4507e6f7ae44b435ab57e3e0fba6 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 24 Mar 2023 16:52:03 -0700 Subject: inlined_vector_test: add coverage of moving vectors of unique pointers. std::unique_ptr is trivially relocatable, but not trivially destructible. This will be important coverage to ensure correctness of upcoming commit(s) that expand the use of memcpy to more trivially relocatable types. PiperOrigin-RevId: 519270234 Change-Id: I8e584a405633dac89bf1f67eab8145971d2ddab2 --- absl/container/inlined_vector_test.cc | 50 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'absl/container') diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc index 898b40db..808f97cc 100644 --- a/absl/container/inlined_vector_test.cc +++ b/absl/container/inlined_vector_test.cc @@ -15,6 +15,7 @@ #include "absl/container/inlined_vector.h" #include +#include #include #include #include @@ -51,15 +52,13 @@ using testing::ElementsAre; using testing::ElementsAreArray; using testing::Eq; using testing::Gt; +using testing::Pointee; using testing::Pointwise; using testing::PrintToString; +using testing::SizeIs; using IntVec = absl::InlinedVector; -MATCHER_P(SizeIs, n, "") { - return testing::ExplainMatchResult(n, arg.size(), result_listener); -} - MATCHER_P(CapacityIs, n, "") { return testing::ExplainMatchResult(n, arg.capacity(), result_listener); } @@ -262,6 +261,49 @@ TEST(IntVec, Hardened) { #endif } +// Move construction of a container of unique pointers should work fine, with no +// leaks, despite the fact that unique pointers are trivially relocatable but +// not trivially destructible. +TEST(UniquePtr, MoveConstruct) { + for (size_t size = 0; size < 16; ++size) { + SCOPED_TRACE(size); + + absl::InlinedVector, 2> a; + for (size_t i = 0; i < size; ++i) { + a.push_back(std::make_unique(i)); + } + + absl::InlinedVector, 2> b(std::move(a)); + + ASSERT_THAT(b, SizeIs(size)); + for (size_t i = 0; i < size; ++i) { + ASSERT_THAT(b[i], Pointee(i)); + } + } +} + +// Move assignment of a container of unique pointers should work fine, with no +// leaks, despite the fact that unique pointers are trivially relocatable but +// not trivially destructible. +TEST(UniquePtr, MoveAssign) { + for (size_t size = 0; size < 16; ++size) { + SCOPED_TRACE(size); + + absl::InlinedVector, 2> a; + for (size_t i = 0; i < size; ++i) { + a.push_back(std::make_unique(i)); + } + + absl::InlinedVector, 2> b; + b = std::move(a); + + ASSERT_THAT(b, SizeIs(size)); + for (size_t i = 0; i < size; ++i) { + ASSERT_THAT(b[i], Pointee(i)); + } + } +} + // At the end of this test loop, the elements between [erase_begin, erase_end) // should have reference counts == 0, and all others elements should have // reference counts == 1. -- cgit v1.2.3