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