Adding unit tests for IndexedDataVector (#5724)
* Rename IndexedDataVectorTests -> MultiIndexedDataVectorTests Signed-off-by: Ken Pruiksma <pruiksma@amazon.com> * Adding unit tests for IndexedDataVector. Updated fixture for MultiIndexedDataVector. Signed-off-by: Ken Pruiksma <pruiksma@amazon.com> * Fixed a bug in the test. Updated IndexedDataVector with more/better comments and removed a non-const function that wasn't necessary. Signed-off-by: Ken Pruiksma <pruiksma@amazon.com> * Further updates to some comments Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>
This commit is contained in:
@@ -27,31 +27,50 @@ namespace AZ::Render
|
||||
static constexpr IndexType NoFreeSlot = std::numeric_limits<IndexType>::max();
|
||||
IndexType m_firstFreeSlot = NoFreeSlot;
|
||||
|
||||
//! Clears all data and resets to initial state.
|
||||
void Clear();
|
||||
|
||||
//! Creates a new entry, default-constructs it, and returns an index that references it.
|
||||
IndexType GetFreeSlotIndex();
|
||||
|
||||
//! Destroys the data referenced by index and frees that index for future use.
|
||||
void RemoveIndex(IndexType index);
|
||||
|
||||
//! Destroys the data and related index by using a pointer to the data itself.
|
||||
void RemoveData(DataType* data);
|
||||
|
||||
//! Returns a reference to the data using the provided index.
|
||||
DataType& GetData(IndexType index);
|
||||
const DataType& GetData(IndexType index) const;
|
||||
|
||||
//! Returns a count of how many items are stored in the IndexedDataVector
|
||||
size_t GetDataCount() const;
|
||||
|
||||
//! Returns a reference to the internal data vector.
|
||||
//! This vector should not be altered by calling code or the IndexedDataVector will be corrupted
|
||||
AZStd::vector<DataType>& GetDataVector();
|
||||
const AZStd::vector<DataType>& GetDataVector() const;
|
||||
|
||||
//! Returns a reference to the internal vector.
|
||||
const AZStd::vector<IndexType>& GetDataToIndexVector() const;
|
||||
|
||||
AZStd::vector<IndexType>& GetIndexVector();
|
||||
const AZStd::vector<IndexType>& GetIndexVector() const;
|
||||
|
||||
//! Returns the offset into the internal data vector for a given index.
|
||||
IndexType GetRawIndex(IndexType index) const;
|
||||
|
||||
//! Returns the logical index for data given its pointer, which could passed to
|
||||
//! GetData() to retrieve the data again.
|
||||
IndexType GetIndexForData(const DataType* data) const;
|
||||
|
||||
private:
|
||||
constexpr static size_t InitialReservedSize = 128;
|
||||
|
||||
// Stores data indices and an embedded free list
|
||||
// Indices to data and an embedded free list in the unused entries
|
||||
AZStd::vector<IndexType> m_indices;
|
||||
// Stores the indirection index
|
||||
|
||||
// Map of the physical index in m_data to the logical index for that data in m_indices.
|
||||
AZStd::vector<IndexType> m_dataToIndices;
|
||||
|
||||
// The actual data.
|
||||
AZStd::vector<DataType> m_data;
|
||||
};
|
||||
} // namespace AZ::Render
|
||||
|
||||
@@ -125,13 +125,7 @@ namespace AZ::Render
|
||||
}
|
||||
|
||||
template<typename DataType, typename IndexType>
|
||||
inline AZStd::vector<IndexType>& IndexedDataVector<DataType, IndexType>::GetIndexVector()
|
||||
{
|
||||
return m_dataToIndices;
|
||||
}
|
||||
|
||||
template<typename DataType, typename IndexType>
|
||||
inline const AZStd::vector<IndexType>& IndexedDataVector<DataType, IndexType>::GetIndexVector() const
|
||||
inline const AZStd::vector<IndexType>& IndexedDataVector<DataType, IndexType>::GetDataToIndexVector() const
|
||||
{
|
||||
return m_dataToIndices;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace AZ
|
||||
{
|
||||
//! MultiIndexedDataVector is similar to IndexedDataVector but adds support for multiple different data vectors each containing different types
|
||||
//! i.e. structure of (N) arrays
|
||||
//! See IndexedDataVectorTests.cpp for examples of use
|
||||
//! See MultiIndexedDataVectorTests.cpp for examples of use
|
||||
template<typename ... Ts>
|
||||
class MultiIndexedDataVector
|
||||
{
|
||||
|
||||
@@ -8,338 +8,218 @@
|
||||
|
||||
#include <AzCore/UnitTest/TestTypes.h>
|
||||
#include <AzCore/Component/ComponentApplication.h>
|
||||
#include <Atom/Feature/Utils/MultiIndexedDataVector.h>
|
||||
#include <Atom/Feature/Utils/IndexedDataVector.h>
|
||||
#include <AzCore/Memory/SystemAllocator.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
namespace UnitTest
|
||||
{
|
||||
using namespace AZ;
|
||||
using namespace AZ::Render;
|
||||
|
||||
|
||||
class IndexedDataVectorTests
|
||||
: public ::testing::Test
|
||||
: public UnitTest::AllocatorsTestFixture
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
CreateAllocator();
|
||||
UnitTest::AllocatorsTestFixture::SetUp();
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
DestroyAllocator();
|
||||
UnitTest::AllocatorsTestFixture::TearDown();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void CreateAllocator()
|
||||
template<typename T>
|
||||
IndexedDataVector<T> SetupIndexedDataVector(size_t size, T initialValue = T(0), T incrementAmount = T(1), AZStd::vector<uint16_t>* indices = nullptr)
|
||||
{
|
||||
static constexpr size_t NumMBToAllocate = 1;
|
||||
SystemAllocator::Descriptor desc;
|
||||
desc.m_heap.m_numFixedMemoryBlocks = 1;
|
||||
desc.m_heap.m_fixedMemoryBlocksByteSize[0] = NumMBToAllocate * 1024 * 1024;
|
||||
m_memBlock = AZ_OS_MALLOC(
|
||||
desc.m_heap.m_fixedMemoryBlocksByteSize[0],
|
||||
desc.m_heap.m_memoryBlockAlignment);
|
||||
desc.m_heap.m_fixedMemoryBlocks[0] = m_memBlock;
|
||||
|
||||
AllocatorInstance<AZ::SystemAllocator>::Create(desc);
|
||||
IndexedDataVector<T> data;
|
||||
T value = initialValue;
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
uint16_t index = data.GetFreeSlotIndex();
|
||||
EXPECT_NE(index, IndexedDataVector<int>::NoFreeSlot);
|
||||
if (indices)
|
||||
{
|
||||
indices->push_back(index);
|
||||
}
|
||||
if (index != IndexedDataVector<int>::NoFreeSlot)
|
||||
{
|
||||
data.GetData(index) = value;
|
||||
value += incrementAmount;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void DestroyAllocator()
|
||||
template<typename T>
|
||||
void ShuffleIndexedDataVector(IndexedDataVector<T>& dataVector, AZStd::vector<uint16_t>& indices)
|
||||
{
|
||||
AllocatorInstance<AZ::SystemAllocator>::Destroy();
|
||||
AZ_OS_FREE(m_memBlock);
|
||||
m_memBlock = nullptr;
|
||||
AZStd::vector<T> values;
|
||||
|
||||
// remove every other element and store it
|
||||
for (size_t i = 0; i < indices.size(); ++i)
|
||||
{
|
||||
values.push_back(dataVector.GetData(indices.at(i)));
|
||||
dataVector.RemoveIndex(indices.at(i));
|
||||
indices.erase(&indices.at(i));
|
||||
}
|
||||
|
||||
for (T value : values)
|
||||
{
|
||||
uint16_t index = dataVector.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
dataVector.GetData(index) = value;
|
||||
}
|
||||
}
|
||||
|
||||
void* m_memBlock = nullptr;
|
||||
};
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestInsert)
|
||||
|
||||
TEST_F(IndexedDataVectorTests, Construction)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
DoubleType = 1,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int, double> myVec;
|
||||
constexpr int NumToInsert = 5;
|
||||
IndexedDataVector<int> testVector;
|
||||
uint16_t index = testVector.GetFreeSlotIndex();
|
||||
EXPECT_NE(index, IndexedDataVector<int>::NoFreeSlot);
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestInsertGetBasic)
|
||||
{
|
||||
constexpr size_t count = 16;
|
||||
constexpr int initialValue = 0;
|
||||
constexpr int increment = 1;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
|
||||
for (int i = 0; i < NumToInsert; ++i)
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
|
||||
|
||||
int value = initialValue;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
myVec.GetData<IntType>(index) = i;
|
||||
myVec.GetData<DoubleType>(index) = (double)i;
|
||||
EXPECT_EQ(testVector.GetData(indices.at(i)), value);
|
||||
value += increment;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestInsertGetComplex)
|
||||
{
|
||||
constexpr size_t count = 16;
|
||||
constexpr int initialValue = 0;
|
||||
constexpr int increment = 1;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
|
||||
|
||||
// Create a set of the data that should be in the IndexedDataVector
|
||||
AZStd::set<int> values;
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
values.emplace(initialValue + i * increment);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < NumToInsert; ++i)
|
||||
// Add and remove items to shuffle the underlying data
|
||||
ShuffleIndexedDataVector(testVector, indices);
|
||||
|
||||
// Check to make sure all the data is still there
|
||||
AZStd::vector<int>& underlyingVector = testVector.GetDataVector();
|
||||
for (size_t i = 0; i < underlyingVector.size(); ++i)
|
||||
{
|
||||
auto index = indices[i];
|
||||
EXPECT_EQ(i, myVec.GetData<IntType>(index));
|
||||
EXPECT_EQ((double)i, myVec.GetData<DoubleType>(index));
|
||||
EXPECT_TRUE(values.contains(underlyingVector.at(i)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestSize)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
};
|
||||
constexpr size_t count = 32;
|
||||
|
||||
MultiIndexedDataVector<int> myVec;
|
||||
constexpr int NumToInsert = 5;
|
||||
for (int i = 0; i < NumToInsert; ++i)
|
||||
{
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
myVec.GetData<IntType>(index) = i;
|
||||
}
|
||||
EXPECT_EQ(NumToInsert, myVec.GetDataCount());
|
||||
EXPECT_EQ(NumToInsert, myVec.GetDataVector<IntType>().size());
|
||||
|
||||
myVec.Clear();
|
||||
|
||||
EXPECT_EQ(0, myVec.GetDataCount());
|
||||
EXPECT_EQ(0, myVec.GetDataVector<IntType>().size());
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count);
|
||||
EXPECT_EQ(testVector.GetDataCount(), count);
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestErase)
|
||||
TEST_F(IndexedDataVectorTests, TestClear)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int> myVec;
|
||||
constexpr int NumToInsert = 200;
|
||||
AZStd::unordered_map<int, uint16_t> valueToIndex;
|
||||
|
||||
for (int i = 0; i < NumToInsert; ++i)
|
||||
{
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
valueToIndex[i] = index;
|
||||
myVec.GetData<IntType>(index) = i;
|
||||
}
|
||||
|
||||
// erase every even number
|
||||
for (int i = 0; i < NumToInsert; i += 2)
|
||||
{
|
||||
uint16_t index = valueToIndex[i];
|
||||
auto previousRawIndex = myVec.GetRawIndex(index);
|
||||
auto movedIndex = myVec.RemoveIndex(index);
|
||||
if (movedIndex != MultiIndexedDataVector<int>::NoFreeSlot)
|
||||
{
|
||||
auto newRawIndex = myVec.GetRawIndex(movedIndex);
|
||||
|
||||
// RemoveIndex() returns the index of the item that moves into its spot if any, so check
|
||||
// to make sure the Raw index of the old matches the raw index of the new
|
||||
EXPECT_EQ(previousRawIndex, newRawIndex);
|
||||
}
|
||||
valueToIndex.erase(i);
|
||||
}
|
||||
|
||||
for (const auto& iter : valueToIndex)
|
||||
{
|
||||
int val = iter.first;
|
||||
uint16_t index = iter.second;
|
||||
EXPECT_EQ(val, myVec.GetData<IntType>(index));
|
||||
}
|
||||
constexpr size_t count = 32;
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count);
|
||||
testVector.Clear();
|
||||
EXPECT_EQ(testVector.GetDataCount(), 0);
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestManyTypes)
|
||||
TEST_F(IndexedDataVectorTests, TestRemove)
|
||||
{
|
||||
enum Types
|
||||
constexpr size_t count = 8;
|
||||
constexpr int initialValue = 0;
|
||||
constexpr int increment = 8;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
|
||||
|
||||
// Remove every other element by index
|
||||
for (uint16_t i = 0; i < count; i += 2)
|
||||
{
|
||||
IntType = 0,
|
||||
StringType = 1,
|
||||
DoubleType = 2,
|
||||
FloatType = 3,
|
||||
CharType = 4,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int, AZStd::string, double, float, const char*> myVec;
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
|
||||
constexpr int TestIntVal = INT_MIN;
|
||||
constexpr double TestDoubleVal = -DBL_MIN;
|
||||
const AZStd::string TestStringVal = "This is an AZStd::string.";
|
||||
constexpr float TestFloatVal = FLT_MAX;
|
||||
const char* TestConstPointerVal = "This is a C array.";
|
||||
|
||||
myVec.GetData<IntType>(index) = TestIntVal;
|
||||
myVec.GetData<StringType>(index) = TestStringVal;
|
||||
myVec.GetData<DoubleType>(index) = TestDoubleVal;
|
||||
myVec.GetData<FloatType>(index) = TestFloatVal;
|
||||
myVec.GetData<CharType>(index) = TestConstPointerVal;
|
||||
|
||||
EXPECT_EQ(TestIntVal, static_cast<int>(myVec.GetData<IntType>(index)));
|
||||
EXPECT_EQ(TestStringVal, static_cast<AZStd::string>(myVec.GetData<StringType>(index)));
|
||||
EXPECT_EQ(TestDoubleVal, static_cast<double>(myVec.GetData<DoubleType>(index)));
|
||||
EXPECT_EQ(TestFloatVal, static_cast<float>(myVec.GetData<FloatType>(index)));
|
||||
EXPECT_STREQ(TestConstPointerVal, static_cast<const char*>(myVec.GetData<CharType>(index)));
|
||||
}
|
||||
|
||||
MultiIndexedDataVector<int32_t, float> CreateTestVector(AZStd::vector<uint16_t>& indices)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int32_t, float> myVec;
|
||||
constexpr int32_t Count = 10;
|
||||
int32_t startInt = 10;
|
||||
float startFloat = 2.0f;
|
||||
testVector.RemoveIndex(i);
|
||||
}
|
||||
|
||||
// Create some initial values
|
||||
for (uint32_t i = 0; i < Count; ++i)
|
||||
EXPECT_EQ(testVector.GetDataCount(), count / 2);
|
||||
|
||||
// Make sure the rest of the data is still there
|
||||
AZStd::vector<uint16_t> remainingIndices;
|
||||
for (size_t i = 1; i < count; i += 2)
|
||||
{
|
||||
uint16_t index = myVec.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
myVec.GetData<IntType>(index) = startInt;
|
||||
myVec.GetData<FloatType>(index) = startFloat;
|
||||
startInt += 1;
|
||||
startFloat += 1.0f;
|
||||
int value = testVector.GetData(indices.at(i));
|
||||
EXPECT_EQ(value, initialValue + increment * i);
|
||||
remainingIndices.push_back(indices.at(i));
|
||||
}
|
||||
|
||||
return myVec;
|
||||
// remove the rest of the valus by value
|
||||
for (uint16_t index : remainingIndices)
|
||||
{
|
||||
int* valuePtr = &testVector.GetData(index);
|
||||
testVector.RemoveData(valuePtr);
|
||||
}
|
||||
|
||||
EXPECT_EQ(testVector.GetDataCount(), 0);
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, TestIndexForData)
|
||||
{
|
||||
constexpr size_t count = 8;
|
||||
constexpr int initialValue = 0;
|
||||
constexpr int increment = 8;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
|
||||
|
||||
// Add and remove items to shuffle the underlying data
|
||||
ShuffleIndexedDataVector(testVector, indices);
|
||||
|
||||
AZStd::vector<int>& underlyingVector = testVector.GetDataVector();
|
||||
for (size_t i = 0; i < underlyingVector.size(); ++i)
|
||||
{
|
||||
int value = underlyingVector.at(i);
|
||||
uint16_t index = testVector.GetIndexForData(&underlyingVector.at(i));
|
||||
|
||||
// The data from GetData(index) should match for the index retrieved using GetIndexForData() for the same data.
|
||||
EXPECT_EQ(testVector.GetData(index), value);
|
||||
}
|
||||
}
|
||||
|
||||
void CheckIndexedData(MultiIndexedDataVector<int32_t, float>& data, AZStd::vector<uint16_t>& indices)
|
||||
TEST_F(IndexedDataVectorTests, TestRawIndex)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
// For each index, get its data and make sure GetIndexForData returns the same
|
||||
// index used to retrieve the data
|
||||
for (uint32_t i = 0; i < data.GetDataCount(); ++i)
|
||||
{
|
||||
int32_t& intData = data.GetData<IntType>(indices.at(i));
|
||||
uint16_t indexForData = data.GetIndexForData<IntType>(&intData);
|
||||
EXPECT_EQ(indices.at(i), indexForData);
|
||||
|
||||
float& floatData = data.GetData<FloatType>(indices.at(i));
|
||||
indexForData = data.GetIndexForData<FloatType>(&floatData);
|
||||
EXPECT_EQ(indices.at(i), indexForData);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, GetIndexForDataSimple)
|
||||
{
|
||||
AZStd::vector<uint16_t> indices;
|
||||
MultiIndexedDataVector<int32_t, float> myVec = CreateTestVector(indices);
|
||||
CheckIndexedData(myVec, indices);
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, GetIndexForDataComplex)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
constexpr size_t count = 8;
|
||||
constexpr int initialValue = 0;
|
||||
constexpr int increment = 8;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
MultiIndexedDataVector<int32_t, float> myVec = CreateTestVector(indices);
|
||||
IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
|
||||
|
||||
// remove every other value to shuffle the data around
|
||||
for (uint32_t i = 0; i < myVec.GetDataCount(); i += 2)
|
||||
{
|
||||
myVec.RemoveIndex(indices.at(i));
|
||||
}
|
||||
// Add and remove items to shuffle the underlying data
|
||||
ShuffleIndexedDataVector(testVector, indices);
|
||||
|
||||
int32_t startInt = 100;
|
||||
float startFloat = 20.0f;
|
||||
|
||||
// Add some data back in
|
||||
const size_t count = myVec.GetDataCount();
|
||||
for (uint32_t i = 0; i < count; i += 2)
|
||||
AZStd::vector<int>& underlyingVector = testVector.GetDataVector();
|
||||
for (size_t i = 0; i < indices.size(); ++i)
|
||||
{
|
||||
uint16_t index = myVec.GetFreeSlotIndex();
|
||||
indices.at(i) = index;
|
||||
myVec.GetData<IntType>(index) = startInt;
|
||||
myVec.GetData<FloatType>(index) = startFloat;
|
||||
startInt += 1;
|
||||
startFloat += 1.0f;
|
||||
// Check that the data retrieved from GetData for a given index matches the data in the underlying vector for the raw index.
|
||||
EXPECT_EQ(testVector.GetData(indices.at(i)), underlyingVector.at(testVector.GetRawIndex(indices.at(i))));
|
||||
}
|
||||
|
||||
CheckIndexedData(myVec, indices);
|
||||
}
|
||||
|
||||
TEST_F(IndexedDataVectorTests, ForEach)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int32_t, float> myVec;
|
||||
constexpr int32_t Count = 10;
|
||||
int32_t startInt = 10;
|
||||
float startFloat = 2.0f;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
AZStd::set<int32_t> intValues;
|
||||
AZStd::set<float> floatValues;
|
||||
|
||||
// Create some initial values
|
||||
for (uint32_t i = 0; i < Count; ++i)
|
||||
{
|
||||
uint16_t index = myVec.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
myVec.GetData<IntType>(index) = startInt;
|
||||
myVec.GetData<FloatType>(index) = startFloat;
|
||||
intValues.insert(startInt);
|
||||
floatValues.insert(startFloat);
|
||||
startInt += 1;
|
||||
startFloat += 1.0f;
|
||||
}
|
||||
|
||||
uint32_t visitCount = 0;
|
||||
myVec.ForEach<IntType>([&](int32_t value) -> bool
|
||||
{
|
||||
intValues.erase(value);
|
||||
++visitCount;
|
||||
return true; // keep iterating
|
||||
});
|
||||
|
||||
// All ints should have been visited and found in the set
|
||||
EXPECT_EQ(visitCount, Count);
|
||||
EXPECT_EQ(intValues.size(), 0);
|
||||
|
||||
visitCount = 0;
|
||||
myVec.ForEach<FloatType>([&](float value) -> bool
|
||||
{
|
||||
floatValues.erase(value);
|
||||
++visitCount;
|
||||
return true; // keep iterating
|
||||
});
|
||||
|
||||
// All floats should have been visited and found in the set
|
||||
EXPECT_EQ(visitCount, Count);
|
||||
EXPECT_EQ(floatValues.size(), 0);
|
||||
|
||||
visitCount = 0;
|
||||
myVec.ForEach<IntType>([&]([[maybe_unused]] int32_t value) -> bool
|
||||
{
|
||||
++visitCount;
|
||||
return false; // stop iterating
|
||||
});
|
||||
|
||||
// Since false is immediately returned, only one element should have been visited.
|
||||
EXPECT_EQ(visitCount, 1);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/UnitTest/TestTypes.h>
|
||||
#include <AzCore/Component/ComponentApplication.h>
|
||||
#include <Atom/Feature/Utils/MultiIndexedDataVector.h>
|
||||
#include <AzCore/Memory/SystemAllocator.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
namespace UnitTest
|
||||
{
|
||||
using namespace AZ;
|
||||
using namespace AZ::Render;
|
||||
|
||||
class MultiIndexedDataVectorTests
|
||||
: public UnitTest::AllocatorsTestFixture
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
UnitTest::AllocatorsTestFixture::SetUp();
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
UnitTest::AllocatorsTestFixture::TearDown();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, TestInsert)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
DoubleType = 1,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int, double> myVec;
|
||||
constexpr int NumToInsert = 5;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
|
||||
for (int i = 0; i < NumToInsert; ++i)
|
||||
{
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
myVec.GetData<IntType>(index) = i;
|
||||
myVec.GetData<DoubleType>(index) = (double)i;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < NumToInsert; ++i)
|
||||
{
|
||||
auto index = indices[i];
|
||||
EXPECT_EQ(i, myVec.GetData<IntType>(index));
|
||||
EXPECT_EQ((double)i, myVec.GetData<DoubleType>(index));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, TestSize)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int> myVec;
|
||||
constexpr int NumToInsert = 5;
|
||||
for (int i = 0; i < NumToInsert; ++i)
|
||||
{
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
myVec.GetData<IntType>(index) = i;
|
||||
}
|
||||
EXPECT_EQ(NumToInsert, myVec.GetDataCount());
|
||||
EXPECT_EQ(NumToInsert, myVec.GetDataVector<IntType>().size());
|
||||
|
||||
myVec.Clear();
|
||||
|
||||
EXPECT_EQ(0, myVec.GetDataCount());
|
||||
EXPECT_EQ(0, myVec.GetDataVector<IntType>().size());
|
||||
}
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, TestErase)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int> myVec;
|
||||
constexpr int NumToInsert = 200;
|
||||
AZStd::unordered_map<int, uint16_t> valueToIndex;
|
||||
|
||||
for (int i = 0; i < NumToInsert; ++i)
|
||||
{
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
valueToIndex[i] = index;
|
||||
myVec.GetData<IntType>(index) = i;
|
||||
}
|
||||
|
||||
// erase every even number
|
||||
for (int i = 0; i < NumToInsert; i += 2)
|
||||
{
|
||||
uint16_t index = valueToIndex[i];
|
||||
auto previousRawIndex = myVec.GetRawIndex(index);
|
||||
auto movedIndex = myVec.RemoveIndex(index);
|
||||
if (movedIndex != MultiIndexedDataVector<int>::NoFreeSlot)
|
||||
{
|
||||
auto newRawIndex = myVec.GetRawIndex(movedIndex);
|
||||
|
||||
// RemoveIndex() returns the index of the item that moves into its spot if any, so check
|
||||
// to make sure the Raw index of the old matches the raw index of the new
|
||||
EXPECT_EQ(previousRawIndex, newRawIndex);
|
||||
}
|
||||
valueToIndex.erase(i);
|
||||
}
|
||||
|
||||
for (const auto& iter : valueToIndex)
|
||||
{
|
||||
int val = iter.first;
|
||||
uint16_t index = iter.second;
|
||||
EXPECT_EQ(val, myVec.GetData<IntType>(index));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, TestManyTypes)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
StringType = 1,
|
||||
DoubleType = 2,
|
||||
FloatType = 3,
|
||||
CharType = 4,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int, AZStd::string, double, float, const char*> myVec;
|
||||
auto index = myVec.GetFreeSlotIndex();
|
||||
|
||||
constexpr int TestIntVal = INT_MIN;
|
||||
constexpr double TestDoubleVal = -DBL_MIN;
|
||||
const AZStd::string TestStringVal = "This is an AZStd::string.";
|
||||
constexpr float TestFloatVal = FLT_MAX;
|
||||
const char* TestConstPointerVal = "This is a C array.";
|
||||
|
||||
myVec.GetData<IntType>(index) = TestIntVal;
|
||||
myVec.GetData<StringType>(index) = TestStringVal;
|
||||
myVec.GetData<DoubleType>(index) = TestDoubleVal;
|
||||
myVec.GetData<FloatType>(index) = TestFloatVal;
|
||||
myVec.GetData<CharType>(index) = TestConstPointerVal;
|
||||
|
||||
EXPECT_EQ(TestIntVal, static_cast<int>(myVec.GetData<IntType>(index)));
|
||||
EXPECT_EQ(TestStringVal, static_cast<AZStd::string>(myVec.GetData<StringType>(index)));
|
||||
EXPECT_EQ(TestDoubleVal, static_cast<double>(myVec.GetData<DoubleType>(index)));
|
||||
EXPECT_EQ(TestFloatVal, static_cast<float>(myVec.GetData<FloatType>(index)));
|
||||
EXPECT_STREQ(TestConstPointerVal, static_cast<const char*>(myVec.GetData<CharType>(index)));
|
||||
}
|
||||
|
||||
MultiIndexedDataVector<int32_t, float> CreateTestVector(AZStd::vector<uint16_t>& indices)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int32_t, float> myVec;
|
||||
constexpr int32_t Count = 10;
|
||||
int32_t startInt = 10;
|
||||
float startFloat = 2.0f;
|
||||
|
||||
// Create some initial values
|
||||
for (uint32_t i = 0; i < Count; ++i)
|
||||
{
|
||||
uint16_t index = myVec.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
myVec.GetData<IntType>(index) = startInt;
|
||||
myVec.GetData<FloatType>(index) = startFloat;
|
||||
startInt += 1;
|
||||
startFloat += 1.0f;
|
||||
}
|
||||
|
||||
return myVec;
|
||||
}
|
||||
|
||||
void CheckIndexedData(MultiIndexedDataVector<int32_t, float>& data, AZStd::vector<uint16_t>& indices)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
// For each index, get its data and make sure GetIndexForData returns the same
|
||||
// index used to retrieve the data
|
||||
for (uint32_t i = 0; i < data.GetDataCount(); ++i)
|
||||
{
|
||||
int32_t& intData = data.GetData<IntType>(indices.at(i));
|
||||
uint16_t indexForData = data.GetIndexForData<IntType>(&intData);
|
||||
EXPECT_EQ(indices.at(i), indexForData);
|
||||
|
||||
float& floatData = data.GetData<FloatType>(indices.at(i));
|
||||
indexForData = data.GetIndexForData<FloatType>(&floatData);
|
||||
EXPECT_EQ(indices.at(i), indexForData);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, GetIndexForDataSimple)
|
||||
{
|
||||
AZStd::vector<uint16_t> indices;
|
||||
MultiIndexedDataVector<int32_t, float> myVec = CreateTestVector(indices);
|
||||
CheckIndexedData(myVec, indices);
|
||||
}
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, GetIndexForDataComplex)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
MultiIndexedDataVector<int32_t, float> myVec = CreateTestVector(indices);
|
||||
|
||||
// remove every other value to shuffle the data around
|
||||
for (uint32_t i = 0; i < myVec.GetDataCount(); i += 2)
|
||||
{
|
||||
myVec.RemoveIndex(indices.at(i));
|
||||
}
|
||||
|
||||
int32_t startInt = 100;
|
||||
float startFloat = 20.0f;
|
||||
|
||||
// Add some data back in
|
||||
const size_t count = myVec.GetDataCount();
|
||||
for (uint32_t i = 0; i < count; i += 2)
|
||||
{
|
||||
uint16_t index = myVec.GetFreeSlotIndex();
|
||||
indices.at(i) = index;
|
||||
myVec.GetData<IntType>(index) = startInt;
|
||||
myVec.GetData<FloatType>(index) = startFloat;
|
||||
startInt += 1;
|
||||
startFloat += 1.0f;
|
||||
}
|
||||
|
||||
CheckIndexedData(myVec, indices);
|
||||
}
|
||||
|
||||
TEST_F(MultiIndexedDataVectorTests, ForEach)
|
||||
{
|
||||
enum Types
|
||||
{
|
||||
IntType = 0,
|
||||
FloatType = 1,
|
||||
};
|
||||
|
||||
MultiIndexedDataVector<int32_t, float> myVec;
|
||||
constexpr int32_t Count = 10;
|
||||
int32_t startInt = 10;
|
||||
float startFloat = 2.0f;
|
||||
|
||||
AZStd::vector<uint16_t> indices;
|
||||
AZStd::set<int32_t> intValues;
|
||||
AZStd::set<float> floatValues;
|
||||
|
||||
// Create some initial values
|
||||
for (uint32_t i = 0; i < Count; ++i)
|
||||
{
|
||||
uint16_t index = myVec.GetFreeSlotIndex();
|
||||
indices.push_back(index);
|
||||
myVec.GetData<IntType>(index) = startInt;
|
||||
myVec.GetData<FloatType>(index) = startFloat;
|
||||
intValues.insert(startInt);
|
||||
floatValues.insert(startFloat);
|
||||
startInt += 1;
|
||||
startFloat += 1.0f;
|
||||
}
|
||||
|
||||
uint32_t visitCount = 0;
|
||||
myVec.ForEach<IntType>([&](int32_t value) -> bool
|
||||
{
|
||||
intValues.erase(value);
|
||||
++visitCount;
|
||||
return true; // keep iterating
|
||||
});
|
||||
|
||||
// All ints should have been visited and found in the set
|
||||
EXPECT_EQ(visitCount, Count);
|
||||
EXPECT_EQ(intValues.size(), 0);
|
||||
|
||||
visitCount = 0;
|
||||
myVec.ForEach<FloatType>([&](float value) -> bool
|
||||
{
|
||||
floatValues.erase(value);
|
||||
++visitCount;
|
||||
return true; // keep iterating
|
||||
});
|
||||
|
||||
// All floats should have been visited and found in the set
|
||||
EXPECT_EQ(visitCount, Count);
|
||||
EXPECT_EQ(floatValues.size(), 0);
|
||||
|
||||
visitCount = 0;
|
||||
myVec.ForEach<IntType>([&]([[maybe_unused]] int32_t value) -> bool
|
||||
{
|
||||
++visitCount;
|
||||
return false; // stop iterating
|
||||
});
|
||||
|
||||
// Since false is immediately returned, only one element should have been visited.
|
||||
EXPECT_EQ(visitCount, 1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ set(FILES
|
||||
Tests/CommonTest.cpp
|
||||
Tests/CoreLights/ShadowmapAtlasTest.cpp
|
||||
Tests/IndexedDataVectorTests.cpp
|
||||
Tests/MultiIndexedDataVectorTests.cpp
|
||||
Tests/IndexableListTests.cpp
|
||||
Tests/SparseVectorTests.cpp
|
||||
Tests/SkinnedMesh/SkinnedMeshDispatchItemTests.cpp
|
||||
|
||||
Reference in New Issue
Block a user