17 #include <initializer_list> 18 #include <type_traits> 22 #include "gtest/gtest.h" 33 const T& AsConst(
const T& t) {
39 explicit MoveOnly(
int value) : value(value) {}
40 MoveOnly(MoveOnly&&) =
default;
41 MoveOnly& operator=(MoveOnly&&) =
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;
57 struct MoveOnlyWithListConstructor {
58 MoveOnlyWithListConstructor() =
default;
59 explicit MoveOnlyWithListConstructor(std::initializer_list<int> ,
62 MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) =
default;
63 MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
69 struct IntMoveOnlyCopyOnly {
70 IntMoveOnlyCopyOnly(
int value, MoveOnly , CopyOnly )
76 struct ListMoveOnlyCopyOnly {
77 ListMoveOnlyCopyOnly(std::initializer_list<int> ilist, MoveOnly ,
81 std::vector<int> values;
84 using FunctionType = void();
85 void FunctionToEmplace() {}
87 using ArrayType =
int[2];
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*>())),
"");
98 noexcept(std::declval<absl::any&>().
swap(std::declval<absl::any&>())),
103 noexcept(
swap(std::declval<absl::any&>(), std::declval<absl::any&>())),
107 TEST(AnyTest, HasValue) {
116 TEST(AnyTest, Type) {
118 EXPECT_EQ(
typeid(
void), o.
type());
120 EXPECT_EQ(
typeid(
int), o.
type());
122 EXPECT_EQ(
typeid(
float), o.
type());
124 EXPECT_EQ(
typeid(
void), o.
type());
127 TEST(AnyTest, EmptyPointerCast) {
131 EXPECT_EQ(
nullptr, absl::any_cast<int>(&o));
133 EXPECT_NE(
nullptr, absl::any_cast<int>(&o));
135 EXPECT_EQ(
nullptr, absl::any_cast<int>(&o));
141 EXPECT_EQ(
nullptr, absl::any_cast<int>(&AsConst(o)));
143 EXPECT_NE(
nullptr, absl::any_cast<int>(&AsConst(o)));
145 EXPECT_EQ(
nullptr, absl::any_cast<int>(&AsConst(o)));
149 TEST(AnyTest, InPlaceConstruction) {
150 const CopyOnly copy_only{};
154 EXPECT_EQ(5, v.value);
157 TEST(AnyTest, InPlaceConstructionVariableTemplate) {
158 const CopyOnly copy_only{};
159 absl::any o(absl::in_place_type<IntMoveOnlyCopyOnly>, 5, MoveOnly(),
162 EXPECT_EQ(5,
v.value);
165 TEST(AnyTest, InPlaceConstructionWithCV) {
166 const CopyOnly copy_only{};
168 MoveOnly(), copy_only);
170 EXPECT_EQ(5, v.value);
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);
178 EXPECT_EQ(5,
v.value);
181 TEST(AnyTest, InPlaceConstructionWithFunction) {
183 FunctionType*& construction_result =
absl::any_cast<FunctionType*&>(
o);
184 EXPECT_EQ(&FunctionToEmplace, construction_result);
187 TEST(AnyTest, InPlaceConstructionWithFunctionVariableTemplate) {
188 absl::any o(absl::in_place_type<FunctionType>, FunctionToEmplace);
190 EXPECT_EQ(&FunctionToEmplace, construction_result);
193 TEST(AnyTest, InPlaceConstructionWithArray) {
194 ArrayType ar = {5, 42};
197 EXPECT_EQ(&ar[0], construction_result);
200 TEST(AnyTest, InPlaceConstructionWithArrayVariableTemplate) {
201 ArrayType ar = {5, 42};
202 absl::any o(absl::in_place_type<ArrayType>, ar);
204 EXPECT_EQ(&ar[0], construction_result);
207 TEST(AnyTest, InPlaceConstructionIlist) {
208 const CopyOnly copy_only{};
210 MoveOnly(), copy_only);
212 std::vector<int> expected_values = {1, 2, 3, 4};
213 EXPECT_EQ(expected_values, v.values);
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);
221 std::vector<int> expected_values = {1, 2, 3, 4};
222 EXPECT_EQ(expected_values, v.values);
225 TEST(AnyTest, InPlaceConstructionIlistWithCV) {
226 const CopyOnly copy_only{};
228 {1, 2, 3, 4}, MoveOnly(), copy_only);
230 std::vector<int> expected_values = {1, 2, 3, 4};
231 EXPECT_EQ(expected_values, v.values);
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);
239 std::vector<int> expected_values = {1, 2, 3, 4};
240 EXPECT_EQ(expected_values, v.values);
243 TEST(AnyTest, InPlaceNoArgs) {
245 EXPECT_EQ(0, absl::any_cast<int&>(o));
248 TEST(AnyTest, InPlaceNoArgsVariableTemplate) {
250 EXPECT_EQ(0, absl::any_cast<int&>(o));
253 template <
typename Enabler,
typename T,
typename... Args>
254 struct CanEmplaceAnyImpl : std::false_type {};
256 template <
typename T,
typename... Args>
257 struct CanEmplaceAnyImpl<
259 std::declval<absl::any&>().emplace<T>(std::declval<Args>()...))>,
260 T, Args...> : std::true_type {};
262 template <
typename T,
typename... Args>
263 using CanEmplaceAny = CanEmplaceAnyImpl<void, T, Args...>;
265 TEST(AnyTest, Emplace) {
266 const CopyOnly copy_only{};
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);
275 EXPECT_EQ(5, v.value);
276 EXPECT_EQ(&emplace_result, &v);
282 TEST(AnyTest, EmplaceWithCV) {
283 const CopyOnly copy_only{};
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);
293 EXPECT_EQ(5, v.value);
294 EXPECT_EQ(&emplace_result, &v);
297 TEST(AnyTest, EmplaceWithFunction) {
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);
306 TEST(AnyTest, EmplaceWithArray) {
308 ArrayType ar = {5, 42};
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);
315 TEST(AnyTest, EmplaceIlist) {
316 const CopyOnly copy_only{};
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);
324 EXPECT_EQ(&v, &emplace_result);
325 std::vector<int> expected_values = {1, 2, 3, 4};
326 EXPECT_EQ(expected_values, v.values);
328 static_assert(!CanEmplaceAny<
int, std::initializer_list<int>>::
value,
"");
329 static_assert(!CanEmplaceAny<MoveOnlyWithListConstructor,
330 std::initializer_list<int>,
int>::
value,
334 TEST(AnyTest, EmplaceIlistWithCV) {
335 const CopyOnly copy_only{};
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(),
345 EXPECT_EQ(&v, &emplace_result);
346 std::vector<int> expected_values = {1, 2, 3, 4};
347 EXPECT_EQ(expected_values, v.values);
350 TEST(AnyTest, EmplaceNoArgs) {
353 EXPECT_EQ(0, absl::any_cast<int>(o));
356 TEST(AnyTest, ConversionConstruction) {
359 EXPECT_EQ(5, absl::any_cast<int>(o));
363 const CopyOnly copy_only(5);
365 EXPECT_EQ(5, absl::any_cast<CopyOnly&>(o).
value);
371 TEST(AnyTest, ConversionAssignment) {
375 EXPECT_EQ(5, absl::any_cast<int>(o));
379 const CopyOnly copy_only(5);
382 EXPECT_EQ(5, absl::any_cast<CopyOnly&>(o).
value);
392 #pragma warning( push ) 393 #pragma warning( disable : 4521) 399 struct WeirdConstructor42 {
400 explicit WeirdConstructor42(
int value) : value(value) {}
403 WeirdConstructor42(
const WeirdConstructor42& other) :
value(other.
value) {}
407 WeirdConstructor42& )
413 #pragma warning( pop ) 416 TEST(AnyTest, WeirdConversionConstruction) {
418 const WeirdConstructor42 source(5);
420 EXPECT_EQ(5, absl::any_cast<WeirdConstructor42&>(o).
value);
424 WeirdConstructor42 source(5);
426 EXPECT_EQ(42, absl::any_cast<WeirdConstructor42&>(o).
value);
430 TEST(AnyTest, WeirdConversionAssignment) {
432 const WeirdConstructor42 source(5);
435 EXPECT_EQ(5, absl::any_cast<WeirdConstructor42&>(o).
value);
439 WeirdConstructor42 source(5);
442 EXPECT_EQ(42, absl::any_cast<WeirdConstructor42&>(o).
value);
448 TEST(AnyTest, AnyCastValue) {
452 EXPECT_EQ(5, absl::any_cast<int>(o));
453 EXPECT_EQ(5, absl::any_cast<int>(AsConst(o)));
455 std::is_same<decltype(absl::any_cast<Value>(o)), Value>::
value,
"");
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)),
469 TEST(AnyTest, AnyCastReference) {
473 EXPECT_EQ(5, absl::any_cast<int&>(o));
474 EXPECT_EQ(5, absl::any_cast<const int&>(AsConst(o)));
476 std::is_same<decltype(absl::any_cast<Value&>(o)), Value&>::
value,
"");
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,
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))),
501 EXPECT_EQ(5, absl::any_cast<const int>(
std::move(o)));
503 std::is_same<decltype(absl::any_cast<const Value&&>(
std::move(o))),
504 const Value&&>::
value,
509 TEST(AnyTest, AnyCastPointer) {
512 EXPECT_EQ(
nullptr, absl::any_cast<char>(&o));
514 EXPECT_EQ(
nullptr, absl::any_cast<char>(&o));
516 EXPECT_EQ(
'a', *absl::any_cast<char>(&o));
518 std::is_same<decltype(absl::any_cast<Value>(&o)), Value*>::
value,
"");
523 EXPECT_EQ(
nullptr, absl::any_cast<const char>(&o));
525 EXPECT_EQ(
nullptr, absl::any_cast<const char>(&o));
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,
534 TEST(AnyTest, MakeAny) {
535 const CopyOnly copy_only{};
536 auto o = absl::make_any<IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
538 EXPECT_EQ(5, absl::any_cast<IntMoveOnlyCopyOnly&>(o).
value);
541 TEST(AnyTest, MakeAnyIList) {
542 const CopyOnly copy_only{};
544 absl::make_any<ListMoveOnlyCopyOnly>({1, 2, 3}, MoveOnly(), copy_only);
547 std::vector<int> expected_values = {1, 2, 3};
548 EXPECT_EQ(expected_values, v.values);
553 InstanceTracker tracker_raii;
556 absl::any o(absl::in_place_type<CopyableOnlyInstance>, 123);
560 const CopyableOnlyInstance* f2 =
absl::any_cast<CopyableOnlyInstance>(&o2);
561 EXPECT_EQ(123, f2->value());
566 const CopyableOnlyInstance* f3 =
absl::any_cast<CopyableOnlyInstance>(&o3);
567 EXPECT_EQ(123, f3->value());
573 EXPECT_EQ(4, absl::any_cast<int>(o4));
574 EXPECT_EQ(4, absl::any_cast<int>(o5));
578 EXPECT_EQ(4, absl::any_cast<int>(o4));
579 EXPECT_EQ(4, absl::any_cast<int>(o6));
583 TEST(AnyTest, Move) {
584 InstanceTracker tracker_raii;
587 any1.
emplace<CopyableOnlyInstance>(5);
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());
597 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any3).
value());
598 EXPECT_EQ(1, tracker_raii.copies());
602 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any4).
value());
603 EXPECT_EQ(1, tracker_raii.copies());
607 EXPECT_EQ(4, absl::any_cast<int>(o4));
609 EXPECT_EQ(4, absl::any_cast<int>(o4));
610 EXPECT_TRUE(o4.has_value());
615 EXPECT_EQ(5, absl::any_cast<int>(o5));
619 TEST(AnyTest, Reset) {
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());
642 #if defined(ABSL_HAVE_STD_ANY) 645 #define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...) \ 646 ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), absl::bad_any_cast, \ 652 #define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...) \ 653 ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), absl::bad_any_cast, \ 656 #endif // defined(ABSL_HAVE_STD_ANY) 658 TEST(AnyTest, ThrowBadAlloc) {
682 absl::any_cast<const float&&>(
absl::any{}));
698 BadCopyable() =
default;
699 BadCopyable(BadCopyable&&) =
default;
700 BadCopyable(
const BadCopyable&) {
701 #ifdef ABSL_HAVE_EXCEPTIONS 709 #define ABSL_ANY_TEST_EXPECT_BAD_COPY(...) \ 710 ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), BadCopy, "Bad copy") 713 TEST(AnyTest, FailedCopy) {
715 const BadCopyable bad{};
720 absl::any src(absl::in_place_type<BadCopyable>);
732 absl::any target(absl::in_place_type<BadCopyable>);
738 absl::any src(absl::in_place_type<BadCopyable>);
745 absl::any src(absl::in_place_type<BadCopyable>);
746 absl::any target(absl::in_place_type<BadCopyable>);
753 TEST(AnyTest, FailedEmplace) {
762 absl::any target(absl::in_place_type<int>);
764 #if defined(ABSL_HAVE_STD_ANY) && defined(__GLIBCXX__) 767 #define ABSL_GLIBCXX_ANY_EMPLACE_EXCEPTION_BUG 1 769 #if defined(ABSL_HAVE_EXCEPTIONS) && \ 770 !defined(ABSL_GLIBCXX_ANY_EMPLACE_EXCEPTION_BUG)
bool has_value() const noexcept
#define ABSL_RAW_LOG(severity,...)
typename std::decay< T >::type decay_t
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
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)))
VT & emplace(Args &&...args)
ValueType any_cast(const any &operand)
const std::type_info & type() const noexcept
typename type_traits_internal::VoidTImpl< Ts... >::type void_t
TEST(Symbolize, Unimplemented)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
#define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...)
#define ABSL_ANY_TEST_EXPECT_BAD_COPY(...)