container_memory_test.cc
Go to the documentation of this file.
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 
16 
17 #include <cstdint>
18 #include <tuple>
19 #include <utility>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
24 
25 namespace absl {
26 namespace container_internal {
27 namespace {
28 
29 using ::testing::Pair;
30 
31 TEST(Memory, AlignmentLargerThanBase) {
32  std::allocator<int8_t> alloc;
33  void* mem = Allocate<2>(&alloc, 3);
34  EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
35  memcpy(mem, "abc", 3);
36  Deallocate<2>(&alloc, mem, 3);
37 }
38 
39 TEST(Memory, AlignmentSmallerThanBase) {
40  std::allocator<int64_t> alloc;
41  void* mem = Allocate<2>(&alloc, 3);
42  EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
43  memcpy(mem, "abc", 3);
44  Deallocate<2>(&alloc, mem, 3);
45 }
46 
47 class Fixture : public ::testing::Test {
48  using Alloc = std::allocator<std::string>;
49 
50  public:
51  Fixture() { ptr_ = std::allocator_traits<Alloc>::allocate(*alloc(), 1); }
52  ~Fixture() override {
54  std::allocator_traits<Alloc>::deallocate(*alloc(), ptr_, 1);
55  }
56  std::string* ptr() { return ptr_; }
57  Alloc* alloc() { return &alloc_; }
58 
59  private:
60  Alloc alloc_;
61  std::string* ptr_;
62 };
63 
64 TEST_F(Fixture, ConstructNoArgs) {
65  ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple());
66  EXPECT_EQ(*ptr(), "");
67 }
68 
69 TEST_F(Fixture, ConstructOneArg) {
70  ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple("abcde"));
71  EXPECT_EQ(*ptr(), "abcde");
72 }
73 
74 TEST_F(Fixture, ConstructTwoArg) {
75  ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple(5, 'a'));
76  EXPECT_EQ(*ptr(), "aaaaa");
77 }
78 
79 TEST(PairArgs, NoArgs) {
80  EXPECT_THAT(PairArgs(),
81  Pair(std::forward_as_tuple(), std::forward_as_tuple()));
82 }
83 
84 TEST(PairArgs, TwoArgs) {
85  EXPECT_EQ(
86  std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
87  PairArgs(1, 'A'));
88 }
89 
90 TEST(PairArgs, Pair) {
91  EXPECT_EQ(
92  std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
93  PairArgs(std::make_pair(1, 'A')));
94 }
95 
96 TEST(PairArgs, Piecewise) {
97  EXPECT_EQ(
98  std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
99  PairArgs(std::piecewise_construct, std::forward_as_tuple(1),
100  std::forward_as_tuple('A')));
101 }
102 
103 TEST(WithConstructed, Simple) {
104  EXPECT_EQ(1, WithConstructed<absl::string_view>(
105  std::make_tuple(std::string("a")),
106  [](absl::string_view str) { return str.size(); }));
107 }
108 
109 template <class F, class Arg>
110 decltype(DecomposeValue(std::declval<F>(), std::declval<Arg>()))
111 DecomposeValueImpl(int, F&& f, Arg&& arg) {
112  return DecomposeValue(std::forward<F>(f), std::forward<Arg>(arg));
113 }
114 
115 template <class F, class Arg>
116 const char* DecomposeValueImpl(char, F&& f, Arg&& arg) {
117  return "not decomposable";
118 }
119 
120 template <class F, class Arg>
121 decltype(DecomposeValueImpl(0, std::declval<F>(), std::declval<Arg>()))
122 TryDecomposeValue(F&& f, Arg&& arg) {
123  return DecomposeValueImpl(0, std::forward<F>(f), std::forward<Arg>(arg));
124 }
125 
126 TEST(DecomposeValue, Decomposable) {
127  auto f = [](const int& x, int&& y) {
128  EXPECT_EQ(&x, &y);
129  EXPECT_EQ(42, x);
130  return 'A';
131  };
132  EXPECT_EQ('A', TryDecomposeValue(f, 42));
133 }
134 
135 TEST(DecomposeValue, NotDecomposable) {
136  auto f = [](void*) {
137  ADD_FAILURE() << "Must not be called";
138  return 'A';
139  };
140  EXPECT_STREQ("not decomposable", TryDecomposeValue(f, 42));
141 }
142 
143 template <class F, class... Args>
144 decltype(DecomposePair(std::declval<F>(), std::declval<Args>()...))
145 DecomposePairImpl(int, F&& f, Args&&... args) {
146  return DecomposePair(std::forward<F>(f), std::forward<Args>(args)...);
147 }
148 
149 template <class F, class... Args>
150 const char* DecomposePairImpl(char, F&& f, Args&&... args) {
151  return "not decomposable";
152 }
153 
154 template <class F, class... Args>
155 decltype(DecomposePairImpl(0, std::declval<F>(), std::declval<Args>()...))
156 TryDecomposePair(F&& f, Args&&... args) {
157  return DecomposePairImpl(0, std::forward<F>(f), std::forward<Args>(args)...);
158 }
159 
160 TEST(DecomposePair, Decomposable) {
161  auto f = [](const int& x, std::piecewise_construct_t, std::tuple<int&&> k,
162  std::tuple<double>&& v) {
163  EXPECT_EQ(&x, &std::get<0>(k));
164  EXPECT_EQ(42, x);
165  EXPECT_EQ(0.5, std::get<0>(v));
166  return 'A';
167  };
168  EXPECT_EQ('A', TryDecomposePair(f, 42, 0.5));
169  EXPECT_EQ('A', TryDecomposePair(f, std::make_pair(42, 0.5)));
170  EXPECT_EQ('A', TryDecomposePair(f, std::piecewise_construct,
171  std::make_tuple(42), std::make_tuple(0.5)));
172 }
173 
174 TEST(DecomposePair, NotDecomposable) {
175  auto f = [](...) {
176  ADD_FAILURE() << "Must not be called";
177  return 'A';
178  };
179  EXPECT_STREQ("not decomposable",
180  TryDecomposePair(f));
181  EXPECT_STREQ("not decomposable",
182  TryDecomposePair(f, std::piecewise_construct, std::make_tuple(),
183  std::make_tuple(0.5)));
184 }
185 
186 } // namespace
187 } // namespace container_internal
188 } // namespace absl
int v
Definition: variant_test.cc:81
std::string * ptr_
void ConstructFromTuple(Alloc *alloc, T *ptr, Tuple &&t)
TEST(NotificationTest, SanityTest)
Alloc alloc_
decltype(std::declval< F >()(std::declval< const Arg & >(), std::declval< Arg >())) DecomposeValue(F &&f, Arg &&arg)
Definition: algorithm.h:29
constexpr size_type size() const noexcept
Definition: string_view.h:260
static std::function< void(void *, Slot *)> destroy
char * ptr
std::pair< std::tuple<>, std::tuple<> > PairArgs()
TEST_F(GraphCyclesTest, NoCycle)
auto DecomposePair(F &&f, Args &&...args) -> decltype(memory_internal::DecomposePairImpl(std::forward< F >(f), PairArgs(std::forward< Args >(args)...)))
void * arg
Definition: mutex.cc:292
decltype(std::declval< F >()(std::declval< T >())) WithConstructed(Tuple &&t, F &&f)
std::allocator< int > alloc
decltype(std::declval< F >()(std::declval< const K & >(), std::piecewise_construct, std::declval< std::tuple< K >>(), std::declval< V >())) DecomposePairImpl(F &&f, std::pair< std::tuple< K >, V > p)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:56