| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998 |
- #include <algorithm>
- #include <array>
- #include <cstddef>
- #include <memory>
- #include <tuple>
- #include <type_traits>
- #include <unordered_set>
- #include <utility>
- #include <gtest/gtest.h>
- #include <entt/core/iterator.hpp>
- #include <entt/core/type_info.hpp>
- #include <entt/entity/component.hpp>
- #include <entt/entity/entity.hpp>
- #include <entt/entity/storage.hpp>
- #include "../../common/config.h"
- #include "../../common/linter.hpp"
- #include "../../common/throwing_allocator.hpp"
- #include "../../common/throwing_type.hpp"
- #include "../../common/tracked_memory_resource.hpp"
- #include "../../common/value_type.h"
- struct StorageBase: testing::Test {
- enum class my_entity : std::uint32_t {};
- struct update_from_destructor {
- update_from_destructor(entt::basic_storage<update_from_destructor, my_entity> &ref, my_entity other)
- : storage{&ref},
- target{other} {}
- update_from_destructor(const update_from_destructor &) = delete;
- update_from_destructor &operator=(const update_from_destructor &) = delete;
- update_from_destructor(update_from_destructor &&other) noexcept
- : storage{std::exchange(other.storage, nullptr)},
- target{std::exchange(other.target, entt::null)} {}
- update_from_destructor &operator=(update_from_destructor &&other) noexcept {
- storage = std::exchange(other.storage, nullptr);
- target = std::exchange(other.target, entt::null);
- return *this;
- }
- ~update_from_destructor() {
- if(target != entt::null && storage->contains(target)) {
- storage->erase(target);
- }
- }
- private:
- entt::basic_storage<update_from_destructor, my_entity> *storage{};
- my_entity target{entt::null};
- };
- struct create_from_constructor {
- create_from_constructor(entt::basic_storage<create_from_constructor, my_entity> &ref, my_entity other)
- : child{other} {
- if(child != entt::null) {
- ref.emplace(child, ref, entt::null);
- }
- }
- my_entity child;
- };
- };
- template<>
- struct entt::component_traits<std::unordered_set<char>, StorageBase::my_entity> {
- static constexpr auto in_place_delete = true;
- static constexpr auto page_size = 4u;
- };
- template<>
- struct entt::component_traits<int, StorageBase::my_entity> {
- static constexpr auto in_place_delete = false;
- static constexpr auto page_size = 128u;
- };
- template<typename Type>
- struct Storage: StorageBase {
- static_assert(entt::component_traits<Type, my_entity>::page_size != 0u, "Empty type not allowed");
- using type = Type;
- };
- template<typename Type>
- using StorageDeathTest = Storage<Type>;
- using StorageTypes = ::testing::Types<int, test::pointer_stable, test::non_trivially_destructible, test::pointer_stable_non_trivially_destructible>;
- TYPED_TEST_SUITE(Storage, StorageTypes, );
- TYPED_TEST_SUITE(StorageDeathTest, StorageTypes, );
- TYPED_TEST(Storage, Constructors) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- ASSERT_EQ(pool.policy(), entt::deletion_policy{traits_type::in_place_delete});
- ASSERT_NO_THROW([[maybe_unused]] auto alloc = pool.get_allocator());
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- pool = entt::basic_storage<value_type, entity_type>{std::allocator<value_type>{}};
- ASSERT_EQ(pool.policy(), entt::deletion_policy{traits_type::in_place_delete});
- ASSERT_NO_THROW([[maybe_unused]] auto alloc = pool.get_allocator());
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- }
- TYPED_TEST(Storage, Move) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{3}, entity_type{2}};
- pool.emplace(entity[0u], 3);
- static_assert(std::is_move_constructible_v<decltype(pool)>, "Move constructible type required");
- static_assert(std::is_move_assignable_v<decltype(pool)>, "Move assignable type required");
- entt::basic_storage<value_type, entity_type> other{std::move(pool)};
- test::is_initialized(pool);
- ASSERT_TRUE(pool.empty());
- ASSERT_FALSE(other.empty());
- ASSERT_EQ(other.info(), entt::type_id<value_type>());
- ASSERT_EQ(other.index(entity[0u]), 0u);
- ASSERT_EQ(other.get(entity[0u]), value_type{3});
- entt::basic_storage<value_type, entity_type> extended{std::move(other), std::allocator<value_type>{}};
- test::is_initialized(other);
- ASSERT_TRUE(other.empty());
- ASSERT_FALSE(extended.empty());
- ASSERT_EQ(extended.info(), entt::type_id<value_type>());
- ASSERT_EQ(extended.index(entity[0u]), 0u);
- ASSERT_EQ(extended.get(entity[0u]), value_type{3});
- pool = std::move(extended);
- test::is_initialized(extended);
- ASSERT_FALSE(pool.empty());
- ASSERT_TRUE(other.empty());
- ASSERT_TRUE(extended.empty());
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- ASSERT_EQ(pool.index(entity[0u]), 0u);
- ASSERT_EQ(pool.get(entity[0u]), value_type{3});
- other = entt::basic_storage<value_type, entity_type>{};
- other.emplace(entity[1u], 2);
- other = std::move(pool);
- test::is_initialized(pool);
- ASSERT_FALSE(pool.empty());
- ASSERT_FALSE(other.empty());
- ASSERT_EQ(other.info(), entt::type_id<value_type>());
- ASSERT_EQ(other.index(entity[0u]), 0u);
- ASSERT_EQ(other.get(entity[0u]), value_type{3});
- }
- TYPED_TEST(Storage, Swap) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- entt::basic_storage<value_type, entity_type> other;
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- ASSERT_EQ(other.info(), entt::type_id<value_type>());
- pool.emplace(entity_type{4}, 1);
- other.emplace(entity_type{2}, 2);
- other.emplace(entity_type{1}, 3);
- other.erase(entity_type{2});
- ASSERT_EQ(pool.size(), 1u);
- ASSERT_EQ(other.size(), 1u + traits_type::in_place_delete);
- pool.swap(other);
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- ASSERT_EQ(other.info(), entt::type_id<value_type>());
- ASSERT_EQ(pool.size(), 1u + traits_type::in_place_delete);
- ASSERT_EQ(other.size(), 1u);
- ASSERT_EQ(pool.index(entity_type{1}), traits_type::in_place_delete);
- ASSERT_EQ(other.index(entity_type{4}), 0u);
- ASSERT_EQ(pool.get(entity_type{1}), value_type{3});
- ASSERT_EQ(other.get(entity_type{4}), value_type{1});
- }
- TYPED_TEST(Storage, Capacity) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- pool.reserve(64);
- ASSERT_EQ(pool.capacity(), traits_type::page_size);
- ASSERT_TRUE(pool.empty());
- pool.reserve(0);
- ASSERT_EQ(pool.capacity(), traits_type::page_size);
- ASSERT_TRUE(pool.empty());
- }
- TYPED_TEST(Storage, ShrinkToFit) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- for(std::size_t next{}; next < traits_type::page_size; ++next) {
- pool.emplace(entity_type(next));
- }
- pool.emplace(entity_type{traits_type::page_size});
- pool.erase(entity_type{traits_type::page_size});
- pool.compact();
- ASSERT_EQ(pool.capacity(), 2 * traits_type::page_size);
- ASSERT_EQ(pool.size(), traits_type::page_size);
- pool.shrink_to_fit();
- ASSERT_EQ(pool.capacity(), traits_type::page_size);
- ASSERT_EQ(pool.size(), traits_type::page_size);
- pool.clear();
- ASSERT_EQ(pool.capacity(), traits_type::page_size);
- ASSERT_EQ(pool.size(), 0u);
- pool.shrink_to_fit();
- ASSERT_EQ(pool.capacity(), 0u);
- ASSERT_EQ(pool.size(), 0u);
- }
- TYPED_TEST(Storage, Raw) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 1);
- pool.emplace(entity_type{3}, 3);
- ASSERT_EQ(pool.raw()[0u][0u], value_type{1});
- ASSERT_EQ(std::as_const(pool).raw()[0u][1u], value_type{3});
- }
- TYPED_TEST(Storage, Iterator) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using iterator = entt::basic_storage<value_type, entity_type>::iterator;
- testing::StaticAssertTypeEq<typename iterator::value_type, value_type>();
- testing::StaticAssertTypeEq<typename iterator::pointer, value_type *>();
- testing::StaticAssertTypeEq<typename iterator::reference, value_type &>();
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 2);
- iterator end{pool.begin()};
- iterator begin{};
- begin = pool.end();
- std::swap(begin, end);
- ASSERT_EQ(begin, pool.begin());
- ASSERT_EQ(end, pool.end());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin.index(), 0);
- ASSERT_EQ(end.index(), -1);
- ASSERT_EQ(begin++, pool.begin());
- ASSERT_EQ(begin--, pool.end());
- ASSERT_EQ(begin + 1, pool.end());
- ASSERT_EQ(end - 1, pool.begin());
- ASSERT_EQ(++begin, pool.end());
- ASSERT_EQ(--begin, pool.begin());
- ASSERT_EQ(begin += 1, pool.end());
- ASSERT_EQ(begin -= 1, pool.begin());
- ASSERT_EQ(begin + (end - begin), pool.end());
- ASSERT_EQ(begin - (begin - end), pool.end());
- ASSERT_EQ(end - (end - begin), pool.begin());
- ASSERT_EQ(end + (begin - end), pool.begin());
- ASSERT_EQ(begin[0u], *pool.begin().operator->());
- ASSERT_LT(begin, end);
- ASSERT_LE(begin, pool.begin());
- ASSERT_GT(end, begin);
- ASSERT_GE(end, pool.end());
- ASSERT_EQ(begin.index(), 0);
- ASSERT_EQ(end.index(), -1);
- pool.emplace(entity_type{3}, 4);
- begin = pool.begin();
- ASSERT_EQ(begin.index(), 1);
- ASSERT_EQ(end.index(), -1);
- ASSERT_EQ(begin[0u], value_type{4});
- ASSERT_EQ(begin[1u], value_type{2});
- }
- TYPED_TEST(Storage, ConstIterator) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using iterator = entt::basic_storage<value_type, entity_type>::const_iterator;
- testing::StaticAssertTypeEq<typename iterator::value_type, value_type>();
- testing::StaticAssertTypeEq<typename iterator::pointer, const value_type *>();
- testing::StaticAssertTypeEq<typename iterator::reference, const value_type &>();
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 2);
- iterator cend{pool.cbegin()};
- iterator cbegin{};
- cbegin = pool.cend();
- std::swap(cbegin, cend);
- ASSERT_EQ(cbegin, std::as_const(pool).begin());
- ASSERT_EQ(cend, std::as_const(pool).end());
- ASSERT_EQ(cbegin, pool.cbegin());
- ASSERT_EQ(cend, pool.cend());
- ASSERT_NE(cbegin, cend);
- ASSERT_EQ(cbegin.index(), 0);
- ASSERT_EQ(cend.index(), -1);
- ASSERT_EQ(cbegin++, pool.cbegin());
- ASSERT_EQ(cbegin--, pool.cend());
- ASSERT_EQ(cbegin + 1, pool.cend());
- ASSERT_EQ(cend - 1, pool.cbegin());
- ASSERT_EQ(++cbegin, pool.cend());
- ASSERT_EQ(--cbegin, pool.cbegin());
- ASSERT_EQ(cbegin += 1, pool.cend());
- ASSERT_EQ(cbegin -= 1, pool.cbegin());
- ASSERT_EQ(cbegin + (cend - cbegin), pool.cend());
- ASSERT_EQ(cbegin - (cbegin - cend), pool.cend());
- ASSERT_EQ(cend - (cend - cbegin), pool.cbegin());
- ASSERT_EQ(cend + (cbegin - cend), pool.cbegin());
- ASSERT_EQ(cbegin[0u], *pool.cbegin().operator->());
- ASSERT_LT(cbegin, cend);
- ASSERT_LE(cbegin, pool.cbegin());
- ASSERT_GT(cend, cbegin);
- ASSERT_GE(cend, pool.cend());
- ASSERT_EQ(cbegin.index(), 0);
- ASSERT_EQ(cend.index(), -1);
- pool.emplace(entity_type{3}, 4);
- cbegin = pool.cbegin();
- ASSERT_EQ(cbegin.index(), 1);
- ASSERT_EQ(cend.index(), -1);
- ASSERT_EQ(cbegin[0u], value_type{4});
- ASSERT_EQ(cbegin[1u], value_type{2});
- }
- TYPED_TEST(Storage, ReverseIterator) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using reverse_iterator = entt::basic_storage<value_type, entity_type>::reverse_iterator;
- testing::StaticAssertTypeEq<typename reverse_iterator::value_type, value_type>();
- testing::StaticAssertTypeEq<typename reverse_iterator::pointer, value_type *>();
- testing::StaticAssertTypeEq<typename reverse_iterator::reference, value_type &>();
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 2);
- reverse_iterator end{pool.rbegin()};
- reverse_iterator begin{};
- begin = pool.rend();
- std::swap(begin, end);
- ASSERT_EQ(begin, pool.rbegin());
- ASSERT_EQ(end, pool.rend());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin.base().index(), -1);
- ASSERT_EQ(end.base().index(), 0);
- ASSERT_EQ(begin++, pool.rbegin());
- ASSERT_EQ(begin--, pool.rend());
- ASSERT_EQ(begin + 1, pool.rend());
- ASSERT_EQ(end - 1, pool.rbegin());
- ASSERT_EQ(++begin, pool.rend());
- ASSERT_EQ(--begin, pool.rbegin());
- ASSERT_EQ(begin += 1, pool.rend());
- ASSERT_EQ(begin -= 1, pool.rbegin());
- ASSERT_EQ(begin + (end - begin), pool.rend());
- ASSERT_EQ(begin - (begin - end), pool.rend());
- ASSERT_EQ(end - (end - begin), pool.rbegin());
- ASSERT_EQ(end + (begin - end), pool.rbegin());
- ASSERT_EQ(begin[0u], *pool.rbegin().operator->());
- ASSERT_LT(begin, end);
- ASSERT_LE(begin, pool.rbegin());
- ASSERT_GT(end, begin);
- ASSERT_GE(end, pool.rend());
- ASSERT_EQ(begin.base().index(), -1);
- ASSERT_EQ(end.base().index(), 0);
- pool.emplace(entity_type{3}, 4);
- begin = pool.rbegin();
- end = pool.rend();
- ASSERT_EQ(begin.base().index(), -1);
- ASSERT_EQ(end.base().index(), 1);
- ASSERT_EQ(begin[0u], value_type{2});
- ASSERT_EQ(begin[1u], value_type{4});
- }
- TYPED_TEST(Storage, ConstReverseIterator) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using const_reverse_iterator = entt::basic_storage<value_type, entity_type>::const_reverse_iterator;
- testing::StaticAssertTypeEq<typename const_reverse_iterator::value_type, value_type>();
- testing::StaticAssertTypeEq<typename const_reverse_iterator::pointer, const value_type *>();
- testing::StaticAssertTypeEq<typename const_reverse_iterator::reference, const value_type &>();
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 2);
- const_reverse_iterator cend{pool.crbegin()};
- const_reverse_iterator cbegin{};
- cbegin = pool.crend();
- std::swap(cbegin, cend);
- ASSERT_EQ(cbegin, std::as_const(pool).rbegin());
- ASSERT_EQ(cend, std::as_const(pool).rend());
- ASSERT_EQ(cbegin, pool.crbegin());
- ASSERT_EQ(cend, pool.crend());
- ASSERT_NE(cbegin, cend);
- ASSERT_EQ(cbegin.base().index(), -1);
- ASSERT_EQ(cend.base().index(), 0);
- ASSERT_EQ(cbegin++, pool.crbegin());
- ASSERT_EQ(cbegin--, pool.crend());
- ASSERT_EQ(cbegin + 1, pool.crend());
- ASSERT_EQ(cend - 1, pool.crbegin());
- ASSERT_EQ(++cbegin, pool.crend());
- ASSERT_EQ(--cbegin, pool.crbegin());
- ASSERT_EQ(cbegin += 1, pool.crend());
- ASSERT_EQ(cbegin -= 1, pool.crbegin());
- ASSERT_EQ(cbegin + (cend - cbegin), pool.crend());
- ASSERT_EQ(cbegin - (cbegin - cend), pool.crend());
- ASSERT_EQ(cend - (cend - cbegin), pool.crbegin());
- ASSERT_EQ(cend + (cbegin - cend), pool.crbegin());
- ASSERT_EQ(cbegin[0u], *pool.crbegin().operator->());
- ASSERT_LT(cbegin, cend);
- ASSERT_LE(cbegin, pool.crbegin());
- ASSERT_GT(cend, cbegin);
- ASSERT_GE(cend, pool.crend());
- ASSERT_EQ(cbegin.base().index(), -1);
- ASSERT_EQ(cend.base().index(), 0);
- pool.emplace(entity_type{3}, 4);
- cbegin = pool.crbegin();
- cend = pool.crend();
- ASSERT_EQ(cbegin.base().index(), -1);
- ASSERT_EQ(cend.base().index(), 1);
- ASSERT_EQ(cbegin[0u], value_type{2});
- ASSERT_EQ(cbegin[1u], value_type{4});
- }
- TYPED_TEST(Storage, IteratorConversion) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 2);
- const typename entt::basic_storage<value_type, entity_type>::iterator it = pool.begin();
- typename entt::basic_storage<value_type, entity_type>::const_iterator cit = it;
- testing::StaticAssertTypeEq<decltype(*it), value_type &>();
- testing::StaticAssertTypeEq<decltype(*cit), const value_type &>();
- ASSERT_EQ(*it.operator->(), value_type{2});
- ASSERT_EQ(*it.operator->(), *cit);
- ASSERT_EQ(it - cit, 0);
- ASSERT_EQ(cit - it, 0);
- ASSERT_LE(it, cit);
- ASSERT_LE(cit, it);
- ASSERT_GE(it, cit);
- ASSERT_GE(cit, it);
- ASSERT_EQ(it, cit);
- ASSERT_NE(++cit, it);
- }
- TYPED_TEST(Storage, IteratorPageSizeAwareness) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- static_assert(!std::is_same_v<value_type, int> || (traits_type::page_size != entt::component_traits<value_type *, entity_type>::page_size), "Different page size required");
- for(unsigned int next{}; next < traits_type::page_size; ++next) {
- pool.emplace(entity_type{next});
- }
- pool.emplace(entity_type{traits_type::page_size});
- // test the proper use of component traits by the storage iterator
- ASSERT_EQ(&pool.begin()[0], pool.raw()[1u]);
- ASSERT_EQ(&pool.begin()[traits_type::page_size], pool.raw()[0u]);
- }
- TYPED_TEST(Storage, Getters) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const entity_type entity{1};
- pool.emplace(entity, 3);
- testing::StaticAssertTypeEq<decltype(pool.get({})), value_type &>();
- testing::StaticAssertTypeEq<decltype(std::as_const(pool).get({})), const value_type &>();
- testing::StaticAssertTypeEq<decltype(pool.get_as_tuple({})), std::tuple<value_type &>>();
- testing::StaticAssertTypeEq<decltype(std::as_const(pool).get_as_tuple({})), std::tuple<const value_type &>>();
- ASSERT_EQ(pool.get(entity), value_type{3});
- ASSERT_EQ(std::as_const(pool).get(entity), value_type{3});
- ASSERT_EQ(pool.get_as_tuple(entity), std::make_tuple(value_type{3}));
- ASSERT_EQ(std::as_const(pool).get_as_tuple(entity), std::make_tuple(value_type{3}));
- }
- ENTT_DEBUG_TYPED_TEST(StorageDeathTest, Getters) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const entity_type entity{4};
- ASSERT_DEATH([[maybe_unused]] const auto &value = pool.get(entity), "");
- ASSERT_DEATH([[maybe_unused]] const auto &value = std::as_const(pool).get(entity), "");
- ASSERT_DEATH([[maybe_unused]] const auto value = pool.get_as_tuple(entity), "");
- ASSERT_DEATH([[maybe_unused]] const auto value = std::as_const(pool).get_as_tuple(entity), "");
- }
- TYPED_TEST(Storage, Value) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const entity_type entity{2};
- pool.emplace(entity);
- ASSERT_EQ(pool.value(entity), &pool.get(entity));
- }
- ENTT_DEBUG_TYPED_TEST(StorageDeathTest, Value) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- ASSERT_DEATH([[maybe_unused]] const void *value = pool.value(entity_type{2}), "");
- }
- TYPED_TEST(Storage, Emplace) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- testing::StaticAssertTypeEq<decltype(pool.emplace({})), value_type &>();
- ASSERT_EQ(pool.emplace(entity_type{3}), value_type{});
- ASSERT_EQ(pool.emplace(entity_type{1}, 2), value_type{2});
- }
- TEST(Storage, EmplaceAggregate) {
- using entity_type = StorageBase::my_entity;
- entt::basic_storage<test::aggregate, entity_type> pool;
- testing::StaticAssertTypeEq<decltype(pool.emplace({})), test::aggregate &>();
- // aggregate types with no args enter the non-aggregate path
- ASSERT_EQ(pool.emplace(entity_type{3}), test::aggregate{});
- // aggregate types with args work despite the lack of support in the standard library
- ASSERT_EQ(pool.emplace(entity_type{1}, 2), test::aggregate{2});
- }
- TEST(Storage, EmplaceSelfMoveSupport) {
- using entity_type = StorageBase::my_entity;
- // see #37 - this test shouldn't crash, that's all
- entt::basic_storage<std::unordered_set<int>, entity_type> pool;
- const entity_type entity{1};
- ASSERT_EQ(pool.policy(), entt::deletion_policy::swap_and_pop);
- pool.emplace(entity).insert(2);
- pool.erase(entity);
- ASSERT_FALSE(pool.contains(entity));
- }
- TEST(Storage, EmplaceSelfMoveSupportInPlaceDelete) {
- using entity_type = StorageBase::my_entity;
- // see #37 - this test shouldn't crash, that's all
- entt::basic_storage<std::unordered_set<char>, entity_type> pool;
- const entity_type entity{1};
- ASSERT_EQ(pool.policy(), entt::deletion_policy::in_place);
- pool.emplace(entity).insert(2);
- pool.erase(entity);
- ASSERT_FALSE(pool.contains(entity));
- }
- TYPED_TEST(Storage, TryEmplace) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- entt::basic_sparse_set<entity_type> &base = pool;
- const std::array entity{entity_type{1}, entity_type{3}};
- value_type instance{4};
- ASSERT_NE(base.push(entity[0u], &instance), base.end());
- ASSERT_EQ(pool.size(), 1u);
- ASSERT_EQ(base.index(entity[0u]), 0u);
- ASSERT_EQ(base.value(entity[0u]), &pool.get(entity[0u]));
- ASSERT_EQ(pool.get(entity[0u]), value_type{4});
- base.erase(entity[0u]);
- ASSERT_NE(base.push(entity.begin(), entity.end()), base.end());
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 3u);
- ASSERT_EQ(base.index(entity[0u]), 1u);
- ASSERT_EQ(base.index(entity[1u]), 2u);
- } else {
- ASSERT_EQ(pool.size(), 2u);
- ASSERT_EQ(base.index(entity[0u]), 0u);
- ASSERT_EQ(base.index(entity[1u]), 1u);
- }
- ASSERT_EQ(pool.get(entity[0u]), value_type{});
- ASSERT_EQ(pool.get(entity[1u]), value_type{});
- base.erase(entity.begin(), entity.end());
- ASSERT_NE(base.push(entity.rbegin(), entity.rend()), base.end());
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 5u);
- ASSERT_EQ(base.index(entity[0u]), 4u);
- ASSERT_EQ(base.index(entity[1u]), 3u);
- } else {
- ASSERT_EQ(pool.size(), 2u);
- ASSERT_EQ(base.index(entity[0u]), 1u);
- ASSERT_EQ(base.index(entity[1u]), 0u);
- }
- ASSERT_EQ(pool.get(entity[0u]), value_type{});
- ASSERT_EQ(pool.get(entity[1u]), value_type{});
- }
- TEST(Storage, TryEmplaceNonDefaultConstructible) {
- using value_type = std::pair<int &, int &>;
- using entity_type = StorageBase::my_entity;
- static_assert(!std::is_default_constructible_v<value_type>, "Default constructible types not allowed");
- entt::basic_storage<value_type, entity_type> pool;
- entt::basic_sparse_set<entity_type> &base = pool;
- const std::array entity{entity_type{1}, entity_type{3}};
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- ASSERT_EQ(pool.info(), base.info());
- ASSERT_FALSE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_EQ(base.push(entity[0u]), base.end());
- ASSERT_FALSE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_EQ(base.find(entity[0u]), base.end());
- ASSERT_TRUE(pool.empty());
- int value = 4;
- value_type instance{value, value};
- ASSERT_NE(base.push(entity[0u], &instance), base.end());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- base.erase(entity[0u]);
- ASSERT_TRUE(pool.empty());
- ASSERT_FALSE(pool.contains(entity[0u]));
- ASSERT_EQ(base.push(entity.begin(), entity.end()), base.end());
- ASSERT_FALSE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_EQ(base.find(entity[0u]), base.end());
- ASSERT_EQ(base.find(entity[1u]), base.end());
- ASSERT_TRUE(pool.empty());
- }
- TEST(Storage, TryEmplaceNonCopyConstructible) {
- using value_type = std::unique_ptr<int>;
- using entity_type = StorageBase::my_entity;
- static_assert(!std::is_copy_constructible_v<value_type>, "Copy constructible types not allowed");
- entt::basic_storage<value_type, entity_type> pool;
- entt::basic_sparse_set<entity_type> &base = pool;
- const std::array entity{entity_type{1}, entity_type{3}};
- ASSERT_EQ(pool.info(), entt::type_id<value_type>());
- ASSERT_EQ(pool.info(), base.info());
- ASSERT_FALSE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_NE(base.push(entity[0u]), base.end());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_NE(base.find(entity[0u]), base.end());
- ASSERT_FALSE(pool.empty());
- value_type instance = std::make_unique<int>(4);
- ASSERT_EQ(base.push(entity[1u], &instance), base.end());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- base.erase(entity[0u]);
- ASSERT_TRUE(pool.empty());
- ASSERT_FALSE(pool.contains(entity[0u]));
- ASSERT_NE(base.push(entity.begin(), entity.end()), base.end());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_TRUE(pool.contains(entity[1u]));
- ASSERT_NE(base.find(entity[0u]), base.end());
- ASSERT_NE(base.find(entity[1u]), base.end());
- ASSERT_FALSE(pool.empty());
- }
- TYPED_TEST(Storage, Patch) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const entity_type entity{2};
- auto callback = [](auto &&elem) {
- if constexpr(std::is_class_v<std::remove_reference_t<decltype(elem)>>) {
- ++elem.value;
- } else {
- ++elem;
- }
- };
- pool.emplace(entity, 0);
- ASSERT_EQ(pool.get(entity), value_type{0});
- pool.patch(entity);
- pool.patch(entity, callback);
- pool.patch(entity, callback, callback);
- ASSERT_EQ(pool.get(entity), value_type{3});
- }
- ENTT_DEBUG_TYPED_TEST(StorageDeathTest, Patch) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- ASSERT_DEATH(pool.patch(entt::null), "");
- }
- TYPED_TEST(Storage, Insert) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{1}, entity_type{3}};
- typename entt::basic_storage<value_type, entity_type>::iterator it{};
- it = pool.insert(entity.begin(), entity.end(), value_type{4});
- ASSERT_EQ(it, pool.cbegin());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_TRUE(pool.contains(entity[1u]));
- ASSERT_FALSE(pool.empty());
- ASSERT_EQ(pool.size(), 2u);
- ASSERT_EQ(pool.get(entity[0u]), value_type{4});
- ASSERT_EQ(pool.get(entity[1u]), value_type{4});
- ASSERT_EQ(*it++.operator->(), value_type{4});
- ASSERT_EQ(*it.operator->(), value_type{4});
- const std::array value{value_type{3}, value_type{1}};
- pool.erase(entity.begin(), entity.end());
- it = pool.insert(entity.rbegin(), entity.rend(), value.begin());
- ASSERT_EQ(it, pool.cbegin());
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 4u);
- ASSERT_EQ(pool.index(entity[0u]), 3u);
- ASSERT_EQ(pool.index(entity[1u]), 2u);
- } else {
- ASSERT_EQ(pool.size(), 2u);
- ASSERT_EQ(pool.index(entity[0u]), 1u);
- ASSERT_EQ(pool.index(entity[1u]), 0u);
- }
- ASSERT_EQ(pool.get(entity[0u]), value_type{1});
- ASSERT_EQ(pool.get(entity[1u]), value_type{3});
- ASSERT_EQ(*it++.operator->(), value_type{1});
- ASSERT_EQ(*it.operator->(), value_type{3});
- }
- TYPED_TEST(Storage, Erase) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{1}, entity_type{3}, entity_type{2}};
- const std::array value{value_type{1}, value_type{2}, value_type{4}};
- pool.insert(entity.begin(), entity.end(), value.begin());
- pool.erase(entity.begin(), entity.end());
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 3u);
- ASSERT_TRUE(pool.data()[2u] == entt::tombstone);
- } else {
- ASSERT_EQ(pool.size(), 0u);
- }
- pool.insert(entity.begin(), entity.end(), value.begin());
- pool.erase(entity.begin(), entity.begin() + 2u);
- ASSERT_EQ(*pool.begin(), value[2u]);
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 6u);
- ASSERT_EQ(pool.index(entity[2u]), 5u);
- } else {
- ASSERT_EQ(pool.size(), 1u);
- }
- pool.erase(entity[2u]);
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 6u);
- ASSERT_TRUE(pool.data()[5u] == entt::tombstone);
- } else {
- ASSERT_EQ(pool.size(), 0u);
- }
- }
- TYPED_TEST(Storage, CrossErase) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- entt::basic_sparse_set<entity_type> set;
- const std::array entity{entity_type{1}, entity_type{3}};
- pool.emplace(entity[0u], 1);
- pool.emplace(entity[1u], 3);
- set.push(entity[1u]);
- pool.erase(set.begin(), set.end());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_EQ(pool.raw()[0u][0u], value_type{1});
- }
- TYPED_TEST(Storage, Remove) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{1}, entity_type{3}, entity_type{2}};
- const std::array value{value_type{1}, value_type{2}, value_type{4}};
- pool.insert(entity.begin(), entity.end(), value.begin());
- ASSERT_EQ(pool.remove(entity.begin(), entity.end()), 3u);
- ASSERT_EQ(pool.remove(entity.begin(), entity.end()), 0u);
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 3u);
- ASSERT_TRUE(pool.data()[2u] == entt::tombstone);
- } else {
- ASSERT_EQ(pool.size(), 0u);
- }
- pool.insert(entity.begin(), entity.end(), value.begin());
- ASSERT_EQ(pool.remove(entity.begin(), entity.begin() + 2u), 2u);
- ASSERT_EQ(*pool.begin(), value[2u]);
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 6u);
- ASSERT_EQ(pool.index(entity[2u]), 5u);
- } else {
- ASSERT_EQ(pool.size(), 1u);
- }
- ASSERT_TRUE(pool.remove(entity[2u]));
- ASSERT_FALSE(pool.remove(entity[2u]));
- if constexpr(traits_type::in_place_delete) {
- ASSERT_EQ(pool.size(), 6u);
- ASSERT_TRUE(pool.data()[5u] == entt::tombstone);
- } else {
- ASSERT_EQ(pool.size(), 0u);
- }
- }
- TYPED_TEST(Storage, CrossRemove) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- entt::basic_sparse_set<entity_type> set;
- const std::array entity{entity_type{1}, entity_type{3}};
- pool.emplace(entity[0u], 1);
- pool.emplace(entity[1u], 3);
- set.push(entity[1u]);
- pool.remove(set.begin(), set.end());
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_EQ(pool.raw()[0u][0u], value_type{1});
- }
- TYPED_TEST(Storage, Clear) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{1}, entity_type{3}, entity_type{2}};
- pool.insert(entity.begin(), entity.end());
- ASSERT_EQ(pool.size(), 3u);
- pool.clear();
- ASSERT_EQ(pool.size(), 0u);
- pool.insert(entity.begin(), entity.end());
- pool.erase(entity[2u]);
- ASSERT_EQ(pool.size(), 2u + traits_type::in_place_delete);
- pool.clear();
- ASSERT_EQ(pool.size(), 0u);
- }
- TYPED_TEST(Storage, Compact) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- ASSERT_TRUE(pool.empty());
- pool.compact();
- ASSERT_TRUE(pool.empty());
- pool.emplace(entity_type{0}, value_type{0});
- pool.compact();
- ASSERT_EQ(pool.size(), 1u);
- pool.emplace(entity_type{4}, value_type{4});
- pool.erase(entity_type{0});
- ASSERT_EQ(pool.size(), 1u + traits_type::in_place_delete);
- ASSERT_EQ(pool.index(entity_type{4}), traits_type::in_place_delete);
- ASSERT_EQ(pool.get(entity_type{4}), value_type{4});
- pool.compact();
- ASSERT_EQ(pool.size(), 1u);
- ASSERT_EQ(pool.index(entity_type{4}), 0u);
- ASSERT_EQ(pool.get(entity_type{4}), value_type{4});
- pool.emplace(entity_type{0}, value_type{0});
- pool.compact();
- ASSERT_EQ(pool.size(), 2u);
- ASSERT_EQ(pool.index(entity_type{4}), 0u);
- ASSERT_EQ(pool.index(entity_type{0}), 1u);
- ASSERT_EQ(pool.get(entity_type{4}), value_type{4});
- ASSERT_EQ(pool.get(entity_type{0}), value_type{0});
- pool.erase(entity_type{0});
- pool.erase(entity_type{4});
- pool.compact();
- ASSERT_TRUE(pool.empty());
- }
- TYPED_TEST(Storage, SwapElements) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{1}, 1);
- pool.emplace(entity_type{2}, 3);
- pool.emplace(entity_type{4}, 8);
- pool.erase(entity_type{2});
- ASSERT_EQ(pool.get(entity_type{1}), value_type{1});
- ASSERT_EQ(pool.get(entity_type{4}), value_type{8});
- ASSERT_EQ(pool.index(entity_type{1}), 0u);
- ASSERT_EQ(pool.index(entity_type{4}), 1u + traits_type::in_place_delete);
- pool.swap_elements(entity_type{1}, entity_type{4});
- ASSERT_EQ(pool.get(entity_type{1}), value_type{1});
- ASSERT_EQ(pool.get(entity_type{4}), value_type{8});
- ASSERT_EQ(pool.index(entity_type{1}), 1u + traits_type::in_place_delete);
- ASSERT_EQ(pool.index(entity_type{4}), 0u);
- }
- TYPED_TEST(Storage, Iterable) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using iterator = entt::basic_storage<value_type, entity_type>::iterable::iterator;
- testing::StaticAssertTypeEq<typename iterator::value_type, std::tuple<entity_type, value_type &>>();
- testing::StaticAssertTypeEq<typename iterator::pointer, entt::input_iterator_pointer<std::tuple<entity_type, value_type &>>>();
- testing::StaticAssertTypeEq<typename iterator::reference, typename iterator::value_type>();
- entt::basic_storage<value_type, entity_type> pool;
- const entt::basic_sparse_set<entity_type> &base = pool;
- pool.emplace(entity_type{1}, 2);
- pool.emplace(entity_type{3}, 4);
- auto iterable = pool.each();
- iterator end{iterable.begin()};
- iterator begin{};
- begin = iterable.end();
- std::swap(begin, end);
- ASSERT_EQ(begin, iterable.begin());
- ASSERT_EQ(end, iterable.end());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin.base(), base.begin());
- ASSERT_EQ(end.base(), base.end());
- ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entity_type{3});
- ASSERT_EQ(std::get<1>(*begin.operator->().operator->()), value_type{4});
- ASSERT_EQ(std::get<0>(*begin), entity_type{3});
- ASSERT_EQ(std::get<1>(*begin), value_type{4});
- ASSERT_EQ(begin++, iterable.begin());
- ASSERT_EQ(begin.base(), ++base.begin());
- ASSERT_EQ(++begin, iterable.end());
- ASSERT_EQ(begin.base(), base.end());
- for(auto [entity, element]: iterable) {
- testing::StaticAssertTypeEq<decltype(entity), entity_type>();
- testing::StaticAssertTypeEq<decltype(element), value_type &>();
- ASSERT_TRUE(entity != entity_type{1} || element == value_type{2});
- ASSERT_TRUE(entity != entity_type{3} || element == value_type{4});
- }
- }
- TYPED_TEST(Storage, ConstIterable) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using iterator = entt::basic_storage<value_type, entity_type>::const_iterable::iterator;
- testing::StaticAssertTypeEq<typename iterator::value_type, std::tuple<entity_type, const value_type &>>();
- testing::StaticAssertTypeEq<typename iterator::pointer, entt::input_iterator_pointer<std::tuple<entity_type, const value_type &>>>();
- testing::StaticAssertTypeEq<typename iterator::reference, typename iterator::value_type>();
- entt::basic_storage<value_type, entity_type> pool;
- const entt::basic_sparse_set<entity_type> &base = pool;
- pool.emplace(entity_type{1}, 2);
- pool.emplace(entity_type{3}, 4);
- auto iterable = std::as_const(pool).each();
- iterator end{iterable.cbegin()};
- iterator begin{};
- begin = iterable.cend();
- std::swap(begin, end);
- ASSERT_EQ(begin, iterable.cbegin());
- ASSERT_EQ(end, iterable.cend());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin.base(), base.begin());
- ASSERT_EQ(end.base(), base.end());
- ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entity_type{3});
- ASSERT_EQ(std::get<1>(*begin.operator->().operator->()), value_type{4});
- ASSERT_EQ(std::get<0>(*begin), entity_type{3});
- ASSERT_EQ(std::get<1>(*begin), value_type{4});
- ASSERT_EQ(begin++, iterable.begin());
- ASSERT_EQ(begin.base(), ++base.begin());
- ASSERT_EQ(++begin, iterable.end());
- ASSERT_EQ(begin.base(), base.end());
- for(auto [entity, element]: iterable) {
- testing::StaticAssertTypeEq<decltype(entity), entity_type>();
- testing::StaticAssertTypeEq<decltype(element), const value_type &>();
- ASSERT_TRUE(entity != entity_type{1} || element == value_type{2});
- ASSERT_TRUE(entity != entity_type{3} || element == value_type{4});
- }
- }
- TYPED_TEST(Storage, IterableIteratorConversion) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{3}, 1);
- const typename entt::basic_storage<value_type, entity_type>::iterable::iterator it = pool.each().begin();
- typename entt::basic_storage<value_type, entity_type>::const_iterable::iterator cit = it;
- testing::StaticAssertTypeEq<decltype(*it), std::tuple<entity_type, value_type &>>();
- testing::StaticAssertTypeEq<decltype(*cit), std::tuple<entity_type, const value_type &>>();
- ASSERT_EQ(it, cit);
- ASSERT_NE(++cit, it);
- }
- TYPED_TEST(Storage, IterableAlgorithmCompatibility) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{3}, 1);
- const auto iterable = pool.each();
- const auto it = std::find_if(iterable.begin(), iterable.end(), [](auto args) { return std::get<0>(args) == entity_type{3}; });
- ASSERT_EQ(std::get<0>(*it), entity_type{3});
- }
- TYPED_TEST(Storage, ReverseIterable) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using iterator = entt::basic_storage<value_type, entity_type>::reverse_iterable::iterator;
- testing::StaticAssertTypeEq<typename iterator::value_type, std::tuple<entity_type, value_type &>>();
- testing::StaticAssertTypeEq<typename iterator::pointer, entt::input_iterator_pointer<std::tuple<entity_type, value_type &>>>();
- testing::StaticAssertTypeEq<typename iterator::reference, typename iterator::value_type>();
- entt::basic_storage<value_type, entity_type> pool;
- const entt::basic_sparse_set<entity_type> &base = pool;
- pool.emplace(entity_type{1}, 2);
- pool.emplace(entity_type{3}, 4);
- auto iterable = pool.reach();
- iterator end{iterable.begin()};
- iterator begin{};
- begin = iterable.end();
- std::swap(begin, end);
- ASSERT_EQ(begin, iterable.begin());
- ASSERT_EQ(end, iterable.end());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin.base(), base.rbegin());
- ASSERT_EQ(end.base(), base.rend());
- ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entity_type{1});
- ASSERT_EQ(std::get<1>(*begin.operator->().operator->()), value_type{2});
- ASSERT_EQ(std::get<0>(*begin), entity_type{1});
- ASSERT_EQ(std::get<1>(*begin), value_type{2});
- ASSERT_EQ(begin++, iterable.begin());
- ASSERT_EQ(begin.base(), ++base.rbegin());
- ASSERT_EQ(++begin, iterable.end());
- ASSERT_EQ(begin.base(), base.rend());
- for(auto [entity, element]: iterable) {
- testing::StaticAssertTypeEq<decltype(entity), entity_type>();
- testing::StaticAssertTypeEq<decltype(element), value_type &>();
- ASSERT_TRUE(entity != entity_type{1} || element == value_type{2});
- ASSERT_TRUE(entity != entity_type{3} || element == value_type{4});
- }
- }
- TYPED_TEST(Storage, ConstReverseIterable) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using iterator = entt::basic_storage<value_type, entity_type>::const_reverse_iterable::iterator;
- testing::StaticAssertTypeEq<typename iterator::value_type, std::tuple<entity_type, const value_type &>>();
- testing::StaticAssertTypeEq<typename iterator::pointer, entt::input_iterator_pointer<std::tuple<entity_type, const value_type &>>>();
- testing::StaticAssertTypeEq<typename iterator::reference, typename iterator::value_type>();
- entt::basic_storage<value_type, entity_type> pool;
- const entt::basic_sparse_set<entity_type> &base = pool;
- pool.emplace(entity_type{1}, 2);
- pool.emplace(entity_type{3}, 4);
- auto iterable = std::as_const(pool).reach();
- iterator end{iterable.cbegin()};
- iterator begin{};
- begin = iterable.cend();
- std::swap(begin, end);
- ASSERT_EQ(begin, iterable.cbegin());
- ASSERT_EQ(end, iterable.cend());
- ASSERT_NE(begin, end);
- ASSERT_EQ(begin.base(), base.rbegin());
- ASSERT_EQ(end.base(), base.rend());
- ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entity_type{1});
- ASSERT_EQ(std::get<1>(*begin.operator->().operator->()), value_type{2});
- ASSERT_EQ(std::get<0>(*begin), entity_type{1});
- ASSERT_EQ(std::get<1>(*begin), value_type{2});
- ASSERT_EQ(begin++, iterable.begin());
- ASSERT_EQ(begin.base(), ++base.rbegin());
- ASSERT_EQ(++begin, iterable.end());
- ASSERT_EQ(begin.base(), base.rend());
- for(auto [entity, element]: iterable) {
- testing::StaticAssertTypeEq<decltype(entity), entity_type>();
- testing::StaticAssertTypeEq<decltype(element), const value_type &>();
- ASSERT_TRUE(entity != entity_type{1} || element == value_type{2});
- ASSERT_TRUE(entity != entity_type{3} || element == value_type{4});
- }
- }
- TYPED_TEST(Storage, ReverseIterableIteratorConversion) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{3}, 1);
- const typename entt::basic_storage<value_type, entity_type>::reverse_iterable::iterator it = pool.reach().begin();
- typename entt::basic_storage<value_type, entity_type>::const_reverse_iterable::iterator cit = it;
- testing::StaticAssertTypeEq<decltype(*it), std::tuple<entity_type, value_type &>>();
- testing::StaticAssertTypeEq<decltype(*cit), std::tuple<entity_type, const value_type &>>();
- ASSERT_EQ(it, cit);
- ASSERT_NE(++cit, it);
- }
- TYPED_TEST(Storage, ReverseIterableAlgorithmCompatibility) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{3}, 1);
- const auto iterable = pool.reach();
- const auto it = std::find_if(iterable.begin(), iterable.end(), [](auto args) { return std::get<0>(args) == entity_type{3}; });
- ASSERT_EQ(std::get<0>(*it), entity_type{3});
- }
- TYPED_TEST(Storage, SortOrdered) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{8}, entity_type{16}, entity_type{2}, entity_type{1}, entity_type{4}};
- const std::array value{value_type{8}, value_type{4}, value_type{2}, value_type{1}, value_type{0}};
- pool.insert(entity.begin(), entity.end(), value.begin());
- pool.sort([&pool](auto lhs, auto rhs) { return pool.get(lhs) < pool.get(rhs); });
- ASSERT_TRUE(std::equal(entity.rbegin(), entity.rend(), pool.entt::template basic_sparse_set<entity_type>::begin(), pool.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(value.rbegin(), value.rend(), pool.begin(), pool.end()));
- }
- TYPED_TEST(Storage, SortReverse) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{8}, entity_type{16}, entity_type{2}, entity_type{1}, entity_type{4}};
- const std::array value{value_type{0}, value_type{1}, value_type{2}, value_type{4}, value_type{8}};
- pool.insert(entity.begin(), entity.end(), value.begin());
- pool.sort([&pool](auto lhs, auto rhs) { return pool.get(lhs) < pool.get(rhs); });
- ASSERT_TRUE(std::equal(entity.begin(), entity.end(), pool.entt::template basic_sparse_set<entity_type>::begin(), pool.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(value.begin(), value.end(), pool.begin(), pool.end()));
- }
- TYPED_TEST(Storage, SortUnordered) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{8}, entity_type{16}, entity_type{2}, entity_type{1}, entity_type{4}};
- const std::array value{value_type{2}, value_type{1}, value_type{0}, value_type{4}, value_type{8}};
- pool.insert(entity.begin(), entity.end(), value.begin());
- pool.sort([&pool](auto lhs, auto rhs) { return pool.get(lhs) < pool.get(rhs); });
- auto begin = pool.begin();
- auto end = pool.end();
- ASSERT_EQ(*(begin++), value[2u]);
- ASSERT_EQ(*(begin++), value[1u]);
- ASSERT_EQ(*(begin++), value[0u]);
- ASSERT_EQ(*(begin++), value[3u]);
- ASSERT_EQ(*(begin++), value[4u]);
- ASSERT_EQ(begin, end);
- ASSERT_EQ(pool.data()[0u], entity[4u]);
- ASSERT_EQ(pool.data()[1u], entity[3u]);
- ASSERT_EQ(pool.data()[2u], entity[0u]);
- ASSERT_EQ(pool.data()[3u], entity[1u]);
- ASSERT_EQ(pool.data()[4u], entity[2u]);
- }
- TYPED_TEST(Storage, SortN) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- const std::array entity{entity_type{8}, entity_type{16}, entity_type{2}, entity_type{1}, entity_type{4}};
- const std::array value{value_type{1}, value_type{2}, value_type{0}, value_type{4}, value_type{8}};
- pool.insert(entity.begin(), entity.end(), value.begin());
- pool.sort_n(0u, [&pool](auto lhs, auto rhs) { return pool.get(lhs) < pool.get(rhs); });
- ASSERT_TRUE(std::equal(entity.rbegin(), entity.rend(), pool.entt::template basic_sparse_set<entity_type>::begin(), pool.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(value.rbegin(), value.rend(), pool.begin(), pool.end()));
- pool.sort_n(2u, [&pool](auto lhs, auto rhs) { return pool.get(lhs) < pool.get(rhs); });
- ASSERT_EQ(pool.raw()[0u][0u], value[1u]);
- ASSERT_EQ(pool.raw()[0u][1u], value[0u]);
- ASSERT_EQ(pool.raw()[0u][2u], value[2u]);
- ASSERT_EQ(pool.data()[0u], entity[1u]);
- ASSERT_EQ(pool.data()[1u], entity[0u]);
- ASSERT_EQ(pool.data()[2u], entity[2u]);
- const auto length = 5u;
- pool.sort_n(length, [&pool](auto lhs, auto rhs) { return pool.get(lhs) < pool.get(rhs); });
- auto begin = pool.begin();
- auto end = pool.end();
- ASSERT_EQ(*(begin++), value[2u]);
- ASSERT_EQ(*(begin++), value[0u]);
- ASSERT_EQ(*(begin++), value[1u]);
- ASSERT_EQ(*(begin++), value[3u]);
- ASSERT_EQ(*(begin++), value[4u]);
- ASSERT_EQ(begin, end);
- ASSERT_EQ(pool.data()[0u], entity[4u]);
- ASSERT_EQ(pool.data()[1u], entity[3u]);
- ASSERT_EQ(pool.data()[2u], entity[1u]);
- ASSERT_EQ(pool.data()[3u], entity[0u]);
- ASSERT_EQ(pool.data()[4u], entity[2u]);
- }
- TYPED_TEST(Storage, SortAsDisjoint) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> lhs;
- const entt::basic_storage<value_type, entity_type> rhs;
- const std::array entity{entity_type{1}, entity_type{2}, entity_type{4}};
- const std::array value{value_type{0}, value_type{1}, value_type{2}};
- lhs.insert(entity.begin(), entity.end(), value.begin());
- ASSERT_TRUE(std::equal(entity.rbegin(), entity.rend(), lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(value.rbegin(), value.rend(), lhs.begin(), lhs.end()));
- lhs.sort_as(rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end());
- ASSERT_TRUE(std::equal(entity.rbegin(), entity.rend(), lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(value.rbegin(), value.rend(), lhs.begin(), lhs.end()));
- }
- TYPED_TEST(Storage, SortAsOverlap) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> lhs;
- entt::basic_storage<value_type, entity_type> rhs;
- const std::array lhs_entity{entity_type{1}, entity_type{2}, entity_type{4}};
- const std::array lhs_value{value_type{0}, value_type{1}, value_type{2}};
- lhs.insert(lhs_entity.begin(), lhs_entity.end(), lhs_value.begin());
- const std::array rhs_entity{entity_type{2}};
- const std::array rhs_value{value_type{1}};
- rhs.insert(rhs_entity.begin(), rhs_entity.end(), rhs_value.begin());
- ASSERT_TRUE(std::equal(lhs_entity.rbegin(), lhs_entity.rend(), lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(lhs_value.rbegin(), lhs_value.rend(), lhs.begin(), lhs.end()));
- ASSERT_TRUE(std::equal(rhs_entity.rbegin(), rhs_entity.rend(), rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(rhs_value.rbegin(), rhs_value.rend(), rhs.begin(), rhs.end()));
- lhs.sort_as(rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end());
- auto begin = lhs.begin();
- auto end = lhs.end();
- ASSERT_EQ(*(begin++), lhs_value[1u]);
- ASSERT_EQ(*(begin++), lhs_value[2u]);
- ASSERT_EQ(*(begin++), lhs_value[0u]);
- ASSERT_EQ(begin, end);
- ASSERT_EQ(lhs.data()[0u], lhs_entity[0u]);
- ASSERT_EQ(lhs.data()[1u], lhs_entity[2u]);
- ASSERT_EQ(lhs.data()[2u], lhs_entity[1u]);
- }
- TYPED_TEST(Storage, SortAsOrdered) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> lhs;
- entt::basic_storage<value_type, entity_type> rhs;
- const std::array lhs_entity{entity_type{1}, entity_type{2}, entity_type{4}, entity_type{8}, entity_type{16}};
- const std::array lhs_value{value_type{0}, value_type{1}, value_type{2}, value_type{4}, value_type{8}};
- lhs.insert(lhs_entity.begin(), lhs_entity.end(), lhs_value.begin());
- const std::array rhs_entity{entity_type{32}, entity_type{1}, entity_type{2}, entity_type{4}, entity_type{8}, entity_type{16}};
- const std::array rhs_value{value_type{16}, value_type{0}, value_type{1}, value_type{2}, value_type{4}, value_type{8}};
- rhs.insert(rhs_entity.begin(), rhs_entity.end(), rhs_value.begin());
- ASSERT_TRUE(std::equal(lhs_entity.rbegin(), lhs_entity.rend(), lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(lhs_value.rbegin(), lhs_value.rend(), lhs.begin(), lhs.end()));
- ASSERT_TRUE(std::equal(rhs_entity.rbegin(), rhs_entity.rend(), rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(rhs_value.rbegin(), rhs_value.rend(), rhs.begin(), rhs.end()));
- rhs.sort_as(lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end());
- ASSERT_TRUE(std::equal(rhs_entity.rbegin(), rhs_entity.rend(), rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(rhs_value.rbegin(), rhs_value.rend(), rhs.begin(), rhs.end()));
- }
- TYPED_TEST(Storage, SortAsReverse) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> lhs;
- entt::basic_storage<value_type, entity_type> rhs;
- const std::array lhs_entity{entity_type{1}, entity_type{2}, entity_type{4}, entity_type{8}, entity_type{16}};
- const std::array lhs_value{value_type{0}, value_type{1}, value_type{2}, value_type{4}, value_type{8}};
- lhs.insert(lhs_entity.begin(), lhs_entity.end(), lhs_value.begin());
- const std::array rhs_entity{entity_type{16}, entity_type{8}, entity_type{4}, entity_type{2}, entity_type{1}, entity_type{32}};
- const std::array rhs_value{value_type{8}, value_type{4}, value_type{2}, value_type{1}, value_type{0}, value_type{16}};
- rhs.insert(rhs_entity.begin(), rhs_entity.end(), rhs_value.begin());
- ASSERT_TRUE(std::equal(lhs_entity.rbegin(), lhs_entity.rend(), lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(lhs_value.rbegin(), lhs_value.rend(), lhs.begin(), lhs.end()));
- ASSERT_TRUE(std::equal(rhs_entity.rbegin(), rhs_entity.rend(), rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(rhs_value.rbegin(), rhs_value.rend(), rhs.begin(), rhs.end()));
- rhs.sort_as(lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end());
- auto begin = rhs.begin();
- auto end = rhs.end();
- ASSERT_EQ(*(begin++), rhs_value[0u]);
- ASSERT_EQ(*(begin++), rhs_value[1u]);
- ASSERT_EQ(*(begin++), rhs_value[2u]);
- ASSERT_EQ(*(begin++), rhs_value[3u]);
- ASSERT_EQ(*(begin++), rhs_value[4u]);
- ASSERT_EQ(*(begin++), rhs_value[5u]);
- ASSERT_EQ(begin, end);
- ASSERT_EQ(rhs.data()[0u], rhs_entity[5u]);
- ASSERT_EQ(rhs.data()[1u], rhs_entity[4u]);
- ASSERT_EQ(rhs.data()[2u], rhs_entity[3u]);
- ASSERT_EQ(rhs.data()[3u], rhs_entity[2u]);
- ASSERT_EQ(rhs.data()[4u], rhs_entity[1u]);
- ASSERT_EQ(rhs.data()[5u], rhs_entity[0u]);
- }
- TYPED_TEST(Storage, SortAsUnordered) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> lhs;
- entt::basic_storage<value_type, entity_type> rhs;
- const std::array lhs_entity{entity_type{1}, entity_type{2}, entity_type{4}, entity_type{8}, entity_type{16}};
- const std::array lhs_value{value_type{0}, value_type{1}, value_type{2}, value_type{4}, value_type{8}};
- lhs.insert(lhs_entity.begin(), lhs_entity.end(), lhs_value.begin());
- const std::array rhs_entity{entity_type{4}, entity_type{2}, entity_type{32}, entity_type{1}, entity_type{8}, entity_type{16}};
- const std::array rhs_value{value_type{2}, value_type{1}, value_type{16}, value_type{0}, value_type{4}, value_type{8}};
- rhs.insert(rhs_entity.begin(), rhs_entity.end(), rhs_value.begin());
- ASSERT_TRUE(std::equal(lhs_entity.rbegin(), lhs_entity.rend(), lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(lhs_value.rbegin(), lhs_value.rend(), lhs.begin(), lhs.end()));
- ASSERT_TRUE(std::equal(rhs_entity.rbegin(), rhs_entity.rend(), rhs.entt::template basic_sparse_set<entity_type>::begin(), rhs.entt::template basic_sparse_set<entity_type>::end()));
- ASSERT_TRUE(std::equal(rhs_value.rbegin(), rhs_value.rend(), rhs.begin(), rhs.end()));
- rhs.sort_as(lhs.entt::template basic_sparse_set<entity_type>::begin(), lhs.entt::template basic_sparse_set<entity_type>::end());
- auto begin = rhs.begin();
- auto end = rhs.end();
- ASSERT_EQ(*(begin++), rhs_value[5u]);
- ASSERT_EQ(*(begin++), rhs_value[4u]);
- ASSERT_EQ(*(begin++), rhs_value[0u]);
- ASSERT_EQ(*(begin++), rhs_value[1u]);
- ASSERT_EQ(*(begin++), rhs_value[3u]);
- ASSERT_EQ(*(begin++), rhs_value[2u]);
- ASSERT_EQ(begin, end);
- ASSERT_EQ(rhs.data()[0u], rhs_entity[2u]);
- ASSERT_EQ(rhs.data()[1u], rhs_entity[3u]);
- ASSERT_EQ(rhs.data()[2u], rhs_entity[1u]);
- ASSERT_EQ(rhs.data()[3u], rhs_entity[0u]);
- ASSERT_EQ(rhs.data()[4u], rhs_entity[4u]);
- ASSERT_EQ(rhs.data()[5u], rhs_entity[5u]);
- }
- TEST(Storage, MoveOnlyComponent) {
- using value_type = std::unique_ptr<int>;
- using entity_type = StorageBase::my_entity;
- static_assert(!std::is_copy_assignable_v<value_type>, "Copy assignable types not allowed");
- static_assert(std::is_move_assignable_v<value_type>, "Move assignable type required");
- // the purpose is to ensure that move only types are always accepted
- [[maybe_unused]] const entt::basic_storage<value_type, entity_type> pool;
- }
- TEST(Storage, NonMovableComponent) {
- using value_type = std::pair<const int, const int>;
- using entity_type = StorageBase::my_entity;
- static_assert(!std::is_move_assignable_v<value_type>, "Move assignable types not allowed");
- // the purpose is to ensure that non-movable types are always accepted
- [[maybe_unused]] const entt::basic_storage<value_type, entity_type> pool;
- }
- ENTT_DEBUG_TEST(StorageDeathTest, NonMovableComponent) {
- using entity_type = StorageBase::my_entity;
- entt::basic_storage<std::pair<const int, const int>, entity_type> pool;
- const entity_type entity{0};
- const entity_type destroy{1};
- const entity_type other{2};
- pool.emplace(entity);
- pool.emplace(destroy);
- pool.emplace(other);
- pool.erase(destroy);
- ASSERT_DEATH(pool.swap_elements(entity, other), "");
- ASSERT_DEATH(pool.compact(), "");
- ASSERT_DEATH(pool.sort([](auto &&lhs, auto &&rhs) { return lhs < rhs; }), "");
- }
- TYPED_TEST(Storage, CanModifyDuringIteration) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- using traits_type = entt::component_traits<value_type, entity_type>;
- entt::basic_storage<value_type, entity_type> pool;
- auto *ptr = &pool.emplace(entity_type{0}, 2);
- ASSERT_EQ(pool.capacity(), traits_type::page_size);
- const auto it = pool.cbegin();
- pool.reserve(traits_type::page_size + 1u);
- ASSERT_EQ(pool.capacity(), 2 * traits_type::page_size);
- ASSERT_EQ(&pool.get(entity_type{0}), ptr);
- // this should crash with asan enabled if we break the constraint
- [[maybe_unused]] const auto &value = *it;
- }
- TYPED_TEST(Storage, ReferencesGuaranteed) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type> pool;
- pool.emplace(entity_type{0}, 0);
- pool.emplace(entity_type{1}, 1);
- ASSERT_EQ(pool.get(entity_type{0}), value_type{0});
- ASSERT_EQ(pool.get(entity_type{1}), value_type{1});
- for(auto &&elem: pool) {
- if(!(elem == value_type{})) {
- elem = value_type{4};
- }
- }
- ASSERT_EQ(pool.get(entity_type{0}), value_type{0});
- ASSERT_EQ(pool.get(entity_type{1}), value_type{4});
- auto begin = pool.begin();
- while(begin != pool.end()) {
- *(begin++) = value_type{3};
- }
- ASSERT_EQ(pool.get(entity_type{0}), value_type{3});
- ASSERT_EQ(pool.get(entity_type{1}), value_type{3});
- }
- TEST(Storage, UpdateFromDestructor) {
- using entity_type = StorageBase::my_entity;
- constexpr auto size = 10u;
- const std::array entity{entity_type{4u}, entity_type{2u}, entity_type{0u}};
- for(auto target: entity) {
- entt::basic_storage<StorageBase::update_from_destructor, entity_type> pool;
- for(std::size_t next{}; next < size; ++next) {
- const auto other = entity_type(next);
- pool.emplace(other, pool, other == entity_type(size / 2) ? target : other);
- }
- pool.erase(entity_type(size / 2));
- ASSERT_EQ(pool.size(), size - 1u - (target != entt::null));
- ASSERT_FALSE(pool.contains(entity_type(size / 2)));
- ASSERT_FALSE(pool.contains(target));
- pool.clear();
- ASSERT_TRUE(pool.empty());
- for(std::size_t next{}; next < size; ++next) {
- ASSERT_FALSE(pool.contains(entity_type(next)));
- }
- }
- }
- TEST(Storage, CreateFromConstructor) {
- using entity_type = StorageBase::my_entity;
- entt::basic_storage<StorageBase::create_from_constructor, entity_type> pool;
- const entity_type entity{0u};
- const entity_type other{1u};
- pool.emplace(entity, pool, other);
- ASSERT_EQ(pool.get(entity).child, other);
- ASSERT_EQ(pool.get(other).child, static_cast<entity_type>(entt::null));
- }
- TEST(Storage, ClassLevelNewDelete) {
- using entity_type = StorageBase::my_entity;
- entt::basic_storage<test::new_delete, entity_type> pool;
- const entity_type entity{0u};
- // yeah, that's for code coverage purposes only :)
- pool.emplace(entity, *std::make_unique<test::new_delete>(test::new_delete{3}));
- ASSERT_EQ(pool.get(entity).value, 3);
- }
- TYPED_TEST(Storage, CustomAllocator) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- const test::throwing_allocator<entity_type> allocator{};
- entt::basic_storage<value_type, entity_type, test::throwing_allocator<value_type>> pool{allocator};
- pool.reserve(1u);
- ASSERT_NE(pool.capacity(), 0u);
- pool.emplace(entity_type{0});
- pool.emplace(entity_type{1});
- decltype(pool) other{std::move(pool), allocator};
- test::is_initialized(pool);
- ASSERT_TRUE(pool.empty());
- ASSERT_FALSE(other.empty());
- ASSERT_NE(other.capacity(), 0u);
- ASSERT_EQ(other.size(), 2u);
- pool = std::move(other);
- test::is_initialized(other);
- ASSERT_FALSE(pool.empty());
- ASSERT_TRUE(other.empty());
- ASSERT_NE(pool.capacity(), 0u);
- ASSERT_EQ(pool.size(), 2u);
- other = {};
- pool.swap(other);
- pool = std::move(other);
- test::is_initialized(other);
- ASSERT_FALSE(pool.empty());
- ASSERT_TRUE(other.empty());
- ASSERT_NE(pool.capacity(), 0u);
- ASSERT_EQ(pool.size(), 2u);
- pool.clear();
- ASSERT_NE(pool.capacity(), 0u);
- ASSERT_EQ(pool.size(), 0u);
- }
- TYPED_TEST(Storage, ThrowingAllocator) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- entt::basic_storage<value_type, entity_type, test::throwing_allocator<value_type>> pool{};
- typename std::decay_t<decltype(pool)>::base_type &base = pool;
- constexpr auto packed_page_size = entt::component_traits<value_type, entity_type>::page_size;
- constexpr auto sparse_page_size = entt::entt_traits<entity_type>::page_size;
- pool.get_allocator().template throw_counter<value_type>(0u);
- ASSERT_THROW(pool.reserve(1u), test::throwing_allocator_exception);
- ASSERT_EQ(pool.capacity(), 0u);
- pool.get_allocator().template throw_counter<value_type>(1u);
- ASSERT_THROW(pool.reserve(2 * packed_page_size), test::throwing_allocator_exception);
- ASSERT_EQ(pool.capacity(), packed_page_size);
- pool.shrink_to_fit();
- ASSERT_EQ(pool.capacity(), 0u);
- pool.get_allocator().template throw_counter<entity_type>(0u);
- ASSERT_THROW(pool.emplace(entity_type{0}, 0), test::throwing_allocator_exception);
- ASSERT_FALSE(pool.contains(entity_type{0}));
- ASSERT_TRUE(pool.empty());
- pool.get_allocator().template throw_counter<entity_type>(0u);
- ASSERT_THROW(base.push(entity_type{0}), test::throwing_allocator_exception);
- ASSERT_FALSE(base.contains(entity_type{0}));
- ASSERT_TRUE(base.empty());
- pool.get_allocator().template throw_counter<value_type>(0u);
- ASSERT_THROW(pool.emplace(entity_type{0}, 0), test::throwing_allocator_exception);
- ASSERT_FALSE(pool.contains(entity_type{0}));
- ASSERT_NO_THROW(pool.compact());
- ASSERT_TRUE(pool.empty());
- pool.emplace(entity_type{0}, 0);
- const std::array entity{entity_type{1}, entity_type{sparse_page_size}};
- pool.get_allocator().template throw_counter<entity_type>(1u);
- ASSERT_THROW(pool.insert(entity.begin(), entity.end(), value_type{0}), test::throwing_allocator_exception);
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- pool.erase(entity[0u]);
- const std::array component{value_type{1}, value_type{sparse_page_size}};
- pool.get_allocator().template throw_counter<entity_type>(0u);
- pool.compact();
- ASSERT_THROW(pool.insert(entity.begin(), entity.end(), component.begin()), test::throwing_allocator_exception);
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- }
- TEST(Storage, ThrowingComponent) {
- using entity_type = StorageBase::my_entity;
- entt::basic_storage<test::throwing_type, entity_type> pool;
- const std::array entity{entity_type{4}, entity_type{1}};
- const std::array value{test::throwing_type{true}, test::throwing_type{false}};
- // strong exception safety
- ASSERT_THROW(pool.emplace(entity[0u], value[0u]), test::throwing_type_exception);
- ASSERT_TRUE(pool.empty());
- // basic exception safety
- ASSERT_THROW(pool.insert(entity.begin(), entity.end(), value[0u]), test::throwing_type_exception);
- ASSERT_EQ(pool.size(), 0u);
- ASSERT_FALSE(pool.contains(entity[1u]));
- // basic exception safety
- ASSERT_THROW(pool.insert(entity.begin(), entity.end(), value.begin()), test::throwing_type_exception);
- ASSERT_EQ(pool.size(), 0u);
- ASSERT_FALSE(pool.contains(entity[1u]));
- // basic exception safety
- ASSERT_THROW(pool.insert(entity.rbegin(), entity.rend(), value.rbegin()), test::throwing_type_exception);
- ASSERT_EQ(pool.size(), 1u);
- ASSERT_TRUE(pool.contains(entity[1u]));
- ASSERT_EQ(pool.get(entity[1u]), value[1u]);
- pool.clear();
- pool.emplace(entity[1u], value[0u].throw_on_copy());
- pool.emplace(entity[0u], value[1u].throw_on_copy());
- // basic exception safety
- ASSERT_THROW(pool.erase(entity[1u]), test::throwing_type_exception);
- ASSERT_EQ(pool.size(), 2u);
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_TRUE(pool.contains(entity[1u]));
- ASSERT_EQ(pool.index(entity[0u]), 1u);
- ASSERT_EQ(pool.index(entity[1u]), 0u);
- ASSERT_EQ(pool.get(entity[0u]), value[1u]);
- // the element may have been moved but it's still there
- ASSERT_EQ(pool.get(entity[1u]), value[0u]);
- pool.get(entity[1u]).throw_on_copy(false);
- pool.erase(entity[1u]);
- ASSERT_EQ(pool.size(), 1u);
- ASSERT_TRUE(pool.contains(entity[0u]));
- ASSERT_FALSE(pool.contains(entity[1u]));
- ASSERT_EQ(pool.index(entity[0u]), 0u);
- ASSERT_EQ(pool.get(entity[0u]), value[1u]);
- }
- #if defined(ENTT_HAS_TRACKED_MEMORY_RESOURCE)
- TYPED_TEST(Storage, NoUsesAllocatorConstruction) {
- using value_type = TestFixture::type;
- using entity_type = TestFixture::my_entity;
- test::tracked_memory_resource memory_resource{};
- entt::basic_storage<value_type, entity_type, std::pmr::polymorphic_allocator<value_type>> pool{&memory_resource};
- const entity_type entity{2};
- pool.emplace(entity);
- pool.erase(entity);
- memory_resource.reset();
- pool.emplace(entity, 0);
- ASSERT_TRUE(pool.get_allocator().resource()->is_equal(memory_resource));
- ASSERT_EQ(memory_resource.do_allocate_counter(), 0u);
- ASSERT_EQ(memory_resource.do_deallocate_counter(), 0u);
- }
- TEST(Storage, UsesAllocatorConstruction) {
- using string_type = test::tracked_memory_resource::string_type;
- using entity_type = StorageBase::my_entity;
- test::tracked_memory_resource memory_resource{};
- entt::basic_storage<string_type, entity_type, std::pmr::polymorphic_allocator<string_type>> pool{&memory_resource};
- const entity_type entity{2};
- pool.emplace(entity);
- pool.erase(entity);
- memory_resource.reset();
- pool.emplace(entity, test::tracked_memory_resource::default_value);
- ASSERT_TRUE(pool.get_allocator().resource()->is_equal(memory_resource));
- ASSERT_GT(memory_resource.do_allocate_counter(), 0u);
- ASSERT_EQ(memory_resource.do_deallocate_counter(), 0u);
- }
- #endif
|