00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/types/any.h"
00016
00017 #include <initializer_list>
00018 #include <type_traits>
00019 #include <utility>
00020 #include <vector>
00021
00022 #include "gtest/gtest.h"
00023 #include "absl/base/config.h"
00024 #include "absl/base/internal/exception_testing.h"
00025 #include "absl/base/internal/raw_logging.h"
00026 #include "absl/container/internal/test_instance_tracker.h"
00027
00028 namespace {
00029 using absl::test_internal::CopyableOnlyInstance;
00030 using absl::test_internal::InstanceTracker;
00031
00032 template <typename T>
00033 const T& AsConst(const T& t) {
00034 return t;
00035 }
00036
00037 struct MoveOnly {
00038 MoveOnly() = default;
00039 explicit MoveOnly(int value) : value(value) {}
00040 MoveOnly(MoveOnly&&) = default;
00041 MoveOnly& operator=(MoveOnly&&) = default;
00042
00043 int value = 0;
00044 };
00045
00046 struct CopyOnly {
00047 CopyOnly() = default;
00048 explicit CopyOnly(int value) : value(value) {}
00049 CopyOnly(CopyOnly&&) = delete;
00050 CopyOnly& operator=(CopyOnly&&) = delete;
00051 CopyOnly(const CopyOnly&) = default;
00052 CopyOnly& operator=(const CopyOnly&) = default;
00053
00054 int value = 0;
00055 };
00056
00057 struct MoveOnlyWithListConstructor {
00058 MoveOnlyWithListConstructor() = default;
00059 explicit MoveOnlyWithListConstructor(std::initializer_list<int> ,
00060 int value)
00061 : value(value) {}
00062 MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) = default;
00063 MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
00064 default;
00065
00066 int value = 0;
00067 };
00068
00069 struct IntMoveOnlyCopyOnly {
00070 IntMoveOnlyCopyOnly(int value, MoveOnly , CopyOnly )
00071 : value(value) {}
00072
00073 int value;
00074 };
00075
00076 struct ListMoveOnlyCopyOnly {
00077 ListMoveOnlyCopyOnly(std::initializer_list<int> ilist, MoveOnly ,
00078 CopyOnly )
00079 : values(ilist) {}
00080
00081 std::vector<int> values;
00082 };
00083
00084 using FunctionType = void();
00085 void FunctionToEmplace() {}
00086
00087 using ArrayType = int[2];
00088 using DecayedArray = absl::decay_t<ArrayType>;
00089
00090 TEST(AnyTest, Noexcept) {
00091 static_assert(std::is_nothrow_default_constructible<absl::any>(), "");
00092 static_assert(std::is_nothrow_move_constructible<absl::any>(), "");
00093 static_assert(std::is_nothrow_move_assignable<absl::any>(), "");
00094 static_assert(noexcept(std::declval<absl::any&>().has_value()), "");
00095 static_assert(noexcept(std::declval<absl::any&>().type()), "");
00096 static_assert(noexcept(absl::any_cast<int>(std::declval<absl::any*>())), "");
00097 static_assert(
00098 noexcept(std::declval<absl::any&>().swap(std::declval<absl::any&>())),
00099 "");
00100
00101 using std::swap;
00102 static_assert(
00103 noexcept(swap(std::declval<absl::any&>(), std::declval<absl::any&>())),
00104 "");
00105 }
00106
00107 TEST(AnyTest, HasValue) {
00108 absl::any o;
00109 EXPECT_FALSE(o.has_value());
00110 o.emplace<int>();
00111 EXPECT_TRUE(o.has_value());
00112 o.reset();
00113 EXPECT_FALSE(o.has_value());
00114 }
00115
00116 TEST(AnyTest, Type) {
00117 absl::any o;
00118 EXPECT_EQ(typeid(void), o.type());
00119 o.emplace<int>(5);
00120 EXPECT_EQ(typeid(int), o.type());
00121 o.emplace<float>(5.f);
00122 EXPECT_EQ(typeid(float), o.type());
00123 o.reset();
00124 EXPECT_EQ(typeid(void), o.type());
00125 }
00126
00127 TEST(AnyTest, EmptyPointerCast) {
00128
00129 {
00130 absl::any o;
00131 EXPECT_EQ(nullptr, absl::any_cast<int>(&o));
00132 o.emplace<int>();
00133 EXPECT_NE(nullptr, absl::any_cast<int>(&o));
00134 o.reset();
00135 EXPECT_EQ(nullptr, absl::any_cast<int>(&o));
00136 }
00137
00138
00139 {
00140 absl::any o;
00141 EXPECT_EQ(nullptr, absl::any_cast<int>(&AsConst(o)));
00142 o.emplace<int>();
00143 EXPECT_NE(nullptr, absl::any_cast<int>(&AsConst(o)));
00144 o.reset();
00145 EXPECT_EQ(nullptr, absl::any_cast<int>(&AsConst(o)));
00146 }
00147 }
00148
00149 TEST(AnyTest, InPlaceConstruction) {
00150 const CopyOnly copy_only{};
00151 absl::any o(absl::in_place_type_t<IntMoveOnlyCopyOnly>(), 5, MoveOnly(),
00152 copy_only);
00153 IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
00154 EXPECT_EQ(5, v.value);
00155 }
00156
00157 TEST(AnyTest, InPlaceConstructionVariableTemplate) {
00158 const CopyOnly copy_only{};
00159 absl::any o(absl::in_place_type<IntMoveOnlyCopyOnly>, 5, MoveOnly(),
00160 copy_only);
00161 auto& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
00162 EXPECT_EQ(5, v.value);
00163 }
00164
00165 TEST(AnyTest, InPlaceConstructionWithCV) {
00166 const CopyOnly copy_only{};
00167 absl::any o(absl::in_place_type_t<const volatile IntMoveOnlyCopyOnly>(), 5,
00168 MoveOnly(), copy_only);
00169 IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
00170 EXPECT_EQ(5, v.value);
00171 }
00172
00173 TEST(AnyTest, InPlaceConstructionWithCVVariableTemplate) {
00174 const CopyOnly copy_only{};
00175 absl::any o(absl::in_place_type<const volatile IntMoveOnlyCopyOnly>, 5,
00176 MoveOnly(), copy_only);
00177 auto& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
00178 EXPECT_EQ(5, v.value);
00179 }
00180
00181 TEST(AnyTest, InPlaceConstructionWithFunction) {
00182 absl::any o(absl::in_place_type_t<FunctionType>(), FunctionToEmplace);
00183 FunctionType*& construction_result = absl::any_cast<FunctionType*&>(o);
00184 EXPECT_EQ(&FunctionToEmplace, construction_result);
00185 }
00186
00187 TEST(AnyTest, InPlaceConstructionWithFunctionVariableTemplate) {
00188 absl::any o(absl::in_place_type<FunctionType>, FunctionToEmplace);
00189 auto& construction_result = absl::any_cast<FunctionType*&>(o);
00190 EXPECT_EQ(&FunctionToEmplace, construction_result);
00191 }
00192
00193 TEST(AnyTest, InPlaceConstructionWithArray) {
00194 ArrayType ar = {5, 42};
00195 absl::any o(absl::in_place_type_t<ArrayType>(), ar);
00196 DecayedArray& construction_result = absl::any_cast<DecayedArray&>(o);
00197 EXPECT_EQ(&ar[0], construction_result);
00198 }
00199
00200 TEST(AnyTest, InPlaceConstructionWithArrayVariableTemplate) {
00201 ArrayType ar = {5, 42};
00202 absl::any o(absl::in_place_type<ArrayType>, ar);
00203 auto& construction_result = absl::any_cast<DecayedArray&>(o);
00204 EXPECT_EQ(&ar[0], construction_result);
00205 }
00206
00207 TEST(AnyTest, InPlaceConstructionIlist) {
00208 const CopyOnly copy_only{};
00209 absl::any o(absl::in_place_type_t<ListMoveOnlyCopyOnly>(), {1, 2, 3, 4},
00210 MoveOnly(), copy_only);
00211 ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00212 std::vector<int> expected_values = {1, 2, 3, 4};
00213 EXPECT_EQ(expected_values, v.values);
00214 }
00215
00216 TEST(AnyTest, InPlaceConstructionIlistVariableTemplate) {
00217 const CopyOnly copy_only{};
00218 absl::any o(absl::in_place_type<ListMoveOnlyCopyOnly>, {1, 2, 3, 4},
00219 MoveOnly(), copy_only);
00220 auto& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00221 std::vector<int> expected_values = {1, 2, 3, 4};
00222 EXPECT_EQ(expected_values, v.values);
00223 }
00224
00225 TEST(AnyTest, InPlaceConstructionIlistWithCV) {
00226 const CopyOnly copy_only{};
00227 absl::any o(absl::in_place_type_t<const volatile ListMoveOnlyCopyOnly>(),
00228 {1, 2, 3, 4}, MoveOnly(), copy_only);
00229 ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00230 std::vector<int> expected_values = {1, 2, 3, 4};
00231 EXPECT_EQ(expected_values, v.values);
00232 }
00233
00234 TEST(AnyTest, InPlaceConstructionIlistWithCVVariableTemplate) {
00235 const CopyOnly copy_only{};
00236 absl::any o(absl::in_place_type<const volatile ListMoveOnlyCopyOnly>,
00237 {1, 2, 3, 4}, MoveOnly(), copy_only);
00238 auto& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00239 std::vector<int> expected_values = {1, 2, 3, 4};
00240 EXPECT_EQ(expected_values, v.values);
00241 }
00242
00243 TEST(AnyTest, InPlaceNoArgs) {
00244 absl::any o(absl::in_place_type_t<int>{});
00245 EXPECT_EQ(0, absl::any_cast<int&>(o));
00246 }
00247
00248 TEST(AnyTest, InPlaceNoArgsVariableTemplate) {
00249 absl::any o(absl::in_place_type<int>);
00250 EXPECT_EQ(0, absl::any_cast<int&>(o));
00251 }
00252
00253 template <typename Enabler, typename T, typename... Args>
00254 struct CanEmplaceAnyImpl : std::false_type {};
00255
00256 template <typename T, typename... Args>
00257 struct CanEmplaceAnyImpl<
00258 absl::void_t<decltype(
00259 std::declval<absl::any&>().emplace<T>(std::declval<Args>()...))>,
00260 T, Args...> : std::true_type {};
00261
00262 template <typename T, typename... Args>
00263 using CanEmplaceAny = CanEmplaceAnyImpl<void, T, Args...>;
00264
00265 TEST(AnyTest, Emplace) {
00266 const CopyOnly copy_only{};
00267 absl::any o;
00268 EXPECT_TRUE((std::is_same<decltype(o.emplace<IntMoveOnlyCopyOnly>(
00269 5, MoveOnly(), copy_only)),
00270 IntMoveOnlyCopyOnly&>::value));
00271 IntMoveOnlyCopyOnly& emplace_result =
00272 o.emplace<IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
00273 EXPECT_EQ(5, emplace_result.value);
00274 IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
00275 EXPECT_EQ(5, v.value);
00276 EXPECT_EQ(&emplace_result, &v);
00277
00278 static_assert(!CanEmplaceAny<int, int, int>::value, "");
00279 static_assert(!CanEmplaceAny<MoveOnly, MoveOnly>::value, "");
00280 }
00281
00282 TEST(AnyTest, EmplaceWithCV) {
00283 const CopyOnly copy_only{};
00284 absl::any o;
00285 EXPECT_TRUE(
00286 (std::is_same<decltype(o.emplace<const volatile IntMoveOnlyCopyOnly>(
00287 5, MoveOnly(), copy_only)),
00288 IntMoveOnlyCopyOnly&>::value));
00289 IntMoveOnlyCopyOnly& emplace_result =
00290 o.emplace<const volatile IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
00291 EXPECT_EQ(5, emplace_result.value);
00292 IntMoveOnlyCopyOnly& v = absl::any_cast<IntMoveOnlyCopyOnly&>(o);
00293 EXPECT_EQ(5, v.value);
00294 EXPECT_EQ(&emplace_result, &v);
00295 }
00296
00297 TEST(AnyTest, EmplaceWithFunction) {
00298 absl::any o;
00299 EXPECT_TRUE(
00300 (std::is_same<decltype(o.emplace<FunctionType>(FunctionToEmplace)),
00301 FunctionType*&>::value));
00302 FunctionType*& emplace_result = o.emplace<FunctionType>(FunctionToEmplace);
00303 EXPECT_EQ(&FunctionToEmplace, emplace_result);
00304 }
00305
00306 TEST(AnyTest, EmplaceWithArray) {
00307 absl::any o;
00308 ArrayType ar = {5, 42};
00309 EXPECT_TRUE(
00310 (std::is_same<decltype(o.emplace<ArrayType>(ar)), DecayedArray&>::value));
00311 DecayedArray& emplace_result = o.emplace<ArrayType>(ar);
00312 EXPECT_EQ(&ar[0], emplace_result);
00313 }
00314
00315 TEST(AnyTest, EmplaceIlist) {
00316 const CopyOnly copy_only{};
00317 absl::any o;
00318 EXPECT_TRUE((std::is_same<decltype(o.emplace<ListMoveOnlyCopyOnly>(
00319 {1, 2, 3, 4}, MoveOnly(), copy_only)),
00320 ListMoveOnlyCopyOnly&>::value));
00321 ListMoveOnlyCopyOnly& emplace_result =
00322 o.emplace<ListMoveOnlyCopyOnly>({1, 2, 3, 4}, MoveOnly(), copy_only);
00323 ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00324 EXPECT_EQ(&v, &emplace_result);
00325 std::vector<int> expected_values = {1, 2, 3, 4};
00326 EXPECT_EQ(expected_values, v.values);
00327
00328 static_assert(!CanEmplaceAny<int, std::initializer_list<int>>::value, "");
00329 static_assert(!CanEmplaceAny<MoveOnlyWithListConstructor,
00330 std::initializer_list<int>, int>::value,
00331 "");
00332 }
00333
00334 TEST(AnyTest, EmplaceIlistWithCV) {
00335 const CopyOnly copy_only{};
00336 absl::any o;
00337 EXPECT_TRUE(
00338 (std::is_same<decltype(o.emplace<const volatile ListMoveOnlyCopyOnly>(
00339 {1, 2, 3, 4}, MoveOnly(), copy_only)),
00340 ListMoveOnlyCopyOnly&>::value));
00341 ListMoveOnlyCopyOnly& emplace_result =
00342 o.emplace<const volatile ListMoveOnlyCopyOnly>({1, 2, 3, 4}, MoveOnly(),
00343 copy_only);
00344 ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00345 EXPECT_EQ(&v, &emplace_result);
00346 std::vector<int> expected_values = {1, 2, 3, 4};
00347 EXPECT_EQ(expected_values, v.values);
00348 }
00349
00350 TEST(AnyTest, EmplaceNoArgs) {
00351 absl::any o;
00352 o.emplace<int>();
00353 EXPECT_EQ(0, absl::any_cast<int>(o));
00354 }
00355
00356 TEST(AnyTest, ConversionConstruction) {
00357 {
00358 absl::any o = 5;
00359 EXPECT_EQ(5, absl::any_cast<int>(o));
00360 }
00361
00362 {
00363 const CopyOnly copy_only(5);
00364 absl::any o = copy_only;
00365 EXPECT_EQ(5, absl::any_cast<CopyOnly&>(o).value);
00366 }
00367
00368 static_assert(!std::is_convertible<MoveOnly, absl::any>::value, "");
00369 }
00370
00371 TEST(AnyTest, ConversionAssignment) {
00372 {
00373 absl::any o;
00374 o = 5;
00375 EXPECT_EQ(5, absl::any_cast<int>(o));
00376 }
00377
00378 {
00379 const CopyOnly copy_only(5);
00380 absl::any o;
00381 o = copy_only;
00382 EXPECT_EQ(5, absl::any_cast<CopyOnly&>(o).value);
00383 }
00384
00385 static_assert(!std::is_assignable<MoveOnly, absl::any>::value, "");
00386 }
00387
00388
00389
00390
00391 #ifdef _MSC_VER
00392 #pragma warning( push )
00393 #pragma warning( disable : 4521)
00394 #endif
00395
00396
00397
00398
00399 struct WeirdConstructor42 {
00400 explicit WeirdConstructor42(int value) : value(value) {}
00401
00402
00403 WeirdConstructor42(const WeirdConstructor42& other) : value(other.value) {}
00404
00405
00406 WeirdConstructor42(
00407 WeirdConstructor42& )
00408 : value(42) {}
00409
00410 int value;
00411 };
00412 #ifdef _MSC_VER
00413 #pragma warning( pop )
00414 #endif
00415
00416 TEST(AnyTest, WeirdConversionConstruction) {
00417 {
00418 const WeirdConstructor42 source(5);
00419 absl::any o = source;
00420 EXPECT_EQ(5, absl::any_cast<WeirdConstructor42&>(o).value);
00421 }
00422
00423 {
00424 WeirdConstructor42 source(5);
00425 absl::any o = source;
00426 EXPECT_EQ(42, absl::any_cast<WeirdConstructor42&>(o).value);
00427 }
00428 }
00429
00430 TEST(AnyTest, WeirdConversionAssignment) {
00431 {
00432 const WeirdConstructor42 source(5);
00433 absl::any o;
00434 o = source;
00435 EXPECT_EQ(5, absl::any_cast<WeirdConstructor42&>(o).value);
00436 }
00437
00438 {
00439 WeirdConstructor42 source(5);
00440 absl::any o;
00441 o = source;
00442 EXPECT_EQ(42, absl::any_cast<WeirdConstructor42&>(o).value);
00443 }
00444 }
00445
00446 struct Value {};
00447
00448 TEST(AnyTest, AnyCastValue) {
00449 {
00450 absl::any o;
00451 o.emplace<int>(5);
00452 EXPECT_EQ(5, absl::any_cast<int>(o));
00453 EXPECT_EQ(5, absl::any_cast<int>(AsConst(o)));
00454 static_assert(
00455 std::is_same<decltype(absl::any_cast<Value>(o)), Value>::value, "");
00456 }
00457
00458 {
00459 absl::any o;
00460 o.emplace<int>(5);
00461 EXPECT_EQ(5, absl::any_cast<const int>(o));
00462 EXPECT_EQ(5, absl::any_cast<const int>(AsConst(o)));
00463 static_assert(std::is_same<decltype(absl::any_cast<const Value>(o)),
00464 const Value>::value,
00465 "");
00466 }
00467 }
00468
00469 TEST(AnyTest, AnyCastReference) {
00470 {
00471 absl::any o;
00472 o.emplace<int>(5);
00473 EXPECT_EQ(5, absl::any_cast<int&>(o));
00474 EXPECT_EQ(5, absl::any_cast<const int&>(AsConst(o)));
00475 static_assert(
00476 std::is_same<decltype(absl::any_cast<Value&>(o)), Value&>::value, "");
00477 }
00478
00479 {
00480 absl::any o;
00481 o.emplace<int>(5);
00482 EXPECT_EQ(5, absl::any_cast<const int>(o));
00483 EXPECT_EQ(5, absl::any_cast<const int>(AsConst(o)));
00484 static_assert(std::is_same<decltype(absl::any_cast<const Value&>(o)),
00485 const Value&>::value,
00486 "");
00487 }
00488
00489 {
00490 absl::any o;
00491 o.emplace<int>(5);
00492 EXPECT_EQ(5, absl::any_cast<int&&>(std::move(o)));
00493 static_assert(std::is_same<decltype(absl::any_cast<Value&&>(std::move(o))),
00494 Value&&>::value,
00495 "");
00496 }
00497
00498 {
00499 absl::any o;
00500 o.emplace<int>(5);
00501 EXPECT_EQ(5, absl::any_cast<const int>(std::move(o)));
00502 static_assert(
00503 std::is_same<decltype(absl::any_cast<const Value&&>(std::move(o))),
00504 const Value&&>::value,
00505 "");
00506 }
00507 }
00508
00509 TEST(AnyTest, AnyCastPointer) {
00510 {
00511 absl::any o;
00512 EXPECT_EQ(nullptr, absl::any_cast<char>(&o));
00513 o.emplace<int>(5);
00514 EXPECT_EQ(nullptr, absl::any_cast<char>(&o));
00515 o.emplace<char>('a');
00516 EXPECT_EQ('a', *absl::any_cast<char>(&o));
00517 static_assert(
00518 std::is_same<decltype(absl::any_cast<Value>(&o)), Value*>::value, "");
00519 }
00520
00521 {
00522 absl::any o;
00523 EXPECT_EQ(nullptr, absl::any_cast<const char>(&o));
00524 o.emplace<int>(5);
00525 EXPECT_EQ(nullptr, absl::any_cast<const char>(&o));
00526 o.emplace<char>('a');
00527 EXPECT_EQ('a', *absl::any_cast<const char>(&o));
00528 static_assert(std::is_same<decltype(absl::any_cast<const Value>(&o)),
00529 const Value*>::value,
00530 "");
00531 }
00532 }
00533
00534 TEST(AnyTest, MakeAny) {
00535 const CopyOnly copy_only{};
00536 auto o = absl::make_any<IntMoveOnlyCopyOnly>(5, MoveOnly(), copy_only);
00537 static_assert(std::is_same<decltype(o), absl::any>::value, "");
00538 EXPECT_EQ(5, absl::any_cast<IntMoveOnlyCopyOnly&>(o).value);
00539 }
00540
00541 TEST(AnyTest, MakeAnyIList) {
00542 const CopyOnly copy_only{};
00543 auto o =
00544 absl::make_any<ListMoveOnlyCopyOnly>({1, 2, 3}, MoveOnly(), copy_only);
00545 static_assert(std::is_same<decltype(o), absl::any>::value, "");
00546 ListMoveOnlyCopyOnly& v = absl::any_cast<ListMoveOnlyCopyOnly&>(o);
00547 std::vector<int> expected_values = {1, 2, 3};
00548 EXPECT_EQ(expected_values, v.values);
00549 }
00550
00551
00552 TEST(AnyTest, Copy) {
00553 InstanceTracker tracker_raii;
00554
00555 {
00556 absl::any o(absl::in_place_type<CopyableOnlyInstance>, 123);
00557 CopyableOnlyInstance* f1 = absl::any_cast<CopyableOnlyInstance>(&o);
00558
00559 absl::any o2(o);
00560 const CopyableOnlyInstance* f2 = absl::any_cast<CopyableOnlyInstance>(&o2);
00561 EXPECT_EQ(123, f2->value());
00562 EXPECT_NE(f1, f2);
00563
00564 absl::any o3;
00565 o3 = o2;
00566 const CopyableOnlyInstance* f3 = absl::any_cast<CopyableOnlyInstance>(&o3);
00567 EXPECT_EQ(123, f3->value());
00568 EXPECT_NE(f2, f3);
00569
00570 const absl::any o4(4);
00571
00572 absl::any o5 = o4;
00573 EXPECT_EQ(4, absl::any_cast<int>(o4));
00574 EXPECT_EQ(4, absl::any_cast<int>(o5));
00575
00576
00577 absl::any o6 = std::move(o4);
00578 EXPECT_EQ(4, absl::any_cast<int>(o4));
00579 EXPECT_EQ(4, absl::any_cast<int>(o6));
00580 }
00581 }
00582
00583 TEST(AnyTest, Move) {
00584 InstanceTracker tracker_raii;
00585
00586 absl::any any1;
00587 any1.emplace<CopyableOnlyInstance>(5);
00588
00589
00590 absl::any any2 = any1;
00591 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any1).value());
00592 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any2).value());
00593 EXPECT_EQ(1, tracker_raii.copies());
00594
00595
00596 absl::any any3 = std::move(any2);
00597 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any3).value());
00598 EXPECT_EQ(1, tracker_raii.copies());
00599
00600 absl::any any4;
00601 any4 = std::move(any3);
00602 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(any4).value());
00603 EXPECT_EQ(1, tracker_raii.copies());
00604
00605 absl::any tmp4(4);
00606 absl::any o4(std::move(tmp4));
00607 EXPECT_EQ(4, absl::any_cast<int>(o4));
00608 o4 = *&o4;
00609 EXPECT_EQ(4, absl::any_cast<int>(o4));
00610 EXPECT_TRUE(o4.has_value());
00611
00612 absl::any o5;
00613 absl::any tmp5(5);
00614 o5 = std::move(tmp5);
00615 EXPECT_EQ(5, absl::any_cast<int>(o5));
00616 }
00617
00618
00619 TEST(AnyTest, Reset) {
00620 absl::any o;
00621 o.emplace<int>();
00622
00623 o.reset();
00624 EXPECT_FALSE(o.has_value());
00625
00626 o.emplace<char>();
00627 EXPECT_TRUE(o.has_value());
00628 }
00629
00630 TEST(AnyTest, ConversionConstructionCausesOneCopy) {
00631 InstanceTracker tracker_raii;
00632 CopyableOnlyInstance counter(5);
00633 absl::any o(counter);
00634 EXPECT_EQ(5, absl::any_cast<CopyableOnlyInstance&>(o).value());
00635 EXPECT_EQ(1, tracker_raii.copies());
00636 }
00637
00639
00641
00642 #if defined(ABSL_HAVE_STD_ANY)
00643
00644
00645 #define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...) \
00646 ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), absl::bad_any_cast, \
00647 "")
00648
00649 #else
00650
00651
00652 #define ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(...) \
00653 ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), absl::bad_any_cast, \
00654 "Bad any cast")
00655
00656 #endif // defined(ABSL_HAVE_STD_ANY)
00657
00658 TEST(AnyTest, ThrowBadAlloc) {
00659 {
00660 absl::any a;
00661 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int&>(a));
00662 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int&>(a));
00663 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int&&>(absl::any{}));
00664 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int&&>(absl::any{}));
00665 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int>(a));
00666 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int>(a));
00667 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int>(absl::any{}));
00668 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int>(absl::any{}));
00669
00670
00671 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int&>(AsConst(a)));
00672 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<int>(AsConst(a)));
00673 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const int>(AsConst(a)));
00674 }
00675
00676 {
00677 absl::any a(absl::in_place_type<int>);
00678 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float&>(a));
00679 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float&>(a));
00680 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float&&>(absl::any{}));
00681 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(
00682 absl::any_cast<const float&&>(absl::any{}));
00683 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float>(a));
00684 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float>(a));
00685 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float>(absl::any{}));
00686 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float>(absl::any{}));
00687
00688
00689 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float&>(AsConst(a)));
00690 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<float>(AsConst(a)));
00691 ABSL_ANY_TEST_EXPECT_BAD_ANY_CAST(absl::any_cast<const float>(AsConst(a)));
00692 }
00693 }
00694
00695 class BadCopy {};
00696
00697 struct BadCopyable {
00698 BadCopyable() = default;
00699 BadCopyable(BadCopyable&&) = default;
00700 BadCopyable(const BadCopyable&) {
00701 #ifdef ABSL_HAVE_EXCEPTIONS
00702 throw BadCopy();
00703 #else
00704 ABSL_RAW_LOG(FATAL, "Bad copy");
00705 #endif
00706 }
00707 };
00708
00709 #define ABSL_ANY_TEST_EXPECT_BAD_COPY(...) \
00710 ABSL_BASE_INTERNAL_EXPECT_FAIL((__VA_ARGS__), BadCopy, "Bad copy")
00711
00712
00713 TEST(AnyTest, FailedCopy) {
00714 {
00715 const BadCopyable bad{};
00716 ABSL_ANY_TEST_EXPECT_BAD_COPY(absl::any{bad});
00717 }
00718
00719 {
00720 absl::any src(absl::in_place_type<BadCopyable>);
00721 ABSL_ANY_TEST_EXPECT_BAD_COPY(absl::any{src});
00722 }
00723
00724 {
00725 BadCopyable bad;
00726 absl::any target;
00727 ABSL_ANY_TEST_EXPECT_BAD_COPY(target = bad);
00728 }
00729
00730 {
00731 BadCopyable bad;
00732 absl::any target(absl::in_place_type<BadCopyable>);
00733 ABSL_ANY_TEST_EXPECT_BAD_COPY(target = bad);
00734 EXPECT_TRUE(target.has_value());
00735 }
00736
00737 {
00738 absl::any src(absl::in_place_type<BadCopyable>);
00739 absl::any target;
00740 ABSL_ANY_TEST_EXPECT_BAD_COPY(target = src);
00741 EXPECT_FALSE(target.has_value());
00742 }
00743
00744 {
00745 absl::any src(absl::in_place_type<BadCopyable>);
00746 absl::any target(absl::in_place_type<BadCopyable>);
00747 ABSL_ANY_TEST_EXPECT_BAD_COPY(target = src);
00748 EXPECT_TRUE(target.has_value());
00749 }
00750 }
00751
00752
00753 TEST(AnyTest, FailedEmplace) {
00754 {
00755 BadCopyable bad;
00756 absl::any target;
00757 ABSL_ANY_TEST_EXPECT_BAD_COPY(target.emplace<BadCopyable>(bad));
00758 }
00759
00760 {
00761 BadCopyable bad;
00762 absl::any target(absl::in_place_type<int>);
00763 ABSL_ANY_TEST_EXPECT_BAD_COPY(target.emplace<BadCopyable>(bad));
00764 #if defined(ABSL_HAVE_STD_ANY) && defined(__GLIBCXX__)
00765
00766
00767 #define ABSL_GLIBCXX_ANY_EMPLACE_EXCEPTION_BUG 1
00768 #endif
00769 #if defined(ABSL_HAVE_EXCEPTIONS) && \
00770 !defined(ABSL_GLIBCXX_ANY_EMPLACE_EXCEPTION_BUG)
00771 EXPECT_FALSE(target.has_value());
00772 #endif
00773 }
00774 }
00775
00776 }