registry.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  1. #include <array>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <functional>
  5. #include <iterator>
  6. #include <memory>
  7. #include <tuple>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <vector>
  11. #include <gtest/gtest.h>
  12. #include <entt/config/config.h>
  13. #include <entt/core/hashed_string.hpp>
  14. #include <entt/core/type_info.hpp>
  15. #include <entt/entity/entity.hpp>
  16. #include <entt/entity/group.hpp>
  17. #include <entt/entity/mixin.hpp>
  18. #include <entt/entity/registry.hpp>
  19. #include <entt/entity/storage.hpp>
  20. #include <entt/entity/view.hpp>
  21. #include <entt/signal/sigh.hpp>
  22. #include "../../common/aggregate.h"
  23. #include "../../common/config.h"
  24. #include "../../common/empty.h"
  25. #include "../../common/entity.h"
  26. #include "../../common/mixin.hpp"
  27. #include "../../common/non_default_constructible.h"
  28. #include "../../common/pointer_stable.h"
  29. struct no_eto_type {
  30. static constexpr std::size_t page_size = 1024u;
  31. };
  32. bool operator==(const no_eto_type &lhs, const no_eto_type &rhs) {
  33. return &lhs == &rhs;
  34. }
  35. struct listener {
  36. template<typename Type>
  37. static void sort(entt::registry &registry) {
  38. registry.sort<Type>([](auto lhs, auto rhs) { return lhs < rhs; });
  39. }
  40. void incr(const entt::registry &, entt::entity entity) {
  41. last = entity;
  42. ++counter;
  43. }
  44. void decr(const entt::registry &, entt::entity entity) {
  45. last = entity;
  46. --counter;
  47. }
  48. entt::entity last{entt::null};
  49. int counter{0};
  50. };
  51. struct owner {
  52. void receive(const entt::registry &ref) {
  53. parent = &ref;
  54. }
  55. const entt::registry *parent{nullptr};
  56. };
  57. struct destruction_order {
  58. using ctx_check_type = int;
  59. destruction_order(const entt::registry &ref, bool &ctx)
  60. : registry{&ref},
  61. ctx_check{&ctx} {
  62. *ctx_check = (registry->ctx().find<ctx_check_type>() != nullptr);
  63. }
  64. destruction_order(const destruction_order &) = delete;
  65. destruction_order &operator=(const destruction_order &) = delete;
  66. ~destruction_order() {
  67. *ctx_check = *ctx_check && (registry->ctx().find<ctx_check_type>() != nullptr);
  68. }
  69. private:
  70. const entt::registry *registry;
  71. bool *ctx_check{};
  72. };
  73. struct entity_traits {
  74. using value_type = test::entity;
  75. using entity_type = uint32_t;
  76. using version_type = uint16_t;
  77. static constexpr entity_type entity_mask = 0xFF;
  78. static constexpr entity_type version_mask = 0x00;
  79. };
  80. template<>
  81. struct entt::entt_traits<test::entity>: entt::basic_entt_traits<entity_traits> {
  82. static constexpr auto page_size = ENTT_SPARSE_PAGE;
  83. };
  84. TEST(Registry, Functionalities) {
  85. using traits_type = entt::entt_traits<entt::entity>;
  86. entt::registry registry{};
  87. ASSERT_NO_THROW([[maybe_unused]] auto alloc = registry.get_allocator());
  88. ASSERT_EQ(registry.storage<entt::entity>().size(), 0u);
  89. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
  90. ASSERT_NO_THROW(registry.storage<entt::entity>().reserve(4));
  91. ASSERT_EQ(registry.storage<entt::entity>().capacity(), 4u);
  92. ASSERT_TRUE(registry.storage<entt::entity>().empty());
  93. ASSERT_EQ(registry.storage<int>().size(), 0u);
  94. ASSERT_EQ(registry.storage<char>().size(), 0u);
  95. ASSERT_TRUE(registry.storage<int>().empty());
  96. ASSERT_TRUE(registry.storage<char>().empty());
  97. const auto e0 = registry.create();
  98. const auto e1 = registry.create();
  99. registry.emplace<int>(e1);
  100. registry.emplace<char>(e1);
  101. ASSERT_TRUE(registry.all_of<>(e0));
  102. ASSERT_FALSE(registry.any_of<>(e1));
  103. ASSERT_EQ(registry.storage<int>().size(), 1u);
  104. ASSERT_EQ(registry.storage<char>().size(), 1u);
  105. ASSERT_FALSE(registry.storage<int>().empty());
  106. ASSERT_FALSE(registry.storage<char>().empty());
  107. ASSERT_NE(e0, e1);
  108. ASSERT_FALSE((registry.all_of<int, const char>(e0)));
  109. ASSERT_TRUE((registry.all_of<const int, char>(e1)));
  110. ASSERT_FALSE((registry.any_of<int, const double>(e0)));
  111. ASSERT_TRUE((registry.any_of<const int, double>(e1)));
  112. ASSERT_EQ(registry.try_get<int>(e0), nullptr);
  113. ASSERT_NE(registry.try_get<int>(e1), nullptr);
  114. ASSERT_EQ(registry.try_get<char>(e0), nullptr);
  115. ASSERT_NE(registry.try_get<char>(e1), nullptr);
  116. ASSERT_EQ(registry.try_get<double>(e0), nullptr);
  117. ASSERT_EQ(registry.try_get<double>(e1), nullptr);
  118. ASSERT_EQ(registry.emplace<int>(e0, 4), 4);
  119. ASSERT_EQ(registry.emplace<char>(e0, 'c'), 'c');
  120. ASSERT_NO_THROW(registry.erase<int>(e1));
  121. ASSERT_NO_THROW(registry.erase<char>(e1));
  122. ASSERT_TRUE((registry.all_of<const int, char>(e0)));
  123. ASSERT_FALSE((registry.all_of<int, const char>(e1)));
  124. ASSERT_TRUE((registry.any_of<const int, double>(e0)));
  125. ASSERT_FALSE((registry.any_of<int, const double>(e1)));
  126. const auto e2 = registry.create();
  127. registry.emplace_or_replace<int>(e2, registry.get<int>(e0));
  128. registry.emplace_or_replace<char>(e2, registry.get<char>(e0));
  129. ASSERT_TRUE((registry.all_of<int, char>(e2)));
  130. ASSERT_EQ(registry.get<int>(e0), 4);
  131. ASSERT_EQ(registry.get<char>(e0), 'c');
  132. ASSERT_NE(registry.try_get<int>(e0), nullptr);
  133. ASSERT_NE(registry.try_get<char>(e0), nullptr);
  134. ASSERT_EQ(registry.try_get<double>(e0), nullptr);
  135. ASSERT_EQ(*registry.try_get<int>(e0), 4);
  136. ASSERT_EQ(*registry.try_get<char>(e0), 'c');
  137. ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 4);
  138. ASSERT_EQ(*std::get<0>(registry.try_get<int, char, double>(e0)), 4);
  139. ASSERT_EQ(std::get<1>(static_cast<const entt::registry &>(registry).get<int, char>(e0)), 'c');
  140. ASSERT_EQ(*std::get<1>(static_cast<const entt::registry &>(registry).try_get<int, char, double>(e0)), 'c');
  141. ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
  142. ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));
  143. ASSERT_NE(&registry.get<int>(e0), &registry.get<int>(e2));
  144. ASSERT_NE(&registry.get<char>(e0), &registry.get<char>(e2));
  145. ASSERT_EQ(registry.patch<int>(e0, [](auto &instance) { instance = 2; }), 2);
  146. ASSERT_EQ(registry.replace<int>(e0, 3), 3);
  147. ASSERT_NO_THROW(registry.emplace_or_replace<int>(e0, 1));
  148. ASSERT_NO_THROW(registry.emplace_or_replace<int>(e1, 1));
  149. ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e0), 1);
  150. ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e1), 1);
  151. ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
  152. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 3u);
  153. ASSERT_EQ(traits_type::to_version(e2), 0u);
  154. ASSERT_EQ(registry.current(e2), 0u);
  155. ASSERT_NO_THROW(registry.destroy(e2));
  156. ASSERT_EQ(traits_type::to_version(e2), 0u);
  157. ASSERT_EQ(registry.current(e2), 1u);
  158. ASSERT_TRUE(registry.valid(e0));
  159. ASSERT_TRUE(registry.valid(e1));
  160. ASSERT_FALSE(registry.valid(e2));
  161. ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
  162. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 2u);
  163. ASSERT_NO_THROW(registry.clear());
  164. ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
  165. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
  166. ASSERT_FALSE(registry.storage<entt::entity>().empty());
  167. const auto e3 = registry.create();
  168. ASSERT_EQ(registry.get_or_emplace<int>(e3, 3), 3);
  169. ASSERT_EQ(registry.get_or_emplace<char>(e3, 'c'), 'c');
  170. ASSERT_EQ(registry.storage<int>().size(), 1u);
  171. ASSERT_EQ(registry.storage<char>().size(), 1u);
  172. ASSERT_FALSE(registry.storage<int>().empty());
  173. ASSERT_FALSE(registry.storage<char>().empty());
  174. ASSERT_TRUE((registry.all_of<int, char>(e3)));
  175. ASSERT_EQ(registry.get<int>(e3), 3);
  176. ASSERT_EQ(registry.get<char>(e3), 'c');
  177. ASSERT_NO_THROW(registry.clear<int>());
  178. ASSERT_EQ(registry.storage<int>().size(), 0u);
  179. ASSERT_EQ(registry.storage<char>().size(), 1u);
  180. ASSERT_TRUE(registry.storage<int>().empty());
  181. ASSERT_FALSE(registry.storage<char>().empty());
  182. ASSERT_NO_THROW(registry.clear());
  183. ASSERT_EQ(registry.storage<int>().size(), 0u);
  184. ASSERT_EQ(registry.storage<char>().size(), 0u);
  185. ASSERT_TRUE(registry.storage<int>().empty());
  186. ASSERT_TRUE(registry.storage<char>().empty());
  187. const auto e4 = registry.create();
  188. const auto e5 = registry.create();
  189. registry.emplace<int>(e4);
  190. ASSERT_EQ(registry.remove<int>(e4), 1u);
  191. ASSERT_EQ(registry.remove<int>(e5), 0u);
  192. ASSERT_EQ(registry.storage<int>().size(), 0u);
  193. ASSERT_EQ(registry.storage<char>().size(), 0u);
  194. ASSERT_TRUE(registry.storage<int>().empty());
  195. }
  196. TEST(Registry, Constructors) {
  197. entt::registry registry{};
  198. entt::registry other{64u};
  199. ASSERT_TRUE(registry.storage<entt::entity>().empty());
  200. ASSERT_NO_THROW([[maybe_unused]] auto alloc = registry.get_allocator());
  201. ASSERT_EQ(registry.storage().begin(), registry.storage().end());
  202. ASSERT_TRUE(other.storage<entt::entity>().empty());
  203. ASSERT_NO_THROW([[maybe_unused]] auto alloc = other.get_allocator());
  204. ASSERT_EQ(other.storage().begin(), other.storage().end());
  205. }
  206. TEST(Registry, Move) {
  207. entt::registry registry{};
  208. const auto entity = registry.create();
  209. owner test{};
  210. registry.on_construct<int>().connect<&owner::receive>(test);
  211. registry.on_destroy<int>().connect<&owner::receive>(test);
  212. ASSERT_EQ(test.parent, nullptr);
  213. registry.emplace<int>(entity);
  214. ASSERT_EQ(test.parent, &registry);
  215. entt::registry other{std::move(registry)};
  216. other.erase<int>(entity);
  217. registry = {};
  218. registry.emplace<int>(registry.create(entity));
  219. ASSERT_EQ(test.parent, &other);
  220. registry = std::move(other);
  221. registry.emplace<int>(entity);
  222. registry.emplace<int>(registry.create(entity));
  223. ASSERT_EQ(test.parent, &registry);
  224. }
  225. TEST(Registry, Swap) {
  226. entt::registry registry{};
  227. const auto entity = registry.create();
  228. owner test{};
  229. registry.on_construct<int>().connect<&owner::receive>(test);
  230. registry.on_destroy<int>().connect<&owner::receive>(test);
  231. ASSERT_EQ(test.parent, nullptr);
  232. registry.emplace<int>(entity);
  233. ASSERT_EQ(test.parent, &registry);
  234. entt::registry other{};
  235. other.swap(registry);
  236. other.erase<int>(entity);
  237. registry = {};
  238. registry.emplace<int>(registry.create(entity));
  239. ASSERT_EQ(test.parent, &other);
  240. registry.swap(other);
  241. registry.emplace<int>(entity);
  242. registry.emplace<int>(registry.create(entity));
  243. ASSERT_EQ(test.parent, &registry);
  244. }
  245. TEST(Registry, StorageIterable) {
  246. using namespace entt::literals;
  247. entt::registry registry{};
  248. const auto entity = registry.create();
  249. auto &storage = registry.storage<int>("other"_hs);
  250. storage.emplace(entity);
  251. for(auto [id, pool]: registry.storage()) {
  252. testing::StaticAssertTypeEq<decltype(pool), entt::sparse_set &>();
  253. testing::StaticAssertTypeEq<decltype(id), entt::id_type>();
  254. ASSERT_TRUE(pool.contains(entity));
  255. ASSERT_EQ(&storage, &pool);
  256. ASSERT_EQ(id, "other"_hs);
  257. }
  258. }
  259. TEST(Registry, ConstStorageIterable) {
  260. using namespace entt::literals;
  261. entt::registry registry{};
  262. const auto entity = registry.create();
  263. auto &storage = registry.storage<int>("other"_hs);
  264. storage.emplace(entity);
  265. for(auto &&curr: std::as_const(registry).storage()) {
  266. testing::StaticAssertTypeEq<decltype(curr.second), const entt::sparse_set &>();
  267. testing::StaticAssertTypeEq<decltype(curr.first), entt::id_type>();
  268. ASSERT_TRUE(curr.second.contains(entity));
  269. ASSERT_EQ(&storage, &curr.second);
  270. ASSERT_EQ(curr.first, "other"_hs);
  271. }
  272. }
  273. TEST(Registry, RegistryStorageIterator) {
  274. entt::registry registry{};
  275. const auto entity = registry.create();
  276. registry.emplace<int>(entity);
  277. auto iterable = registry.storage();
  278. auto end{iterable.begin()};
  279. decltype(end) begin{};
  280. begin = iterable.end();
  281. std::swap(begin, end);
  282. ASSERT_EQ(begin, iterable.cbegin());
  283. ASSERT_EQ(end, iterable.cend());
  284. ASSERT_NE(begin, end);
  285. ASSERT_EQ(begin++, iterable.begin());
  286. ASSERT_EQ(begin--, iterable.end());
  287. ASSERT_EQ(begin + 1, iterable.end());
  288. ASSERT_EQ(end - 1, iterable.begin());
  289. ASSERT_EQ(++begin, iterable.end());
  290. ASSERT_EQ(--begin, iterable.begin());
  291. ASSERT_EQ(begin += 1, iterable.end());
  292. ASSERT_EQ(begin -= 1, iterable.begin());
  293. ASSERT_EQ(begin + (end - begin), iterable.end());
  294. ASSERT_EQ(begin - (begin - end), iterable.end());
  295. ASSERT_EQ(end - (end - begin), iterable.begin());
  296. ASSERT_EQ(end + (begin - end), iterable.begin());
  297. ASSERT_EQ(begin[0u].first, iterable.begin()->first);
  298. ASSERT_EQ(&begin[0u].second, &(*iterable.begin()).second);
  299. ASSERT_LT(begin, end);
  300. ASSERT_LE(begin, iterable.begin());
  301. ASSERT_GT(end, begin);
  302. ASSERT_GE(end, iterable.end());
  303. ASSERT_EQ(begin[0u].first, entt::type_id<int>().hash());
  304. ASSERT_TRUE(begin[0u].second.contains(entity));
  305. }
  306. TEST(Registry, RegistryConstStorageIterator) {
  307. entt::registry registry{};
  308. const auto entity = registry.create();
  309. registry.emplace<int>(entity);
  310. auto iterable = std::as_const(registry).storage();
  311. auto end{iterable.begin()};
  312. decltype(end) begin{};
  313. begin = iterable.end();
  314. std::swap(begin, end);
  315. ASSERT_EQ(begin, iterable.cbegin());
  316. ASSERT_EQ(end, iterable.cend());
  317. ASSERT_NE(begin, end);
  318. ASSERT_EQ(begin++, iterable.begin());
  319. ASSERT_EQ(begin--, iterable.end());
  320. ASSERT_EQ(begin + 1, iterable.end());
  321. ASSERT_EQ(end - 1, iterable.begin());
  322. ASSERT_EQ(++begin, iterable.end());
  323. ASSERT_EQ(--begin, iterable.begin());
  324. ASSERT_EQ(begin += 1, iterable.end());
  325. ASSERT_EQ(begin -= 1, iterable.begin());
  326. ASSERT_EQ(begin + (end - begin), iterable.end());
  327. ASSERT_EQ(begin - (begin - end), iterable.end());
  328. ASSERT_EQ(end - (end - begin), iterable.begin());
  329. ASSERT_EQ(end + (begin - end), iterable.begin());
  330. ASSERT_EQ(begin[0u].first, iterable.begin()->first);
  331. ASSERT_EQ(&begin[0u].second, &(*iterable.begin()).second);
  332. ASSERT_LT(begin, end);
  333. ASSERT_LE(begin, iterable.begin());
  334. ASSERT_GT(end, begin);
  335. ASSERT_GE(end, iterable.end());
  336. ASSERT_EQ(begin[0u].first, entt::type_id<int>().hash());
  337. ASSERT_TRUE(begin[0u].second.contains(entity));
  338. }
  339. TEST(Registry, RegistryStorageIteratorConversion) {
  340. entt::registry registry{};
  341. registry.storage<int>();
  342. auto proxy = registry.storage();
  343. [[maybe_unused]] auto cproxy = std::as_const(registry).storage();
  344. const typename decltype(proxy)::iterator it = proxy.begin();
  345. typename decltype(cproxy)::iterator cit = it;
  346. testing::StaticAssertTypeEq<decltype(*it), std::pair<entt::id_type, entt::sparse_set &>>();
  347. testing::StaticAssertTypeEq<decltype(*cit), std::pair<entt::id_type, const entt::sparse_set &>>();
  348. ASSERT_EQ(it->first, entt::type_id<int>().hash());
  349. ASSERT_EQ((*it).second.type(), entt::type_id<int>());
  350. ASSERT_EQ(it->first, cit->first);
  351. ASSERT_EQ((*it).second.type(), (*cit).second.type());
  352. ASSERT_EQ(it - cit, 0);
  353. ASSERT_EQ(cit - it, 0);
  354. ASSERT_LE(it, cit);
  355. ASSERT_LE(cit, it);
  356. ASSERT_GE(it, cit);
  357. ASSERT_GE(cit, it);
  358. ASSERT_EQ(it, cit);
  359. ASSERT_NE(++cit, it);
  360. }
  361. TEST(Registry, Storage) {
  362. using namespace entt::literals;
  363. entt::registry registry{};
  364. auto &storage = registry.storage<test::empty>("other"_hs);
  365. const auto entity = registry.create();
  366. testing::StaticAssertTypeEq<decltype(registry.storage<test::empty>()), entt::storage_type_t<test::empty> &>();
  367. testing::StaticAssertTypeEq<decltype(registry.storage<const test::empty>()), const entt::storage_type_t<test::empty> &>();
  368. testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage<test::empty>()), const entt::storage_type_t<test::empty> *>();
  369. testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage<const test::empty>()), const entt::storage_type_t<test::empty> *>();
  370. testing::StaticAssertTypeEq<decltype(registry.storage("other"_hs)), entt::storage_type_t<test::empty>::base_type *>();
  371. testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage("other"_hs)), const entt::storage_type_t<test::empty>::base_type *>();
  372. ASSERT_EQ(&registry.storage<test::empty>("other"_hs), &storage);
  373. ASSERT_EQ(&registry.storage<const test::empty>("other"_hs), &storage);
  374. ASSERT_NE(std::as_const(registry).storage<test::empty>(), &storage);
  375. ASSERT_NE(std::as_const(registry).storage<const test::empty>(), &storage);
  376. ASSERT_NE(registry.storage("other"_hs), nullptr);
  377. ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
  378. ASSERT_FALSE(registry.any_of<test::empty>(entity));
  379. ASSERT_FALSE(storage.contains(entity));
  380. registry.emplace<test::empty>(entity);
  381. ASSERT_FALSE(storage.contains(entity));
  382. ASSERT_TRUE(registry.any_of<test::empty>(entity));
  383. ASSERT_EQ((entt::basic_view{registry.storage<test::empty>(), storage}.size_hint()), 0u);
  384. storage.emplace(entity);
  385. ASSERT_TRUE(storage.contains(entity));
  386. ASSERT_TRUE(registry.any_of<test::empty>(entity));
  387. ASSERT_EQ((entt::basic_view{registry.storage<test::empty>(), storage}.size_hint()), 1u);
  388. registry.destroy(entity);
  389. ASSERT_EQ(registry.create(entity), entity);
  390. ASSERT_FALSE(storage.contains(entity));
  391. ASSERT_FALSE(registry.any_of<test::empty>(entity));
  392. }
  393. ENTT_DEBUG_TEST(RegistryDeathTest, Storage) {
  394. using namespace entt::literals;
  395. entt::registry registry{};
  396. registry.storage<test::empty>("other"_hs);
  397. ASSERT_DEATH(registry.storage<int>("other"_hs), "");
  398. ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<int>("other"_hs), "");
  399. ASSERT_DEATH(registry.storage<entt::entity>("other"_hs), "");
  400. ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<entt::entity>("other"_hs), "");
  401. }
  402. TEST(Registry, StorageReset) {
  403. using namespace entt::literals;
  404. entt::registry registry{};
  405. registry.storage<int>();
  406. registry.storage<int>("other"_hs);
  407. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  408. ASSERT_NE(registry.storage("other"_hs), nullptr);
  409. ASSERT_EQ(registry.reset("other"_hs), 1u);
  410. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  411. ASSERT_EQ(registry.storage("other"_hs), nullptr);
  412. ASSERT_EQ(registry.reset("other"_hs), 0u);
  413. ASSERT_EQ(registry.reset(entt::type_id<int>().hash()), 1u);
  414. ASSERT_EQ(registry.reset(entt::type_id<int>().hash()), 0u);
  415. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  416. ASSERT_EQ(registry.storage("other"_hs), nullptr);
  417. }
  418. ENTT_DEBUG_TEST(RegistryDeathTest, StorageReset) {
  419. entt::registry registry{};
  420. const entt::entity entity = registry.create();
  421. ASSERT_TRUE(registry.valid(entity));
  422. ASSERT_DEATH(registry.reset(entt::type_id<entt::entity>().hash()), "");
  423. ASSERT_TRUE(registry.valid(entity));
  424. }
  425. TEST(Registry, Identifiers) {
  426. using traits_type = entt::entt_traits<entt::entity>;
  427. entt::registry registry{};
  428. const auto pre = registry.create();
  429. ASSERT_EQ(traits_type::to_integral(pre), traits_type::to_entity(pre));
  430. registry.destroy(pre);
  431. const auto post = registry.create();
  432. ASSERT_NE(pre, post);
  433. ASSERT_EQ(traits_type::to_entity(pre), traits_type::to_entity(post));
  434. ASSERT_NE(traits_type::to_version(pre), traits_type::to_version(post));
  435. ASSERT_NE(traits_type::to_version(pre), registry.current(pre));
  436. ASSERT_EQ(traits_type::to_version(post), registry.current(post));
  437. const auto invalid = traits_type::combine(traits_type::to_entity(post) + 1u, {});
  438. ASSERT_EQ(traits_type::to_version(invalid), typename traits_type::version_type{});
  439. ASSERT_EQ(registry.current(invalid), traits_type::to_version(entt::tombstone));
  440. }
  441. TEST(Registry, MoreOnIdentifiers) {
  442. using traits_type = entt::entt_traits<entt::entity>;
  443. entt::registry registry{};
  444. const std::array entity{registry.create(), registry.create()};
  445. registry.destroy(entity[0]);
  446. ASSERT_FALSE(registry.valid(entity[0]));
  447. ASSERT_TRUE(registry.valid(entity[1]));
  448. ASSERT_EQ(registry.current(entt::null), traits_type::to_version(entt::tombstone));
  449. ASSERT_EQ(registry.current(entity[0]), traits_type::to_version(entity[0]) + 1u);
  450. ASSERT_EQ(registry.current(entity[1]), traits_type::to_version(entity[1]));
  451. registry.destroy(entity[1]);
  452. ASSERT_FALSE(registry.valid(entity[1]));
  453. ASSERT_EQ(registry.current(entity[1]), traits_type::to_version(entity[1]) + 1u);
  454. }
  455. TEST(Registry, VersionOverflow) {
  456. using traits_type = entt::entt_traits<entt::entity>;
  457. entt::registry registry{};
  458. const auto entity = registry.create();
  459. registry.destroy(entity);
  460. ASSERT_NE(registry.current(entity), traits_type::to_version(entity));
  461. ASSERT_NE(registry.current(entity), typename traits_type::version_type{});
  462. registry.destroy(registry.create(), traits_type::to_version(entt::tombstone) - 1u);
  463. registry.destroy(registry.create());
  464. ASSERT_EQ(registry.current(entity), traits_type::to_version(entity));
  465. ASSERT_EQ(registry.current(entity), typename traits_type::version_type{});
  466. }
  467. TEST(Registry, NullEntity) {
  468. entt::registry registry{};
  469. const entt::entity entity = entt::null;
  470. ASSERT_FALSE(registry.valid(entity));
  471. ASSERT_NE(registry.create(entity), entity);
  472. }
  473. TEST(Registry, TombstoneVersion) {
  474. using traits_type = entt::entt_traits<entt::entity>;
  475. entt::registry registry{};
  476. const entt::entity entity = entt::tombstone;
  477. ASSERT_FALSE(registry.valid(entity));
  478. const auto other = registry.create();
  479. const auto vers = traits_type::to_version(entity);
  480. const auto required = traits_type::construct(traits_type::to_entity(other), vers);
  481. ASSERT_NE(registry.destroy(other, vers), vers);
  482. ASSERT_NE(registry.create(required), required);
  483. }
  484. TEST(Registry, CreateManyEntitiesAtOnce) {
  485. using traits_type = entt::entt_traits<entt::entity>;
  486. entt::registry registry{};
  487. std::array<entt::entity, 3u> entity{};
  488. const auto entt = registry.create();
  489. registry.destroy(registry.create());
  490. registry.destroy(entt);
  491. registry.destroy(registry.create());
  492. registry.create(entity.begin(), entity.end());
  493. ASSERT_TRUE(registry.valid(entity[0]));
  494. ASSERT_TRUE(registry.valid(entity[1]));
  495. ASSERT_TRUE(registry.valid(entity[2]));
  496. ASSERT_EQ(traits_type::to_entity(entity[0]), 0u);
  497. ASSERT_EQ(traits_type::to_version(entity[0]), 2u);
  498. ASSERT_EQ(traits_type::to_entity(entity[1]), 1u);
  499. ASSERT_EQ(traits_type::to_version(entity[1]), 1u);
  500. ASSERT_EQ(traits_type::to_entity(entity[2]), 2u);
  501. ASSERT_EQ(traits_type::to_version(entity[2]), 0u);
  502. }
  503. TEST(Registry, CreateManyEntitiesAtOnceWithListener) {
  504. entt::registry registry{};
  505. std::array<entt::entity, 3u> entity{};
  506. listener listener;
  507. registry.on_construct<int>().connect<&listener::incr>(listener);
  508. registry.create(entity.begin(), entity.end());
  509. registry.insert(entity.begin(), entity.end(), 1);
  510. registry.insert(entity.begin(), entity.end(), 'c');
  511. ASSERT_EQ(registry.get<int>(entity[0]), 1);
  512. ASSERT_EQ(registry.get<char>(entity[1]), 'c');
  513. ASSERT_EQ(listener.counter, 3);
  514. registry.on_construct<int>().disconnect<&listener::incr>(listener);
  515. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  516. registry.create(entity.begin(), entity.end());
  517. registry.insert(entity.begin(), entity.end(), 'a');
  518. registry.insert<test::empty>(entity.begin(), entity.end());
  519. ASSERT_TRUE(registry.all_of<test::empty>(entity[0]));
  520. ASSERT_EQ(registry.get<char>(entity[2]), 'a');
  521. ASSERT_EQ(listener.counter, 6);
  522. }
  523. TEST(Registry, CreateWithHint) {
  524. using traits_type = entt::entt_traits<entt::entity>;
  525. entt::registry registry{};
  526. auto e3 = registry.create(entt::entity{3});
  527. auto e2 = registry.create(entt::entity{3});
  528. ASSERT_EQ(e2, entt::entity{0});
  529. ASSERT_FALSE(registry.valid(entt::entity{1}));
  530. ASSERT_FALSE(registry.valid(entt::entity{2}));
  531. ASSERT_EQ(e3, entt::entity{3});
  532. registry.destroy(e2);
  533. ASSERT_EQ(traits_type::to_version(e2), 0u);
  534. ASSERT_EQ(registry.current(e2), 1u);
  535. e2 = registry.create();
  536. auto e1 = registry.create(entt::entity{2});
  537. ASSERT_EQ(traits_type::to_entity(e2), 0u);
  538. ASSERT_EQ(traits_type::to_version(e2), 1u);
  539. ASSERT_EQ(traits_type::to_entity(e1), 2u);
  540. ASSERT_EQ(traits_type::to_version(e1), 0u);
  541. registry.destroy(e1);
  542. registry.destroy(e2);
  543. auto e0 = registry.create(entt::entity{0});
  544. ASSERT_EQ(e0, entt::entity{0});
  545. ASSERT_EQ(traits_type::to_version(e0), 0u);
  546. }
  547. TEST(Registry, CreateClearCycle) {
  548. using traits_type = entt::entt_traits<entt::entity>;
  549. entt::registry registry{};
  550. entt::entity pre = entt::null;
  551. entt::entity post = entt::null;
  552. const std::size_t first_iteration = 10u;
  553. const std::size_t second_iteration = 7u;
  554. const std::size_t third_iteration = 5u;
  555. for(std::size_t i{}; i < first_iteration; ++i) {
  556. const auto entity = registry.create();
  557. registry.emplace<double>(entity);
  558. }
  559. registry.clear();
  560. for(std::size_t i{}; i < second_iteration; ++i) {
  561. const auto entity = registry.create();
  562. registry.emplace<int>(entity);
  563. if(i == 3u) {
  564. pre = entity;
  565. }
  566. }
  567. registry.clear();
  568. for(std::size_t i{}; i < third_iteration; ++i) {
  569. const auto entity = registry.create();
  570. if(i == 3u) {
  571. post = entity;
  572. }
  573. }
  574. ASSERT_FALSE(registry.valid(pre));
  575. ASSERT_TRUE(registry.valid(post));
  576. ASSERT_NE(traits_type::to_version(pre), traits_type::to_version(post));
  577. ASSERT_EQ(traits_type::to_version(pre) + 1, traits_type::to_version(post));
  578. ASSERT_EQ(registry.current(pre), registry.current(post));
  579. }
  580. TEST(Registry, CreateDestroyReleaseCornerCase) {
  581. entt::registry registry{};
  582. const auto e0 = registry.create();
  583. const auto e1 = registry.create();
  584. registry.destroy(e0);
  585. registry.storage<entt::entity>().erase(e1);
  586. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
  587. ASSERT_EQ(registry.current(e0), 1u);
  588. ASSERT_EQ(registry.current(e1), 1u);
  589. }
  590. ENTT_DEBUG_TEST(RegistryDeathTest, CreateTooManyEntities) {
  591. entt::basic_registry<test::entity> registry{};
  592. std::vector<test::entity> entity(entt::entt_traits<test::entity>::to_entity(entt::null));
  593. registry.create(entity.begin(), entity.end());
  594. ASSERT_DEATH([[maybe_unused]] const auto entt = registry.create(), "");
  595. }
  596. TEST(Registry, DestroyVersion) {
  597. entt::registry registry{};
  598. const auto e0 = registry.create();
  599. const auto e1 = registry.create();
  600. ASSERT_EQ(registry.current(e0), 0u);
  601. ASSERT_EQ(registry.current(e1), 0u);
  602. registry.destroy(e0);
  603. registry.destroy(e1, 3);
  604. ASSERT_EQ(registry.current(e0), 1u);
  605. ASSERT_EQ(registry.current(e1), 3u);
  606. }
  607. ENTT_DEBUG_TEST(RegistryDeathTest, DestroyVersion) {
  608. entt::registry registry{};
  609. const auto entity = registry.create();
  610. registry.destroy(entity);
  611. ASSERT_DEATH(registry.destroy(entity), "");
  612. ASSERT_DEATH(registry.destroy(entity, 3), "");
  613. }
  614. TEST(Registry, DestroyRange) {
  615. entt::registry registry{};
  616. const auto iview = registry.view<int>();
  617. const auto icview = registry.view<int, char>();
  618. std::array<entt::entity, 3u> entity{};
  619. registry.create(entity.begin(), entity.end());
  620. registry.emplace<int>(entity[0u]);
  621. registry.emplace<char>(entity[0u]);
  622. registry.emplace<double>(entity[0u]);
  623. registry.emplace<int>(entity[1u]);
  624. registry.emplace<char>(entity[1u]);
  625. registry.emplace<int>(entity[2u]);
  626. ASSERT_TRUE(registry.valid(entity[0u]));
  627. ASSERT_TRUE(registry.valid(entity[1u]));
  628. ASSERT_TRUE(registry.valid(entity[2u]));
  629. registry.destroy(icview.begin(), icview.end());
  630. ASSERT_FALSE(registry.valid(entity[0u]));
  631. ASSERT_FALSE(registry.valid(entity[1u]));
  632. ASSERT_TRUE(registry.valid(entity[2u]));
  633. ASSERT_EQ(registry.storage<int>().size(), 1u);
  634. ASSERT_EQ(registry.storage<char>().size(), 0u);
  635. ASSERT_EQ(registry.storage<double>().size(), 0u);
  636. registry.destroy(iview.begin(), iview.end());
  637. ASSERT_FALSE(registry.valid(entity[2u]));
  638. ASSERT_NO_THROW(registry.destroy(iview.rbegin(), iview.rend()));
  639. ASSERT_EQ(iview.size(), 0u);
  640. ASSERT_EQ(icview.size_hint(), 0u);
  641. ASSERT_EQ(registry.storage<int>().size(), 0u);
  642. ASSERT_EQ(registry.storage<char>().size(), 0u);
  643. ASSERT_EQ(registry.storage<double>().size(), 0u);
  644. registry.create(entity.begin(), entity.end());
  645. registry.insert<int>(entity.begin(), entity.end());
  646. ASSERT_TRUE(registry.valid(entity[0u]));
  647. ASSERT_TRUE(registry.valid(entity[1u]));
  648. ASSERT_TRUE(registry.valid(entity[2u]));
  649. ASSERT_EQ(registry.storage<int>().size(), 3u);
  650. registry.destroy(entity.begin(), entity.end());
  651. ASSERT_FALSE(registry.valid(entity[0u]));
  652. ASSERT_FALSE(registry.valid(entity[1u]));
  653. ASSERT_FALSE(registry.valid(entity[2u]));
  654. ASSERT_EQ(registry.storage<int>().size(), 0u);
  655. entt::sparse_set managed{};
  656. registry.create(entity.begin(), entity.end());
  657. managed.push(entity.begin(), entity.end());
  658. registry.insert<int>(managed.begin(), managed.end());
  659. ASSERT_TRUE(registry.valid(managed[0u]));
  660. ASSERT_TRUE(registry.valid(managed[1u]));
  661. ASSERT_TRUE(registry.valid(managed[2u]));
  662. ASSERT_EQ(registry.storage<int>().size(), 3u);
  663. registry.destroy(managed.begin(), managed.end());
  664. ASSERT_FALSE(registry.valid(managed[0u]));
  665. ASSERT_FALSE(registry.valid(managed[1u]));
  666. ASSERT_FALSE(registry.valid(managed[2u]));
  667. ASSERT_EQ(registry.storage<int>().size(), 0u);
  668. }
  669. TEST(Registry, StableDestroy) {
  670. entt::registry registry{};
  671. const auto iview = registry.view<int>();
  672. const auto icview = registry.view<int, test::pointer_stable>();
  673. std::array<entt::entity, 3u> entity{};
  674. registry.create(entity.begin(), entity.end());
  675. registry.emplace<int>(entity[0u]);
  676. registry.emplace<test::pointer_stable>(entity[0u]);
  677. registry.emplace<double>(entity[0u]);
  678. registry.emplace<int>(entity[1u]);
  679. registry.emplace<test::pointer_stable>(entity[1u]);
  680. registry.emplace<int>(entity[2u]);
  681. ASSERT_TRUE(registry.valid(entity[0u]));
  682. ASSERT_TRUE(registry.valid(entity[1u]));
  683. ASSERT_TRUE(registry.valid(entity[2u]));
  684. registry.destroy(icview.begin(), icview.end());
  685. ASSERT_FALSE(registry.valid(entity[0u]));
  686. ASSERT_FALSE(registry.valid(entity[1u]));
  687. ASSERT_TRUE(registry.valid(entity[2u]));
  688. ASSERT_EQ(registry.storage<int>().size(), 1u);
  689. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  690. ASSERT_EQ(registry.storage<double>().size(), 0u);
  691. registry.destroy(iview.begin(), iview.end());
  692. ASSERT_FALSE(registry.valid(entity[2u]));
  693. ASSERT_EQ(iview.size(), 0u);
  694. ASSERT_EQ(icview.size_hint(), 0u);
  695. ASSERT_EQ(registry.storage<int>().size(), 0u);
  696. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  697. ASSERT_EQ(registry.storage<double>().size(), 0u);
  698. }
  699. TEST(Registry, Emplace) {
  700. entt::registry registry{};
  701. const auto entity = registry.create();
  702. ASSERT_FALSE(registry.all_of<int>(entity));
  703. const auto &ref = registry.emplace<int>(entity, 4);
  704. ASSERT_TRUE(registry.all_of<int>(entity));
  705. ASSERT_EQ(registry.get<int>(entity), ref);
  706. ASSERT_EQ(ref, 4);
  707. }
  708. TEST(Registry, EmplaceEmpty) {
  709. entt::registry registry{};
  710. const auto entity = registry.create();
  711. ASSERT_FALSE(registry.all_of<test::empty>(entity));
  712. registry.emplace<test::empty>(entity, 4);
  713. ASSERT_TRUE(registry.all_of<test::empty>(entity));
  714. }
  715. TEST(Registry, EmplaceAggregate) {
  716. entt::registry registry{};
  717. const auto entity = registry.create();
  718. ASSERT_FALSE(registry.all_of<test::aggregate>(entity));
  719. const auto &ref = registry.emplace<test::aggregate>(entity, 4);
  720. ASSERT_TRUE(registry.all_of<test::aggregate>(entity));
  721. ASSERT_EQ(registry.get<test::aggregate>(entity).value, ref.value);
  722. ASSERT_EQ(ref.value, 4);
  723. }
  724. TEST(Registry, EmplaceTypesFromStandardTemplateLibrary) {
  725. // see #37 - the test shouldn't crash, that's all
  726. entt::registry registry{};
  727. const auto entity = registry.create();
  728. registry.emplace<std::unordered_set<int>>(entity).insert(1);
  729. registry.destroy(entity);
  730. }
  731. TEST(Registry, EmplaceWithComponents) {
  732. // it should compile, that's all
  733. entt::registry registry{};
  734. const auto value = 0;
  735. registry.emplace<int>(registry.create(), value);
  736. }
  737. TEST(Registry, EmplaceMoveOnlyComponent) {
  738. entt::registry registry{};
  739. // the purpose is to ensure that move only types are always accepted
  740. registry.emplace<std::unique_ptr<int>>(registry.create());
  741. }
  742. TEST(Registry, EmplaceNonDefaultConstructibleComponent) {
  743. entt::registry registry{};
  744. // the purpose is to ensure that non default constructible type are always accepted
  745. registry.emplace<test::non_default_constructible>(registry.create(), 1);
  746. }
  747. TEST(Registry, StableEmplace) {
  748. entt::registry registry{};
  749. registry.on_construct<int>().connect<&listener::sort<int>>();
  750. registry.emplace<int>(registry.create(), 0);
  751. ASSERT_EQ(registry.emplace<int>(registry.create(), 1), 1);
  752. }
  753. ENTT_DEBUG_TEST(RegistryDeathTest, Emplace) {
  754. entt::registry registry{};
  755. const auto entity = registry.create();
  756. registry.destroy(entity);
  757. ASSERT_DEATH(registry.emplace<int>(entity), "");
  758. }
  759. TEST(Registry, Insert) {
  760. entt::registry registry{};
  761. std::array<entt::entity, 3u> entity{};
  762. registry.create(entity.begin(), entity.end());
  763. registry.emplace<int>(entity[0u]);
  764. registry.emplace<char>(entity[0u]);
  765. registry.emplace<double>(entity[0u]);
  766. registry.emplace<int>(entity[1u]);
  767. registry.emplace<char>(entity[1u]);
  768. registry.emplace<int>(entity[2u]);
  769. ASSERT_FALSE(registry.all_of<float>(entity[0u]));
  770. ASSERT_FALSE(registry.all_of<float>(entity[1u]));
  771. ASSERT_FALSE(registry.all_of<float>(entity[2u]));
  772. const auto icview = registry.view<int, char>();
  773. registry.insert(icview.begin(), icview.end(), 3.f);
  774. ASSERT_EQ(registry.get<float>(entity[0u]), 3.f);
  775. ASSERT_EQ(registry.get<float>(entity[1u]), 3.f);
  776. ASSERT_FALSE(registry.all_of<float>(entity[2u]));
  777. registry.clear<float>();
  778. std::array value{0.f, 1.f, 2.f};
  779. const auto iview = registry.view<int>();
  780. registry.insert<float>(iview.rbegin(), iview.rend(), value.begin());
  781. ASSERT_EQ(registry.get<float>(entity[0u]), 0.f);
  782. ASSERT_EQ(registry.get<float>(entity[1u]), 1.f);
  783. ASSERT_EQ(registry.get<float>(entity[2u]), 2.f);
  784. }
  785. ENTT_DEBUG_TEST(RegistryDeathTest, Insert) {
  786. entt::registry registry{};
  787. const std::array entity{registry.create()};
  788. const std::array value{0};
  789. registry.destroy(entity[0]);
  790. ASSERT_DEATH(registry.insert<int>(entity.begin(), entity.end(), value[0]), "");
  791. ASSERT_DEATH(registry.insert<int>(entity.begin(), entity.end(), value.begin()), "");
  792. }
  793. TEST(Registry, EmplaceOrReplace) {
  794. entt::registry registry{};
  795. const auto entity = registry.create();
  796. ASSERT_FALSE(registry.all_of<int>(entity));
  797. const auto &ref = registry.emplace_or_replace<int>(entity, 4);
  798. ASSERT_TRUE(registry.all_of<int>(entity));
  799. ASSERT_EQ(registry.get<int>(entity), ref);
  800. ASSERT_EQ(ref, 4);
  801. registry.emplace_or_replace<int>(entity, 0);
  802. ASSERT_EQ(ref, 0);
  803. }
  804. TEST(Registry, EmplaceOrReplaceEmpty) {
  805. entt::registry registry{};
  806. const auto entity = registry.create();
  807. ASSERT_FALSE(registry.all_of<test::empty>(entity));
  808. registry.emplace_or_replace<test::empty>(entity);
  809. ASSERT_TRUE(registry.all_of<test::empty>(entity));
  810. registry.emplace_or_replace<test::empty>(entity);
  811. ASSERT_EQ(registry.storage<test::empty>().size(), 1u);
  812. }
  813. TEST(Registry, EmplaceOrReplaceAggregate) {
  814. entt::registry registry{};
  815. const auto entity = registry.create();
  816. auto &instance = registry.emplace_or_replace<test::aggregate>(entity, 1);
  817. ASSERT_EQ(instance.value, 1);
  818. }
  819. ENTT_DEBUG_TEST(RegistryDeathTest, EmplaceOrReplace) {
  820. entt::registry registry{};
  821. const auto entity = registry.create();
  822. registry.destroy(entity);
  823. ASSERT_DEATH(registry.emplace_or_replace<int>(entity), "");
  824. }
  825. TEST(Registry, Patch) {
  826. entt::registry registry{};
  827. const auto entity = registry.create();
  828. registry.emplace<int>(entity, 3);
  829. ASSERT_EQ(registry.get<int>(entity), 3);
  830. registry.patch<int>(entity);
  831. ASSERT_EQ(registry.get<int>(entity), 3);
  832. registry.patch<int>(entity, [](auto &value) { value = 1; });
  833. ASSERT_EQ(registry.get<int>(entity), 1);
  834. }
  835. TEST(Registry, Replace) {
  836. entt::registry registry{};
  837. const auto entity = registry.create();
  838. registry.emplace<int>(entity, 3);
  839. ASSERT_EQ(registry.get<int>(entity), 3);
  840. registry.replace<int>(entity);
  841. ASSERT_EQ(registry.get<int>(entity), 0);
  842. registry.replace<int>(entity, 1);
  843. ASSERT_EQ(registry.get<int>(entity), 1);
  844. }
  845. TEST(Registry, ReplaceAggregate) {
  846. entt::registry registry{};
  847. const auto entity = registry.create();
  848. registry.emplace<test::aggregate>(entity, 0);
  849. auto &instance = registry.replace<test::aggregate>(entity, 1);
  850. ASSERT_EQ(instance.value, 1);
  851. }
  852. TEST(Registry, Remove) {
  853. entt::registry registry{};
  854. const auto iview = registry.view<int>();
  855. const auto icview = registry.view<int, char>();
  856. std::array<entt::entity, 3u> entity{};
  857. registry.create(entity.begin(), entity.end());
  858. registry.emplace<int>(entity[0u]);
  859. registry.emplace<char>(entity[0u]);
  860. registry.emplace<double>(entity[0u]);
  861. registry.emplace<int>(entity[1u]);
  862. registry.emplace<char>(entity[1u]);
  863. registry.emplace<int>(entity[2u]);
  864. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  865. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  866. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  867. registry.remove<int, char>(entity[0u]);
  868. ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 2u);
  869. ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 0u);
  870. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  871. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  872. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  873. ASSERT_EQ(registry.storage<int>().size(), 1u);
  874. ASSERT_EQ(registry.storage<char>().size(), 0u);
  875. ASSERT_EQ(registry.storage<double>().size(), 1u);
  876. ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
  877. ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
  878. ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
  879. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  880. ASSERT_EQ(registry.remove<int>(iview.begin(), iview.end()), 0u);
  881. ASSERT_EQ(registry.storage<int>().size(), 0u);
  882. ASSERT_EQ(registry.storage<char>().size(), 0u);
  883. ASSERT_EQ(registry.storage<double>().size(), 1u);
  884. registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
  885. registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
  886. ASSERT_EQ(registry.storage<int>().size(), 1u);
  887. ASSERT_EQ(registry.storage<char>().size(), 1u);
  888. registry.remove<int, char>(iview.begin(), iview.end());
  889. registry.remove<int, char>(iview.begin(), iview.end());
  890. ASSERT_EQ(registry.storage<int>().size(), 0u);
  891. ASSERT_EQ(registry.storage<char>().size(), 0u);
  892. registry.insert<int>(entity.begin(), entity.end());
  893. registry.insert<char>(entity.begin(), entity.end());
  894. ASSERT_EQ(registry.storage<int>().size(), 3u);
  895. ASSERT_EQ(registry.storage<char>().size(), 3u);
  896. registry.remove<int, char>(entity.begin(), entity.end());
  897. registry.remove<int, char>(entity.begin(), entity.end());
  898. ASSERT_EQ(registry.storage<int>().size(), 0u);
  899. ASSERT_EQ(registry.storage<char>().size(), 0u);
  900. ASSERT_FALSE(registry.orphan(entity[0u]));
  901. ASSERT_TRUE(registry.orphan(entity[1u]));
  902. ASSERT_TRUE(registry.orphan(entity[2u]));
  903. }
  904. TEST(Registry, StableRemove) {
  905. entt::registry registry{};
  906. const auto iview = registry.view<int>();
  907. const auto icview = registry.view<int, test::pointer_stable>();
  908. std::array<entt::entity, 3u> entity{};
  909. registry.create(entity.begin(), entity.end());
  910. registry.emplace<int>(entity[0u]);
  911. registry.emplace<test::pointer_stable>(entity[0u]);
  912. registry.emplace<double>(entity[0u]);
  913. registry.emplace<int>(entity[1u]);
  914. registry.emplace<test::pointer_stable>(entity[1u]);
  915. registry.emplace<int>(entity[2u]);
  916. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  917. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  918. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  919. registry.remove<int, test::pointer_stable>(entity[0u]);
  920. ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 2u);
  921. ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 0u);
  922. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  923. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  924. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  925. ASSERT_EQ(registry.storage<int>().size(), 1u);
  926. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  927. ASSERT_EQ(registry.storage<double>().size(), 1u);
  928. ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
  929. ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
  930. ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
  931. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  932. ASSERT_EQ(registry.storage<int>().size(), 0u);
  933. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  934. ASSERT_EQ(registry.storage<double>().size(), 1u);
  935. }
  936. TEST(Registry, Erase) {
  937. entt::registry registry{};
  938. const auto iview = registry.view<int>();
  939. const auto icview = registry.view<int, char>();
  940. std::array<entt::entity, 3u> entity{};
  941. registry.create(entity.begin(), entity.end());
  942. registry.emplace<int>(entity[0u]);
  943. registry.emplace<char>(entity[0u]);
  944. registry.emplace<double>(entity[0u]);
  945. registry.emplace<int>(entity[1u]);
  946. registry.emplace<char>(entity[1u]);
  947. registry.emplace<int>(entity[2u]);
  948. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  949. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  950. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  951. registry.erase<int, char>(entity[0u]);
  952. registry.erase<int, char>(icview.begin(), icview.end());
  953. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  954. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  955. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  956. ASSERT_EQ(registry.storage<int>().size(), 1u);
  957. ASSERT_EQ(registry.storage<char>().size(), 0u);
  958. ASSERT_EQ(registry.storage<double>().size(), 1u);
  959. registry.erase<int>(iview.begin(), iview.end());
  960. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  961. ASSERT_NO_THROW(registry.erase<int>(iview.rbegin(), iview.rend()));
  962. ASSERT_EQ(registry.storage<int>().size(), 0u);
  963. ASSERT_EQ(registry.storage<char>().size(), 0u);
  964. ASSERT_EQ(registry.storage<double>().size(), 1u);
  965. registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
  966. registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
  967. ASSERT_EQ(registry.storage<int>().size(), 1u);
  968. ASSERT_EQ(registry.storage<char>().size(), 1u);
  969. registry.erase<int, char>(iview.begin(), iview.end());
  970. ASSERT_EQ(registry.storage<int>().size(), 0u);
  971. ASSERT_EQ(registry.storage<char>().size(), 0u);
  972. registry.insert<int>(entity.begin(), entity.end());
  973. registry.insert<char>(entity.begin(), entity.end());
  974. ASSERT_EQ(registry.storage<int>().size(), 3u);
  975. ASSERT_EQ(registry.storage<char>().size(), 3u);
  976. registry.erase<int, char>(entity.begin(), entity.end());
  977. ASSERT_EQ(registry.storage<int>().size(), 0u);
  978. ASSERT_EQ(registry.storage<char>().size(), 0u);
  979. ASSERT_FALSE(registry.orphan(entity[0u]));
  980. ASSERT_TRUE(registry.orphan(entity[1u]));
  981. ASSERT_TRUE(registry.orphan(entity[2u]));
  982. }
  983. ENTT_DEBUG_TEST(RegistryDeathTest, Erase) {
  984. entt::registry registry{};
  985. const std::array entity{registry.create()};
  986. ASSERT_FALSE((registry.any_of<int>(entity[0u])));
  987. ASSERT_DEATH((registry.erase<int>(entity.begin(), entity.end())), "");
  988. ASSERT_DEATH(registry.erase<int>(entity[0u]), "");
  989. }
  990. TEST(Registry, StableErase) {
  991. entt::registry registry{};
  992. const auto iview = registry.view<int>();
  993. const auto icview = registry.view<int, test::pointer_stable>();
  994. std::array<entt::entity, 3u> entity{};
  995. registry.create(entity.begin(), entity.end());
  996. registry.emplace<int>(entity[0u]);
  997. registry.emplace<test::pointer_stable>(entity[0u]);
  998. registry.emplace<double>(entity[0u]);
  999. registry.emplace<int>(entity[1u]);
  1000. registry.emplace<test::pointer_stable>(entity[1u]);
  1001. registry.emplace<int>(entity[2u]);
  1002. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  1003. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  1004. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  1005. registry.erase<int, test::pointer_stable>(entity[0u]);
  1006. registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
  1007. registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
  1008. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  1009. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  1010. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  1011. ASSERT_EQ(registry.storage<int>().size(), 1u);
  1012. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1013. ASSERT_EQ(registry.storage<double>().size(), 1u);
  1014. registry.erase<int>(iview.begin(), iview.end());
  1015. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  1016. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1017. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1018. ASSERT_EQ(registry.storage<double>().size(), 1u);
  1019. }
  1020. TEST(Registry, EraseIf) {
  1021. using namespace entt::literals;
  1022. entt::registry registry{};
  1023. const auto entity = registry.create();
  1024. registry.emplace<int>(entity);
  1025. registry.storage<int>("other"_hs).emplace(entity);
  1026. registry.emplace<char>(entity);
  1027. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1028. ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
  1029. ASSERT_TRUE(registry.storage<char>().contains(entity));
  1030. registry.erase_if(entity, [](auto &&...) { return false; });
  1031. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1032. ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
  1033. ASSERT_TRUE(registry.storage<char>().contains(entity));
  1034. registry.erase_if(entity, [](entt::id_type id, auto &&...) { return id == "other"_hs; });
  1035. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1036. ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
  1037. ASSERT_TRUE(registry.storage<char>().contains(entity));
  1038. registry.erase_if(entity, [](auto, const auto &storage) { return storage.type() == entt::type_id<char>(); });
  1039. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1040. ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
  1041. ASSERT_FALSE(registry.storage<char>().contains(entity));
  1042. }
  1043. TEST(Registry, Compact) {
  1044. entt::registry registry{};
  1045. std::array<entt::entity, 2u> entity{};
  1046. registry.create(entity.begin(), entity.end());
  1047. registry.emplace<int>(entity[0u]);
  1048. registry.emplace<test::pointer_stable>(entity[0u]);
  1049. registry.emplace<int>(entity[1u]);
  1050. registry.emplace<test::pointer_stable>(entity[1u]);
  1051. ASSERT_EQ(registry.storage<int>().size(), 2u);
  1052. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1053. registry.destroy(entity.begin(), entity.end());
  1054. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1055. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1056. registry.compact<int>();
  1057. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1058. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1059. registry.compact();
  1060. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1061. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 0u);
  1062. }
  1063. TEST(Registry, AllAnyOf) {
  1064. entt::registry registry{};
  1065. const auto entity = registry.create();
  1066. ASSERT_FALSE(registry.all_of<int>(entity));
  1067. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  1068. ASSERT_FALSE((registry.any_of<int, char>(entity)));
  1069. registry.emplace<int>(entity);
  1070. ASSERT_TRUE(registry.all_of<int>(entity));
  1071. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  1072. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  1073. registry.emplace<char>(entity);
  1074. ASSERT_TRUE(registry.all_of<int>(entity));
  1075. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  1076. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  1077. }
  1078. TEST(Registry, Get) {
  1079. entt::registry registry{};
  1080. const auto entity = registry.create();
  1081. registry.emplace<int>(entity, 1);
  1082. registry.emplace<char>(entity, 'c');
  1083. ASSERT_EQ(std::as_const(registry).get<int>(entity), 1);
  1084. ASSERT_EQ((std::as_const(registry).get<int, char>(entity)), std::make_tuple(1, 'c'));
  1085. registry.get<int>(entity) = 3;
  1086. std::get<1>(registry.get<int, char>(entity)) = 'a';
  1087. ASSERT_EQ(registry.get<int>(entity), 3);
  1088. ASSERT_EQ((registry.get<int, char>(entity)), std::make_tuple(3, 'a'));
  1089. }
  1090. TEST(Registry, GetOrEmplace) {
  1091. entt::registry registry{};
  1092. const auto entity = registry.create();
  1093. const auto value = 3;
  1094. const auto other = 1.;
  1095. ASSERT_EQ(registry.get_or_emplace<int>(entity, value), value);
  1096. ASSERT_EQ(registry.get_or_emplace<double>(entity, other), other);
  1097. ASSERT_TRUE((registry.all_of<int, double>(entity)));
  1098. ASSERT_EQ(registry.get<int>(entity), value);
  1099. ASSERT_EQ(registry.get<double>(entity), other);
  1100. }
  1101. TEST(Registry, GetOrEmplaceEmpty) {
  1102. entt::registry registry{};
  1103. const auto entity = registry.create();
  1104. // get_or_emplace must work for empty types
  1105. // NOLINTNEXTLINE(readability-redundant-casting)
  1106. static_cast<void>(registry.get_or_emplace<test::empty>(entity));
  1107. ASSERT_TRUE((registry.all_of<test::empty>(entity)));
  1108. }
  1109. ENTT_DEBUG_TEST(RegistryDeathTest, GetOrEmplace) {
  1110. entt::registry registry{};
  1111. const auto entity = registry.create();
  1112. registry.destroy(entity);
  1113. ASSERT_DEATH([[maybe_unused]] const auto value = registry.get_or_emplace<int>(entity), "");
  1114. }
  1115. TEST(Registry, TryGet) {
  1116. entt::registry registry{};
  1117. const auto entity = registry.create();
  1118. ASSERT_EQ(registry.try_get<int>(entity), nullptr);
  1119. ASSERT_EQ(std::as_const(registry).try_get<int>(entity), nullptr);
  1120. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1121. const int &elem = registry.emplace<int>(entity);
  1122. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1123. ASSERT_EQ(registry.try_get<int>(entity), &elem);
  1124. ASSERT_EQ(std::as_const(registry).try_get<int>(entity), &elem);
  1125. }
  1126. TEST(Registry, Clear) {
  1127. entt::registry registry{};
  1128. const std::array entity{registry.create(), registry.create()};
  1129. registry.insert<int>(entity.begin(), entity.end());
  1130. registry.insert<char>(entity.begin(), entity.end());
  1131. ASSERT_TRUE((registry.all_of<int, char>(entity[0])));
  1132. ASSERT_TRUE((registry.all_of<int, char>(entity[1])));
  1133. registry.clear<int>();
  1134. ASSERT_TRUE(registry.valid(entity[0]));
  1135. ASSERT_TRUE(registry.valid(entity[1]));
  1136. ASSERT_FALSE(registry.all_of<int>(entity[0]));
  1137. ASSERT_FALSE(registry.all_of<int>(entity[1]));
  1138. ASSERT_TRUE(registry.all_of<char>(entity[0]));
  1139. ASSERT_TRUE(registry.all_of<char>(entity[1]));
  1140. registry.clear();
  1141. ASSERT_FALSE(registry.valid(entity[0]));
  1142. ASSERT_FALSE(registry.valid(entity[1]));
  1143. ASSERT_TRUE(registry.storage<int>().empty());
  1144. ASSERT_TRUE(registry.storage<char>().empty());
  1145. }
  1146. TEST(Registry, Orphan) {
  1147. entt::registry registry{};
  1148. std::array<entt::entity, 3u> entity{};
  1149. registry.create(entity.begin(), entity.end());
  1150. registry.emplace<int>(entity[0u]);
  1151. registry.emplace<int>(entity[2u]);
  1152. for(auto [entt]: registry.storage<entt::entity>().each()) {
  1153. ASSERT_TRUE(entt != entity[1u] || registry.orphan(entt));
  1154. }
  1155. registry.erase<int>(entity[0u]);
  1156. registry.erase<int>(entity[2u]);
  1157. for(auto [entt]: registry.storage<entt::entity>().each()) {
  1158. ASSERT_TRUE(registry.orphan(entt));
  1159. }
  1160. }
  1161. TEST(Registry, Signals) {
  1162. entt::registry registry{};
  1163. std::array<entt::entity, 2u> entity{};
  1164. listener listener;
  1165. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  1166. registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
  1167. registry.on_construct<int>().connect<&listener::incr>(listener);
  1168. registry.on_destroy<int>().connect<&listener::decr>(listener);
  1169. registry.create(entity.begin(), entity.end());
  1170. registry.insert<test::empty>(entity.begin(), entity.end());
  1171. ASSERT_EQ(listener.counter, 2);
  1172. ASSERT_EQ(listener.last, entity[1u]);
  1173. registry.insert<int>(entity.rbegin(), entity.rend());
  1174. ASSERT_EQ(listener.counter, 4);
  1175. ASSERT_EQ(listener.last, entity[0u]);
  1176. registry.erase<test::empty, int>(entity[0u]);
  1177. ASSERT_EQ(listener.counter, 2);
  1178. ASSERT_EQ(listener.last, entity[0u]);
  1179. registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
  1180. registry.on_destroy<int>().disconnect<&listener::decr>(listener);
  1181. registry.erase<test::empty, int>(entity[1u]);
  1182. ASSERT_EQ(listener.counter, 2);
  1183. ASSERT_EQ(listener.last, entity[0u]);
  1184. registry.on_construct<test::empty>().disconnect<&listener::incr>(listener);
  1185. registry.on_construct<int>().disconnect<&listener::incr>(listener);
  1186. registry.emplace<test::empty>(entity[1u]);
  1187. registry.emplace<int>(entity[1u]);
  1188. ASSERT_EQ(listener.counter, 2);
  1189. ASSERT_EQ(listener.last, entity[0u]);
  1190. registry.on_construct<int>().connect<&listener::incr>(listener);
  1191. registry.on_destroy<int>().connect<&listener::decr>(listener);
  1192. registry.emplace<int>(entity[0u]);
  1193. registry.erase<int>(entity[1u]);
  1194. ASSERT_EQ(listener.counter, 2);
  1195. ASSERT_EQ(listener.last, entity[1u]);
  1196. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  1197. registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
  1198. registry.erase<test::empty>(entity[1u]);
  1199. registry.emplace<test::empty>(entity[0u]);
  1200. ASSERT_EQ(listener.counter, 2);
  1201. ASSERT_EQ(listener.last, entity[0u]);
  1202. registry.clear<test::empty, int>();
  1203. ASSERT_EQ(listener.counter, 0);
  1204. ASSERT_EQ(listener.last, entity[0u]);
  1205. registry.insert<test::empty>(entity.begin(), entity.end());
  1206. registry.insert<int>(entity.begin(), entity.end());
  1207. registry.destroy(entity[1u]);
  1208. ASSERT_EQ(listener.counter, 2);
  1209. ASSERT_EQ(listener.last, entity[1u]);
  1210. registry.erase<int, test::empty>(entity[0u]);
  1211. registry.emplace_or_replace<int>(entity[0u]);
  1212. registry.emplace_or_replace<test::empty>(entity[0u]);
  1213. ASSERT_EQ(listener.counter, 2);
  1214. ASSERT_EQ(listener.last, entity[0u]);
  1215. registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
  1216. registry.on_destroy<int>().disconnect<&listener::decr>(listener);
  1217. registry.emplace_or_replace<test::empty>(entity[0u]);
  1218. registry.emplace_or_replace<int>(entity[0u]);
  1219. ASSERT_EQ(listener.counter, 2);
  1220. ASSERT_EQ(listener.last, entity[0u]);
  1221. registry.on_update<test::empty>().connect<&listener::incr>(listener);
  1222. registry.on_update<int>().connect<&listener::incr>(listener);
  1223. registry.emplace_or_replace<test::empty>(entity[0u]);
  1224. registry.emplace_or_replace<int>(entity[0u]);
  1225. ASSERT_EQ(listener.counter, 4);
  1226. ASSERT_EQ(listener.last, entity[0u]);
  1227. registry.replace<test::empty>(entity[0u]);
  1228. registry.replace<int>(entity[0u]);
  1229. ASSERT_EQ(listener.counter, 6);
  1230. ASSERT_EQ(listener.last, entity[0u]);
  1231. }
  1232. TEST(Registry, SignalsOnRuntimePool) {
  1233. using namespace entt::literals;
  1234. entt::registry registry{};
  1235. const auto entity = registry.create();
  1236. listener listener;
  1237. registry.on_construct<int>("custom"_hs).connect<&listener::incr>(listener);
  1238. registry.on_update<int>("custom"_hs).connect<&listener::incr>(listener);
  1239. registry.on_destroy<int>("custom"_hs).connect<&listener::incr>(listener);
  1240. ASSERT_EQ(listener.counter, 0);
  1241. registry.emplace<int>(entity);
  1242. registry.patch<int>(entity);
  1243. registry.erase<int>(entity);
  1244. ASSERT_EQ(listener.counter, 0);
  1245. registry.storage<int>("custom"_hs).emplace(entity);
  1246. registry.storage<int>("custom"_hs).patch(entity);
  1247. registry.storage<int>("custom"_hs).erase(entity);
  1248. ASSERT_EQ(listener.counter, 3);
  1249. }
  1250. TEST(Registry, SignalsOnEntity) {
  1251. entt::registry registry{};
  1252. listener listener;
  1253. registry.on_construct<entt::entity>().connect<&listener::incr>(listener);
  1254. entt::entity entity = registry.create();
  1255. entt::entity other = registry.create();
  1256. ASSERT_EQ(listener.counter, 2);
  1257. ASSERT_EQ(listener.last, other);
  1258. registry.destroy(other);
  1259. registry.destroy(entity);
  1260. ASSERT_EQ(listener.counter, 2);
  1261. ASSERT_EQ(listener.last, other);
  1262. registry.on_construct<entt::entity>().disconnect(&listener);
  1263. other = registry.create();
  1264. entity = registry.create();
  1265. ASSERT_EQ(listener.counter, 2);
  1266. ASSERT_NE(listener.last, entity);
  1267. ASSERT_NE(listener.last, other);
  1268. registry.on_update<entt::entity>().connect<&listener::decr>(listener);
  1269. registry.patch<entt::entity>(entity);
  1270. ASSERT_EQ(listener.counter, 1);
  1271. ASSERT_EQ(listener.last, entity);
  1272. registry.on_update<entt::entity>().disconnect(&listener);
  1273. registry.patch<entt::entity>(other);
  1274. ASSERT_EQ(listener.counter, 1);
  1275. ASSERT_NE(listener.last, other);
  1276. registry.on_destroy<entt::entity>().connect<&listener::decr>(listener);
  1277. registry.destroy(entity);
  1278. ASSERT_EQ(listener.counter, 0);
  1279. ASSERT_EQ(listener.last, entity);
  1280. registry.on_destroy<entt::entity>().disconnect(&listener);
  1281. registry.destroy(other);
  1282. ASSERT_EQ(listener.counter, 0);
  1283. ASSERT_NE(listener.last, other);
  1284. }
  1285. TEST(Registry, SignalWhenDestroying) {
  1286. entt::registry registry{};
  1287. const auto entity = registry.create();
  1288. registry.on_destroy<double>().connect<&entt::registry::remove<char>>();
  1289. registry.emplace<double>(entity);
  1290. registry.emplace<int>(entity);
  1291. ASSERT_NE(registry.storage(entt::type_id<double>().hash()), nullptr);
  1292. ASSERT_NE(registry.storage(entt::type_id<int>().hash()), nullptr);
  1293. ASSERT_EQ(registry.storage(entt::type_id<char>().hash()), nullptr);
  1294. ASSERT_TRUE(registry.valid(entity));
  1295. registry.destroy(entity);
  1296. ASSERT_NE(registry.storage(entt::type_id<char>().hash()), nullptr);
  1297. ASSERT_FALSE(registry.valid(entity));
  1298. }
  1299. TEST(Registry, SelfSignal) {
  1300. entt::registry registry{};
  1301. const auto entity = registry.create();
  1302. // required because of an issue of VS2019
  1303. constexpr auto emplace_or_replace = &entt::registry::emplace_or_replace<double>;
  1304. constexpr auto remove = &entt::registry::remove<double>;
  1305. registry.on_construct<int>().connect<emplace_or_replace>();
  1306. registry.on_destroy<int>().connect<remove>();
  1307. registry.emplace<double>(entity, .3);
  1308. ASSERT_FALSE(registry.all_of<int>(entity));
  1309. ASSERT_EQ(registry.get<double>(entity), .3);
  1310. registry.emplace<int>(entity);
  1311. ASSERT_TRUE(registry.all_of<int>(entity));
  1312. ASSERT_EQ(registry.get<double>(entity), .0);
  1313. registry.erase<int>(entity);
  1314. ASSERT_FALSE((registry.any_of<int, double>(entity)));
  1315. registry.on_construct<int>().disconnect<emplace_or_replace>();
  1316. registry.on_destroy<int>().disconnect<remove>();
  1317. registry.emplace<int>(entity);
  1318. ASSERT_TRUE((registry.any_of<int, double>(entity)));
  1319. ASSERT_FALSE(registry.all_of<double>(entity));
  1320. }
  1321. TEST(Registry, View) {
  1322. entt::registry registry{};
  1323. std::array<entt::entity, 3u> entity{};
  1324. auto iview = std::as_const(registry).view<int>();
  1325. auto cview = std::as_const(registry).view<char>();
  1326. ASSERT_FALSE(iview);
  1327. ASSERT_FALSE(cview);
  1328. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1329. ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
  1330. auto mview = registry.view<int, char>();
  1331. auto fview = registry.view<int>(entt::exclude<char>);
  1332. ASSERT_TRUE(mview);
  1333. ASSERT_TRUE(fview);
  1334. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1335. ASSERT_NE(std::as_const(registry).storage<char>(), nullptr);
  1336. iview = std::as_const(registry).view<int>();
  1337. cview = std::as_const(registry).view<char>();
  1338. ASSERT_TRUE(iview);
  1339. ASSERT_TRUE(cview);
  1340. registry.create(entity.begin(), entity.end());
  1341. registry.emplace<int>(entity[0u], 0);
  1342. registry.emplace<char>(entity[0u], 'c');
  1343. registry.emplace<int>(entity[1u], 0);
  1344. registry.emplace<int>(entity[2u], 0);
  1345. registry.emplace<char>(entity[2u], 'c');
  1346. ASSERT_EQ(iview.size(), 3u);
  1347. ASSERT_EQ(cview.size(), 2u);
  1348. ASSERT_EQ(mview.size_hint(), 3u);
  1349. ASSERT_EQ(fview.size_hint(), 3u);
  1350. mview.refresh();
  1351. ASSERT_EQ(mview.size_hint(), 2u);
  1352. ASSERT_EQ(fview.size_hint(), 3u);
  1353. ASSERT_NE(mview.begin(), mview.end());
  1354. ASSERT_NE(fview.begin(), fview.end());
  1355. ASSERT_EQ(std::distance(mview.begin(), mview.end()), 2);
  1356. ASSERT_EQ(std::distance(fview.begin(), fview.end()), 1);
  1357. mview.each([&entity, first = true](auto entt, auto &&...) mutable {
  1358. ASSERT_EQ(entt, first ? entity[2u] : entity[0u]);
  1359. first = false;
  1360. });
  1361. fview.each([&entity](auto entt, auto &&...) {
  1362. ASSERT_EQ(entt, entity[1u]);
  1363. });
  1364. }
  1365. TEST(Registry, ExcludeOnlyView) {
  1366. entt::registry registry{};
  1367. std::array<entt::entity, 4u> entity{};
  1368. auto view = registry.view<entt::entity>(entt::exclude<int>);
  1369. registry.create(entity.begin(), entity.end());
  1370. registry.emplace<int>(entity[0u], 0);
  1371. registry.emplace<int>(entity[2u], 0);
  1372. registry.emplace<int>(entity[3u], 0);
  1373. registry.destroy(entity[3u]);
  1374. ASSERT_EQ(view.size_hint(), 3u);
  1375. ASSERT_NE(view.begin(), view.end());
  1376. ASSERT_EQ(std::distance(view.begin(), view.end()), 1);
  1377. ASSERT_EQ(*view.begin(), entity[1u]);
  1378. for(auto [entt]: view.each()) {
  1379. ASSERT_EQ(entt, entity[1u]);
  1380. }
  1381. view.each([&entity](auto entt) {
  1382. ASSERT_EQ(entt, entity[1u]);
  1383. });
  1384. }
  1385. TEST(Registry, CleanViewAfterRemoveAndClear) {
  1386. entt::registry registry{};
  1387. auto view = registry.view<int, char>();
  1388. const auto entity = registry.create();
  1389. registry.emplace<int>(entity);
  1390. registry.emplace<char>(entity);
  1391. ASSERT_EQ(view.size_hint(), 1u);
  1392. registry.erase<char>(entity);
  1393. ASSERT_EQ(view.size_hint(), 1u);
  1394. registry.emplace<char>(entity);
  1395. ASSERT_EQ(view.size_hint(), 1u);
  1396. registry.clear<int>();
  1397. ASSERT_EQ(view.size_hint(), 0u);
  1398. registry.emplace<int>(entity);
  1399. ASSERT_EQ(view.size_hint(), 1u);
  1400. registry.clear();
  1401. ASSERT_EQ(view.size_hint(), 0u);
  1402. }
  1403. TEST(Registry, NonOwningGroupInitOnFirstUse) {
  1404. entt::registry registry{};
  1405. std::array<entt::entity, 3u> entity{};
  1406. registry.create(entity.begin(), entity.end());
  1407. registry.insert<int>(entity.begin(), entity.end(), 0);
  1408. registry.emplace<char>(entity[0u], 'c');
  1409. registry.emplace<char>(entity[2u], 'c');
  1410. std::size_t cnt{};
  1411. auto group = registry.group(entt::get<int, char>);
  1412. group.each([&cnt](auto...) { ++cnt; });
  1413. ASSERT_FALSE((registry.owned<int, char>()));
  1414. ASSERT_EQ(cnt, 2u);
  1415. }
  1416. TEST(Registry, NonOwningGroupInitOnEmplace) {
  1417. entt::registry registry{};
  1418. std::array<entt::entity, 3u> entity{};
  1419. auto group = registry.group(entt::get<int, char>);
  1420. registry.create(entity.begin(), entity.end());
  1421. registry.insert<int>(entity.begin(), entity.end(), 0);
  1422. registry.emplace<char>(entity[0u], 'c');
  1423. registry.emplace<char>(entity[2u], 'c');
  1424. std::size_t cnt{};
  1425. group.each([&cnt](auto...) { ++cnt; });
  1426. ASSERT_FALSE((registry.owned<int, char>()));
  1427. ASSERT_EQ(cnt, 2u);
  1428. }
  1429. TEST(Registry, FullOwningGroupInitOnFirstUse) {
  1430. entt::registry registry{};
  1431. std::array<entt::entity, 3u> entity{};
  1432. registry.create(entity.begin(), entity.end());
  1433. registry.insert<int>(entity.begin(), entity.end(), 0);
  1434. registry.emplace<char>(entity[0u], 'c');
  1435. registry.emplace<char>(entity[2u], 'c');
  1436. std::size_t cnt{};
  1437. auto group = registry.group<int, char>();
  1438. group.each([&cnt](auto...) { ++cnt; });
  1439. ASSERT_TRUE(registry.owned<int>());
  1440. ASSERT_TRUE(registry.owned<char>());
  1441. ASSERT_FALSE(registry.owned<double>());
  1442. ASSERT_EQ(cnt, 2u);
  1443. }
  1444. TEST(Registry, FullOwningGroupInitOnEmplace) {
  1445. entt::registry registry{};
  1446. std::array<entt::entity, 3u> entity{};
  1447. auto group = registry.group<int, char>();
  1448. registry.create(entity.begin(), entity.end());
  1449. registry.insert<int>(entity.begin(), entity.end(), 0);
  1450. registry.emplace<char>(entity[0u], 'c');
  1451. registry.emplace<char>(entity[2u], 'c');
  1452. std::size_t cnt{};
  1453. group.each([&cnt](auto...) { ++cnt; });
  1454. ASSERT_TRUE(registry.owned<int>());
  1455. ASSERT_TRUE(registry.owned<char>());
  1456. ASSERT_FALSE(registry.owned<double>());
  1457. ASSERT_EQ(cnt, 2u);
  1458. }
  1459. TEST(Registry, PartialOwningGroupInitOnFirstUse) {
  1460. entt::registry registry{};
  1461. std::array<entt::entity, 3u> entity{};
  1462. registry.create(entity.begin(), entity.end());
  1463. registry.insert<int>(entity.begin(), entity.end(), 0);
  1464. registry.emplace<char>(entity[0u], 'c');
  1465. registry.emplace<char>(entity[2u], 'c');
  1466. std::size_t cnt{};
  1467. auto group = registry.group<int>(entt::get<char>);
  1468. group.each([&cnt](auto...) { ++cnt; });
  1469. ASSERT_TRUE((registry.owned<int, char>()));
  1470. ASSERT_TRUE(registry.owned<int>());
  1471. ASSERT_FALSE(registry.owned<char>());
  1472. ASSERT_EQ(cnt, 2u);
  1473. }
  1474. TEST(Registry, PartialOwningGroupInitOnEmplace) {
  1475. entt::registry registry{};
  1476. std::array<entt::entity, 3u> entity{};
  1477. auto group = registry.group<int>(entt::get<char>);
  1478. registry.create(entity.begin(), entity.end());
  1479. registry.insert<int>(entity.begin(), entity.end(), 0);
  1480. registry.emplace<char>(entity[0u], 'c');
  1481. registry.emplace<char>(entity[2u], 'c');
  1482. std::size_t cnt{};
  1483. group.each([&cnt](auto...) { ++cnt; });
  1484. ASSERT_TRUE((registry.owned<int, char>()));
  1485. ASSERT_TRUE(registry.owned<int>());
  1486. ASSERT_FALSE(registry.owned<char>());
  1487. ASSERT_EQ(cnt, 2u);
  1488. }
  1489. TEST(Registry, CleanNonOwningGroupViewAfterRemoveAndClear) {
  1490. entt::registry registry{};
  1491. auto group = registry.group(entt::get<int, char>);
  1492. const auto entity = registry.create();
  1493. registry.emplace<int>(entity, 0);
  1494. registry.emplace<char>(entity, 'c');
  1495. ASSERT_EQ(group.size(), 1u);
  1496. registry.erase<char>(entity);
  1497. ASSERT_EQ(group.size(), 0u);
  1498. registry.emplace<char>(entity, 'c');
  1499. ASSERT_EQ(group.size(), 1u);
  1500. registry.clear<int>();
  1501. ASSERT_EQ(group.size(), 0u);
  1502. registry.emplace<int>(entity, 0);
  1503. ASSERT_EQ(group.size(), 1u);
  1504. registry.clear();
  1505. ASSERT_EQ(group.size(), 0u);
  1506. }
  1507. TEST(Registry, CleanFullOwningGroupViewAfterRemoveAndClear) {
  1508. entt::registry registry{};
  1509. auto group = registry.group<int, char>();
  1510. const auto entity = registry.create();
  1511. registry.emplace<int>(entity, 0);
  1512. registry.emplace<char>(entity, 'c');
  1513. ASSERT_EQ(group.size(), 1u);
  1514. registry.erase<char>(entity);
  1515. ASSERT_EQ(group.size(), 0u);
  1516. registry.emplace<char>(entity, 'c');
  1517. ASSERT_EQ(group.size(), 1u);
  1518. registry.clear<int>();
  1519. ASSERT_EQ(group.size(), 0u);
  1520. registry.emplace<int>(entity, 0);
  1521. ASSERT_EQ(group.size(), 1u);
  1522. registry.clear();
  1523. ASSERT_EQ(group.size(), 0u);
  1524. }
  1525. TEST(Registry, CleanPartialOwningGroupViewAfterRemoveAndClear) {
  1526. entt::registry registry{};
  1527. auto group = registry.group<int>(entt::get<char>);
  1528. const auto entity = registry.create();
  1529. registry.emplace<int>(entity, 0);
  1530. registry.emplace<char>(entity, 'c');
  1531. ASSERT_EQ(group.size(), 1u);
  1532. registry.erase<char>(entity);
  1533. ASSERT_EQ(group.size(), 0u);
  1534. registry.emplace<char>(entity, 'c');
  1535. ASSERT_EQ(group.size(), 1u);
  1536. registry.clear<int>();
  1537. ASSERT_EQ(group.size(), 0u);
  1538. registry.emplace<int>(entity, 0);
  1539. ASSERT_EQ(group.size(), 1u);
  1540. registry.clear();
  1541. ASSERT_EQ(group.size(), 0u);
  1542. }
  1543. ENTT_DEBUG_TEST(RegistryDeathTest, NestedGroups) {
  1544. entt::registry registry{};
  1545. registry.group<int, double>(entt::get<char>);
  1546. ASSERT_DEATH(registry.group<int>(entt::get<char>), "");
  1547. ASSERT_DEATH(registry.group<int>(entt::get<char, double>), "");
  1548. ASSERT_DEATH(registry.group<int>(entt::get<char>, entt::exclude<double>), "");
  1549. ASSERT_DEATH((registry.group<int, double>()), "");
  1550. }
  1551. ENTT_DEBUG_TEST(RegistryDeathTest, ConflictingGroups) {
  1552. entt::registry registry{};
  1553. registry.group<char>(entt::get<int>, entt::exclude<double>);
  1554. ASSERT_DEATH(registry.group<char>(entt::get<float>, entt::exclude<double>), "");
  1555. }
  1556. TEST(Registry, GroupIfExists) {
  1557. entt::registry registry{};
  1558. const auto entity = registry.create();
  1559. auto group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1560. ASSERT_FALSE(group);
  1561. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1562. ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
  1563. ASSERT_EQ(std::as_const(registry).storage<double>(), nullptr);
  1564. registry.emplace<int>(entity);
  1565. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1566. ASSERT_FALSE(group);
  1567. registry.emplace<char>(entity);
  1568. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1569. ASSERT_FALSE(group);
  1570. registry.emplace<double>(entity);
  1571. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1572. ASSERT_FALSE(group);
  1573. registry.group<int>(entt::get<char>, entt::exclude<double>);
  1574. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1575. ASSERT_TRUE(group);
  1576. }
  1577. TEST(Registry, SortSingle) {
  1578. entt::registry registry{};
  1579. int val = 0;
  1580. registry.emplace<int>(registry.create(), val++);
  1581. registry.emplace<int>(registry.create(), val++);
  1582. registry.emplace<int>(registry.create(), val++);
  1583. for(auto entity: registry.view<int>()) {
  1584. ASSERT_EQ(registry.get<int>(entity), --val);
  1585. }
  1586. registry.sort<int>(std::less{});
  1587. for(auto entity: registry.view<int>()) {
  1588. ASSERT_EQ(registry.get<int>(entity), val++);
  1589. }
  1590. }
  1591. TEST(Registry, SortMulti) {
  1592. entt::registry registry{};
  1593. unsigned int uval = 0u;
  1594. int ival = 0;
  1595. for(auto i = 0; i < 3; ++i) {
  1596. const auto entity = registry.create();
  1597. registry.emplace<unsigned int>(entity, uval++);
  1598. registry.emplace<int>(entity, ival++);
  1599. }
  1600. for(auto entity: registry.view<unsigned int>()) {
  1601. ASSERT_EQ(registry.get<unsigned int>(entity), --uval);
  1602. }
  1603. for(auto entity: registry.view<int>()) {
  1604. ASSERT_EQ(registry.get<int>(entity), --ival);
  1605. }
  1606. registry.sort<unsigned int>(std::less{});
  1607. registry.sort<int, unsigned int>();
  1608. for(auto entity: registry.view<unsigned int>()) {
  1609. ASSERT_EQ(registry.get<unsigned int>(entity), uval++);
  1610. }
  1611. for(auto entity: registry.view<int>()) {
  1612. ASSERT_EQ(registry.get<int>(entity), ival++);
  1613. }
  1614. }
  1615. TEST(Registry, SortEmpty) {
  1616. entt::registry registry{};
  1617. registry.emplace<test::empty>(registry.create());
  1618. registry.emplace<test::empty>(registry.create());
  1619. registry.emplace<test::empty>(registry.create());
  1620. ASSERT_LT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
  1621. ASSERT_LT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
  1622. registry.sort<test::empty>(std::less{});
  1623. ASSERT_GT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
  1624. ASSERT_GT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
  1625. }
  1626. TEST(Registry, Context) {
  1627. entt::registry registry{};
  1628. auto &ctx = registry.ctx();
  1629. const auto &cctx = std::as_const(registry).ctx();
  1630. ASSERT_FALSE(ctx.contains<char>());
  1631. ASSERT_FALSE(cctx.contains<const int>());
  1632. ASSERT_EQ(ctx.find<char>(), nullptr);
  1633. ASSERT_EQ(cctx.find<const int>(), nullptr);
  1634. ctx.emplace<char>();
  1635. ctx.emplace<int>();
  1636. ASSERT_TRUE(ctx.contains<char>());
  1637. ASSERT_TRUE(cctx.contains<int>());
  1638. ASSERT_NE(ctx.find<const char>(), nullptr);
  1639. ASSERT_NE(cctx.find<const int>(), nullptr);
  1640. ASSERT_FALSE(ctx.erase<double>());
  1641. ASSERT_TRUE(ctx.erase<int>());
  1642. ASSERT_TRUE(ctx.contains<const char>());
  1643. ASSERT_FALSE(cctx.contains<const int>());
  1644. ASSERT_NE(ctx.find<char>(), nullptr);
  1645. ASSERT_EQ(cctx.find<int>(), nullptr);
  1646. ASSERT_FALSE(ctx.erase<int>());
  1647. ASSERT_TRUE(ctx.erase<char>());
  1648. ctx.emplace<char>('c');
  1649. ctx.emplace<int>(1);
  1650. ASSERT_EQ(ctx.emplace<char>('a'), 'c');
  1651. ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
  1652. ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
  1653. ASSERT_EQ(ctx.get<char>(), 'c');
  1654. ASSERT_EQ(ctx.emplace<const int>(0), 1);
  1655. ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
  1656. ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
  1657. ASSERT_EQ(ctx.get<int>(), 1);
  1658. ASSERT_EQ(ctx.find<double>(), nullptr);
  1659. ASSERT_EQ(cctx.find<double>(), nullptr);
  1660. ASSERT_EQ(ctx.insert_or_assign<char>('a'), 'a');
  1661. ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
  1662. ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
  1663. ASSERT_EQ(ctx.get<const char>(), 'a');
  1664. ASSERT_EQ(ctx.insert_or_assign<const int>(0), 0);
  1665. ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
  1666. ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
  1667. ASSERT_EQ(ctx.get<int>(), 0);
  1668. }
  1669. TEST(Registry, ContextHint) {
  1670. using namespace entt::literals;
  1671. entt::registry registry{};
  1672. auto &ctx = registry.ctx();
  1673. const auto &cctx = std::as_const(registry).ctx();
  1674. ctx.emplace<int>(1);
  1675. ctx.emplace_as<int>("other"_hs, 3);
  1676. ASSERT_TRUE(ctx.contains<int>());
  1677. ASSERT_TRUE(cctx.contains<const int>("other"_hs));
  1678. ASSERT_FALSE(ctx.contains<char>("other"_hs));
  1679. ASSERT_NE(cctx.find<const int>(), nullptr);
  1680. ASSERT_NE(ctx.find<int>("other"_hs), nullptr);
  1681. ASSERT_EQ(cctx.find<const char>("other"_hs), nullptr);
  1682. ASSERT_EQ(ctx.get<int>(), 1);
  1683. ASSERT_EQ(cctx.get<const int>("other"_hs), 3);
  1684. ctx.insert_or_assign(3);
  1685. ctx.insert_or_assign("other"_hs, 1);
  1686. ASSERT_EQ(ctx.get<const int>(), 3);
  1687. ASSERT_EQ(cctx.get<int>("other"_hs), 1);
  1688. ASSERT_FALSE(ctx.erase<char>("other"_hs));
  1689. ASSERT_TRUE(ctx.erase<int>());
  1690. ASSERT_TRUE(cctx.contains<int>("other"_hs));
  1691. ASSERT_EQ(ctx.get<int>("other"_hs), 1);
  1692. ASSERT_TRUE(ctx.erase<int>("other"_hs));
  1693. ASSERT_FALSE(cctx.contains<int>("other"_hs));
  1694. ASSERT_EQ(ctx.find<int>("other"_hs), nullptr);
  1695. }
  1696. TEST(Registry, ContextAsRef) {
  1697. entt::registry registry{};
  1698. int value{3};
  1699. registry.ctx().emplace<int &>(value);
  1700. ASSERT_NE(registry.ctx().find<int>(), nullptr);
  1701. ASSERT_NE(registry.ctx().find<const int>(), nullptr);
  1702. ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
  1703. ASSERT_EQ(registry.ctx().get<const int>(), value);
  1704. ASSERT_EQ(registry.ctx().get<int>(), value);
  1705. registry.ctx().get<int>() = 2;
  1706. ASSERT_EQ(value, 2);
  1707. ASSERT_EQ(registry.ctx().get<int>(), value);
  1708. value = 1;
  1709. ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
  1710. }
  1711. TEST(Registry, ContextAsConstRef) {
  1712. entt::registry registry{};
  1713. int value{3};
  1714. registry.ctx().emplace<const int &>(value);
  1715. ASSERT_EQ(registry.ctx().find<int>(), nullptr);
  1716. ASSERT_NE(registry.ctx().find<const int>(), nullptr);
  1717. ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
  1718. ASSERT_EQ(registry.ctx().get<const int>(), value);
  1719. value = 1;
  1720. ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
  1721. }
  1722. TEST(Registry, ContextPoolMemberDestructionOrder) {
  1723. auto registry = std::make_unique<entt::registry>();
  1724. const auto entity = registry->create();
  1725. bool ctx_check = false;
  1726. registry->ctx().emplace<typename destruction_order::ctx_check_type>();
  1727. registry->emplace<destruction_order>(entity, *registry, ctx_check);
  1728. registry.reset();
  1729. ASSERT_TRUE(ctx_check);
  1730. }
  1731. TEST(Registry, Constness) {
  1732. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<int>({})), int &>();
  1733. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<test::empty>({})), void>();
  1734. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<>({})), std::tuple<>>();
  1735. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int>({})), int &>();
  1736. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int, const char>({})), std::tuple<int &, const char &>>();
  1737. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<>({})), std::tuple<>>();
  1738. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int>({})), int *>();
  1739. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int, const char>({})), std::tuple<int *, const char *>>();
  1740. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<int>()), int &>();
  1741. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<const char>()), const char &>();
  1742. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<int>()), int *>();
  1743. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<const char>()), const char *>();
  1744. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<>({})), std::tuple<>>();
  1745. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int>({})), const int &>();
  1746. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int, const char>({})), std::tuple<const int &, const char &>>();
  1747. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<>({})), std::tuple<>>();
  1748. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int>({})), const int *>();
  1749. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int, const char>({})), std::tuple<const int *, const char *>>();
  1750. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<int>()), const int &>();
  1751. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<const char>()), const char &>();
  1752. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<int>()), const int *>();
  1753. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<const char>()), const char *>();
  1754. }
  1755. TEST(Registry, AssignEntities) {
  1756. using traits_type = entt::entt_traits<entt::entity>;
  1757. entt::registry registry{};
  1758. std::array<entt::entity, 3u> entity{};
  1759. registry.create(entity.begin(), entity.end());
  1760. registry.destroy(entity[1]);
  1761. registry.destroy(entity[2]);
  1762. entt::registry other{};
  1763. auto &src = registry.storage<entt::entity>();
  1764. auto &dst = other.storage<entt::entity>();
  1765. dst.push(src.rbegin(), src.rend());
  1766. dst.free_list(src.free_list());
  1767. ASSERT_EQ(registry.storage<entt::entity>().size(), other.storage<entt::entity>().size());
  1768. ASSERT_TRUE(other.valid(entity[0]));
  1769. ASSERT_FALSE(other.valid(entity[1]));
  1770. ASSERT_FALSE(other.valid(entity[2]));
  1771. ASSERT_EQ(registry.create(), other.create());
  1772. ASSERT_EQ(traits_type::to_entity(other.create()), traits_type::to_integral(entity[1]));
  1773. }
  1774. TEST(Registry, ScramblingPoolsIsAllowed) {
  1775. entt::registry registry{};
  1776. registry.on_destroy<int>().connect<&listener::sort<int>>();
  1777. for(std::size_t i{}; i < 2u; ++i) {
  1778. const auto entity = registry.create();
  1779. registry.emplace<int>(entity, static_cast<int>(i));
  1780. }
  1781. registry.destroy(registry.view<int>().back());
  1782. // thanks to @andranik3949 for pointing out this missing test
  1783. registry.view<const int>().each([](const auto entity, const auto &value) {
  1784. ASSERT_EQ(static_cast<int>(entt::to_integral(entity)), value);
  1785. });
  1786. }
  1787. TEST(Registry, AssureMixinLoop) {
  1788. entt::registry registry{};
  1789. const auto entity = registry.create();
  1790. ASSERT_EQ(std::as_const(registry).storage<test::assure_loop>(), nullptr);
  1791. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1792. registry.emplace<test::assure_loop>(entity);
  1793. ASSERT_NE(std::as_const(registry).storage<test::assure_loop>(), nullptr);
  1794. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1795. ASSERT_TRUE(registry.all_of<test::assure_loop>(entity));
  1796. ASSERT_FALSE(registry.all_of<int>(entity));
  1797. }
  1798. TEST(Registry, VoidType) {
  1799. using namespace entt::literals;
  1800. entt::registry registry{};
  1801. const auto entity = registry.create();
  1802. auto &storage = registry.storage<void>("custom"_hs);
  1803. storage.emplace(entity);
  1804. ASSERT_TRUE(registry.storage<void>().empty());
  1805. ASSERT_FALSE(registry.storage<void>("custom"_hs).empty());
  1806. ASSERT_TRUE(registry.storage<void>("custom"_hs).contains(entity));
  1807. ASSERT_EQ(registry.storage<void>().type(), entt::type_id<void>());
  1808. ASSERT_EQ(registry.storage<void>("custom"_hs).type(), entt::type_id<void>());
  1809. }
  1810. TEST(Registry, NoEtoType) {
  1811. entt::registry registry{};
  1812. const auto entity = registry.create();
  1813. registry.emplace<no_eto_type>(entity);
  1814. registry.emplace<int>(entity, 1);
  1815. ASSERT_NE(registry.storage<no_eto_type>().raw(), nullptr);
  1816. ASSERT_NE(registry.try_get<no_eto_type>(entity), nullptr);
  1817. ASSERT_EQ(registry.view<no_eto_type>().get(entity), std::as_const(registry).view<const no_eto_type>().get(entity));
  1818. auto view = registry.view<no_eto_type, int>();
  1819. auto cview = std::as_const(registry).view<const no_eto_type, const int>();
  1820. ASSERT_EQ((std::get<0>(view.get<no_eto_type, int>(entity))), (std::get<0>(cview.get<const no_eto_type, const int>(entity))));
  1821. }