1 // Copyright 2022 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/utility/utility.h"
16
17 #include <memory>
18 #include <sstream>
19 #include <string>
20 #include <tuple>
21 #include <type_traits>
22 #include <utility>
23 #include <vector>
24
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 #include "absl/base/attributes.h"
28 #include "absl/memory/memory.h"
29 #include "absl/strings/str_cat.h"
30
31 namespace {
32
33 #ifdef _MSC_VER
34 // Warnings for unused variables in this test are false positives. On other
35 // platforms, they are suppressed by ABSL_ATTRIBUTE_UNUSED, but that doesn't
36 // work on MSVC.
37 // Both the unused variables and the name length warnings are due to calls
38 // to absl::make_index_sequence with very large values, creating very long type
39 // names. The resulting warnings are so long they make build output unreadable.
40 #pragma warning(push)
41 #pragma warning(disable : 4503) // decorated name length exceeded
42 #pragma warning(disable : 4101) // unreferenced local variable
43 #endif // _MSC_VER
44
45 using ::testing::ElementsAre;
46 using ::testing::Pointee;
47 using ::testing::StaticAssertTypeEq;
48
TEST(IntegerSequenceTest,ValueType)49 TEST(IntegerSequenceTest, ValueType) {
50 StaticAssertTypeEq<int, absl::integer_sequence<int>::value_type>();
51 StaticAssertTypeEq<char, absl::integer_sequence<char>::value_type>();
52 }
53
TEST(IntegerSequenceTest,Size)54 TEST(IntegerSequenceTest, Size) {
55 EXPECT_EQ(0, (absl::integer_sequence<int>::size()));
56 EXPECT_EQ(1, (absl::integer_sequence<int, 0>::size()));
57 EXPECT_EQ(1, (absl::integer_sequence<int, 1>::size()));
58 EXPECT_EQ(2, (absl::integer_sequence<int, 1, 2>::size()));
59 EXPECT_EQ(3, (absl::integer_sequence<int, 0, 1, 2>::size()));
60 EXPECT_EQ(3, (absl::integer_sequence<int, -123, 123, 456>::size()));
61 constexpr size_t sz = absl::integer_sequence<int, 0, 1>::size();
62 EXPECT_EQ(2, sz);
63 }
64
TEST(IntegerSequenceTest,MakeIndexSequence)65 TEST(IntegerSequenceTest, MakeIndexSequence) {
66 StaticAssertTypeEq<absl::index_sequence<>, absl::make_index_sequence<0>>();
67 StaticAssertTypeEq<absl::index_sequence<0>, absl::make_index_sequence<1>>();
68 StaticAssertTypeEq<absl::index_sequence<0, 1>,
69 absl::make_index_sequence<2>>();
70 StaticAssertTypeEq<absl::index_sequence<0, 1, 2>,
71 absl::make_index_sequence<3>>();
72 }
73
TEST(IntegerSequenceTest,MakeIntegerSequence)74 TEST(IntegerSequenceTest, MakeIntegerSequence) {
75 StaticAssertTypeEq<absl::integer_sequence<int>,
76 absl::make_integer_sequence<int, 0>>();
77 StaticAssertTypeEq<absl::integer_sequence<int, 0>,
78 absl::make_integer_sequence<int, 1>>();
79 StaticAssertTypeEq<absl::integer_sequence<int, 0, 1>,
80 absl::make_integer_sequence<int, 2>>();
81 StaticAssertTypeEq<absl::integer_sequence<int, 0, 1, 2>,
82 absl::make_integer_sequence<int, 3>>();
83 }
84
85 template <typename... Ts>
86 class Counter {};
87
88 template <size_t... Is>
CountAll(absl::index_sequence<Is...>)89 void CountAll(absl::index_sequence<Is...>) {
90 // We only need an alias here, but instantiate a variable to silence warnings
91 // for unused typedefs in some compilers.
92 ABSL_ATTRIBUTE_UNUSED Counter<absl::make_index_sequence<Is>...> seq;
93 }
94
95 // This test verifies that absl::make_index_sequence can handle large arguments
96 // without blowing up template instantiation stack, going OOM or taking forever
97 // to compile (there is hard 15 minutes limit imposed by forge).
TEST(IntegerSequenceTest,MakeIndexSequencePerformance)98 TEST(IntegerSequenceTest, MakeIndexSequencePerformance) {
99 // O(log N) template instantiations.
100 // We only need an alias here, but instantiate a variable to silence warnings
101 // for unused typedefs in some compilers.
102 ABSL_ATTRIBUTE_UNUSED absl::make_index_sequence<(1 << 16) - 1> seq;
103 // O(N) template instantiations.
104 CountAll(absl::make_index_sequence<(1 << 8) - 1>());
105 }
106
107 template <typename F, typename Tup, size_t... Is>
ApplyFromTupleImpl(F f,const Tup & tup,absl::index_sequence<Is...>)108 auto ApplyFromTupleImpl(F f, const Tup& tup, absl::index_sequence<Is...>)
109 -> decltype(f(std::get<Is>(tup)...)) {
110 return f(std::get<Is>(tup)...);
111 }
112
113 template <typename Tup>
114 using TupIdxSeq = absl::make_index_sequence<std::tuple_size<Tup>::value>;
115
116 template <typename F, typename Tup>
ApplyFromTuple(F f,const Tup & tup)117 auto ApplyFromTuple(F f, const Tup& tup)
118 -> decltype(ApplyFromTupleImpl(f, tup, TupIdxSeq<Tup>{})) {
119 return ApplyFromTupleImpl(f, tup, TupIdxSeq<Tup>{});
120 }
121
122 template <typename T>
Fmt(const T & x)123 std::string Fmt(const T& x) {
124 std::ostringstream os;
125 os << x;
126 return os.str();
127 }
128
129 struct PoorStrCat {
130 template <typename... Args>
operator ()__anon4780e5a00111::PoorStrCat131 std::string operator()(const Args&... args) const {
132 std::string r;
133 for (const auto& e : {Fmt(args)...}) r += e;
134 return r;
135 }
136 };
137
138 template <typename Tup, size_t... Is>
TupStringVecImpl(const Tup & tup,absl::index_sequence<Is...>)139 std::vector<std::string> TupStringVecImpl(const Tup& tup,
140 absl::index_sequence<Is...>) {
141 return {Fmt(std::get<Is>(tup))...};
142 }
143
144 template <typename... Ts>
TupStringVec(const std::tuple<Ts...> & tup)145 std::vector<std::string> TupStringVec(const std::tuple<Ts...>& tup) {
146 return TupStringVecImpl(tup, absl::index_sequence_for<Ts...>());
147 }
148
TEST(MakeIndexSequenceTest,ApplyFromTupleExample)149 TEST(MakeIndexSequenceTest, ApplyFromTupleExample) {
150 PoorStrCat f{};
151 EXPECT_EQ("12abc3.14", f(12, "abc", 3.14));
152 EXPECT_EQ("12abc3.14", ApplyFromTuple(f, std::make_tuple(12, "abc", 3.14)));
153 }
154
TEST(IndexSequenceForTest,Basic)155 TEST(IndexSequenceForTest, Basic) {
156 StaticAssertTypeEq<absl::index_sequence<>, absl::index_sequence_for<>>();
157 StaticAssertTypeEq<absl::index_sequence<0>, absl::index_sequence_for<int>>();
158 StaticAssertTypeEq<absl::index_sequence<0, 1, 2, 3>,
159 absl::index_sequence_for<int, void, char, int>>();
160 }
161
TEST(IndexSequenceForTest,Example)162 TEST(IndexSequenceForTest, Example) {
163 EXPECT_THAT(TupStringVec(std::make_tuple(12, "abc", 3.14)),
164 ElementsAre("12", "abc", "3.14"));
165 }
166
Function(int a,int b)167 int Function(int a, int b) { return a - b; }
168
Sink(std::unique_ptr<int> p)169 int Sink(std::unique_ptr<int> p) { return *p; }
170
Factory(int n)171 std::unique_ptr<int> Factory(int n) { return absl::make_unique<int>(n); }
172
NoOp()173 void NoOp() {}
174
175 struct ConstFunctor {
operator ()__anon4780e5a00111::ConstFunctor176 int operator()(int a, int b) const { return a - b; }
177 };
178
179 struct MutableFunctor {
operator ()__anon4780e5a00111::MutableFunctor180 int operator()(int a, int b) { return a - b; }
181 };
182
183 struct EphemeralFunctor {
EphemeralFunctor__anon4780e5a00111::EphemeralFunctor184 EphemeralFunctor() {}
EphemeralFunctor__anon4780e5a00111::EphemeralFunctor185 EphemeralFunctor(const EphemeralFunctor&) {}
EphemeralFunctor__anon4780e5a00111::EphemeralFunctor186 EphemeralFunctor(EphemeralFunctor&&) {}
operator ()__anon4780e5a00111::EphemeralFunctor187 int operator()(int a, int b) && { return a - b; }
188 };
189
190 struct OverloadedFunctor {
OverloadedFunctor__anon4780e5a00111::OverloadedFunctor191 OverloadedFunctor() {}
OverloadedFunctor__anon4780e5a00111::OverloadedFunctor192 OverloadedFunctor(const OverloadedFunctor&) {}
OverloadedFunctor__anon4780e5a00111::OverloadedFunctor193 OverloadedFunctor(OverloadedFunctor&&) {}
194 template <typename... Args>
operator ()__anon4780e5a00111::OverloadedFunctor195 std::string operator()(const Args&... args) & {
196 return absl::StrCat("&", args...);
197 }
198 template <typename... Args>
operator ()__anon4780e5a00111::OverloadedFunctor199 std::string operator()(const Args&... args) const& {
200 return absl::StrCat("const&", args...);
201 }
202 template <typename... Args>
operator ()__anon4780e5a00111::OverloadedFunctor203 std::string operator()(const Args&... args) && {
204 return absl::StrCat("&&", args...);
205 }
206 };
207
208 struct Class {
Method__anon4780e5a00111::Class209 int Method(int a, int b) { return a - b; }
ConstMethod__anon4780e5a00111::Class210 int ConstMethod(int a, int b) const { return a - b; }
211
212 int member;
213 };
214
215 struct FlipFlop {
ConstMethod__anon4780e5a00111::FlipFlop216 int ConstMethod() const { return member; }
operator *__anon4780e5a00111::FlipFlop217 FlipFlop operator*() const { return {-member}; }
218
219 int member;
220 };
221
TEST(ApplyTest,Function)222 TEST(ApplyTest, Function) {
223 EXPECT_EQ(1, absl::apply(Function, std::make_tuple(3, 2)));
224 EXPECT_EQ(1, absl::apply(&Function, std::make_tuple(3, 2)));
225 }
226
TEST(ApplyTest,NonCopyableArgument)227 TEST(ApplyTest, NonCopyableArgument) {
228 EXPECT_EQ(42, absl::apply(Sink, std::make_tuple(absl::make_unique<int>(42))));
229 }
230
TEST(ApplyTest,NonCopyableResult)231 TEST(ApplyTest, NonCopyableResult) {
232 EXPECT_THAT(absl::apply(Factory, std::make_tuple(42)), Pointee(42));
233 }
234
TEST(ApplyTest,VoidResult)235 TEST(ApplyTest, VoidResult) { absl::apply(NoOp, std::tuple<>()); }
236
TEST(ApplyTest,ConstFunctor)237 TEST(ApplyTest, ConstFunctor) {
238 EXPECT_EQ(1, absl::apply(ConstFunctor(), std::make_tuple(3, 2)));
239 }
240
TEST(ApplyTest,MutableFunctor)241 TEST(ApplyTest, MutableFunctor) {
242 MutableFunctor f;
243 EXPECT_EQ(1, absl::apply(f, std::make_tuple(3, 2)));
244 EXPECT_EQ(1, absl::apply(MutableFunctor(), std::make_tuple(3, 2)));
245 }
TEST(ApplyTest,EphemeralFunctor)246 TEST(ApplyTest, EphemeralFunctor) {
247 EphemeralFunctor f;
248 EXPECT_EQ(1, absl::apply(std::move(f), std::make_tuple(3, 2)));
249 EXPECT_EQ(1, absl::apply(EphemeralFunctor(), std::make_tuple(3, 2)));
250 }
TEST(ApplyTest,OverloadedFunctor)251 TEST(ApplyTest, OverloadedFunctor) {
252 OverloadedFunctor f;
253 const OverloadedFunctor& cf = f;
254
255 EXPECT_EQ("&", absl::apply(f, std::tuple<>{}));
256 EXPECT_EQ("& 42", absl::apply(f, std::make_tuple(" 42")));
257
258 EXPECT_EQ("const&", absl::apply(cf, std::tuple<>{}));
259 EXPECT_EQ("const& 42", absl::apply(cf, std::make_tuple(" 42")));
260
261 EXPECT_EQ("&&", absl::apply(std::move(f), std::tuple<>{}));
262 OverloadedFunctor f2;
263 EXPECT_EQ("&& 42", absl::apply(std::move(f2), std::make_tuple(" 42")));
264 }
265
TEST(ApplyTest,ReferenceWrapper)266 TEST(ApplyTest, ReferenceWrapper) {
267 ConstFunctor cf;
268 MutableFunctor mf;
269 EXPECT_EQ(1, absl::apply(std::cref(cf), std::make_tuple(3, 2)));
270 EXPECT_EQ(1, absl::apply(std::ref(cf), std::make_tuple(3, 2)));
271 EXPECT_EQ(1, absl::apply(std::ref(mf), std::make_tuple(3, 2)));
272 }
273
TEST(ApplyTest,MemberFunction)274 TEST(ApplyTest, MemberFunction) {
275 std::unique_ptr<Class> p(new Class);
276 std::unique_ptr<const Class> cp(new Class);
277 EXPECT_EQ(
278 1, absl::apply(&Class::Method,
279 std::tuple<std::unique_ptr<Class>&, int, int>(p, 3, 2)));
280 EXPECT_EQ(1, absl::apply(&Class::Method,
281 std::tuple<Class*, int, int>(p.get(), 3, 2)));
282 EXPECT_EQ(
283 1, absl::apply(&Class::Method, std::tuple<Class&, int, int>(*p, 3, 2)));
284
285 EXPECT_EQ(
286 1, absl::apply(&Class::ConstMethod,
287 std::tuple<std::unique_ptr<Class>&, int, int>(p, 3, 2)));
288 EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
289 std::tuple<Class*, int, int>(p.get(), 3, 2)));
290 EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
291 std::tuple<Class&, int, int>(*p, 3, 2)));
292
293 EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
294 std::tuple<std::unique_ptr<const Class>&, int, int>(
295 cp, 3, 2)));
296 EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
297 std::tuple<const Class*, int, int>(cp.get(), 3, 2)));
298 EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
299 std::tuple<const Class&, int, int>(*cp, 3, 2)));
300
301 EXPECT_EQ(1, absl::apply(&Class::Method,
302 std::make_tuple(absl::make_unique<Class>(), 3, 2)));
303 EXPECT_EQ(1, absl::apply(&Class::ConstMethod,
304 std::make_tuple(absl::make_unique<Class>(), 3, 2)));
305 EXPECT_EQ(
306 1, absl::apply(&Class::ConstMethod,
307 std::make_tuple(absl::make_unique<const Class>(), 3, 2)));
308 }
309
TEST(ApplyTest,DataMember)310 TEST(ApplyTest, DataMember) {
311 std::unique_ptr<Class> p(new Class{42});
312 std::unique_ptr<const Class> cp(new Class{42});
313 EXPECT_EQ(
314 42, absl::apply(&Class::member, std::tuple<std::unique_ptr<Class>&>(p)));
315 EXPECT_EQ(42, absl::apply(&Class::member, std::tuple<Class&>(*p)));
316 EXPECT_EQ(42, absl::apply(&Class::member, std::tuple<Class*>(p.get())));
317
318 absl::apply(&Class::member, std::tuple<std::unique_ptr<Class>&>(p)) = 42;
319 absl::apply(&Class::member, std::tuple<Class*>(p.get())) = 42;
320 absl::apply(&Class::member, std::tuple<Class&>(*p)) = 42;
321
322 EXPECT_EQ(42, absl::apply(&Class::member,
323 std::tuple<std::unique_ptr<const Class>&>(cp)));
324 EXPECT_EQ(42, absl::apply(&Class::member, std::tuple<const Class&>(*cp)));
325 EXPECT_EQ(42,
326 absl::apply(&Class::member, std::tuple<const Class*>(cp.get())));
327 }
328
TEST(ApplyTest,FlipFlop)329 TEST(ApplyTest, FlipFlop) {
330 FlipFlop obj = {42};
331 // This call could resolve to (obj.*&FlipFlop::ConstMethod)() or
332 // ((*obj).*&FlipFlop::ConstMethod)(). We verify that it's the former.
333 EXPECT_EQ(42, absl::apply(&FlipFlop::ConstMethod, std::make_tuple(obj)));
334 EXPECT_EQ(42, absl::apply(&FlipFlop::member, std::make_tuple(obj)));
335 }
336
TEST(ExchangeTest,MoveOnly)337 TEST(ExchangeTest, MoveOnly) {
338 auto a = Factory(1);
339 EXPECT_EQ(1, *a);
340 auto b = absl::exchange(a, Factory(2));
341 EXPECT_EQ(2, *a);
342 EXPECT_EQ(1, *b);
343 }
344
TEST(MakeFromTupleTest,String)345 TEST(MakeFromTupleTest, String) {
346 EXPECT_EQ(
347 absl::make_from_tuple<std::string>(std::make_tuple("hello world", 5)),
348 "hello");
349 }
350
TEST(MakeFromTupleTest,MoveOnlyParameter)351 TEST(MakeFromTupleTest, MoveOnlyParameter) {
352 struct S {
353 S(std::unique_ptr<int> n, std::unique_ptr<int> m) : value(*n + *m) {}
354 int value = 0;
355 };
356 auto tup =
357 std::make_tuple(absl::make_unique<int>(3), absl::make_unique<int>(4));
358 auto s = absl::make_from_tuple<S>(std::move(tup));
359 EXPECT_EQ(s.value, 7);
360 }
361
TEST(MakeFromTupleTest,NoParameters)362 TEST(MakeFromTupleTest, NoParameters) {
363 struct S {
364 S() : value(1) {}
365 int value = 2;
366 };
367 EXPECT_EQ(absl::make_from_tuple<S>(std::make_tuple()).value, 1);
368 }
369
TEST(MakeFromTupleTest,Pair)370 TEST(MakeFromTupleTest, Pair) {
371 EXPECT_EQ(
372 (absl::make_from_tuple<std::pair<bool, int>>(std::make_tuple(true, 17))),
373 std::make_pair(true, 17));
374 }
375
376 } // namespace
377