17 #include "absl/memory/memory.h"
19 #include <sys/types.h>
24 #include <type_traits>
28 #include "gmock/gmock.h"
29 #include "gtest/gtest.h"
38 class DestructorVerifier {
40 DestructorVerifier() { ++instance_count_; }
41 DestructorVerifier(
const DestructorVerifier&) =
delete;
42 DestructorVerifier& operator=(
const DestructorVerifier&) =
delete;
43 ~DestructorVerifier() { --instance_count_; }
46 static int instance_count() {
return instance_count_; }
50 static int instance_count_;
53 int DestructorVerifier::instance_count_ = 0;
59 auto dv =
new DestructorVerifier;
60 EXPECT_EQ(1, DestructorVerifier::instance_count());
62 EXPECT_EQ(1, DestructorVerifier::instance_count());
64 EXPECT_EQ(0, DestructorVerifier::instance_count());
66 TEST(MakeUniqueTest, Basic) {
67 std::unique_ptr<std::string>
p = absl::make_unique<std::string>();
69 p = absl::make_unique<std::string>(
"hi");
76 struct InitializationVerifier {
77 static constexpr
int kDefaultScalar = 0x43;
78 static constexpr
int kDefaultArray = 0x4B;
80 static void*
operator new(
size_t n) {
81 void*
ret = ::operator
new(
n);
86 static void*
operator new[](
size_t n) {
87 void*
ret = ::operator
new[](
n);
97 auto p = absl::make_unique<InitializationVerifier>();
103 TEST(Initialization, MakeUniqueArray) {
104 auto p = absl::make_unique<InitializationVerifier[]>(2);
113 MoveOnly() =
default;
114 explicit MoveOnly(
int i1) : ip1{
new int{
i1}} {}
115 MoveOnly(
int i1,
int i2) : ip1{
new int{
i1}}, ip2{
new int{
i2}} {}
116 std::unique_ptr<int> ip1;
117 std::unique_ptr<int> ip2;
120 struct AcceptMoveOnly {
121 explicit AcceptMoveOnly(MoveOnly
m) : m_(
std::
move(
m)) {}
125 TEST(MakeUniqueTest, MoveOnlyTypeAndValue) {
126 using ExpectedType = std::unique_ptr<MoveOnly>;
128 auto p = absl::make_unique<MoveOnly>();
129 static_assert(std::is_same<decltype(
p), ExpectedType>::
value,
130 "unexpected return type");
135 auto p = absl::make_unique<MoveOnly>(1);
136 static_assert(std::is_same<decltype(
p), ExpectedType>::
value,
137 "unexpected return type");
142 auto p = absl::make_unique<MoveOnly>(1, 2);
143 static_assert(std::is_same<decltype(
p), ExpectedType>::
value,
144 "unexpected return type");
150 TEST(MakeUniqueTest, AcceptMoveOnly) {
151 auto p = absl::make_unique<AcceptMoveOnly>(MoveOnly());
152 p = std::unique_ptr<AcceptMoveOnly>(
new AcceptMoveOnly(MoveOnly()));
156 void*
operator new[](
size_t n) {
157 allocs().push_back(
n);
158 return ::operator
new[](
n);
160 void operator delete[](
void*
p) { return ::operator
delete[](
p); }
161 static std::vector<size_t>& allocs() {
162 static auto&
v = *
new std::vector<size_t>;
170 ArrayWatch::allocs().clear();
172 auto p = absl::make_unique<ArrayWatch[]>(5);
173 static_assert(std::is_same<decltype(
p), std::unique_ptr<ArrayWatch[]>>::
value,
174 "unexpected return type");
178 TEST(Make_UniqueTest, NotAmbiguousWithStdMakeUnique) {
181 struct TakesStdType {
182 explicit TakesStdType(
const std::vector<int>& vec) {}
185 (void)make_unique<TakesStdType>(std::vector<int>());
190 TEST(MakeUniqueTestNC, AcceptMoveOnlyLvalue) {
192 auto p = absl::make_unique<AcceptMoveOnly>(
m);
194 TEST(MakeUniqueTestNC, KnownBoundArray) {
195 auto p = absl::make_unique<ArrayWatch[5]>();
199 TEST(RawPtrTest, RawPointer) {
204 TEST(RawPtrTest, SmartPointer) {
206 std::unique_ptr<int>
p(
o);
210 class IntPointerNonConstDeref {
212 explicit IntPointerNonConstDeref(
int*
p) :
p_(
p) {}
213 friend bool operator!=(
const IntPointerNonConstDeref&
a, std::nullptr_t) {
214 return a.p_ !=
nullptr;
219 std::unique_ptr<int>
p_;
222 TEST(RawPtrTest, SmartPointerNonConstDereference) {
224 IntPointerNonConstDeref
p(
o);
228 TEST(RawPtrTest, NullValuedRawPointer) {
233 TEST(RawPtrTest, NullValuedSmartPointer) {
234 std::unique_ptr<int>
p;
238 TEST(RawPtrTest, Nullptr) {
244 TEST(RawPtrTest, Null) {
250 TEST(RawPtrTest, Zero) {
256 TEST(ShareUniquePtrTest, Share) {
257 auto up = absl::make_unique<int>();
263 TEST(ShareUniquePtrTest, ShareNull) {
265 using pointer =
void*;
266 void operator()(pointer) {
267 ASSERT_TRUE(
false) <<
"Deleter should not have been called.";
271 std::unique_ptr<void, NeverDie> up;
275 TEST(WeakenPtrTest, Weak) {
276 auto sp = std::make_shared<int>();
290 template <
typename T>
291 struct SmartPointer {
292 using difference_type = char;
297 using difference_type =
int16_t;
298 template <
typename U>
299 using rebind = SmartPointer<U>;
301 static PointerWith pointer_to(
303 return PointerWith{&
r};
309 template <
typename...
Args>
310 struct PointerWithout {};
318 std::is_same<TraitsWith::rebind<int64_t>, SmartPointer<int64_t>>::
value));
322 PointerWithout<double, int>>::
value));
326 EXPECT_TRUE((std::is_same<TraitsWithout::rebind<int64_t>,
327 PointerWithout<int64_t, int>>::
value));
337 TEST(PointerTraits, Functions) {
357 using pointer = SmartPointer<X>;
360 HasPointer>::pointer>::
value));
367 SmartPointer<const X>,
373 struct HasVoidPointer {
375 struct void_pointer {};
378 EXPECT_TRUE((std::is_same<HasVoidPointer::void_pointer,
380 HasVoidPointer>::void_pointer>::
value));
383 HasPointer>::void_pointer>::
value));
385 struct HasConstVoidPointer {
387 struct const_void_pointer {};
391 (std::is_same<HasConstVoidPointer::const_void_pointer,
393 HasConstVoidPointer>::const_void_pointer>::
value));
394 EXPECT_TRUE((std::is_same<SmartPointer<const void>,
396 HasPointer>::const_void_pointer>::
value));
398 struct HasDifferenceType {
400 using difference_type =
int;
404 HasDifferenceType>::difference_type>::
value));
406 HasPointer>::difference_type>::
value));
410 using size_type =
unsigned int;
413 HasSizeType>::size_type>::
value));
415 HasPointer>::size_type>::
value));
417 struct HasPropagateOnCopy {
419 struct propagate_on_container_copy_assignment {};
423 (std::is_same<HasPropagateOnCopy::propagate_on_container_copy_assignment,
425 propagate_on_container_copy_assignment>::
value));
429 A>::propagate_on_container_copy_assignment>::
value));
431 struct HasPropagateOnMove {
433 struct propagate_on_container_move_assignment {};
437 (std::is_same<HasPropagateOnMove::propagate_on_container_move_assignment,
439 propagate_on_container_move_assignment>::
value));
443 A>::propagate_on_container_move_assignment>::
value));
445 struct HasPropagateOnSwap {
447 struct propagate_on_container_swap {};
451 (std::is_same<HasPropagateOnSwap::propagate_on_container_swap,
453 propagate_on_container_swap>::
value));
456 propagate_on_container_swap>::
value));
458 struct HasIsAlwaysEqual {
460 struct is_always_equal {};
463 EXPECT_TRUE((std::is_same<HasIsAlwaysEqual::is_always_equal,
465 HasIsAlwaysEqual>::is_always_equal>::
value));
467 A>::is_always_equal>::
value));
477 template <
typename T>
478 struct AllocWithPrivateInheritance :
private std::allocator<T> {
486 (std::is_same<AllocWithPrivateInheritance<int>,
488 rebind_alloc<int>>::
value));
491 template <
typename T>
494 struct AllocWithRebind {
496 template <
typename T>
498 using other = Rebound<T>;
502 template <
typename T,
typename U>
503 struct AllocWithoutRebind {
509 (std::is_same<Rebound<int>,
511 AllocWithRebind>::
template rebind_alloc<int>>::
value));
515 AllocWithRebind>::
template rebind_traits<int>>::
value));
518 (std::is_same<AllocWithoutRebind<double, char>,
520 int,
char>>::
template rebind_alloc<double>>::
value));
524 int,
char>>::
template rebind_traits<double>>::
value));
529 explicit TestValue(
int* trace) : trace(trace) { ++*trace; }
533 int* trace =
nullptr;
536 struct MinimalMockAllocator {
537 MinimalMockAllocator() :
value(0) {}
539 MinimalMockAllocator(
const MinimalMockAllocator& other)
551 alignas(TestValue)
char buffer[
sizeof(TestValue)];
552 auto*
x =
reinterpret_cast<TestValue*
>(
buffer);
553 MinimalMockAllocator mock;
559 static_cast<void>(Traits::allocate(mock, 7,
static_cast<const void*
>(&hint)));
560 EXPECT_EQ(
x, Traits::allocate(mock, 7,
static_cast<const void*
>(&hint)));
561 Traits::deallocate(mock,
x, 7);
570 Traits::max_size(mock));
573 EXPECT_EQ(0, Traits::select_on_container_copy_construction(mock).
value);
576 struct FullMockAllocator {
577 FullMockAllocator() :
value(0) {}
579 FullMockAllocator(
const FullMockAllocator& other) :
value(other.
value) {}
587 MOCK_METHOD(FullMockAllocator, select_on_container_copy_construction, (),
596 TestValue
x(&trace),
y;
597 FullMockAllocator mock;
604 EXPECT_CALL(mock, select_on_container_copy_construction())
605 .WillRepeatedly(
Return(FullMockAllocator(23)));
608 EXPECT_EQ(&
y, Traits::allocate(mock, 13,
static_cast<const void*
>(&hint)));
619 EXPECT_EQ(23, Traits::select_on_container_copy_construction(mock).
value);
622 TEST(AllocatorNoThrowTest, DefaultAllocator) {
623 #if defined(ABSL_ALLOCATOR_NOTHROW) && ABSL_ALLOCATOR_NOTHROW
630 TEST(AllocatorNoThrowTest, StdAllocator) {
631 #if defined(ABSL_ALLOCATOR_NOTHROW) && ABSL_ALLOCATOR_NOTHROW
638 TEST(AllocatorNoThrowTest, CustomAllocator) {
639 struct NoThrowAllocator {
642 struct CanThrowAllocator {
645 struct UnspecifiedAllocator {};