summaryrefslogtreecommitdiff
path: root/absl/memory
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-05-03 13:25:53 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-05-03 13:27:00 -0700
commit8d9cef25c70321c794ff5559cc0b1e0fa5f85e72 (patch)
treec0769706828973b938b8a788f9b0f7e5ea3dd657 /absl/memory
parentcc04c0e07427deb78091231238189d14561a9441 (diff)
Don't construct/destroy object twice
PiperOrigin-RevId: 446274314 Change-Id: Ibf641808c533a10e0aef8d1601095e539ae5c43a
Diffstat (limited to 'absl/memory')
-rw-r--r--absl/memory/memory_test.cc21
1 files changed, 11 insertions, 10 deletions
diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc
index 1990c7ba..5d5719b3 100644
--- a/absl/memory/memory_test.cc
+++ b/absl/memory/memory_test.cc
@@ -548,22 +548,23 @@ struct MinimalMockAllocator {
TEST(AllocatorTraits, FunctionsMinimal) {
int trace = 0;
int hint;
- TestValue x(&trace);
+ alignas(TestValue) char buffer[sizeof(TestValue)];
+ auto* x = reinterpret_cast<TestValue*>(buffer);
MinimalMockAllocator mock;
using Traits = absl::allocator_traits<MinimalMockAllocator>;
- EXPECT_CALL(mock, allocate(7)).WillRepeatedly(Return(&x));
- EXPECT_CALL(mock, deallocate(&x, 7));
+ EXPECT_CALL(mock, allocate(7)).WillRepeatedly(Return(x));
+ EXPECT_CALL(mock, deallocate(x, 7));
- EXPECT_EQ(&x, Traits::allocate(mock, 7));
+ EXPECT_EQ(x, Traits::allocate(mock, 7));
static_cast<void>(Traits::allocate(mock, 7, static_cast<const void*>(&hint)));
- EXPECT_EQ(&x, Traits::allocate(mock, 7, static_cast<const void*>(&hint)));
- Traits::deallocate(mock, &x, 7);
+ EXPECT_EQ(x, Traits::allocate(mock, 7, static_cast<const void*>(&hint)));
+ Traits::deallocate(mock, x, 7);
+ EXPECT_EQ(0, trace);
+ Traits::construct(mock, x, &trace);
EXPECT_EQ(1, trace);
- Traits::construct(mock, &x, &trace);
- EXPECT_EQ(2, trace);
- Traits::destroy(mock, &x);
- EXPECT_EQ(1, trace);
+ Traits::destroy(mock, x);
+ EXPECT_EQ(0, trace);
EXPECT_EQ(std::numeric_limits<size_t>::max() / sizeof(TestValue),
Traits::max_size(mock));