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