| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466 |
- #include <algorithm>
- #include <cstdint>
- #include <cstring>
- #include <iterator>
- #include <type_traits>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- #include <gtest/gtest.h>
- #include <entt/core/any.hpp>
- #include <entt/core/type_info.hpp>
- #include "../common/config.h"
- struct empty {
- ~empty() {
- ++counter;
- }
- inline static int counter = 0;
- };
- struct fat {
- fat(double v1, double v2, double v3, double v4)
- : value{v1, v2, v3, v4} {}
- ~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));
- }
- inline static int counter{0};
- double value[4];
- };
- struct not_comparable {
- bool operator==(const not_comparable &) const = delete;
- };
- struct not_copyable {
- not_copyable()
- : payload{} {}
- not_copyable(const not_copyable &) = delete;
- not_copyable(not_copyable &&) = default;
- not_copyable &operator=(const not_copyable &) = delete;
- not_copyable &operator=(not_copyable &&) = default;
- double payload;
- };
- struct not_movable {
- not_movable() = default;
- not_movable(const not_movable &) = default;
- not_movable(not_movable &&) = delete;
- not_movable &operator=(const not_movable &) = default;
- not_movable &operator=(not_movable &&) = delete;
- double payload{};
- };
- struct alignas(64u) over_aligned {};
- struct Any: ::testing::Test {
- void SetUp() override {
- fat::counter = 0;
- empty::counter = 0;
- }
- };
- using AnyDeathTest = Any;
- TEST_F(Any, SBO) {
- entt::any any{'c'};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- 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_F(Any, NoSBO) {
- fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- 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_F(Any, Empty) {
- entt::any any{};
- ASSERT_FALSE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
- ASSERT_EQ(any.data(), nullptr);
- }
- TEST_F(Any, SBOInPlaceTypeConstruction) {
- entt::any any{std::in_place_type<int>, 42};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- 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 = any.as_ref();
- ASSERT_TRUE(other);
- ASSERT_FALSE(other.owner());
- ASSERT_EQ(other.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST_F(Any, SBOAsRefConstruction) {
- int value = 42;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- 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);
- any.emplace<int &>(value);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.type(), 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.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST_F(Any, SBOAsConstRefConstruction) {
- const int value = 42;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- 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);
- any.emplace<const int &>(value);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.type(), 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.type(), entt::type_id<int>());
- ASSERT_EQ(entt::any_cast<int>(other), 42);
- ASSERT_EQ(other.data(), any.data());
- }
- TEST_F(Any, SBOCopyConstruction) {
- entt::any any{42};
- entt::any other{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- 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_F(Any, SBOCopyAssignment) {
- entt::any any{42};
- entt::any other{3};
- other = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- 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_F(Any, SBOMoveConstruction) {
- entt::any any{42};
- entt::any other{std::move(any)};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_NE(any.data(), nullptr);
- 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_F(Any, SBOMoveAssignment) {
- entt::any any{42};
- entt::any other{3};
- other = std::move(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_NE(any.data(), nullptr);
- 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_F(Any, SBODirectAssignment) {
- entt::any any{};
- any = 42;
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- 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_F(Any, SBOAssignValue) {
- entt::any any{42};
- entt::any other{3};
- entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_TRUE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- }
- TEST_F(Any, SBOAsRefAssignValue) {
- int value = 42;
- entt::any any{entt::forward_as_any(value)};
- entt::any other{3};
- entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_TRUE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- ASSERT_EQ(value, 3);
- }
- TEST_F(Any, SBOAsConstRefAssignValue) {
- const int value = 42;
- entt::any any{entt::forward_as_any(value)};
- entt::any other{3};
- entt::any invalid{'c'};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_FALSE(any.assign(other));
- ASSERT_FALSE(any.assign(invalid));
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_EQ(value, 42);
- }
- TEST_F(Any, SBOTransferValue) {
- entt::any any{42};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_TRUE(any.assign(3));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- }
- TEST_F(Any, SBOTransferConstValue) {
- const int value = 3;
- entt::any any{42};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_TRUE(any.assign(entt::forward_as_any(value)));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- }
- TEST_F(Any, SBOAsRefTransferValue) {
- int value = 42;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_TRUE(any.assign(3));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<int>(any), 3);
- ASSERT_EQ(value, 3);
- }
- TEST_F(Any, SBOAsConstRefTransferValue) {
- const int value = 42;
- entt::any any{entt::forward_as_any(value)};
- ASSERT_TRUE(any);
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_FALSE(any.assign(3));
- ASSERT_FALSE(any.assign('c'));
- ASSERT_EQ(entt::any_cast<int>(any), 42);
- ASSERT_EQ(value, 42);
- }
- TEST_F(Any, NoSBOInPlaceTypeConstruction) {
- 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.type(), 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.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST_F(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.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);
- any.emplace<fat &>(instance);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.type(), 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.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST_F(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.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);
- any.emplace<const fat &>(instance);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.type(), 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.type(), entt::type_id<fat>());
- ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
- ASSERT_EQ(other.data(), any.data());
- }
- TEST_F(Any, NoSBOCopyConstruction) {
- fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{any};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- 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_F(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_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- 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_F(Any, NoSBOMoveConstruction) {
- fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{std::move(any)};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.data(), nullptr);
- 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_F(Any, NoSBOMoveAssignment) {
- fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{3};
- other = std::move(any);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.data(), nullptr);
- 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_F(Any, NoSBODirectAssignment) {
- fat instance{.1, .2, .3, .4};
- entt::any any{};
- any = instance;
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- 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_F(Any, NoSBOAssignValue) {
- entt::any any{fat{.1, .2, .3, .4}};
- entt::any other{fat{.0, .1, .2, .3}};
- 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_F(Any, NoSBOAsRefAssignValue) {
- fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- entt::any other{fat{.0, .1, .2, .3}};
- 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_F(Any, NoSBOAsConstRefAssignValue) {
- const fat instance{.1, .2, .3, .4};
- entt::any any{entt::forward_as_any(instance)};
- entt::any other{fat{.0, .1, .2, .3}};
- 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_F(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_F(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_F(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_F(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_F(Any, VoidInPlaceTypeConstruction) {
- entt::any any{std::in_place_type<void>};
- ASSERT_FALSE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- }
- TEST_F(Any, VoidCopyConstruction) {
- entt::any any{std::in_place_type<void>};
- entt::any other{any};
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(other.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST_F(Any, VoidCopyAssignment) {
- entt::any any{std::in_place_type<void>};
- entt::any other{42};
- other = any;
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(other.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST_F(Any, VoidMoveConstruction) {
- entt::any any{std::in_place_type<void>};
- entt::any other{std::move(any)};
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(other.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST_F(Any, VoidMoveAssignment) {
- entt::any any{std::in_place_type<void>};
- entt::any other{42};
- other = std::move(any);
- ASSERT_FALSE(any);
- ASSERT_FALSE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- ASSERT_EQ(other.type(), entt::type_id<void>());
- ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
- }
- TEST_F(Any, SBOMoveValidButUnspecifiedState) {
- entt::any any{42};
- entt::any other{std::move(any)};
- entt::any valid = std::move(other);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(valid);
- }
- TEST_F(Any, NoSBOMoveValidButUnspecifiedState) {
- fat instance{.1, .2, .3, .4};
- entt::any any{instance};
- entt::any other{std::move(any)};
- entt::any valid = std::move(other);
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(valid);
- }
- TEST_F(Any, VoidMoveValidButUnspecifiedState) {
- 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_F(Any, SBODestruction) {
- {
- entt::any any{std::in_place_type<empty>};
- any.emplace<empty>();
- any = empty{};
- entt::any other{std::move(any)};
- any = std::move(other);
- }
- ASSERT_EQ(empty::counter, 6);
- }
- TEST_F(Any, NoSBODestruction) {
- {
- entt::any any{std::in_place_type<fat>, 1., 2., 3., 4.};
- any.emplace<fat>(1., 2., 3., 4.);
- any = fat{1., 2., 3., 4.};
- entt::any other{std::move(any)};
- any = std::move(other);
- }
- ASSERT_EQ(fat::counter, 4);
- }
- TEST_F(Any, VoidDestruction) {
- // just let asan tell us if everything is ok here
- [[maybe_unused]] entt::any any{std::in_place_type<void>};
- }
- TEST_F(Any, Emplace) {
- entt::any any{};
- any.emplace<int>(42);
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- 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_F(Any, EmplaceVoid) {
- entt::any any{};
- any.emplace<void>();
- ASSERT_FALSE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- }
- TEST_F(Any, Reset) {
- entt::any any{42};
- ASSERT_TRUE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<int>());
- any.reset();
- ASSERT_FALSE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- int value = 42;
- any.emplace<int &>(value);
- ASSERT_TRUE(any);
- ASSERT_FALSE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<int>());
- any.reset();
- ASSERT_FALSE(any);
- ASSERT_TRUE(any.owner());
- ASSERT_EQ(any.type(), entt::type_id<void>());
- }
- TEST_F(Any, SBOSwap) {
- entt::any lhs{'c'};
- entt::any rhs{42};
- std::swap(lhs, rhs);
- ASSERT_TRUE(lhs.owner());
- ASSERT_TRUE(rhs.owner());
- 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_F(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(entt::any_cast<fat>(lhs), (fat{.4, .3, .2, .1}));
- ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
- }
- TEST_F(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_TRUE(lhs.owner());
- ASSERT_TRUE(rhs.owner());
- ASSERT_EQ(pre, lhs.data());
- }
- TEST_F(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.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_F(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.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_F(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.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_F(Any, SBOWithEmptySwap) {
- entt::any lhs{'c'};
- entt::any rhs{};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_TRUE(lhs.owner());
- 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_TRUE(rhs.owner());
- 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_F(Any, SBOWithVoidSwap) {
- entt::any lhs{'c'};
- entt::any rhs{std::in_place_type<void>};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_TRUE(lhs.owner());
- 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_TRUE(rhs.owner());
- 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_F(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.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_F(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.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_F(Any, NoSBOWithEmptySwap) {
- entt::any lhs{fat{.1, .2, .3, .4}};
- entt::any rhs{};
- std::swap(lhs, rhs);
- ASSERT_FALSE(lhs);
- ASSERT_TRUE(lhs.owner());
- 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_TRUE(rhs.owner());
- 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_F(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_TRUE(lhs.owner());
- 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_TRUE(rhs.owner());
- 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_F(Any, AsRef) {
- entt::any any{42};
- auto ref = any.as_ref();
- auto cref = std::as_const(any).as_ref();
- ASSERT_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- 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_EQ(entt::any_cast<int>(&cref), nullptr);
- 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_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- 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(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 = 42;
- cref = 42;
- ASSERT_TRUE(ref.owner());
- ASSERT_TRUE(cref.owner());
- 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_F(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(entt::forward_as_any(value), 3);
- test(3, entt::make_any<const int &>(value));
- test('c', value);
- }
- TEST_F(Any, NotComparable) {
- auto test = [](const auto &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(not_comparable{});
- test(std::unordered_map<int, not_comparable>{});
- test(std::vector<not_comparable>{});
- }
- TEST_F(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_F(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);
- not_copyable instance{};
- instance.payload = 42.;
- entt::any ref{entt::forward_as_any(instance)};
- entt::any cref{entt::forward_as_any(std::as_const(instance).payload)};
- ASSERT_EQ(entt::any_cast<not_copyable>(std::move(ref)).payload, 42.);
- ASSERT_EQ(entt::any_cast<double>(std::move(cref)), 42.);
- ASSERT_EQ(entt::any_cast<int>(entt::any{42}), 42);
- }
- ENTT_DEBUG_TEST_F(AnyDeathTest, AnyCast) {
- entt::any any{42};
- const auto &cany = any;
- ASSERT_DEATH(entt::any_cast<double &>(any), "");
- ASSERT_DEATH(entt::any_cast<const double &>(cany), "");
- not_copyable instance{};
- instance.payload = 42.;
- entt::any ref{entt::forward_as_any(instance)};
- entt::any cref{entt::forward_as_any(std::as_const(instance).payload)};
- ASSERT_DEATH(entt::any_cast<not_copyable>(std::as_const(ref).as_ref()), "");
- ASSERT_DEATH(entt::any_cast<double>(entt::any{42}), "");
- }
- TEST_F(Any, MakeAny) {
- int value = 42;
- 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(entt::any_cast<const int &>(any), 42);
- ASSERT_EQ(entt::any_cast<const int &>(ext), 42);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
- 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_F(Any, ForwardAsAny) {
- int value = 42;
- auto any = entt::forward_as_any(std::move(value));
- auto ref = entt::forward_as_any(value);
- auto cref = entt::forward_as_any(std::as_const(value));
- ASSERT_TRUE(any);
- ASSERT_TRUE(ref);
- ASSERT_TRUE(cref);
- ASSERT_TRUE(any.owner());
- ASSERT_FALSE(ref.owner());
- ASSERT_FALSE(cref.owner());
- 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), 42);
- ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
- ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
- ASSERT_NE(any.data(), &value);
- ASSERT_EQ(ref.data(), &value);
- }
- TEST_F(Any, NotCopyableType) {
- const not_copyable value{};
- entt::any any{std::in_place_type<not_copyable>};
- 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.type(), other.type());
- 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_TRUE(copy.owner());
- copy = any;
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(copy.owner());
- }
- TEST_F(Any, NotCopyableValueType) {
- std::vector<entt::any> vec{};
- vec.emplace_back(std::in_place_type<not_copyable>);
- 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<not_copyable>);
- ASSERT_EQ(vec.size(), 2u);
- ASSERT_TRUE(vec[0u]);
- ASSERT_TRUE(vec[1u]);
- }
- TEST_F(Any, NotMovableType) {
- entt::any any{std::in_place_type<not_movable>};
- entt::any other{std::in_place_type<not_movable>};
- ASSERT_TRUE(any);
- ASSERT_TRUE(other);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(other.owner());
- ASSERT_EQ(any.type(), other.type());
- 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());
- copy = any;
- ASSERT_TRUE(any);
- ASSERT_TRUE(copy);
- ASSERT_TRUE(any.owner());
- ASSERT_TRUE(copy.owner());
- }
- TEST_F(Any, Array) {
- entt::any any{std::in_place_type<int[1]>};
- entt::any copy{any};
- ASSERT_TRUE(any);
- ASSERT_FALSE(copy);
- ASSERT_EQ(any.type(), entt::type_id<int[1]>());
- ASSERT_NE(entt::any_cast<int[1]>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int[2]>(&any), nullptr);
- ASSERT_EQ(entt::any_cast<int *>(&any), nullptr);
- entt::any_cast<int(&)[1]>(any)[0] = 42;
- ASSERT_EQ(entt::any_cast<const int(&)[1]>(std::as_const(any))[0], 42);
- }
- TEST_F(Any, CopyMoveReference) {
- int value{};
- auto test = [&](auto &&ref) {
- value = 3;
- auto any = entt::forward_as_any(ref);
- entt::any move = std::move(any);
- entt::any copy = move;
- ASSERT_TRUE(any);
- ASSERT_TRUE(move);
- ASSERT_TRUE(copy);
- ASSERT_FALSE(any.owner());
- ASSERT_FALSE(move.owner());
- ASSERT_TRUE(copy.owner());
- ASSERT_EQ(move.type(), entt::type_id<int>());
- ASSERT_EQ(copy.type(), 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 = 42;
- ASSERT_EQ(entt::any_cast<const int &>(move), 42);
- ASSERT_EQ(entt::any_cast<const int &>(copy), 3);
- };
- test(value);
- test(std::as_const(value));
- }
- TEST_F(Any, SBOVsZeroedSBOSize) {
- entt::any sbo{42};
- const auto *broken = sbo.data();
- entt::any other = std::move(sbo);
- ASSERT_NE(broken, other.data());
- entt::basic_any<0u> dyn{42};
- const auto *valid = dyn.data();
- entt::basic_any<0u> same = std::move(dyn);
- ASSERT_EQ(valid, same.data());
- }
- TEST_F(Any, SboAlignment) {
- static constexpr auto alignment = alignof(over_aligned);
- entt::basic_any<alignment, alignment> sbo[2] = {over_aligned{}, over_aligned{}};
- const auto *data = sbo[0].data();
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
- std::swap(sbo[0], sbo[1]);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
- ASSERT_NE(data, sbo[1].data());
- }
- TEST_F(Any, NoSboAlignment) {
- static constexpr auto alignment = alignof(over_aligned);
- entt::basic_any<alignment> nosbo[2] = {over_aligned{}, over_aligned{}};
- const auto *data = nosbo[0].data();
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
- std::swap(nosbo[0], nosbo[1]);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
- ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
- ASSERT_EQ(data, nosbo[1].data());
- }
- TEST_F(Any, AggregatesMustWork) {
- struct aggregate_type {
- int value;
- };
- // the goal of this test is to enforce the requirements for aggregate types
- entt::any{std::in_place_type<aggregate_type>, 42}.emplace<aggregate_type>(42);
- }
- TEST_F(Any, DeducedArrayType) {
- entt::any any{"array of char"};
- ASSERT_TRUE(any);
- ASSERT_EQ(any.type(), 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.type(), entt::type_id<const char *>());
- ASSERT_EQ((std::strcmp("another array of char", entt::any_cast<const char *>(any))), 0);
- }
|