20 #include <type_traits> 23 #include "gmock/gmock.h" 24 #include "gtest/gtest.h" 38 #pragma warning( push ) 39 #pragma warning( disable : 4503 ) // decorated name length exceeded 40 #pragma warning( disable : 4101 ) // unreferenced local variable 43 using ::testing::ElementsAre;
44 using ::testing::Pointee;
45 using ::testing::StaticAssertTypeEq;
47 TEST(IntegerSequenceTest, ValueType) {
52 TEST(IntegerSequenceTest, Size) {
63 TEST(IntegerSequenceTest, MakeIndexSequence) {
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>
121 std::string Fmt(
const T& x) {
122 std::ostringstream os;
128 template <
typename... Args>
129 std::string operator()(
const Args&... args)
const {
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) {
149 EXPECT_EQ(
"12abc3.14", f(12,
"abc", 3.14));
150 EXPECT_EQ(
"12abc3.14", ApplyFromTuple(f, std::make_tuple(12,
"abc", 3.14)));
153 TEST(IndexSequenceForTest, Basic) {
156 StaticAssertTypeEq<absl::index_sequence<0, 1, 2, 3>,
160 TEST(IndexSequenceForTest, Example) {
161 EXPECT_THAT(TupStringVec(std::make_tuple(12,
"abc", 3.14)),
162 ElementsAre(
"12",
"abc",
"3.14"));
165 int Function(
int a,
int b) {
return a -
b; }
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>
193 std::string operator()(
const Args&... args) & {
196 template <
typename... Args>
197 std::string operator()(
const Args&... args)
const& {
200 template <
typename... Args>
201 std::string operator()(
const Args&... args) && {
207 int Method(
int a,
int b) {
return a -
b; }
208 int ConstMethod(
int a,
int b)
const {
return a -
b; }
214 int ConstMethod()
const {
return member; }
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)));
225 TEST(ApplyTest, NonCopyableArgument) {
226 EXPECT_EQ(42,
absl::apply(Sink, std::make_tuple(absl::make_unique<int>(42))));
229 TEST(ApplyTest, NonCopyableResult) {
230 EXPECT_THAT(
absl::apply(Factory, std::make_tuple(42)),
231 ::testing::Pointee(42));
236 TEST(ApplyTest, ConstFunctor) {
237 EXPECT_EQ(1,
absl::apply(ConstFunctor(), std::make_tuple(3, 2)));
240 TEST(ApplyTest, MutableFunctor) {
242 EXPECT_EQ(1,
absl::apply(f, std::make_tuple(3, 2)));
243 EXPECT_EQ(1,
absl::apply(MutableFunctor(), std::make_tuple(3, 2)));
245 TEST(ApplyTest, EphemeralFunctor) {
248 EXPECT_EQ(1,
absl::apply(EphemeralFunctor(), std::make_tuple(3, 2)));
250 TEST(ApplyTest, OverloadedFunctor) {
252 const OverloadedFunctor& cf = f;
255 EXPECT_EQ(
"& 42",
absl::apply(f, std::make_tuple(
" 42")));
257 EXPECT_EQ(
"const&",
absl::apply(cf, std::tuple<>{}));
258 EXPECT_EQ(
"const& 42",
absl::apply(cf, std::make_tuple(
" 42")));
261 OverloadedFunctor f2;
265 TEST(ApplyTest, ReferenceWrapper) {
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)));
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)));
282 1,
absl::apply(&Class::Method, std::tuple<Class&, int, int>(*p, 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)));
301 std::make_tuple(absl::make_unique<Class>(), 3, 2)));
303 std::make_tuple(absl::make_unique<Class>(), 3, 2)));
306 std::make_tuple(absl::make_unique<const Class>(), 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) {
332 EXPECT_EQ(42,
absl::apply(&FlipFlop::ConstMethod, std::make_tuple(obj)));
336 TEST(ExchangeTest, MoveOnly) {
#define ABSL_ATTRIBUTE_UNUSED
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
make_integer_sequence< size_t, N > make_index_sequence
uint128 operator*(uint128 lhs, uint128 rhs)
typename utility_internal::Gen< T, N >::type make_integer_sequence
static constexpr size_t size() noexcept
auto apply(Functor &&functor, Tuple &&t) -> decltype(utility_internal::apply_helper(absl::forward< Functor >(functor), absl::forward< Tuple >(t), absl::make_index_sequence< std::tuple_size< typename std::remove_reference< Tuple >::type >::value >
make_index_sequence< sizeof...(Ts)> index_sequence_for
TEST(Symbolize, Unimplemented)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
T exchange(T &obj, U &&new_value)