Commit c92b6ce0 by Abseil Team Committed by Copybara-Service

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
parent 700e786e
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "absl/container/inlined_vector.h" #include "absl/container/inlined_vector.h"
#include <algorithm> #include <algorithm>
#include <cstddef>
#include <forward_list> #include <forward_list>
#include <iterator> #include <iterator>
#include <list> #include <list>
...@@ -51,15 +52,13 @@ using testing::ElementsAre; ...@@ -51,15 +52,13 @@ using testing::ElementsAre;
using testing::ElementsAreArray; using testing::ElementsAreArray;
using testing::Eq; using testing::Eq;
using testing::Gt; using testing::Gt;
using testing::Pointee;
using testing::Pointwise; using testing::Pointwise;
using testing::PrintToString; using testing::PrintToString;
using testing::SizeIs;
using IntVec = absl::InlinedVector<int, 8>; using IntVec = absl::InlinedVector<int, 8>;
MATCHER_P(SizeIs, n, "") {
return testing::ExplainMatchResult(n, arg.size(), result_listener);
}
MATCHER_P(CapacityIs, n, "") { MATCHER_P(CapacityIs, n, "") {
return testing::ExplainMatchResult(n, arg.capacity(), result_listener); return testing::ExplainMatchResult(n, arg.capacity(), result_listener);
} }
...@@ -262,6 +261,49 @@ TEST(IntVec, Hardened) { ...@@ -262,6 +261,49 @@ TEST(IntVec, Hardened) {
#endif #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<std::unique_ptr<size_t>, 2> a;
for (size_t i = 0; i < size; ++i) {
a.push_back(std::make_unique<size_t>(i));
}
absl::InlinedVector<std::unique_ptr<size_t>, 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<std::unique_ptr<size_t>, 2> a;
for (size_t i = 0; i < size; ++i) {
a.push_back(std::make_unique<size_t>(i));
}
absl::InlinedVector<std::unique_ptr<size_t>, 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) // 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 // should have reference counts == 0, and all others elements should have
// reference counts == 1. // reference counts == 1.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment