IndexedDataVectorTests.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #include <Atom/Feature/Utils/IndexedDataVector.h>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <gtest/gtest.h>
  13. namespace UnitTest
  14. {
  15. using namespace AZ;
  16. using namespace AZ::Render;
  17. class IndexedDataVectorTests
  18. : public UnitTest::LeakDetectionFixture
  19. {
  20. public:
  21. template<typename T>
  22. IndexedDataVector<T> SetupIndexedDataVector(size_t size, T initialValue = T(0), T incrementAmount = T(1), AZStd::vector<uint16_t>* indices = nullptr)
  23. {
  24. IndexedDataVector<T> data;
  25. T value = initialValue;
  26. for (size_t i = 0; i < size; ++i)
  27. {
  28. uint16_t index = data.GetFreeSlotIndex();
  29. EXPECT_NE(index, IndexedDataVector<int>::NoFreeSlot);
  30. if (indices)
  31. {
  32. indices->push_back(index);
  33. }
  34. if (index != IndexedDataVector<int>::NoFreeSlot)
  35. {
  36. data.GetData(index) = value;
  37. value += incrementAmount;
  38. }
  39. }
  40. return data;
  41. }
  42. template<typename T>
  43. void ShuffleIndexedDataVector(IndexedDataVector<T>& dataVector, AZStd::vector<uint16_t>& indices)
  44. {
  45. AZStd::vector<T> values;
  46. // remove every other element and store it
  47. for (size_t i = 0; i < indices.size(); ++i)
  48. {
  49. values.push_back(dataVector.GetData(indices.at(i)));
  50. dataVector.RemoveIndex(indices.at(i));
  51. indices.erase(&indices.at(i));
  52. }
  53. for (T value : values)
  54. {
  55. uint16_t index = dataVector.GetFreeSlotIndex();
  56. indices.push_back(index);
  57. dataVector.GetData(index) = value;
  58. }
  59. }
  60. };
  61. TEST_F(IndexedDataVectorTests, Construction)
  62. {
  63. IndexedDataVector<int> testVector;
  64. uint16_t index = testVector.GetFreeSlotIndex();
  65. EXPECT_NE(index, IndexedDataVector<int>::NoFreeSlot);
  66. }
  67. TEST_F(IndexedDataVectorTests, TestInsertGetBasic)
  68. {
  69. constexpr size_t count = 16;
  70. constexpr int initialValue = 0;
  71. constexpr int increment = 1;
  72. AZStd::vector<uint16_t> indices;
  73. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
  74. int value = initialValue;
  75. for (size_t i = 0; i < count; ++i)
  76. {
  77. EXPECT_EQ(testVector.GetData(indices.at(i)), value);
  78. value += increment;
  79. }
  80. }
  81. TEST_F(IndexedDataVectorTests, TestInsertGetComplex)
  82. {
  83. constexpr size_t count = 16;
  84. constexpr int initialValue = 0;
  85. constexpr int increment = 1;
  86. AZStd::vector<uint16_t> indices;
  87. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
  88. // Create a set of the data that should be in the IndexedDataVector
  89. AZStd::set<int> values;
  90. for (int i = 0; i < count; ++i)
  91. {
  92. values.emplace(initialValue + i * increment);
  93. }
  94. // Add and remove items to shuffle the underlying data
  95. ShuffleIndexedDataVector(testVector, indices);
  96. // Check to make sure all the data is still there
  97. AZStd::vector<int>& underlyingVector = testVector.GetDataVector();
  98. for (size_t i = 0; i < underlyingVector.size(); ++i)
  99. {
  100. EXPECT_TRUE(values.contains(underlyingVector.at(i)));
  101. }
  102. }
  103. TEST_F(IndexedDataVectorTests, TestSize)
  104. {
  105. constexpr size_t count = 32;
  106. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count);
  107. EXPECT_EQ(testVector.GetDataCount(), count);
  108. }
  109. TEST_F(IndexedDataVectorTests, TestClear)
  110. {
  111. constexpr size_t count = 32;
  112. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count);
  113. testVector.Clear();
  114. EXPECT_EQ(testVector.GetDataCount(), 0);
  115. }
  116. TEST_F(IndexedDataVectorTests, TestRemove)
  117. {
  118. constexpr size_t count = 8;
  119. constexpr int initialValue = 0;
  120. constexpr int increment = 8;
  121. AZStd::vector<uint16_t> indices;
  122. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
  123. // Remove every other element by index
  124. for (uint16_t i = 0; i < count; i += 2)
  125. {
  126. testVector.RemoveIndex(i);
  127. }
  128. EXPECT_EQ(testVector.GetDataCount(), count / 2);
  129. // Make sure the rest of the data is still there
  130. AZStd::vector<uint16_t> remainingIndices;
  131. for (size_t i = 1; i < count; i += 2)
  132. {
  133. int value = testVector.GetData(indices.at(i));
  134. EXPECT_EQ(value, initialValue + increment * i);
  135. remainingIndices.push_back(indices.at(i));
  136. }
  137. // remove the rest of the valus by value
  138. for (uint16_t index : remainingIndices)
  139. {
  140. int* valuePtr = &testVector.GetData(index);
  141. testVector.RemoveData(valuePtr);
  142. }
  143. EXPECT_EQ(testVector.GetDataCount(), 0);
  144. }
  145. TEST_F(IndexedDataVectorTests, TestIndexForData)
  146. {
  147. constexpr size_t count = 8;
  148. constexpr int initialValue = 0;
  149. constexpr int increment = 8;
  150. AZStd::vector<uint16_t> indices;
  151. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
  152. // Add and remove items to shuffle the underlying data
  153. ShuffleIndexedDataVector(testVector, indices);
  154. AZStd::vector<int>& underlyingVector = testVector.GetDataVector();
  155. for (size_t i = 0; i < underlyingVector.size(); ++i)
  156. {
  157. int value = underlyingVector.at(i);
  158. uint16_t index = testVector.GetIndexForData(&underlyingVector.at(i));
  159. // The data from GetData(index) should match for the index retrieved using GetIndexForData() for the same data.
  160. EXPECT_EQ(testVector.GetData(index), value);
  161. }
  162. }
  163. TEST_F(IndexedDataVectorTests, TestRawIndex)
  164. {
  165. constexpr size_t count = 8;
  166. constexpr int initialValue = 0;
  167. constexpr int increment = 8;
  168. AZStd::vector<uint16_t> indices;
  169. IndexedDataVector<int> testVector = SetupIndexedDataVector<int>(count, initialValue, increment, &indices);
  170. // Add and remove items to shuffle the underlying data
  171. ShuffleIndexedDataVector(testVector, indices);
  172. AZStd::vector<int>& underlyingVector = testVector.GetDataVector();
  173. for (size_t i = 0; i < indices.size(); ++i)
  174. {
  175. // Check that the data retrieved from GetData for a given index matches the data in the underlying vector for the raw index.
  176. EXPECT_EQ(testVector.GetData(indices.at(i)), underlyingVector.at(testVector.GetRawIndex(indices.at(i))));
  177. }
  178. }
  179. }