diff --git a/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp b/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp index 1b2782c..ef7a6c9 100644 --- a/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp +++ b/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp @@ -98,7 +98,7 @@ class SlotMap [[nodiscard]] SlotKey Insert(ARGS&&... p_args) { - ASSERT(UtilsAssertHandler, m_data.size() < SlotKey::MAX_INDEX, "Too many elements for SlotMap!"); + ASSERT(UtilsAssertHandler, m_slots.size() < SlotKey::MAX_INDEX, "All slots have been exhausted!"); const typename SlotKey::IndexType dataIndex = static_cast(m_data.size()); m_data.emplace_back(std::forward(p_args)...); @@ -145,7 +145,8 @@ class SlotMap [[nodiscard]] bool Has(const SlotKey p_slotKey) const { - return p_slotKey.Index() < m_slots.size() && p_slotKey.Version() == m_slots[p_slotKey.Index()].Version(); + return p_slotKey.Valid() && + (p_slotKey.Index() < m_slots.size() && p_slotKey.Version() == m_slots[p_slotKey.Index()].Version()); } [[nodiscard]] @@ -192,7 +193,7 @@ class SlotMap } [[nodiscard]] - eastl::vector::size_type Empty() const + bool Empty() const { return m_data.empty(); } diff --git a/Bigfoot/Tests/Utils/Containers/SlotMap.cpp b/Bigfoot/Tests/Utils/Containers/SlotMap.cpp index 223c6cd..d28e755 100644 --- a/Bigfoot/Tests/Utils/Containers/SlotMap.cpp +++ b/Bigfoot/Tests/Utils/Containers/SlotMap.cpp @@ -154,6 +154,33 @@ TEST_F(SlotMapFixture, Remove_ShouldNotRecycleASlotWhenVersionWasExhausted) // Dead slot must not be recycled; a new insert must allocate a fresh slot index const auto newKey = m_slotMap.Insert(2); EXPECT_NE(newKey.Index(), key.Index()); + + // Ensure an invalid key does not return an exhausted slot + EXPECT_EQ(m_slotMap.Get(SlotMap::SlotKey {}), nullptr); +} + +/****************************************************************************************/ + +TEST_F(SlotMapFixture, Remove_ShouldNotDoAnythingInCaseOfDoubleRemove) +{ + const auto slotKey1 = m_slotMap.Insert(42); + const auto slotKey2 = m_slotMap.Insert(69); + + m_slotMap.Remove(slotKey1); + m_slotMap.Remove(slotKey1); + + EXPECT_EQ(*m_slotMap.Get(slotKey2), 69); +} + +/****************************************************************************************/ + +TEST_F(SlotMapFixture, Remove_ShouldNotDoAnythingInCaseStaleKey) +{ + const auto slotKey1 = m_slotMap.Insert(42); + + m_slotMap.Remove(SlotMap::SlotKey {2, 0}); + + EXPECT_EQ(*m_slotMap.Get(slotKey1), 42); } /****************************************************************************************/