| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468 |
- #include <algorithm>
- #include <cstddef>
- #include <functional>
- #include <iterator>
- #include <tuple>
- #include <type_traits>
- #include <utility>
- #include <gtest/gtest.h>
- #include <entt/entity/group.hpp>
- #include <entt/entity/registry.hpp>
- struct empty_type {};
- struct boxed_int {
- int value;
- };
- bool operator==(const boxed_int &lhs, const boxed_int &rhs) {
- return lhs.value == rhs.value;
- }
- TEST(NonOwningGroup, Functionalities) {
- entt::registry registry;
- auto group = registry.group(entt::get<int, char>);
- auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int, const char>);
- ASSERT_TRUE(group.empty());
- const auto e0 = registry.create();
- registry.emplace<char>(e0, '1');
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 42);
- registry.emplace<char>(e1, '2');
- ASSERT_FALSE(group.empty());
- ASSERT_NO_FATAL_FAILURE(group.begin()++);
- ASSERT_NO_FATAL_FAILURE(++cgroup.begin());
- ASSERT_NO_FATAL_FAILURE([](auto it) { return it++; }(group.rbegin()));
- ASSERT_NO_FATAL_FAILURE([](auto it) { return ++it; }(cgroup.rbegin()));
- ASSERT_NE(group.begin(), group.end());
- ASSERT_NE(cgroup.begin(), cgroup.end());
- ASSERT_NE(group.rbegin(), group.rend());
- ASSERT_NE(cgroup.rbegin(), cgroup.rend());
- ASSERT_EQ(group.size(), 1u);
- registry.emplace<int>(e0);
- ASSERT_EQ(group.size(), 2u);
- registry.erase<int>(e0);
- ASSERT_EQ(group.size(), 1u);
- for(auto entity: group) {
- ASSERT_EQ(std::get<0>(cgroup.get<const int, const char>(entity)), 42);
- ASSERT_EQ(std::get<1>(group.get<int, char>(entity)), '2');
- ASSERT_EQ(cgroup.get<const char>(entity), '2');
- }
- ASSERT_EQ(group.handle().data()[0u], e1);
- registry.erase<char>(e0);
- registry.erase<char>(e1);
- ASSERT_EQ(group.begin(), group.end());
- ASSERT_EQ(cgroup.begin(), cgroup.end());
- ASSERT_EQ(group.rbegin(), group.rend());
- ASSERT_EQ(cgroup.rbegin(), cgroup.rend());
- ASSERT_TRUE(group.empty());
- ASSERT_NE(group.capacity(), 0u);
- group.shrink_to_fit();
- ASSERT_EQ(group.capacity(), 0u);
- decltype(group) invalid{};
- ASSERT_TRUE(group);
- ASSERT_TRUE(cgroup);
- ASSERT_FALSE(invalid);
- }
- TEST(NonOwningGroup, Handle) {
- entt::registry registry;
- const auto entity = registry.create();
- auto group = registry.group(entt::get<int, char>);
- auto &&handle = group.handle();
- ASSERT_TRUE(handle.empty());
- ASSERT_FALSE(handle.contains(entity));
- ASSERT_EQ(&handle, &group.handle());
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- ASSERT_FALSE(handle.empty());
- ASSERT_TRUE(handle.contains(entity));
- ASSERT_EQ(&handle, &group.handle());
- }
- TEST(NonOwningGroup, Invalid) {
- entt::registry registry{};
- auto group = std::as_const(registry).group_if_exists(entt::get<const empty_type, const int>);
- const auto entity = registry.create();
- registry.emplace<empty_type>(entity);
- registry.emplace<int>(entity);
- ASSERT_FALSE(group);
- ASSERT_TRUE(group.empty());
- ASSERT_EQ(group.size(), 0u);
- ASSERT_EQ(group.capacity(), 0u);
- ASSERT_NO_FATAL_FAILURE(group.shrink_to_fit());
- ASSERT_EQ(group.begin(), group.end());
- ASSERT_EQ(group.rbegin(), group.rend());
- ASSERT_FALSE(group.contains(entity));
- ASSERT_EQ(group.find(entity), group.end());
- ASSERT_EQ(group.front(), entt::entity{entt::null});
- ASSERT_EQ(group.back(), entt::entity{entt::null});
- }
- TEST(NonOwningGroup, ElementAccess) {
- entt::registry registry;
- auto group = registry.group(entt::get<int, char>);
- auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int, const char>);
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- for(auto i = 0u; i < group.size(); ++i) {
- ASSERT_EQ(group[i], i ? e0 : e1);
- ASSERT_EQ(cgroup[i], i ? e0 : e1);
- }
- }
- TEST(NonOwningGroup, Contains) {
- entt::registry registry;
- auto group = registry.group(entt::get<int, char>);
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- registry.destroy(e0);
- ASSERT_FALSE(group.contains(e0));
- ASSERT_TRUE(group.contains(e1));
- }
- TEST(NonOwningGroup, Empty) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<double>(e0);
- registry.emplace<int>(e0);
- registry.emplace<float>(e0);
- const auto e1 = registry.create();
- registry.emplace<char>(e1);
- registry.emplace<float>(e1);
- ASSERT_TRUE(registry.group(entt::get<char, int, float>).empty());
- ASSERT_TRUE(registry.group(entt::get<double, char, int, float>).empty());
- }
- TEST(NonOwningGroup, Each) {
- entt::registry registry;
- entt::entity entity[2]{registry.create(), registry.create()};
- auto group = registry.group(entt::get<int, char>);
- auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int, const char>);
- registry.emplace<int>(entity[0u], 0);
- registry.emplace<char>(entity[0u], 0);
- registry.emplace<int>(entity[1u], 1);
- registry.emplace<char>(entity[1u], 1);
- auto iterable = group.each();
- auto citerable = cgroup.each();
- ASSERT_NE(citerable.begin(), citerable.end());
- ASSERT_NO_THROW(iterable.begin()->operator=(*iterable.begin()));
- ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
- auto it = iterable.begin();
- ASSERT_EQ((it++, ++it), iterable.end());
- group.each([expected = 1u](auto entt, int &ivalue, char &cvalue) mutable {
- ASSERT_EQ(entt::to_integral(entt), expected);
- ASSERT_EQ(ivalue, expected);
- ASSERT_EQ(cvalue, expected);
- --expected;
- });
- cgroup.each([expected = 1u](const int &ivalue, const char &cvalue) mutable {
- ASSERT_EQ(ivalue, expected);
- ASSERT_EQ(cvalue, expected);
- --expected;
- });
- ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
- ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
- static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
- static_assert(std::is_same_v<decltype(std::get<2>(*citerable.begin())), const char &>);
- // do not use iterable, make sure an iterable group works when created from a temporary
- for(auto [entt, ivalue, cvalue]: registry.group(entt::get<int, char>).each()) {
- ASSERT_EQ(entt::to_integral(entt), ivalue);
- ASSERT_EQ(entt::to_integral(entt), cvalue);
- }
- }
- TEST(NonOwningGroup, Sort) {
- entt::registry registry;
- auto group = registry.group(entt::get<const int, unsigned int>);
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- const auto e2 = registry.create();
- const auto e3 = registry.create();
- registry.emplace<unsigned int>(e0, 0u);
- registry.emplace<unsigned int>(e1, 1u);
- registry.emplace<unsigned int>(e2, 2u);
- registry.emplace<unsigned int>(e3, 3u);
- registry.emplace<int>(e0, 0);
- registry.emplace<int>(e1, 1);
- registry.emplace<int>(e2, 2);
- ASSERT_EQ(group.handle().data()[0u], e0);
- ASSERT_EQ(group.handle().data()[1u], e1);
- ASSERT_EQ(group.handle().data()[2u], e2);
- group.sort([](const entt::entity lhs, const entt::entity rhs) {
- return entt::to_integral(lhs) < entt::to_integral(rhs);
- });
- ASSERT_EQ(group.handle().data()[0u], e2);
- ASSERT_EQ(group.handle().data()[1u], e1);
- ASSERT_EQ(group.handle().data()[2u], e0);
- ASSERT_EQ((group.get<const int, unsigned int>(e0)), (std::make_tuple(0, 0u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e1)), (std::make_tuple(1, 1u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e2)), (std::make_tuple(2, 2u)));
- ASSERT_FALSE(group.contains(e3));
- group.sort<const int>([](const int lhs, const int rhs) {
- return lhs > rhs;
- });
- ASSERT_EQ(group.handle().data()[0u], e0);
- ASSERT_EQ(group.handle().data()[1u], e1);
- ASSERT_EQ(group.handle().data()[2u], e2);
- ASSERT_EQ((group.get<const int, unsigned int>(e0)), (std::make_tuple(0, 0u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e1)), (std::make_tuple(1, 1u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e2)), (std::make_tuple(2, 2u)));
- ASSERT_FALSE(group.contains(e3));
- group.sort<const int, unsigned int>([](const auto lhs, const auto rhs) {
- static_assert(std::is_same_v<decltype(std::get<0>(lhs)), const int &>);
- static_assert(std::is_same_v<decltype(std::get<1>(rhs)), unsigned int &>);
- return std::get<0>(lhs) < std::get<0>(rhs);
- });
- ASSERT_EQ(group.handle().data()[0u], e2);
- ASSERT_EQ(group.handle().data()[1u], e1);
- ASSERT_EQ(group.handle().data()[2u], e0);
- ASSERT_EQ((group.get<const int, unsigned int>(e0)), (std::make_tuple(0, 0u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e1)), (std::make_tuple(1, 1u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e2)), (std::make_tuple(2, 2u)));
- ASSERT_FALSE(group.contains(e3));
- }
- TEST(NonOwningGroup, SortAsAPool) {
- entt::registry registry;
- auto group = registry.group(entt::get<const int, unsigned int>);
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- const auto e2 = registry.create();
- const auto e3 = registry.create();
- auto uval = 0u;
- auto ival = 0;
- registry.emplace<unsigned int>(e0, uval++);
- registry.emplace<unsigned int>(e1, uval++);
- registry.emplace<unsigned int>(e2, uval++);
- registry.emplace<unsigned int>(e3, uval + 1);
- registry.emplace<int>(e0, ival++);
- registry.emplace<int>(e1, ival++);
- registry.emplace<int>(e2, ival++);
- for(auto entity: group) {
- ASSERT_EQ(group.get<unsigned int>(entity), --uval);
- ASSERT_EQ(group.get<const int>(entity), --ival);
- }
- registry.sort<unsigned int>(std::less<unsigned int>{});
- group.sort<unsigned int>();
- ASSERT_EQ((group.get<const int, unsigned int>(e0)), (std::make_tuple(0, 0u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e1)), (std::make_tuple(1, 1u)));
- ASSERT_EQ((group.get<const int, unsigned int>(e2)), (std::make_tuple(2, 2u)));
- ASSERT_FALSE(group.contains(e3));
- for(auto entity: group) {
- ASSERT_EQ(group.get<unsigned int>(entity), uval++);
- ASSERT_EQ(group.get<const int>(entity), ival++);
- }
- }
- TEST(NonOwningGroup, IndexRebuiltOnDestroy) {
- entt::registry registry;
- auto group = registry.group(entt::get<int, unsigned int>);
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- registry.emplace<unsigned int>(e0, 0u);
- registry.emplace<unsigned int>(e1, 1u);
- registry.emplace<int>(e0, 0);
- registry.emplace<int>(e1, 1);
- registry.destroy(e0);
- registry.emplace<int>(registry.create(), 42);
- ASSERT_EQ(group.size(), 1u);
- ASSERT_EQ(group[{}], e1);
- ASSERT_EQ(group.get<int>(e1), 1);
- ASSERT_EQ(group.get<unsigned int>(e1), 1u);
- group.each([e1](auto entity, auto ivalue, auto uivalue) {
- ASSERT_EQ(entity, e1);
- ASSERT_EQ(ivalue, 1);
- ASSERT_EQ(uivalue, 1u);
- });
- for(auto &&curr: group.each()) {
- ASSERT_EQ(std::get<0>(curr), e1);
- ASSERT_EQ(std::get<1>(curr), 1);
- ASSERT_EQ(std::get<2>(curr), 1u);
- }
- }
- TEST(NonOwningGroup, ConstNonConstAndAllInBetween) {
- entt::registry registry;
- auto group = registry.group(entt::get<int, empty_type, const char>);
- ASSERT_EQ(group.size(), 0u);
- const auto entity = registry.create();
- registry.emplace<int>(entity, 0);
- registry.emplace<empty_type>(entity);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(group.size(), 1u);
- static_assert(std::is_same_v<decltype(group.get<int>({})), int &>);
- static_assert(std::is_same_v<decltype(group.get<const char>({})), const char &>);
- static_assert(std::is_same_v<decltype(group.get<int, const char>({})), std::tuple<int &, const char &>>);
- static_assert(std::is_same_v<decltype(group.get({})), std::tuple<int &, const char &>>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).group_if_exists(entt::get<int, char>)), decltype(std::as_const(registry).group_if_exists(entt::get<const int, const char>))>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).group_if_exists(entt::get<const int, char>)), decltype(std::as_const(registry).group_if_exists(entt::get<const int, const char>))>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).group_if_exists(entt::get<int, const char>)), decltype(std::as_const(registry).group_if_exists(entt::get<const int, const char>))>);
- group.each([](auto &&i, auto &&c) {
- static_assert(std::is_same_v<decltype(i), int &>);
- static_assert(std::is_same_v<decltype(c), const char &>);
- });
- for(auto [entt, iv, cv]: group.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), const char &>);
- }
- }
- TEST(NonOwningGroup, Find) {
- entt::registry registry;
- auto group = registry.group(entt::get<int, const char>);
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- const auto e2 = registry.create();
- registry.emplace<int>(e2);
- registry.emplace<char>(e2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3);
- registry.emplace<char>(e3);
- registry.erase<int>(e1);
- ASSERT_NE(group.find(e0), group.end());
- ASSERT_EQ(group.find(e1), group.end());
- ASSERT_NE(group.find(e2), group.end());
- ASSERT_NE(group.find(e3), group.end());
- auto it = group.find(e2);
- ASSERT_EQ(*it, e2);
- ASSERT_EQ(*(++it), e3);
- ASSERT_EQ(*(++it), e0);
- ASSERT_EQ(++it, group.end());
- ASSERT_EQ(++group.find(e0), group.end());
- const auto e4 = registry.create();
- registry.destroy(e4);
- const auto e5 = registry.create();
- registry.emplace<int>(e5);
- registry.emplace<char>(e5);
- ASSERT_NE(group.find(e5), group.end());
- ASSERT_EQ(group.find(e4), group.end());
- }
- TEST(NonOwningGroup, ExcludedComponents) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<int>(e0, 0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 1);
- registry.emplace<char>(e1);
- const auto group = registry.group(entt::get<int>, entt::exclude<char>);
- const auto e2 = registry.create();
- registry.emplace<int>(e2, 2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3, 3);
- registry.emplace<char>(e3);
- for(const auto entity: group) {
- ASSERT_TRUE(entity == e0 || entity == e2);
- if(entity == e0) {
- ASSERT_EQ(group.get<int>(e0), 0);
- } else if(entity == e2) {
- ASSERT_EQ(group.get<int>(e2), 2);
- }
- }
- registry.emplace<char>(e0);
- registry.emplace<char>(e2);
- ASSERT_TRUE(group.empty());
- registry.erase<char>(e1);
- registry.erase<char>(e3);
- for(const auto entity: group) {
- ASSERT_TRUE(entity == e1 || entity == e3);
- if(entity == e1) {
- ASSERT_EQ(group.get<int>(e1), 1);
- } else if(entity == e3) {
- ASSERT_EQ(group.get<int>(e3), 3);
- }
- }
- }
- TEST(NonOwningGroup, EmptyAndNonEmptyTypes) {
- entt::registry registry;
- const auto group = registry.group(entt::get<int, empty_type>);
- const auto e0 = registry.create();
- registry.emplace<empty_type>(e0);
- registry.emplace<int>(e0);
- const auto e1 = registry.create();
- registry.emplace<empty_type>(e1);
- registry.emplace<int>(e1);
- registry.emplace<int>(registry.create());
- for(const auto entity: group) {
- ASSERT_TRUE(entity == e0 || entity == e1);
- }
- group.each([e0, e1](const auto entity, const int &) {
- ASSERT_TRUE(entity == e0 || entity == e1);
- });
- for(auto [entt, iv]: group.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- ASSERT_TRUE(entt == e0 || entt == e1);
- }
- ASSERT_EQ(group.size(), 2u);
- }
- TEST(NonOwningGroup, TrackEntitiesOnComponentDestruction) {
- entt::registry registry;
- const auto group = registry.group(entt::get<int>, entt::exclude<char>);
- const auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int>, entt::exclude<char>);
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- ASSERT_TRUE(group.empty());
- ASSERT_TRUE(cgroup.empty());
- registry.erase<char>(entity);
- ASSERT_FALSE(group.empty());
- ASSERT_FALSE(cgroup.empty());
- }
- TEST(NonOwningGroup, EmptyTypes) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- registry.emplace<empty_type>(entity);
- registry.group(entt::get<int, char, empty_type>).each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.group(entt::get<int, char, empty_type>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.group(entt::get<int, empty_type, char>).each([check = true](int, char) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, iv, cv]: registry.group(entt::get<int, empty_type, char>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.group(entt::get<empty_type, int, char>).each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.group(entt::get<empty_type, int, char>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- auto iterable = registry.group(entt::get<int, char, double>).each();
- ASSERT_EQ(iterable.begin(), iterable.end());
- }
- TEST(NonOwningGroup, FrontBack) {
- entt::registry registry;
- auto group = registry.group<>(entt::get<const int, const char>);
- ASSERT_EQ(group.front(), static_cast<entt::entity>(entt::null));
- ASSERT_EQ(group.back(), static_cast<entt::entity>(entt::null));
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- const auto entity = registry.create();
- registry.emplace<char>(entity);
- ASSERT_EQ(group.front(), e1);
- ASSERT_EQ(group.back(), e0);
- }
- TEST(NonOwningGroup, SignalRace) {
- entt::registry registry;
- registry.on_construct<double>().connect<&entt::registry::emplace_or_replace<int>>();
- const auto group = registry.group(entt::get<int, double>);
- auto entity = registry.create();
- registry.emplace<double>(entity);
- ASSERT_EQ(group.size(), 1u);
- }
- TEST(NonOwningGroup, ExtendedGet) {
- using type = decltype(std::declval<entt::registry>().group(entt::get<int, empty_type, char>).get({}));
- static_assert(std::tuple_size_v<type> == 2u);
- static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
- static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity, 42);
- registry.emplace<char>(entity, 'c');
- const auto tup = registry.group(entt::get<int, char>).get(entity);
- ASSERT_EQ(std::get<0>(tup), 42);
- ASSERT_EQ(std::get<1>(tup), 'c');
- }
- TEST(NonOwningGroup, IterableGroupAlgorithmCompatibility) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- const auto group = registry.group(entt::get<int, char>);
- const auto iterable = group.each();
- const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
- ASSERT_EQ(std::get<0>(*it), entity);
- }
- TEST(NonOwningGroup, Storage) {
- entt::registry registry;
- const auto entity = registry.create();
- const auto group = registry.group(entt::get<int, const char>);
- static_assert(std::is_same_v<decltype(group.storage<0u>()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
- static_assert(std::is_same_v<decltype(group.storage<int>()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
- static_assert(std::is_same_v<decltype(group.storage<1u>()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
- static_assert(std::is_same_v<decltype(group.storage<const char>()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
- ASSERT_EQ(group.size(), 0u);
- group.storage<int>().emplace(entity);
- registry.emplace<char>(entity);
- ASSERT_EQ(group.size(), 1u);
- ASSERT_TRUE(group.storage<int>().contains(entity));
- ASSERT_TRUE(group.storage<const char>().contains(entity));
- ASSERT_TRUE((registry.all_of<int, char>(entity)));
- group.storage<0u>().erase(entity);
- ASSERT_EQ(group.size(), 0u);
- ASSERT_TRUE(group.storage<1u>().contains(entity));
- ASSERT_FALSE((registry.all_of<int, char>(entity)));
- }
- TEST(OwningGroup, Functionalities) {
- entt::registry registry;
- auto group = registry.group<int>(entt::get<char>);
- auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::get<const char>);
- ASSERT_TRUE(group.empty());
- const auto e0 = registry.create();
- registry.emplace<char>(e0, '1');
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 42);
- registry.emplace<char>(e1, '2');
- ASSERT_FALSE(group.empty());
- ASSERT_NO_FATAL_FAILURE(group.begin()++);
- ASSERT_NO_FATAL_FAILURE(++cgroup.begin());
- ASSERT_NO_FATAL_FAILURE([](auto it) { return it++; }(group.rbegin()));
- ASSERT_NO_FATAL_FAILURE([](auto it) { return ++it; }(cgroup.rbegin()));
- ASSERT_NE(group.begin(), group.end());
- ASSERT_NE(cgroup.begin(), cgroup.end());
- ASSERT_NE(group.rbegin(), group.rend());
- ASSERT_NE(cgroup.rbegin(), cgroup.rend());
- ASSERT_EQ(group.size(), 1u);
- registry.emplace<int>(e0);
- ASSERT_EQ(group.size(), 2u);
- registry.erase<int>(e0);
- ASSERT_EQ(group.size(), 1u);
- ASSERT_EQ(cgroup.storage<const int>().raw()[0u][0u], 42);
- ASSERT_EQ(group.storage<int>().raw()[0u][0u], 42);
- for(auto entity: group) {
- ASSERT_EQ(std::get<0>(cgroup.get<const int, const char>(entity)), 42);
- ASSERT_EQ(std::get<1>(group.get<int, char>(entity)), '2');
- ASSERT_EQ(cgroup.get<const char>(entity), '2');
- }
- ASSERT_EQ(group.storage<int>().raw()[0u][0u], 42);
- registry.erase<char>(e0);
- registry.erase<char>(e1);
- ASSERT_EQ(group.begin(), group.end());
- ASSERT_EQ(cgroup.begin(), cgroup.end());
- ASSERT_EQ(group.rbegin(), group.rend());
- ASSERT_EQ(cgroup.rbegin(), cgroup.rend());
- ASSERT_TRUE(group.empty());
- decltype(group) invalid{};
- ASSERT_TRUE(group);
- ASSERT_TRUE(cgroup);
- ASSERT_FALSE(invalid);
- }
- TEST(OwningGroup, Invalid) {
- entt::registry registry{};
- auto group = std::as_const(registry).group_if_exists<const int>(entt::get<const empty_type>);
- const auto entity = registry.create();
- registry.emplace<empty_type>(entity);
- registry.emplace<int>(entity);
- ASSERT_FALSE(group);
- ASSERT_TRUE(group.empty());
- ASSERT_EQ(group.size(), 0u);
- ASSERT_EQ(group.begin(), group.end());
- ASSERT_EQ(group.rbegin(), group.rend());
- ASSERT_FALSE(group.contains(entity));
- ASSERT_EQ(group.find(entity), group.end());
- ASSERT_EQ(group.front(), entt::entity{entt::null});
- ASSERT_EQ(group.back(), entt::entity{entt::null});
- }
- TEST(OwningGroup, ElementAccess) {
- entt::registry registry;
- auto group = registry.group<int>(entt::get<char>);
- auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::get<const char>);
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- for(auto i = 0u; i < group.size(); ++i) {
- ASSERT_EQ(group[i], i ? e0 : e1);
- ASSERT_EQ(cgroup[i], i ? e0 : e1);
- }
- }
- TEST(OwningGroup, Contains) {
- entt::registry registry;
- auto group = registry.group<int>(entt::get<char>);
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- registry.destroy(e0);
- ASSERT_FALSE(group.contains(e0));
- ASSERT_TRUE(group.contains(e1));
- }
- TEST(OwningGroup, Empty) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<double>(e0);
- registry.emplace<int>(e0);
- registry.emplace<float>(e0);
- const auto e1 = registry.create();
- registry.emplace<char>(e1);
- registry.emplace<float>(e1);
- ASSERT_TRUE((registry.group<char, int>(entt::get<float>).empty()));
- ASSERT_TRUE((registry.group<double, float>(entt::get<char, int>).empty()));
- }
- TEST(OwningGroup, Each) {
- entt::registry registry;
- entt::entity entity[2]{registry.create(), registry.create()};
- auto group = registry.group<int>(entt::get<char>);
- auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::get<const char>);
- registry.emplace<int>(entity[0u], 0);
- registry.emplace<char>(entity[0u], 0);
- registry.emplace<int>(entity[1u], 1);
- registry.emplace<char>(entity[1u], 1);
- auto iterable = group.each();
- auto citerable = cgroup.each();
- ASSERT_NE(citerable.begin(), citerable.end());
- ASSERT_NO_THROW(iterable.begin()->operator=(*iterable.begin()));
- ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
- auto it = iterable.begin();
- ASSERT_EQ((it++, ++it), iterable.end());
- group.each([expected = 1u](auto entt, int &ivalue, char &cvalue) mutable {
- ASSERT_EQ(entt::to_integral(entt), expected);
- ASSERT_EQ(ivalue, expected);
- ASSERT_EQ(cvalue, expected);
- --expected;
- });
- cgroup.each([expected = 1u](const int &ivalue, const char &cvalue) mutable {
- ASSERT_EQ(ivalue, expected);
- ASSERT_EQ(cvalue, expected);
- --expected;
- });
- ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
- ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
- static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
- static_assert(std::is_same_v<decltype(std::get<2>(*citerable.begin())), const char &>);
- // do not use iterable, make sure an iterable group works when created from a temporary
- for(auto [entt, ivalue, cvalue]: registry.group<int>(entt::get<char>).each()) {
- ASSERT_EQ(entt::to_integral(entt), ivalue);
- ASSERT_EQ(entt::to_integral(entt), cvalue);
- }
- }
- TEST(OwningGroup, SortOrdered) {
- entt::registry registry;
- auto group = registry.group<boxed_int, char>();
- entt::entity entities[5]{};
- registry.create(std::begin(entities), std::end(entities));
- registry.emplace<boxed_int>(entities[0], 12);
- registry.emplace<char>(entities[0], 'a');
- registry.emplace<boxed_int>(entities[1], 9);
- registry.emplace<char>(entities[1], 'b');
- registry.emplace<boxed_int>(entities[2], 6);
- registry.emplace<char>(entities[2], 'c');
- registry.emplace<boxed_int>(entities[3], 1);
- registry.emplace<boxed_int>(entities[4], 2);
- group.sort([&group](const entt::entity lhs, const entt::entity rhs) {
- return group.get<boxed_int>(lhs).value < group.get<boxed_int>(rhs).value;
- });
- ASSERT_EQ(group.storage<boxed_int>().data()[0u], entities[0]);
- ASSERT_EQ(group.storage<boxed_int>().data()[1u], entities[1]);
- ASSERT_EQ(group.storage<boxed_int>().data()[2u], entities[2]);
- ASSERT_EQ(group.storage<boxed_int>().data()[3u], entities[3]);
- ASSERT_EQ(group.storage<boxed_int>().data()[4u], entities[4]);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][0u].value, 12);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][1u].value, 9);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][2u].value, 6);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][3u].value, 1);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][4u].value, 2);
- ASSERT_EQ(group.storage<char>().raw()[0u][0u], 'a');
- ASSERT_EQ(group.storage<char>().raw()[0u][1u], 'b');
- ASSERT_EQ(group.storage<char>().raw()[0u][2u], 'c');
- ASSERT_EQ((group.get<boxed_int, char>(entities[0])), (std::make_tuple(boxed_int{12}, 'a')));
- ASSERT_EQ((group.get<boxed_int, char>(entities[1])), (std::make_tuple(boxed_int{9}, 'b')));
- ASSERT_EQ((group.get<boxed_int, char>(entities[2])), (std::make_tuple(boxed_int{6}, 'c')));
- ASSERT_FALSE(group.contains(entities[3]));
- ASSERT_FALSE(group.contains(entities[4]));
- }
- TEST(OwningGroup, SortReverse) {
- entt::registry registry;
- auto group = registry.group<boxed_int, char>();
- entt::entity entities[5]{};
- registry.create(std::begin(entities), std::end(entities));
- registry.emplace<boxed_int>(entities[0], 6);
- registry.emplace<char>(entities[0], 'a');
- registry.emplace<boxed_int>(entities[1], 9);
- registry.emplace<char>(entities[1], 'b');
- registry.emplace<boxed_int>(entities[2], 12);
- registry.emplace<char>(entities[2], 'c');
- registry.emplace<boxed_int>(entities[3], 1);
- registry.emplace<boxed_int>(entities[4], 2);
- group.sort<boxed_int>([](const auto &lhs, const auto &rhs) {
- return lhs.value < rhs.value;
- });
- ASSERT_EQ(group.storage<boxed_int>().data()[0u], entities[2]);
- ASSERT_EQ(group.storage<boxed_int>().data()[1u], entities[1]);
- ASSERT_EQ(group.storage<boxed_int>().data()[2u], entities[0]);
- ASSERT_EQ(group.storage<boxed_int>().data()[3u], entities[3]);
- ASSERT_EQ(group.storage<boxed_int>().data()[4u], entities[4]);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][0u].value, 12);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][1u].value, 9);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][2u].value, 6);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][3u].value, 1);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][4u].value, 2);
- ASSERT_EQ(group.storage<char>().raw()[0u][0u], 'c');
- ASSERT_EQ(group.storage<char>().raw()[0u][1u], 'b');
- ASSERT_EQ(group.storage<char>().raw()[0u][2u], 'a');
- ASSERT_EQ((group.get<boxed_int, char>(entities[0])), (std::make_tuple(boxed_int{6}, 'a')));
- ASSERT_EQ((group.get<boxed_int, char>(entities[1])), (std::make_tuple(boxed_int{9}, 'b')));
- ASSERT_EQ((group.get<boxed_int, char>(entities[2])), (std::make_tuple(boxed_int{12}, 'c')));
- ASSERT_FALSE(group.contains(entities[3]));
- ASSERT_FALSE(group.contains(entities[4]));
- }
- TEST(OwningGroup, SortUnordered) {
- entt::registry registry;
- auto group = registry.group<boxed_int>(entt::get<char>);
- entt::entity entities[7]{};
- registry.create(std::begin(entities), std::end(entities));
- registry.emplace<boxed_int>(entities[0], 6);
- registry.emplace<char>(entities[0], 'c');
- registry.emplace<boxed_int>(entities[1], 3);
- registry.emplace<char>(entities[1], 'b');
- registry.emplace<boxed_int>(entities[2], 1);
- registry.emplace<char>(entities[2], 'a');
- registry.emplace<boxed_int>(entities[3], 9);
- registry.emplace<char>(entities[3], 'd');
- registry.emplace<boxed_int>(entities[4], 12);
- registry.emplace<char>(entities[4], 'e');
- registry.emplace<boxed_int>(entities[5], 4);
- registry.emplace<boxed_int>(entities[6], 5);
- group.sort<boxed_int, char>([](const auto lhs, const auto rhs) {
- static_assert(std::is_same_v<decltype(std::get<0>(lhs)), boxed_int &>);
- static_assert(std::is_same_v<decltype(std::get<1>(rhs)), char &>);
- return std::get<1>(lhs) < std::get<1>(rhs);
- });
- ASSERT_EQ(group.storage<boxed_int>().data()[0u], entities[4]);
- ASSERT_EQ(group.storage<boxed_int>().data()[1u], entities[3]);
- ASSERT_EQ(group.storage<boxed_int>().data()[2u], entities[0]);
- ASSERT_EQ(group.storage<boxed_int>().data()[3u], entities[1]);
- ASSERT_EQ(group.storage<boxed_int>().data()[4u], entities[2]);
- ASSERT_EQ(group.storage<boxed_int>().data()[5u], entities[5]);
- ASSERT_EQ(group.storage<boxed_int>().data()[6u], entities[6]);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][0u].value, 12);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][1u].value, 9);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][2u].value, 6);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][3u].value, 3);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][4u].value, 1);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][5u].value, 4);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][6u].value, 5);
- ASSERT_EQ(group.get<char>(group.storage<boxed_int>().data()[0u]), 'e');
- ASSERT_EQ(group.get<char>(group.storage<boxed_int>().data()[1u]), 'd');
- ASSERT_EQ(group.get<char>(group.storage<boxed_int>().data()[2u]), 'c');
- ASSERT_EQ(group.get<char>(group.storage<boxed_int>().data()[3u]), 'b');
- ASSERT_EQ(group.get<char>(group.storage<boxed_int>().data()[4u]), 'a');
- ASSERT_FALSE(group.contains(entities[5]));
- ASSERT_FALSE(group.contains(entities[6]));
- }
- TEST(OwningGroup, SortWithExclusionList) {
- entt::registry registry;
- auto group = registry.group<boxed_int>(entt::exclude<char>);
- entt::entity entities[5]{};
- registry.create(std::begin(entities), std::end(entities));
- registry.emplace<boxed_int>(entities[0], 0);
- registry.emplace<boxed_int>(entities[1], 1);
- registry.emplace<boxed_int>(entities[2], 2);
- registry.emplace<boxed_int>(entities[3], 3);
- registry.emplace<boxed_int>(entities[4], 4);
- registry.emplace<char>(entities[2]);
- group.sort([](const entt::entity lhs, const entt::entity rhs) {
- return lhs < rhs;
- });
- ASSERT_EQ(group.storage<boxed_int>().data()[0u], entities[4]);
- ASSERT_EQ(group.storage<boxed_int>().data()[1u], entities[3]);
- ASSERT_EQ(group.storage<boxed_int>().data()[2u], entities[1]);
- ASSERT_EQ(group.storage<boxed_int>().data()[3u], entities[0]);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][0u].value, 4);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][1u].value, 3);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][2u].value, 1);
- ASSERT_EQ(group.storage<boxed_int>().raw()[0u][3u].value, 0);
- ASSERT_EQ(group.get<boxed_int>(entities[0]).value, 0);
- ASSERT_EQ(group.get<boxed_int>(entities[1]).value, 1);
- ASSERT_EQ(group.get<boxed_int>(entities[3]).value, 3);
- ASSERT_EQ(group.get<boxed_int>(entities[4]).value, 4);
- ASSERT_FALSE(group.contains(entities[2]));
- }
- TEST(OwningGroup, IndexRebuiltOnDestroy) {
- entt::registry registry;
- auto group = registry.group<int>(entt::get<unsigned int>);
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- registry.emplace<unsigned int>(e0, 0u);
- registry.emplace<unsigned int>(e1, 1u);
- registry.emplace<int>(e0, 0);
- registry.emplace<int>(e1, 1);
- registry.destroy(e0);
- registry.emplace<int>(registry.create(), 42);
- ASSERT_EQ(group.size(), 1u);
- ASSERT_EQ(group[{}], e1);
- ASSERT_EQ(group.get<int>(e1), 1);
- ASSERT_EQ(group.get<unsigned int>(e1), 1u);
- group.each([e1](auto entity, auto ivalue, auto uivalue) {
- ASSERT_EQ(entity, e1);
- ASSERT_EQ(ivalue, 1);
- ASSERT_EQ(uivalue, 1u);
- });
- for(auto &&curr: group.each()) {
- ASSERT_EQ(std::get<0>(curr), e1);
- ASSERT_EQ(std::get<1>(curr), 1);
- ASSERT_EQ(std::get<2>(curr), 1u);
- }
- }
- TEST(OwningGroup, ConstNonConstAndAllInBetween) {
- entt::registry registry;
- auto group = registry.group<int, const char>(entt::get<empty_type, double, const float>);
- ASSERT_EQ(group.size(), 0u);
- const auto entity = registry.create();
- registry.emplace<int>(entity, 0);
- registry.emplace<char>(entity, 'c');
- registry.emplace<empty_type>(entity);
- registry.emplace<double>(entity, 0.);
- registry.emplace<float>(entity, 0.f);
- ASSERT_EQ(group.size(), 1u);
- static_assert(std::is_same_v<decltype(group.get<int>({})), int &>);
- static_assert(std::is_same_v<decltype(group.get<const char>({})), const char &>);
- static_assert(std::is_same_v<decltype(group.get<double>({})), double &>);
- static_assert(std::is_same_v<decltype(group.get<const float>({})), const float &>);
- static_assert(std::is_same_v<decltype(group.get<int, const char, double, const float>({})), std::tuple<int &, const char &, double &, const float &>>);
- static_assert(std::is_same_v<decltype(group.get({})), std::tuple<int &, const char &, double &, const float &>>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).group_if_exists<int>(entt::get<char>)), decltype(std::as_const(registry).group_if_exists<const int>(entt::get<const char>))>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).group_if_exists<const int>(entt::get<char>)), decltype(std::as_const(registry).group_if_exists<const int>(entt::get<const char>))>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).group_if_exists<int>(entt::get<const char>)), decltype(std::as_const(registry).group_if_exists<const int>(entt::get<const char>))>);
- group.each([](auto &&i, auto &&c, auto &&d, auto &&f) {
- static_assert(std::is_same_v<decltype(i), int &>);
- static_assert(std::is_same_v<decltype(c), const char &>);
- static_assert(std::is_same_v<decltype(d), double &>);
- static_assert(std::is_same_v<decltype(f), const float &>);
- });
- for(auto [entt, iv, cv, dv, fv]: group.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), const char &>);
- static_assert(std::is_same_v<decltype(dv), double &>);
- static_assert(std::is_same_v<decltype(fv), const float &>);
- }
- }
- TEST(OwningGroup, Find) {
- entt::registry registry;
- auto group = registry.group<int>(entt::get<const char>);
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- const auto e2 = registry.create();
- registry.emplace<int>(e2);
- registry.emplace<char>(e2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3);
- registry.emplace<char>(e3);
- registry.erase<int>(e1);
- ASSERT_NE(group.find(e0), group.end());
- ASSERT_EQ(group.find(e1), group.end());
- ASSERT_NE(group.find(e2), group.end());
- ASSERT_NE(group.find(e3), group.end());
- auto it = group.find(e2);
- ASSERT_EQ(*it, e2);
- ASSERT_EQ(*(++it), e3);
- ASSERT_EQ(*(++it), e0);
- ASSERT_EQ(++it, group.end());
- ASSERT_EQ(++group.find(e0), group.end());
- const auto e4 = registry.create();
- registry.destroy(e4);
- const auto e5 = registry.create();
- registry.emplace<int>(e5);
- registry.emplace<char>(e5);
- ASSERT_NE(group.find(e5), group.end());
- ASSERT_EQ(group.find(e4), group.end());
- }
- TEST(OwningGroup, ExcludedComponents) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<int>(e0, 0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 1);
- registry.emplace<char>(e1);
- const auto group = registry.group<int>(entt::exclude<char, double>);
- const auto e2 = registry.create();
- registry.emplace<int>(e2, 2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3, 3);
- registry.emplace<double>(e3);
- for(const auto entity: group) {
- ASSERT_TRUE(entity == e0 || entity == e2);
- if(entity == e0) {
- ASSERT_EQ(group.get<int>(e0), 0);
- } else if(entity == e2) {
- ASSERT_EQ(group.get<int>(e2), 2);
- }
- }
- registry.emplace<char>(e0);
- registry.emplace<double>(e2);
- ASSERT_TRUE(group.empty());
- registry.erase<char>(e1);
- registry.erase<double>(e3);
- for(const auto entity: group) {
- ASSERT_TRUE(entity == e1 || entity == e3);
- if(entity == e1) {
- ASSERT_EQ(group.get<int>(e1), 1);
- } else if(entity == e3) {
- ASSERT_EQ(group.get<int>(e3), 3);
- }
- }
- }
- TEST(OwningGroup, EmptyAndNonEmptyTypes) {
- entt::registry registry;
- const auto group = registry.group<int>(entt::get<empty_type>);
- const auto e0 = registry.create();
- registry.emplace<empty_type>(e0);
- registry.emplace<int>(e0);
- const auto e1 = registry.create();
- registry.emplace<empty_type>(e1);
- registry.emplace<int>(e1);
- registry.emplace<int>(registry.create());
- for(const auto entity: group) {
- ASSERT_TRUE(entity == e0 || entity == e1);
- }
- group.each([e0, e1](const auto entity, const int &) {
- ASSERT_TRUE(entity == e0 || entity == e1);
- });
- for(auto [entt, iv]: group.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- ASSERT_TRUE(entt == e0 || entt == e1);
- }
- ASSERT_EQ(group.size(), 2u);
- }
- TEST(OwningGroup, TrackEntitiesOnComponentDestruction) {
- entt::registry registry;
- const auto group = registry.group<int>(entt::exclude<char>);
- const auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::exclude<char>);
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- ASSERT_TRUE(group.empty());
- ASSERT_TRUE(cgroup.empty());
- registry.erase<char>(entity);
- ASSERT_FALSE(group.empty());
- ASSERT_FALSE(cgroup.empty());
- }
- TEST(OwningGroup, EmptyTypes) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- registry.emplace<empty_type>(entity);
- registry.group<int>(entt::get<char, empty_type>).each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.group<int>(entt::get<char, empty_type>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.group<char>(entt::get<empty_type, int>).each([check = true](char, int) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, cv, iv]: registry.group<char>(entt::get<empty_type, int>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- ASSERT_EQ(entity, entt);
- }
- registry.group<empty_type>(entt::get<int, char>).each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.group<empty_type>(entt::get<int, char>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- auto iterable = registry.group<double>(entt::get<int, char>).each();
- ASSERT_EQ(iterable.begin(), iterable.end());
- }
- TEST(OwningGroup, FrontBack) {
- entt::registry registry;
- auto group = registry.group<const char>(entt::get<const int>);
- ASSERT_EQ(group.front(), static_cast<entt::entity>(entt::null));
- ASSERT_EQ(group.back(), static_cast<entt::entity>(entt::null));
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- const auto entity = registry.create();
- registry.emplace<char>(entity);
- ASSERT_EQ(group.front(), e1);
- ASSERT_EQ(group.back(), e0);
- }
- TEST(OwningGroup, SignalRace) {
- entt::registry registry;
- registry.on_construct<double>().connect<&entt::registry::emplace_or_replace<int>>();
- const auto group = registry.group<int>(entt::get<double>);
- auto entity = registry.create();
- registry.emplace<double>(entity);
- ASSERT_EQ(group.size(), 1u);
- }
- TEST(OwningGroup, StableLateInitialization) {
- entt::registry registry;
- for(std::size_t i{}; i < 30u; ++i) {
- auto entity = registry.create();
- if(!(i % 2u)) registry.emplace<int>(entity);
- if(!(i % 3u)) registry.emplace<char>(entity);
- }
- // thanks to @pgruenbacher for pointing out this corner case
- ASSERT_EQ((registry.group<int, char>().size()), 5u);
- }
- TEST(OwningGroup, PreventEarlyOptOut) {
- entt::registry registry;
- registry.emplace<int>(registry.create(), 3);
- const auto entity = registry.create();
- registry.emplace<char>(entity, 'c');
- registry.emplace<int>(entity, 2);
- // thanks to @pgruenbacher for pointing out this corner case
- registry.group<char, int>().each([entity](const auto entt, const auto &c, const auto &i) {
- ASSERT_EQ(entity, entt);
- ASSERT_EQ(c, 'c');
- ASSERT_EQ(i, 2);
- });
- }
- TEST(OwningGroup, SwappingValuesIsAllowed) {
- entt::registry registry;
- const auto group = registry.group<boxed_int>(entt::get<empty_type>);
- for(std::size_t i{}; i < 2u; ++i) {
- const auto entity = registry.create();
- registry.emplace<boxed_int>(entity, static_cast<int>(i));
- registry.emplace<empty_type>(entity);
- }
- registry.destroy(group.back());
- // thanks to @andranik3949 for pointing out this missing test
- registry.view<const boxed_int>().each([](const auto entity, const auto &value) {
- ASSERT_EQ(entt::to_integral(entity), value.value);
- });
- }
- TEST(OwningGroup, ExtendedGet) {
- using type = decltype(std::declval<entt::registry>().group<int, empty_type>(entt::get<char>).get({}));
- static_assert(std::tuple_size_v<type> == 2u);
- static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
- static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity, 42);
- registry.emplace<char>(entity, 'c');
- const auto tup = registry.group<int>(entt::get<char>).get(entity);
- ASSERT_EQ(std::get<0>(tup), 42);
- ASSERT_EQ(std::get<1>(tup), 'c');
- }
- TEST(OwningGroup, IterableGroupAlgorithmCompatibility) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- const auto group = registry.group<int>(entt::get<char>);
- const auto iterable = group.each();
- const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
- ASSERT_EQ(std::get<0>(*it), entity);
- }
- TEST(OwningGroup, Storage) {
- entt::registry registry;
- const auto entity = registry.create();
- const auto group = registry.group<int>(entt::get<const char>);
- static_assert(std::is_same_v<decltype(group.storage<0u>()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
- static_assert(std::is_same_v<decltype(group.storage<int>()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
- static_assert(std::is_same_v<decltype(group.storage<1u>()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
- static_assert(std::is_same_v<decltype(group.storage<const char>()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
- ASSERT_EQ(group.size(), 0u);
- group.storage<int>().emplace(entity);
- registry.emplace<char>(entity);
- ASSERT_EQ(group.size(), 1u);
- ASSERT_TRUE(group.storage<int>().contains(entity));
- ASSERT_TRUE(group.storage<const char>().contains(entity));
- ASSERT_TRUE((registry.all_of<int, char>(entity)));
- group.storage<0u>().erase(entity);
- ASSERT_EQ(group.size(), 0u);
- ASSERT_TRUE(group.storage<1u>().contains(entity));
- ASSERT_FALSE((registry.all_of<int, char>(entity)));
- }
|