| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553 |
- #include <array>
- #include <cstddef>
- #include <cstdint>
- #include <functional>
- #include <iterator>
- #include <memory>
- #include <tuple>
- #include <unordered_set>
- #include <utility>
- #include <vector>
- #include <gtest/gtest.h>
- #include <entt/config/config.h>
- #include <entt/core/hashed_string.hpp>
- #include <entt/core/type_info.hpp>
- #include <entt/entity/entity.hpp>
- #include <entt/entity/group.hpp>
- #include <entt/entity/mixin.hpp>
- #include <entt/entity/registry.hpp>
- #include <entt/entity/storage.hpp>
- #include <entt/entity/view.hpp>
- #include <entt/signal/sigh.hpp>
- #include "../../common/aggregate.h"
- #include "../../common/config.h"
- #include "../../common/empty.h"
- #include "../../common/entity.h"
- #include "../../common/mixin.hpp"
- #include "../../common/non_default_constructible.h"
- #include "../../common/pointer_stable.h"
- struct no_eto_type {
- static constexpr std::size_t page_size = 1024u;
- };
- bool operator==(const no_eto_type &lhs, const no_eto_type &rhs) {
- return &lhs == &rhs;
- }
- struct listener {
- template<typename Type>
- static void sort(entt::registry ®istry) {
- registry.sort<Type>([](auto lhs, auto rhs) { return lhs < rhs; });
- }
- void incr(const entt::registry &, entt::entity entity) {
- last = entity;
- ++counter;
- }
- void decr(const entt::registry &, entt::entity entity) {
- last = entity;
- --counter;
- }
- entt::entity last{entt::null};
- int counter{0};
- };
- struct owner {
- void receive(const entt::registry &ref) {
- parent = &ref;
- }
- const entt::registry *parent{nullptr};
- };
- struct destruction_order {
- using ctx_check_type = int;
- destruction_order(const entt::registry &ref, bool &ctx)
- : registry{&ref},
- ctx_check{&ctx} {
- *ctx_check = (registry->ctx().find<ctx_check_type>() != nullptr);
- }
- destruction_order(const destruction_order &) = delete;
- destruction_order &operator=(const destruction_order &) = delete;
- ~destruction_order() {
- *ctx_check = *ctx_check && (registry->ctx().find<ctx_check_type>() != nullptr);
- }
- private:
- const entt::registry *registry;
- bool *ctx_check{};
- };
- struct entity_traits {
- using value_type = test::entity;
- using entity_type = uint32_t;
- using version_type = uint16_t;
- static constexpr entity_type entity_mask = 0xFF;
- static constexpr entity_type version_mask = 0x00;
- };
- template<>
- struct entt::entt_traits<test::entity>: entt::basic_entt_traits<entity_traits> {
- static constexpr auto page_size = ENTT_SPARSE_PAGE;
- };
- TEST(Registry, Functionalities) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- ASSERT_NO_THROW([[maybe_unused]] auto alloc = registry.get_allocator());
- ASSERT_EQ(registry.storage<entt::entity>().size(), 0u);
- ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
- ASSERT_NO_THROW(registry.storage<entt::entity>().reserve(4));
- ASSERT_EQ(registry.storage<entt::entity>().capacity(), 4u);
- ASSERT_TRUE(registry.storage<entt::entity>().empty());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_TRUE(registry.storage<int>().empty());
- ASSERT_TRUE(registry.storage<char>().empty());
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- ASSERT_TRUE(registry.all_of<>(e0));
- ASSERT_FALSE(registry.any_of<>(e1));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 1u);
- ASSERT_FALSE(registry.storage<int>().empty());
- ASSERT_FALSE(registry.storage<char>().empty());
- ASSERT_NE(e0, e1);
- ASSERT_FALSE((registry.all_of<int, const char>(e0)));
- ASSERT_TRUE((registry.all_of<const int, char>(e1)));
- ASSERT_FALSE((registry.any_of<int, const double>(e0)));
- ASSERT_TRUE((registry.any_of<const int, double>(e1)));
- ASSERT_EQ(registry.try_get<int>(e0), nullptr);
- ASSERT_NE(registry.try_get<int>(e1), nullptr);
- ASSERT_EQ(registry.try_get<char>(e0), nullptr);
- ASSERT_NE(registry.try_get<char>(e1), nullptr);
- ASSERT_EQ(registry.try_get<double>(e0), nullptr);
- ASSERT_EQ(registry.try_get<double>(e1), nullptr);
- ASSERT_EQ(registry.emplace<int>(e0, 4), 4);
- ASSERT_EQ(registry.emplace<char>(e0, 'c'), 'c');
- ASSERT_NO_THROW(registry.erase<int>(e1));
- ASSERT_NO_THROW(registry.erase<char>(e1));
- ASSERT_TRUE((registry.all_of<const int, char>(e0)));
- ASSERT_FALSE((registry.all_of<int, const char>(e1)));
- ASSERT_TRUE((registry.any_of<const int, double>(e0)));
- ASSERT_FALSE((registry.any_of<int, const double>(e1)));
- const auto e2 = registry.create();
- registry.emplace_or_replace<int>(e2, registry.get<int>(e0));
- registry.emplace_or_replace<char>(e2, registry.get<char>(e0));
- ASSERT_TRUE((registry.all_of<int, char>(e2)));
- ASSERT_EQ(registry.get<int>(e0), 4);
- ASSERT_EQ(registry.get<char>(e0), 'c');
- ASSERT_NE(registry.try_get<int>(e0), nullptr);
- ASSERT_NE(registry.try_get<char>(e0), nullptr);
- ASSERT_EQ(registry.try_get<double>(e0), nullptr);
- ASSERT_EQ(*registry.try_get<int>(e0), 4);
- ASSERT_EQ(*registry.try_get<char>(e0), 'c');
- ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 4);
- ASSERT_EQ(*std::get<0>(registry.try_get<int, char, double>(e0)), 4);
- ASSERT_EQ(std::get<1>(static_cast<const entt::registry &>(registry).get<int, char>(e0)), 'c');
- ASSERT_EQ(*std::get<1>(static_cast<const entt::registry &>(registry).try_get<int, char, double>(e0)), 'c');
- ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
- ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));
- ASSERT_NE(®istry.get<int>(e0), ®istry.get<int>(e2));
- ASSERT_NE(®istry.get<char>(e0), ®istry.get<char>(e2));
- ASSERT_EQ(registry.patch<int>(e0, [](auto &instance) { instance = 2; }), 2);
- ASSERT_EQ(registry.replace<int>(e0, 3), 3);
- ASSERT_NO_THROW(registry.emplace_or_replace<int>(e0, 1));
- ASSERT_NO_THROW(registry.emplace_or_replace<int>(e1, 1));
- ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e0), 1);
- ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e1), 1);
- ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
- ASSERT_EQ(registry.storage<entt::entity>().free_list(), 3u);
- ASSERT_EQ(traits_type::to_version(e2), 0u);
- ASSERT_EQ(registry.current(e2), 0u);
- ASSERT_NO_THROW(registry.destroy(e2));
- ASSERT_EQ(traits_type::to_version(e2), 0u);
- ASSERT_EQ(registry.current(e2), 1u);
- ASSERT_TRUE(registry.valid(e0));
- ASSERT_TRUE(registry.valid(e1));
- ASSERT_FALSE(registry.valid(e2));
- ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
- ASSERT_EQ(registry.storage<entt::entity>().free_list(), 2u);
- ASSERT_NO_THROW(registry.clear());
- ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
- ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
- ASSERT_FALSE(registry.storage<entt::entity>().empty());
- const auto e3 = registry.create();
- ASSERT_EQ(registry.get_or_emplace<int>(e3, 3), 3);
- ASSERT_EQ(registry.get_or_emplace<char>(e3, 'c'), 'c');
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 1u);
- ASSERT_FALSE(registry.storage<int>().empty());
- ASSERT_FALSE(registry.storage<char>().empty());
- ASSERT_TRUE((registry.all_of<int, char>(e3)));
- ASSERT_EQ(registry.get<int>(e3), 3);
- ASSERT_EQ(registry.get<char>(e3), 'c');
- ASSERT_NO_THROW(registry.clear<int>());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 1u);
- ASSERT_TRUE(registry.storage<int>().empty());
- ASSERT_FALSE(registry.storage<char>().empty());
- ASSERT_NO_THROW(registry.clear());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_TRUE(registry.storage<int>().empty());
- ASSERT_TRUE(registry.storage<char>().empty());
- const auto e4 = registry.create();
- const auto e5 = registry.create();
- registry.emplace<int>(e4);
- ASSERT_EQ(registry.remove<int>(e4), 1u);
- ASSERT_EQ(registry.remove<int>(e5), 0u);
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_TRUE(registry.storage<int>().empty());
- }
- TEST(Registry, Constructors) {
- entt::registry registry{};
- entt::registry other{64u};
- ASSERT_TRUE(registry.storage<entt::entity>().empty());
- ASSERT_NO_THROW([[maybe_unused]] auto alloc = registry.get_allocator());
- ASSERT_EQ(registry.storage().begin(), registry.storage().end());
- ASSERT_TRUE(other.storage<entt::entity>().empty());
- ASSERT_NO_THROW([[maybe_unused]] auto alloc = other.get_allocator());
- ASSERT_EQ(other.storage().begin(), other.storage().end());
- }
- TEST(Registry, Move) {
- entt::registry registry{};
- const auto entity = registry.create();
- owner test{};
- registry.on_construct<int>().connect<&owner::receive>(test);
- registry.on_destroy<int>().connect<&owner::receive>(test);
- ASSERT_EQ(test.parent, nullptr);
- registry.emplace<int>(entity);
- ASSERT_EQ(test.parent, ®istry);
- entt::registry other{std::move(registry)};
- other.erase<int>(entity);
- registry = {};
- registry.emplace<int>(registry.create(entity));
- ASSERT_EQ(test.parent, &other);
- registry = std::move(other);
- registry.emplace<int>(entity);
- registry.emplace<int>(registry.create(entity));
- ASSERT_EQ(test.parent, ®istry);
- }
- TEST(Registry, Swap) {
- entt::registry registry{};
- const auto entity = registry.create();
- owner test{};
- registry.on_construct<int>().connect<&owner::receive>(test);
- registry.on_destroy<int>().connect<&owner::receive>(test);
- ASSERT_EQ(test.parent, nullptr);
- registry.emplace<int>(entity);
- ASSERT_EQ(test.parent, ®istry);
- entt::registry other{};
- other.swap(registry);
- other.erase<int>(entity);
- registry = {};
- registry.emplace<int>(registry.create(entity));
- ASSERT_EQ(test.parent, &other);
- registry.swap(other);
- registry.emplace<int>(entity);
- registry.emplace<int>(registry.create(entity));
- ASSERT_EQ(test.parent, ®istry);
- }
- TEST(Registry, StorageIterable) {
- using namespace entt::literals;
- entt::registry registry{};
- const auto entity = registry.create();
- auto &storage = registry.storage<int>("other"_hs);
- storage.emplace(entity);
- for(auto [id, pool]: registry.storage()) {
- testing::StaticAssertTypeEq<decltype(pool), entt::sparse_set &>();
- testing::StaticAssertTypeEq<decltype(id), entt::id_type>();
- ASSERT_TRUE(pool.contains(entity));
- ASSERT_EQ(&storage, &pool);
- ASSERT_EQ(id, "other"_hs);
- }
- }
- TEST(Registry, ConstStorageIterable) {
- using namespace entt::literals;
- entt::registry registry{};
- const auto entity = registry.create();
- auto &storage = registry.storage<int>("other"_hs);
- storage.emplace(entity);
- for(auto &&curr: std::as_const(registry).storage()) {
- testing::StaticAssertTypeEq<decltype(curr.second), const entt::sparse_set &>();
- testing::StaticAssertTypeEq<decltype(curr.first), entt::id_type>();
- ASSERT_TRUE(curr.second.contains(entity));
- ASSERT_EQ(&storage, &curr.second);
- ASSERT_EQ(curr.first, "other"_hs);
- }
- }
- TEST(Registry, RegistryStorageIterator) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- auto iterable = registry.storage();
- auto end{iterable.begin()};
- decltype(end) begin{};
- begin = iterable.end();
- std::swap(begin, end);
- ASSERT_EQ(begin, iterable.cbegin());
- ASSERT_EQ(end, iterable.cend());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin++, iterable.begin());
- ASSERT_EQ(begin--, iterable.end());
- ASSERT_EQ(begin + 1, iterable.end());
- ASSERT_EQ(end - 1, iterable.begin());
- ASSERT_EQ(++begin, iterable.end());
- ASSERT_EQ(--begin, iterable.begin());
- ASSERT_EQ(begin += 1, iterable.end());
- ASSERT_EQ(begin -= 1, iterable.begin());
- ASSERT_EQ(begin + (end - begin), iterable.end());
- ASSERT_EQ(begin - (begin - end), iterable.end());
- ASSERT_EQ(end - (end - begin), iterable.begin());
- ASSERT_EQ(end + (begin - end), iterable.begin());
- ASSERT_EQ(begin[0u].first, iterable.begin()->first);
- ASSERT_EQ(&begin[0u].second, &(*iterable.begin()).second);
- ASSERT_LT(begin, end);
- ASSERT_LE(begin, iterable.begin());
- ASSERT_GT(end, begin);
- ASSERT_GE(end, iterable.end());
- ASSERT_EQ(begin[0u].first, entt::type_id<int>().hash());
- ASSERT_TRUE(begin[0u].second.contains(entity));
- }
- TEST(Registry, RegistryConstStorageIterator) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- auto iterable = std::as_const(registry).storage();
- auto end{iterable.begin()};
- decltype(end) begin{};
- begin = iterable.end();
- std::swap(begin, end);
- ASSERT_EQ(begin, iterable.cbegin());
- ASSERT_EQ(end, iterable.cend());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin++, iterable.begin());
- ASSERT_EQ(begin--, iterable.end());
- ASSERT_EQ(begin + 1, iterable.end());
- ASSERT_EQ(end - 1, iterable.begin());
- ASSERT_EQ(++begin, iterable.end());
- ASSERT_EQ(--begin, iterable.begin());
- ASSERT_EQ(begin += 1, iterable.end());
- ASSERT_EQ(begin -= 1, iterable.begin());
- ASSERT_EQ(begin + (end - begin), iterable.end());
- ASSERT_EQ(begin - (begin - end), iterable.end());
- ASSERT_EQ(end - (end - begin), iterable.begin());
- ASSERT_EQ(end + (begin - end), iterable.begin());
- ASSERT_EQ(begin[0u].first, iterable.begin()->first);
- ASSERT_EQ(&begin[0u].second, &(*iterable.begin()).second);
- ASSERT_LT(begin, end);
- ASSERT_LE(begin, iterable.begin());
- ASSERT_GT(end, begin);
- ASSERT_GE(end, iterable.end());
- ASSERT_EQ(begin[0u].first, entt::type_id<int>().hash());
- ASSERT_TRUE(begin[0u].second.contains(entity));
- }
- TEST(Registry, RegistryStorageIteratorConversion) {
- entt::registry registry{};
- registry.storage<int>();
- auto proxy = registry.storage();
- [[maybe_unused]] auto cproxy = std::as_const(registry).storage();
- const typename decltype(proxy)::iterator it = proxy.begin();
- typename decltype(cproxy)::iterator cit = it;
- testing::StaticAssertTypeEq<decltype(*it), std::pair<entt::id_type, entt::sparse_set &>>();
- testing::StaticAssertTypeEq<decltype(*cit), std::pair<entt::id_type, const entt::sparse_set &>>();
- ASSERT_EQ(it->first, entt::type_id<int>().hash());
- ASSERT_EQ((*it).second.type(), entt::type_id<int>());
- ASSERT_EQ(it->first, cit->first);
- ASSERT_EQ((*it).second.type(), (*cit).second.type());
- ASSERT_EQ(it - cit, 0);
- ASSERT_EQ(cit - it, 0);
- ASSERT_LE(it, cit);
- ASSERT_LE(cit, it);
- ASSERT_GE(it, cit);
- ASSERT_GE(cit, it);
- ASSERT_EQ(it, cit);
- ASSERT_NE(++cit, it);
- }
- TEST(Registry, Storage) {
- using namespace entt::literals;
- entt::registry registry{};
- auto &storage = registry.storage<test::empty>("other"_hs);
- const auto entity = registry.create();
- testing::StaticAssertTypeEq<decltype(registry.storage<test::empty>()), entt::storage_type_t<test::empty> &>();
- testing::StaticAssertTypeEq<decltype(registry.storage<const test::empty>()), const entt::storage_type_t<test::empty> &>();
- testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage<test::empty>()), const entt::storage_type_t<test::empty> *>();
- testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage<const test::empty>()), const entt::storage_type_t<test::empty> *>();
- testing::StaticAssertTypeEq<decltype(registry.storage("other"_hs)), entt::storage_type_t<test::empty>::base_type *>();
- testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage("other"_hs)), const entt::storage_type_t<test::empty>::base_type *>();
- ASSERT_EQ(®istry.storage<test::empty>("other"_hs), &storage);
- ASSERT_EQ(®istry.storage<const test::empty>("other"_hs), &storage);
- ASSERT_NE(std::as_const(registry).storage<test::empty>(), &storage);
- ASSERT_NE(std::as_const(registry).storage<const test::empty>(), &storage);
- ASSERT_NE(registry.storage("other"_hs), nullptr);
- ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
- ASSERT_FALSE(registry.any_of<test::empty>(entity));
- ASSERT_FALSE(storage.contains(entity));
- registry.emplace<test::empty>(entity);
- ASSERT_FALSE(storage.contains(entity));
- ASSERT_TRUE(registry.any_of<test::empty>(entity));
- ASSERT_EQ((entt::basic_view{registry.storage<test::empty>(), storage}.size_hint()), 0u);
- storage.emplace(entity);
- ASSERT_TRUE(storage.contains(entity));
- ASSERT_TRUE(registry.any_of<test::empty>(entity));
- ASSERT_EQ((entt::basic_view{registry.storage<test::empty>(), storage}.size_hint()), 1u);
- registry.destroy(entity);
- ASSERT_EQ(registry.create(entity), entity);
- ASSERT_FALSE(storage.contains(entity));
- ASSERT_FALSE(registry.any_of<test::empty>(entity));
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, Storage) {
- using namespace entt::literals;
- entt::registry registry{};
- registry.storage<test::empty>("other"_hs);
- ASSERT_DEATH(registry.storage<int>("other"_hs), "");
- ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<int>("other"_hs), "");
- ASSERT_DEATH(registry.storage<entt::entity>("other"_hs), "");
- ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<entt::entity>("other"_hs), "");
- }
- TEST(Registry, StorageReset) {
- using namespace entt::literals;
- entt::registry registry{};
- registry.storage<int>();
- registry.storage<int>("other"_hs);
- ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_NE(registry.storage("other"_hs), nullptr);
- ASSERT_EQ(registry.reset("other"_hs), 1u);
- ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_EQ(registry.storage("other"_hs), nullptr);
- ASSERT_EQ(registry.reset("other"_hs), 0u);
- ASSERT_EQ(registry.reset(entt::type_id<int>().hash()), 1u);
- ASSERT_EQ(registry.reset(entt::type_id<int>().hash()), 0u);
- ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_EQ(registry.storage("other"_hs), nullptr);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, StorageReset) {
- entt::registry registry{};
- const entt::entity entity = registry.create();
- ASSERT_TRUE(registry.valid(entity));
- ASSERT_DEATH(registry.reset(entt::type_id<entt::entity>().hash()), "");
- ASSERT_TRUE(registry.valid(entity));
- }
- TEST(Registry, Identifiers) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- const auto pre = registry.create();
- ASSERT_EQ(traits_type::to_integral(pre), traits_type::to_entity(pre));
- registry.destroy(pre);
- const auto post = registry.create();
- ASSERT_NE(pre, post);
- ASSERT_EQ(traits_type::to_entity(pre), traits_type::to_entity(post));
- ASSERT_NE(traits_type::to_version(pre), traits_type::to_version(post));
- ASSERT_NE(traits_type::to_version(pre), registry.current(pre));
- ASSERT_EQ(traits_type::to_version(post), registry.current(post));
- const auto invalid = traits_type::combine(traits_type::to_entity(post) + 1u, {});
- ASSERT_EQ(traits_type::to_version(invalid), typename traits_type::version_type{});
- ASSERT_EQ(registry.current(invalid), traits_type::to_version(entt::tombstone));
- }
- TEST(Registry, MoreOnIdentifiers) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- const std::array entity{registry.create(), registry.create()};
- registry.destroy(entity[0]);
- ASSERT_FALSE(registry.valid(entity[0]));
- ASSERT_TRUE(registry.valid(entity[1]));
- ASSERT_EQ(registry.current(entt::null), traits_type::to_version(entt::tombstone));
- ASSERT_EQ(registry.current(entity[0]), traits_type::to_version(entity[0]) + 1u);
- ASSERT_EQ(registry.current(entity[1]), traits_type::to_version(entity[1]));
- registry.destroy(entity[1]);
- ASSERT_FALSE(registry.valid(entity[1]));
- ASSERT_EQ(registry.current(entity[1]), traits_type::to_version(entity[1]) + 1u);
- }
- TEST(Registry, VersionOverflow) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- const auto entity = registry.create();
- registry.destroy(entity);
- ASSERT_NE(registry.current(entity), traits_type::to_version(entity));
- ASSERT_NE(registry.current(entity), typename traits_type::version_type{});
- registry.destroy(registry.create(), traits_type::to_version(entt::tombstone) - 1u);
- registry.destroy(registry.create());
- ASSERT_EQ(registry.current(entity), traits_type::to_version(entity));
- ASSERT_EQ(registry.current(entity), typename traits_type::version_type{});
- }
- TEST(Registry, NullEntity) {
- entt::registry registry{};
- const entt::entity entity = entt::null;
- ASSERT_FALSE(registry.valid(entity));
- ASSERT_NE(registry.create(entity), entity);
- }
- TEST(Registry, TombstoneVersion) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- const entt::entity entity = entt::tombstone;
- ASSERT_FALSE(registry.valid(entity));
- const auto other = registry.create();
- const auto vers = traits_type::to_version(entity);
- const auto required = traits_type::construct(traits_type::to_entity(other), vers);
- ASSERT_NE(registry.destroy(other, vers), vers);
- ASSERT_NE(registry.create(required), required);
- }
- TEST(Registry, CreateManyEntitiesAtOnce) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- const auto entt = registry.create();
- registry.destroy(registry.create());
- registry.destroy(entt);
- registry.destroy(registry.create());
- registry.create(entity.begin(), entity.end());
- ASSERT_TRUE(registry.valid(entity[0]));
- ASSERT_TRUE(registry.valid(entity[1]));
- ASSERT_TRUE(registry.valid(entity[2]));
- ASSERT_EQ(traits_type::to_entity(entity[0]), 0u);
- ASSERT_EQ(traits_type::to_version(entity[0]), 2u);
- ASSERT_EQ(traits_type::to_entity(entity[1]), 1u);
- ASSERT_EQ(traits_type::to_version(entity[1]), 1u);
- ASSERT_EQ(traits_type::to_entity(entity[2]), 2u);
- ASSERT_EQ(traits_type::to_version(entity[2]), 0u);
- }
- TEST(Registry, CreateManyEntitiesAtOnceWithListener) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- listener listener;
- registry.on_construct<int>().connect<&listener::incr>(listener);
- registry.create(entity.begin(), entity.end());
- registry.insert(entity.begin(), entity.end(), 1);
- registry.insert(entity.begin(), entity.end(), 'c');
- ASSERT_EQ(registry.get<int>(entity[0]), 1);
- ASSERT_EQ(registry.get<char>(entity[1]), 'c');
- ASSERT_EQ(listener.counter, 3);
- registry.on_construct<int>().disconnect<&listener::incr>(listener);
- registry.on_construct<test::empty>().connect<&listener::incr>(listener);
- registry.create(entity.begin(), entity.end());
- registry.insert(entity.begin(), entity.end(), 'a');
- registry.insert<test::empty>(entity.begin(), entity.end());
- ASSERT_TRUE(registry.all_of<test::empty>(entity[0]));
- ASSERT_EQ(registry.get<char>(entity[2]), 'a');
- ASSERT_EQ(listener.counter, 6);
- }
- TEST(Registry, CreateWithHint) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- auto e3 = registry.create(entt::entity{3});
- auto e2 = registry.create(entt::entity{3});
- ASSERT_EQ(e2, entt::entity{0});
- ASSERT_FALSE(registry.valid(entt::entity{1}));
- ASSERT_FALSE(registry.valid(entt::entity{2}));
- ASSERT_EQ(e3, entt::entity{3});
- registry.destroy(e2);
- ASSERT_EQ(traits_type::to_version(e2), 0u);
- ASSERT_EQ(registry.current(e2), 1u);
- e2 = registry.create();
- auto e1 = registry.create(entt::entity{2});
- ASSERT_EQ(traits_type::to_entity(e2), 0u);
- ASSERT_EQ(traits_type::to_version(e2), 1u);
- ASSERT_EQ(traits_type::to_entity(e1), 2u);
- ASSERT_EQ(traits_type::to_version(e1), 0u);
- registry.destroy(e1);
- registry.destroy(e2);
- auto e0 = registry.create(entt::entity{0});
- ASSERT_EQ(e0, entt::entity{0});
- ASSERT_EQ(traits_type::to_version(e0), 0u);
- }
- TEST(Registry, CreateClearCycle) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- entt::entity pre = entt::null;
- entt::entity post = entt::null;
- const std::size_t first_iteration = 10u;
- const std::size_t second_iteration = 7u;
- const std::size_t third_iteration = 5u;
- for(std::size_t i{}; i < first_iteration; ++i) {
- const auto entity = registry.create();
- registry.emplace<double>(entity);
- }
- registry.clear();
- for(std::size_t i{}; i < second_iteration; ++i) {
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- if(i == 3u) {
- pre = entity;
- }
- }
- registry.clear();
- for(std::size_t i{}; i < third_iteration; ++i) {
- const auto entity = registry.create();
- if(i == 3u) {
- post = entity;
- }
- }
- ASSERT_FALSE(registry.valid(pre));
- ASSERT_TRUE(registry.valid(post));
- ASSERT_NE(traits_type::to_version(pre), traits_type::to_version(post));
- ASSERT_EQ(traits_type::to_version(pre) + 1, traits_type::to_version(post));
- ASSERT_EQ(registry.current(pre), registry.current(post));
- }
- TEST(Registry, CreateDestroyReleaseCornerCase) {
- entt::registry registry{};
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- registry.destroy(e0);
- registry.storage<entt::entity>().erase(e1);
- ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
- ASSERT_EQ(registry.current(e0), 1u);
- ASSERT_EQ(registry.current(e1), 1u);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, CreateTooManyEntities) {
- entt::basic_registry<test::entity> registry{};
- std::vector<test::entity> entity(entt::entt_traits<test::entity>::to_entity(entt::null));
- registry.create(entity.begin(), entity.end());
- ASSERT_DEATH([[maybe_unused]] const auto entt = registry.create(), "");
- }
- TEST(Registry, DestroyVersion) {
- entt::registry registry{};
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- ASSERT_EQ(registry.current(e0), 0u);
- ASSERT_EQ(registry.current(e1), 0u);
- registry.destroy(e0);
- registry.destroy(e1, 3);
- ASSERT_EQ(registry.current(e0), 1u);
- ASSERT_EQ(registry.current(e1), 3u);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, DestroyVersion) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.destroy(entity);
- ASSERT_DEATH(registry.destroy(entity), "");
- ASSERT_DEATH(registry.destroy(entity, 3), "");
- }
- TEST(Registry, DestroyRange) {
- entt::registry registry{};
- const auto iview = registry.view<int>();
- const auto icview = registry.view<int, char>();
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<char>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<char>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_TRUE(registry.valid(entity[0u]));
- ASSERT_TRUE(registry.valid(entity[1u]));
- ASSERT_TRUE(registry.valid(entity[2u]));
- registry.destroy(icview.begin(), icview.end());
- ASSERT_FALSE(registry.valid(entity[0u]));
- ASSERT_FALSE(registry.valid(entity[1u]));
- ASSERT_TRUE(registry.valid(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_EQ(registry.storage<double>().size(), 0u);
- registry.destroy(iview.begin(), iview.end());
- ASSERT_FALSE(registry.valid(entity[2u]));
- ASSERT_NO_THROW(registry.destroy(iview.rbegin(), iview.rend()));
- ASSERT_EQ(iview.size(), 0u);
- ASSERT_EQ(icview.size_hint(), 0u);
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_EQ(registry.storage<double>().size(), 0u);
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end());
- ASSERT_TRUE(registry.valid(entity[0u]));
- ASSERT_TRUE(registry.valid(entity[1u]));
- ASSERT_TRUE(registry.valid(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 3u);
- registry.destroy(entity.begin(), entity.end());
- ASSERT_FALSE(registry.valid(entity[0u]));
- ASSERT_FALSE(registry.valid(entity[1u]));
- ASSERT_FALSE(registry.valid(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- entt::sparse_set managed{};
- registry.create(entity.begin(), entity.end());
- managed.push(entity.begin(), entity.end());
- registry.insert<int>(managed.begin(), managed.end());
- ASSERT_TRUE(registry.valid(managed[0u]));
- ASSERT_TRUE(registry.valid(managed[1u]));
- ASSERT_TRUE(registry.valid(managed[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 3u);
- registry.destroy(managed.begin(), managed.end());
- ASSERT_FALSE(registry.valid(managed[0u]));
- ASSERT_FALSE(registry.valid(managed[1u]));
- ASSERT_FALSE(registry.valid(managed[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- }
- TEST(Registry, StableDestroy) {
- entt::registry registry{};
- const auto iview = registry.view<int>();
- const auto icview = registry.view<int, test::pointer_stable>();
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<test::pointer_stable>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<test::pointer_stable>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_TRUE(registry.valid(entity[0u]));
- ASSERT_TRUE(registry.valid(entity[1u]));
- ASSERT_TRUE(registry.valid(entity[2u]));
- registry.destroy(icview.begin(), icview.end());
- ASSERT_FALSE(registry.valid(entity[0u]));
- ASSERT_FALSE(registry.valid(entity[1u]));
- ASSERT_TRUE(registry.valid(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- ASSERT_EQ(registry.storage<double>().size(), 0u);
- registry.destroy(iview.begin(), iview.end());
- ASSERT_FALSE(registry.valid(entity[2u]));
- ASSERT_EQ(iview.size(), 0u);
- ASSERT_EQ(icview.size_hint(), 0u);
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- ASSERT_EQ(registry.storage<double>().size(), 0u);
- }
- TEST(Registry, Emplace) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_FALSE(registry.all_of<int>(entity));
- const auto &ref = registry.emplace<int>(entity, 4);
- ASSERT_TRUE(registry.all_of<int>(entity));
- ASSERT_EQ(registry.get<int>(entity), ref);
- ASSERT_EQ(ref, 4);
- }
- TEST(Registry, EmplaceEmpty) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_FALSE(registry.all_of<test::empty>(entity));
- registry.emplace<test::empty>(entity, 4);
- ASSERT_TRUE(registry.all_of<test::empty>(entity));
- }
- TEST(Registry, EmplaceAggregate) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_FALSE(registry.all_of<test::aggregate>(entity));
- const auto &ref = registry.emplace<test::aggregate>(entity, 4);
- ASSERT_TRUE(registry.all_of<test::aggregate>(entity));
- ASSERT_EQ(registry.get<test::aggregate>(entity).value, ref.value);
- ASSERT_EQ(ref.value, 4);
- }
- TEST(Registry, EmplaceTypesFromStandardTemplateLibrary) {
- // see #37 - the test shouldn't crash, that's all
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<std::unordered_set<int>>(entity).insert(1);
- registry.destroy(entity);
- }
- TEST(Registry, EmplaceWithComponents) {
- // it should compile, that's all
- entt::registry registry{};
- const auto value = 0;
- registry.emplace<int>(registry.create(), value);
- }
- TEST(Registry, EmplaceMoveOnlyComponent) {
- entt::registry registry{};
- // the purpose is to ensure that move only types are always accepted
- registry.emplace<std::unique_ptr<int>>(registry.create());
- }
- TEST(Registry, EmplaceNonDefaultConstructibleComponent) {
- entt::registry registry{};
- // the purpose is to ensure that non default constructible type are always accepted
- registry.emplace<test::non_default_constructible>(registry.create(), 1);
- }
- TEST(Registry, StableEmplace) {
- entt::registry registry{};
- registry.on_construct<int>().connect<&listener::sort<int>>();
- registry.emplace<int>(registry.create(), 0);
- ASSERT_EQ(registry.emplace<int>(registry.create(), 1), 1);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, Emplace) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.destroy(entity);
- ASSERT_DEATH(registry.emplace<int>(entity), "");
- }
- TEST(Registry, Insert) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<char>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<char>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_FALSE(registry.all_of<float>(entity[0u]));
- ASSERT_FALSE(registry.all_of<float>(entity[1u]));
- ASSERT_FALSE(registry.all_of<float>(entity[2u]));
- const auto icview = registry.view<int, char>();
- registry.insert(icview.begin(), icview.end(), 3.f);
- ASSERT_EQ(registry.get<float>(entity[0u]), 3.f);
- ASSERT_EQ(registry.get<float>(entity[1u]), 3.f);
- ASSERT_FALSE(registry.all_of<float>(entity[2u]));
- registry.clear<float>();
- std::array value{0.f, 1.f, 2.f};
- const auto iview = registry.view<int>();
- registry.insert<float>(iview.rbegin(), iview.rend(), value.begin());
- ASSERT_EQ(registry.get<float>(entity[0u]), 0.f);
- ASSERT_EQ(registry.get<float>(entity[1u]), 1.f);
- ASSERT_EQ(registry.get<float>(entity[2u]), 2.f);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, Insert) {
- entt::registry registry{};
- const std::array entity{registry.create()};
- const std::array value{0};
- registry.destroy(entity[0]);
- ASSERT_DEATH(registry.insert<int>(entity.begin(), entity.end(), value[0]), "");
- ASSERT_DEATH(registry.insert<int>(entity.begin(), entity.end(), value.begin()), "");
- }
- TEST(Registry, EmplaceOrReplace) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_FALSE(registry.all_of<int>(entity));
- const auto &ref = registry.emplace_or_replace<int>(entity, 4);
- ASSERT_TRUE(registry.all_of<int>(entity));
- ASSERT_EQ(registry.get<int>(entity), ref);
- ASSERT_EQ(ref, 4);
- registry.emplace_or_replace<int>(entity, 0);
- ASSERT_EQ(ref, 0);
- }
- TEST(Registry, EmplaceOrReplaceAggregate) {
- entt::registry registry{};
- const auto entity = registry.create();
- auto &instance = registry.emplace_or_replace<test::aggregate>(entity, 1);
- ASSERT_EQ(instance.value, 1);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, EmplaceOrReplace) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.destroy(entity);
- ASSERT_DEATH(registry.emplace_or_replace<int>(entity), "");
- }
- TEST(Registry, Patch) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<int>(entity, 3);
- ASSERT_EQ(registry.get<int>(entity), 3);
- registry.patch<int>(entity);
- ASSERT_EQ(registry.get<int>(entity), 3);
- registry.patch<int>(entity, [](auto &value) { value = 1; });
- ASSERT_EQ(registry.get<int>(entity), 1);
- }
- TEST(Registry, Replace) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<int>(entity, 3);
- ASSERT_EQ(registry.get<int>(entity), 3);
- registry.replace<int>(entity);
- ASSERT_EQ(registry.get<int>(entity), 0);
- registry.replace<int>(entity, 1);
- ASSERT_EQ(registry.get<int>(entity), 1);
- }
- TEST(Registry, ReplaceAggregate) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<test::aggregate>(entity, 0);
- auto &instance = registry.replace<test::aggregate>(entity, 1);
- ASSERT_EQ(instance.value, 1);
- }
- TEST(Registry, Remove) {
- entt::registry registry{};
- const auto iview = registry.view<int>();
- const auto icview = registry.view<int, char>();
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<char>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<char>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_TRUE(registry.any_of<int>(entity[0u]));
- ASSERT_TRUE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- registry.remove<int, char>(entity[0u]);
- ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 2u);
- ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 0u);
- ASSERT_FALSE(registry.any_of<int>(entity[0u]));
- ASSERT_FALSE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
- ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
- ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
- ASSERT_FALSE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.remove<int>(iview.begin(), iview.end()), 0u);
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
- registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 1u);
- registry.remove<int, char>(iview.begin(), iview.end());
- registry.remove<int, char>(iview.begin(), iview.end());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- registry.insert<int>(entity.begin(), entity.end());
- registry.insert<char>(entity.begin(), entity.end());
- ASSERT_EQ(registry.storage<int>().size(), 3u);
- ASSERT_EQ(registry.storage<char>().size(), 3u);
- registry.remove<int, char>(entity.begin(), entity.end());
- registry.remove<int, char>(entity.begin(), entity.end());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_FALSE(registry.orphan(entity[0u]));
- ASSERT_TRUE(registry.orphan(entity[1u]));
- ASSERT_TRUE(registry.orphan(entity[2u]));
- }
- TEST(Registry, StableRemove) {
- entt::registry registry{};
- const auto iview = registry.view<int>();
- const auto icview = registry.view<int, test::pointer_stable>();
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<test::pointer_stable>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<test::pointer_stable>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_TRUE(registry.any_of<int>(entity[0u]));
- ASSERT_TRUE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- registry.remove<int, test::pointer_stable>(entity[0u]);
- ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 2u);
- ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 0u);
- ASSERT_FALSE(registry.any_of<int>(entity[0u]));
- ASSERT_FALSE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
- ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
- ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
- ASSERT_FALSE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- }
- TEST(Registry, Erase) {
- entt::registry registry{};
- const auto iview = registry.view<int>();
- const auto icview = registry.view<int, char>();
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<char>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<char>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_TRUE(registry.any_of<int>(entity[0u]));
- ASSERT_TRUE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- registry.erase<int, char>(entity[0u]);
- registry.erase<int, char>(icview.begin(), icview.end());
- ASSERT_FALSE(registry.any_of<int>(entity[0u]));
- ASSERT_FALSE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- registry.erase<int>(iview.begin(), iview.end());
- ASSERT_FALSE(registry.any_of<int>(entity[2u]));
- ASSERT_NO_THROW(registry.erase<int>(iview.rbegin(), iview.rend()));
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
- registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<char>().size(), 1u);
- registry.erase<int, char>(iview.begin(), iview.end());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- registry.insert<int>(entity.begin(), entity.end());
- registry.insert<char>(entity.begin(), entity.end());
- ASSERT_EQ(registry.storage<int>().size(), 3u);
- ASSERT_EQ(registry.storage<char>().size(), 3u);
- registry.erase<int, char>(entity.begin(), entity.end());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<char>().size(), 0u);
- ASSERT_FALSE(registry.orphan(entity[0u]));
- ASSERT_TRUE(registry.orphan(entity[1u]));
- ASSERT_TRUE(registry.orphan(entity[2u]));
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, Erase) {
- entt::registry registry{};
- const std::array entity{registry.create()};
- ASSERT_FALSE((registry.any_of<int>(entity[0u])));
- ASSERT_DEATH((registry.erase<int>(entity.begin(), entity.end())), "");
- ASSERT_DEATH(registry.erase<int>(entity[0u]), "");
- }
- TEST(Registry, StableErase) {
- entt::registry registry{};
- const auto iview = registry.view<int>();
- const auto icview = registry.view<int, test::pointer_stable>();
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<test::pointer_stable>(entity[0u]);
- registry.emplace<double>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<test::pointer_stable>(entity[1u]);
- registry.emplace<int>(entity[2u]);
- ASSERT_TRUE(registry.any_of<int>(entity[0u]));
- ASSERT_TRUE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- registry.erase<int, test::pointer_stable>(entity[0u]);
- registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
- registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
- ASSERT_FALSE(registry.any_of<int>(entity[0u]));
- ASSERT_FALSE(registry.all_of<int>(entity[1u]));
- ASSERT_TRUE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 1u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- registry.erase<int>(iview.begin(), iview.end());
- ASSERT_FALSE(registry.any_of<int>(entity[2u]));
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- ASSERT_EQ(registry.storage<double>().size(), 1u);
- }
- TEST(Registry, EraseIf) {
- using namespace entt::literals;
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.storage<int>("other"_hs).emplace(entity);
- registry.emplace<char>(entity);
- ASSERT_TRUE(registry.storage<int>().contains(entity));
- ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
- ASSERT_TRUE(registry.storage<char>().contains(entity));
- registry.erase_if(entity, [](auto &&...) { return false; });
- ASSERT_TRUE(registry.storage<int>().contains(entity));
- ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
- ASSERT_TRUE(registry.storage<char>().contains(entity));
- registry.erase_if(entity, [](entt::id_type id, auto &&...) { return id == "other"_hs; });
- ASSERT_TRUE(registry.storage<int>().contains(entity));
- ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
- ASSERT_TRUE(registry.storage<char>().contains(entity));
- registry.erase_if(entity, [](auto, const auto &storage) { return storage.type() == entt::type_id<char>(); });
- ASSERT_TRUE(registry.storage<int>().contains(entity));
- ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
- ASSERT_FALSE(registry.storage<char>().contains(entity));
- }
- TEST(Registry, Compact) {
- entt::registry registry{};
- std::array<entt::entity, 2u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<test::pointer_stable>(entity[0u]);
- registry.emplace<int>(entity[1u]);
- registry.emplace<test::pointer_stable>(entity[1u]);
- ASSERT_EQ(registry.storage<int>().size(), 2u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- registry.destroy(entity.begin(), entity.end());
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- registry.compact<int>();
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
- registry.compact();
- ASSERT_EQ(registry.storage<int>().size(), 0u);
- ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 0u);
- }
- TEST(Registry, AllAnyOf) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_FALSE(registry.all_of<int>(entity));
- ASSERT_FALSE((registry.all_of<int, char>(entity)));
- ASSERT_FALSE((registry.any_of<int, char>(entity)));
- registry.emplace<int>(entity);
- ASSERT_TRUE(registry.all_of<int>(entity));
- ASSERT_FALSE((registry.all_of<int, char>(entity)));
- ASSERT_TRUE((registry.any_of<int, char>(entity)));
- registry.emplace<char>(entity);
- ASSERT_TRUE(registry.all_of<int>(entity));
- ASSERT_TRUE((registry.all_of<int, char>(entity)));
- ASSERT_TRUE((registry.any_of<int, char>(entity)));
- }
- TEST(Registry, Get) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<int>(entity, 1);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(std::as_const(registry).get<int>(entity), 1);
- ASSERT_EQ((std::as_const(registry).get<int, char>(entity)), std::make_tuple(1, 'c'));
- registry.get<int>(entity) = 3;
- std::get<1>(registry.get<int, char>(entity)) = 'a';
- ASSERT_EQ(registry.get<int>(entity), 3);
- ASSERT_EQ((registry.get<int, char>(entity)), std::make_tuple(3, 'a'));
- }
- TEST(Registry, GetOrEmplace) {
- entt::registry registry{};
- const auto entity = registry.create();
- const auto value = 3;
- const auto other = 1.;
- ASSERT_EQ(registry.get_or_emplace<int>(entity, value), value);
- ASSERT_EQ(registry.get_or_emplace<double>(entity, other), other);
- ASSERT_TRUE((registry.all_of<int, double>(entity)));
- ASSERT_EQ(registry.get<int>(entity), value);
- ASSERT_EQ(registry.get<double>(entity), other);
- }
- TEST(Registry, GetOrEmplaceEmpty) {
- entt::registry registry{};
- const auto entity = registry.create();
- // get_or_emplace must work for empty types
- // NOLINTNEXTLINE(readability-redundant-casting)
- static_cast<void>(registry.get_or_emplace<test::empty>(entity));
- ASSERT_TRUE((registry.all_of<test::empty>(entity)));
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, GetOrEmplace) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.destroy(entity);
- ASSERT_DEATH([[maybe_unused]] const auto value = registry.get_or_emplace<int>(entity), "");
- }
- TEST(Registry, TryGet) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_EQ(registry.try_get<int>(entity), nullptr);
- ASSERT_EQ(std::as_const(registry).try_get<int>(entity), nullptr);
- ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
- const int &elem = registry.emplace<int>(entity);
- ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_EQ(registry.try_get<int>(entity), &elem);
- ASSERT_EQ(std::as_const(registry).try_get<int>(entity), &elem);
- }
- TEST(Registry, Clear) {
- entt::registry registry{};
- const std::array entity{registry.create(), registry.create()};
- registry.insert<int>(entity.begin(), entity.end());
- registry.insert<char>(entity.begin(), entity.end());
- ASSERT_TRUE((registry.all_of<int, char>(entity[0])));
- ASSERT_TRUE((registry.all_of<int, char>(entity[1])));
- registry.clear<int>();
- ASSERT_TRUE(registry.valid(entity[0]));
- ASSERT_TRUE(registry.valid(entity[1]));
- ASSERT_FALSE(registry.all_of<int>(entity[0]));
- ASSERT_FALSE(registry.all_of<int>(entity[1]));
- ASSERT_TRUE(registry.all_of<char>(entity[0]));
- ASSERT_TRUE(registry.all_of<char>(entity[1]));
- registry.clear();
- ASSERT_FALSE(registry.valid(entity[0]));
- ASSERT_FALSE(registry.valid(entity[1]));
- ASSERT_TRUE(registry.storage<int>().empty());
- ASSERT_TRUE(registry.storage<char>().empty());
- }
- TEST(Registry, Orphan) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u]);
- registry.emplace<int>(entity[2u]);
- for(auto [entt]: registry.storage<entt::entity>().each()) {
- ASSERT_TRUE(entt != entity[1u] || registry.orphan(entt));
- }
- registry.erase<int>(entity[0u]);
- registry.erase<int>(entity[2u]);
- for(auto [entt]: registry.storage<entt::entity>().each()) {
- ASSERT_TRUE(registry.orphan(entt));
- }
- }
- TEST(Registry, Signals) {
- entt::registry registry{};
- std::array<entt::entity, 2u> entity{};
- listener listener;
- registry.on_construct<test::empty>().connect<&listener::incr>(listener);
- registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
- registry.on_construct<int>().connect<&listener::incr>(listener);
- registry.on_destroy<int>().connect<&listener::decr>(listener);
- registry.create(entity.begin(), entity.end());
- registry.insert<test::empty>(entity.begin(), entity.end());
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[1u]);
- registry.insert<int>(entity.rbegin(), entity.rend());
- ASSERT_EQ(listener.counter, 4);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.erase<test::empty, int>(entity[0u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
- registry.on_destroy<int>().disconnect<&listener::decr>(listener);
- registry.erase<test::empty, int>(entity[1u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.on_construct<test::empty>().disconnect<&listener::incr>(listener);
- registry.on_construct<int>().disconnect<&listener::incr>(listener);
- registry.emplace<test::empty>(entity[1u]);
- registry.emplace<int>(entity[1u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.on_construct<int>().connect<&listener::incr>(listener);
- registry.on_destroy<int>().connect<&listener::decr>(listener);
- registry.emplace<int>(entity[0u]);
- registry.erase<int>(entity[1u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[1u]);
- registry.on_construct<test::empty>().connect<&listener::incr>(listener);
- registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
- registry.erase<test::empty>(entity[1u]);
- registry.emplace<test::empty>(entity[0u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.clear<test::empty, int>();
- ASSERT_EQ(listener.counter, 0);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.insert<test::empty>(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end());
- registry.destroy(entity[1u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[1u]);
- registry.erase<int, test::empty>(entity[0u]);
- registry.emplace_or_replace<int>(entity[0u]);
- registry.emplace_or_replace<test::empty>(entity[0u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
- registry.on_destroy<int>().disconnect<&listener::decr>(listener);
- registry.emplace_or_replace<test::empty>(entity[0u]);
- registry.emplace_or_replace<int>(entity[0u]);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.on_update<test::empty>().connect<&listener::incr>(listener);
- registry.on_update<int>().connect<&listener::incr>(listener);
- registry.emplace_or_replace<test::empty>(entity[0u]);
- registry.emplace_or_replace<int>(entity[0u]);
- ASSERT_EQ(listener.counter, 4);
- ASSERT_EQ(listener.last, entity[0u]);
- registry.replace<test::empty>(entity[0u]);
- registry.replace<int>(entity[0u]);
- ASSERT_EQ(listener.counter, 6);
- ASSERT_EQ(listener.last, entity[0u]);
- }
- TEST(Registry, SignalsOnRuntimePool) {
- using namespace entt::literals;
- entt::registry registry{};
- const auto entity = registry.create();
- listener listener;
- registry.on_construct<int>("custom"_hs).connect<&listener::incr>(listener);
- registry.on_update<int>("custom"_hs).connect<&listener::incr>(listener);
- registry.on_destroy<int>("custom"_hs).connect<&listener::incr>(listener);
- ASSERT_EQ(listener.counter, 0);
- registry.emplace<int>(entity);
- registry.patch<int>(entity);
- registry.erase<int>(entity);
- ASSERT_EQ(listener.counter, 0);
- registry.storage<int>("custom"_hs).emplace(entity);
- registry.storage<int>("custom"_hs).patch(entity);
- registry.storage<int>("custom"_hs).erase(entity);
- ASSERT_EQ(listener.counter, 3);
- }
- TEST(Registry, SignalsOnEntity) {
- entt::registry registry{};
- listener listener;
- registry.on_construct<entt::entity>().connect<&listener::incr>(listener);
- entt::entity entity = registry.create();
- entt::entity other = registry.create();
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, other);
- registry.destroy(other);
- registry.destroy(entity);
- ASSERT_EQ(listener.counter, 2);
- ASSERT_EQ(listener.last, other);
- registry.on_construct<entt::entity>().disconnect(&listener);
- other = registry.create();
- entity = registry.create();
- ASSERT_EQ(listener.counter, 2);
- ASSERT_NE(listener.last, entity);
- ASSERT_NE(listener.last, other);
- registry.on_update<entt::entity>().connect<&listener::decr>(listener);
- registry.patch<entt::entity>(entity);
- ASSERT_EQ(listener.counter, 1);
- ASSERT_EQ(listener.last, entity);
- registry.on_update<entt::entity>().disconnect(&listener);
- registry.patch<entt::entity>(other);
- ASSERT_EQ(listener.counter, 1);
- ASSERT_NE(listener.last, other);
- registry.on_destroy<entt::entity>().connect<&listener::decr>(listener);
- registry.destroy(entity);
- ASSERT_EQ(listener.counter, 0);
- ASSERT_EQ(listener.last, entity);
- registry.on_destroy<entt::entity>().disconnect(&listener);
- registry.destroy(other);
- ASSERT_EQ(listener.counter, 0);
- ASSERT_NE(listener.last, other);
- }
- TEST(Registry, SignalWhenDestroying) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.on_destroy<double>().connect<&entt::registry::remove<char>>();
- registry.emplace<double>(entity);
- registry.emplace<int>(entity);
- ASSERT_NE(registry.storage(entt::type_id<double>().hash()), nullptr);
- ASSERT_NE(registry.storage(entt::type_id<int>().hash()), nullptr);
- ASSERT_EQ(registry.storage(entt::type_id<char>().hash()), nullptr);
- ASSERT_TRUE(registry.valid(entity));
- registry.destroy(entity);
- ASSERT_NE(registry.storage(entt::type_id<char>().hash()), nullptr);
- ASSERT_FALSE(registry.valid(entity));
- }
- TEST(Registry, SelfSignal) {
- entt::registry registry{};
- const auto entity = registry.create();
- // required because of an issue of VS2019
- constexpr auto emplace_or_replace = &entt::registry::emplace_or_replace<double>;
- constexpr auto remove = &entt::registry::remove<double>;
- registry.on_construct<int>().connect<emplace_or_replace>();
- registry.on_destroy<int>().connect<remove>();
- registry.emplace<double>(entity, .3);
- ASSERT_FALSE(registry.all_of<int>(entity));
- ASSERT_EQ(registry.get<double>(entity), .3);
- registry.emplace<int>(entity);
- ASSERT_TRUE(registry.all_of<int>(entity));
- ASSERT_EQ(registry.get<double>(entity), .0);
- registry.erase<int>(entity);
- ASSERT_FALSE((registry.any_of<int, double>(entity)));
- registry.on_construct<int>().disconnect<emplace_or_replace>();
- registry.on_destroy<int>().disconnect<remove>();
- registry.emplace<int>(entity);
- ASSERT_TRUE((registry.any_of<int, double>(entity)));
- ASSERT_FALSE(registry.all_of<double>(entity));
- }
- TEST(Registry, View) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- auto iview = std::as_const(registry).view<int>();
- auto cview = std::as_const(registry).view<char>();
- ASSERT_FALSE(iview);
- ASSERT_FALSE(cview);
- ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
- auto mview = registry.view<int, char>();
- auto fview = registry.view<int>(entt::exclude<char>);
- ASSERT_TRUE(mview);
- ASSERT_TRUE(fview);
- ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_NE(std::as_const(registry).storage<char>(), nullptr);
- iview = std::as_const(registry).view<int>();
- cview = std::as_const(registry).view<char>();
- ASSERT_TRUE(iview);
- ASSERT_TRUE(cview);
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u], 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<int>(entity[1u], 0);
- registry.emplace<int>(entity[2u], 0);
- registry.emplace<char>(entity[2u], 'c');
- ASSERT_EQ(iview.size(), 3u);
- ASSERT_EQ(cview.size(), 2u);
- ASSERT_EQ(mview.size_hint(), 3u);
- ASSERT_EQ(fview.size_hint(), 3u);
- mview.refresh();
- ASSERT_EQ(mview.size_hint(), 2u);
- ASSERT_EQ(fview.size_hint(), 3u);
- ASSERT_NE(mview.begin(), mview.end());
- ASSERT_NE(fview.begin(), fview.end());
- ASSERT_EQ(std::distance(mview.begin(), mview.end()), 2);
- ASSERT_EQ(std::distance(fview.begin(), fview.end()), 1);
- mview.each([&entity, first = true](auto entt, auto &&...) mutable {
- ASSERT_EQ(entt, first ? entity[2u] : entity[0u]);
- first = false;
- });
- fview.each([&entity](auto entt, auto &&...) {
- ASSERT_EQ(entt, entity[1u]);
- });
- }
- TEST(Registry, ExcludeOnlyView) {
- entt::registry registry{};
- std::array<entt::entity, 4u> entity{};
- auto view = registry.view<entt::entity>(entt::exclude<int>);
- registry.create(entity.begin(), entity.end());
- registry.emplace<int>(entity[0u], 0);
- registry.emplace<int>(entity[2u], 0);
- registry.emplace<int>(entity[3u], 0);
- registry.destroy(entity[3u]);
- ASSERT_EQ(view.size_hint(), 3u);
- ASSERT_NE(view.begin(), view.end());
- ASSERT_EQ(std::distance(view.begin(), view.end()), 1);
- ASSERT_EQ(*view.begin(), entity[1u]);
- for(auto [entt]: view.each()) {
- ASSERT_EQ(entt, entity[1u]);
- }
- view.each([&entity](auto entt) {
- ASSERT_EQ(entt, entity[1u]);
- });
- }
- TEST(Registry, CleanViewAfterRemoveAndClear) {
- entt::registry registry{};
- auto view = registry.view<int, char>();
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- registry.erase<char>(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- registry.emplace<char>(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- registry.clear<int>();
- ASSERT_EQ(view.size_hint(), 0u);
- registry.emplace<int>(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- registry.clear();
- ASSERT_EQ(view.size_hint(), 0u);
- }
- TEST(Registry, NonOwningGroupInitOnFirstUse) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end(), 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<char>(entity[2u], 'c');
- std::size_t cnt{};
- auto group = registry.group(entt::get<int, char>);
- group.each([&cnt](auto...) { ++cnt; });
- ASSERT_FALSE((registry.owned<int, char>()));
- ASSERT_EQ(cnt, 2u);
- }
- TEST(Registry, NonOwningGroupInitOnEmplace) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- auto group = registry.group(entt::get<int, char>);
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end(), 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<char>(entity[2u], 'c');
- std::size_t cnt{};
- group.each([&cnt](auto...) { ++cnt; });
- ASSERT_FALSE((registry.owned<int, char>()));
- ASSERT_EQ(cnt, 2u);
- }
- TEST(Registry, FullOwningGroupInitOnFirstUse) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end(), 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<char>(entity[2u], 'c');
- std::size_t cnt{};
- auto group = registry.group<int, char>();
- group.each([&cnt](auto...) { ++cnt; });
- ASSERT_TRUE(registry.owned<int>());
- ASSERT_TRUE(registry.owned<char>());
- ASSERT_FALSE(registry.owned<double>());
- ASSERT_EQ(cnt, 2u);
- }
- TEST(Registry, FullOwningGroupInitOnEmplace) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- auto group = registry.group<int, char>();
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end(), 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<char>(entity[2u], 'c');
- std::size_t cnt{};
- group.each([&cnt](auto...) { ++cnt; });
- ASSERT_TRUE(registry.owned<int>());
- ASSERT_TRUE(registry.owned<char>());
- ASSERT_FALSE(registry.owned<double>());
- ASSERT_EQ(cnt, 2u);
- }
- TEST(Registry, PartialOwningGroupInitOnFirstUse) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end(), 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<char>(entity[2u], 'c');
- std::size_t cnt{};
- auto group = registry.group<int>(entt::get<char>);
- group.each([&cnt](auto...) { ++cnt; });
- ASSERT_TRUE((registry.owned<int, char>()));
- ASSERT_TRUE(registry.owned<int>());
- ASSERT_FALSE(registry.owned<char>());
- ASSERT_EQ(cnt, 2u);
- }
- TEST(Registry, PartialOwningGroupInitOnEmplace) {
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- auto group = registry.group<int>(entt::get<char>);
- registry.create(entity.begin(), entity.end());
- registry.insert<int>(entity.begin(), entity.end(), 0);
- registry.emplace<char>(entity[0u], 'c');
- registry.emplace<char>(entity[2u], 'c');
- std::size_t cnt{};
- group.each([&cnt](auto...) { ++cnt; });
- ASSERT_TRUE((registry.owned<int, char>()));
- ASSERT_TRUE(registry.owned<int>());
- ASSERT_FALSE(registry.owned<char>());
- ASSERT_EQ(cnt, 2u);
- }
- TEST(Registry, CleanNonOwningGroupViewAfterRemoveAndClear) {
- entt::registry registry{};
- auto group = registry.group(entt::get<int, char>);
- const auto entity = registry.create();
- registry.emplace<int>(entity, 0);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- registry.erase<char>(entity);
- ASSERT_EQ(group.size(), 0u);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- registry.clear<int>();
- ASSERT_EQ(group.size(), 0u);
- registry.emplace<int>(entity, 0);
- ASSERT_EQ(group.size(), 1u);
- registry.clear();
- ASSERT_EQ(group.size(), 0u);
- }
- TEST(Registry, CleanFullOwningGroupViewAfterRemoveAndClear) {
- entt::registry registry{};
- auto group = registry.group<int, char>();
- const auto entity = registry.create();
- registry.emplace<int>(entity, 0);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- registry.erase<char>(entity);
- ASSERT_EQ(group.size(), 0u);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- registry.clear<int>();
- ASSERT_EQ(group.size(), 0u);
- registry.emplace<int>(entity, 0);
- ASSERT_EQ(group.size(), 1u);
- registry.clear();
- ASSERT_EQ(group.size(), 0u);
- }
- TEST(Registry, CleanPartialOwningGroupViewAfterRemoveAndClear) {
- entt::registry registry{};
- auto group = registry.group<int>(entt::get<char>);
- const auto entity = registry.create();
- registry.emplace<int>(entity, 0);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- registry.erase<char>(entity);
- ASSERT_EQ(group.size(), 0u);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- registry.clear<int>();
- ASSERT_EQ(group.size(), 0u);
- registry.emplace<int>(entity, 0);
- ASSERT_EQ(group.size(), 1u);
- registry.clear();
- ASSERT_EQ(group.size(), 0u);
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, NestedGroups) {
- entt::registry registry{};
- registry.group<int, double>(entt::get<char>);
- ASSERT_DEATH(registry.group<int>(entt::get<char>), "");
- ASSERT_DEATH(registry.group<int>(entt::get<char, double>), "");
- ASSERT_DEATH(registry.group<int>(entt::get<char>, entt::exclude<double>), "");
- ASSERT_DEATH((registry.group<int, double>()), "");
- }
- ENTT_DEBUG_TEST(RegistryDeathTest, ConflictingGroups) {
- entt::registry registry{};
- registry.group<char>(entt::get<int>, entt::exclude<double>);
- ASSERT_DEATH(registry.group<char>(entt::get<float>, entt::exclude<double>), "");
- }
- TEST(Registry, GroupIfExists) {
- entt::registry registry{};
- const auto entity = registry.create();
- auto group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
- ASSERT_FALSE(group);
- ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
- ASSERT_EQ(std::as_const(registry).storage<double>(), nullptr);
- registry.emplace<int>(entity);
- group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
- ASSERT_FALSE(group);
- registry.emplace<char>(entity);
- group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
- ASSERT_FALSE(group);
- registry.emplace<double>(entity);
- group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
- ASSERT_FALSE(group);
- registry.group<int>(entt::get<char>, entt::exclude<double>);
- group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
- ASSERT_TRUE(group);
- }
- TEST(Registry, SortSingle) {
- entt::registry registry{};
- int val = 0;
- registry.emplace<int>(registry.create(), val++);
- registry.emplace<int>(registry.create(), val++);
- registry.emplace<int>(registry.create(), val++);
- for(auto entity: registry.view<int>()) {
- ASSERT_EQ(registry.get<int>(entity), --val);
- }
- registry.sort<int>(std::less{});
- for(auto entity: registry.view<int>()) {
- ASSERT_EQ(registry.get<int>(entity), val++);
- }
- }
- TEST(Registry, SortMulti) {
- entt::registry registry{};
- unsigned int uval = 0u;
- int ival = 0;
- for(auto i = 0; i < 3; ++i) {
- const auto entity = registry.create();
- registry.emplace<unsigned int>(entity, uval++);
- registry.emplace<int>(entity, ival++);
- }
- for(auto entity: registry.view<unsigned int>()) {
- ASSERT_EQ(registry.get<unsigned int>(entity), --uval);
- }
- for(auto entity: registry.view<int>()) {
- ASSERT_EQ(registry.get<int>(entity), --ival);
- }
- registry.sort<unsigned int>(std::less{});
- registry.sort<int, unsigned int>();
- for(auto entity: registry.view<unsigned int>()) {
- ASSERT_EQ(registry.get<unsigned int>(entity), uval++);
- }
- for(auto entity: registry.view<int>()) {
- ASSERT_EQ(registry.get<int>(entity), ival++);
- }
- }
- TEST(Registry, SortEmpty) {
- entt::registry registry{};
- registry.emplace<test::empty>(registry.create());
- registry.emplace<test::empty>(registry.create());
- registry.emplace<test::empty>(registry.create());
- ASSERT_LT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
- ASSERT_LT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
- registry.sort<test::empty>(std::less{});
- ASSERT_GT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
- ASSERT_GT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
- }
- TEST(Registry, Context) {
- entt::registry registry{};
- auto &ctx = registry.ctx();
- const auto &cctx = std::as_const(registry).ctx();
- ASSERT_FALSE(ctx.contains<char>());
- ASSERT_FALSE(cctx.contains<const int>());
- ASSERT_EQ(ctx.find<char>(), nullptr);
- ASSERT_EQ(cctx.find<const int>(), nullptr);
- ctx.emplace<char>();
- ctx.emplace<int>();
- ASSERT_TRUE(ctx.contains<char>());
- ASSERT_TRUE(cctx.contains<int>());
- ASSERT_NE(ctx.find<const char>(), nullptr);
- ASSERT_NE(cctx.find<const int>(), nullptr);
- ASSERT_FALSE(ctx.erase<double>());
- ASSERT_TRUE(ctx.erase<int>());
- ASSERT_TRUE(ctx.contains<const char>());
- ASSERT_FALSE(cctx.contains<const int>());
- ASSERT_NE(ctx.find<char>(), nullptr);
- ASSERT_EQ(cctx.find<int>(), nullptr);
- ASSERT_FALSE(ctx.erase<int>());
- ASSERT_TRUE(ctx.erase<char>());
- ctx.emplace<char>('c');
- ctx.emplace<int>(1);
- ASSERT_EQ(ctx.emplace<char>('a'), 'c');
- ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
- ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
- ASSERT_EQ(ctx.get<char>(), 'c');
- ASSERT_EQ(ctx.emplace<const int>(0), 1);
- ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
- ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
- ASSERT_EQ(ctx.get<int>(), 1);
- ASSERT_EQ(ctx.find<double>(), nullptr);
- ASSERT_EQ(cctx.find<double>(), nullptr);
- ASSERT_EQ(ctx.insert_or_assign<char>('a'), 'a');
- ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
- ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
- ASSERT_EQ(ctx.get<const char>(), 'a');
- ASSERT_EQ(ctx.insert_or_assign<const int>(0), 0);
- ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
- ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
- ASSERT_EQ(ctx.get<int>(), 0);
- }
- TEST(Registry, ContextHint) {
- using namespace entt::literals;
- entt::registry registry{};
- auto &ctx = registry.ctx();
- const auto &cctx = std::as_const(registry).ctx();
- ctx.emplace<int>(1);
- ctx.emplace_as<int>("other"_hs, 3);
- ASSERT_TRUE(ctx.contains<int>());
- ASSERT_TRUE(cctx.contains<const int>("other"_hs));
- ASSERT_FALSE(ctx.contains<char>("other"_hs));
- ASSERT_NE(cctx.find<const int>(), nullptr);
- ASSERT_NE(ctx.find<int>("other"_hs), nullptr);
- ASSERT_EQ(cctx.find<const char>("other"_hs), nullptr);
- ASSERT_EQ(ctx.get<int>(), 1);
- ASSERT_EQ(cctx.get<const int>("other"_hs), 3);
- ctx.insert_or_assign(3);
- ctx.insert_or_assign("other"_hs, 1);
- ASSERT_EQ(ctx.get<const int>(), 3);
- ASSERT_EQ(cctx.get<int>("other"_hs), 1);
- ASSERT_FALSE(ctx.erase<char>("other"_hs));
- ASSERT_TRUE(ctx.erase<int>());
- ASSERT_TRUE(cctx.contains<int>("other"_hs));
- ASSERT_EQ(ctx.get<int>("other"_hs), 1);
- ASSERT_TRUE(ctx.erase<int>("other"_hs));
- ASSERT_FALSE(cctx.contains<int>("other"_hs));
- ASSERT_EQ(ctx.find<int>("other"_hs), nullptr);
- }
- TEST(Registry, ContextAsRef) {
- entt::registry registry{};
- int value{3};
- registry.ctx().emplace<int &>(value);
- ASSERT_NE(registry.ctx().find<int>(), nullptr);
- ASSERT_NE(registry.ctx().find<const int>(), nullptr);
- ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
- ASSERT_EQ(registry.ctx().get<const int>(), value);
- ASSERT_EQ(registry.ctx().get<int>(), value);
- registry.ctx().get<int>() = 2;
- ASSERT_EQ(value, 2);
- ASSERT_EQ(registry.ctx().get<int>(), value);
- value = 1;
- ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
- }
- TEST(Registry, ContextAsConstRef) {
- entt::registry registry{};
- int value{3};
- registry.ctx().emplace<const int &>(value);
- ASSERT_EQ(registry.ctx().find<int>(), nullptr);
- ASSERT_NE(registry.ctx().find<const int>(), nullptr);
- ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
- ASSERT_EQ(registry.ctx().get<const int>(), value);
- value = 1;
- ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
- }
- TEST(Registry, ContextPoolMemberDestructionOrder) {
- auto registry = std::make_unique<entt::registry>();
- const auto entity = registry->create();
- bool ctx_check = false;
- registry->ctx().emplace<typename destruction_order::ctx_check_type>();
- registry->emplace<destruction_order>(entity, *registry, ctx_check);
- registry.reset();
- ASSERT_TRUE(ctx_check);
- }
- TEST(Registry, Constness) {
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<int>({})), int &>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<test::empty>({})), void>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<>({})), std::tuple<>>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int>({})), int &>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int, const char>({})), std::tuple<int &, const char &>>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<>({})), std::tuple<>>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int>({})), int *>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int, const char>({})), std::tuple<int *, const char *>>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<int>()), int &>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<const char>()), const char &>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<int>()), int *>();
- testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<const char>()), const char *>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<>({})), std::tuple<>>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int>({})), const int &>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int, const char>({})), std::tuple<const int &, const char &>>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<>({})), std::tuple<>>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int>({})), const int *>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int, const char>({})), std::tuple<const int *, const char *>>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<int>()), const int &>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<const char>()), const char &>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<int>()), const int *>();
- testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<const char>()), const char *>();
- }
- TEST(Registry, AssignEntities) {
- using traits_type = entt::entt_traits<entt::entity>;
- entt::registry registry{};
- std::array<entt::entity, 3u> entity{};
- registry.create(entity.begin(), entity.end());
- registry.destroy(entity[1]);
- registry.destroy(entity[2]);
- entt::registry other{};
- auto &src = registry.storage<entt::entity>();
- auto &dst = other.storage<entt::entity>();
- dst.push(src.rbegin(), src.rend());
- dst.free_list(src.free_list());
- ASSERT_EQ(registry.storage<entt::entity>().size(), other.storage<entt::entity>().size());
- ASSERT_TRUE(other.valid(entity[0]));
- ASSERT_FALSE(other.valid(entity[1]));
- ASSERT_FALSE(other.valid(entity[2]));
- ASSERT_EQ(registry.create(), other.create());
- ASSERT_EQ(traits_type::to_entity(other.create()), traits_type::to_integral(entity[1]));
- }
- TEST(Registry, ScramblingPoolsIsAllowed) {
- entt::registry registry{};
- registry.on_destroy<int>().connect<&listener::sort<int>>();
- for(std::size_t i{}; i < 2u; ++i) {
- const auto entity = registry.create();
- registry.emplace<int>(entity, static_cast<int>(i));
- }
- registry.destroy(registry.view<int>().back());
- // thanks to @andranik3949 for pointing out this missing test
- registry.view<const int>().each([](const auto entity, const auto &value) {
- ASSERT_EQ(static_cast<int>(entt::to_integral(entity)), value);
- });
- }
- TEST(Registry, AssureMixinLoop) {
- entt::registry registry{};
- const auto entity = registry.create();
- ASSERT_EQ(std::as_const(registry).storage<test::assure_loop>(), nullptr);
- ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
- registry.emplace<test::assure_loop>(entity);
- ASSERT_NE(std::as_const(registry).storage<test::assure_loop>(), nullptr);
- ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
- ASSERT_TRUE(registry.all_of<test::assure_loop>(entity));
- ASSERT_FALSE(registry.all_of<int>(entity));
- }
- TEST(Registry, VoidType) {
- using namespace entt::literals;
- entt::registry registry{};
- const auto entity = registry.create();
- auto &storage = registry.storage<void>("custom"_hs);
- storage.emplace(entity);
- ASSERT_TRUE(registry.storage<void>().empty());
- ASSERT_FALSE(registry.storage<void>("custom"_hs).empty());
- ASSERT_TRUE(registry.storage<void>("custom"_hs).contains(entity));
- ASSERT_EQ(registry.storage<void>().type(), entt::type_id<void>());
- ASSERT_EQ(registry.storage<void>("custom"_hs).type(), entt::type_id<void>());
- }
- TEST(Registry, NoEtoType) {
- entt::registry registry{};
- const auto entity = registry.create();
- registry.emplace<no_eto_type>(entity);
- registry.emplace<int>(entity, 1);
- ASSERT_NE(registry.storage<no_eto_type>().raw(), nullptr);
- ASSERT_NE(registry.try_get<no_eto_type>(entity), nullptr);
- ASSERT_EQ(registry.view<no_eto_type>().get(entity), std::as_const(registry).view<const no_eto_type>().get(entity));
- auto view = registry.view<no_eto_type, int>();
- auto cview = std::as_const(registry).view<const no_eto_type, const int>();
- ASSERT_EQ((std::get<0>(view.get<no_eto_type, int>(entity))), (std::get<0>(cview.get<const no_eto_type, const int>(entity))));
- }
|