registry.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  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, EmplaceOrReplaceAggregate) {
  805. entt::registry registry{};
  806. const auto entity = registry.create();
  807. auto &instance = registry.emplace_or_replace<test::aggregate>(entity, 1);
  808. ASSERT_EQ(instance.value, 1);
  809. }
  810. ENTT_DEBUG_TEST(RegistryDeathTest, EmplaceOrReplace) {
  811. entt::registry registry{};
  812. const auto entity = registry.create();
  813. registry.destroy(entity);
  814. ASSERT_DEATH(registry.emplace_or_replace<int>(entity), "");
  815. }
  816. TEST(Registry, Patch) {
  817. entt::registry registry{};
  818. const auto entity = registry.create();
  819. registry.emplace<int>(entity, 3);
  820. ASSERT_EQ(registry.get<int>(entity), 3);
  821. registry.patch<int>(entity);
  822. ASSERT_EQ(registry.get<int>(entity), 3);
  823. registry.patch<int>(entity, [](auto &value) { value = 1; });
  824. ASSERT_EQ(registry.get<int>(entity), 1);
  825. }
  826. TEST(Registry, Replace) {
  827. entt::registry registry{};
  828. const auto entity = registry.create();
  829. registry.emplace<int>(entity, 3);
  830. ASSERT_EQ(registry.get<int>(entity), 3);
  831. registry.replace<int>(entity);
  832. ASSERT_EQ(registry.get<int>(entity), 0);
  833. registry.replace<int>(entity, 1);
  834. ASSERT_EQ(registry.get<int>(entity), 1);
  835. }
  836. TEST(Registry, ReplaceAggregate) {
  837. entt::registry registry{};
  838. const auto entity = registry.create();
  839. registry.emplace<test::aggregate>(entity, 0);
  840. auto &instance = registry.replace<test::aggregate>(entity, 1);
  841. ASSERT_EQ(instance.value, 1);
  842. }
  843. TEST(Registry, Remove) {
  844. entt::registry registry{};
  845. const auto iview = registry.view<int>();
  846. const auto icview = registry.view<int, char>();
  847. std::array<entt::entity, 3u> entity{};
  848. registry.create(entity.begin(), entity.end());
  849. registry.emplace<int>(entity[0u]);
  850. registry.emplace<char>(entity[0u]);
  851. registry.emplace<double>(entity[0u]);
  852. registry.emplace<int>(entity[1u]);
  853. registry.emplace<char>(entity[1u]);
  854. registry.emplace<int>(entity[2u]);
  855. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  856. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  857. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  858. registry.remove<int, char>(entity[0u]);
  859. ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 2u);
  860. ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 0u);
  861. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  862. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  863. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  864. ASSERT_EQ(registry.storage<int>().size(), 1u);
  865. ASSERT_EQ(registry.storage<char>().size(), 0u);
  866. ASSERT_EQ(registry.storage<double>().size(), 1u);
  867. ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
  868. ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
  869. ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
  870. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  871. ASSERT_EQ(registry.remove<int>(iview.begin(), iview.end()), 0u);
  872. ASSERT_EQ(registry.storage<int>().size(), 0u);
  873. ASSERT_EQ(registry.storage<char>().size(), 0u);
  874. ASSERT_EQ(registry.storage<double>().size(), 1u);
  875. registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
  876. registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
  877. ASSERT_EQ(registry.storage<int>().size(), 1u);
  878. ASSERT_EQ(registry.storage<char>().size(), 1u);
  879. registry.remove<int, char>(iview.begin(), iview.end());
  880. registry.remove<int, char>(iview.begin(), iview.end());
  881. ASSERT_EQ(registry.storage<int>().size(), 0u);
  882. ASSERT_EQ(registry.storage<char>().size(), 0u);
  883. registry.insert<int>(entity.begin(), entity.end());
  884. registry.insert<char>(entity.begin(), entity.end());
  885. ASSERT_EQ(registry.storage<int>().size(), 3u);
  886. ASSERT_EQ(registry.storage<char>().size(), 3u);
  887. registry.remove<int, char>(entity.begin(), entity.end());
  888. registry.remove<int, char>(entity.begin(), entity.end());
  889. ASSERT_EQ(registry.storage<int>().size(), 0u);
  890. ASSERT_EQ(registry.storage<char>().size(), 0u);
  891. ASSERT_FALSE(registry.orphan(entity[0u]));
  892. ASSERT_TRUE(registry.orphan(entity[1u]));
  893. ASSERT_TRUE(registry.orphan(entity[2u]));
  894. }
  895. TEST(Registry, StableRemove) {
  896. entt::registry registry{};
  897. const auto iview = registry.view<int>();
  898. const auto icview = registry.view<int, test::pointer_stable>();
  899. std::array<entt::entity, 3u> entity{};
  900. registry.create(entity.begin(), entity.end());
  901. registry.emplace<int>(entity[0u]);
  902. registry.emplace<test::pointer_stable>(entity[0u]);
  903. registry.emplace<double>(entity[0u]);
  904. registry.emplace<int>(entity[1u]);
  905. registry.emplace<test::pointer_stable>(entity[1u]);
  906. registry.emplace<int>(entity[2u]);
  907. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  908. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  909. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  910. registry.remove<int, test::pointer_stable>(entity[0u]);
  911. ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 2u);
  912. ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 0u);
  913. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  914. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  915. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  916. ASSERT_EQ(registry.storage<int>().size(), 1u);
  917. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  918. ASSERT_EQ(registry.storage<double>().size(), 1u);
  919. ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
  920. ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
  921. ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
  922. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  923. ASSERT_EQ(registry.storage<int>().size(), 0u);
  924. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  925. ASSERT_EQ(registry.storage<double>().size(), 1u);
  926. }
  927. TEST(Registry, Erase) {
  928. entt::registry registry{};
  929. const auto iview = registry.view<int>();
  930. const auto icview = registry.view<int, char>();
  931. std::array<entt::entity, 3u> entity{};
  932. registry.create(entity.begin(), entity.end());
  933. registry.emplace<int>(entity[0u]);
  934. registry.emplace<char>(entity[0u]);
  935. registry.emplace<double>(entity[0u]);
  936. registry.emplace<int>(entity[1u]);
  937. registry.emplace<char>(entity[1u]);
  938. registry.emplace<int>(entity[2u]);
  939. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  940. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  941. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  942. registry.erase<int, char>(entity[0u]);
  943. registry.erase<int, char>(icview.begin(), icview.end());
  944. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  945. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  946. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  947. ASSERT_EQ(registry.storage<int>().size(), 1u);
  948. ASSERT_EQ(registry.storage<char>().size(), 0u);
  949. ASSERT_EQ(registry.storage<double>().size(), 1u);
  950. registry.erase<int>(iview.begin(), iview.end());
  951. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  952. ASSERT_NO_THROW(registry.erase<int>(iview.rbegin(), iview.rend()));
  953. ASSERT_EQ(registry.storage<int>().size(), 0u);
  954. ASSERT_EQ(registry.storage<char>().size(), 0u);
  955. ASSERT_EQ(registry.storage<double>().size(), 1u);
  956. registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
  957. registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
  958. ASSERT_EQ(registry.storage<int>().size(), 1u);
  959. ASSERT_EQ(registry.storage<char>().size(), 1u);
  960. registry.erase<int, char>(iview.begin(), iview.end());
  961. ASSERT_EQ(registry.storage<int>().size(), 0u);
  962. ASSERT_EQ(registry.storage<char>().size(), 0u);
  963. registry.insert<int>(entity.begin(), entity.end());
  964. registry.insert<char>(entity.begin(), entity.end());
  965. ASSERT_EQ(registry.storage<int>().size(), 3u);
  966. ASSERT_EQ(registry.storage<char>().size(), 3u);
  967. registry.erase<int, char>(entity.begin(), entity.end());
  968. ASSERT_EQ(registry.storage<int>().size(), 0u);
  969. ASSERT_EQ(registry.storage<char>().size(), 0u);
  970. ASSERT_FALSE(registry.orphan(entity[0u]));
  971. ASSERT_TRUE(registry.orphan(entity[1u]));
  972. ASSERT_TRUE(registry.orphan(entity[2u]));
  973. }
  974. ENTT_DEBUG_TEST(RegistryDeathTest, Erase) {
  975. entt::registry registry{};
  976. const std::array entity{registry.create()};
  977. ASSERT_FALSE((registry.any_of<int>(entity[0u])));
  978. ASSERT_DEATH((registry.erase<int>(entity.begin(), entity.end())), "");
  979. ASSERT_DEATH(registry.erase<int>(entity[0u]), "");
  980. }
  981. TEST(Registry, StableErase) {
  982. entt::registry registry{};
  983. const auto iview = registry.view<int>();
  984. const auto icview = registry.view<int, test::pointer_stable>();
  985. std::array<entt::entity, 3u> entity{};
  986. registry.create(entity.begin(), entity.end());
  987. registry.emplace<int>(entity[0u]);
  988. registry.emplace<test::pointer_stable>(entity[0u]);
  989. registry.emplace<double>(entity[0u]);
  990. registry.emplace<int>(entity[1u]);
  991. registry.emplace<test::pointer_stable>(entity[1u]);
  992. registry.emplace<int>(entity[2u]);
  993. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  994. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  995. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  996. registry.erase<int, test::pointer_stable>(entity[0u]);
  997. registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
  998. registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
  999. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  1000. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  1001. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  1002. ASSERT_EQ(registry.storage<int>().size(), 1u);
  1003. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1004. ASSERT_EQ(registry.storage<double>().size(), 1u);
  1005. registry.erase<int>(iview.begin(), iview.end());
  1006. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  1007. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1008. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1009. ASSERT_EQ(registry.storage<double>().size(), 1u);
  1010. }
  1011. TEST(Registry, EraseIf) {
  1012. using namespace entt::literals;
  1013. entt::registry registry{};
  1014. const auto entity = registry.create();
  1015. registry.emplace<int>(entity);
  1016. registry.storage<int>("other"_hs).emplace(entity);
  1017. registry.emplace<char>(entity);
  1018. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1019. ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
  1020. ASSERT_TRUE(registry.storage<char>().contains(entity));
  1021. registry.erase_if(entity, [](auto &&...) { return false; });
  1022. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1023. ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
  1024. ASSERT_TRUE(registry.storage<char>().contains(entity));
  1025. registry.erase_if(entity, [](entt::id_type id, auto &&...) { return id == "other"_hs; });
  1026. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1027. ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
  1028. ASSERT_TRUE(registry.storage<char>().contains(entity));
  1029. registry.erase_if(entity, [](auto, const auto &storage) { return storage.type() == entt::type_id<char>(); });
  1030. ASSERT_TRUE(registry.storage<int>().contains(entity));
  1031. ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
  1032. ASSERT_FALSE(registry.storage<char>().contains(entity));
  1033. }
  1034. TEST(Registry, Compact) {
  1035. entt::registry registry{};
  1036. std::array<entt::entity, 2u> entity{};
  1037. registry.create(entity.begin(), entity.end());
  1038. registry.emplace<int>(entity[0u]);
  1039. registry.emplace<test::pointer_stable>(entity[0u]);
  1040. registry.emplace<int>(entity[1u]);
  1041. registry.emplace<test::pointer_stable>(entity[1u]);
  1042. ASSERT_EQ(registry.storage<int>().size(), 2u);
  1043. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1044. registry.destroy(entity.begin(), entity.end());
  1045. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1046. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1047. registry.compact<int>();
  1048. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1049. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1050. registry.compact();
  1051. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1052. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 0u);
  1053. }
  1054. TEST(Registry, AllAnyOf) {
  1055. entt::registry registry{};
  1056. const auto entity = registry.create();
  1057. ASSERT_FALSE(registry.all_of<int>(entity));
  1058. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  1059. ASSERT_FALSE((registry.any_of<int, char>(entity)));
  1060. registry.emplace<int>(entity);
  1061. ASSERT_TRUE(registry.all_of<int>(entity));
  1062. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  1063. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  1064. registry.emplace<char>(entity);
  1065. ASSERT_TRUE(registry.all_of<int>(entity));
  1066. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  1067. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  1068. }
  1069. TEST(Registry, Get) {
  1070. entt::registry registry{};
  1071. const auto entity = registry.create();
  1072. registry.emplace<int>(entity, 1);
  1073. registry.emplace<char>(entity, 'c');
  1074. ASSERT_EQ(std::as_const(registry).get<int>(entity), 1);
  1075. ASSERT_EQ((std::as_const(registry).get<int, char>(entity)), std::make_tuple(1, 'c'));
  1076. registry.get<int>(entity) = 3;
  1077. std::get<1>(registry.get<int, char>(entity)) = 'a';
  1078. ASSERT_EQ(registry.get<int>(entity), 3);
  1079. ASSERT_EQ((registry.get<int, char>(entity)), std::make_tuple(3, 'a'));
  1080. }
  1081. TEST(Registry, GetOrEmplace) {
  1082. entt::registry registry{};
  1083. const auto entity = registry.create();
  1084. const auto value = 3;
  1085. const auto other = 1.;
  1086. ASSERT_EQ(registry.get_or_emplace<int>(entity, value), value);
  1087. ASSERT_EQ(registry.get_or_emplace<double>(entity, other), other);
  1088. ASSERT_TRUE((registry.all_of<int, double>(entity)));
  1089. ASSERT_EQ(registry.get<int>(entity), value);
  1090. ASSERT_EQ(registry.get<double>(entity), other);
  1091. }
  1092. TEST(Registry, GetOrEmplaceEmpty) {
  1093. entt::registry registry{};
  1094. const auto entity = registry.create();
  1095. // get_or_emplace must work for empty types
  1096. // NOLINTNEXTLINE(readability-redundant-casting)
  1097. static_cast<void>(registry.get_or_emplace<test::empty>(entity));
  1098. ASSERT_TRUE((registry.all_of<test::empty>(entity)));
  1099. }
  1100. ENTT_DEBUG_TEST(RegistryDeathTest, GetOrEmplace) {
  1101. entt::registry registry{};
  1102. const auto entity = registry.create();
  1103. registry.destroy(entity);
  1104. ASSERT_DEATH([[maybe_unused]] const auto value = registry.get_or_emplace<int>(entity), "");
  1105. }
  1106. TEST(Registry, TryGet) {
  1107. entt::registry registry{};
  1108. const auto entity = registry.create();
  1109. ASSERT_EQ(registry.try_get<int>(entity), nullptr);
  1110. ASSERT_EQ(std::as_const(registry).try_get<int>(entity), nullptr);
  1111. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1112. const int &elem = registry.emplace<int>(entity);
  1113. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1114. ASSERT_EQ(registry.try_get<int>(entity), &elem);
  1115. ASSERT_EQ(std::as_const(registry).try_get<int>(entity), &elem);
  1116. }
  1117. TEST(Registry, Clear) {
  1118. entt::registry registry{};
  1119. const std::array entity{registry.create(), registry.create()};
  1120. registry.insert<int>(entity.begin(), entity.end());
  1121. registry.insert<char>(entity.begin(), entity.end());
  1122. ASSERT_TRUE((registry.all_of<int, char>(entity[0])));
  1123. ASSERT_TRUE((registry.all_of<int, char>(entity[1])));
  1124. registry.clear<int>();
  1125. ASSERT_TRUE(registry.valid(entity[0]));
  1126. ASSERT_TRUE(registry.valid(entity[1]));
  1127. ASSERT_FALSE(registry.all_of<int>(entity[0]));
  1128. ASSERT_FALSE(registry.all_of<int>(entity[1]));
  1129. ASSERT_TRUE(registry.all_of<char>(entity[0]));
  1130. ASSERT_TRUE(registry.all_of<char>(entity[1]));
  1131. registry.clear();
  1132. ASSERT_FALSE(registry.valid(entity[0]));
  1133. ASSERT_FALSE(registry.valid(entity[1]));
  1134. ASSERT_TRUE(registry.storage<int>().empty());
  1135. ASSERT_TRUE(registry.storage<char>().empty());
  1136. }
  1137. TEST(Registry, Orphan) {
  1138. entt::registry registry{};
  1139. std::array<entt::entity, 3u> entity{};
  1140. registry.create(entity.begin(), entity.end());
  1141. registry.emplace<int>(entity[0u]);
  1142. registry.emplace<int>(entity[2u]);
  1143. for(auto [entt]: registry.storage<entt::entity>().each()) {
  1144. ASSERT_TRUE(entt != entity[1u] || registry.orphan(entt));
  1145. }
  1146. registry.erase<int>(entity[0u]);
  1147. registry.erase<int>(entity[2u]);
  1148. for(auto [entt]: registry.storage<entt::entity>().each()) {
  1149. ASSERT_TRUE(registry.orphan(entt));
  1150. }
  1151. }
  1152. TEST(Registry, Signals) {
  1153. entt::registry registry{};
  1154. std::array<entt::entity, 2u> entity{};
  1155. listener listener;
  1156. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  1157. registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
  1158. registry.on_construct<int>().connect<&listener::incr>(listener);
  1159. registry.on_destroy<int>().connect<&listener::decr>(listener);
  1160. registry.create(entity.begin(), entity.end());
  1161. registry.insert<test::empty>(entity.begin(), entity.end());
  1162. ASSERT_EQ(listener.counter, 2);
  1163. ASSERT_EQ(listener.last, entity[1u]);
  1164. registry.insert<int>(entity.rbegin(), entity.rend());
  1165. ASSERT_EQ(listener.counter, 4);
  1166. ASSERT_EQ(listener.last, entity[0u]);
  1167. registry.erase<test::empty, int>(entity[0u]);
  1168. ASSERT_EQ(listener.counter, 2);
  1169. ASSERT_EQ(listener.last, entity[0u]);
  1170. registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
  1171. registry.on_destroy<int>().disconnect<&listener::decr>(listener);
  1172. registry.erase<test::empty, int>(entity[1u]);
  1173. ASSERT_EQ(listener.counter, 2);
  1174. ASSERT_EQ(listener.last, entity[0u]);
  1175. registry.on_construct<test::empty>().disconnect<&listener::incr>(listener);
  1176. registry.on_construct<int>().disconnect<&listener::incr>(listener);
  1177. registry.emplace<test::empty>(entity[1u]);
  1178. registry.emplace<int>(entity[1u]);
  1179. ASSERT_EQ(listener.counter, 2);
  1180. ASSERT_EQ(listener.last, entity[0u]);
  1181. registry.on_construct<int>().connect<&listener::incr>(listener);
  1182. registry.on_destroy<int>().connect<&listener::decr>(listener);
  1183. registry.emplace<int>(entity[0u]);
  1184. registry.erase<int>(entity[1u]);
  1185. ASSERT_EQ(listener.counter, 2);
  1186. ASSERT_EQ(listener.last, entity[1u]);
  1187. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  1188. registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
  1189. registry.erase<test::empty>(entity[1u]);
  1190. registry.emplace<test::empty>(entity[0u]);
  1191. ASSERT_EQ(listener.counter, 2);
  1192. ASSERT_EQ(listener.last, entity[0u]);
  1193. registry.clear<test::empty, int>();
  1194. ASSERT_EQ(listener.counter, 0);
  1195. ASSERT_EQ(listener.last, entity[0u]);
  1196. registry.insert<test::empty>(entity.begin(), entity.end());
  1197. registry.insert<int>(entity.begin(), entity.end());
  1198. registry.destroy(entity[1u]);
  1199. ASSERT_EQ(listener.counter, 2);
  1200. ASSERT_EQ(listener.last, entity[1u]);
  1201. registry.erase<int, test::empty>(entity[0u]);
  1202. registry.emplace_or_replace<int>(entity[0u]);
  1203. registry.emplace_or_replace<test::empty>(entity[0u]);
  1204. ASSERT_EQ(listener.counter, 2);
  1205. ASSERT_EQ(listener.last, entity[0u]);
  1206. registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
  1207. registry.on_destroy<int>().disconnect<&listener::decr>(listener);
  1208. registry.emplace_or_replace<test::empty>(entity[0u]);
  1209. registry.emplace_or_replace<int>(entity[0u]);
  1210. ASSERT_EQ(listener.counter, 2);
  1211. ASSERT_EQ(listener.last, entity[0u]);
  1212. registry.on_update<test::empty>().connect<&listener::incr>(listener);
  1213. registry.on_update<int>().connect<&listener::incr>(listener);
  1214. registry.emplace_or_replace<test::empty>(entity[0u]);
  1215. registry.emplace_or_replace<int>(entity[0u]);
  1216. ASSERT_EQ(listener.counter, 4);
  1217. ASSERT_EQ(listener.last, entity[0u]);
  1218. registry.replace<test::empty>(entity[0u]);
  1219. registry.replace<int>(entity[0u]);
  1220. ASSERT_EQ(listener.counter, 6);
  1221. ASSERT_EQ(listener.last, entity[0u]);
  1222. }
  1223. TEST(Registry, SignalsOnRuntimePool) {
  1224. using namespace entt::literals;
  1225. entt::registry registry{};
  1226. const auto entity = registry.create();
  1227. listener listener;
  1228. registry.on_construct<int>("custom"_hs).connect<&listener::incr>(listener);
  1229. registry.on_update<int>("custom"_hs).connect<&listener::incr>(listener);
  1230. registry.on_destroy<int>("custom"_hs).connect<&listener::incr>(listener);
  1231. ASSERT_EQ(listener.counter, 0);
  1232. registry.emplace<int>(entity);
  1233. registry.patch<int>(entity);
  1234. registry.erase<int>(entity);
  1235. ASSERT_EQ(listener.counter, 0);
  1236. registry.storage<int>("custom"_hs).emplace(entity);
  1237. registry.storage<int>("custom"_hs).patch(entity);
  1238. registry.storage<int>("custom"_hs).erase(entity);
  1239. ASSERT_EQ(listener.counter, 3);
  1240. }
  1241. TEST(Registry, SignalsOnEntity) {
  1242. entt::registry registry{};
  1243. listener listener;
  1244. registry.on_construct<entt::entity>().connect<&listener::incr>(listener);
  1245. entt::entity entity = registry.create();
  1246. entt::entity other = registry.create();
  1247. ASSERT_EQ(listener.counter, 2);
  1248. ASSERT_EQ(listener.last, other);
  1249. registry.destroy(other);
  1250. registry.destroy(entity);
  1251. ASSERT_EQ(listener.counter, 2);
  1252. ASSERT_EQ(listener.last, other);
  1253. registry.on_construct<entt::entity>().disconnect(&listener);
  1254. other = registry.create();
  1255. entity = registry.create();
  1256. ASSERT_EQ(listener.counter, 2);
  1257. ASSERT_NE(listener.last, entity);
  1258. ASSERT_NE(listener.last, other);
  1259. registry.on_update<entt::entity>().connect<&listener::decr>(listener);
  1260. registry.patch<entt::entity>(entity);
  1261. ASSERT_EQ(listener.counter, 1);
  1262. ASSERT_EQ(listener.last, entity);
  1263. registry.on_update<entt::entity>().disconnect(&listener);
  1264. registry.patch<entt::entity>(other);
  1265. ASSERT_EQ(listener.counter, 1);
  1266. ASSERT_NE(listener.last, other);
  1267. registry.on_destroy<entt::entity>().connect<&listener::decr>(listener);
  1268. registry.destroy(entity);
  1269. ASSERT_EQ(listener.counter, 0);
  1270. ASSERT_EQ(listener.last, entity);
  1271. registry.on_destroy<entt::entity>().disconnect(&listener);
  1272. registry.destroy(other);
  1273. ASSERT_EQ(listener.counter, 0);
  1274. ASSERT_NE(listener.last, other);
  1275. }
  1276. TEST(Registry, SignalWhenDestroying) {
  1277. entt::registry registry{};
  1278. const auto entity = registry.create();
  1279. registry.on_destroy<double>().connect<&entt::registry::remove<char>>();
  1280. registry.emplace<double>(entity);
  1281. registry.emplace<int>(entity);
  1282. ASSERT_NE(registry.storage(entt::type_id<double>().hash()), nullptr);
  1283. ASSERT_NE(registry.storage(entt::type_id<int>().hash()), nullptr);
  1284. ASSERT_EQ(registry.storage(entt::type_id<char>().hash()), nullptr);
  1285. ASSERT_TRUE(registry.valid(entity));
  1286. registry.destroy(entity);
  1287. ASSERT_NE(registry.storage(entt::type_id<char>().hash()), nullptr);
  1288. ASSERT_FALSE(registry.valid(entity));
  1289. }
  1290. TEST(Registry, SelfSignal) {
  1291. entt::registry registry{};
  1292. const auto entity = registry.create();
  1293. // required because of an issue of VS2019
  1294. constexpr auto emplace_or_replace = &entt::registry::emplace_or_replace<double>;
  1295. constexpr auto remove = &entt::registry::remove<double>;
  1296. registry.on_construct<int>().connect<emplace_or_replace>();
  1297. registry.on_destroy<int>().connect<remove>();
  1298. registry.emplace<double>(entity, .3);
  1299. ASSERT_FALSE(registry.all_of<int>(entity));
  1300. ASSERT_EQ(registry.get<double>(entity), .3);
  1301. registry.emplace<int>(entity);
  1302. ASSERT_TRUE(registry.all_of<int>(entity));
  1303. ASSERT_EQ(registry.get<double>(entity), .0);
  1304. registry.erase<int>(entity);
  1305. ASSERT_FALSE((registry.any_of<int, double>(entity)));
  1306. registry.on_construct<int>().disconnect<emplace_or_replace>();
  1307. registry.on_destroy<int>().disconnect<remove>();
  1308. registry.emplace<int>(entity);
  1309. ASSERT_TRUE((registry.any_of<int, double>(entity)));
  1310. ASSERT_FALSE(registry.all_of<double>(entity));
  1311. }
  1312. TEST(Registry, View) {
  1313. entt::registry registry{};
  1314. std::array<entt::entity, 3u> entity{};
  1315. auto iview = std::as_const(registry).view<int>();
  1316. auto cview = std::as_const(registry).view<char>();
  1317. ASSERT_FALSE(iview);
  1318. ASSERT_FALSE(cview);
  1319. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1320. ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
  1321. auto mview = registry.view<int, char>();
  1322. auto fview = registry.view<int>(entt::exclude<char>);
  1323. ASSERT_TRUE(mview);
  1324. ASSERT_TRUE(fview);
  1325. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1326. ASSERT_NE(std::as_const(registry).storage<char>(), nullptr);
  1327. iview = std::as_const(registry).view<int>();
  1328. cview = std::as_const(registry).view<char>();
  1329. ASSERT_TRUE(iview);
  1330. ASSERT_TRUE(cview);
  1331. registry.create(entity.begin(), entity.end());
  1332. registry.emplace<int>(entity[0u], 0);
  1333. registry.emplace<char>(entity[0u], 'c');
  1334. registry.emplace<int>(entity[1u], 0);
  1335. registry.emplace<int>(entity[2u], 0);
  1336. registry.emplace<char>(entity[2u], 'c');
  1337. ASSERT_EQ(iview.size(), 3u);
  1338. ASSERT_EQ(cview.size(), 2u);
  1339. ASSERT_EQ(mview.size_hint(), 3u);
  1340. ASSERT_EQ(fview.size_hint(), 3u);
  1341. mview.refresh();
  1342. ASSERT_EQ(mview.size_hint(), 2u);
  1343. ASSERT_EQ(fview.size_hint(), 3u);
  1344. ASSERT_NE(mview.begin(), mview.end());
  1345. ASSERT_NE(fview.begin(), fview.end());
  1346. ASSERT_EQ(std::distance(mview.begin(), mview.end()), 2);
  1347. ASSERT_EQ(std::distance(fview.begin(), fview.end()), 1);
  1348. mview.each([&entity, first = true](auto entt, auto &&...) mutable {
  1349. ASSERT_EQ(entt, first ? entity[2u] : entity[0u]);
  1350. first = false;
  1351. });
  1352. fview.each([&entity](auto entt, auto &&...) {
  1353. ASSERT_EQ(entt, entity[1u]);
  1354. });
  1355. }
  1356. TEST(Registry, ExcludeOnlyView) {
  1357. entt::registry registry{};
  1358. std::array<entt::entity, 4u> entity{};
  1359. auto view = registry.view<entt::entity>(entt::exclude<int>);
  1360. registry.create(entity.begin(), entity.end());
  1361. registry.emplace<int>(entity[0u], 0);
  1362. registry.emplace<int>(entity[2u], 0);
  1363. registry.emplace<int>(entity[3u], 0);
  1364. registry.destroy(entity[3u]);
  1365. ASSERT_EQ(view.size_hint(), 3u);
  1366. ASSERT_NE(view.begin(), view.end());
  1367. ASSERT_EQ(std::distance(view.begin(), view.end()), 1);
  1368. ASSERT_EQ(*view.begin(), entity[1u]);
  1369. for(auto [entt]: view.each()) {
  1370. ASSERT_EQ(entt, entity[1u]);
  1371. }
  1372. view.each([&entity](auto entt) {
  1373. ASSERT_EQ(entt, entity[1u]);
  1374. });
  1375. }
  1376. TEST(Registry, CleanViewAfterRemoveAndClear) {
  1377. entt::registry registry{};
  1378. auto view = registry.view<int, char>();
  1379. const auto entity = registry.create();
  1380. registry.emplace<int>(entity);
  1381. registry.emplace<char>(entity);
  1382. ASSERT_EQ(view.size_hint(), 1u);
  1383. registry.erase<char>(entity);
  1384. ASSERT_EQ(view.size_hint(), 1u);
  1385. registry.emplace<char>(entity);
  1386. ASSERT_EQ(view.size_hint(), 1u);
  1387. registry.clear<int>();
  1388. ASSERT_EQ(view.size_hint(), 0u);
  1389. registry.emplace<int>(entity);
  1390. ASSERT_EQ(view.size_hint(), 1u);
  1391. registry.clear();
  1392. ASSERT_EQ(view.size_hint(), 0u);
  1393. }
  1394. TEST(Registry, NonOwningGroupInitOnFirstUse) {
  1395. entt::registry registry{};
  1396. std::array<entt::entity, 3u> entity{};
  1397. registry.create(entity.begin(), entity.end());
  1398. registry.insert<int>(entity.begin(), entity.end(), 0);
  1399. registry.emplace<char>(entity[0u], 'c');
  1400. registry.emplace<char>(entity[2u], 'c');
  1401. std::size_t cnt{};
  1402. auto group = registry.group(entt::get<int, char>);
  1403. group.each([&cnt](auto...) { ++cnt; });
  1404. ASSERT_FALSE((registry.owned<int, char>()));
  1405. ASSERT_EQ(cnt, 2u);
  1406. }
  1407. TEST(Registry, NonOwningGroupInitOnEmplace) {
  1408. entt::registry registry{};
  1409. std::array<entt::entity, 3u> entity{};
  1410. auto group = registry.group(entt::get<int, char>);
  1411. registry.create(entity.begin(), entity.end());
  1412. registry.insert<int>(entity.begin(), entity.end(), 0);
  1413. registry.emplace<char>(entity[0u], 'c');
  1414. registry.emplace<char>(entity[2u], 'c');
  1415. std::size_t cnt{};
  1416. group.each([&cnt](auto...) { ++cnt; });
  1417. ASSERT_FALSE((registry.owned<int, char>()));
  1418. ASSERT_EQ(cnt, 2u);
  1419. }
  1420. TEST(Registry, FullOwningGroupInitOnFirstUse) {
  1421. entt::registry registry{};
  1422. std::array<entt::entity, 3u> entity{};
  1423. registry.create(entity.begin(), entity.end());
  1424. registry.insert<int>(entity.begin(), entity.end(), 0);
  1425. registry.emplace<char>(entity[0u], 'c');
  1426. registry.emplace<char>(entity[2u], 'c');
  1427. std::size_t cnt{};
  1428. auto group = registry.group<int, char>();
  1429. group.each([&cnt](auto...) { ++cnt; });
  1430. ASSERT_TRUE(registry.owned<int>());
  1431. ASSERT_TRUE(registry.owned<char>());
  1432. ASSERT_FALSE(registry.owned<double>());
  1433. ASSERT_EQ(cnt, 2u);
  1434. }
  1435. TEST(Registry, FullOwningGroupInitOnEmplace) {
  1436. entt::registry registry{};
  1437. std::array<entt::entity, 3u> entity{};
  1438. auto group = registry.group<int, char>();
  1439. registry.create(entity.begin(), entity.end());
  1440. registry.insert<int>(entity.begin(), entity.end(), 0);
  1441. registry.emplace<char>(entity[0u], 'c');
  1442. registry.emplace<char>(entity[2u], 'c');
  1443. std::size_t cnt{};
  1444. group.each([&cnt](auto...) { ++cnt; });
  1445. ASSERT_TRUE(registry.owned<int>());
  1446. ASSERT_TRUE(registry.owned<char>());
  1447. ASSERT_FALSE(registry.owned<double>());
  1448. ASSERT_EQ(cnt, 2u);
  1449. }
  1450. TEST(Registry, PartialOwningGroupInitOnFirstUse) {
  1451. entt::registry registry{};
  1452. std::array<entt::entity, 3u> entity{};
  1453. registry.create(entity.begin(), entity.end());
  1454. registry.insert<int>(entity.begin(), entity.end(), 0);
  1455. registry.emplace<char>(entity[0u], 'c');
  1456. registry.emplace<char>(entity[2u], 'c');
  1457. std::size_t cnt{};
  1458. auto group = registry.group<int>(entt::get<char>);
  1459. group.each([&cnt](auto...) { ++cnt; });
  1460. ASSERT_TRUE((registry.owned<int, char>()));
  1461. ASSERT_TRUE(registry.owned<int>());
  1462. ASSERT_FALSE(registry.owned<char>());
  1463. ASSERT_EQ(cnt, 2u);
  1464. }
  1465. TEST(Registry, PartialOwningGroupInitOnEmplace) {
  1466. entt::registry registry{};
  1467. std::array<entt::entity, 3u> entity{};
  1468. auto group = registry.group<int>(entt::get<char>);
  1469. registry.create(entity.begin(), entity.end());
  1470. registry.insert<int>(entity.begin(), entity.end(), 0);
  1471. registry.emplace<char>(entity[0u], 'c');
  1472. registry.emplace<char>(entity[2u], 'c');
  1473. std::size_t cnt{};
  1474. group.each([&cnt](auto...) { ++cnt; });
  1475. ASSERT_TRUE((registry.owned<int, char>()));
  1476. ASSERT_TRUE(registry.owned<int>());
  1477. ASSERT_FALSE(registry.owned<char>());
  1478. ASSERT_EQ(cnt, 2u);
  1479. }
  1480. TEST(Registry, CleanNonOwningGroupViewAfterRemoveAndClear) {
  1481. entt::registry registry{};
  1482. auto group = registry.group(entt::get<int, char>);
  1483. const auto entity = registry.create();
  1484. registry.emplace<int>(entity, 0);
  1485. registry.emplace<char>(entity, 'c');
  1486. ASSERT_EQ(group.size(), 1u);
  1487. registry.erase<char>(entity);
  1488. ASSERT_EQ(group.size(), 0u);
  1489. registry.emplace<char>(entity, 'c');
  1490. ASSERT_EQ(group.size(), 1u);
  1491. registry.clear<int>();
  1492. ASSERT_EQ(group.size(), 0u);
  1493. registry.emplace<int>(entity, 0);
  1494. ASSERT_EQ(group.size(), 1u);
  1495. registry.clear();
  1496. ASSERT_EQ(group.size(), 0u);
  1497. }
  1498. TEST(Registry, CleanFullOwningGroupViewAfterRemoveAndClear) {
  1499. entt::registry registry{};
  1500. auto group = registry.group<int, char>();
  1501. const auto entity = registry.create();
  1502. registry.emplace<int>(entity, 0);
  1503. registry.emplace<char>(entity, 'c');
  1504. ASSERT_EQ(group.size(), 1u);
  1505. registry.erase<char>(entity);
  1506. ASSERT_EQ(group.size(), 0u);
  1507. registry.emplace<char>(entity, 'c');
  1508. ASSERT_EQ(group.size(), 1u);
  1509. registry.clear<int>();
  1510. ASSERT_EQ(group.size(), 0u);
  1511. registry.emplace<int>(entity, 0);
  1512. ASSERT_EQ(group.size(), 1u);
  1513. registry.clear();
  1514. ASSERT_EQ(group.size(), 0u);
  1515. }
  1516. TEST(Registry, CleanPartialOwningGroupViewAfterRemoveAndClear) {
  1517. entt::registry registry{};
  1518. auto group = registry.group<int>(entt::get<char>);
  1519. const auto entity = registry.create();
  1520. registry.emplace<int>(entity, 0);
  1521. registry.emplace<char>(entity, 'c');
  1522. ASSERT_EQ(group.size(), 1u);
  1523. registry.erase<char>(entity);
  1524. ASSERT_EQ(group.size(), 0u);
  1525. registry.emplace<char>(entity, 'c');
  1526. ASSERT_EQ(group.size(), 1u);
  1527. registry.clear<int>();
  1528. ASSERT_EQ(group.size(), 0u);
  1529. registry.emplace<int>(entity, 0);
  1530. ASSERT_EQ(group.size(), 1u);
  1531. registry.clear();
  1532. ASSERT_EQ(group.size(), 0u);
  1533. }
  1534. ENTT_DEBUG_TEST(RegistryDeathTest, NestedGroups) {
  1535. entt::registry registry{};
  1536. registry.group<int, double>(entt::get<char>);
  1537. ASSERT_DEATH(registry.group<int>(entt::get<char>), "");
  1538. ASSERT_DEATH(registry.group<int>(entt::get<char, double>), "");
  1539. ASSERT_DEATH(registry.group<int>(entt::get<char>, entt::exclude<double>), "");
  1540. ASSERT_DEATH((registry.group<int, double>()), "");
  1541. }
  1542. ENTT_DEBUG_TEST(RegistryDeathTest, ConflictingGroups) {
  1543. entt::registry registry{};
  1544. registry.group<char>(entt::get<int>, entt::exclude<double>);
  1545. ASSERT_DEATH(registry.group<char>(entt::get<float>, entt::exclude<double>), "");
  1546. }
  1547. TEST(Registry, GroupIfExists) {
  1548. entt::registry registry{};
  1549. const auto entity = registry.create();
  1550. auto group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1551. ASSERT_FALSE(group);
  1552. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1553. ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
  1554. ASSERT_EQ(std::as_const(registry).storage<double>(), nullptr);
  1555. registry.emplace<int>(entity);
  1556. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1557. ASSERT_FALSE(group);
  1558. registry.emplace<char>(entity);
  1559. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1560. ASSERT_FALSE(group);
  1561. registry.emplace<double>(entity);
  1562. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1563. ASSERT_FALSE(group);
  1564. registry.group<int>(entt::get<char>, entt::exclude<double>);
  1565. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1566. ASSERT_TRUE(group);
  1567. }
  1568. TEST(Registry, SortSingle) {
  1569. entt::registry registry{};
  1570. int val = 0;
  1571. registry.emplace<int>(registry.create(), val++);
  1572. registry.emplace<int>(registry.create(), val++);
  1573. registry.emplace<int>(registry.create(), val++);
  1574. for(auto entity: registry.view<int>()) {
  1575. ASSERT_EQ(registry.get<int>(entity), --val);
  1576. }
  1577. registry.sort<int>(std::less{});
  1578. for(auto entity: registry.view<int>()) {
  1579. ASSERT_EQ(registry.get<int>(entity), val++);
  1580. }
  1581. }
  1582. TEST(Registry, SortMulti) {
  1583. entt::registry registry{};
  1584. unsigned int uval = 0u;
  1585. int ival = 0;
  1586. for(auto i = 0; i < 3; ++i) {
  1587. const auto entity = registry.create();
  1588. registry.emplace<unsigned int>(entity, uval++);
  1589. registry.emplace<int>(entity, ival++);
  1590. }
  1591. for(auto entity: registry.view<unsigned int>()) {
  1592. ASSERT_EQ(registry.get<unsigned int>(entity), --uval);
  1593. }
  1594. for(auto entity: registry.view<int>()) {
  1595. ASSERT_EQ(registry.get<int>(entity), --ival);
  1596. }
  1597. registry.sort<unsigned int>(std::less{});
  1598. registry.sort<int, unsigned int>();
  1599. for(auto entity: registry.view<unsigned int>()) {
  1600. ASSERT_EQ(registry.get<unsigned int>(entity), uval++);
  1601. }
  1602. for(auto entity: registry.view<int>()) {
  1603. ASSERT_EQ(registry.get<int>(entity), ival++);
  1604. }
  1605. }
  1606. TEST(Registry, SortEmpty) {
  1607. entt::registry registry{};
  1608. registry.emplace<test::empty>(registry.create());
  1609. registry.emplace<test::empty>(registry.create());
  1610. registry.emplace<test::empty>(registry.create());
  1611. ASSERT_LT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
  1612. ASSERT_LT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
  1613. registry.sort<test::empty>(std::less{});
  1614. ASSERT_GT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
  1615. ASSERT_GT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
  1616. }
  1617. TEST(Registry, Context) {
  1618. entt::registry registry{};
  1619. auto &ctx = registry.ctx();
  1620. const auto &cctx = std::as_const(registry).ctx();
  1621. ASSERT_FALSE(ctx.contains<char>());
  1622. ASSERT_FALSE(cctx.contains<const int>());
  1623. ASSERT_EQ(ctx.find<char>(), nullptr);
  1624. ASSERT_EQ(cctx.find<const int>(), nullptr);
  1625. ctx.emplace<char>();
  1626. ctx.emplace<int>();
  1627. ASSERT_TRUE(ctx.contains<char>());
  1628. ASSERT_TRUE(cctx.contains<int>());
  1629. ASSERT_NE(ctx.find<const char>(), nullptr);
  1630. ASSERT_NE(cctx.find<const int>(), nullptr);
  1631. ASSERT_FALSE(ctx.erase<double>());
  1632. ASSERT_TRUE(ctx.erase<int>());
  1633. ASSERT_TRUE(ctx.contains<const char>());
  1634. ASSERT_FALSE(cctx.contains<const int>());
  1635. ASSERT_NE(ctx.find<char>(), nullptr);
  1636. ASSERT_EQ(cctx.find<int>(), nullptr);
  1637. ASSERT_FALSE(ctx.erase<int>());
  1638. ASSERT_TRUE(ctx.erase<char>());
  1639. ctx.emplace<char>('c');
  1640. ctx.emplace<int>(1);
  1641. ASSERT_EQ(ctx.emplace<char>('a'), 'c');
  1642. ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
  1643. ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
  1644. ASSERT_EQ(ctx.get<char>(), 'c');
  1645. ASSERT_EQ(ctx.emplace<const int>(0), 1);
  1646. ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
  1647. ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
  1648. ASSERT_EQ(ctx.get<int>(), 1);
  1649. ASSERT_EQ(ctx.find<double>(), nullptr);
  1650. ASSERT_EQ(cctx.find<double>(), nullptr);
  1651. ASSERT_EQ(ctx.insert_or_assign<char>('a'), 'a');
  1652. ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
  1653. ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
  1654. ASSERT_EQ(ctx.get<const char>(), 'a');
  1655. ASSERT_EQ(ctx.insert_or_assign<const int>(0), 0);
  1656. ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
  1657. ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
  1658. ASSERT_EQ(ctx.get<int>(), 0);
  1659. }
  1660. TEST(Registry, ContextHint) {
  1661. using namespace entt::literals;
  1662. entt::registry registry{};
  1663. auto &ctx = registry.ctx();
  1664. const auto &cctx = std::as_const(registry).ctx();
  1665. ctx.emplace<int>(1);
  1666. ctx.emplace_as<int>("other"_hs, 3);
  1667. ASSERT_TRUE(ctx.contains<int>());
  1668. ASSERT_TRUE(cctx.contains<const int>("other"_hs));
  1669. ASSERT_FALSE(ctx.contains<char>("other"_hs));
  1670. ASSERT_NE(cctx.find<const int>(), nullptr);
  1671. ASSERT_NE(ctx.find<int>("other"_hs), nullptr);
  1672. ASSERT_EQ(cctx.find<const char>("other"_hs), nullptr);
  1673. ASSERT_EQ(ctx.get<int>(), 1);
  1674. ASSERT_EQ(cctx.get<const int>("other"_hs), 3);
  1675. ctx.insert_or_assign(3);
  1676. ctx.insert_or_assign("other"_hs, 1);
  1677. ASSERT_EQ(ctx.get<const int>(), 3);
  1678. ASSERT_EQ(cctx.get<int>("other"_hs), 1);
  1679. ASSERT_FALSE(ctx.erase<char>("other"_hs));
  1680. ASSERT_TRUE(ctx.erase<int>());
  1681. ASSERT_TRUE(cctx.contains<int>("other"_hs));
  1682. ASSERT_EQ(ctx.get<int>("other"_hs), 1);
  1683. ASSERT_TRUE(ctx.erase<int>("other"_hs));
  1684. ASSERT_FALSE(cctx.contains<int>("other"_hs));
  1685. ASSERT_EQ(ctx.find<int>("other"_hs), nullptr);
  1686. }
  1687. TEST(Registry, ContextAsRef) {
  1688. entt::registry registry{};
  1689. int value{3};
  1690. registry.ctx().emplace<int &>(value);
  1691. ASSERT_NE(registry.ctx().find<int>(), nullptr);
  1692. ASSERT_NE(registry.ctx().find<const int>(), nullptr);
  1693. ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
  1694. ASSERT_EQ(registry.ctx().get<const int>(), value);
  1695. ASSERT_EQ(registry.ctx().get<int>(), value);
  1696. registry.ctx().get<int>() = 2;
  1697. ASSERT_EQ(value, 2);
  1698. ASSERT_EQ(registry.ctx().get<int>(), value);
  1699. value = 1;
  1700. ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
  1701. }
  1702. TEST(Registry, ContextAsConstRef) {
  1703. entt::registry registry{};
  1704. int value{3};
  1705. registry.ctx().emplace<const int &>(value);
  1706. ASSERT_EQ(registry.ctx().find<int>(), nullptr);
  1707. ASSERT_NE(registry.ctx().find<const int>(), nullptr);
  1708. ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
  1709. ASSERT_EQ(registry.ctx().get<const int>(), value);
  1710. value = 1;
  1711. ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
  1712. }
  1713. TEST(Registry, ContextPoolMemberDestructionOrder) {
  1714. auto registry = std::make_unique<entt::registry>();
  1715. const auto entity = registry->create();
  1716. bool ctx_check = false;
  1717. registry->ctx().emplace<typename destruction_order::ctx_check_type>();
  1718. registry->emplace<destruction_order>(entity, *registry, ctx_check);
  1719. registry.reset();
  1720. ASSERT_TRUE(ctx_check);
  1721. }
  1722. TEST(Registry, Constness) {
  1723. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<int>({})), int &>();
  1724. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<test::empty>({})), void>();
  1725. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<>({})), std::tuple<>>();
  1726. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int>({})), int &>();
  1727. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int, const char>({})), std::tuple<int &, const char &>>();
  1728. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<>({})), std::tuple<>>();
  1729. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int>({})), int *>();
  1730. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int, const char>({})), std::tuple<int *, const char *>>();
  1731. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<int>()), int &>();
  1732. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<const char>()), const char &>();
  1733. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<int>()), int *>();
  1734. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<const char>()), const char *>();
  1735. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<>({})), std::tuple<>>();
  1736. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int>({})), const int &>();
  1737. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int, const char>({})), std::tuple<const int &, const char &>>();
  1738. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<>({})), std::tuple<>>();
  1739. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int>({})), const int *>();
  1740. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int, const char>({})), std::tuple<const int *, const char *>>();
  1741. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<int>()), const int &>();
  1742. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<const char>()), const char &>();
  1743. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<int>()), const int *>();
  1744. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<const char>()), const char *>();
  1745. }
  1746. TEST(Registry, AssignEntities) {
  1747. using traits_type = entt::entt_traits<entt::entity>;
  1748. entt::registry registry{};
  1749. std::array<entt::entity, 3u> entity{};
  1750. registry.create(entity.begin(), entity.end());
  1751. registry.destroy(entity[1]);
  1752. registry.destroy(entity[2]);
  1753. entt::registry other{};
  1754. auto &src = registry.storage<entt::entity>();
  1755. auto &dst = other.storage<entt::entity>();
  1756. dst.push(src.rbegin(), src.rend());
  1757. dst.free_list(src.free_list());
  1758. ASSERT_EQ(registry.storage<entt::entity>().size(), other.storage<entt::entity>().size());
  1759. ASSERT_TRUE(other.valid(entity[0]));
  1760. ASSERT_FALSE(other.valid(entity[1]));
  1761. ASSERT_FALSE(other.valid(entity[2]));
  1762. ASSERT_EQ(registry.create(), other.create());
  1763. ASSERT_EQ(traits_type::to_entity(other.create()), traits_type::to_integral(entity[1]));
  1764. }
  1765. TEST(Registry, ScramblingPoolsIsAllowed) {
  1766. entt::registry registry{};
  1767. registry.on_destroy<int>().connect<&listener::sort<int>>();
  1768. for(std::size_t i{}; i < 2u; ++i) {
  1769. const auto entity = registry.create();
  1770. registry.emplace<int>(entity, static_cast<int>(i));
  1771. }
  1772. registry.destroy(registry.view<int>().back());
  1773. // thanks to @andranik3949 for pointing out this missing test
  1774. registry.view<const int>().each([](const auto entity, const auto &value) {
  1775. ASSERT_EQ(static_cast<int>(entt::to_integral(entity)), value);
  1776. });
  1777. }
  1778. TEST(Registry, AssureMixinLoop) {
  1779. entt::registry registry{};
  1780. const auto entity = registry.create();
  1781. ASSERT_EQ(std::as_const(registry).storage<test::assure_loop>(), nullptr);
  1782. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1783. registry.emplace<test::assure_loop>(entity);
  1784. ASSERT_NE(std::as_const(registry).storage<test::assure_loop>(), nullptr);
  1785. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1786. ASSERT_TRUE(registry.all_of<test::assure_loop>(entity));
  1787. ASSERT_FALSE(registry.all_of<int>(entity));
  1788. }
  1789. TEST(Registry, VoidType) {
  1790. using namespace entt::literals;
  1791. entt::registry registry{};
  1792. const auto entity = registry.create();
  1793. auto &storage = registry.storage<void>("custom"_hs);
  1794. storage.emplace(entity);
  1795. ASSERT_TRUE(registry.storage<void>().empty());
  1796. ASSERT_FALSE(registry.storage<void>("custom"_hs).empty());
  1797. ASSERT_TRUE(registry.storage<void>("custom"_hs).contains(entity));
  1798. ASSERT_EQ(registry.storage<void>().type(), entt::type_id<void>());
  1799. ASSERT_EQ(registry.storage<void>("custom"_hs).type(), entt::type_id<void>());
  1800. }
  1801. TEST(Registry, NoEtoType) {
  1802. entt::registry registry{};
  1803. const auto entity = registry.create();
  1804. registry.emplace<no_eto_type>(entity);
  1805. registry.emplace<int>(entity, 1);
  1806. ASSERT_NE(registry.storage<no_eto_type>().raw(), nullptr);
  1807. ASSERT_NE(registry.try_get<no_eto_type>(entity), nullptr);
  1808. ASSERT_EQ(registry.view<no_eto_type>().get(entity), std::as_const(registry).view<const no_eto_type>().get(entity));
  1809. auto view = registry.view<no_eto_type, int>();
  1810. auto cview = std::as_const(registry).view<const no_eto_type, const int>();
  1811. ASSERT_EQ((std::get<0>(view.get<no_eto_type, int>(entity))), (std::get<0>(cview.get<const no_eto_type, const int>(entity))));
  1812. }