| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812 |
- #include <array>
- #include <cstddef>
- #include <cstdint>
- #include <cstring>
- #include <memory>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- #include <gtest/gtest.h>
- #include <entt/core/any.hpp>
- #include <entt/core/type_info.hpp>
- #include "../../common/aggregate.h"
- #include "../../common/config.h"
- #include "../../common/linter.hpp"
- #include "../../common/new_delete.h"
- #include "../../common/non_comparable.h"
- #include "../../common/non_movable.h"
- template<std::size_t Len>
- struct tracker {
- tracker(int &cnt)
- : counter{&cnt} {}
- tracker(const tracker &) = default;
- tracker &operator=(const tracker &) = delete;
- ~tracker() {
- ++(*counter);
- }
- private:
- std::array<std::byte, Len> buffer{};
- int *counter{};
- };
- struct fat {
- fat(double v1, double v2, double v3, double v4)
- : value{v1, v2, v3, v4} {}
- [[nodiscard]] bool operator==(const fat &other) const {
- return (value == other.value);
- }
- std::array<double, 4u> value{};
- };
- struct alignas(64u) over_aligned {};
- TEST(Any, Empty) {
- entt::any any{};
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(any.data(), nullptr);
- }
- TEST(Any, SBO) {
- entt::any any{'c'};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- 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) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- 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, SBOInPlaceConstruction) {
- std::unique_ptr<int> elem = std::make_unique<int>(2);
- // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
- entt::any any{std::in_place, elem.release()};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::ref);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOInPlaceNullptrConstruction) {
- int *instance = nullptr;
- const entt::any any{std::in_place, instance};
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- }
- TEST(Any, SBOInPlaceTypeConstruction) {
- entt::any any{std::in_place_type<int>, 2};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::ref);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOAsRefConstruction) {
- int value = 2;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::ref);
- ASSERT_EQ(any.info(), 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), 2);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_EQ(any.data(), &value);
- ASSERT_EQ(std::as_const(any).data(), &value);
- any.emplace<int &>(value);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::ref);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(&any), &value);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::ref);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOAsConstRefConstruction) {
- const int value = 2;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::cref);
- ASSERT_EQ(any.info(), 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), 2);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_EQ(any.data(), nullptr);
- ASSERT_EQ(std::as_const(any).data(), &value);
- any.emplace<const int &>(value);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::cref);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<const int>(&any), &value);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::cref);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, SBOCopyConstruction) {
- const entt::any any{2};
- entt::any other{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::embedded);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- }
- TEST(Any, SBOCopyAssignment) {
- const entt::any any{2};
- entt::any other{3};
- other = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::embedded);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- }
- TEST(Any, SBOSelfCopyAssignment) {
- entt::any any{2};
- // avoid warnings due to self-assignment
- any = *&any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- }
- TEST(Any, SBOMoveConstruction) {
- entt::any any{2};
- entt::any other{std::move(any)};
- test::is_initialized(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::embedded);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- }
- TEST(Any, SBOMoveAssignment) {
- entt::any any{2};
- entt::any other{3};
- other = std::move(any);
- test::is_initialized(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::embedded);
- ASSERT_EQ(other.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<int>(other), 2);
- }
- TEST(AnyDeathTest, SBOSelfMoveAssignment) {
- entt::any any{2};
- // avoid warnings due to self-assignment
- any = std::move(*&any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- ASSERT_EQ(any.data(), nullptr);
- }
- TEST(Any, SBODirectAssignment) {
- entt::any any{};
- any = 2;
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- }
- TEST(Any, SBOAssignValue) {
- entt::any any{2};
- const entt::any other{3};
- const entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_TRUE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- }
- TEST(Any, SBOAsRefAssignValue) {
- int value = 2;
- entt::any any{entt::forward_as_any(value)};
- const entt::any other{3};
- const entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_TRUE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- ASSERT_EQ(value, 3);
- }
- TEST(Any, SBOAsConstRefAssignValue) {
- const int value = 2;
- entt::any any{entt::forward_as_any(value)};
- const entt::any other{3};
- const entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_FALSE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_EQ(value, 2);
- }
- TEST(Any, SBOTransferValue) {
- entt::any any{2};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_TRUE(any.assign(3));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- }
- TEST(Any, SBOTransferConstValue) {
- const int value = 3;
- entt::any any{2};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_TRUE(any.assign(entt::forward_as_any(value)));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- }
- TEST(Any, SBOAsRefTransferValue) {
- int value = 2;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_TRUE(any.assign(3));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- ASSERT_EQ(value, 3);
- }
- TEST(Any, SBOAsConstRefTransferValue) {
- const int value = 2;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_FALSE(any.assign(3));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- ASSERT_EQ(value, 2);
- }
- TEST(Any, NoSBOInPlaceConstruction) {
- std::unique_ptr<fat> elem = std::make_unique<fat>(.1, .2, .3, .4);
- // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
- entt::any any{std::in_place, elem.release()};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(any.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), (fat{.1, .2, .3, .4}));
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::ref);
- ASSERT_EQ(other.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST(Any, NoSBOInPlaceNullptrConstruction) {
- fat *instance = nullptr;
- const entt::any any{std::in_place, instance};
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- }
- TEST(Any, NoSBOInPlaceTypeConstruction) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{std::in_place_type<fat>, instance};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(any.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::ref);
- ASSERT_EQ(other.info(), 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{entt::forward_as_any(instance)};
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::ref);
- ASSERT_EQ(any.info(), 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);
- any.emplace<fat &>(instance);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::ref);
- ASSERT_EQ(any.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::ref);
- ASSERT_EQ(other.info(), 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) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::cref);
- ASSERT_EQ(any.info(), 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);
- any.emplace<const fat &>(instance);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::cref);
- ASSERT_EQ(any.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
- auto other = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::cref);
- ASSERT_EQ(other.info(), 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) {
- const fat instance{.1, .2, .3, .4};
- const entt::any any{instance};
- entt::any other{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(other.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBOCopyAssignment) {
- const fat instance{.1, .2, .3, .4};
- const entt::any any{instance};
- entt::any other{3};
- other = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(other.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBOSelfCopyAssignment) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- // avoid warnings due to self-assignment
- any = *&any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(any.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- }
- TEST(Any, NoSBOMoveConstruction) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{std::move(any)};
- test::is_initialized(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(other.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(Any, NoSBOMoveAssignment) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{3};
- other = std::move(any);
- test::is_initialized(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(other.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(other), instance);
- }
- TEST(AnyDeathTest, NoSBOSelfMoveAssignment) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- // avoid warnings due to self-assignment
- any = std::move(*&any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- ASSERT_EQ(any.data(), nullptr);
- }
- TEST(Any, NoSBODirectAssignment) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{};
- any = instance;
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(any.info(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<fat>(any), instance);
- }
- TEST(Any, NoSBOAssignValue) {
- entt::any any{fat{.1, .2, .3, .4}};
- const entt::any other{fat{.0, .1, .2, .3}};
- const entt::any invalid{'c'};
- const void *addr = std::as_const(any).data();
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_TRUE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
- ASSERT_EQ(addr, std::as_const(any).data());
- }
- TEST(Any, NoSBOAsRefAssignValue) {
- fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- const entt::any other{fat{.0, .1, .2, .3}};
- const entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_TRUE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
- ASSERT_EQ(instance, (fat{.0, .1, .2, .3}));
- }
- TEST(Any, NoSBOAsConstRefAssignValue) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- const entt::any other{fat{.0, .1, .2, .3}};
- const entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_FALSE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_EQ(instance, (fat{.1, .2, .3, .4}));
- }
- TEST(Any, NoSBOTransferValue) {
- entt::any any{fat{.1, .2, .3, .4}};
- const void *addr = std::as_const(any).data();
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_TRUE(any.assign(fat{.0, .1, .2, .3}));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
- ASSERT_EQ(addr, std::as_const(any).data());
- }
- TEST(Any, NoSBOTransferConstValue) {
- const fat instance{.0, .1, .2, .3};
- entt::any any{fat{.1, .2, .3, .4}};
- const void *addr = std::as_const(any).data();
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_TRUE(any.assign(entt::forward_as_any(instance)));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
- ASSERT_EQ(addr, std::as_const(any).data());
- }
- TEST(Any, NoSBOAsRefTransferValue) {
- fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- const void *addr = std::as_const(any).data();
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_TRUE(any.assign(fat{.0, .1, .2, .3}));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
- ASSERT_EQ(instance, (fat{.0, .1, .2, .3}));
- ASSERT_EQ(addr, std::as_const(any).data());
- }
- TEST(Any, NoSBOAsConstRefTransferValue) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- const void *addr = std::as_const(any).data();
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_FALSE(any.assign(fat{.0, .1, .2, .3}));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
- ASSERT_EQ(instance, (fat{.1, .2, .3, .4}));
- ASSERT_EQ(addr, std::as_const(any).data());
- }
- TEST(Any, VoidInPlaceTypeConstruction) {
- entt::any any{std::in_place_type<void>};
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- 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(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::empty);
- ASSERT_EQ(other.info(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, VoidCopyAssignment) {
- const entt::any any{std::in_place_type<void>};
- entt::any other{2};
- other = any;
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::empty);
- ASSERT_EQ(other.info(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, VoidSelfCopyAssignment) {
- entt::any any{std::in_place_type<void>};
- // avoid warnings due to self-assignment
- any = *&any;
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- }
- TEST(Any, VoidMoveConstruction) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::move(any)};
- test::is_initialized(any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::empty);
- ASSERT_EQ(other.info(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(Any, VoidMoveAssignment) {
- entt::any any{std::in_place_type<void>};
- entt::any other{2};
- other = std::move(any);
- test::is_initialized(any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.policy(), entt::any_policy::empty);
- ASSERT_EQ(other.info(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST(AnyDeathTest, VoidSelfMoveAssignment) {
- entt::any any{std::in_place_type<void>};
- // avoid warnings due to self-assignment
- any = std::move(*&any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- ASSERT_EQ(any.data(), nullptr);
- }
- TEST(Any, SBOMoveValidButUnspecifiedState) {
- entt::any any{2};
- entt::any other{std::move(any)};
- const entt::any valid = std::move(other);
- test::is_initialized(any);
- test::is_initialized(other);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(valid);
- }
- TEST(Any, NoSBOMoveValidButUnspecifiedState) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{std::move(any)};
- const entt::any valid = std::move(other);
- test::is_initialized(any);
- test::is_initialized(other);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(valid);
- }
- TEST(Any, VoidMoveValidButUnspecifiedState) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::move(any)};
- const entt::any valid = std::move(other);
- test::is_initialized(any);
- test::is_initialized(other);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_FALSE(valid);
- }
- TEST(Any, SBODestruction) {
- using tracker_type = tracker<0u>;
- int counter{};
- {
- entt::any any{std::in_place_type<tracker_type>, counter};
- any.emplace<tracker_type>(counter);
- any = tracker_type{counter};
- entt::any other{std::move(any)};
- any = std::move(other);
- }
- ASSERT_EQ(counter, 6);
- }
- TEST(Any, NoSBODestruction) {
- using tracker_type = tracker<entt::any::length>;
- int counter{};
- {
- entt::any any{std::in_place_type<tracker_type>, counter};
- any.emplace<tracker_type>(counter);
- any = tracker_type{counter};
- entt::any other{std::move(any)};
- any = std::move(other);
- }
- ASSERT_EQ(counter, 4);
- }
- TEST(Any, VoidDestruction) {
- // just let asan tell us if everything is ok here
- [[maybe_unused]] const entt::any any{std::in_place_type<void>};
- }
- TEST(Any, Emplace) {
- entt::any any{};
- any.emplace<int>(2);
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int>(any), 2);
- }
- TEST(Any, EmplaceVoid) {
- entt::any any{};
- any.emplace<void>();
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- }
- TEST(Any, Reset) {
- entt::any any{2};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- any.reset();
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- int value = 2;
- any.emplace<int &>(value);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::ref);
- ASSERT_EQ(any.info(), entt::type_id<int>());
- any.reset();
- ASSERT_FALSE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::empty);
- ASSERT_EQ(any.info(), entt::type_id<void>());
- }
- TEST(Any, SBOSwap) {
- entt::any lhs{'c'};
- entt::any rhs{2};
- std::swap(lhs, rhs);
- ASSERT_TRUE(lhs.owner());
- ASSERT_TRUE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(rhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(lhs.info(), entt::type_id<int>());
- ASSERT_EQ(rhs.info(), 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), 2);
- 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_TRUE(lhs.owner());
- ASSERT_TRUE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(rhs.policy(), entt::any_policy::dynamic);
- 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_FALSE(lhs.owner());
- ASSERT_FALSE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
- ASSERT_EQ(rhs.policy(), entt::any_policy::empty);
- 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_TRUE(lhs.owner());
- ASSERT_TRUE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(rhs.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(lhs.info(), entt::type_id<char>());
- ASSERT_EQ(rhs.info(), 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{entt::forward_as_any(value)};
- entt::any rhs{'c'};
- std::swap(lhs, rhs);
- ASSERT_TRUE(lhs.owner());
- ASSERT_FALSE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(rhs.policy(), entt::any_policy::ref);
- ASSERT_EQ(lhs.info(), entt::type_id<char>());
- ASSERT_EQ(rhs.info(), 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) {
- const int value = 3;
- entt::any lhs{entt::forward_as_any(value)};
- entt::any rhs{'c'};
- std::swap(lhs, rhs);
- ASSERT_TRUE(lhs.owner());
- ASSERT_FALSE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(rhs.policy(), entt::any_policy::cref);
- ASSERT_EQ(lhs.info(), entt::type_id<char>());
- ASSERT_EQ(rhs.info(), 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_FALSE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
- ASSERT_EQ(rhs.info(), 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_TRUE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(lhs.info(), 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_FALSE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
- ASSERT_EQ(rhs.info(), 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_TRUE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
- ASSERT_EQ(lhs.info(), 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{entt::forward_as_any(value)};
- entt::any rhs{fat{.1, .2, .3, .4}};
- std::swap(lhs, rhs);
- ASSERT_TRUE(lhs.owner());
- ASSERT_FALSE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(rhs.policy(), entt::any_policy::ref);
- ASSERT_EQ(lhs.info(), entt::type_id<fat>());
- ASSERT_EQ(rhs.info(), 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) {
- const int value = 3;
- entt::any lhs{entt::forward_as_any(value)};
- entt::any rhs{fat{.1, .2, .3, .4}};
- std::swap(lhs, rhs);
- ASSERT_TRUE(lhs.owner());
- ASSERT_FALSE(rhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(rhs.policy(), entt::any_policy::cref);
- ASSERT_EQ(lhs.info(), entt::type_id<fat>());
- ASSERT_EQ(rhs.info(), 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_FALSE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
- ASSERT_EQ(rhs.info(), 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_TRUE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(lhs.info(), 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_FALSE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
- ASSERT_EQ(rhs.info(), 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_TRUE(lhs.owner());
- ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(lhs.info(), 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{2};
- auto ref = any.as_ref();
- auto cref = std::as_const(any).as_ref();
- ASSERT_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- ASSERT_EQ(ref.policy(), entt::any_policy::ref);
- ASSERT_EQ(cref.policy(), entt::any_policy::cref);
- 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), 2);
- ASSERT_EQ(entt::any_cast<int>(ref), 2);
- ASSERT_EQ(entt::any_cast<int>(cref), 2);
- ASSERT_EQ(entt::any_cast<const int>(any), 2);
- ASSERT_EQ(entt::any_cast<const int>(ref), 2);
- ASSERT_EQ(entt::any_cast<const int>(cref), 2);
- ASSERT_EQ(entt::any_cast<int &>(any), 2);
- ASSERT_EQ(entt::any_cast<const int &>(any), 2);
- ASSERT_EQ(entt::any_cast<int &>(ref), 2);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
- ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 2);
- 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_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- ASSERT_EQ(ref.policy(), entt::any_policy::cref);
- ASSERT_EQ(cref.policy(), entt::any_policy::ref);
- ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
- ASSERT_EQ(entt::any_cast<int>(&cref), any.data());
- ref = ref.as_ref();
- cref = std::as_const(cref).as_ref();
- ASSERT_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- ASSERT_EQ(ref.policy(), entt::any_policy::cref);
- ASSERT_EQ(cref.policy(), entt::any_policy::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_EQ(entt::any_cast<int>(&ref), nullptr);
- ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 3);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 3);
- ref = 2;
- cref = 2;
- ASSERT_TRUE(ref.owner());
- ASSERT_TRUE(cref.owner());
- ASSERT_EQ(ref.policy(), entt::any_policy::embedded);
- ASSERT_EQ(cref.policy(), entt::any_policy::embedded);
- ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
- ASSERT_NE(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<int &>(ref), 2);
- ASSERT_EQ(entt::any_cast<int &>(cref), 2);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 2);
- ASSERT_NE(entt::any_cast<int>(&ref), any.data());
- ASSERT_NE(entt::any_cast<int>(&cref), any.data());
- }
- TEST(Any, Comparable) {
- const entt::any any{'c'};
- const entt::any other{'a'};
- 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);
- }
- TEST(Any, NoSBOComparable) {
- const entt::any any{fat{.1, .2, .3, .4}};
- const entt::any other{fat{.0, .1, .2, .3}};
- 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);
- }
- TEST(Any, RefComparable) {
- int value = 2;
- const entt::any any{entt::forward_as_any(value)};
- const entt::any other{3};
- 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);
- }
- TEST(Any, ConstRefComparable) {
- int value = 2;
- const entt::any any{3};
- const entt::any other{entt::make_any<const int &>(value)};
- 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);
- }
- TEST(Any, UnrelatedComparable) {
- const entt::any any{'c'};
- const entt::any other{2};
- 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);
- }
- TEST(Any, NonComparable) {
- const test::non_comparable instance{};
- auto any = entt::forward_as_any(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(Any, AssociativeContainerOfNonComparable) {
- const std::unordered_map<int, test::non_comparable> instance{};
- auto any = entt::forward_as_any(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(Any, SequenceContainerOfNonComparable) {
- const std::vector<test::non_comparable> instance{};
- auto any = entt::forward_as_any(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(Any, CompareVoid) {
- const 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{2};
- 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), 2);
- ASSERT_EQ(*entt::any_cast<int>(&cany), 2);
- ASSERT_EQ(entt::any_cast<int &>(any), 2);
- ASSERT_EQ(entt::any_cast<const int &>(cany), 2);
- auto instance = std::make_unique<double>(2.);
- entt::any ref{entt::forward_as_any(instance)};
- entt::any cref{entt::forward_as_any(std::as_const(*instance))};
- ASSERT_EQ(entt::any_cast<double>(std::move(cref)), 2.);
- ASSERT_EQ(*entt::any_cast<std::unique_ptr<double>>(std::move(ref)), 2.);
- ASSERT_EQ(entt::any_cast<int>(entt::any{2}), 2);
- }
- ENTT_DEBUG_TEST(AnyDeathTest, AnyCast) {
- entt::any any{2};
- const auto &cany = any;
- ASSERT_DEATH([[maybe_unused]] auto &elem = entt::any_cast<double &>(any), "");
- ASSERT_DEATH([[maybe_unused]] const auto &elem = entt::any_cast<const double &>(cany), "");
- auto instance = std::make_unique<double>(2.);
- entt::any ref{entt::forward_as_any(instance)};
- const entt::any cref{entt::forward_as_any(std::as_const(*instance))};
- ASSERT_DEATH([[maybe_unused]] auto elem = entt::any_cast<std::unique_ptr<double>>(std::as_const(ref).as_ref()), "");
- ASSERT_DEATH([[maybe_unused]] auto elem = entt::any_cast<double>(entt::any{2}), "");
- }
- TEST(Any, MakeAny) {
- int value = 2;
- auto any = entt::make_any<int>(value);
- auto ext = entt::make_any<int, sizeof(int), alignof(int)>(value);
- auto ref = entt::make_any<int &>(value);
- ASSERT_TRUE(any);
- ASSERT_TRUE(ext);
- ASSERT_TRUE(ref);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(ext.owner());
- ASSERT_FALSE(ref.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(ext.policy(), entt::any_policy::embedded);
- ASSERT_EQ(ref.policy(), entt::any_policy::ref);
- ASSERT_EQ(entt::any_cast<const int &>(any), 2);
- ASSERT_EQ(entt::any_cast<const int &>(ext), 2);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
- ASSERT_EQ(decltype(any)::length, entt::any::length);
- ASSERT_NE(decltype(ext)::length, entt::any::length);
- ASSERT_EQ(decltype(ref)::length, entt::any::length);
- ASSERT_NE(any.data(), &value);
- ASSERT_NE(ext.data(), &value);
- ASSERT_EQ(ref.data(), &value);
- }
- TEST(Any, ForwardAsAny) {
- int value = 2;
- auto ref = entt::forward_as_any(value);
- auto cref = entt::forward_as_any(std::as_const(value));
- auto any = entt::forward_as_any(static_cast<int &&>(value));
- ASSERT_TRUE(any);
- ASSERT_TRUE(ref);
- ASSERT_TRUE(cref);
- ASSERT_TRUE(any.owner());
- ASSERT_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(ref.policy(), entt::any_policy::ref);
- ASSERT_EQ(cref.policy(), entt::any_policy::cref);
- ASSERT_NE(entt::any_cast<int>(&any), nullptr);
- ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
- ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
- ASSERT_EQ(entt::any_cast<const int &>(any), 2);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 2);
- ASSERT_NE(any.data(), &value);
- ASSERT_EQ(ref.data(), &value);
- }
- TEST(Any, NonCopyableType) {
- const std::unique_ptr<int> value{};
- entt::any any{std::in_place_type<std::unique_ptr<int>>};
- entt::any other = entt::forward_as_any(value);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(other.policy(), entt::any_policy::cref);
- ASSERT_EQ(any.info(), other.info());
- ASSERT_FALSE(any.assign(other));
- ASSERT_FALSE(any.assign(std::move(other)));
- entt::any copy{any};
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- ASSERT_TRUE(any.owner());
- ASSERT_FALSE(copy.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(copy.policy(), entt::any_policy::empty);
- copy = any;
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- ASSERT_TRUE(any.owner());
- ASSERT_FALSE(copy.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::embedded);
- ASSERT_EQ(copy.policy(), entt::any_policy::empty);
- }
- TEST(Any, NonCopyableValueType) {
- std::vector<entt::any> vec{};
- vec.emplace_back(std::in_place_type<std::unique_ptr<int>>);
- vec.shrink_to_fit();
- ASSERT_EQ(vec.size(), 1u);
- ASSERT_EQ(vec.capacity(), 1u);
- ASSERT_TRUE(vec[0u]);
- // strong exception guarantee due to noexcept move ctor
- vec.emplace_back(std::in_place_type<std::unique_ptr<int>>);
- ASSERT_EQ(vec.size(), 2u);
- ASSERT_TRUE(vec[0u]);
- ASSERT_TRUE(vec[1u]);
- }
- TEST(Any, NonMovableType) {
- entt::any any{std::in_place_type<test::non_movable>};
- entt::any other{std::in_place_type<test::non_movable>};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(any.info(), other.info());
- ASSERT_TRUE(any.assign(other));
- ASSERT_TRUE(any.assign(std::move(other)));
- entt::any copy{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(copy);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(copy.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(copy.policy(), entt::any_policy::dynamic);
- copy = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(copy);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(copy.owner());
- ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
- ASSERT_EQ(copy.policy(), entt::any_policy::dynamic);
- }
- TEST(Any, Array) {
- // NOLINTNEXTLINE(*-avoid-c-arrays)
- entt::any any{std::in_place_type<int[1]>};
- const entt::any copy{any};
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- // NOLINTBEGIN(*-avoid-c-arrays)
- ASSERT_EQ(any.info(), entt::type_id<int[1]>());
- ASSERT_NE(entt::any_cast<int[1]>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int[2]>(&any), nullptr);
- // NOLINTEND(*-avoid-c-arrays)
- ASSERT_EQ(entt::any_cast<int *>(&any), nullptr);
- // NOLINTNEXTLINE(*-avoid-c-arrays)
- entt::any_cast<int(&)[1]>(any)[0] = 2;
- // NOLINTNEXTLINE(*-avoid-c-arrays)
- ASSERT_EQ(entt::any_cast<const int(&)[1]>(std::as_const(any))[0], 2);
- }
- TEST(Any, CopyMoveReference) {
- int value = 3;
- auto any = entt::forward_as_any(value);
- entt::any move = std::move(any);
- entt::any copy = move;
- test::is_initialized(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(move);
- ASSERT_TRUE(copy);
- ASSERT_FALSE(move.owner());
- ASSERT_TRUE(copy.owner());
- ASSERT_EQ(move.policy(), entt::any_policy::ref);
- ASSERT_EQ(copy.policy(), entt::any_policy::embedded);
- ASSERT_EQ(move.info(), entt::type_id<int>());
- ASSERT_EQ(copy.info(), entt::type_id<int>());
- ASSERT_EQ(std::as_const(move).data(), &value);
- ASSERT_NE(std::as_const(copy).data(), &value);
- ASSERT_EQ(entt::any_cast<int>(move), 3);
- ASSERT_EQ(entt::any_cast<int>(copy), 3);
- value = 2;
- ASSERT_EQ(entt::any_cast<int &>(move), 2);
- ASSERT_EQ(entt::any_cast<int &>(copy), 3);
- }
- TEST(Any, CopyMoveConstReference) {
- int value = 3;
- auto any = entt::forward_as_any(std::as_const(value));
- entt::any move = std::move(any);
- entt::any copy = move;
- test::is_initialized(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(move);
- ASSERT_TRUE(copy);
- ASSERT_FALSE(move.owner());
- ASSERT_TRUE(copy.owner());
- ASSERT_EQ(move.policy(), entt::any_policy::cref);
- ASSERT_EQ(copy.policy(), entt::any_policy::embedded);
- ASSERT_EQ(move.info(), entt::type_id<int>());
- ASSERT_EQ(copy.info(), entt::type_id<int>());
- ASSERT_EQ(std::as_const(move).data(), &value);
- ASSERT_NE(std::as_const(copy).data(), &value);
- ASSERT_EQ(entt::any_cast<int>(move), 3);
- ASSERT_EQ(entt::any_cast<int>(copy), 3);
- value = 2;
- ASSERT_EQ(entt::any_cast<const int &>(move), 2);
- ASSERT_EQ(entt::any_cast<const int &>(copy), 3);
- }
- TEST(Any, SBOVsZeroedSBOSize) {
- entt::any sbo{2};
- const auto *broken = sbo.data();
- entt::any other = std::move(sbo);
- ASSERT_NE(broken, other.data());
- entt::basic_any<0u> dyn{2};
- const auto *valid = dyn.data();
- entt::basic_any<0u> same = std::move(dyn);
- ASSERT_EQ(valid, same.data());
- }
- TEST(Any, SboAlignment) {
- constexpr auto alignment = alignof(over_aligned);
- using any_type = entt::basic_any<alignment, alignment>;
- std::array<any_type, 2u> sbo = {over_aligned{}, over_aligned{}};
- const auto *data = sbo[0].data();
- // NOLINTBEGIN(*-reinterpret-cast)
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
- // NOLINTEND(*-reinterpret-cast)
- std::swap(sbo[0], sbo[1]);
- // NOLINTBEGIN(*-reinterpret-cast)
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
- // NOLINTEND(*-reinterpret-cast)
- ASSERT_NE(data, sbo[1].data());
- }
- TEST(Any, NoSboAlignment) {
- constexpr auto alignment = alignof(over_aligned);
- using any_type = entt::basic_any<alignment>;
- std::array<any_type, 2u> nosbo = {over_aligned{}, over_aligned{}};
- const auto *data = nosbo[0].data();
- // NOLINTBEGIN(*-reinterpret-cast)
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
- // NOLINTEND(*-reinterpret-cast)
- std::swap(nosbo[0], nosbo[1]);
- // NOLINTBEGIN(*-reinterpret-cast)
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
- // NOLINTEND(*-reinterpret-cast)
- ASSERT_EQ(data, nosbo[1].data());
- }
- TEST(Any, AggregatesMustWork) {
- // the goal of this test is to enforce the requirements for aggregate types
- entt::any{std::in_place_type<test::aggregate>, 2}.emplace<test::aggregate>(2);
- }
- TEST(Any, DeducedArrayType) {
- entt::any any{"array of char"};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.info(), entt::type_id<const char *>());
- ASSERT_EQ((std::strcmp("array of char", entt::any_cast<const char *>(any))), 0);
- any = "another array of char";
- ASSERT_TRUE(any);
- ASSERT_EQ(any.info(), entt::type_id<const char *>());
- ASSERT_EQ((std::strcmp("another array of char", entt::any_cast<const char *>(any))), 0);
- }
- TEST(Any, ClassLevelNewDelete) {
- // yeah, that's for code coverage purposes only :)
- entt::any any{std::in_place_type<test::new_delete>, *std::make_unique<test::new_delete>(test::new_delete{3})};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.info(), entt::type_id<test::new_delete>());
- ASSERT_EQ(entt::any_cast<const test::new_delete &>(any).value, 3);
- }
|