Files
Bigfoot/Bigfoot/Tests/Utils/Containers/SlotMap.cpp
T
rboullard b7f4677c68
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 44s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 29s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 46s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 30s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 1m21s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 1m3s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m25s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 1m8s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 1m9s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m34s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 1m13s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 37s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 28s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 57s
Bigfoot / Sonarqube (push) Successful in 6m0s
sonarqube ERROR
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 49s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 30s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 52s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 36s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 1m23s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 44s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 59s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 51s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m5s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 56s
Bigfoot / Clang Format Checks (push) Successful in 13s
Bigfoot / Sonarqube (pull_request) Failing after 5m14s
BigFile/Sasquatch (#1)
Implementing an asset management system

BigFile is a sqlite database, for toolmode
Sasquatch is a custom binary file, for gamemode

Assets are built from flatbuffers
References allow to get an Asset, from an UUID, from anywhere in the code
HardReference refcount the asset and allow automatic removal

Reviewed-on: #1
Co-authored-by: Romain BOULLARD <romain.boullard@protonmail.com>
2026-09-01 16:32:01 +00:00

1446 lines
46 KiB
C++

/*********************************************************************
* \file SlotMap.cpp
*
* \author Romain BOULLARD
* \date August 2026
*********************************************************************/
#include <Utils/Containers/SlotMap.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace Bigfoot
{
class SlotMapKeyFixture: public ::testing::Test
{
protected:
using SlotMapKey = SlotMapKey<>;
};
/****************************************************************************************/
TEST_F(SlotMapKeyFixture, INVALID_VERSION_IsZero)
{
EXPECT_EQ(SlotMapKey::INVALID_VERSION, 0);
}
/****************************************************************************************/
TEST_F(SlotMapKeyFixture, MAX_INDEX_IsMaxU32)
{
EXPECT_EQ(SlotMapKey::MAX_INDEX, std::numeric_limits<std::uint32_t>::max());
}
/****************************************************************************************/
TEST_F(SlotMapKeyFixture, Valid_ShouldReturnFalseIfVersionIsInvalid)
{
EXPECT_FALSE(SlotMapKey().Valid());
EXPECT_FALSE(SlotMapKey(0, 0).Valid());
EXPECT_FALSE(SlotMapKey(SlotMapKey::INVALID_VERSION, 0).Valid());
EXPECT_FALSE(SlotMapKey(0, 42).Valid());
EXPECT_FALSE(SlotMapKey(SlotMapKey::INVALID_VERSION, 42).Valid());
}
/****************************************************************************************/
TEST_F(SlotMapKeyFixture, Valid_ShouldReturnTrueIfVersionisNotInvalid)
{
EXPECT_TRUE(SlotMapKey(1, 0).Valid());
EXPECT_TRUE(SlotMapKey(1, 42).Valid());
}
/****************************************************************************************/
TEST_F(SlotMapKeyFixture, GetVersion_ShouldReturnTheVersion)
{
EXPECT_EQ(SlotMapKey(0, 0).GetVersion(), 0);
EXPECT_EQ(SlotMapKey(42, 69).GetVersion(), 42);
}
/****************************************************************************************/
TEST_F(SlotMapKeyFixture, GetIndex_ShouldReturnTheIndex)
{
EXPECT_EQ(SlotMapKey(0, 0).GetIndex(), 0);
EXPECT_EQ(SlotMapKey(42, 69).GetIndex(), 69);
}
/****************************************************************************************/
class SlotMapValueMock
{
public:
SlotMapValueMock() = default;
virtual ~SlotMapValueMock() = default;
SlotMapValueMock(const SlotMapValueMock&) = delete;
SlotMapValueMock(SlotMapValueMock&&) = delete;
virtual void Destruct() = 0;
SlotMapValueMock& operator=(const SlotMapValueMock&) = delete;
SlotMapValueMock& operator=(SlotMapValueMock&&) = delete;
};
class SlotMapValue
{
public:
inline static SlotMapValueMock* ms_mock = nullptr;
SlotMapValue() = default;
SlotMapValue(const std::uint32_t p_value):
m_value(p_value)
{
}
SlotMapValue(const SlotMapValue& p_value) = default;
SlotMapValue(SlotMapValue&& p_value) = default;
~SlotMapValue()
{
if (ms_mock)
{
ms_mock->Destruct();
}
}
SlotMapValue& operator=(const SlotMapValue& p_value) = default;
SlotMapValue& operator=(SlotMapValue&& p_value) = default;
operator std::uint32_t() const
{
return m_value;
}
private:
std::uint32_t m_value = 0;
};
template<class SLOTMAP>
class SlotMapFixture: public ::testing::Test
{
protected:
class SlotMapValueMockImpl: public SlotMapValueMock
{
public:
MOCK_METHOD(void, Destruct, (), (override));
};
using SlotMapKey = SLOTMAP::SlotMapKey;
std::unique_ptr<SlotMapValueMockImpl> m_mock;
SLOTMAP m_slotMap;
void SetUp() override
{
m_mock = std::make_unique<::testing::NiceMock<SlotMapValueMockImpl>>();
SlotMapValue::ms_mock = m_mock.get();
}
void TearDown() override
{
SlotMapValue::ms_mock = nullptr;
m_mock.reset();
}
};
class SlotMapNameGenerator
{
public:
template<typename T>
static std::string GetName([[maybe_unused]] int p_index)
{
if constexpr (std::is_same_v<T, DenseSlotMap<SlotMapValue, SlotMapKey<std::uint8_t, std::uint8_t>>>)
{
return "DenseSlotMap";
}
if constexpr (std::is_same_v<T, StableSlotMap<SlotMapValue, SlotMapKey<std::uint8_t, std::uint8_t>>>)
{
return "StableSlotMap";
}
}
};
using SlotMapTypes = ::testing::Types<DenseSlotMap<SlotMapValue, SlotMapKey<std::uint8_t, std::uint8_t>>,
StableSlotMap<SlotMapValue, SlotMapKey<std::uint8_t, std::uint8_t>>>;
TYPED_TEST_SUITE(SlotMapFixture, SlotMapTypes, SlotMapNameGenerator);
// DenseSlotMap has no notion of pages, so the shared TYPED_TEST bodies below can't reference TypeParam::PAGE_SIZE
constexpr std::uint32_t PAGE_SIZE = StableSlotMap<SlotMapValue, SlotMapKey<std::uint8_t, std::uint8_t>>::PAGE_SIZE;
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Has_ReturnTrueIfSlotMapHasTheKey)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key = this->m_slotMap.Insert(42);
EXPECT_TRUE(this->m_slotMap.Has(key));
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Has_ReturnFalseIfSlotMapDoesNotHaveTheKey)
{
using SlotMapKey = TestFixture::SlotMapKey;
std::ignore = this->m_slotMap.Insert(42);
EXPECT_FALSE(this->m_slotMap.Has(SlotMapKey {1, 23}));
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Has_ReturnFalseForStaleKey)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey staleKey = this->m_slotMap.Insert(42);
this->m_slotMap.Remove(staleKey);
std::ignore = this->m_slotMap.Insert(69);
EXPECT_FALSE(this->m_slotMap.Has(staleKey));
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Size_ShouldReturnTheSizeOfTheSlotMap)
{
using SlotMapKey = TestFixture::SlotMapKey;
EXPECT_EQ(this->m_slotMap.Size(), 0);
const SlotMapKey key1 = this->m_slotMap.Insert(42);
const SlotMapKey key2 = this->m_slotMap.Insert(69);
EXPECT_EQ(this->m_slotMap.Size(), 2);
this->m_slotMap.Remove(key1);
EXPECT_EQ(this->m_slotMap.Size(), 1);
this->m_slotMap.Remove(key2);
EXPECT_EQ(this->m_slotMap.Size(), 0);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Empty_ShouldReturnTrueIfTheSlotMapIsEmpty)
{
using SlotMapKey = TestFixture::SlotMapKey;
EXPECT_TRUE(this->m_slotMap.Empty());
const SlotMapKey key1 = this->m_slotMap.Insert(42);
const SlotMapKey key2 = this->m_slotMap.Insert(69);
this->m_slotMap.Remove(key1);
this->m_slotMap.Remove(key2);
EXPECT_TRUE(this->m_slotMap.Empty());
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Empty_ShouldReturnFalseIfTheSlotMapIsNotEmpty)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key1 = this->m_slotMap.Insert(42);
EXPECT_FALSE(this->m_slotMap.Empty());
const SlotMapKey key2 = this->m_slotMap.Insert(69);
EXPECT_FALSE(this->m_slotMap.Empty());
this->m_slotMap.Remove(key1);
EXPECT_FALSE(this->m_slotMap.Empty());
this->m_slotMap.Remove(key2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Insert_ShouldReturnAValidSlotKey)
{
EXPECT_TRUE(this->m_slotMap.Insert(42).Valid());
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Insert_ShouldRecycleIndicesWhenPossible)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key1 = this->m_slotMap.Insert(42);
const SlotMapKey key2 = this->m_slotMap.Insert(69);
this->m_slotMap.Remove(key1);
this->m_slotMap.Remove(key2);
const SlotMapKey key11 = this->m_slotMap.Insert(42);
const SlotMapKey key21 = this->m_slotMap.Insert(69);
EXPECT_EQ(key1.GetVersion() + 1, key11.GetVersion());
EXPECT_EQ(key1.GetIndex(), key21.GetIndex()); // LIFO
EXPECT_EQ(key2.GetVersion() + 1, key21.GetVersion());
EXPECT_EQ(key2.GetIndex(), key11.GetIndex()); // LIFO
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Get_ShouldReturnTheValue)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key = this->m_slotMap.Insert(42);
EXPECT_EQ(*this->m_slotMap.Get(key), 42);
EXPECT_EQ(*std::as_const(this->m_slotMap).Get(key), 42);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Get_ShouldNullptrIfTheValueIsNotInMap)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key {1, 0};
EXPECT_EQ(this->m_slotMap.Get(key), nullptr);
EXPECT_EQ(std::as_const(this->m_slotMap).Get(key), nullptr);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Get_ShouldReturnNullptrForStaleKey)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey staleKey = this->m_slotMap.Insert(42);
this->m_slotMap.Remove(staleKey);
std::ignore = this->m_slotMap.Insert(69);
EXPECT_EQ(this->m_slotMap.Get(staleKey), nullptr);
EXPECT_EQ(std::as_const(this->m_slotMap).Get(staleKey), nullptr);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Remove_ShouldRemoveTheValue)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key = this->m_slotMap.Insert(42);
EXPECT_TRUE(this->m_slotMap.Has(key));
EXPECT_CALL(*this->m_mock, Destruct()).Times(1);
this->m_slotMap.Remove(key);
EXPECT_FALSE(this->m_slotMap.Has(key));
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Remove_ShouldBeANoOpForStaleKey)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey staleKey = this->m_slotMap.Insert(42);
this->m_slotMap.Remove(staleKey);
const SlotMapKey freshKey = this->m_slotMap.Insert(69);
this->m_slotMap.Remove(staleKey);
EXPECT_TRUE(this->m_slotMap.Has(freshKey));
EXPECT_EQ(*this->m_slotMap.Get(freshKey), 69);
EXPECT_EQ(this->m_slotMap.Size(), 1);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Remove_ExhaustingAllVersionsOfKeyMarksItDeadForGood)
{
using SlotMapKey = TestFixture::SlotMapKey;
for (typename SlotMapKey::VersionType version = 0; version < SlotMapKey::MAX_VERSION; ++version)
{
const SlotMapKey key = this->m_slotMap.Insert(42);
EXPECT_EQ(key.GetIndex(), 0);
EXPECT_EQ(key.GetVersion(), version + 1);
this->m_slotMap.Remove(key);
}
EXPECT_TRUE(this->m_slotMap.Empty());
const SlotMapKey key = this->m_slotMap.Insert(42);
EXPECT_EQ(key.GetIndex(), 1);
EXPECT_EQ(key.GetVersion(), 1);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Reset_ShouldClearTheMapAndOldKeysAreValid)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key1 = this->m_slotMap.Insert(42);
const SlotMapKey key2 = this->m_slotMap.Insert(69);
EXPECT_CALL(*this->m_mock, Destruct()).Times(2);
this->m_slotMap.Reset();
EXPECT_EQ(this->m_slotMap.Size(), 0);
EXPECT_TRUE(this->m_slotMap.Empty());
EXPECT_FALSE(this->m_slotMap.Has(key1));
EXPECT_FALSE(this->m_slotMap.Has(key2));
EXPECT_EQ(this->m_slotMap.Insert(42), key1);
EXPECT_EQ(this->m_slotMap.Insert(69), key2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Clear_ShouldClearTheMapAndOldKeysAreInvalid)
{
using SlotMapKey = TestFixture::SlotMapKey;
const SlotMapKey key1 = this->m_slotMap.Insert(42);
const SlotMapKey key2 = this->m_slotMap.Insert(69);
EXPECT_CALL(*this->m_mock, Destruct()).Times(2);
this->m_slotMap.Clear();
EXPECT_EQ(this->m_slotMap.Size(), 0);
EXPECT_TRUE(this->m_slotMap.Empty());
EXPECT_FALSE(this->m_slotMap.Has(key1));
EXPECT_FALSE(this->m_slotMap.Has(key2));
EXPECT_NE(this->m_slotMap.Insert(42), key1);
EXPECT_NE(this->m_slotMap.Insert(69), key2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldTraverseInInsertionOrder)
{
EXPECT_EQ(this->m_slotMap.begin(), this->m_slotMap.end());
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
std::uint32_t expected = 1;
for (auto it = this->m_slotMap.begin(); it != this->m_slotMap.end(); ++it)
{
EXPECT_EQ(*it, expected);
++expected;
}
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldSupportPreAndPostIncrementDecrement)
{
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
// Pre-increment advances and returns *this
auto preIt = this->m_slotMap.begin();
auto& preIncrementResult = ++preIt;
EXPECT_EQ(&preIncrementResult, &preIt);
EXPECT_EQ(*preIt, 2);
// Post-increment advances but returns the old value
auto postIt = this->m_slotMap.begin();
const auto postIncrementResult = postIt++;
EXPECT_EQ(*postIncrementResult, 1);
EXPECT_EQ(*postIt, 2);
// Pre-decrement retreats and returns *this
auto preDecIt = this->m_slotMap.end();
--preDecIt; // now at 3
auto& preDecrementResult = --preDecIt; // now at 2
EXPECT_EQ(&preDecrementResult, &preDecIt);
EXPECT_EQ(*preDecIt, 2);
// Post-decrement retreats but returns the old value
auto postDecIt = this->m_slotMap.end();
--postDecIt; // now at 3
const auto postDecrementResult = postDecIt--;
EXPECT_EQ(*postDecrementResult, 3);
EXPECT_EQ(*postDecIt, 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldSkipRemovedSlotsDuringTraversal)
{
using SlotMapKey = TestFixture::SlotMapKey;
std::ignore = this->m_slotMap.Insert(1);
const SlotMapKey key2 = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
this->m_slotMap.Remove(key2);
std::size_t count = 0;
std::uint32_t sum = 0;
for (const std::uint32_t value: this->m_slotMap)
{
++count;
sum += value;
}
EXPECT_EQ(count, 2);
EXPECT_EQ(sum, 4);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldTraverseAcrossMultiplePages)
{
// StableSlotMap's PAGE_SIZE is 64, so 100 elements force it to span pages
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
std::uint32_t expected = 0;
for (const std::uint32_t value: this->m_slotMap)
{
EXPECT_EQ(value, expected);
++expected;
}
EXPECT_EQ(expected, elementCount);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldCrossThePageBoundaryForwardAndBackward)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto boundaryIt = this->m_slotMap.begin();
for (std::uint32_t step = 0; step < PAGE_SIZE - 1; ++step)
{
++boundaryIt;
}
EXPECT_EQ(*boundaryIt, PAGE_SIZE - 1);
++boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE);
--boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE - 1);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldSupportPreAndPostIncrementDecrementAcrossTheBoundary)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto preBoundaryIt = this->m_slotMap.begin();
auto& preBoundaryIncrementResult = ++preBoundaryIt;
EXPECT_EQ(&preBoundaryIncrementResult, &preBoundaryIt);
EXPECT_EQ(*preBoundaryIt, 1);
auto postBoundaryIt = this->m_slotMap.begin();
const auto postBoundaryIncrementResult = postBoundaryIt++;
EXPECT_EQ(*postBoundaryIncrementResult, 0);
EXPECT_EQ(*postBoundaryIt, 1);
auto preBoundaryDecIt = this->m_slotMap.end();
--preBoundaryDecIt; // now at 99
auto& preBoundaryDecrementResult = --preBoundaryDecIt; // now at 98
EXPECT_EQ(&preBoundaryDecrementResult, &preBoundaryDecIt);
EXPECT_EQ(*preBoundaryDecIt, 98);
auto postBoundaryDecIt = this->m_slotMap.end();
--postBoundaryDecIt; // now at 99
const auto postBoundaryDecrementResult = postBoundaryDecIt--;
EXPECT_EQ(*postBoundaryDecrementResult, 99);
EXPECT_EQ(*postBoundaryDecIt, 98);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldSkipHolesStraddlingThePageBoundary)
{
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> keys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
keys[value] = this->m_slotMap.Insert(value);
}
this->m_slotMap.Remove(keys[PAGE_SIZE - 1]);
this->m_slotMap.Remove(keys[PAGE_SIZE]);
std::size_t count = 0;
for (const std::uint32_t value: this->m_slotMap)
{
EXPECT_NE(value, PAGE_SIZE - 1);
EXPECT_NE(value, PAGE_SIZE);
++count;
}
EXPECT_EQ(count, elementCount - 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldSkipAFullyEmptyLeadingPage)
{
// A fully empty leading page must be skipped by begin()/SeekFirstFrom
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> leadingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
leadingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = 0; value < PAGE_SIZE; ++value)
{
this->m_slotMap.Remove(leadingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (const std::uint32_t value: this->m_slotMap)
{
++count;
sum += value;
}
EXPECT_EQ(count, elementCount - PAGE_SIZE);
EXPECT_EQ(sum, 2934U); // 64 + 65 + ... + 99
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, Iterator_ShouldSkipAFullyEmptyTrailingPage)
{
// A fully empty trailing page must be skipped when retreating past it
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> trailingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
trailingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = PAGE_SIZE; value < elementCount; ++value)
{
this->m_slotMap.Remove(trailingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (const std::uint32_t value: this->m_slotMap)
{
++count;
sum += value;
}
EXPECT_EQ(count, PAGE_SIZE);
EXPECT_EQ(sum, 2016U); // 0 + 1 + ... + 63
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldTraverseInInsertionOrder)
{
const TypeParam& constMap = this->m_slotMap;
// const_iterator is reachable two ways: cbegin()/cend(), or begin()/end() on a const object
EXPECT_EQ(this->m_slotMap.cbegin(), this->m_slotMap.cend());
EXPECT_EQ(constMap.begin(), constMap.end());
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
std::uint32_t expected = 1;
for (auto it = this->m_slotMap.cbegin(); it != this->m_slotMap.cend(); ++it)
{
EXPECT_EQ(*it, expected);
++expected;
}
expected = 1;
for (auto it = constMap.begin(); it != constMap.end(); ++it)
{
EXPECT_EQ(*it, expected);
++expected;
}
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldSupportPreAndPostIncrementDecrement)
{
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
// Pre-increment advances and returns *this
auto preIt = this->m_slotMap.cbegin();
auto& preIncrementResult = ++preIt;
EXPECT_EQ(&preIncrementResult, &preIt);
EXPECT_EQ(*preIt, 2);
// Post-increment advances but returns the old value
auto postIt = this->m_slotMap.cbegin();
const auto postIncrementResult = postIt++;
EXPECT_EQ(*postIncrementResult, 1);
EXPECT_EQ(*postIt, 2);
// Pre-decrement retreats and returns *this
auto preDecIt = this->m_slotMap.cend();
--preDecIt; // now at 3
auto& preDecrementResult = --preDecIt; // now at 2
EXPECT_EQ(&preDecrementResult, &preDecIt);
EXPECT_EQ(*preDecIt, 2);
// Post-decrement retreats but returns the old value
auto postDecIt = this->m_slotMap.cend();
--postDecIt; // now at 3
const auto postDecrementResult = postDecIt--;
EXPECT_EQ(*postDecrementResult, 3);
EXPECT_EQ(*postDecIt, 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldSkipRemovedSlotsDuringTraversal)
{
using SlotMapKey = TestFixture::SlotMapKey;
const TypeParam& constMap = this->m_slotMap;
std::ignore = this->m_slotMap.Insert(1);
const SlotMapKey key2 = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
// Removed slots are skipped identically through cbegin()/cend() and the const-object overloads
this->m_slotMap.Remove(key2);
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.cbegin(); it != this->m_slotMap.cend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, 2);
EXPECT_EQ(sum, 4);
count = 0;
sum = 0;
for (auto it = constMap.begin(); it != constMap.end(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, 2);
EXPECT_EQ(sum, 4);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldTraverseAcrossMultiplePages)
{
// StableSlotMap's PAGE_SIZE is 64, so 100 elements force it to span pages
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
std::uint32_t expected = 0;
for (auto it = this->m_slotMap.cbegin(); it != this->m_slotMap.cend(); ++it)
{
EXPECT_EQ(*it, expected);
++expected;
}
EXPECT_EQ(expected, elementCount);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldCrossThePageBoundaryForwardAndBackward)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto boundaryIt = this->m_slotMap.cbegin();
for (std::uint32_t step = 0; step < PAGE_SIZE - 1; ++step)
{
++boundaryIt;
}
EXPECT_EQ(*boundaryIt, PAGE_SIZE - 1);
++boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE);
--boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE - 1);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldSupportPreAndPostIncrementDecrementAcrossTheBoundary)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto preBoundaryIt = this->m_slotMap.cbegin();
auto& preBoundaryIncrementResult = ++preBoundaryIt;
EXPECT_EQ(&preBoundaryIncrementResult, &preBoundaryIt);
EXPECT_EQ(*preBoundaryIt, 1);
auto postBoundaryIt = this->m_slotMap.cbegin();
const auto postBoundaryIncrementResult = postBoundaryIt++;
EXPECT_EQ(*postBoundaryIncrementResult, 0);
EXPECT_EQ(*postBoundaryIt, 1);
auto preBoundaryDecIt = this->m_slotMap.cend();
--preBoundaryDecIt; // now at 99
auto& preBoundaryDecrementResult = --preBoundaryDecIt; // now at 98
EXPECT_EQ(&preBoundaryDecrementResult, &preBoundaryDecIt);
EXPECT_EQ(*preBoundaryDecIt, 98);
auto postBoundaryDecIt = this->m_slotMap.cend();
--postBoundaryDecIt; // now at 99
const auto postBoundaryDecrementResult = postBoundaryDecIt--;
EXPECT_EQ(*postBoundaryDecrementResult, 99);
EXPECT_EQ(*postBoundaryDecIt, 98);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldSkipHolesStraddlingThePageBoundary)
{
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> keys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
keys[value] = this->m_slotMap.Insert(value);
}
this->m_slotMap.Remove(keys[PAGE_SIZE - 1]);
this->m_slotMap.Remove(keys[PAGE_SIZE]);
std::size_t count = 0;
for (auto it = this->m_slotMap.cbegin(); it != this->m_slotMap.cend(); ++it)
{
EXPECT_NE(*it, PAGE_SIZE - 1);
EXPECT_NE(*it, PAGE_SIZE);
++count;
}
EXPECT_EQ(count, elementCount - 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldSkipAFullyEmptyLeadingPage)
{
// A fully empty leading page must be skipped by cbegin()/SeekFirstFrom
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> leadingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
leadingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = 0; value < PAGE_SIZE; ++value)
{
this->m_slotMap.Remove(leadingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.cbegin(); it != this->m_slotMap.cend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, elementCount - PAGE_SIZE);
EXPECT_EQ(sum, 2934U); // 64 + 65 + ... + 99
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstIterator_ShouldSkipAFullyEmptyTrailingPage)
{
// A fully empty trailing page must be skipped when retreating past it
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> trailingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
trailingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = PAGE_SIZE; value < elementCount; ++value)
{
this->m_slotMap.Remove(trailingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.cbegin(); it != this->m_slotMap.cend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, PAGE_SIZE);
EXPECT_EQ(sum, 2016U); // 0 + 1 + ... + 63
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldTraverseInReverseInsertionOrder)
{
EXPECT_EQ(this->m_slotMap.rbegin(), this->m_slotMap.rend());
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
std::uint32_t expected = 3;
for (auto it = this->m_slotMap.rbegin(); it != this->m_slotMap.rend(); ++it)
{
EXPECT_EQ(*it, expected);
--expected;
}
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldSupportPreAndPostIncrementDecrement)
{
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
// Pre-increment advances and returns *this
auto preIt = this->m_slotMap.rbegin();
auto& preIncrementResult = ++preIt;
EXPECT_EQ(&preIncrementResult, &preIt);
EXPECT_EQ(*preIt, 2);
// Post-increment advances but returns the old value
auto postIt = this->m_slotMap.rbegin();
const auto postIncrementResult = postIt++;
EXPECT_EQ(*postIncrementResult, 3);
EXPECT_EQ(*postIt, 2);
// Pre-decrement retreats and returns *this
auto preDecIt = this->m_slotMap.rend();
--preDecIt; // now at 1
auto& preDecrementResult = --preDecIt; // now at 2
EXPECT_EQ(&preDecrementResult, &preDecIt);
EXPECT_EQ(*preDecIt, 2);
// Post-decrement retreats but returns the old value
auto postDecIt = this->m_slotMap.rend();
--postDecIt; // now at 1
const auto postDecrementResult = postDecIt--;
EXPECT_EQ(*postDecrementResult, 1);
EXPECT_EQ(*postDecIt, 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldSkipRemovedSlotsDuringTraversal)
{
using SlotMapKey = TestFixture::SlotMapKey;
std::ignore = this->m_slotMap.Insert(1);
const SlotMapKey key2 = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
this->m_slotMap.Remove(key2);
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.rbegin(); it != this->m_slotMap.rend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, 2);
EXPECT_EQ(sum, 4);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldTraverseAcrossMultiplePages)
{
// StableSlotMap's PAGE_SIZE is 64, so 100 elements force it to span pages
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
std::uint32_t expected = elementCount - 1;
for (auto it = this->m_slotMap.rbegin(); it != this->m_slotMap.rend(); ++it)
{
EXPECT_EQ(*it, expected);
--expected;
}
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldCrossThePageBoundaryForwardAndBackward)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
// Explicitly cross the page boundary (value 64 -> 63) forward and backward
auto boundaryIt = this->m_slotMap.rbegin();
while (*boundaryIt != PAGE_SIZE)
{
++boundaryIt;
}
++boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE - 1);
--boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldSupportPreAndPostIncrementDecrementAcrossTheBoundary)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto preBoundaryIt = this->m_slotMap.rbegin();
auto& preBoundaryIncrementResult = ++preBoundaryIt;
EXPECT_EQ(&preBoundaryIncrementResult, &preBoundaryIt);
EXPECT_EQ(*preBoundaryIt, 98);
auto postBoundaryIt = this->m_slotMap.rbegin();
const auto postBoundaryIncrementResult = postBoundaryIt++;
EXPECT_EQ(*postBoundaryIncrementResult, 99);
EXPECT_EQ(*postBoundaryIt, 98);
auto preBoundaryDecIt = this->m_slotMap.rend();
--preBoundaryDecIt; // now at 0
auto& preBoundaryDecrementResult = --preBoundaryDecIt; // now at 1
EXPECT_EQ(&preBoundaryDecrementResult, &preBoundaryDecIt);
EXPECT_EQ(*preBoundaryDecIt, 1);
auto postBoundaryDecIt = this->m_slotMap.rend();
--postBoundaryDecIt; // now at 0
const auto postBoundaryDecrementResult = postBoundaryDecIt--;
EXPECT_EQ(*postBoundaryDecrementResult, 0);
EXPECT_EQ(*postBoundaryDecIt, 1);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldSkipHolesStraddlingThePageBoundary)
{
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> keys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
keys[value] = this->m_slotMap.Insert(value);
}
this->m_slotMap.Remove(keys[PAGE_SIZE - 1]);
this->m_slotMap.Remove(keys[PAGE_SIZE]);
std::size_t count = 0;
for (auto it = this->m_slotMap.rbegin(); it != this->m_slotMap.rend(); ++it)
{
EXPECT_NE(*it, PAGE_SIZE - 1);
EXPECT_NE(*it, PAGE_SIZE);
++count;
}
EXPECT_EQ(count, elementCount - 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldSkipAFullyEmptyLeadingPage)
{
// A fully empty leading page must be skipped when retreating past it (rend() side)
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> leadingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
leadingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = 0; value < PAGE_SIZE; ++value)
{
this->m_slotMap.Remove(leadingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.rbegin(); it != this->m_slotMap.rend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, elementCount - PAGE_SIZE);
EXPECT_EQ(sum, 2934U); // 64 + 65 + ... + 99
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ReverseIterator_ShouldSkipAFullyEmptyTrailingPage)
{
// A fully empty trailing page must be skipped by rbegin()/SeekLastFrom
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> trailingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
trailingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = PAGE_SIZE; value < elementCount; ++value)
{
this->m_slotMap.Remove(trailingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.rbegin(); it != this->m_slotMap.rend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, PAGE_SIZE);
EXPECT_EQ(sum, 2016U); // 0 + 1 + ... + 63
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldTraverseInReverseInsertionOrder)
{
const TypeParam& constMap = this->m_slotMap;
// const_reverse_iterator is reachable two ways: crbegin()/crend(), or rbegin()/rend() on a
// const object
EXPECT_EQ(this->m_slotMap.crbegin(), this->m_slotMap.crend());
EXPECT_EQ(constMap.rbegin(), constMap.rend());
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
std::uint32_t expected = 3;
for (auto it = this->m_slotMap.crbegin(); it != this->m_slotMap.crend(); ++it)
{
EXPECT_EQ(*it, expected);
--expected;
}
expected = 3;
for (auto it = constMap.rbegin(); it != constMap.rend(); ++it)
{
EXPECT_EQ(*it, expected);
--expected;
}
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldSupportPreAndPostIncrementDecrement)
{
std::ignore = this->m_slotMap.Insert(1);
std::ignore = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
// Pre-increment advances and returns *this
auto preIt = this->m_slotMap.crbegin();
auto& preIncrementResult = ++preIt;
EXPECT_EQ(&preIncrementResult, &preIt);
EXPECT_EQ(*preIt, 2);
// Post-increment advances but returns the old value
auto postIt = this->m_slotMap.crbegin();
const auto postIncrementResult = postIt++;
EXPECT_EQ(*postIncrementResult, 3);
EXPECT_EQ(*postIt, 2);
// Pre-decrement retreats and returns *this
auto preDecIt = this->m_slotMap.crend();
--preDecIt; // now at 1
auto& preDecrementResult = --preDecIt; // now at 2
EXPECT_EQ(&preDecrementResult, &preDecIt);
EXPECT_EQ(*preDecIt, 2);
// Post-decrement retreats but returns the old value
auto postDecIt = this->m_slotMap.crend();
--postDecIt; // now at 1
const auto postDecrementResult = postDecIt--;
EXPECT_EQ(*postDecrementResult, 1);
EXPECT_EQ(*postDecIt, 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldSkipRemovedSlotsDuringTraversal)
{
using SlotMapKey = TestFixture::SlotMapKey;
const TypeParam& constMap = this->m_slotMap;
std::ignore = this->m_slotMap.Insert(1);
const SlotMapKey key2 = this->m_slotMap.Insert(2);
std::ignore = this->m_slotMap.Insert(3);
// Removed slots are skipped identically through crbegin()/crend() and the const-object overloads
this->m_slotMap.Remove(key2);
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.crbegin(); it != this->m_slotMap.crend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, 2);
EXPECT_EQ(sum, 4);
count = 0;
sum = 0;
for (auto it = constMap.rbegin(); it != constMap.rend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, 2);
EXPECT_EQ(sum, 4);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldTraverseAcrossMultiplePages)
{
// StableSlotMap's PAGE_SIZE is 64, so 100 elements force it to span pages
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
std::uint32_t expected = elementCount - 1;
for (auto it = this->m_slotMap.crbegin(); it != this->m_slotMap.crend(); ++it)
{
EXPECT_EQ(*it, expected);
--expected;
}
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldCrossThePageBoundaryForwardAndBackward)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto boundaryIt = this->m_slotMap.crbegin();
while (*boundaryIt != PAGE_SIZE)
{
++boundaryIt;
}
++boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE - 1);
--boundaryIt;
EXPECT_EQ(*boundaryIt, PAGE_SIZE);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldSupportPreAndPostIncrementDecrementAcrossTheBoundary)
{
constexpr std::uint32_t elementCount = 100;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
std::ignore = this->m_slotMap.Insert(value);
}
auto preBoundaryIt = this->m_slotMap.crbegin();
auto& preBoundaryIncrementResult = ++preBoundaryIt;
EXPECT_EQ(&preBoundaryIncrementResult, &preBoundaryIt);
EXPECT_EQ(*preBoundaryIt, 98);
auto postBoundaryIt = this->m_slotMap.crbegin();
const auto postBoundaryIncrementResult = postBoundaryIt++;
EXPECT_EQ(*postBoundaryIncrementResult, 99);
EXPECT_EQ(*postBoundaryIt, 98);
auto preBoundaryDecIt = this->m_slotMap.crend();
--preBoundaryDecIt; // now at 0
auto& preBoundaryDecrementResult = --preBoundaryDecIt; // now at 1
EXPECT_EQ(&preBoundaryDecrementResult, &preBoundaryDecIt);
EXPECT_EQ(*preBoundaryDecIt, 1);
auto postBoundaryDecIt = this->m_slotMap.crend();
--postBoundaryDecIt; // now at 0
const auto postBoundaryDecrementResult = postBoundaryDecIt--;
EXPECT_EQ(*postBoundaryDecrementResult, 0);
EXPECT_EQ(*postBoundaryDecIt, 1);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldSkipHolesStraddlingThePageBoundary)
{
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> keys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
keys[value] = this->m_slotMap.Insert(value);
}
this->m_slotMap.Remove(keys[PAGE_SIZE - 1]);
this->m_slotMap.Remove(keys[PAGE_SIZE]);
std::size_t count = 0;
for (auto it = this->m_slotMap.crbegin(); it != this->m_slotMap.crend(); ++it)
{
EXPECT_NE(*it, PAGE_SIZE - 1);
EXPECT_NE(*it, PAGE_SIZE);
++count;
}
EXPECT_EQ(count, elementCount - 2);
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldSkipAFullyEmptyLeadingPage)
{
// A fully empty leading page must be skipped when retreating past it (crend() side)
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> leadingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
leadingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = 0; value < PAGE_SIZE; ++value)
{
this->m_slotMap.Remove(leadingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.crbegin(); it != this->m_slotMap.crend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, elementCount - PAGE_SIZE);
EXPECT_EQ(sum, 2934U); // 64 + 65 + ... + 99
}
/****************************************************************************************/
TYPED_TEST(SlotMapFixture, ConstReverseIterator_ShouldSkipAFullyEmptyTrailingPage)
{
// A fully empty trailing page must be skipped by crbegin()/SeekLastFrom
using SlotMapKey = TestFixture::SlotMapKey;
constexpr std::uint32_t elementCount = 100;
eastl::array<SlotMapKey, elementCount> trailingKeys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
trailingKeys[value] = this->m_slotMap.Insert(value);
}
for (std::uint32_t value = PAGE_SIZE; value < elementCount; ++value)
{
this->m_slotMap.Remove(trailingKeys[value]);
}
std::size_t count = 0;
std::uint32_t sum = 0;
for (auto it = this->m_slotMap.crbegin(); it != this->m_slotMap.crend(); ++it)
{
++count;
sum += *it;
}
EXPECT_EQ(count, PAGE_SIZE);
EXPECT_EQ(sum, 2016U); // 0 + 1 + ... + 63
}
/****************************************************************************************/
class StableSlotMapFixture: public ::testing::Test
{
protected:
StableSlotMap<std::uint32_t> m_slotMap;
using SlotMapKey = StableSlotMap<std::uint32_t>::SlotMapKey;
};
/****************************************************************************************/
TEST_F(StableSlotMapFixture, PointersAreStable)
{
const SlotMapKey key = m_slotMap.Insert(42);
const std::uint32_t* const pointer = m_slotMap.Get(key);
// Inserting enough elements to span multiple pages (PAGE_SIZE is 64) must not move already
// -inserted data: unlike DenseSlotMap's contiguous eastl::vector, growth only appends new
// Page objects (each individually heap-allocated), it never reallocates existing ones.
constexpr std::uint32_t elementCount = 200;
eastl::array<SlotMapKey, elementCount> keys;
for (std::uint32_t value = 0; value < elementCount; ++value)
{
keys[value] = m_slotMap.Insert(value);
}
EXPECT_EQ(m_slotMap.Get(key), pointer);
EXPECT_EQ(*pointer, 42);
// Removing unrelated elements, including ones sharing the same page as key, must not move it
// either: Remove() only destroys that slot in place, it never compacts the page's array.
for (std::uint32_t value = 0; value < elementCount; value += 2)
{
m_slotMap.Remove(keys[value]);
}
EXPECT_EQ(m_slotMap.Get(key), pointer);
EXPECT_EQ(*pointer, 42);
// Re-inserting into the freed slots must not move it either
for (std::uint32_t value = 0; value < elementCount / 2; ++value)
{
std::ignore = m_slotMap.Insert(value);
}
EXPECT_EQ(m_slotMap.Get(key), pointer);
EXPECT_EQ(*pointer, 42);
}
} // namespace Bigfoot