any_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/types/any.h"
16 
17 #include <initializer_list>
18 #include <type_traits>
19 #include <utility>
20 #include <vector>
21 
22 #include "gtest/gtest.h"
23 #include "absl/base/config.h"
27 
28 namespace {
31 
32 template <typename T>
33 const T& AsConst(const T& t) {
34  return t;
35 }
36 
37 struct MoveOnly {
38  MoveOnly() = default;
39  explicit MoveOnly(int value) : value(value) {}
40  MoveOnly(MoveOnly&&) = default;
41  MoveOnly& operator=(MoveOnly&&) = default;
42 
43  int value = 0;
44 };
45 
46 struct CopyOnly {
47  CopyOnly() = default;
48  explicit CopyOnly(int value) : value(value) {}
49  CopyOnly(CopyOnly&&) = delete;
50  CopyOnly& operator=(CopyOnly&&) = delete;
51  CopyOnly(const CopyOnly&) = default;
52  CopyOnly& operator=(const CopyOnly&) = default;
53 
54  int value = 0;
55 };
56 
57 struct MoveOnlyWithListConstructor {
58  MoveOnlyWithListConstructor() = default;
59  explicit MoveOnlyWithListConstructor(std::initializer_list<int> /*ilist*/,
60  int value)
61  : value(value) {}
62  MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) = default;
63  MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
64  default;
65 
66  int value = 0;
67 };
68 
69 struct IntMoveOnlyCopyOnly {
70  IntMoveOnlyCopyOnly(int value, MoveOnly /*move_only*/, CopyOnly /*copy_only*/)
71  : value(value) {}
72 
73  int value;
74 };
75 
76 struct ListMoveOnlyCopyOnly {
77  ListMoveOnlyCopyOnly(std::initializer_list<int> ilist, MoveOnly /*move_only*/,
78  CopyOnly /*copy_only*/)
79  : values(ilist) {}
80 
81  std::vector<int> values;
82 };
83 
84 using FunctionType = void();
85 void FunctionToEmplace() {}
86 
87 using ArrayType = int[2];
88 using DecayedArray = absl::decay_t<ArrayType>;
89 
90 TEST(AnyTest, Noexcept) {
91  static_assert(std::is_nothrow_default_constructible<absl::any>(), "");
92  static_assert(std::is_nothrow_move_constructible<absl::any>(), "");
93  static_assert(std::is_nothrow_move_assignable<absl::any>(), "");
94  static_assert(noexcept(std::declval<absl::any&>().has_value()), "");
95  static_assert(noexcept(std::declval<absl::any&>().type()), "");
96  static_assert(noexcept(absl::any_cast<int>(std::declval<absl::any*>())), "");
97  static_assert(
98  noexcept(std::declval<absl::any&>().swap(std::declval<absl::any&>())),
99  "");
100 
101  using std::swap;
102  static_assert(
103  noexcept(swap(std::declval<absl::any&>(), std::declval<absl::any&>())),
104  "");
105 }
106 
107 TEST(AnyTest, HasValue) {
108  absl::any o;
109  EXPECT_FALSE(o.has_value());
110  o.emplace<int>();
111  EXPECT_TRUE(o.has_value());
112  o.reset();
113  EXPECT_FALSE(o.has_value());
114 }
115 
116 TEST(AnyTest, Type) {
117  absl::any o;
118  EXPECT_EQ(typeid(void), o.type());
119  o.emplace<int>(5);
120  EXPECT_EQ(typeid(int), o.type());
121  o.emplace<float>(5.f);
122  EXPECT_EQ(typeid(float), o.type());
123  o.reset();
124  EXPECT_EQ(typeid(void), o.type());
125 }
126 
127 TEST(AnyTest, EmptyPointerCast) {
128  // pointer-to-unqualified overload
129  {
130  absl::any o;
131  EXPECT_EQ(nullptr, absl::any_cast<int>(&o));
132  o.emplace<int>();
133  EXPECT_NE(nullptr, absl::any_cast<int>(&o));
134  o.reset();
135  EXPECT_EQ(nullptr, absl::any_cast<int>(&o));
136  }
137 
138  // pointer-to-const overload
139  {
140  absl::any o;
141  EXPECT_EQ(nullptr, absl::any_cast<int>(&AsConst(o)));
142  o.emplace<int>();
143  EXPECT_NE(nullptr, absl::any_cast<int>(&AsConst(o)));
144  o.reset();
145  EXPECT_EQ(nullptr, absl::any_cast<int>(&AsConst(o)));
146  }
147 }
148 
149 TEST(AnyTest, InPlaceConstruction) {
150  const CopyOnly copy_only{};
152  copy_only);
153  IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
154  EXPECT_EQ(5, v.value);
155 }
156 
157 TEST(AnyTest, InPlaceConstructionVariableTemplate) {
158  const CopyOnly copy_only{};
159  absl::any o(absl::in_place_type<IntMoveOnlyCopyOnly>, 5, MoveOnly(),
160  copy_only);
161  auto& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
162  EXPECT_EQ(5, v.value);
163 }
164 
165 TEST(AnyTest, InPlaceConstructionWithCV) {
166  const CopyOnly copy_only{};
168  MoveOnly(), copy_only);
169  IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
170  EXPECT_EQ(5, v.value);
171 }
172 
173 TEST(AnyTest, InPlaceConstructionWithCVVariableTemplate) {
174  const CopyOnly copy_only{};
175  absl::any o(absl::in_place_type<const volatile IntMoveOnlyCopyOnly>, 5,
176  MoveOnly(), copy_only);
177  auto& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
178  EXPECT_EQ(5, v.value);
179 }
180 
181 TEST(AnyTest, InPlaceConstructionWithFunction) {
182  absl::any o(absl::in_place_type_t<FunctionType>(), FunctionToEmplace);
183  FunctionType*& construction_result = absl::any_cast<FunctionType*&>(o);
184  EXPECT_EQ(&FunctionToEmplace, construction_result);
185 }
186 
187 TEST(AnyTest, InPlaceConstructionWithFunctionVariableTemplate) {
188  absl::any o(absl::in_place_type<FunctionType>, FunctionToEmplace);
189  auto& construction_result = absl::any_cast<FunctionType*&>(o);
190  EXPECT_EQ(&FunctionToEmplace, construction_result);
191 }
192 
193 TEST(AnyTest, InPlaceConstructionWithArray) {
194  ArrayType ar = {5, 42};
196  DecayedArray& construction_result = absl::any_cast<DecayedArray&>(o);
197  EXPECT_EQ(&ar[0], construction_result);
198 }
199 
200 TEST(AnyTest, InPlaceConstructionWithArrayVariableTemplate) {
201  ArrayType ar = {5, 42};
202  absl::any o(absl::in_place_type<ArrayType>, ar);
203  auto& construction_result = absl::any_cast<DecayedArray&>(o);
204  EXPECT_EQ(&ar[0], construction_result);
205 }
206 
207 TEST(AnyTest, InPlaceConstructionIlist) {
208  const CopyOnly copy_only{};
210  MoveOnly(), copy_only);
211  ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
212  std::vector<int> expected_values = {1, 2, 3, 4};
213  EXPECT_EQ(expected_values, v.values);
214 }
215 
216 TEST(AnyTest, InPlaceConstructionIlistVariableTemplate) {
217  const CopyOnly copy_only{};
218  absl::any o(absl::in_place_type<ListMoveOnlyCopyOnly>, {1, 2, 3, 4},
219  MoveOnly(), copy_only);
220  auto& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
221  std::vector<int> expected_values = {1, 2, 3, 4};
222  EXPECT_EQ(expected_values, v.values);
223 }
224 
225 TEST(AnyTest, InPlaceConstructionIlistWithCV) {
226  const CopyOnly copy_only{};
228  {1, 2, 3, 4}, MoveOnly(), copy_only);
229  ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
230  std::vector<int> expected_values = {1, 2, 3, 4};
231  EXPECT_EQ(expected_values, v.values);
232 }
233 
234 TEST(AnyTest, InPlaceConstructionIlistWithCVVariableTemplate) {
235  const CopyOnly copy_only{};
236  absl::any o(absl::in_place_type<const volatile ListMoveOnlyCopyOnly>,
237  {1, 2, 3, 4}, MoveOnly(), copy_only);
238  auto& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
239  std::vector<int> expected_values = {1, 2, 3, 4};
240  EXPECT_EQ(expected_values, v.values);
241 }
242 
243 TEST(AnyTest, InPlaceNoArgs) {
245  EXPECT_EQ(0, absl::any_cast<int&>(o));
246 }
247 
248 TEST(AnyTest, InPlaceNoArgsVariableTemplate) {
249  absl::any o(absl::in_place_type<int>);
250  EXPECT_EQ(0, absl::any_cast<int&>(o));
251 }
252 
253 template <typename Enabler, typename T, typename... Args>
254 struct CanEmplaceAnyImpl : std::false_type {};
255 
256 template <typename T, typename... Args>
257 struct CanEmplaceAnyImpl<
258  absl::void_t<decltype(
259  std::declval<absl::any&>().emplace<T>(std::declval<Args>()...))>,
260  T, Args...> : std::true_type {};
261 
262 template <typename T, typename... Args>
263 using CanEmplaceAny = CanEmplaceAnyImpl<void, T, Args...>;
264 
265 TEST(AnyTest, Emplace) {
266  const CopyOnly copy_only{};
267  absl::any o;
268  EXPECT_TRUE((std::is_same<decltype(o.emplace<IntMoveOnlyCopyOnly>(
269  5, MoveOnly(), copy_only)),
270  IntMoveOnlyCopyOnly&>::value));
271  IntMoveOnlyCopyOnly& emplace_result =
272  o.emplace<IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
273  EXPECT_EQ(5, emplace_result.value);
274  IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
275  EXPECT_EQ(5, v.value);
276  EXPECT_EQ(&emplace_result, &v);
277 
278  static_assert(!CanEmplaceAny<int, int, int>::value, "");
279  static_assert(!CanEmplaceAny<MoveOnly, MoveOnly>::value, "");
280 }
281 
282 TEST(AnyTest, EmplaceWithCV) {
283  const CopyOnly copy_only{};
284  absl::any o;
285  EXPECT_TRUE(
286  (std::is_same<decltype(o.emplace<const volatile IntMoveOnlyCopyOnly>(
287  5, MoveOnly(), copy_only)),
288  IntMoveOnlyCopyOnly&>::value));
289  IntMoveOnlyCopyOnly& emplace_result =
290  o.emplace<const volatile IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
291  EXPECT_EQ(5, emplace_result.value);
292  IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
293  EXPECT_EQ(5, v.value);
294  EXPECT_EQ(&emplace_result, &v);
295 }
296 
297 TEST(AnyTest, EmplaceWithFunction) {
298  absl::any o;
299  EXPECT_TRUE(
300  (std::is_same<decltype(o.emplace<FunctionType>(FunctionToEmplace)),
301  FunctionType*&>::value));
302  FunctionType*& emplace_result = o.emplace<FunctionType>(FunctionToEmplace);
303  EXPECT_EQ(&FunctionToEmplace, emplace_result);
304 }
305 
306 TEST(AnyTest, EmplaceWithArray) {
307  absl::any o;
308  ArrayType ar = {5, 42};
309  EXPECT_TRUE(
310  (std::is_same<decltype(o.emplace<ArrayType>(ar)), DecayedArray&>::value));
311  DecayedArray& emplace_result = o.emplace<ArrayType>(ar);
312  EXPECT_EQ(&ar[0], emplace_result);
313 }
314 
315 TEST(AnyTest, EmplaceIlist) {
316  const CopyOnly copy_only{};
317  absl::any o;
318  EXPECT_TRUE((std::is_same<decltype(o.emplace<ListMoveOnlyCopyOnly>(
319  {1, 2, 3, 4}, MoveOnly(), copy_only)),
320  ListMoveOnlyCopyOnly&>::value));
321  ListMoveOnlyCopyOnly& emplace_result =
322  o.emplace<ListMoveOnlyCopyOnly>({1, 2, 3, 4}, MoveOnly(), copy_only);
323  ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
324  EXPECT_EQ(&v, &emplace_result);
325  std::vector<int> expected_values = {1, 2, 3, 4};
326  EXPECT_EQ(expected_values, v.values);
327 
328  static_assert(!CanEmplaceAny<int, std::initializer_list<int>>::value, "");
329  static_assert(!CanEmplaceAny<MoveOnlyWithListConstructor,
330  std::initializer_list<int>, int>::value,
331  "");
332 }
333 
334 TEST(AnyTest, EmplaceIlistWithCV) {
335  const CopyOnly copy_only{};
336  absl::any o;
337  EXPECT_TRUE(
338  (std::is_same<decltype(o.emplace<const volatile ListMoveOnlyCopyOnly>(
339  {1, 2, 3, 4}, MoveOnly(), copy_only)),
340  ListMoveOnlyCopyOnly&>::value));
341  ListMoveOnlyCopyOnly& emplace_result =
342  o.emplace<const volatile ListMoveOnlyCopyOnly>({1, 2, 3, 4}, MoveOnly(),
343  copy_only);
344  ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
345  EXPECT_EQ(&v, &emplace_result);
346  std::vector<int> expected_values = {1, 2, 3, 4};
347  EXPECT_EQ(expected_values, v.values);
348 }
349 
350 TEST(AnyTest, EmplaceNoArgs) {
351  absl::any o;
352  o.emplace<int>();
353  EXPECT_EQ(0, absl::any_cast<int>(o));
354 }
355 
356 TEST(AnyTest, ConversionConstruction) {
357  {
358  absl::any o = 5;
359  EXPECT_EQ(5, absl::any_cast<int>(o));
360  }
361 
362  {
363  const CopyOnly copy_only(5);
364  absl::any o = copy_only;
365  EXPECT_EQ(5, absl::any_cast<CopyOnly&>(o).value);
366  }
367 
369 }
370 
371 TEST(AnyTest, ConversionAssignment) {
372  {
373  absl::any o;
374  o = 5;
375  EXPECT_EQ(5, absl::any_cast<int>(o));
376  }
377 
378  {
379  const CopyOnly copy_only(5);
380  absl::any o;
381  o = copy_only;
382  EXPECT_EQ(5, absl::any_cast<CopyOnly&>(o).value);
383  }
384 
386 }
387 
388 // Suppress MSVC warnings.
389 // 4521: multiple copy constructors specified
390 // We wrote multiple of them to test that the correct overloads are selected.
391 #ifdef _MSC_VER
392 #pragma warning( push )
393 #pragma warning( disable : 4521)
394 #endif
395 
396 // Weird type for testing, only used to make sure we "properly" perfect-forward
397 // when being placed into an absl::any (use the l-value constructor if given an
398 // l-value rather than use the copy constructor).
399 struct WeirdConstructor42 {
400  explicit WeirdConstructor42(int value) : value(value) {}
401 
402  // Copy-constructor
403  WeirdConstructor42(const WeirdConstructor42& other) : value(other.value) {}
404 
405  // L-value "weird" constructor (used when given an l-value)
406  WeirdConstructor42(
407  WeirdConstructor42& /*other*/) // NOLINT(runtime/references)
408  : value(42) {}
409 
410  int value;
411 };
412 #ifdef _MSC_VER
413 #pragma warning( pop )
414 #endif
415 
416 TEST(AnyTest, WeirdConversionConstruction) {
417  {
418  const WeirdConstructor42 source(5);
419  absl::any o = source; // Actual copy
420  EXPECT_EQ(5, absl::any_cast<WeirdConstructor42&>(o).value);
421  }
422 
423  {
424  WeirdConstructor42 source(5);
425  absl::any o = source; // Weird "conversion"
426  EXPECT_EQ(42, absl::any_cast<WeirdConstructor42&>(o).value);
427  }
428 }
429 
430 TEST(AnyTest, WeirdConversionAssignment) {
431  {
432  const WeirdConstructor42 source(5);
433  absl::any o;
434  o = source; // Actual copy
435  EXPECT_EQ(5, absl::any_cast<WeirdConstructor42&>(o).value);
436  }
437 
438  {
439  WeirdConstructor42 source(5);
440  absl::any o;
441  o = source; // Weird "conversion"
442  EXPECT_EQ(42, absl::any_cast<WeirdConstructor42&>(o).value);
443  }
444 }
445 
446 struct Value {};
447 
448 TEST(AnyTest, AnyCastValue) {
449  {
450  absl::any o;
451  o.emplace<int>(5);
452  EXPECT_EQ(5, absl::any_cast<int>(o));
453  EXPECT_EQ(5, absl::any_cast<int>(AsConst(o)));
454  static_assert(
455  std::is_same<decltype(absl::any_cast<Value>(o)), Value>::value, "");
456  }
457 
458  {
459  absl::any o;
460  o.emplace<int>(5);
461  EXPECT_EQ(5, absl::any_cast<const int>(o));
462  EXPECT_EQ(5, absl::any_cast<const int>(AsConst(o)));
463  static_assert(std::is_same<decltype(absl::any_cast<const Value>(o)),
464  const Value>::value,
465  "");
466  }
467 }
468 
469 TEST(AnyTest, AnyCastReference) {
470  {
471  absl::any o;
472  o.emplace<int>(5);
473  EXPECT_EQ(5, absl::any_cast<int&>(o));
474  EXPECT_EQ(5, absl::any_cast<const int&>(AsConst(o)));
475  static_assert(
476  std::is_same<decltype(absl::any_cast<Value&>(o)), Value&>::value, "");
477  }
478 
479  {
480  absl::any o;
481  o.emplace<int>(5);
482  EXPECT_EQ(5, absl::any_cast<const int>(o));
483  EXPECT_EQ(5, absl::any_cast<const int>(AsConst(o)));
484  static_assert(std::is_same<decltype(absl::any_cast<const Value&>(o)),
485  const Value&>::value,
486  "");
487  }
488 
489  {
490  absl::any o;
491  o.emplace<int>(5);
492  EXPECT_EQ(5, absl::any_cast<int&&>(std::move(o)));
493  static_assert(std::is_same<decltype(absl::any_cast<Value&&>(std::move(o))),
494  Value&&>::value,
495  "");
496  }
497 
498  {
499  absl::any o;
500  o.emplace<int>(5);
501  EXPECT_EQ(5, absl::any_cast<const int>(std::move(o)));
502  static_assert(
503  std::is_same<decltype(absl::any_cast<const Value&&>(std::move(o))),
504  const Value&&>::value,
505  "");
506  }
507 }
508 
509 TEST(AnyTest, AnyCastPointer) {
510  {
511  absl::any o;
512  EXPECT_EQ(nullptr, absl::any_cast<char>(&o));
513  o.emplace<int>(5);
514  EXPECT_EQ(nullptr, absl::any_cast<char>(&o));
515  o.emplace<char>('a');
516  EXPECT_EQ('a', *absl::any_cast<char>(&o));
517  static_assert(
518  std::is_same<decltype(absl::any_cast<Value>(&o)), Value*>::value, "");
519  }
520 
521  {
522  absl::any o;
523  EXPECT_EQ(nullptr, absl::any_cast<const char>(&o));
524  o.emplace<int>(5);
525  EXPECT_EQ(nullptr, absl::any_cast<const char>(&o));
526  o.emplace<char>('a');
527  EXPECT_EQ('a', *absl::any_cast<const char>(&o));
528  static_assert(std::is_same<decltype(absl::any_cast<const Value>(&o)),
529  const Value*>::value,
530  "");
531  }
532 }
533 
534 TEST(AnyTest, MakeAny) {
535  const CopyOnly copy_only{};
536  auto o = absl::make_any<IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
537  static_assert(std::is_same<decltype(o), absl::any>::value, "");
538  EXPECT_EQ(5, absl::any_cast<IntMoveOnlyCopyOnly&>(o).value);
539 }
540 
541 TEST(AnyTest, MakeAnyIList) {
542  const CopyOnly copy_only{};
543  auto o =
544  absl::make_any<ListMoveOnlyCopyOnly>({1, 2, 3}, MoveOnly(), copy_only);
545  static_assert(std::is_same<decltype(o), absl::any>::value, "");
546  ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
547  std::vector<int> expected_values = {1, 2, 3};
548  EXPECT_EQ(expected_values, v.values);
549 }
550 
551 // Test the use of copy constructor and operator=
552 TEST(AnyTest, Copy) {
553  InstanceTracker tracker_raii;
554 
555  {
556  absl::any o(absl::in_place_type<CopyableOnlyInstance>, 123);
557  CopyableOnlyInstance* f1 = absl::any_cast<CopyableOnlyInstance>(&o);
558 
559  absl::any o2(o);
560  const CopyableOnlyInstance* f2 = absl::any_cast<CopyableOnlyInstance>(&o2);
561  EXPECT_EQ(123, f2->value());
562  EXPECT_NE(f1, f2);
563 
564  absl::any o3;
565  o3 = o2;
566  const CopyableOnlyInstance* f3 = absl::any_cast<CopyableOnlyInstance>(&o3);
567  EXPECT_EQ(123, f3->value());
568  EXPECT_NE(f2, f3);
569 
570  const absl::any o4(4);
571  // copy construct from const lvalue ref.
572  absl::any o5 = o4;
573  EXPECT_EQ(4, absl::any_cast<int>(o4));
574  EXPECT_EQ(4, absl::any_cast<int>(o5));
575 
576  // Copy construct from const rvalue ref.
577  absl::any o6 = std::move(o4); // NOLINT
578  EXPECT_EQ(4, absl::any_cast<int>(o4));
579  EXPECT_EQ(4, absl::any_cast<int>(o6));
580  }
581 }
582 
583 TEST(AnyTest, Move) {
584  InstanceTracker tracker_raii;
585 
586  absl::any any1;
587  any1.emplace<CopyableOnlyInstance>(5);
588 
589  // This is a copy, so copy count increases to 1.
590  absl::any any2 = any1;
591  EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any1).value());
592  EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any2).value());
593  EXPECT_EQ(1, tracker_raii.copies());
594 
595  // This isn't a copy, so copy count doesn't increase.
596  absl::any any3 = std::move(any2);
597  EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any3).value());
598  EXPECT_EQ(1, tracker_raii.copies());
599 
600  absl::any any4;
601  any4 = std::move(any3);
602  EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any4).value());
603  EXPECT_EQ(1, tracker_raii.copies());
604 
605  absl::any tmp4(4);
606  absl::any o4(std::move(tmp4)); // move construct
607  EXPECT_EQ(4, absl::any_cast<int>(o4));
608  o4 = *&o4; // self assign
609  EXPECT_EQ(4, absl::any_cast<int>(o4));
610  EXPECT_TRUE(o4.has_value());
611 
612  absl::any o5;
613  absl::any tmp5(5);
614  o5 = std::move(tmp5); // move assign
615  EXPECT_EQ(5, absl::any_cast<int>(o5));
616 }
617 
618 // Reset the ObjectOwner with an object of a different type
619 TEST(AnyTest, Reset) {
620  absl::any o;
621  o.emplace<int>();
622 
623  o.reset();
624  EXPECT_FALSE(o.has_value());
625 
626  o.emplace<char>();
627  EXPECT_TRUE(o.has_value());
628 }
629 
630 TEST(AnyTest, ConversionConstructionCausesOneCopy) {
631  InstanceTracker tracker_raii;
632  CopyableOnlyInstance counter(5);
634  EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(o).value());
635  EXPECT_EQ(1, tracker_raii.copies());
636 }
637 
639 // Tests for Exception Behavior //
641 
642 #if defined(ABSL_HAVE_STD_ANY)
643 
644 // If using a std `any` implementation, we can't check for a specific message.
645 #define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...) \
646  ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), absl::bad_any_cast, \
647  "")
648 
649 #else
650 
651 // If using the absl `any` implementation, we can rely on a specific message.
652 #define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...) \
653  ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), absl::bad_any_cast, \
654  "Bad any cast")
655 
656 #endif // defined(ABSL_HAVE_STD_ANY)
657 
658 TEST(AnyTest, ThrowBadAlloc) {
659  {
660  absl::any a;
661  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int&>(a));
662  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int&>(a));
663  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int&&>(absl::any{}));
664  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int&&>(absl::any{}));
665  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int>(a));
666  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int>(a));
667  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int>(absl::any{}));
668  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int>(absl::any{}));
669 
670  // const absl::any operand
671  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int&>(AsConst(a)));
672  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int>(AsConst(a)));
673  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int>(AsConst(a)));
674  }
675 
676  {
677  absl::any a(absl::in_place_type<int>);
678  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float&>(a));
679  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float&>(a));
680  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float&&>(absl::any{}));
682  absl::any_cast<const float&&>(absl::any{}));
683  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float>(a));
684  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float>(a));
685  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float>(absl::any{}));
686  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float>(absl::any{}));
687 
688  // const absl::any operand
689  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float&>(AsConst(a)));
690  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float>(AsConst(a)));
691  ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float>(AsConst(a)));
692  }
693 }
694 
695 class BadCopy {};
696 
697 struct BadCopyable {
698  BadCopyable() = default;
699  BadCopyable(BadCopyable&&) = default;
700  BadCopyable(const BadCopyable&) {
701 #ifdef ABSL_HAVE_EXCEPTIONS
702  throw BadCopy();
703 #else
704  ABSL_RAW_LOG(FATAL, "Bad copy");
705 #endif
706  }
707 };
708 
709 #define ABSL_ANY_TEST_EXPECT_BAD_COPY(...) \
710  ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), BadCopy, "Bad copy")
711 
712 // Test the guarantees regarding exceptions in copy/assign.
713 TEST(AnyTest, FailedCopy) {
714  {
715  const BadCopyable bad{};
717  }
718 
719  {
720  absl::any src(absl::in_place_type<BadCopyable>);
722  }
723 
724  {
725  BadCopyable bad;
726  absl::any target;
727  ABSL_ANY_TEST_EXPECT_BAD_COPY(target = bad);
728  }
729 
730  {
731  BadCopyable bad;
732  absl::any target(absl::in_place_type<BadCopyable>);
733  ABSL_ANY_TEST_EXPECT_BAD_COPY(target = bad);
734  EXPECT_TRUE(target.has_value());
735  }
736 
737  {
738  absl::any src(absl::in_place_type<BadCopyable>);
739  absl::any target;
740  ABSL_ANY_TEST_EXPECT_BAD_COPY(target = src);
741  EXPECT_FALSE(target.has_value());
742  }
743 
744  {
745  absl::any src(absl::in_place_type<BadCopyable>);
746  absl::any target(absl::in_place_type<BadCopyable>);
747  ABSL_ANY_TEST_EXPECT_BAD_COPY(target = src);
748  EXPECT_TRUE(target.has_value());
749  }
750 }
751 
752 // Test the guarantees regarding exceptions in emplace.
753 TEST(AnyTest, FailedEmplace) {
754  {
755  BadCopyable bad;
756  absl::any target;
757  ABSL_ANY_TEST_EXPECT_BAD_COPY(target.emplace<BadCopyable>(bad));
758  }
759 
760  {
761  BadCopyable bad;
762  absl::any target(absl::in_place_type<int>);
763  ABSL_ANY_TEST_EXPECT_BAD_COPY(target.emplace<BadCopyable>(bad));
764 #if defined(ABSL_HAVE_STD_ANY) && defined(__GLIBCXX__)
765  // libstdc++ std::any::emplace() implementation (as of 7.2) has a bug: if an
766  // exception is thrown, *this contains a value.
767 #define ABSL_GLIBCXX_ANY_EMPLACE_EXCEPTION_BUG 1
768 #endif
769 #if defined(ABSL_HAVE_EXCEPTIONS) && \
770  !defined(ABSL_GLIBCXX_ANY_EMPLACE_EXCEPTION_BUG)
771  EXPECT_FALSE(target.has_value());
772 #endif
773  }
774 }
775 
776 } // namespace
int v
Definition: variant_test.cc:81
void reset() noexcept
Definition: any.h:352
bool has_value() const noexcept
Definition: any.h:365
#define ABSL_RAW_LOG(severity,...)
Definition: raw_logging.h:42
typename std::decay< T >::type decay_t
Definition: type_traits.h:544
int * counter
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
Definition: utility.h:189
Definition: algorithm.h:29
Definition: any.h:214
size_t value
void Copy(FlagOpFn op, const void *src, void *dst)
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
UnboundConversion o
Definition: parser_test.cc:86
ValueType any_cast(const any &operand)
Definition: any.h:479
const std::type_info & type() const noexcept
Definition: any.h:370
typename type_traits_internal::VoidTImpl< Ts... >::type void_t
Definition: type_traits.h:171
VT & emplace(Args &&... args)
Definition: any.h:316
TEST(Symbolize, Unimplemented)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
#define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...)
Definition: any_test.cc:652
#define ABSL_ANY_TEST_EXPECT_BAD_COPY(...)
Definition: any_test.cc:709


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