diff options
author | Arthur O'Dwyer <arthur.j.odwyer@gmail.com> | 2024-02-21 09:17:54 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-02-21 09:18:57 -0800 |
commit | 92c8575d24cb5c27718e1cd1ed21a9847351a11b (patch) | |
tree | 3720389d85a5175ca9a42f1b3354118207e9302d /absl/container/inlined_vector_test.cc | |
parent | b0f85e2355b173d3f89dee29a7f817b52f8e72a2 (diff) | |
download | abseil-92c8575d24cb5c27718e1cd1ed21a9847351a11b.tar.gz abseil-92c8575d24cb5c27718e1cd1ed21a9847351a11b.tar.bz2 abseil-92c8575d24cb5c27718e1cd1ed21a9847351a11b.zip |
PR #1618: inlined_vector: Use trivial relocation for `SwapInlinedElements`
Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/1618
I noticed while working on #1615 that `inlined_vector` could use the trivial relocatability trait here, too.
Here the memcpy codepath already exists; we just have to opt in to using it.
Merge 567a1dd9b6b3352f649e900b24834b59e39cfa14 into a7012a5bfcf26a41b9dd32d4c429004773503dd6
Merging this change closes #1618
COPYBARA_INTEGRATE_REVIEW=https://github.com/abseil/abseil-cpp/pull/1618 from Quuxplusone:trivial-swap 567a1dd9b6b3352f649e900b24834b59e39cfa14
PiperOrigin-RevId: 609019296
Change-Id: I4055ab790245752179e405b490fcd479e7389726
Diffstat (limited to 'absl/container/inlined_vector_test.cc')
-rw-r--r-- | absl/container/inlined_vector_test.cc | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc index 241389ae..5ecf88a9 100644 --- a/absl/container/inlined_vector_test.cc +++ b/absl/container/inlined_vector_test.cc @@ -304,6 +304,35 @@ TEST(UniquePtr, MoveAssign) { } } +// Swapping containers of unique pointers should work fine, with no +// leaks, despite the fact that unique pointers are trivially relocatable but +// not trivially destructible. +// TODO(absl-team): Using unique_ptr here is technically correct, but +// a trivially relocatable struct would be less semantically confusing. +TEST(UniquePtr, Swap) { + for (size_t size1 = 0; size1 < 5; ++size1) { + for (size_t size2 = 0; size2 < 5; ++size2) { + absl::InlinedVector<std::unique_ptr<size_t>, 2> a; + absl::InlinedVector<std::unique_ptr<size_t>, 2> b; + for (size_t i = 0; i < size1; ++i) { + a.push_back(std::make_unique<size_t>(i + 10)); + } + for (size_t i = 0; i < size2; ++i) { + b.push_back(std::make_unique<size_t>(i + 20)); + } + a.swap(b); + ASSERT_THAT(a, SizeIs(size2)); + ASSERT_THAT(b, SizeIs(size1)); + for (size_t i = 0; i < a.size(); ++i) { + ASSERT_THAT(a[i], Pointee(i + 20)); + } + for (size_t i = 0; i < b.size(); ++i) { + ASSERT_THAT(b[i], Pointee(i + 10)); + } + } + } +} + // 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. @@ -783,7 +812,9 @@ TEST(OverheadTest, Storage) { // The union should be absorbing some of the allocation bookkeeping overhead // in the larger vectors, leaving only the size_ field as overhead. - struct T { void* val; }; + struct T { + void* val; + }; size_t expected_overhead = sizeof(T); EXPECT_EQ((2 * expected_overhead), |