utility_test.cc
Go to the documentation of this file.
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 
47 TEST(IntegerSequenceTest, ValueType) {
50 }
51 
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()));
59  constexpr size_t sz = absl::integer_sequence<int, 0, 1>::size();
60  EXPECT_EQ(2, sz);
61 }
62 
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>,
68  StaticAssertTypeEq<absl::index_sequence<0, 1, 2>,
70 }
71 
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>,
81 }
82 
83 template <typename... Ts>
84 class Counter {};
85 
86 template <size_t... 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).
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.
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>
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>
113 
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>{});
118 }
119 
120 template <typename T>
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>
129  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>
137 std::vector<std::string> TupStringVecImpl(const Tup& tup,
139  return {Fmt(std::get<Is>(tup))...};
140 }
141 
142 template <typename... Ts>
143 std::vector<std::string> TupStringVec(const std::tuple<Ts...>& tup) {
144  return TupStringVecImpl(tup, absl::index_sequence_for<Ts...>());
145 }
146 
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 
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>,
158 }
159 
160 TEST(IndexSequenceForTest, Example) {
161  EXPECT_THAT(TupStringVec(std::make_tuple(12, "abc", 3.14)),
162  ElementsAre("12", "abc", "3.14"));
163 }
164 
165 int Function(int a, int b) { return a - b; }
166 
167 int Sink(std::unique_ptr<int> p) { return *p; }
168 
169 std::unique_ptr<int> Factory(int n) { return absl::make_unique<int>(n); }
170 
171 void NoOp() {}
172 
173 struct ConstFunctor {
174  int operator()(int a, int b) const { return a - b; }
175 };
176 
177 struct MutableFunctor {
178  int operator()(int a, int b) { return a - b; }
179 };
180 
181 struct EphemeralFunctor {
182  EphemeralFunctor() {}
183  EphemeralFunctor(const EphemeralFunctor&) {}
184  EphemeralFunctor(EphemeralFunctor&&) {}
185  int operator()(int a, int b) && { return a - b; }
186 };
187 
188 struct OverloadedFunctor {
189  OverloadedFunctor() {}
190  OverloadedFunctor(const OverloadedFunctor&) {}
191  OverloadedFunctor(OverloadedFunctor&&) {}
192  template <typename... Args>
193  std::string operator()(const Args&... args) & {
194  return absl::StrCat("&", args...);
195  }
196  template <typename... Args>
197  std::string operator()(const Args&... args) const& {
198  return absl::StrCat("const&", args...);
199  }
200  template <typename... Args>
201  std::string operator()(const Args&... args) && {
202  return absl::StrCat("&&", args...);
203  }
204 };
205 
206 struct Class {
207  int Method(int a, int b) { return a - b; }
208  int ConstMethod(int a, int b) const { return a - b; }
209 
210  int member;
211 };
212 
213 struct FlipFlop {
214  int ConstMethod() const { return member; }
215  FlipFlop operator*() const { return {-member}; }
216 
217  int member;
218 };
219 
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 
225 TEST(ApplyTest, NonCopyableArgument) {
226  EXPECT_EQ(42, absl::apply(Sink, std::make_tuple(absl::make_unique<int>(42))));
227 }
228 
229 TEST(ApplyTest, NonCopyableResult) {
230  EXPECT_THAT(absl::apply(Factory, std::make_tuple(42)),
231  ::testing::Pointee(42));
232 }
233 
234 TEST(ApplyTest, VoidResult) { absl::apply(NoOp, std::tuple<>()); }
235 
236 TEST(ApplyTest, ConstFunctor) {
237  EXPECT_EQ(1, absl::apply(ConstFunctor(), std::make_tuple(3, 2)));
238 }
239 
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 }
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 }
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 
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 
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 
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 
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 
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 
344 } // namespace
345 
#define ABSL_ATTRIBUTE_UNUSED
Definition: attributes.h:549
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: str_cat.cc:98
make_integer_sequence< size_t, N > make_index_sequence
Definition: utility.h:148
uint128 operator*(uint128 lhs, uint128 rhs)
Definition: int128.h:671
typename utility_internal::Gen< T, N >::type make_integer_sequence
Definition: utility.h:140
static constexpr size_t size() noexcept
Definition: utility.h:76
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 >
Definition: utility.h:287
make_index_sequence< sizeof...(Ts)> index_sequence_for
Definition: utility.h:156
int member
Definition: invoke_test.cc:74
TEST(Symbolize, Unimplemented)
uint64_t b
Definition: layout_test.cc:50
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
T exchange(T &obj, U &&new_value)
Definition: utility.h:312


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:21