registry.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. #include <unordered_set>
  2. #include <functional>
  3. #include <iterator>
  4. #include <memory>
  5. #include <cstdint>
  6. #include <type_traits>
  7. #include <gtest/gtest.h>
  8. #include <entt/core/type_traits.hpp>
  9. #include <entt/entity/registry.hpp>
  10. #include <entt/entity/entity.hpp>
  11. struct empty_type {};
  12. struct non_default_constructible {
  13. non_default_constructible(int v): value{v} {}
  14. int value;
  15. };
  16. struct aggregate {
  17. int value{};
  18. };
  19. struct listener {
  20. template<typename Component>
  21. static void sort(entt::registry &registry) {
  22. registry.sort<Component>([](auto lhs, auto rhs) { return lhs < rhs; });
  23. }
  24. template<typename Component>
  25. void incr(const entt::registry &, entt::entity entity) {
  26. last = entity;
  27. ++counter;
  28. }
  29. template<typename Component>
  30. void decr(const entt::registry &, entt::entity entity) {
  31. last = entity;
  32. --counter;
  33. }
  34. entt::entity last{entt::null};
  35. int counter{0};
  36. };
  37. struct owner {
  38. void receive(const entt::registry &ref) {
  39. parent = &ref;
  40. }
  41. const entt::registry *parent{nullptr};
  42. };
  43. TEST(Registry, Context) {
  44. entt::registry registry;
  45. ASSERT_EQ(registry.try_ctx<char>(), nullptr);
  46. ASSERT_EQ(registry.try_ctx<const int>(), nullptr);
  47. ASSERT_EQ(registry.try_ctx<double>(), nullptr);
  48. registry.set<char>();
  49. registry.set<int>();
  50. // suppress the warning due to the [[nodiscard]] attribute
  51. static_cast<void>(registry.ctx_or_set<double>());
  52. ASSERT_NE(registry.try_ctx<char>(), nullptr);
  53. ASSERT_NE(registry.try_ctx<const int>(), nullptr);
  54. ASSERT_NE(registry.try_ctx<double>(), nullptr);
  55. registry.unset<int>();
  56. registry.unset<double>();
  57. auto count = 0;
  58. registry.ctx([&count](auto info) {
  59. ASSERT_EQ(info.hash(), entt::type_hash<char>::value());
  60. ++count;
  61. });
  62. ASSERT_EQ(count, 1);
  63. ASSERT_NE(registry.try_ctx<char>(), nullptr);
  64. ASSERT_EQ(registry.try_ctx<const int>(), nullptr);
  65. ASSERT_EQ(registry.try_ctx<double>(), nullptr);
  66. registry.set<char>('c');
  67. registry.set<int>(0);
  68. registry.set<double>(1.);
  69. registry.set<int>(42);
  70. ASSERT_EQ(registry.ctx_or_set<char>('a'), 'c');
  71. ASSERT_NE(registry.try_ctx<char>(), nullptr);
  72. ASSERT_EQ(registry.try_ctx<char>(), &registry.ctx<char>());
  73. ASSERT_EQ(registry.ctx<char>(), std::as_const(registry).ctx<const char>());
  74. ASSERT_EQ(registry.ctx<const int>(), 42);
  75. ASSERT_NE(registry.try_ctx<int>(), nullptr);
  76. ASSERT_EQ(registry.try_ctx<const int>(), &registry.ctx<int>());
  77. ASSERT_EQ(registry.ctx<int>(), std::as_const(registry).ctx<const int>());
  78. ASSERT_EQ(registry.ctx<const double>(), 1.);
  79. ASSERT_NE(registry.try_ctx<double>(), nullptr);
  80. ASSERT_EQ(registry.try_ctx<const double>(), &registry.ctx<double>());
  81. ASSERT_EQ(registry.ctx<double>(), std::as_const(registry).ctx<const double>());
  82. ASSERT_EQ(registry.try_ctx<float>(), nullptr);
  83. }
  84. TEST(Registry, ContextAsRef) {
  85. entt::registry registry;
  86. int value{3};
  87. registry.set<int &>(value);
  88. ASSERT_NE(registry.try_ctx<int>(), nullptr);
  89. ASSERT_NE(registry.try_ctx<const int>(), nullptr);
  90. ASSERT_NE(std::as_const(registry).try_ctx<const int>(), nullptr);
  91. ASSERT_EQ(registry.ctx<const int>(), 3);
  92. ASSERT_EQ(registry.ctx<int>(), 3);
  93. registry.ctx<int>() = 42;
  94. ASSERT_EQ(registry.ctx<int>(), 42);
  95. ASSERT_EQ(value, 42);
  96. value = 3;
  97. ASSERT_EQ(std::as_const(registry).ctx<const int>(), 3);
  98. }
  99. TEST(Registry, ContextAsConstRef) {
  100. entt::registry registry;
  101. int value{3};
  102. registry.set<const int &>(value);
  103. ASSERT_EQ(registry.try_ctx<int>(), nullptr);
  104. ASSERT_NE(registry.try_ctx<const int>(), nullptr);
  105. ASSERT_NE(std::as_const(registry).try_ctx<const int>(), nullptr);
  106. ASSERT_EQ(registry.ctx<const int>(), 3);
  107. value = 42;
  108. ASSERT_EQ(std::as_const(registry).ctx<const int>(), 42);
  109. }
  110. TEST(Registry, Functionalities) {
  111. entt::registry registry;
  112. ASSERT_EQ(registry.size(), 0u);
  113. ASSERT_EQ(registry.alive(), 0u);
  114. ASSERT_NO_FATAL_FAILURE((registry.reserve<int, char>(8)));
  115. ASSERT_NO_FATAL_FAILURE(registry.reserve_pools(16));
  116. ASSERT_NO_FATAL_FAILURE(registry.reserve(42));
  117. ASSERT_TRUE(registry.empty());
  118. ASSERT_EQ(registry.capacity(), 42u);
  119. ASSERT_EQ(registry.capacity<int>(), 8u);
  120. ASSERT_EQ(registry.capacity<char>(), 8u);
  121. ASSERT_EQ(registry.size<int>(), 0u);
  122. ASSERT_EQ(registry.size<char>(), 0u);
  123. ASSERT_TRUE((registry.empty<int, char>()));
  124. registry.prepare<double>();
  125. const auto e0 = registry.create();
  126. const auto e1 = registry.create();
  127. registry.emplace<int>(e1);
  128. registry.emplace<char>(e1);
  129. ASSERT_TRUE(registry.all_of<>(e0));
  130. ASSERT_FALSE(registry.any_of<>(e1));
  131. ASSERT_EQ(registry.size<int>(), 1u);
  132. ASSERT_EQ(registry.size<char>(), 1u);
  133. ASSERT_FALSE(registry.empty<int>());
  134. ASSERT_FALSE(registry.empty<char>());
  135. ASSERT_NE(e0, e1);
  136. ASSERT_FALSE((registry.all_of<int, char>(e0)));
  137. ASSERT_TRUE((registry.all_of<int, char>(e1)));
  138. ASSERT_FALSE((registry.any_of<int, double>(e0)));
  139. ASSERT_TRUE((registry.any_of<int, double>(e1)));
  140. ASSERT_EQ(registry.try_get<int>(e0), nullptr);
  141. ASSERT_NE(registry.try_get<int>(e1), nullptr);
  142. ASSERT_EQ(registry.try_get<char>(e0), nullptr);
  143. ASSERT_NE(registry.try_get<char>(e1), nullptr);
  144. ASSERT_EQ(registry.try_get<double>(e0), nullptr);
  145. ASSERT_EQ(registry.try_get<double>(e1), nullptr);
  146. ASSERT_EQ(registry.emplace<int>(e0, 42), 42);
  147. ASSERT_EQ(registry.emplace<char>(e0, 'c'), 'c');
  148. ASSERT_NO_FATAL_FAILURE(registry.erase<int>(e1));
  149. ASSERT_NO_FATAL_FAILURE(registry.erase<char>(e1));
  150. ASSERT_TRUE((registry.all_of<int, char>(e0)));
  151. ASSERT_FALSE((registry.all_of<int, char>(e1)));
  152. ASSERT_TRUE((registry.any_of<int, double>(e0)));
  153. ASSERT_FALSE((registry.any_of<int, double>(e1)));
  154. const auto e2 = registry.create();
  155. registry.emplace_or_replace<int>(e2, registry.get<int>(e0));
  156. registry.emplace_or_replace<char>(e2, registry.get<char>(e0));
  157. ASSERT_TRUE((registry.all_of<int, char>(e2)));
  158. ASSERT_EQ(registry.get<int>(e0), 42);
  159. ASSERT_EQ(registry.get<char>(e0), 'c');
  160. ASSERT_NE(registry.try_get<int>(e0), nullptr);
  161. ASSERT_NE(registry.try_get<char>(e0), nullptr);
  162. ASSERT_EQ(registry.try_get<double>(e0), nullptr);
  163. ASSERT_EQ(*registry.try_get<int>(e0), 42);
  164. ASSERT_EQ(*registry.try_get<char>(e0), 'c');
  165. ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 42);
  166. ASSERT_EQ(*std::get<0>(registry.try_get<int, char, double>(e0)), 42);
  167. ASSERT_EQ(std::get<1>(static_cast<const entt::registry &>(registry).get<int, char>(e0)), 'c');
  168. ASSERT_EQ(*std::get<1>(static_cast<const entt::registry &>(registry).try_get<int, char, double>(e0)), 'c');
  169. ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
  170. ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));
  171. ASSERT_NE(&registry.get<int>(e0), &registry.get<int>(e2));
  172. ASSERT_NE(&registry.get<char>(e0), &registry.get<char>(e2));
  173. ASSERT_EQ(registry.patch<int>(e0, [](auto &instance) { instance = 2; }), 2);
  174. ASSERT_EQ(registry.replace<int>(e0, 3), 3);
  175. ASSERT_NO_FATAL_FAILURE(registry.emplace_or_replace<int>(e0, 1));
  176. ASSERT_NO_FATAL_FAILURE(registry.emplace_or_replace<int>(e1, 1));
  177. ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e0), 1);
  178. ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e1), 1);
  179. ASSERT_EQ(registry.size(), 3u);
  180. ASSERT_EQ(registry.alive(), 3u);
  181. ASSERT_FALSE(registry.empty());
  182. ASSERT_EQ(registry.version(e2), 0u);
  183. ASSERT_EQ(registry.current(e2), 0u);
  184. ASSERT_NO_FATAL_FAILURE(registry.destroy(e2));
  185. ASSERT_EQ(registry.version(e2), 0u);
  186. ASSERT_EQ(registry.current(e2), 1u);
  187. ASSERT_TRUE(registry.valid(e0));
  188. ASSERT_TRUE(registry.valid(e1));
  189. ASSERT_FALSE(registry.valid(e2));
  190. ASSERT_EQ(registry.size(), 3u);
  191. ASSERT_EQ(registry.alive(), 2u);
  192. ASSERT_FALSE(registry.empty());
  193. ASSERT_NO_FATAL_FAILURE(registry.clear());
  194. ASSERT_EQ(registry.size(), 3u);
  195. ASSERT_EQ(registry.alive(), 0u);
  196. ASSERT_TRUE(registry.empty());
  197. const auto e3 = registry.create();
  198. ASSERT_EQ(registry.get_or_emplace<int>(e3, 3), 3);
  199. ASSERT_EQ(registry.get_or_emplace<char>(e3, 'c'), 'c');
  200. ASSERT_EQ(registry.size<int>(), 1u);
  201. ASSERT_EQ(registry.size<char>(), 1u);
  202. ASSERT_FALSE(registry.empty<int>());
  203. ASSERT_FALSE(registry.empty<char>());
  204. ASSERT_TRUE((registry.all_of<int, char>(e3)));
  205. ASSERT_EQ(registry.get<int>(e3), 3);
  206. ASSERT_EQ(registry.get<char>(e3), 'c');
  207. ASSERT_NO_FATAL_FAILURE(registry.clear<int>());
  208. ASSERT_EQ(registry.size<int>(), 0u);
  209. ASSERT_EQ(registry.size<char>(), 1u);
  210. ASSERT_TRUE(registry.empty<int>());
  211. ASSERT_FALSE(registry.empty<char>());
  212. ASSERT_NO_FATAL_FAILURE(registry.clear());
  213. ASSERT_EQ(registry.size<int>(), 0u);
  214. ASSERT_EQ(registry.size<char>(), 0u);
  215. ASSERT_TRUE((registry.empty<int, char>()));
  216. const auto e4 = registry.create();
  217. const auto e5 = registry.create();
  218. registry.emplace<int>(e4);
  219. ASSERT_EQ(registry.remove<int>(e4), 1u);
  220. ASSERT_EQ(registry.remove<int>(e5), 0u);
  221. ASSERT_EQ(registry.size<int>(), 0u);
  222. ASSERT_EQ(registry.size<char>(), 0u);
  223. ASSERT_TRUE(registry.empty<int>());
  224. ASSERT_EQ(registry.capacity<int>(), 8u);
  225. ASSERT_EQ(registry.capacity<char>(), 8u);
  226. registry.shrink_to_fit<int, char>();
  227. ASSERT_EQ(registry.capacity<int>(), 0u);
  228. ASSERT_EQ(registry.capacity<char>(), 0u);
  229. }
  230. TEST(Registry, Move) {
  231. entt::registry registry;
  232. const auto entity = registry.create();
  233. owner test{};
  234. registry.on_construct<int>().connect<&owner::receive>(test);
  235. registry.on_destroy<int>().connect<&owner::receive>(test);
  236. ASSERT_EQ(test.parent, nullptr);
  237. registry.emplace<int>(entity);
  238. ASSERT_EQ(test.parent, &registry);
  239. entt::registry other{std::move(registry)};
  240. other.erase<int>(entity);
  241. registry.emplace<int>(registry.create(entity));
  242. ASSERT_EQ(test.parent, &other);
  243. registry = std::move(other);
  244. registry.emplace<int>(entity);
  245. registry.emplace<int>(registry.create(entity));
  246. ASSERT_EQ(test.parent, &registry);
  247. }
  248. TEST(Registry, ReplaceAggregate) {
  249. entt::registry registry;
  250. const auto entity = registry.create();
  251. registry.emplace<aggregate>(entity, 0);
  252. auto &instance = registry.replace<aggregate>(entity, 42);
  253. ASSERT_EQ(instance.value, 42);
  254. }
  255. TEST(Registry, EmplaceOrReplaceAggregate) {
  256. entt::registry registry;
  257. const auto entity = registry.create();
  258. auto &instance = registry.emplace_or_replace<aggregate>(entity, 42);
  259. ASSERT_EQ(instance.value, 42);
  260. }
  261. TEST(Registry, Identifiers) {
  262. entt::registry registry;
  263. const auto pre = registry.create();
  264. ASSERT_EQ(pre, registry.entity(pre));
  265. registry.destroy(pre);
  266. const auto post = registry.create();
  267. ASSERT_NE(pre, post);
  268. ASSERT_EQ(entt::registry::entity(pre), entt::registry::entity(post));
  269. ASSERT_NE(entt::registry::version(pre), entt::registry::version(post));
  270. ASSERT_NE(registry.version(pre), registry.current(pre));
  271. ASSERT_EQ(registry.version(post), registry.current(post));
  272. }
  273. TEST(Registry, Data) {
  274. entt::registry registry;
  275. ASSERT_EQ(std::as_const(registry).data(), nullptr);
  276. const auto entity = registry.create();
  277. ASSERT_EQ(*std::as_const(registry).data(), entity);
  278. const auto other = registry.create();
  279. registry.destroy(entity);
  280. ASSERT_NE(*std::as_const(registry).data(), entity);
  281. ASSERT_EQ(*(std::as_const(registry).data() + 1u), other);
  282. }
  283. TEST(Registry, CreateManyEntitiesAtOnce) {
  284. entt::registry registry;
  285. entt::entity entities[3];
  286. const auto entity = registry.create();
  287. registry.destroy(registry.create());
  288. registry.destroy(entity);
  289. registry.destroy(registry.create());
  290. registry.create(std::begin(entities), std::end(entities));
  291. ASSERT_TRUE(registry.valid(entities[0]));
  292. ASSERT_TRUE(registry.valid(entities[1]));
  293. ASSERT_TRUE(registry.valid(entities[2]));
  294. ASSERT_EQ(registry.entity(entities[0]), entt::entity{0});
  295. ASSERT_EQ(registry.version(entities[0]), entt::registry::version_type{2});
  296. ASSERT_EQ(registry.entity(entities[1]), entt::entity{1});
  297. ASSERT_EQ(registry.version(entities[1]), entt::registry::version_type{1});
  298. ASSERT_EQ(registry.entity(entities[2]), entt::entity{2});
  299. ASSERT_EQ(registry.version(entities[2]), entt::registry::version_type{0});
  300. }
  301. TEST(Registry, CreateManyEntitiesAtOnceWithListener) {
  302. entt::registry registry;
  303. entt::entity entities[3];
  304. listener listener;
  305. registry.on_construct<int>().connect<&listener::incr<int>>(listener);
  306. registry.create(std::begin(entities), std::end(entities));
  307. registry.insert(std::begin(entities), std::end(entities), 42);
  308. registry.insert(std::begin(entities), std::end(entities), 'c');
  309. ASSERT_EQ(registry.get<int>(entities[0]), 42);
  310. ASSERT_EQ(registry.get<char>(entities[1]), 'c');
  311. ASSERT_EQ(listener.counter, 3);
  312. registry.on_construct<int>().disconnect<&listener::incr<int>>(listener);
  313. registry.on_construct<empty_type>().connect<&listener::incr<empty_type>>(listener);
  314. registry.create(std::begin(entities), std::end(entities));
  315. registry.insert(std::begin(entities), std::end(entities), 'a');
  316. registry.insert<empty_type>(std::begin(entities), std::end(entities));
  317. ASSERT_TRUE(registry.all_of<empty_type>(entities[0]));
  318. ASSERT_EQ(registry.get<char>(entities[2]), 'a');
  319. ASSERT_EQ(listener.counter, 6);
  320. }
  321. TEST(Registry, CreateWithHint) {
  322. entt::registry registry;
  323. auto e3 = registry.create(entt::entity{3});
  324. auto e2 = registry.create(entt::entity{3});
  325. ASSERT_EQ(e2, entt::entity{2});
  326. ASSERT_FALSE(registry.valid(entt::entity{1}));
  327. ASSERT_EQ(e3, entt::entity{3});
  328. registry.destroy(e2);
  329. ASSERT_EQ(registry.version(e2), entt::registry::version_type{});
  330. ASSERT_EQ(registry.current(e2), entt::registry::version_type{1});
  331. e2 = registry.create();
  332. auto e1 = registry.create(entt::entity{2});
  333. ASSERT_EQ(registry.entity(e2), entt::entity{2});
  334. ASSERT_EQ(registry.version(e2), entt::registry::version_type{1});
  335. ASSERT_EQ(registry.entity(e1), entt::entity{1});
  336. ASSERT_EQ(registry.version(e1), entt::registry::version_type{});
  337. registry.destroy(e1);
  338. registry.destroy(e2);
  339. auto e0 = registry.create(entt::entity{0});
  340. ASSERT_EQ(e0, entt::entity{0});
  341. ASSERT_EQ(registry.version(e0), entt::registry::version_type{});
  342. }
  343. TEST(Registry, DestroyWithVersion) {
  344. entt::registry registry;
  345. const auto e0 = registry.create();
  346. const auto e1 = registry.create();
  347. ASSERT_EQ(registry.current(e0), entt::registry::version_type{});
  348. ASSERT_EQ(registry.current(e1), entt::registry::version_type{});
  349. registry.destroy(e0);
  350. registry.destroy(e1, 3);
  351. ASSERT_EQ(registry.current(e0), entt::registry::version_type{1});
  352. ASSERT_EQ(registry.current(e1), entt::registry::version_type{3});
  353. }
  354. TEST(Registry, CreateDestroyEntities) {
  355. entt::registry registry;
  356. entt::entity pre{}, post{};
  357. for(int i = 0; i < 10; ++i) {
  358. const auto entity = registry.create();
  359. registry.emplace<double>(entity);
  360. }
  361. registry.clear();
  362. for(int i = 0; i < 7; ++i) {
  363. const auto entity = registry.create();
  364. registry.emplace<int>(entity);
  365. if(i == 3) { pre = entity; }
  366. }
  367. registry.clear();
  368. for(int i = 0; i < 5; ++i) {
  369. const auto entity = registry.create();
  370. if(i == 3) { post = entity; }
  371. }
  372. ASSERT_FALSE(registry.valid(pre));
  373. ASSERT_TRUE(registry.valid(post));
  374. ASSERT_NE(registry.version(pre), registry.version(post));
  375. ASSERT_EQ(registry.version(pre) + 1, registry.version(post));
  376. ASSERT_EQ(registry.current(pre), registry.current(post));
  377. }
  378. TEST(Registry, CreateDestroyCornerCase) {
  379. entt::registry registry;
  380. const auto e0 = registry.create();
  381. const auto e1 = registry.create();
  382. registry.destroy(e0);
  383. registry.destroy(e1);
  384. registry.each([](auto) { FAIL(); });
  385. ASSERT_EQ(registry.current(e0), entt::registry::version_type{1});
  386. ASSERT_EQ(registry.current(e1), entt::registry::version_type{1});
  387. }
  388. TEST(Registry, VersionOverflow) {
  389. using traits_type = entt::entt_traits<entt::entity>;
  390. entt::registry registry;
  391. const auto entity = registry.create();
  392. registry.destroy(entity, typename traits_type::version_type(traits_type::version_mask));
  393. registry.destroy(registry.create());
  394. ASSERT_EQ(registry.current(entity), registry.version(entity));
  395. ASSERT_EQ(registry.current(entity), typename traits_type::version_type{});
  396. }
  397. TEST(Registry, Each) {
  398. entt::registry registry;
  399. entt::registry::size_type tot;
  400. entt::registry::size_type match;
  401. registry.create();
  402. registry.emplace<int>(registry.create());
  403. registry.create();
  404. registry.emplace<int>(registry.create());
  405. registry.create();
  406. tot = 0u;
  407. match = 0u;
  408. registry.each([&](auto entity) {
  409. if(registry.all_of<int>(entity)) { ++match; }
  410. registry.create();
  411. ++tot;
  412. });
  413. ASSERT_EQ(tot, 5u);
  414. ASSERT_EQ(match, 2u);
  415. tot = 0u;
  416. match = 0u;
  417. registry.each([&](auto entity) {
  418. if(registry.all_of<int>(entity)) {
  419. registry.destroy(entity);
  420. ++match;
  421. }
  422. ++tot;
  423. });
  424. ASSERT_EQ(tot, 10u);
  425. ASSERT_EQ(match, 2u);
  426. tot = 0u;
  427. match = 0u;
  428. registry.each([&](auto entity) {
  429. if(registry.all_of<int>(entity)) { ++match; }
  430. registry.destroy(entity);
  431. ++tot;
  432. });
  433. ASSERT_EQ(tot, 8u);
  434. ASSERT_EQ(match, 0u);
  435. registry.each([&](auto) { FAIL(); });
  436. }
  437. TEST(Registry, Orphans) {
  438. entt::registry registry;
  439. entt::registry::size_type tot{};
  440. entt::entity entities[3u]{};
  441. registry.create(std::begin(entities), std::end(entities));
  442. registry.emplace<int>(entities[0u]);
  443. registry.emplace<int>(entities[2u]);
  444. registry.orphans([&](auto) { ++tot; });
  445. ASSERT_EQ(tot, 1u);
  446. registry.erase<int>(entities[0u]);
  447. registry.erase<int>(entities[2u]);
  448. tot = {};
  449. registry.orphans([&](auto) { ++tot; });
  450. ASSERT_EQ(tot, 3u);
  451. registry.clear();
  452. tot = {};
  453. registry.orphans([&](auto) { ++tot; });
  454. ASSERT_EQ(tot, 0u);
  455. }
  456. TEST(Registry, View) {
  457. entt::registry registry;
  458. auto mview = registry.view<int, char>();
  459. auto iview = registry.view<int>();
  460. auto cview = registry.view<char>();
  461. const auto e0 = registry.create();
  462. registry.emplace<int>(e0, 0);
  463. registry.emplace<char>(e0, 'c');
  464. const auto e1 = registry.create();
  465. registry.emplace<int>(e1, 0);
  466. const auto e2 = registry.create();
  467. registry.emplace<int>(e2, 0);
  468. registry.emplace<char>(e2, 'c');
  469. ASSERT_EQ(iview.size(), 3u);
  470. ASSERT_EQ(cview.size(), 2u);
  471. std::size_t cnt{};
  472. mview.each([&cnt](auto...) { ++cnt; });
  473. ASSERT_EQ(cnt, 2u);
  474. }
  475. TEST(Registry, NonOwningGroupInitOnFirstUse) {
  476. entt::registry registry;
  477. auto create = [&](auto... component) {
  478. const auto entity = registry.create();
  479. (registry.emplace<decltype(component)>(entity, component), ...);
  480. };
  481. create(0, 'c');
  482. create(0);
  483. create(0, 'c');
  484. std::size_t cnt{};
  485. auto group = registry.group<>(entt::get<int, char>);
  486. group.each([&cnt](auto...) { ++cnt; });
  487. ASSERT_TRUE((registry.sortable<int, char>()));
  488. ASSERT_EQ(cnt, 2u);
  489. }
  490. TEST(Registry, NonOwningGroupInitOnEmplace) {
  491. entt::registry registry;
  492. auto group = registry.group<>(entt::get<int, char>);
  493. auto create = [&](auto... component) {
  494. const auto entity = registry.create();
  495. (registry.emplace<decltype(component)>(entity, component), ...);
  496. };
  497. create(0, 'c');
  498. create(0);
  499. create(0, 'c');
  500. std::size_t cnt{};
  501. group.each([&cnt](auto...) { ++cnt; });
  502. ASSERT_TRUE((registry.sortable<int, char>()));
  503. ASSERT_EQ(cnt, 2u);
  504. }
  505. TEST(Registry, FullOwningGroupInitOnFirstUse) {
  506. entt::registry registry;
  507. auto create = [&](auto... component) {
  508. const auto entity = registry.create();
  509. (registry.emplace<decltype(component)>(entity, component), ...);
  510. };
  511. create(0, 'c');
  512. create(0);
  513. create(0, 'c');
  514. std::size_t cnt{};
  515. auto group = registry.group<int, char>();
  516. group.each([&cnt](auto...) { ++cnt; });
  517. ASSERT_FALSE(registry.sortable<int>());
  518. ASSERT_FALSE(registry.sortable<char>());
  519. ASSERT_TRUE(registry.sortable<double>());
  520. ASSERT_EQ(cnt, 2u);
  521. }
  522. TEST(Registry, FullOwningGroupInitOnEmplace) {
  523. entt::registry registry;
  524. auto group = registry.group<int, char>();
  525. auto create = [&](auto... component) {
  526. const auto entity = registry.create();
  527. (registry.emplace<decltype(component)>(entity, component), ...);
  528. };
  529. create(0, 'c');
  530. create(0);
  531. create(0, 'c');
  532. std::size_t cnt{};
  533. group.each([&cnt](auto...) { ++cnt; });
  534. ASSERT_FALSE(registry.sortable<int>());
  535. ASSERT_FALSE(registry.sortable<char>());
  536. ASSERT_TRUE(registry.sortable<double>());
  537. ASSERT_EQ(cnt, 2u);
  538. }
  539. TEST(Registry, PartialOwningGroupInitOnFirstUse) {
  540. entt::registry registry;
  541. auto create = [&](auto... component) {
  542. const auto entity = registry.create();
  543. (registry.emplace<decltype(component)>(entity, component), ...);
  544. };
  545. create(0, 'c');
  546. create(0);
  547. create(0, 'c');
  548. std::size_t cnt{};
  549. auto group = registry.group<int>(entt::get<char>);
  550. group.each([&cnt](auto...) { ++cnt; });
  551. ASSERT_FALSE((registry.sortable<int, char>()));
  552. ASSERT_FALSE(registry.sortable<int>());
  553. ASSERT_TRUE(registry.sortable<char>());
  554. ASSERT_EQ(cnt, 2u);
  555. }
  556. TEST(Registry, PartialOwningGroupInitOnEmplace) {
  557. entt::registry registry;
  558. auto group = registry.group<int>(entt::get<char>);
  559. auto create = [&](auto... component) {
  560. const auto entity = registry.create();
  561. (registry.emplace<decltype(component)>(entity, component), ...);
  562. };
  563. create(0, 'c');
  564. create(0);
  565. create(0, 'c');
  566. std::size_t cnt{};
  567. group.each([&cnt](auto...) { ++cnt; });
  568. ASSERT_FALSE((registry.sortable<int, char>()));
  569. ASSERT_FALSE(registry.sortable<int>());
  570. ASSERT_TRUE(registry.sortable<char>());
  571. ASSERT_EQ(cnt, 2u);
  572. }
  573. TEST(Registry, CleanViewAfterRemoveAndClear) {
  574. entt::registry registry;
  575. auto view = registry.view<int, char>();
  576. const auto entity = registry.create();
  577. registry.emplace<int>(entity);
  578. registry.emplace<char>(entity);
  579. ASSERT_EQ(view.size_hint(), 1u);
  580. registry.erase<char>(entity);
  581. ASSERT_EQ(view.size_hint(), 1u);
  582. registry.emplace<char>(entity);
  583. ASSERT_EQ(view.size_hint(), 1u);
  584. registry.clear<int>();
  585. ASSERT_EQ(view.size_hint(), 0u);
  586. registry.emplace<int>(entity);
  587. ASSERT_EQ(view.size_hint(), 1u);
  588. registry.clear();
  589. ASSERT_EQ(view.size_hint(), 0u);
  590. }
  591. TEST(Registry, CleanNonOwningGroupViewAfterRemoveAndClear) {
  592. entt::registry registry;
  593. auto group = registry.group<>(entt::get<int, char>);
  594. const auto entity = registry.create();
  595. registry.emplace<int>(entity, 0);
  596. registry.emplace<char>(entity, 'c');
  597. ASSERT_EQ(group.size(), 1u);
  598. registry.erase<char>(entity);
  599. ASSERT_EQ(group.size(), 0u);
  600. registry.emplace<char>(entity, 'c');
  601. ASSERT_EQ(group.size(), 1u);
  602. registry.clear<int>();
  603. ASSERT_EQ(group.size(), 0u);
  604. registry.emplace<int>(entity, 0);
  605. ASSERT_EQ(group.size(), 1u);
  606. registry.clear();
  607. ASSERT_EQ(group.size(), 0u);
  608. }
  609. TEST(Registry, CleanFullOwningGroupViewAfterRemoveAndClear) {
  610. entt::registry registry;
  611. auto group = registry.group<int, char>();
  612. const auto entity = registry.create();
  613. registry.emplace<int>(entity, 0);
  614. registry.emplace<char>(entity, 'c');
  615. ASSERT_EQ(group.size(), 1u);
  616. registry.erase<char>(entity);
  617. ASSERT_EQ(group.size(), 0u);
  618. registry.emplace<char>(entity, 'c');
  619. ASSERT_EQ(group.size(), 1u);
  620. registry.clear<int>();
  621. ASSERT_EQ(group.size(), 0u);
  622. registry.emplace<int>(entity, 0);
  623. ASSERT_EQ(group.size(), 1u);
  624. registry.clear();
  625. ASSERT_EQ(group.size(), 0u);
  626. }
  627. TEST(Registry, CleanPartialOwningGroupViewAfterRemoveAndClear) {
  628. entt::registry registry;
  629. auto group = registry.group<int>(entt::get<char>);
  630. const auto entity = registry.create();
  631. registry.emplace<int>(entity, 0);
  632. registry.emplace<char>(entity, 'c');
  633. ASSERT_EQ(group.size(), 1u);
  634. registry.erase<char>(entity);
  635. ASSERT_EQ(group.size(), 0u);
  636. registry.emplace<char>(entity, 'c');
  637. ASSERT_EQ(group.size(), 1u);
  638. registry.clear<int>();
  639. ASSERT_EQ(group.size(), 0u);
  640. registry.emplace<int>(entity, 0);
  641. ASSERT_EQ(group.size(), 1u);
  642. registry.clear();
  643. ASSERT_EQ(group.size(), 0u);
  644. }
  645. TEST(Registry, NestedGroups) {
  646. entt::registry registry;
  647. entt::entity entities[10];
  648. registry.create(std::begin(entities), std::end(entities));
  649. registry.insert<int>(std::begin(entities), std::end(entities));
  650. registry.insert<char>(std::begin(entities), std::end(entities));
  651. const auto g1 = registry.group<int>(entt::get<char>, entt::exclude<double>);
  652. ASSERT_TRUE(registry.sortable(g1));
  653. ASSERT_EQ(g1.size(), 10u);
  654. const auto g2 = registry.group<int>(entt::get<char>);
  655. ASSERT_TRUE(registry.sortable(g1));
  656. ASSERT_FALSE(registry.sortable(g2));
  657. ASSERT_EQ(g1.size(), 10u);
  658. ASSERT_EQ(g2.size(), 10u);
  659. for(auto i = 0u; i < 5u; ++i) {
  660. ASSERT_TRUE(g1.contains(entities[i*2+1]));
  661. ASSERT_TRUE(g1.contains(entities[i*2]));
  662. ASSERT_TRUE(g2.contains(entities[i*2+1]));
  663. ASSERT_TRUE(g2.contains(entities[i*2]));
  664. registry.emplace<double>(entities[i*2]);
  665. }
  666. ASSERT_EQ(g1.size(), 5u);
  667. ASSERT_EQ(g2.size(), 10u);
  668. for(auto i = 0u; i < 5u; ++i) {
  669. ASSERT_TRUE(g1.contains(entities[i*2+1]));
  670. ASSERT_FALSE(g1.contains(entities[i*2]));
  671. ASSERT_TRUE(g2.contains(entities[i*2+1]));
  672. ASSERT_TRUE(g2.contains(entities[i*2]));
  673. registry.erase<int>(entities[i*2+1]);
  674. }
  675. ASSERT_EQ(g1.size(), 0u);
  676. ASSERT_EQ(g2.size(), 5u);
  677. const auto g3= registry.group<int, float>(entt::get<char>, entt::exclude<double>);
  678. ASSERT_FALSE(registry.sortable(g1));
  679. ASSERT_FALSE(registry.sortable(g2));
  680. ASSERT_TRUE(registry.sortable(g3));
  681. ASSERT_EQ(g1.size(), 0u);
  682. ASSERT_EQ(g2.size(), 5u);
  683. ASSERT_EQ(g3.size(), 0u);
  684. for(auto i = 0u; i < 5u; ++i) {
  685. ASSERT_FALSE(g1.contains(entities[i*2+1]));
  686. ASSERT_FALSE(g1.contains(entities[i*2]));
  687. ASSERT_FALSE(g2.contains(entities[i*2+1]));
  688. ASSERT_TRUE(g2.contains(entities[i*2]));
  689. ASSERT_FALSE(g3.contains(entities[i*2+1]));
  690. ASSERT_FALSE(g3.contains(entities[i*2]));
  691. registry.emplace<int>(entities[i*2+1]);
  692. }
  693. ASSERT_EQ(g1.size(), 5u);
  694. ASSERT_EQ(g2.size(), 10u);
  695. ASSERT_EQ(g3.size(), 0u);
  696. for(auto i = 0u; i < 5u; ++i) {
  697. ASSERT_TRUE(g1.contains(entities[i*2+1]));
  698. ASSERT_FALSE(g1.contains(entities[i*2]));
  699. ASSERT_TRUE(g2.contains(entities[i*2+1]));
  700. ASSERT_TRUE(g2.contains(entities[i*2]));
  701. ASSERT_FALSE(g3.contains(entities[i*2+1]));
  702. ASSERT_FALSE(g3.contains(entities[i*2]));
  703. registry.emplace<float>(entities[i*2]);
  704. }
  705. ASSERT_EQ(g1.size(), 5u);
  706. ASSERT_EQ(g2.size(), 10u);
  707. ASSERT_EQ(g3.size(), 0u);
  708. for(auto i = 0u; i < 5u; ++i) {
  709. registry.erase<double>(entities[i*2]);
  710. }
  711. ASSERT_EQ(g1.size(), 10u);
  712. ASSERT_EQ(g2.size(), 10u);
  713. ASSERT_EQ(g3.size(), 5u);
  714. for(auto i = 0u; i < 5u; ++i) {
  715. ASSERT_TRUE(g1.contains(entities[i*2+1]));
  716. ASSERT_TRUE(g1.contains(entities[i*2]));
  717. ASSERT_TRUE(g2.contains(entities[i*2+1]));
  718. ASSERT_TRUE(g2.contains(entities[i*2]));
  719. ASSERT_FALSE(g3.contains(entities[i*2+1]));
  720. ASSERT_TRUE(g3.contains(entities[i*2]));
  721. registry.erase<int>(entities[i*2+1]);
  722. registry.erase<int>(entities[i*2]);
  723. }
  724. ASSERT_EQ(g1.size(), 0u);
  725. ASSERT_EQ(g2.size(), 0u);
  726. ASSERT_EQ(g3.size(), 0u);
  727. }
  728. TEST(Registry, SortSingle) {
  729. entt::registry registry;
  730. int val = 0;
  731. registry.emplace<int>(registry.create(), val++);
  732. registry.emplace<int>(registry.create(), val++);
  733. registry.emplace<int>(registry.create(), val++);
  734. for(auto entity: registry.view<int>()) {
  735. ASSERT_EQ(registry.get<int>(entity), --val);
  736. }
  737. registry.sort<int>(std::less<int>{});
  738. for(auto entity: registry.view<int>()) {
  739. ASSERT_EQ(registry.get<int>(entity), val++);
  740. }
  741. }
  742. TEST(Registry, SortMulti) {
  743. entt::registry registry;
  744. unsigned int uval = 0u;
  745. int ival = 0;
  746. for(auto i = 0; i < 3; ++i) {
  747. const auto entity = registry.create();
  748. registry.emplace<unsigned int>(entity, uval++);
  749. registry.emplace<int>(entity, ival++);
  750. }
  751. for(auto entity: registry.view<unsigned int>()) {
  752. ASSERT_EQ(registry.get<unsigned int>(entity), --uval);
  753. }
  754. for(auto entity: registry.view<int>()) {
  755. ASSERT_EQ(registry.get<int>(entity), --ival);
  756. }
  757. registry.sort<unsigned int>(std::less<unsigned int>{});
  758. registry.sort<int, unsigned int>();
  759. for(auto entity: registry.view<unsigned int>()) {
  760. ASSERT_EQ(registry.get<unsigned int>(entity), uval++);
  761. }
  762. for(auto entity: registry.view<int>()) {
  763. ASSERT_EQ(registry.get<int>(entity), ival++);
  764. }
  765. }
  766. TEST(Registry, SortEmpty) {
  767. entt::registry registry;
  768. registry.emplace<empty_type>(registry.create());
  769. registry.emplace<empty_type>(registry.create());
  770. registry.emplace<empty_type>(registry.create());
  771. ASSERT_LT(registry.view<empty_type>().data()[0], registry.view<empty_type>().data()[1]);
  772. ASSERT_LT(registry.view<empty_type>().data()[1], registry.view<empty_type>().data()[2]);
  773. registry.sort<empty_type>(std::less<entt::entity>{});
  774. ASSERT_GT(registry.view<empty_type>().data()[0], registry.view<empty_type>().data()[1]);
  775. ASSERT_GT(registry.view<empty_type>().data()[1], registry.view<empty_type>().data()[2]);
  776. }
  777. TEST(Registry, ComponentsWithTypesFromStandardTemplateLibrary) {
  778. // see #37 - the test shouldn't crash, that's all
  779. entt::registry registry;
  780. const auto entity = registry.create();
  781. registry.emplace<std::unordered_set<int>>(entity).insert(42);
  782. registry.destroy(entity);
  783. }
  784. TEST(Registry, ConstructWithComponents) {
  785. // it should compile, that's all
  786. entt::registry registry;
  787. const auto value = 0;
  788. registry.emplace<int>(registry.create(), value);
  789. }
  790. TEST(Registry, Signals) {
  791. entt::registry registry;
  792. listener listener;
  793. registry.on_construct<empty_type>().connect<&listener::incr<empty_type>>(listener);
  794. registry.on_destroy<empty_type>().connect<&listener::decr<empty_type>>(listener);
  795. registry.on_construct<int>().connect<&listener::incr<int>>(listener);
  796. registry.on_destroy<int>().connect<&listener::decr<int>>(listener);
  797. auto e0 = registry.create();
  798. auto e1 = registry.create();
  799. registry.emplace<empty_type>(e0);
  800. registry.emplace<empty_type>(e1);
  801. ASSERT_EQ(listener.counter, 2);
  802. ASSERT_EQ(listener.last, e1);
  803. registry.emplace<int>(e1);
  804. registry.emplace<int>(e0);
  805. ASSERT_EQ(listener.counter, 4);
  806. ASSERT_EQ(listener.last, e0);
  807. registry.erase<empty_type>(e0);
  808. registry.erase<int>(e0);
  809. ASSERT_EQ(listener.counter, 2);
  810. ASSERT_EQ(listener.last, e0);
  811. registry.on_destroy<empty_type>().disconnect<&listener::decr<empty_type>>(listener);
  812. registry.on_destroy<int>().disconnect<&listener::decr<int>>(listener);
  813. registry.erase<empty_type>(e1);
  814. registry.erase<int>(e1);
  815. ASSERT_EQ(listener.counter, 2);
  816. ASSERT_EQ(listener.last, e0);
  817. registry.on_construct<empty_type>().disconnect<&listener::incr<empty_type>>(listener);
  818. registry.on_construct<int>().disconnect<&listener::incr<int>>(listener);
  819. registry.emplace<empty_type>(e1);
  820. registry.emplace<int>(e1);
  821. ASSERT_EQ(listener.counter, 2);
  822. ASSERT_EQ(listener.last, e0);
  823. registry.on_construct<int>().connect<&listener::incr<int>>(listener);
  824. registry.on_destroy<int>().connect<&listener::decr<int>>(listener);
  825. registry.emplace<int>(e0);
  826. registry.erase<int>(e1);
  827. ASSERT_EQ(listener.counter, 2);
  828. ASSERT_EQ(listener.last, e1);
  829. registry.on_construct<empty_type>().connect<&listener::incr<empty_type>>(listener);
  830. registry.on_destroy<empty_type>().connect<&listener::decr<empty_type>>(listener);
  831. registry.erase<empty_type>(e1);
  832. registry.emplace<empty_type>(e0);
  833. ASSERT_EQ(listener.counter, 2);
  834. ASSERT_EQ(listener.last, e0);
  835. registry.clear<empty_type>();
  836. registry.clear<int>();
  837. ASSERT_EQ(listener.counter, 0);
  838. ASSERT_EQ(listener.last, e0);
  839. registry.emplace<empty_type>(e0);
  840. registry.emplace<empty_type>(e1);
  841. registry.emplace<int>(e0);
  842. registry.emplace<int>(e1);
  843. registry.destroy(e1);
  844. ASSERT_EQ(listener.counter, 2);
  845. ASSERT_EQ(listener.last, e1);
  846. registry.erase<int>(e0);
  847. registry.erase<empty_type>(e0);
  848. registry.emplace_or_replace<int>(e0);
  849. registry.emplace_or_replace<empty_type>(e0);
  850. ASSERT_EQ(listener.counter, 2);
  851. ASSERT_EQ(listener.last, e0);
  852. registry.on_destroy<empty_type>().disconnect<&listener::decr<empty_type>>(listener);
  853. registry.on_destroy<int>().disconnect<&listener::decr<int>>(listener);
  854. registry.emplace_or_replace<empty_type>(e0);
  855. registry.emplace_or_replace<int>(e0);
  856. ASSERT_EQ(listener.counter, 2);
  857. ASSERT_EQ(listener.last, e0);
  858. registry.on_update<empty_type>().connect<&listener::incr<empty_type>>(listener);
  859. registry.on_update<int>().connect<&listener::incr<int>>(listener);
  860. registry.emplace_or_replace<empty_type>(e0);
  861. registry.emplace_or_replace<int>(e0);
  862. ASSERT_EQ(listener.counter, 4);
  863. ASSERT_EQ(listener.last, e0);
  864. registry.replace<empty_type>(e0);
  865. registry.replace<int>(e0);
  866. ASSERT_EQ(listener.counter, 6);
  867. ASSERT_EQ(listener.last, e0);
  868. }
  869. TEST(Registry, RangeDestroy) {
  870. entt::registry registry;
  871. const auto e0 = registry.create();
  872. const auto e1 = registry.create();
  873. const auto e2 = registry.create();
  874. registry.emplace<int>(e0);
  875. registry.emplace<char>(e0);
  876. registry.emplace<double>(e0);
  877. registry.emplace<int>(e1);
  878. registry.emplace<char>(e1);
  879. registry.emplace<int>(e2);
  880. ASSERT_TRUE(registry.valid(e0));
  881. ASSERT_TRUE(registry.valid(e1));
  882. ASSERT_TRUE(registry.valid(e2));
  883. {
  884. const auto view = registry.view<int, char>();
  885. registry.destroy(view.begin(), view.end());
  886. }
  887. ASSERT_FALSE(registry.valid(e0));
  888. ASSERT_FALSE(registry.valid(e1));
  889. ASSERT_TRUE(registry.valid(e2));
  890. {
  891. const auto view = registry.view<int>();
  892. registry.destroy(view.begin(), view.end());
  893. }
  894. ASSERT_FALSE(registry.valid(e0));
  895. ASSERT_FALSE(registry.valid(e1));
  896. ASSERT_FALSE(registry.valid(e2));
  897. }
  898. TEST(Registry, Insert) {
  899. entt::registry registry;
  900. const auto e0 = registry.create();
  901. const auto e1 = registry.create();
  902. const auto e2 = registry.create();
  903. registry.emplace<int>(e0);
  904. registry.emplace<char>(e0);
  905. registry.emplace<double>(e0);
  906. registry.emplace<int>(e1);
  907. registry.emplace<char>(e1);
  908. registry.emplace<int>(e2);
  909. ASSERT_FALSE(registry.all_of<float>(e0));
  910. ASSERT_FALSE(registry.all_of<float>(e1));
  911. ASSERT_FALSE(registry.all_of<float>(e2));
  912. const auto icview = registry.view<int, char>();
  913. registry.insert(icview.begin(), icview.end(), 3.f);
  914. ASSERT_EQ(registry.get<float>(e0), 3.f);
  915. ASSERT_EQ(registry.get<float>(e1), 3.f);
  916. ASSERT_FALSE(registry.all_of<float>(e2));
  917. registry.clear<float>();
  918. float value[3]{0.f, 1.f, 2.f};
  919. const auto iview = registry.view<int>();
  920. registry.insert<float>(iview.data(), iview.data() + iview.size(), value, value + iview.size());
  921. ASSERT_EQ(registry.get<float>(e0), 0.f);
  922. ASSERT_EQ(registry.get<float>(e1), 1.f);
  923. ASSERT_EQ(registry.get<float>(e2), 2.f);
  924. }
  925. TEST(Registry, Erase) {
  926. entt::registry registry;
  927. const auto e0 = registry.create();
  928. const auto e1 = registry.create();
  929. const auto e2 = registry.create();
  930. registry.emplace<int>(e0);
  931. registry.emplace<char>(e0);
  932. registry.emplace<double>(e0);
  933. registry.emplace<int>(e1);
  934. registry.emplace<char>(e1);
  935. registry.emplace<int>(e2);
  936. ASSERT_TRUE(registry.all_of<int>(e0));
  937. ASSERT_TRUE(registry.all_of<int>(e1));
  938. ASSERT_TRUE(registry.all_of<int>(e2));
  939. const auto view = registry.view<int, char>();
  940. registry.erase<int>(e0);
  941. registry.erase<int>(view.begin(), view.end());
  942. registry.erase<int>(view.begin(), view.end());
  943. ASSERT_DEATH(registry.erase<int>(e0), "");
  944. ASSERT_DEATH(registry.erase<int>(e1), "");
  945. ASSERT_FALSE(registry.all_of<int>(e0));
  946. ASSERT_FALSE(registry.all_of<int>(e1));
  947. ASSERT_TRUE(registry.all_of<int>(e2));
  948. }
  949. TEST(Registry, Remove) {
  950. entt::registry registry;
  951. const auto e0 = registry.create();
  952. const auto e1 = registry.create();
  953. const auto e2 = registry.create();
  954. registry.emplace<int>(e0);
  955. registry.emplace<char>(e0);
  956. registry.emplace<double>(e0);
  957. registry.emplace<int>(e1);
  958. registry.emplace<char>(e1);
  959. registry.emplace<int>(e2);
  960. ASSERT_TRUE(registry.all_of<int>(e0));
  961. ASSERT_TRUE(registry.all_of<int>(e1));
  962. ASSERT_TRUE(registry.all_of<int>(e2));
  963. const auto view = registry.view<int, char>();
  964. ASSERT_EQ(registry.remove<int>(e0), 1u);
  965. ASSERT_EQ(registry.remove<int>(view.begin(), view.end()), 1u);
  966. ASSERT_EQ(registry.remove<int>(view.begin(), view.end()), 0u);
  967. ASSERT_EQ(registry.remove<int>(e1), 0u);
  968. ASSERT_FALSE(registry.all_of<int>(e0));
  969. ASSERT_FALSE(registry.all_of<int>(e1));
  970. ASSERT_TRUE(registry.all_of<int>(e2));
  971. }
  972. TEST(Registry, NonOwningGroupInterleaved) {
  973. entt::registry registry;
  974. typename entt::entity entity = entt::null;
  975. entity = registry.create();
  976. registry.emplace<int>(entity);
  977. registry.emplace<char>(entity);
  978. const auto group = registry.group<>(entt::get<int, char>);
  979. entity = registry.create();
  980. registry.emplace<int>(entity);
  981. registry.emplace<char>(entity);
  982. std::size_t cnt{};
  983. group.each([&cnt](auto...) { ++cnt; });
  984. ASSERT_EQ(cnt, 2u);
  985. }
  986. TEST(Registry, FullOwningGroupInterleaved) {
  987. entt::registry registry;
  988. typename entt::entity entity = entt::null;
  989. entity = registry.create();
  990. registry.emplace<int>(entity);
  991. registry.emplace<char>(entity);
  992. const auto group = registry.group<int, char>();
  993. entity = registry.create();
  994. registry.emplace<int>(entity);
  995. registry.emplace<char>(entity);
  996. std::size_t cnt{};
  997. group.each([&cnt](auto...) { ++cnt; });
  998. ASSERT_EQ(cnt, 2u);
  999. }
  1000. TEST(Registry, PartialOwningGroupInterleaved) {
  1001. entt::registry registry;
  1002. typename entt::entity entity = entt::null;
  1003. entity = registry.create();
  1004. registry.emplace<int>(entity);
  1005. registry.emplace<char>(entity);
  1006. const auto group = registry.group<int>(entt::get<char>);
  1007. entity = registry.create();
  1008. registry.emplace<int>(entity);
  1009. registry.emplace<char>(entity);
  1010. std::size_t cnt{};
  1011. group.each([&cnt](auto...) { ++cnt; });
  1012. ASSERT_EQ(cnt, 2u);
  1013. }
  1014. TEST(Registry, NonOwningGroupSortInterleaved) {
  1015. entt::registry registry;
  1016. const auto group = registry.group<>(entt::get<int, char>);
  1017. const auto e0 = registry.create();
  1018. registry.emplace<int>(e0, 0);
  1019. registry.emplace<char>(e0, '0');
  1020. const auto e1 = registry.create();
  1021. registry.emplace<int>(e1, 1);
  1022. registry.emplace<char>(e1, '1');
  1023. registry.sort<int>(std::greater{});
  1024. registry.sort<char>(std::less{});
  1025. const auto e2 = registry.create();
  1026. registry.emplace<int>(e2, 2);
  1027. registry.emplace<char>(e2, '2');
  1028. group.each([e0, e1, e2](const auto entity, const auto &i, const auto &c) {
  1029. if(entity == e0) {
  1030. ASSERT_EQ(i, 0);
  1031. ASSERT_EQ(c, '0');
  1032. } else if(entity == e1) {
  1033. ASSERT_EQ(i, 1);
  1034. ASSERT_EQ(c, '1');
  1035. } else if(entity == e2) {
  1036. ASSERT_EQ(i, 2);
  1037. ASSERT_EQ(c, '2');
  1038. }
  1039. });
  1040. }
  1041. TEST(Registry, GetOrEmplace) {
  1042. entt::registry registry;
  1043. const auto entity = registry.create();
  1044. const auto value = registry.get_or_emplace<int>(entity, 3);
  1045. ASSERT_TRUE(registry.all_of<int>(entity));
  1046. ASSERT_EQ(registry.get<int>(entity), value);
  1047. ASSERT_EQ(registry.get<int>(entity), 3);
  1048. }
  1049. TEST(Registry, Constness) {
  1050. entt::registry registry;
  1051. ASSERT_TRUE((std::is_same_v<decltype(registry.emplace<int>({})), int &>));
  1052. ASSERT_TRUE((std::is_same_v<decltype(registry.emplace<empty_type>({})), void>));
  1053. ASSERT_TRUE((std::is_same_v<decltype(registry.get<int>({})), int &>));
  1054. ASSERT_TRUE((std::is_same_v<decltype(registry.get<int, const char>({})), std::tuple<int &, const char &>>));
  1055. ASSERT_TRUE((std::is_same_v<decltype(registry.try_get<int>({})), int *>));
  1056. ASSERT_TRUE((std::is_same_v<decltype(registry.try_get<int, const char>({})), std::tuple<int *, const char *>>));
  1057. ASSERT_TRUE((std::is_same_v<decltype(std::as_const(registry).get<int>({})), const int &>));
  1058. ASSERT_TRUE((std::is_same_v<decltype(std::as_const(registry).get<int, const char>({})), std::tuple<const int &, const char &>>));
  1059. ASSERT_TRUE((std::is_same_v<decltype(std::as_const(registry).try_get<int>({})), const int *>));
  1060. ASSERT_TRUE((std::is_same_v<decltype(std::as_const(registry).try_get<int, const char>({})), std::tuple<const int *, const char *>>));
  1061. }
  1062. TEST(Registry, MoveOnlyComponent) {
  1063. entt::registry registry;
  1064. // the purpose is to ensure that move only types are always accepted
  1065. registry.emplace<std::unique_ptr<int>>(registry.create());
  1066. }
  1067. TEST(Registry, NonDefaultConstructibleComponent) {
  1068. entt::registry registry;
  1069. // the purpose is to ensure that non default constructible type are always accepted
  1070. registry.emplace<non_default_constructible>(registry.create(), 42);
  1071. }
  1072. TEST(Registry, Dependencies) {
  1073. entt::registry registry;
  1074. const auto entity = registry.create();
  1075. // required because of an issue of VS2019
  1076. constexpr auto emplace_or_replace = &entt::registry::emplace_or_replace<double>;
  1077. constexpr auto remove = &entt::registry::remove<double>;
  1078. registry.on_construct<int>().connect<emplace_or_replace>();
  1079. registry.on_destroy<int>().connect<remove>();
  1080. registry.emplace<double>(entity, .3);
  1081. ASSERT_FALSE(registry.all_of<int>(entity));
  1082. ASSERT_EQ(registry.get<double>(entity), .3);
  1083. registry.emplace<int>(entity);
  1084. ASSERT_TRUE(registry.all_of<int>(entity));
  1085. ASSERT_EQ(registry.get<double>(entity), .0);
  1086. registry.erase<int>(entity);
  1087. ASSERT_FALSE((registry.any_of<int, double>(entity)));
  1088. registry.on_construct<int>().disconnect<emplace_or_replace>();
  1089. registry.on_destroy<int>().disconnect<remove>();
  1090. registry.emplace<int>(entity);
  1091. ASSERT_TRUE((registry.any_of<int, double>(entity)));
  1092. ASSERT_FALSE(registry.all_of<double>(entity));
  1093. }
  1094. TEST(Registry, StableEmplace) {
  1095. entt::registry registry;
  1096. registry.on_construct<int>().connect<&listener::sort<int>>();
  1097. registry.emplace<int>(registry.create(), 0);
  1098. ASSERT_EQ(registry.emplace<int>(registry.create(), 1), 1);
  1099. }
  1100. TEST(Registry, AssignEntities) {
  1101. entt::registry registry;
  1102. entt::entity entities[3];
  1103. registry.create(std::begin(entities), std::end(entities));
  1104. registry.destroy(entities[1]);
  1105. registry.destroy(entities[2]);
  1106. entt::registry other;
  1107. const auto *data = registry.data();
  1108. other.assign(data, data + registry.size(), registry.destroyed());
  1109. ASSERT_EQ(registry.size(), other.size());
  1110. ASSERT_TRUE(other.valid(entities[0]));
  1111. ASSERT_FALSE(other.valid(entities[1]));
  1112. ASSERT_FALSE(other.valid(entities[2]));
  1113. ASSERT_EQ(registry.create(), other.create());
  1114. ASSERT_EQ(other.entity(other.create()), entities[1]);
  1115. }
  1116. TEST(Registry, Visit) {
  1117. entt::registry registry;
  1118. const auto entity = registry.create();
  1119. const auto other = registry.create();
  1120. registry.emplace<int>(entity);
  1121. registry.emplace<double>(other);
  1122. registry.emplace<char>(entity);
  1123. bool hasType[3]{};
  1124. registry.visit([&hasType](auto info) {
  1125. hasType[0] = hasType[0] || (info.hash() == entt::type_hash<int>::value());
  1126. hasType[1] = hasType[1] || (info.hash() == entt::type_hash<double>::value());
  1127. hasType[2] = hasType[2] || (info.hash() == entt::type_hash<char>::value());
  1128. });
  1129. ASSERT_TRUE(hasType[0] && hasType[1] && hasType[2]);
  1130. hasType[0] = hasType[1] = hasType[2] = false;
  1131. registry.visit(entity, [&hasType](auto info) {
  1132. hasType[0] = hasType[0] || (info.hash() == entt::type_hash<int>::value());
  1133. hasType[1] = hasType[1] || (info.hash() == entt::type_hash<double>::value());
  1134. hasType[2] = hasType[2] || (info.hash() == entt::type_hash<char>::value());
  1135. });
  1136. ASSERT_TRUE(hasType[0] && !hasType[1] && hasType[2]);
  1137. hasType[0] = hasType[2] = false;
  1138. registry.visit(other, [&hasType](auto info) {
  1139. hasType[0] = hasType[0] || (info.hash() == entt::type_hash<int>::value());
  1140. hasType[1] = hasType[1] || (info.hash() == entt::type_hash<double>::value());
  1141. hasType[2] = hasType[2] || (info.hash() == entt::type_hash<char>::value());
  1142. });
  1143. ASSERT_TRUE(!hasType[0] && hasType[1] && !hasType[2]);
  1144. hasType[1] = false;
  1145. }
  1146. TEST(Registry, ScramblingPoolsIsAllowed) {
  1147. entt::registry registry;
  1148. registry.on_destroy<int>().connect<&listener::sort<int>>();
  1149. for(std::size_t i{}; i < 2u; ++i) {
  1150. const auto entity = registry.create();
  1151. registry.emplace<int>(entity, static_cast<int>(i));
  1152. }
  1153. registry.destroy(registry.view<int>().back());
  1154. // thanks to @andranik3949 for pointing out this missing test
  1155. registry.view<const int>().each([](const auto entity, const auto &value) {
  1156. ASSERT_EQ(entt::to_integral(entity), value);
  1157. });
  1158. }