15 #include "absl/utility/utility.h"
20 #include <type_traits>
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"
38 #pragma warning( push )
39 #pragma warning( disable : 4503 ) // decorated name length exceeded
40 #pragma warning( disable : 4101 ) // unreferenced local variable
52 TEST(IntegerSequenceTest, Size) {
66 StaticAssertTypeEq<absl::index_sequence<0, 1>,
68 StaticAssertTypeEq<absl::index_sequence<0, 1, 2>,
72 TEST(IntegerSequenceTest, MakeIntegerSequence) {
73 StaticAssertTypeEq<absl::integer_sequence<int>,
75 StaticAssertTypeEq<absl::integer_sequence<int, 0>,
77 StaticAssertTypeEq<absl::integer_sequence<int, 0, 1>,
79 StaticAssertTypeEq<absl::integer_sequence<int, 0, 1, 2>,
83 template <
typename... Ts>
86 template <
size_t... Is>
96 TEST(IntegerSequenceTest, MakeIndexSequencePerformance) {
105 template <
typename F,
typename Tup,
size_t... Is>
107 -> decltype(
f(std::get<Is>(tup)...)) {
108 return f(std::get<Is>(tup)...);
111 template <
typename Tup>
114 template <
typename F,
typename Tup>
115 auto ApplyFromTuple(
F f,
const Tup& tup)
116 -> decltype(ApplyFromTupleImpl(
f, tup, TupIdxSeq<Tup>{})) {
117 return ApplyFromTupleImpl(
f, tup, TupIdxSeq<Tup>{});
120 template <
typename T>
122 std::ostringstream os;
128 template <
typename...
Args>
131 for (
const auto& e : {Fmt(
args)...})
r += e;
136 template <
typename Tup,
size_t... Is>
137 std::vector<std::string> TupStringVecImpl(
const Tup& tup,
139 return {Fmt(std::get<Is>(tup))...};
142 template <
typename... Ts>
143 std::vector<std::string> TupStringVec(
const std::tuple<Ts...>& tup) {
147 TEST(MakeIndexSequenceTest, ApplyFromTupleExample) {
153 TEST(IndexSequenceForTest, Basic) {
156 StaticAssertTypeEq<absl::index_sequence<0, 1, 2, 3>,
160 TEST(IndexSequenceForTest, Example) {
167 int Sink(std::unique_ptr<int>
p) {
return *
p; }
169 std::unique_ptr<int> Factory(
int n) {
return absl::make_unique<int>(
n); }
173 struct ConstFunctor {
174 int operator()(
int a,
int b)
const {
return a -
b; }
177 struct MutableFunctor {
178 int operator()(
int a,
int b) {
return a -
b; }
181 struct EphemeralFunctor {
182 EphemeralFunctor() {}
183 EphemeralFunctor(
const EphemeralFunctor&) {}
184 EphemeralFunctor(EphemeralFunctor&&) {}
185 int operator()(
int a,
int b) && {
return a -
b; }
188 struct OverloadedFunctor {
189 OverloadedFunctor() {}
190 OverloadedFunctor(
const OverloadedFunctor&) {}
191 OverloadedFunctor(OverloadedFunctor&&) {}
192 template <
typename...
Args>
196 template <
typename...
Args>
200 template <
typename...
Args>
208 int ConstMethod(
int a,
int b)
const {
return a -
b; }
214 int ConstMethod()
const {
return member; }
225 TEST(ApplyTest, NonCopyableArgument) {
229 TEST(ApplyTest, NonCopyableResult) {
236 TEST(ApplyTest, ConstFunctor) {
240 TEST(ApplyTest, MutableFunctor) {
245 TEST(ApplyTest, EphemeralFunctor) {
250 TEST(ApplyTest, OverloadedFunctor) {
252 const OverloadedFunctor& cf =
f;
261 OverloadedFunctor
f2;
265 TEST(ApplyTest, ReferenceWrapper) {
273 TEST(ApplyTest, MemberFunction) {
274 std::unique_ptr<Class>
p(
new Class);
275 std::unique_ptr<const Class> cp(
new Class);
278 std::tuple<std::unique_ptr<Class>&,
int,
int>(
p, 3, 2)));
280 std::tuple<Class*, int, int>(
p.get(), 3, 2)));
286 std::tuple<std::unique_ptr<Class>&,
int,
int>(
p, 3, 2)));
288 std::tuple<Class*, int, int>(
p.get(), 3, 2)));
290 std::tuple<Class&, int, int>(*
p, 3, 2)));
293 std::tuple<std::unique_ptr<const Class>&,
int,
int>(
296 std::tuple<const Class*, int, int>(cp.get(), 3, 2)));
298 std::tuple<const Class&, int, int>(*cp, 3, 2)));
309 TEST(ApplyTest, DataMember) {
310 std::unique_ptr<Class>
p(
new Class{42});
311 std::unique_ptr<const Class> cp(
new Class{42});
322 std::tuple<std::unique_ptr<const Class>&>(cp)));
328 TEST(ApplyTest, FlipFlop) {
336 TEST(ExchangeTest, MoveOnly) {
344 TEST(MakeFromTupleTest, String) {
350 TEST(MakeFromTupleTest, MoveOnlyParameter) {
352 S(std::unique_ptr<int>
n, std::unique_ptr<int>
m) :
value(*
n + *
m) {}
357 auto s = absl::make_from_tuple<S>(
std::move(tup));
361 TEST(MakeFromTupleTest, NoParameters) {
372 std::make_pair(
true, 17));