memory_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Tests for pointer utilities.
  15. #include "absl/memory/memory.h"
  16. #include <sys/types.h>
  17. #include <cstddef>
  18. #include <memory>
  19. #include <string>
  20. #include <type_traits>
  21. #include <utility>
  22. #include <vector>
  23. #include "gmock/gmock.h"
  24. #include "gtest/gtest.h"
  25. namespace {
  26. using ::testing::ElementsAre;
  27. using ::testing::Return;
  28. // This class creates observable behavior to verify that a destructor has
  29. // been called, via the instance_count variable.
  30. class DestructorVerifier {
  31. public:
  32. DestructorVerifier() { ++instance_count_; }
  33. DestructorVerifier(const DestructorVerifier&) = delete;
  34. DestructorVerifier& operator=(const DestructorVerifier&) = delete;
  35. ~DestructorVerifier() { --instance_count_; }
  36. // The number of instances of this class currently active.
  37. static int instance_count() { return instance_count_; }
  38. private:
  39. // The number of instances of this class currently active.
  40. static int instance_count_;
  41. };
  42. int DestructorVerifier::instance_count_ = 0;
  43. TEST(WrapUniqueTest, WrapUnique) {
  44. // Test that the unique_ptr is constructed properly by verifying that the
  45. // destructor for its payload gets called at the proper time.
  46. {
  47. auto dv = new DestructorVerifier;
  48. EXPECT_EQ(1, DestructorVerifier::instance_count());
  49. std::unique_ptr<DestructorVerifier> ptr = absl::WrapUnique(dv);
  50. EXPECT_EQ(1, DestructorVerifier::instance_count());
  51. }
  52. EXPECT_EQ(0, DestructorVerifier::instance_count());
  53. }
  54. TEST(MakeUniqueTest, Basic) {
  55. std::unique_ptr<std::string> p = absl::make_unique<std::string>();
  56. EXPECT_EQ("", *p);
  57. p = absl::make_unique<std::string>("hi");
  58. EXPECT_EQ("hi", *p);
  59. }
  60. // InitializationVerifier fills in a pattern when allocated so we can
  61. // distinguish between its default and value initialized states (without
  62. // accessing truly uninitialized memory).
  63. struct InitializationVerifier {
  64. static constexpr int kDefaultScalar = 0x43;
  65. static constexpr int kDefaultArray = 0x4B;
  66. static void* operator new(size_t n) {
  67. void* ret = ::operator new(n);
  68. memset(ret, kDefaultScalar, n);
  69. return ret;
  70. }
  71. static void* operator new[](size_t n) {
  72. void* ret = ::operator new[](n);
  73. memset(ret, kDefaultArray, n);
  74. return ret;
  75. }
  76. int a;
  77. int b;
  78. };
  79. TEST(Initialization, MakeUnique) {
  80. auto p = absl::make_unique<InitializationVerifier>();
  81. EXPECT_EQ(0, p->a);
  82. EXPECT_EQ(0, p->b);
  83. }
  84. TEST(Initialization, MakeUniqueArray) {
  85. auto p = absl::make_unique<InitializationVerifier[]>(2);
  86. EXPECT_EQ(0, p[0].a);
  87. EXPECT_EQ(0, p[0].b);
  88. EXPECT_EQ(0, p[1].a);
  89. EXPECT_EQ(0, p[1].b);
  90. }
  91. struct MoveOnly {
  92. MoveOnly() = default;
  93. explicit MoveOnly(int i1) : ip1{new int{i1}} {}
  94. MoveOnly(int i1, int i2) : ip1{new int{i1}}, ip2{new int{i2}} {}
  95. std::unique_ptr<int> ip1;
  96. std::unique_ptr<int> ip2;
  97. };
  98. struct AcceptMoveOnly {
  99. explicit AcceptMoveOnly(MoveOnly m) : m_(std::move(m)) {}
  100. MoveOnly m_;
  101. };
  102. TEST(MakeUniqueTest, MoveOnlyTypeAndValue) {
  103. using ExpectedType = std::unique_ptr<MoveOnly>;
  104. {
  105. auto p = absl::make_unique<MoveOnly>();
  106. static_assert(std::is_same<decltype(p), ExpectedType>::value,
  107. "unexpected return type");
  108. EXPECT_TRUE(!p->ip1);
  109. EXPECT_TRUE(!p->ip2);
  110. }
  111. {
  112. auto p = absl::make_unique<MoveOnly>(1);
  113. static_assert(std::is_same<decltype(p), ExpectedType>::value,
  114. "unexpected return type");
  115. EXPECT_TRUE(p->ip1 && *p->ip1 == 1);
  116. EXPECT_TRUE(!p->ip2);
  117. }
  118. {
  119. auto p = absl::make_unique<MoveOnly>(1, 2);
  120. static_assert(std::is_same<decltype(p), ExpectedType>::value,
  121. "unexpected return type");
  122. EXPECT_TRUE(p->ip1 && *p->ip1 == 1);
  123. EXPECT_TRUE(p->ip2 && *p->ip2 == 2);
  124. }
  125. }
  126. TEST(MakeUniqueTest, AcceptMoveOnly) {
  127. auto p = absl::make_unique<AcceptMoveOnly>(MoveOnly());
  128. p = std::unique_ptr<AcceptMoveOnly>(new AcceptMoveOnly(MoveOnly()));
  129. }
  130. struct ArrayWatch {
  131. void* operator new[](size_t n) {
  132. allocs().push_back(n);
  133. return ::operator new[](n);
  134. }
  135. void operator delete[](void* p) { return ::operator delete[](p); }
  136. static std::vector<size_t>& allocs() {
  137. static auto& v = *new std::vector<size_t>;
  138. return v;
  139. }
  140. };
  141. TEST(Make_UniqueTest, Array) {
  142. // Ensure state is clean before we start so that these tests
  143. // are order-agnostic.
  144. ArrayWatch::allocs().clear();
  145. auto p = absl::make_unique<ArrayWatch[]>(5);
  146. static_assert(std::is_same<decltype(p), std::unique_ptr<ArrayWatch[]>>::value,
  147. "unexpected return type");
  148. EXPECT_THAT(ArrayWatch::allocs(), ElementsAre(5 * sizeof(ArrayWatch)));
  149. }
  150. TEST(Make_UniqueTest, NotAmbiguousWithStdMakeUnique) {
  151. // Ensure that absl::make_unique is not ambiguous with std::make_unique.
  152. // In C++14 mode, the below call to make_unique has both types as candidates.
  153. struct TakesStdType {
  154. explicit TakesStdType(const std::vector<int>& vec) {}
  155. };
  156. using absl::make_unique;
  157. (void)make_unique<TakesStdType>(std::vector<int>());
  158. }
  159. #if 0
  160. // These tests shouldn't compile.
  161. TEST(MakeUniqueTestNC, AcceptMoveOnlyLvalue) {
  162. auto m = MoveOnly();
  163. auto p = absl::make_unique<AcceptMoveOnly>(m);
  164. }
  165. TEST(MakeUniqueTestNC, KnownBoundArray) {
  166. auto p = absl::make_unique<ArrayWatch[5]>();
  167. }
  168. #endif
  169. TEST(RawPtrTest, RawPointer) {
  170. int i = 5;
  171. EXPECT_EQ(&i, absl::RawPtr(&i));
  172. }
  173. TEST(RawPtrTest, SmartPointer) {
  174. int* o = new int(5);
  175. std::unique_ptr<int> p(o);
  176. EXPECT_EQ(o, absl::RawPtr(p));
  177. }
  178. class IntPointerNonConstDeref {
  179. public:
  180. explicit IntPointerNonConstDeref(int* p) : p_(p) {}
  181. friend bool operator!=(const IntPointerNonConstDeref& a, std::nullptr_t) {
  182. return a.p_ != nullptr;
  183. }
  184. int& operator*() { return *p_; }
  185. private:
  186. std::unique_ptr<int> p_;
  187. };
  188. TEST(RawPtrTest, SmartPointerNonConstDereference) {
  189. int* o = new int(5);
  190. IntPointerNonConstDeref p(o);
  191. EXPECT_EQ(o, absl::RawPtr(p));
  192. }
  193. TEST(RawPtrTest, NullValuedRawPointer) {
  194. int* p = nullptr;
  195. EXPECT_EQ(nullptr, absl::RawPtr(p));
  196. }
  197. TEST(RawPtrTest, NullValuedSmartPointer) {
  198. std::unique_ptr<int> p;
  199. EXPECT_EQ(nullptr, absl::RawPtr(p));
  200. }
  201. TEST(RawPtrTest, Nullptr) {
  202. auto p = absl::RawPtr(nullptr);
  203. EXPECT_TRUE((std::is_same<std::nullptr_t, decltype(p)>::value));
  204. EXPECT_EQ(nullptr, p);
  205. }
  206. TEST(RawPtrTest, Null) {
  207. auto p = absl::RawPtr(nullptr);
  208. EXPECT_TRUE((std::is_same<std::nullptr_t, decltype(p)>::value));
  209. EXPECT_EQ(nullptr, p);
  210. }
  211. TEST(RawPtrTest, Zero) {
  212. auto p = absl::RawPtr(nullptr);
  213. EXPECT_TRUE((std::is_same<std::nullptr_t, decltype(p)>::value));
  214. EXPECT_EQ(nullptr, p);
  215. }
  216. TEST(ShareUniquePtrTest, Share) {
  217. auto up = absl::make_unique<int>();
  218. int* rp = up.get();
  219. auto sp = absl::ShareUniquePtr(std::move(up));
  220. EXPECT_EQ(sp.get(), rp);
  221. }
  222. TEST(ShareUniquePtrTest, ShareNull) {
  223. struct NeverDie {
  224. using pointer = void*;
  225. void operator()(pointer) {
  226. ASSERT_TRUE(false) << "Deleter should not have been called.";
  227. }
  228. };
  229. std::unique_ptr<void, NeverDie> up;
  230. auto sp = absl::ShareUniquePtr(std::move(up));
  231. }
  232. TEST(WeakenPtrTest, Weak) {
  233. auto sp = std::make_shared<int>();
  234. auto wp = absl::WeakenPtr(sp);
  235. EXPECT_EQ(sp.get(), wp.lock().get());
  236. sp.reset();
  237. EXPECT_TRUE(wp.expired());
  238. }
  239. // Should not compile.
  240. /*
  241. TEST(RawPtrTest, NotAPointer) {
  242. absl::RawPtr(1.5);
  243. }
  244. */
  245. template <typename T>
  246. struct SmartPointer {
  247. using difference_type = char;
  248. };
  249. struct PointerWith {
  250. using element_type = int32_t;
  251. using difference_type = int16_t;
  252. template <typename U>
  253. using rebind = SmartPointer<U>;
  254. static PointerWith pointer_to(
  255. element_type& r) { // NOLINT(runtime/references)
  256. return PointerWith{&r};
  257. }
  258. element_type* ptr;
  259. };
  260. template <typename... Args>
  261. struct PointerWithout {};
  262. TEST(PointerTraits, Types) {
  263. using TraitsWith = absl::pointer_traits<PointerWith>;
  264. EXPECT_TRUE((std::is_same<TraitsWith::pointer, PointerWith>::value));
  265. EXPECT_TRUE((std::is_same<TraitsWith::element_type, int32_t>::value));
  266. EXPECT_TRUE((std::is_same<TraitsWith::difference_type, int16_t>::value));
  267. EXPECT_TRUE((
  268. std::is_same<TraitsWith::rebind<int64_t>, SmartPointer<int64_t>>::value));
  269. using TraitsWithout = absl::pointer_traits<PointerWithout<double, int>>;
  270. EXPECT_TRUE((std::is_same<TraitsWithout::pointer,
  271. PointerWithout<double, int>>::value));
  272. EXPECT_TRUE((std::is_same<TraitsWithout::element_type, double>::value));
  273. EXPECT_TRUE(
  274. (std::is_same<TraitsWithout ::difference_type, std::ptrdiff_t>::value));
  275. EXPECT_TRUE((std::is_same<TraitsWithout::rebind<int64_t>,
  276. PointerWithout<int64_t, int>>::value));
  277. using TraitsRawPtr = absl::pointer_traits<char*>;
  278. EXPECT_TRUE((std::is_same<TraitsRawPtr::pointer, char*>::value));
  279. EXPECT_TRUE((std::is_same<TraitsRawPtr::element_type, char>::value));
  280. EXPECT_TRUE(
  281. (std::is_same<TraitsRawPtr::difference_type, std::ptrdiff_t>::value));
  282. EXPECT_TRUE((std::is_same<TraitsRawPtr::rebind<int64_t>, int64_t*>::value));
  283. }
  284. TEST(PointerTraits, Functions) {
  285. int i;
  286. EXPECT_EQ(&i, absl::pointer_traits<PointerWith>::pointer_to(i).ptr);
  287. EXPECT_EQ(&i, absl::pointer_traits<int*>::pointer_to(i));
  288. }
  289. TEST(AllocatorTraits, Typedefs) {
  290. struct A {
  291. struct value_type {};
  292. };
  293. EXPECT_TRUE((
  294. std::is_same<A,
  295. typename absl::allocator_traits<A>::allocator_type>::value));
  296. EXPECT_TRUE(
  297. (std::is_same<A::value_type,
  298. typename absl::allocator_traits<A>::value_type>::value));
  299. struct X {};
  300. struct HasPointer {
  301. using value_type = X;
  302. using pointer = SmartPointer<X>;
  303. };
  304. EXPECT_TRUE((std::is_same<SmartPointer<X>, typename absl::allocator_traits<
  305. HasPointer>::pointer>::value));
  306. EXPECT_TRUE(
  307. (std::is_same<A::value_type*,
  308. typename absl::allocator_traits<A>::pointer>::value));
  309. EXPECT_TRUE(
  310. (std::is_same<
  311. SmartPointer<const X>,
  312. typename absl::allocator_traits<HasPointer>::const_pointer>::value));
  313. EXPECT_TRUE(
  314. (std::is_same<const A::value_type*,
  315. typename absl::allocator_traits<A>::const_pointer>::value));
  316. struct HasVoidPointer {
  317. using value_type = X;
  318. struct void_pointer {};
  319. };
  320. EXPECT_TRUE((std::is_same<HasVoidPointer::void_pointer,
  321. typename absl::allocator_traits<
  322. HasVoidPointer>::void_pointer>::value));
  323. EXPECT_TRUE(
  324. (std::is_same<SmartPointer<void>, typename absl::allocator_traits<
  325. HasPointer>::void_pointer>::value));
  326. struct HasConstVoidPointer {
  327. using value_type = X;
  328. struct const_void_pointer {};
  329. };
  330. EXPECT_TRUE(
  331. (std::is_same<HasConstVoidPointer::const_void_pointer,
  332. typename absl::allocator_traits<
  333. HasConstVoidPointer>::const_void_pointer>::value));
  334. EXPECT_TRUE((std::is_same<SmartPointer<const void>,
  335. typename absl::allocator_traits<
  336. HasPointer>::const_void_pointer>::value));
  337. struct HasDifferenceType {
  338. using value_type = X;
  339. using difference_type = int;
  340. };
  341. EXPECT_TRUE(
  342. (std::is_same<int, typename absl::allocator_traits<
  343. HasDifferenceType>::difference_type>::value));
  344. EXPECT_TRUE((std::is_same<char, typename absl::allocator_traits<
  345. HasPointer>::difference_type>::value));
  346. struct HasSizeType {
  347. using value_type = X;
  348. using size_type = unsigned int;
  349. };
  350. EXPECT_TRUE((std::is_same<unsigned int, typename absl::allocator_traits<
  351. HasSizeType>::size_type>::value));
  352. EXPECT_TRUE((std::is_same<unsigned char, typename absl::allocator_traits<
  353. HasPointer>::size_type>::value));
  354. struct HasPropagateOnCopy {
  355. using value_type = X;
  356. struct propagate_on_container_copy_assignment {};
  357. };
  358. EXPECT_TRUE(
  359. (std::is_same<HasPropagateOnCopy::propagate_on_container_copy_assignment,
  360. typename absl::allocator_traits<HasPropagateOnCopy>::
  361. propagate_on_container_copy_assignment>::value));
  362. EXPECT_TRUE(
  363. (std::is_same<std::false_type,
  364. typename absl::allocator_traits<
  365. A>::propagate_on_container_copy_assignment>::value));
  366. struct HasPropagateOnMove {
  367. using value_type = X;
  368. struct propagate_on_container_move_assignment {};
  369. };
  370. EXPECT_TRUE(
  371. (std::is_same<HasPropagateOnMove::propagate_on_container_move_assignment,
  372. typename absl::allocator_traits<HasPropagateOnMove>::
  373. propagate_on_container_move_assignment>::value));
  374. EXPECT_TRUE(
  375. (std::is_same<std::false_type,
  376. typename absl::allocator_traits<
  377. A>::propagate_on_container_move_assignment>::value));
  378. struct HasPropagateOnSwap {
  379. using value_type = X;
  380. struct propagate_on_container_swap {};
  381. };
  382. EXPECT_TRUE(
  383. (std::is_same<HasPropagateOnSwap::propagate_on_container_swap,
  384. typename absl::allocator_traits<HasPropagateOnSwap>::
  385. propagate_on_container_swap>::value));
  386. EXPECT_TRUE(
  387. (std::is_same<std::false_type, typename absl::allocator_traits<A>::
  388. propagate_on_container_swap>::value));
  389. struct HasIsAlwaysEqual {
  390. using value_type = X;
  391. struct is_always_equal {};
  392. };
  393. EXPECT_TRUE((std::is_same<HasIsAlwaysEqual::is_always_equal,
  394. typename absl::allocator_traits<
  395. HasIsAlwaysEqual>::is_always_equal>::value));
  396. EXPECT_TRUE((std::is_same<std::true_type, typename absl::allocator_traits<
  397. A>::is_always_equal>::value));
  398. struct NonEmpty {
  399. using value_type = X;
  400. int i;
  401. };
  402. EXPECT_TRUE(
  403. (std::is_same<std::false_type,
  404. absl::allocator_traits<NonEmpty>::is_always_equal>::value));
  405. }
  406. template <typename T>
  407. struct AllocWithPrivateInheritance : private std::allocator<T> {
  408. using value_type = T;
  409. };
  410. TEST(AllocatorTraits, RebindWithPrivateInheritance) {
  411. // Regression test for some versions of gcc that do not like the sfinae we
  412. // used in combination with private inheritance.
  413. EXPECT_TRUE(
  414. (std::is_same<AllocWithPrivateInheritance<int>,
  415. absl::allocator_traits<AllocWithPrivateInheritance<char>>::
  416. rebind_alloc<int>>::value));
  417. }
  418. template <typename T>
  419. struct Rebound {};
  420. struct AllocWithRebind {
  421. using value_type = int;
  422. template <typename T>
  423. struct rebind {
  424. using other = Rebound<T>;
  425. };
  426. };
  427. template <typename T, typename U>
  428. struct AllocWithoutRebind {
  429. using value_type = int;
  430. };
  431. TEST(AllocatorTraits, Rebind) {
  432. EXPECT_TRUE(
  433. (std::is_same<Rebound<int>,
  434. typename absl::allocator_traits<
  435. AllocWithRebind>::template rebind_alloc<int>>::value));
  436. EXPECT_TRUE(
  437. (std::is_same<absl::allocator_traits<Rebound<int>>,
  438. typename absl::allocator_traits<
  439. AllocWithRebind>::template rebind_traits<int>>::value));
  440. EXPECT_TRUE(
  441. (std::is_same<AllocWithoutRebind<double, char>,
  442. typename absl::allocator_traits<AllocWithoutRebind<
  443. int, char>>::template rebind_alloc<double>>::value));
  444. EXPECT_TRUE(
  445. (std::is_same<absl::allocator_traits<AllocWithoutRebind<double, char>>,
  446. typename absl::allocator_traits<AllocWithoutRebind<
  447. int, char>>::template rebind_traits<double>>::value));
  448. }
  449. struct TestValue {
  450. TestValue() {}
  451. explicit TestValue(int* trace) : trace(trace) { ++*trace; }
  452. ~TestValue() {
  453. if (trace) --*trace;
  454. }
  455. int* trace = nullptr;
  456. };
  457. struct MinimalMockAllocator {
  458. MinimalMockAllocator() : value(0) {}
  459. explicit MinimalMockAllocator(int value) : value(value) {}
  460. MinimalMockAllocator(const MinimalMockAllocator& other)
  461. : value(other.value) {}
  462. using value_type = TestValue;
  463. MOCK_METHOD(value_type*, allocate, (size_t));
  464. MOCK_METHOD(void, deallocate, (value_type*, size_t));
  465. int value;
  466. };
  467. TEST(AllocatorTraits, FunctionsMinimal) {
  468. int trace = 0;
  469. int hint;
  470. TestValue x(&trace);
  471. MinimalMockAllocator mock;
  472. using Traits = absl::allocator_traits<MinimalMockAllocator>;
  473. EXPECT_CALL(mock, allocate(7)).WillRepeatedly(Return(&x));
  474. EXPECT_CALL(mock, deallocate(&x, 7));
  475. EXPECT_EQ(&x, Traits::allocate(mock, 7));
  476. static_cast<void>(Traits::allocate(mock, 7, static_cast<const void*>(&hint)));
  477. EXPECT_EQ(&x, Traits::allocate(mock, 7, static_cast<const void*>(&hint)));
  478. Traits::deallocate(mock, &x, 7);
  479. EXPECT_EQ(1, trace);
  480. Traits::construct(mock, &x, &trace);
  481. EXPECT_EQ(2, trace);
  482. Traits::destroy(mock, &x);
  483. EXPECT_EQ(1, trace);
  484. EXPECT_EQ(std::numeric_limits<size_t>::max() / sizeof(TestValue),
  485. Traits::max_size(mock));
  486. EXPECT_EQ(0, mock.value);
  487. EXPECT_EQ(0, Traits::select_on_container_copy_construction(mock).value);
  488. }
  489. struct FullMockAllocator {
  490. FullMockAllocator() : value(0) {}
  491. explicit FullMockAllocator(int value) : value(value) {}
  492. FullMockAllocator(const FullMockAllocator& other) : value(other.value) {}
  493. using value_type = TestValue;
  494. MOCK_METHOD(value_type*, allocate, (size_t));
  495. MOCK_METHOD(value_type*, allocate, (size_t, const void*));
  496. MOCK_METHOD(void, construct, (value_type*, int*));
  497. MOCK_METHOD(void, destroy, (value_type*));
  498. MOCK_METHOD(size_t, max_size, (),
  499. (const));
  500. MOCK_METHOD(FullMockAllocator, select_on_container_copy_construction, (),
  501. (const));
  502. int value;
  503. };
  504. TEST(AllocatorTraits, FunctionsFull) {
  505. int trace = 0;
  506. int hint;
  507. TestValue x(&trace), y;
  508. FullMockAllocator mock;
  509. using Traits = absl::allocator_traits<FullMockAllocator>;
  510. EXPECT_CALL(mock, allocate(7)).WillRepeatedly(Return(&x));
  511. EXPECT_CALL(mock, allocate(13, &hint)).WillRepeatedly(Return(&y));
  512. EXPECT_CALL(mock, construct(&x, &trace));
  513. EXPECT_CALL(mock, destroy(&x));
  514. EXPECT_CALL(mock, max_size()).WillRepeatedly(Return(17));
  515. EXPECT_CALL(mock, select_on_container_copy_construction())
  516. .WillRepeatedly(Return(FullMockAllocator(23)));
  517. EXPECT_EQ(&x, Traits::allocate(mock, 7));
  518. EXPECT_EQ(&y, Traits::allocate(mock, 13, static_cast<const void*>(&hint)));
  519. EXPECT_EQ(1, trace);
  520. Traits::construct(mock, &x, &trace);
  521. EXPECT_EQ(1, trace);
  522. Traits::destroy(mock, &x);
  523. EXPECT_EQ(1, trace);
  524. EXPECT_EQ(17, Traits::max_size(mock));
  525. EXPECT_EQ(0, mock.value);
  526. EXPECT_EQ(23, Traits::select_on_container_copy_construction(mock).value);
  527. }
  528. TEST(AllocatorNoThrowTest, DefaultAllocator) {
  529. #if defined(ABSL_ALLOCATOR_NOTHROW) && ABSL_ALLOCATOR_NOTHROW
  530. EXPECT_TRUE(absl::default_allocator_is_nothrow::value);
  531. #else
  532. EXPECT_FALSE(absl::default_allocator_is_nothrow::value);
  533. #endif
  534. }
  535. TEST(AllocatorNoThrowTest, StdAllocator) {
  536. #if defined(ABSL_ALLOCATOR_NOTHROW) && ABSL_ALLOCATOR_NOTHROW
  537. EXPECT_TRUE(absl::allocator_is_nothrow<std::allocator<int>>::value);
  538. #else
  539. EXPECT_FALSE(absl::allocator_is_nothrow<std::allocator<int>>::value);
  540. #endif
  541. }
  542. TEST(AllocatorNoThrowTest, CustomAllocator) {
  543. struct NoThrowAllocator {
  544. using is_nothrow = std::true_type;
  545. };
  546. struct CanThrowAllocator {
  547. using is_nothrow = std::false_type;
  548. };
  549. struct UnspecifiedAllocator {};
  550. EXPECT_TRUE(absl::allocator_is_nothrow<NoThrowAllocator>::value);
  551. EXPECT_FALSE(absl::allocator_is_nothrow<CanThrowAllocator>::value);
  552. EXPECT_FALSE(absl::allocator_is_nothrow<UnspecifiedAllocator>::value);
  553. }
  554. } // namespace