• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "absl/container/internal/compressed_tuple.h"
16 
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <type_traits>
21 #include <utility>
22 #include <vector>
23 
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "absl/container/internal/test_instance_tracker.h"
27 #include "absl/memory/memory.h"
28 #include "absl/types/any.h"
29 #include "absl/types/optional.h"
30 #include "absl/utility/utility.h"
31 
32 // These are declared at global scope purely so that error messages
33 // are smaller and easier to understand.
34 enum class CallType { kConstRef, kConstMove };
35 
36 template <int>
37 struct Empty {
valueEmpty38   constexpr CallType value() const& { return CallType::kConstRef; }
valueEmpty39   constexpr CallType value() const&& { return CallType::kConstMove; }
40 };
41 
42 template <typename T>
43 struct NotEmpty {
44   T value;
45 };
46 
47 template <typename T, typename U>
48 struct TwoValues {
49   T value1;
50   U value2;
51 };
52 
53 
54 namespace absl {
55 ABSL_NAMESPACE_BEGIN
56 namespace container_internal {
57 namespace {
58 
59 using absl::test_internal::CopyableMovableInstance;
60 using absl::test_internal::InstanceTracker;
61 using ::testing::Each;
62 
TEST(CompressedTupleTest,Sizeof)63 TEST(CompressedTupleTest, Sizeof) {
64   EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int>));
65   EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>>));
66   EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>, Empty<1>>));
67   EXPECT_EQ(sizeof(int),
68             sizeof(CompressedTuple<int, Empty<0>, Empty<1>, Empty<2>>));
69 
70   EXPECT_EQ(sizeof(TwoValues<int, double>),
71             sizeof(CompressedTuple<int, NotEmpty<double>>));
72   EXPECT_EQ(sizeof(TwoValues<int, double>),
73             sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>>));
74   EXPECT_EQ(sizeof(TwoValues<int, double>),
75             sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>, Empty<1>>));
76 }
77 
TEST(CompressedTupleTest,PointerToEmpty)78 TEST(CompressedTupleTest, PointerToEmpty) {
79   auto to_void_ptrs = [](const auto&... objs) {
80     return std::vector<const void*>{static_cast<const void*>(&objs)...};
81   };
82   {
83     using Tuple = CompressedTuple<int, Empty<0>>;
84     EXPECT_EQ(sizeof(int), sizeof(Tuple));
85     Tuple t;
86     EXPECT_THAT(to_void_ptrs(t.get<1>()), Each(&t));
87   }
88   {
89     using Tuple = CompressedTuple<int, Empty<0>, Empty<1>>;
90     EXPECT_EQ(sizeof(int), sizeof(Tuple));
91     Tuple t;
92     EXPECT_THAT(to_void_ptrs(t.get<1>(), t.get<2>()), Each(&t));
93   }
94   {
95     using Tuple = CompressedTuple<int, Empty<0>, Empty<1>, Empty<2>>;
96     EXPECT_EQ(sizeof(int), sizeof(Tuple));
97     Tuple t;
98     EXPECT_THAT(to_void_ptrs(t.get<1>(), t.get<2>(), t.get<3>()), Each(&t));
99   }
100 }
101 
TEST(CompressedTupleTest,OneMoveOnRValueConstructionTemp)102 TEST(CompressedTupleTest, OneMoveOnRValueConstructionTemp) {
103   InstanceTracker tracker;
104   CompressedTuple<CopyableMovableInstance> x1(CopyableMovableInstance(1));
105   EXPECT_EQ(tracker.instances(), 1);
106   EXPECT_EQ(tracker.copies(), 0);
107   EXPECT_LE(tracker.moves(), 1);
108   EXPECT_EQ(x1.get<0>().value(), 1);
109 }
110 
TEST(CompressedTupleTest,OneMoveOnRValueConstructionMove)111 TEST(CompressedTupleTest, OneMoveOnRValueConstructionMove) {
112   InstanceTracker tracker;
113 
114   CopyableMovableInstance i1(1);
115   CompressedTuple<CopyableMovableInstance> x1(std::move(i1));
116   EXPECT_EQ(tracker.instances(), 2);
117   EXPECT_EQ(tracker.copies(), 0);
118   EXPECT_LE(tracker.moves(), 1);
119   EXPECT_EQ(x1.get<0>().value(), 1);
120 }
121 
TEST(CompressedTupleTest,OneMoveOnRValueConstructionMixedTypes)122 TEST(CompressedTupleTest, OneMoveOnRValueConstructionMixedTypes) {
123   InstanceTracker tracker;
124   CopyableMovableInstance i1(1);
125   CopyableMovableInstance i2(2);
126   Empty<0> empty;
127   CompressedTuple<CopyableMovableInstance, CopyableMovableInstance&, Empty<0>>
128       x1(std::move(i1), i2, empty);
129   EXPECT_EQ(x1.get<0>().value(), 1);
130   EXPECT_EQ(x1.get<1>().value(), 2);
131   EXPECT_EQ(tracker.copies(), 0);
132   EXPECT_EQ(tracker.moves(), 1);
133 }
134 
135 struct IncompleteType;
136 CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>>
MakeWithIncomplete(CopyableMovableInstance i1,IncompleteType & t,Empty<0> empty)137 MakeWithIncomplete(CopyableMovableInstance i1,
138                    IncompleteType& t,  // NOLINT
139                    Empty<0> empty) {
140   return CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>>{
141       std::move(i1), t, empty};
142 }
143 
144 struct IncompleteType {};
TEST(CompressedTupleTest,OneMoveOnRValueConstructionWithIncompleteType)145 TEST(CompressedTupleTest, OneMoveOnRValueConstructionWithIncompleteType) {
146   InstanceTracker tracker;
147   CopyableMovableInstance i1(1);
148   Empty<0> empty;
149   struct DerivedType : IncompleteType {int value = 0;};
150   DerivedType fd;
151   fd.value = 7;
152 
153   CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>> x1 =
154       MakeWithIncomplete(std::move(i1), fd, empty);
155 
156   EXPECT_EQ(x1.get<0>().value(), 1);
157   EXPECT_EQ(static_cast<DerivedType&>(x1.get<1>()).value, 7);
158 
159   EXPECT_EQ(tracker.copies(), 0);
160   EXPECT_EQ(tracker.moves(), 2);
161 }
162 
TEST(CompressedTupleTest,OneMoveOnRValueConstructionMixedTypes_BraceInitPoisonPillExpected)163 TEST(CompressedTupleTest,
164      OneMoveOnRValueConstructionMixedTypes_BraceInitPoisonPillExpected) {
165   InstanceTracker tracker;
166   CopyableMovableInstance i1(1);
167   CopyableMovableInstance i2(2);
168   CompressedTuple<CopyableMovableInstance, CopyableMovableInstance&, Empty<0>>
169       x1(std::move(i1), i2, {});  // NOLINT
170   EXPECT_EQ(x1.get<0>().value(), 1);
171   EXPECT_EQ(x1.get<1>().value(), 2);
172   EXPECT_EQ(tracker.instances(), 3);
173   // We are forced into the `const Ts&...` constructor (invoking copies)
174   // because we need it to deduce the type of `{}`.
175   // std::tuple also has this behavior.
176   // Note, this test is proof that this is expected behavior, but it is not
177   // _desired_ behavior.
178   EXPECT_EQ(tracker.copies(), 1);
179   EXPECT_EQ(tracker.moves(), 0);
180 }
181 
TEST(CompressedTupleTest,OneCopyOnLValueConstruction)182 TEST(CompressedTupleTest, OneCopyOnLValueConstruction) {
183   InstanceTracker tracker;
184   CopyableMovableInstance i1(1);
185 
186   CompressedTuple<CopyableMovableInstance> x1(i1);
187   EXPECT_EQ(tracker.copies(), 1);
188   EXPECT_EQ(tracker.moves(), 0);
189 
190   tracker.ResetCopiesMovesSwaps();
191 
192   CopyableMovableInstance i2(2);
193   const CopyableMovableInstance& i2_ref = i2;
194   CompressedTuple<CopyableMovableInstance> x2(i2_ref);
195   EXPECT_EQ(tracker.copies(), 1);
196   EXPECT_EQ(tracker.moves(), 0);
197 }
198 
TEST(CompressedTupleTest,OneMoveOnRValueAccess)199 TEST(CompressedTupleTest, OneMoveOnRValueAccess) {
200   InstanceTracker tracker;
201   CopyableMovableInstance i1(1);
202   CompressedTuple<CopyableMovableInstance> x(std::move(i1));
203   tracker.ResetCopiesMovesSwaps();
204 
205   CopyableMovableInstance i2 = std::move(x).get<0>();
206   EXPECT_EQ(tracker.copies(), 0);
207   EXPECT_EQ(tracker.moves(), 1);
208 }
209 
TEST(CompressedTupleTest,OneCopyOnLValueAccess)210 TEST(CompressedTupleTest, OneCopyOnLValueAccess) {
211   InstanceTracker tracker;
212 
213   CompressedTuple<CopyableMovableInstance> x(CopyableMovableInstance(0));
214   EXPECT_EQ(tracker.copies(), 0);
215   EXPECT_EQ(tracker.moves(), 1);
216 
217   CopyableMovableInstance t = x.get<0>();
218   EXPECT_EQ(tracker.copies(), 1);
219   EXPECT_EQ(tracker.moves(), 1);
220 }
221 
TEST(CompressedTupleTest,ZeroCopyOnRefAccess)222 TEST(CompressedTupleTest, ZeroCopyOnRefAccess) {
223   InstanceTracker tracker;
224 
225   CompressedTuple<CopyableMovableInstance> x(CopyableMovableInstance(0));
226   EXPECT_EQ(tracker.copies(), 0);
227   EXPECT_EQ(tracker.moves(), 1);
228 
229   CopyableMovableInstance& t1 = x.get<0>();
230   const CopyableMovableInstance& t2 = x.get<0>();
231   EXPECT_EQ(tracker.copies(), 0);
232   EXPECT_EQ(tracker.moves(), 1);
233   EXPECT_EQ(t1.value(), 0);
234   EXPECT_EQ(t2.value(), 0);
235 }
236 
TEST(CompressedTupleTest,Access)237 TEST(CompressedTupleTest, Access) {
238   struct S {
239     std::string x;
240   };
241   CompressedTuple<int, Empty<0>, S> x(7, {}, S{"ABC"});
242   EXPECT_EQ(sizeof(x), sizeof(TwoValues<int, S>));
243   EXPECT_EQ(7, x.get<0>());
244   EXPECT_EQ("ABC", x.get<2>().x);
245 }
246 
TEST(CompressedTupleTest,NonClasses)247 TEST(CompressedTupleTest, NonClasses) {
248   CompressedTuple<int, const char*> x(7, "ABC");
249   EXPECT_EQ(7, x.get<0>());
250   EXPECT_STREQ("ABC", x.get<1>());
251 }
252 
TEST(CompressedTupleTest,MixClassAndNonClass)253 TEST(CompressedTupleTest, MixClassAndNonClass) {
254   CompressedTuple<int, const char*, Empty<0>, NotEmpty<double>> x(7, "ABC", {},
255                                                                   {1.25});
256   struct Mock {
257     int v;
258     const char* p;
259     double d;
260   };
261   EXPECT_EQ(sizeof(x), sizeof(Mock));
262   EXPECT_EQ(7, x.get<0>());
263   EXPECT_STREQ("ABC", x.get<1>());
264   EXPECT_EQ(1.25, x.get<3>().value);
265 }
266 
TEST(CompressedTupleTest,Nested)267 TEST(CompressedTupleTest, Nested) {
268   CompressedTuple<int, CompressedTuple<int>,
269                   CompressedTuple<int, CompressedTuple<int>>>
270       x(1, CompressedTuple<int>(2),
271         CompressedTuple<int, CompressedTuple<int>>(3, CompressedTuple<int>(4)));
272   EXPECT_EQ(1, x.get<0>());
273   EXPECT_EQ(2, x.get<1>().get<0>());
274   EXPECT_EQ(3, x.get<2>().get<0>());
275   EXPECT_EQ(4, x.get<2>().get<1>().get<0>());
276 
277   CompressedTuple<Empty<0>, Empty<0>,
278                   CompressedTuple<Empty<0>, CompressedTuple<Empty<0>>>>
279       y;
280   std::set<Empty<0>*> empties{&y.get<0>(), &y.get<1>(), &y.get<2>().get<0>(),
281                               &y.get<2>().get<1>().get<0>()};
282 #ifdef _MSC_VER
283   // MSVC has a bug where many instances of the same base class are layed out in
284   // the same address when using __declspec(empty_bases).
285   // This will be fixed in a future version of MSVC.
286   int expected = 1;
287 #else
288   int expected = 4;
289 #endif
290   EXPECT_EQ(expected, sizeof(y));
291   EXPECT_EQ(expected, empties.size());
292   EXPECT_EQ(sizeof(y), sizeof(Empty<0>) * empties.size());
293 
294   EXPECT_EQ(4 * sizeof(char),
295             sizeof(CompressedTuple<CompressedTuple<char, char>,
296                                    CompressedTuple<char, char>>));
297   EXPECT_TRUE((std::is_empty<CompressedTuple<Empty<0>, Empty<1>>>::value));
298 
299   // Make sure everything still works when things are nested.
300   struct CT_Empty : CompressedTuple<Empty<0>> {};
301   CompressedTuple<Empty<0>, CT_Empty> nested_empty;
302   auto contained = nested_empty.get<0>();
303   auto nested = nested_empty.get<1>().get<0>();
304   EXPECT_TRUE((std::is_same<decltype(contained), decltype(nested)>::value));
305 }
306 
TEST(CompressedTupleTest,Reference)307 TEST(CompressedTupleTest, Reference) {
308   int i = 7;
309   std::string s = "Very long string that goes in the heap";
310   CompressedTuple<int, int&, std::string, std::string&> x(i, i, s, s);
311 
312   // Sanity check. We should have not moved from `s`
313   EXPECT_EQ(s, "Very long string that goes in the heap");
314 
315   EXPECT_EQ(x.get<0>(), x.get<1>());
316   EXPECT_NE(&x.get<0>(), &x.get<1>());
317   EXPECT_EQ(&x.get<1>(), &i);
318 
319   EXPECT_EQ(x.get<2>(), x.get<3>());
320   EXPECT_NE(&x.get<2>(), &x.get<3>());
321   EXPECT_EQ(&x.get<3>(), &s);
322 }
323 
TEST(CompressedTupleTest,NoElements)324 TEST(CompressedTupleTest, NoElements) {
325   CompressedTuple<> x;
326   static_cast<void>(x);  // Silence -Wunused-variable.
327   EXPECT_TRUE(std::is_empty<CompressedTuple<>>::value);
328 }
329 
TEST(CompressedTupleTest,MoveOnlyElements)330 TEST(CompressedTupleTest, MoveOnlyElements) {
331   CompressedTuple<std::unique_ptr<std::string>> str_tup(
332       absl::make_unique<std::string>("str"));
333 
334   CompressedTuple<CompressedTuple<std::unique_ptr<std::string>>,
335                   std::unique_ptr<int>>
336   x(std::move(str_tup), absl::make_unique<int>(5));
337 
338   EXPECT_EQ(*x.get<0>().get<0>(), "str");
339   EXPECT_EQ(*x.get<1>(), 5);
340 
341   std::unique_ptr<std::string> x0 = std::move(x.get<0>()).get<0>();
342   std::unique_ptr<int> x1 = std::move(x).get<1>();
343 
344   EXPECT_EQ(*x0, "str");
345   EXPECT_EQ(*x1, 5);
346 }
347 
TEST(CompressedTupleTest,MoveConstructionMoveOnlyElements)348 TEST(CompressedTupleTest, MoveConstructionMoveOnlyElements) {
349   CompressedTuple<std::unique_ptr<std::string>> base(
350       absl::make_unique<std::string>("str"));
351   EXPECT_EQ(*base.get<0>(), "str");
352 
353   CompressedTuple<std::unique_ptr<std::string>> copy(std::move(base));
354   EXPECT_EQ(*copy.get<0>(), "str");
355 }
356 
TEST(CompressedTupleTest,AnyElements)357 TEST(CompressedTupleTest, AnyElements) {
358   any a(std::string("str"));
359   CompressedTuple<any, any&> x(any(5), a);
360   EXPECT_EQ(absl::any_cast<int>(x.get<0>()), 5);
361   EXPECT_EQ(absl::any_cast<std::string>(x.get<1>()), "str");
362 
363   a = 0.5f;
364   EXPECT_EQ(absl::any_cast<float>(x.get<1>()), 0.5);
365 }
366 
TEST(CompressedTupleTest,Constexpr)367 TEST(CompressedTupleTest, Constexpr) {
368   struct NonTrivialStruct {
369     constexpr NonTrivialStruct() = default;
370     constexpr int value() const { return v; }
371     int v = 5;
372   };
373   struct TrivialStruct {
374     TrivialStruct() = default;
375     constexpr int value() const { return v; }
376     int v;
377   };
378   constexpr CompressedTuple<int, double, CompressedTuple<int>, Empty<0>> x(
379       7, 1.25, CompressedTuple<int>(5), {});
380   constexpr int x0 = x.get<0>();
381   constexpr double x1 = x.get<1>();
382   constexpr int x2 = x.get<2>().get<0>();
383   constexpr CallType x3 = x.get<3>().value();
384 
385   EXPECT_EQ(x0, 7);
386   EXPECT_EQ(x1, 1.25);
387   EXPECT_EQ(x2, 5);
388   EXPECT_EQ(x3, CallType::kConstRef);
389 
390   constexpr CompressedTuple<Empty<0>, TrivialStruct, int> trivial = {};
391   constexpr CallType trivial0 = trivial.get<0>().value();
392   constexpr int trivial1 = trivial.get<1>().value();
393   constexpr int trivial2 = trivial.get<2>();
394 
395   EXPECT_EQ(trivial0, CallType::kConstRef);
396   EXPECT_EQ(trivial1, 0);
397   EXPECT_EQ(trivial2, 0);
398 
399   constexpr CompressedTuple<Empty<0>, NonTrivialStruct, absl::optional<int>>
400       non_trivial = {};
401   constexpr CallType non_trivial0 = non_trivial.get<0>().value();
402   constexpr int non_trivial1 = non_trivial.get<1>().value();
403   constexpr absl::optional<int> non_trivial2 = non_trivial.get<2>();
404 
405   EXPECT_EQ(non_trivial0, CallType::kConstRef);
406   EXPECT_EQ(non_trivial1, 5);
407   EXPECT_EQ(non_trivial2, absl::nullopt);
408 
409   static constexpr char data[] = "DEF";
410   constexpr CompressedTuple<const char*> z(data);
411   constexpr const char* z1 = z.get<0>();
412   EXPECT_EQ(std::string(z1), std::string(data));
413 
414 #if defined(__clang__)
415   // An apparent bug in earlier versions of gcc claims these are ambiguous.
416   constexpr int x2m = std::move(x.get<2>()).get<0>();
417   constexpr CallType x3m = std::move(x).get<3>().value();
418   EXPECT_EQ(x2m, 5);
419   EXPECT_EQ(x3m, CallType::kConstMove);
420 #endif
421 }
422 
423 #if defined(__clang__) || defined(__GNUC__)
TEST(CompressedTupleTest,EmptyFinalClass)424 TEST(CompressedTupleTest, EmptyFinalClass) {
425   struct S final {
426     int f() const { return 5; }
427   };
428   CompressedTuple<S> x;
429   EXPECT_EQ(x.get<0>().f(), 5);
430 }
431 #endif
432 
433 // TODO(b/214288561): enable this test.
TEST(CompressedTupleTest,DISABLED_NestedEbo)434 TEST(CompressedTupleTest, DISABLED_NestedEbo) {
435   struct Empty1 {};
436   struct Empty2 {};
437   CompressedTuple<Empty1, CompressedTuple<Empty2>, int> x;
438   CompressedTuple<Empty1, Empty2, int> y;
439   // Currently fails with sizeof(x) == 8, sizeof(y) == 4.
440   EXPECT_EQ(sizeof(x), sizeof(y));
441 }
442 
443 }  // namespace
444 }  // namespace container_internal
445 ABSL_NAMESPACE_END
446 }  // namespace absl
447