| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797 |
- #include <algorithm>
- #include <memory>
- #include <unordered_map>
- #include <vector>
- #include <gtest/gtest.h>
- #include <entt/core/any.hpp>
- struct fat {
- double value[4];
- inline static int counter = 0;
- ~fat() { ++counter; }
- bool operator==(const fat &other) const {
- return std::equal(std::begin(value), std::end(value), std::begin(other.value), std::end(other.value));
- }
- };
- struct empty {
- inline static int counter = 0;
- ~empty() { ++counter; }
- };
- struct not_comparable {
- bool operator==(const not_comparable &) const = delete;
- };
- TEST(Any, SBO) {
- entt::any any{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<char>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<char>(any), 'c');
- }
- TEST(Any, NoSBO) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{instance};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- }
- TEST(Any, Empty) {
- entt::any any{};
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.type());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(any.data(), nullptr);
- }
- TEST(Any, SBOInPlaceTypeConstruction) {
- entt::any any{std::in_place_type<int>, 42};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- auto other = as_ref(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOAsRefConstruction) {
- int value = 42;
- entt::any any{std::ref(value)};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<const int>(&any), &value);
- ASSERT_EQ(entt::any_cast<int>(&any), &value);
- ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
- ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
- ASSERT_EQ(entt::any_cast<const int &>(any), 42);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_EQ(any.data(), &value);
- ASSERT_EQ(std::as_const(any).data(), &value);
- auto other = as_ref(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOAsConstRefConstruction) {
- int value = 42;
- entt::any any{std::cref(value)};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<const int>(&any), &value);
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
- ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
- ASSERT_EQ(entt::any_cast<const int &>(any), 42);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_EQ(any.data(), nullptr);
- ASSERT_EQ(std::as_const(any).data(), &value);
- auto other = as_ref(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOCopyConstruction) {
- entt::any any{42};
- entt::any other{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- }
- TEST(Any, SBOCopyAssignment) {
- entt::any any{42};
- entt::any other{3};
- other = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- }
- TEST(Any, SBOMoveConstruction) {
- entt::any any{42};
- entt::any other{std::move(any)};
- ASSERT_FALSE(any);
- ASSERT_TRUE(other);
- ASSERT_FALSE(any.type());
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- }
- TEST(Any, SBOMoveAssignment) {
- entt::any any{42};
- entt::any other{3};
- other = std::move(any);
- ASSERT_FALSE(any);
- ASSERT_TRUE(other);
- ASSERT_FALSE(any.type());
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- }
- TEST(Any, SBODirectAssignment) {
- entt::any any{};
- any = 42;
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- }
- TEST(Any, NoSBOInPlaceTypeConstruction) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{std::in_place_type<fat>, instance};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- auto other = as_ref(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{{.1, .2, .3, .4}}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, NoSBOAsRefConstruction) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{std::ref(instance)};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
- ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
- ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
- ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
- ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- ASSERT_EQ(any.data(), &instance);
- ASSERT_EQ(std::as_const(any).data(), &instance);
- auto other = as_ref(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{{.1, .2, .3, .4}}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, NoSBOAsConstRefConstruction) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{std::cref(instance)};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
- ASSERT_EQ(entt::any_cast<fat>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
- ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
- ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- ASSERT_EQ(any.data(), nullptr);
- ASSERT_EQ(std::as_const(any).data(), &instance);
- auto other = as_ref(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{{.1, .2, .3, .4}}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, NoSBOCopyConstruction) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{instance};
- entt::any other{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBOCopyAssignment) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{instance};
- entt::any other{3};
- other = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBOMoveConstruction) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{instance};
- entt::any other{std::move(any)};
- ASSERT_FALSE(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBOMoveAssignment) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{instance};
- entt::any other{3};
- other = std::move(any);
- ASSERT_FALSE(any);
- ASSERT_TRUE(other);
- ASSERT_EQ(other.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBODirectAssignment) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{};
- any = instance;
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- }
- TEST(Any, VoidInPlaceTypeConstruction) {
- entt::any any{std::in_place_type<void>};
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.type());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- }
- TEST(Any, VoidCopyConstruction) {
- entt::any any{std::in_place_type<void>};
- entt::any other{any};
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(any.type());
- ASSERT_FALSE(other.type());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, VoidCopyAssignment) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::in_place_type<void>};
- other = any;
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(any.type());
- ASSERT_FALSE(other.type());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, VoidMoveConstruction) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::move(any)};
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(any.type());
- ASSERT_FALSE(other.type());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, VoidMoveAssignment) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::in_place_type<void>};
- other = std::move(any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(any.type());
- ASSERT_FALSE(other.type());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, SBOMoveInvalidate) {
- entt::any any{42};
- entt::any other{std::move(any)};
- entt::any valid = std::move(other);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_TRUE(valid);
- }
- TEST(Any, NoSBOMoveInvalidate) {
- fat instance{{.1, .2, .3, .4}};
- entt::any any{instance};
- entt::any other{std::move(any)};
- entt::any valid = std::move(other);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_TRUE(valid);
- }
- TEST(Any, VoidMoveInvalidate) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::move(any)};
- entt::any valid = std::move(other);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(valid);
- }
- TEST(Any, SBODestruction) {
- {
- entt::any any{empty{}};
- empty::counter = 0;
- }
- ASSERT_EQ(empty::counter, 1);
- }
- TEST(Any, NoSBODestruction) {
- {
- entt::any any{fat{}};
- fat::counter = 0;
- }
- ASSERT_EQ(fat::counter, 1);
- }
- TEST(Any, VoidDestruction) {
- // just let asan tell us if everything is ok here
- [[maybe_unused]] entt::any any{std::in_place_type<void>};
- }
- TEST(Any, Emplace) {
- entt::any any{};
- any.emplace<int>(42);
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- }
- TEST(Any, EmplaceVoid) {
- entt::any any{};
- any.emplace<void>();
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.type());
- }
- TEST(Any, SBOSwap) {
- entt::any lhs{'c'};
- entt::any rhs{42};
- std::swap(lhs, rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<int>());
- ASSERT_EQ(rhs.type(), entt::type_id<char>());
- ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<int>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<int>(lhs), 42);
- ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
- }
- TEST(Any, NoSBOSwap) {
- entt::any lhs{fat{{.1, .2, .3, .4}}};
- entt::any rhs{fat{{.4, .3, .2, .1}}};
- std::swap(lhs, rhs);
- ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{{.4, .3, .2, .1}}));
- ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{{.1, .2, .3, .4}}));
- }
- TEST(Any, VoidSwap) {
- entt::any lhs{std::in_place_type<void>};
- entt::any rhs{std::in_place_type<void>};
- const auto *pre = lhs.data();
- std::swap(lhs, rhs);
- ASSERT_EQ(pre, lhs.data());
- }
- TEST(Any, SBOWithNoSBOSwap) {
- entt::any lhs{fat{{.1, .2, .3, .4}}};
- entt::any rhs{'c'};
- std::swap(lhs, rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<char>());
- ASSERT_EQ(rhs.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
- ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{{.1, .2, .3, .4}}));
- }
- TEST(Any, SBOWithRefSwap) {
- int value = 3;
- entt::any lhs{std::ref(value)};
- entt::any rhs{'c'};
- std::swap(lhs, rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<char>());
- ASSERT_EQ(rhs.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
- ASSERT_EQ(entt::any_cast<int>(rhs), 3);
- ASSERT_EQ(rhs.data(), &value);
- }
- TEST(Any, SBOWithConstRefSwap) {
- int value = 3;
- entt::any lhs{std::cref(value)};
- entt::any rhs{'c'};
- std::swap(lhs, rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<char>());
- ASSERT_EQ(rhs.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
- ASSERT_EQ(entt::any_cast<int>(rhs), 3);
- ASSERT_EQ(rhs.data(), nullptr);
- ASSERT_EQ(std::as_const(rhs).data(), &value);
- }
- TEST(Any, SBOWithEmptySwap) {
- entt::any lhs{'c'};
- entt::any rhs{};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_EQ(rhs.type(), entt::type_id<char>());
- ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
- std::swap(lhs, rhs);
- ASSERT_FALSE(rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<char>());
- ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
- }
- TEST(Any, SBOWithVoidSwap) {
- entt::any lhs{'c'};
- entt::any rhs{std::in_place_type<void>};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_EQ(rhs.type(), entt::type_id<char>());
- ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
- std::swap(lhs, rhs);
- ASSERT_FALSE(rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<char>());
- ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
- }
- TEST(Any, NoSBOWithRefSwap) {
- int value = 3;
- entt::any lhs{std::ref(value)};
- entt::any rhs{fat{{.1, .2, .3, .4}}};
- std::swap(lhs, rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<fat>());
- ASSERT_EQ(rhs.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{{.1, .2, .3, .4}}));
- ASSERT_EQ(entt::any_cast<int>(rhs), 3);
- ASSERT_EQ(rhs.data(), &value);
- }
- TEST(Any, NoSBOWithConstRefSwap) {
- int value = 3;
- entt::any lhs{std::cref(value)};
- entt::any rhs{fat{{.1, .2, .3, .4}}};
- std::swap(lhs, rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<fat>());
- ASSERT_EQ(rhs.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{{.1, .2, .3, .4}}));
- ASSERT_EQ(entt::any_cast<int>(rhs), 3);
- ASSERT_EQ(rhs.data(), nullptr);
- ASSERT_EQ(std::as_const(rhs).data(), &value);
- }
- TEST(Any, NoSBOWithEmptySwap) {
- entt::any lhs{fat{{.1, .2, .3, .4}}};
- entt::any rhs{};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_EQ(rhs.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{{.1, .2, .3, .4}}));
- std::swap(lhs, rhs);
- ASSERT_FALSE(rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{{.1, .2, .3, .4}}));
- }
- TEST(Any, NoSBOWithVoidSwap) {
- entt::any lhs{fat{{.1, .2, .3, .4}}};
- entt::any rhs{std::in_place_type<void>};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_EQ(rhs.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{{.1, .2, .3, .4}}));
- std::swap(lhs, rhs);
- ASSERT_FALSE(rhs);
- ASSERT_EQ(lhs.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{{.1, .2, .3, .4}}));
- }
- TEST(Any, AsRef) {
- entt::any any{42};
- auto ref = as_ref(any);
- auto cref = as_ref(std::as_const(any));
- ASSERT_EQ(entt::any_cast<int>(&any), any.data());
- ASSERT_EQ(entt::any_cast<int>(&ref), any.data());
- ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<const int>(&any), any.data());
- ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
- ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_EQ(entt::any_cast<int>(ref), 42);
- ASSERT_EQ(entt::any_cast<int>(cref), 42);
- ASSERT_EQ(entt::any_cast<const int>(any), 42);
- ASSERT_EQ(entt::any_cast<const int>(ref), 42);
- ASSERT_EQ(entt::any_cast<const int>(cref), 42);
- ASSERT_EQ(entt::any_cast<int &>(any), 42);
- ASSERT_EQ(entt::any_cast<const int &>(any), 42);
- ASSERT_EQ(entt::any_cast<int &>(ref), 42);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
- ASSERT_DEATH(entt::any_cast<int &>(cref), ".*");
- ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
- entt::any_cast<int &>(any) = 3;
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- ASSERT_EQ(entt::any_cast<int>(ref), 3);
- ASSERT_EQ(entt::any_cast<int>(cref), 3);
- std::swap(ref, cref);
- ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
- ASSERT_EQ(entt::any_cast<int>(&cref), any.data());
- ref = as_ref(ref);
- cref = as_ref(std::as_const(cref));
- ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
- ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
- ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
- ASSERT_DEATH(entt::any_cast<int &>(ref), ".*");
- ASSERT_DEATH(entt::any_cast<int &>(cref), ".*");
- ASSERT_EQ(entt::any_cast<const int &>(ref), 3);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 3);
- ref = 42;
- cref = 42;
- ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
- ASSERT_NE(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<int &>(ref), 42);
- ASSERT_EQ(entt::any_cast<int &>(cref), 42);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
- ASSERT_NE(entt::any_cast<int>(&ref), any.data());
- ASSERT_NE(entt::any_cast<int>(&cref), any.data());
- }
- TEST(Any, Comparable) {
- auto test = [](entt::any any, entt::any other) {
- ASSERT_EQ(any, any);
- ASSERT_NE(other, any);
- ASSERT_NE(any, entt::any{});
- ASSERT_TRUE(any == any);
- ASSERT_FALSE(other == any);
- ASSERT_TRUE(any != other);
- ASSERT_TRUE(entt::any{} != any);
- };
- int value = 42;
- test('c', 'a');
- test(fat{{.1, .2, .3, .4}}, fat{{.0, .1, .2, .3}});
- test(std::ref(value), 3);
- test(3, std::cref(value));
- }
- TEST(Any, NotComparable) {
- auto test = [](const auto &instance) {
- entt::any any{std::cref(instance)};
- ASSERT_EQ(any, any);
- ASSERT_NE(any, entt::any{instance});
- ASSERT_NE(entt::any{}, any);
- ASSERT_TRUE(any == any);
- ASSERT_FALSE(any == entt::any{instance});
- ASSERT_TRUE(entt::any{} != any);
- };
- test(not_comparable{});
- test(std::unordered_map<int, not_comparable>{});
- test(std::vector<not_comparable>{});
- }
- TEST(Any, CompareVoid) {
- entt::any any{std::in_place_type<void>};
- ASSERT_EQ(any, any);
- ASSERT_EQ(any, entt::any{std::in_place_type<void>});
- ASSERT_NE(entt::any{'a'}, any);
- ASSERT_EQ(any, entt::any{});
- ASSERT_TRUE(any == any);
- ASSERT_TRUE(any == entt::any{std::in_place_type<void>});
- ASSERT_FALSE(entt::any{'a'} == any);
- ASSERT_TRUE(any != entt::any{'a'});
- ASSERT_FALSE(entt::any{} != any);
- }
- TEST(Any, AnyCast) {
- entt::any any{42};
- const auto &cany = any;
- ASSERT_EQ(entt::any_cast<char>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<char>(&cany), nullptr);
- ASSERT_EQ(*entt::any_cast<int>(&any), 42);
- ASSERT_EQ(*entt::any_cast<int>(&cany), 42);
- ASSERT_EQ(entt::any_cast<int &>(any), 42);
- ASSERT_EQ(entt::any_cast<const int &>(cany), 42);
- ASSERT_EQ(entt::any_cast<int>(42), 42);
- }
- TEST(Any, NonCopyableType) {
- entt::any any{std::in_place_type<std::unique_ptr<int>>, new int{42}};
- entt::any copy{any};
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- copy = any;
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- }
|