container_memory_test.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2018 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. #include "absl/container/internal/container_memory.h"
  15. #include <cstdint>
  16. #include <tuple>
  17. #include <typeindex>
  18. #include <typeinfo>
  19. #include <utility>
  20. #include "gmock/gmock.h"
  21. #include "gtest/gtest.h"
  22. #include "absl/container/internal/test_instance_tracker.h"
  23. #include "absl/strings/string_view.h"
  24. namespace absl {
  25. ABSL_NAMESPACE_BEGIN
  26. namespace container_internal {
  27. namespace {
  28. using ::absl::test_internal::CopyableMovableInstance;
  29. using ::absl::test_internal::InstanceTracker;
  30. using ::testing::_;
  31. using ::testing::ElementsAre;
  32. using ::testing::Gt;
  33. using ::testing::Pair;
  34. TEST(Memory, AlignmentLargerThanBase) {
  35. std::allocator<int8_t> alloc;
  36. void* mem = Allocate<2>(&alloc, 3);
  37. EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
  38. memcpy(mem, "abc", 3);
  39. Deallocate<2>(&alloc, mem, 3);
  40. }
  41. TEST(Memory, AlignmentSmallerThanBase) {
  42. std::allocator<int64_t> alloc;
  43. void* mem = Allocate<2>(&alloc, 3);
  44. EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
  45. memcpy(mem, "abc", 3);
  46. Deallocate<2>(&alloc, mem, 3);
  47. }
  48. std::map<std::type_index, int>& AllocationMap() {
  49. static auto* map = new std::map<std::type_index, int>;
  50. return *map;
  51. }
  52. template <typename T>
  53. struct TypeCountingAllocator {
  54. TypeCountingAllocator() = default;
  55. template <typename U>
  56. TypeCountingAllocator(const TypeCountingAllocator<U>&) {} // NOLINT
  57. using value_type = T;
  58. T* allocate(size_t n, const void* = nullptr) {
  59. AllocationMap()[typeid(T)] += n;
  60. return std::allocator<T>().allocate(n);
  61. }
  62. void deallocate(T* p, std::size_t n) {
  63. AllocationMap()[typeid(T)] -= n;
  64. return std::allocator<T>().deallocate(p, n);
  65. }
  66. };
  67. TEST(Memory, AllocateDeallocateMatchType) {
  68. TypeCountingAllocator<int> alloc;
  69. void* mem = Allocate<1>(&alloc, 1);
  70. // Verify that it was allocated
  71. EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, Gt(0))));
  72. Deallocate<1>(&alloc, mem, 1);
  73. // Verify that the deallocation matched.
  74. EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, 0)));
  75. }
  76. class Fixture : public ::testing::Test {
  77. using Alloc = std::allocator<std::string>;
  78. public:
  79. Fixture() { ptr_ = std::allocator_traits<Alloc>::allocate(*alloc(), 1); }
  80. ~Fixture() override {
  81. std::allocator_traits<Alloc>::destroy(*alloc(), ptr_);
  82. std::allocator_traits<Alloc>::deallocate(*alloc(), ptr_, 1);
  83. }
  84. std::string* ptr() { return ptr_; }
  85. Alloc* alloc() { return &alloc_; }
  86. private:
  87. Alloc alloc_;
  88. std::string* ptr_;
  89. };
  90. TEST_F(Fixture, ConstructNoArgs) {
  91. ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple());
  92. EXPECT_EQ(*ptr(), "");
  93. }
  94. TEST_F(Fixture, ConstructOneArg) {
  95. ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple("abcde"));
  96. EXPECT_EQ(*ptr(), "abcde");
  97. }
  98. TEST_F(Fixture, ConstructTwoArg) {
  99. ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple(5, 'a'));
  100. EXPECT_EQ(*ptr(), "aaaaa");
  101. }
  102. TEST(PairArgs, NoArgs) {
  103. EXPECT_THAT(PairArgs(),
  104. Pair(std::forward_as_tuple(), std::forward_as_tuple()));
  105. }
  106. TEST(PairArgs, TwoArgs) {
  107. EXPECT_EQ(
  108. std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
  109. PairArgs(1, 'A'));
  110. }
  111. TEST(PairArgs, Pair) {
  112. EXPECT_EQ(
  113. std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
  114. PairArgs(std::make_pair(1, 'A')));
  115. }
  116. TEST(PairArgs, Piecewise) {
  117. EXPECT_EQ(
  118. std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
  119. PairArgs(std::piecewise_construct, std::forward_as_tuple(1),
  120. std::forward_as_tuple('A')));
  121. }
  122. TEST(WithConstructed, Simple) {
  123. EXPECT_EQ(1, WithConstructed<absl::string_view>(
  124. std::make_tuple(std::string("a")),
  125. [](absl::string_view str) { return str.size(); }));
  126. }
  127. template <class F, class Arg>
  128. decltype(DecomposeValue(std::declval<F>(), std::declval<Arg>()))
  129. DecomposeValueImpl(int, F&& f, Arg&& arg) {
  130. return DecomposeValue(std::forward<F>(f), std::forward<Arg>(arg));
  131. }
  132. template <class F, class Arg>
  133. const char* DecomposeValueImpl(char, F&& f, Arg&& arg) {
  134. return "not decomposable";
  135. }
  136. template <class F, class Arg>
  137. decltype(DecomposeValueImpl(0, std::declval<F>(), std::declval<Arg>()))
  138. TryDecomposeValue(F&& f, Arg&& arg) {
  139. return DecomposeValueImpl(0, std::forward<F>(f), std::forward<Arg>(arg));
  140. }
  141. TEST(DecomposeValue, Decomposable) {
  142. auto f = [](const int& x, int&& y) { // NOLINT
  143. EXPECT_EQ(&x, &y);
  144. EXPECT_EQ(42, x);
  145. return 'A';
  146. };
  147. EXPECT_EQ('A', TryDecomposeValue(f, 42));
  148. }
  149. TEST(DecomposeValue, NotDecomposable) {
  150. auto f = [](void*) {
  151. ADD_FAILURE() << "Must not be called";
  152. return 'A';
  153. };
  154. EXPECT_STREQ("not decomposable", TryDecomposeValue(f, 42));
  155. }
  156. template <class F, class... Args>
  157. decltype(DecomposePair(std::declval<F>(), std::declval<Args>()...))
  158. DecomposePairImpl(int, F&& f, Args&&... args) {
  159. return DecomposePair(std::forward<F>(f), std::forward<Args>(args)...);
  160. }
  161. template <class F, class... Args>
  162. const char* DecomposePairImpl(char, F&& f, Args&&... args) {
  163. return "not decomposable";
  164. }
  165. template <class F, class... Args>
  166. decltype(DecomposePairImpl(0, std::declval<F>(), std::declval<Args>()...))
  167. TryDecomposePair(F&& f, Args&&... args) {
  168. return DecomposePairImpl(0, std::forward<F>(f), std::forward<Args>(args)...);
  169. }
  170. TEST(DecomposePair, Decomposable) {
  171. auto f = [](const int& x, // NOLINT
  172. std::piecewise_construct_t, std::tuple<int&&> k,
  173. std::tuple<double>&& v) {
  174. EXPECT_EQ(&x, &std::get<0>(k));
  175. EXPECT_EQ(42, x);
  176. EXPECT_EQ(0.5, std::get<0>(v));
  177. return 'A';
  178. };
  179. EXPECT_EQ('A', TryDecomposePair(f, 42, 0.5));
  180. EXPECT_EQ('A', TryDecomposePair(f, std::make_pair(42, 0.5)));
  181. EXPECT_EQ('A', TryDecomposePair(f, std::piecewise_construct,
  182. std::make_tuple(42), std::make_tuple(0.5)));
  183. }
  184. TEST(DecomposePair, NotDecomposable) {
  185. auto f = [](...) {
  186. ADD_FAILURE() << "Must not be called";
  187. return 'A';
  188. };
  189. EXPECT_STREQ("not decomposable",
  190. TryDecomposePair(f));
  191. EXPECT_STREQ("not decomposable",
  192. TryDecomposePair(f, std::piecewise_construct, std::make_tuple(),
  193. std::make_tuple(0.5)));
  194. }
  195. TEST(MapSlotPolicy, ConstKeyAndValue) {
  196. using slot_policy = map_slot_policy<const CopyableMovableInstance,
  197. const CopyableMovableInstance>;
  198. using slot_type = typename slot_policy::slot_type;
  199. union Slots {
  200. Slots() {}
  201. ~Slots() {}
  202. slot_type slots[100];
  203. } slots;
  204. std::allocator<
  205. std::pair<const CopyableMovableInstance, const CopyableMovableInstance>>
  206. alloc;
  207. InstanceTracker tracker;
  208. slot_policy::construct(&alloc, &slots.slots[0], CopyableMovableInstance(1),
  209. CopyableMovableInstance(1));
  210. for (int i = 0; i < 99; ++i) {
  211. slot_policy::transfer(&alloc, &slots.slots[i + 1], &slots.slots[i]);
  212. }
  213. slot_policy::destroy(&alloc, &slots.slots[99]);
  214. EXPECT_EQ(tracker.copies(), 0);
  215. }
  216. } // namespace
  217. } // namespace container_internal
  218. ABSL_NAMESPACE_END
  219. } // namespace absl