15 #include "absl/container/internal/container_memory.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "absl/container/internal/test_instance_tracker.h"
26 #include "absl/strings/string_view.h"
30 namespace container_internal {
33 using ::absl::test_internal::CopyableMovableInstance;
34 using ::absl::test_internal::InstanceTracker;
40 TEST(Memory, AlignmentLargerThanBase) {
41 std::allocator<int8_t>
alloc;
48 TEST(Memory, AlignmentSmallerThanBase) {
49 std::allocator<int64_t>
alloc;
56 std::map<std::type_index, int>& AllocationMap() {
57 static auto*
map =
new std::map<std::type_index, int>;
62 struct TypeCountingAllocator {
63 TypeCountingAllocator() =
default;
65 TypeCountingAllocator(
const TypeCountingAllocator<U>&) {}
69 T* allocate(
size_t n,
const void* =
nullptr) {
70 AllocationMap()[
typeid(
T)] += n;
71 return std::allocator<T>().allocate(n);
73 void deallocate(
T* p, std::size_t n) {
74 AllocationMap()[
typeid(
T)] -= n;
75 return std::allocator<T>().deallocate(p, n);
79 TEST(Memory, AllocateDeallocateMatchType) {
80 TypeCountingAllocator<int>
alloc;
90 using Alloc = std::allocator<std::string>;
96 std::allocator_traits<Alloc>::deallocate(*
alloc(),
ptr_, 1);
123 Pair(std::forward_as_tuple(), std::forward_as_tuple()));
128 std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple(
'A')),
134 std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple(
'A')),
140 std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple(
'A')),
141 PairArgs(std::piecewise_construct, std::forward_as_tuple(1),
142 std::forward_as_tuple(
'A')));
146 EXPECT_EQ(1, WithConstructed<absl::string_view>(
151 template <
class F,
class Arg>
153 DecomposeValueImpl(
int, F&& f,
Arg&&
arg) {
157 template <
class F,
class Arg>
158 const char* DecomposeValueImpl(
char, F&& f,
Arg&&
arg) {
159 return "not decomposable";
162 template <
class F,
class Arg>
163 decltype(DecomposeValueImpl(0, std::declval<F>(), std::declval<Arg>()))
164 TryDecomposeValue(F&& f,
Arg&&
arg) {
165 return DecomposeValueImpl(0, std::forward<F>(f), std::forward<Arg>(
arg));
169 auto f = [](
const int&
x,
int&&
y) {
174 EXPECT_EQ(
'A', TryDecomposeValue(f, 42));
182 EXPECT_STREQ(
"not decomposable", TryDecomposeValue(f, 42));
185 template <
class F,
class...
Args>
186 decltype(
DecomposePair(std::declval<F>(), std::declval<Args>()...))
191 template <
class F,
class...
Args>
193 return "not decomposable";
196 template <
class F,
class...
Args>
198 TryDecomposePair(F&& f,
Args&&...
args) {
203 auto f = [](
const int&
x,
204 std::piecewise_construct_t, std::tuple<int&&>
k,
205 std::tuple<double>&&
v) {
211 EXPECT_EQ(
'A', TryDecomposePair(f, 42, 0.5));
212 EXPECT_EQ(
'A', TryDecomposePair(f, std::make_pair(42, 0.5)));
213 EXPECT_EQ(
'A', TryDecomposePair(f, std::piecewise_construct,
223 TryDecomposePair(f));
229 TEST(MapSlotPolicy, ConstKeyAndValue) {
230 using slot_policy = map_slot_policy<
const CopyableMovableInstance,
231 const CopyableMovableInstance>;
232 using slot_type =
typename slot_policy::slot_type;
237 slot_type slots[100];
241 std::pair<const CopyableMovableInstance, const CopyableMovableInstance>>
245 CopyableMovableInstance(1));
246 for (
int i = 0;
i < 99; ++
i) {