| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507 |
- #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/registry.hpp>
- #include <entt/entity/view.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(std::as_const(registry).storage<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_NE(registry.storage("other"_hs), nullptr);
- ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
- ASSERT_EQ(®istry.storage<test::empty>("other"_hs), &storage);
- ASSERT_NE(std::as_const(registry).storage<test::empty>(), &storage);
- 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{}, post{};
- 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, 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 = registry.get_or_emplace<int>(entity, 3);
- // get_or_emplace must work for empty types
- static_cast<void>(registry.get_or_emplace<test::empty>(entity));
- ASSERT_TRUE((registry.all_of<int, test::empty>(entity)));
- ASSERT_EQ(registry.get<int>(entity), value);
- ASSERT_EQ(registry.get<int>(entity), 3);
- }
- 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(), 4u);
- 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))));
- }
|