20 #include "gmock/gmock.h" 21 #include "gtest/gtest.h" 26 namespace container_internal {
33 constexpr
CallType value()
const& {
return CallType::kConstRef; }
34 constexpr
CallType value()
const&& {
return CallType::kConstMove; }
42 template <
typename T,
typename U>
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>>));
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>>));
63 TEST(CompressedTupleTest, Access) {
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);
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>());
79 TEST(CompressedTupleTest, MixClassAndNonClass) {
80 CompressedTuple<int, const char*, Empty<0>, NotEmpty<double>> x(7,
"ABC", {},
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);
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>());
103 CompressedTuple<Empty<0>, Empty<0>,
104 CompressedTuple<Empty<0>, CompressedTuple<Empty<0>>>>
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>()};
116 EXPECT_EQ(expected,
sizeof(y));
117 EXPECT_EQ(expected, empties.size());
118 EXPECT_EQ(
sizeof(y),
sizeof(Empty<0>) * empties.size());
120 EXPECT_EQ(4 *
sizeof(
char),
121 sizeof(CompressedTuple<CompressedTuple<char, char>,
122 CompressedTuple<char, char>>));
124 (std::is_empty<CompressedTuple<CompressedTuple<Empty<0>>,
125 CompressedTuple<Empty<1>>>>::
value));
128 TEST(CompressedTupleTest, Reference) {
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);
134 EXPECT_EQ(s,
"Very long std::string that goes in the heap");
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);
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);
145 TEST(CompressedTupleTest, NoElements) {
147 static_cast<void>(x);
148 EXPECT_TRUE(std::is_empty<CompressedTuple<>>::
value);
151 TEST(CompressedTupleTest, MoveOnlyElements) {
152 CompressedTuple<std::unique_ptr<std::string>> str_tup(
153 absl::make_unique<std::string>(
"str"));
155 CompressedTuple<CompressedTuple<std::unique_ptr<std::string>>,
156 std::unique_ptr<int>>
157 x(
std::move(str_tup), absl::make_unique<int>(5));
159 EXPECT_EQ(*x.get<0>().get<0>(),
"str");
160 EXPECT_EQ(*x.get<1>(), 5);
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>();
165 EXPECT_EQ(*x0,
"str");
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();
180 EXPECT_EQ(x3, CallType::kConstRef);
182 #if defined(__clang__) 184 constexpr
int x2m =
absl::move(x.get<2>()).get<0>();
187 EXPECT_EQ(x3m, CallType::kConstMove);
191 #if defined(__clang__) || defined(__GNUC__) 192 TEST(CompressedTupleTest, EmptyFinalClass) {
194 int f()
const {
return 5; }
196 CompressedTuple<S> x;
197 EXPECT_EQ(x.get<0>().f(), 5);
TEST(NotificationTest, SanityTest)
size_t Sizeof(FlagOpFn op)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept