compressed_tuple_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 <memory>
18 #include <string>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/memory/memory.h"
23 #include "absl/utility/utility.h"
24 
25 namespace absl {
26 namespace container_internal {
27 namespace {
28 
29 enum class CallType { kConstRef, kConstMove };
30 
31 template <int>
32 struct Empty {
33  constexpr CallType value() const& { return CallType::kConstRef; }
34  constexpr CallType value() const&& { return CallType::kConstMove; }
35 };
36 
37 template <typename T>
38 struct NotEmpty {
39  T value;
40 };
41 
42 template <typename T, typename U>
43 struct TwoValues {
44  T value1;
45  U value2;
46 };
47 
48 TEST(CompressedTupleTest, Sizeof) {
49  EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int>));
50  EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>>));
51  EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>, Empty<1>>));
52  EXPECT_EQ(sizeof(int),
53  sizeof(CompressedTuple<int, Empty<0>, Empty<1>, Empty<2>>));
54 
55  EXPECT_EQ(sizeof(TwoValues<int, double>),
56  sizeof(CompressedTuple<int, NotEmpty<double>>));
57  EXPECT_EQ(sizeof(TwoValues<int, double>),
58  sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>>));
59  EXPECT_EQ(sizeof(TwoValues<int, double>),
60  sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>, Empty<1>>));
61 }
62 
63 TEST(CompressedTupleTest, Access) {
64  struct S {
65  std::string x;
66  };
67  CompressedTuple<int, Empty<0>, S> x(7, {}, S{"ABC"});
68  EXPECT_EQ(sizeof(x), sizeof(TwoValues<int, S>));
69  EXPECT_EQ(7, x.get<0>());
70  EXPECT_EQ("ABC", x.get<2>().x);
71 }
72 
73 TEST(CompressedTupleTest, NonClasses) {
74  CompressedTuple<int, const char*> x(7, "ABC");
75  EXPECT_EQ(7, x.get<0>());
76  EXPECT_STREQ("ABC", x.get<1>());
77 }
78 
79 TEST(CompressedTupleTest, MixClassAndNonClass) {
80  CompressedTuple<int, const char*, Empty<0>, NotEmpty<double>> x(7, "ABC", {},
81  {1.25});
82  struct Mock {
83  int v;
84  const char* p;
85  double d;
86  };
87  EXPECT_EQ(sizeof(x), sizeof(Mock));
88  EXPECT_EQ(7, x.get<0>());
89  EXPECT_STREQ("ABC", x.get<1>());
90  EXPECT_EQ(1.25, x.get<3>().value);
91 }
92 
93 TEST(CompressedTupleTest, Nested) {
94  CompressedTuple<int, CompressedTuple<int>,
95  CompressedTuple<int, CompressedTuple<int>>>
96  x(1, CompressedTuple<int>(2),
97  CompressedTuple<int, CompressedTuple<int>>(3, CompressedTuple<int>(4)));
98  EXPECT_EQ(1, x.get<0>());
99  EXPECT_EQ(2, x.get<1>().get<0>());
100  EXPECT_EQ(3, x.get<2>().get<0>());
101  EXPECT_EQ(4, x.get<2>().get<1>().get<0>());
102 
103  CompressedTuple<Empty<0>, Empty<0>,
104  CompressedTuple<Empty<0>, CompressedTuple<Empty<0>>>>
105  y;
106  std::set<Empty<0>*> empties{&y.get<0>(), &y.get<1>(), &y.get<2>().get<0>(),
107  &y.get<2>().get<1>().get<0>()};
108 #ifdef _MSC_VER
109  // MSVC has a bug where many instances of the same base class are layed out in
110  // the same address when using __declspec(empty_bases).
111  // This will be fixed in a future version of MSVC.
112  int expected = 1;
113 #else
114  int expected = 4;
115 #endif
116  EXPECT_EQ(expected, sizeof(y));
117  EXPECT_EQ(expected, empties.size());
118  EXPECT_EQ(sizeof(y), sizeof(Empty<0>) * empties.size());
119 
120  EXPECT_EQ(4 * sizeof(char),
121  sizeof(CompressedTuple<CompressedTuple<char, char>,
122  CompressedTuple<char, char>>));
123  EXPECT_TRUE(
124  (std::is_empty<CompressedTuple<CompressedTuple<Empty<0>>,
125  CompressedTuple<Empty<1>>>>::value));
126 }
127 
128 TEST(CompressedTupleTest, Reference) {
129  int i = 7;
130  std::string s = "Very long std::string that goes in the heap";
131  CompressedTuple<int, int&, std::string, std::string&> x(i, i, s, s);
132 
133  // Sanity check. We should have not moved from `s`
134  EXPECT_EQ(s, "Very long std::string that goes in the heap");
135 
136  EXPECT_EQ(x.get<0>(), x.get<1>());
137  EXPECT_NE(&x.get<0>(), &x.get<1>());
138  EXPECT_EQ(&x.get<1>(), &i);
139 
140  EXPECT_EQ(x.get<2>(), x.get<3>());
141  EXPECT_NE(&x.get<2>(), &x.get<3>());
142  EXPECT_EQ(&x.get<3>(), &s);
143 }
144 
145 TEST(CompressedTupleTest, NoElements) {
146  CompressedTuple<> x;
147  static_cast<void>(x); // Silence -Wunused-variable.
148  EXPECT_TRUE(std::is_empty<CompressedTuple<>>::value);
149 }
150 
151 TEST(CompressedTupleTest, MoveOnlyElements) {
152  CompressedTuple<std::unique_ptr<std::string>> str_tup(
153  absl::make_unique<std::string>("str"));
154 
155  CompressedTuple<CompressedTuple<std::unique_ptr<std::string>>,
156  std::unique_ptr<int>>
157  x(std::move(str_tup), absl::make_unique<int>(5));
158 
159  EXPECT_EQ(*x.get<0>().get<0>(), "str");
160  EXPECT_EQ(*x.get<1>(), 5);
161 
162  std::unique_ptr<std::string> x0 = std::move(x.get<0>()).get<0>();
163  std::unique_ptr<int> x1 = std::move(x).get<1>();
164 
165  EXPECT_EQ(*x0, "str");
166  EXPECT_EQ(*x1, 5);
167 }
168 
169 TEST(CompressedTupleTest, Constexpr) {
170  constexpr CompressedTuple<int, double, CompressedTuple<int>, Empty<0>> x(
171  7, 1.25, CompressedTuple<int>(5), {});
172  constexpr int x0 = x.get<0>();
173  constexpr double x1 = x.get<1>();
174  constexpr int x2 = x.get<2>().get<0>();
175  constexpr CallType x3 = x.get<3>().value();
176 
177  EXPECT_EQ(x0, 7);
178  EXPECT_EQ(x1, 1.25);
179  EXPECT_EQ(x2, 5);
180  EXPECT_EQ(x3, CallType::kConstRef);
181 
182 #if defined(__clang__)
183  // An apparent bug in earlier versions of gcc claims these are ambiguous.
184  constexpr int x2m = absl::move(x.get<2>()).get<0>();
185  constexpr CallType x3m = absl::move(x).get<3>().value();
186  EXPECT_EQ(x2m, 5);
187  EXPECT_EQ(x3m, CallType::kConstMove);
188 #endif
189 }
190 
191 #if defined(__clang__) || defined(__GNUC__)
192 TEST(CompressedTupleTest, EmptyFinalClass) {
193  struct S final {
194  int f() const { return 5; }
195  };
196  CompressedTuple<S> x;
197  EXPECT_EQ(x.get<0>().f(), 5);
198 }
199 #endif
200 
201 } // namespace
202 } // namespace container_internal
203 } // namespace absl
int v
Definition: variant_test.cc:81
TEST(NotificationTest, SanityTest)
size_t Sizeof(FlagOpFn op)
Definition: algorithm.h:29
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35