MultiIndexedDataVectorTests.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/MultiIndexedDataVector.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 MultiIndexedDataVectorTests
  18. : public UnitTest::LeakDetectionFixture
  19. {
  20. };
  21. TEST_F(MultiIndexedDataVectorTests, TestInsert)
  22. {
  23. enum Types
  24. {
  25. IntType = 0,
  26. DoubleType = 1,
  27. };
  28. MultiIndexedDataVector<int, double> myVec;
  29. constexpr int NumToInsert = 5;
  30. AZStd::vector<uint16_t> indices;
  31. for (int i = 0; i < NumToInsert; ++i)
  32. {
  33. auto index = myVec.GetFreeSlotIndex();
  34. indices.push_back(index);
  35. myVec.GetData<IntType>(index) = i;
  36. myVec.GetData<DoubleType>(index) = (double)i;
  37. }
  38. for (size_t i = 0; i < NumToInsert; ++i)
  39. {
  40. auto index = indices[i];
  41. EXPECT_EQ(i, myVec.GetData<IntType>(index));
  42. EXPECT_EQ((double)i, myVec.GetData<DoubleType>(index));
  43. }
  44. }
  45. TEST_F(MultiIndexedDataVectorTests, TestSize)
  46. {
  47. enum Types
  48. {
  49. IntType = 0,
  50. };
  51. MultiIndexedDataVector<int> myVec;
  52. constexpr int NumToInsert = 5;
  53. for (int i = 0; i < NumToInsert; ++i)
  54. {
  55. auto index = myVec.GetFreeSlotIndex();
  56. myVec.GetData<IntType>(index) = i;
  57. }
  58. EXPECT_EQ(NumToInsert, myVec.GetDataCount());
  59. EXPECT_EQ(NumToInsert, myVec.GetDataVector<IntType>().size());
  60. myVec.Clear();
  61. EXPECT_EQ(0, myVec.GetDataCount());
  62. EXPECT_EQ(0, myVec.GetDataVector<IntType>().size());
  63. }
  64. TEST_F(MultiIndexedDataVectorTests, TestErase)
  65. {
  66. enum Types
  67. {
  68. IntType = 0,
  69. };
  70. MultiIndexedDataVector<int> myVec;
  71. constexpr int NumToInsert = 200;
  72. AZStd::unordered_map<int, uint16_t> valueToIndex;
  73. for (int i = 0; i < NumToInsert; ++i)
  74. {
  75. auto index = myVec.GetFreeSlotIndex();
  76. valueToIndex[i] = index;
  77. myVec.GetData<IntType>(index) = i;
  78. }
  79. // erase every even number
  80. for (int i = 0; i < NumToInsert; i += 2)
  81. {
  82. uint16_t index = valueToIndex[i];
  83. auto previousRawIndex = myVec.GetRawIndex(index);
  84. auto movedIndex = myVec.RemoveIndex(index);
  85. if (movedIndex != MultiIndexedDataVector<int>::NoFreeSlot)
  86. {
  87. auto newRawIndex = myVec.GetRawIndex(movedIndex);
  88. // RemoveIndex() returns the index of the item that moves into its spot if any, so check
  89. // to make sure the Raw index of the old matches the raw index of the new
  90. EXPECT_EQ(previousRawIndex, newRawIndex);
  91. }
  92. valueToIndex.erase(i);
  93. }
  94. for (const auto& iter : valueToIndex)
  95. {
  96. int val = iter.first;
  97. uint16_t index = iter.second;
  98. EXPECT_EQ(val, myVec.GetData<IntType>(index));
  99. }
  100. }
  101. TEST_F(MultiIndexedDataVectorTests, TestManyTypes)
  102. {
  103. enum Types
  104. {
  105. IntType = 0,
  106. StringType = 1,
  107. DoubleType = 2,
  108. FloatType = 3,
  109. CharType = 4,
  110. };
  111. MultiIndexedDataVector<int, AZStd::string, double, float, const char*> myVec;
  112. auto index = myVec.GetFreeSlotIndex();
  113. constexpr int TestIntVal = INT_MIN;
  114. constexpr double TestDoubleVal = -DBL_MIN;
  115. const AZStd::string TestStringVal = "This is an AZStd::string.";
  116. constexpr float TestFloatVal = FLT_MAX;
  117. const char* TestConstPointerVal = "This is a C array.";
  118. myVec.GetData<IntType>(index) = TestIntVal;
  119. myVec.GetData<StringType>(index) = TestStringVal;
  120. myVec.GetData<DoubleType>(index) = TestDoubleVal;
  121. myVec.GetData<FloatType>(index) = TestFloatVal;
  122. myVec.GetData<CharType>(index) = TestConstPointerVal;
  123. EXPECT_EQ(TestIntVal, static_cast<int>(myVec.GetData<IntType>(index)));
  124. EXPECT_EQ(TestStringVal, static_cast<AZStd::string>(myVec.GetData<StringType>(index)));
  125. EXPECT_EQ(TestDoubleVal, static_cast<double>(myVec.GetData<DoubleType>(index)));
  126. EXPECT_EQ(TestFloatVal, static_cast<float>(myVec.GetData<FloatType>(index)));
  127. EXPECT_STREQ(TestConstPointerVal, static_cast<const char*>(myVec.GetData<CharType>(index)));
  128. }
  129. MultiIndexedDataVector<int32_t, float> CreateTestVector(AZStd::vector<uint16_t>& indices)
  130. {
  131. enum Types
  132. {
  133. IntType = 0,
  134. FloatType = 1,
  135. };
  136. MultiIndexedDataVector<int32_t, float> myVec;
  137. constexpr int32_t Count = 10;
  138. int32_t startInt = 10;
  139. float startFloat = 2.0f;
  140. // Create some initial values
  141. for (uint32_t i = 0; i < Count; ++i)
  142. {
  143. uint16_t index = myVec.GetFreeSlotIndex();
  144. indices.push_back(index);
  145. myVec.GetData<IntType>(index) = startInt;
  146. myVec.GetData<FloatType>(index) = startFloat;
  147. startInt += 1;
  148. startFloat += 1.0f;
  149. }
  150. return myVec;
  151. }
  152. void CheckIndexedData(MultiIndexedDataVector<int32_t, float>& data, AZStd::vector<uint16_t>& indices)
  153. {
  154. enum Types
  155. {
  156. IntType = 0,
  157. FloatType = 1,
  158. };
  159. // For each index, get its data and make sure GetIndexForData returns the same
  160. // index used to retrieve the data
  161. for (uint32_t i = 0; i < data.GetDataCount(); ++i)
  162. {
  163. int32_t& intData = data.GetData<IntType>(indices.at(i));
  164. uint16_t indexForData = data.GetIndexForData<IntType>(&intData);
  165. EXPECT_EQ(indices.at(i), indexForData);
  166. float& floatData = data.GetData<FloatType>(indices.at(i));
  167. indexForData = data.GetIndexForData<FloatType>(&floatData);
  168. EXPECT_EQ(indices.at(i), indexForData);
  169. }
  170. }
  171. TEST_F(MultiIndexedDataVectorTests, GetIndexForDataSimple)
  172. {
  173. AZStd::vector<uint16_t> indices;
  174. MultiIndexedDataVector<int32_t, float> myVec = CreateTestVector(indices);
  175. CheckIndexedData(myVec, indices);
  176. }
  177. TEST_F(MultiIndexedDataVectorTests, GetIndexForDataComplex)
  178. {
  179. enum Types
  180. {
  181. IntType = 0,
  182. FloatType = 1,
  183. };
  184. AZStd::vector<uint16_t> indices;
  185. MultiIndexedDataVector<int32_t, float> myVec = CreateTestVector(indices);
  186. // remove every other value to shuffle the data around
  187. for (uint32_t i = 0; i < myVec.GetDataCount(); i += 2)
  188. {
  189. myVec.RemoveIndex(indices.at(i));
  190. }
  191. int32_t startInt = 100;
  192. float startFloat = 20.0f;
  193. // Add some data back in
  194. const size_t count = myVec.GetDataCount();
  195. for (uint32_t i = 0; i < count; i += 2)
  196. {
  197. uint16_t index = myVec.GetFreeSlotIndex();
  198. indices.at(i) = index;
  199. myVec.GetData<IntType>(index) = startInt;
  200. myVec.GetData<FloatType>(index) = startFloat;
  201. startInt += 1;
  202. startFloat += 1.0f;
  203. }
  204. CheckIndexedData(myVec, indices);
  205. }
  206. TEST_F(MultiIndexedDataVectorTests, ForEach)
  207. {
  208. enum Types
  209. {
  210. IntType = 0,
  211. FloatType = 1,
  212. };
  213. MultiIndexedDataVector<int32_t, float> myVec;
  214. constexpr int32_t Count = 10;
  215. int32_t startInt = 10;
  216. float startFloat = 2.0f;
  217. AZStd::vector<uint16_t> indices;
  218. AZStd::set<int32_t> intValues;
  219. AZStd::set<float> floatValues;
  220. // Create some initial values
  221. for (uint32_t i = 0; i < Count; ++i)
  222. {
  223. uint16_t index = myVec.GetFreeSlotIndex();
  224. indices.push_back(index);
  225. myVec.GetData<IntType>(index) = startInt;
  226. myVec.GetData<FloatType>(index) = startFloat;
  227. intValues.insert(startInt);
  228. floatValues.insert(startFloat);
  229. startInt += 1;
  230. startFloat += 1.0f;
  231. }
  232. uint32_t visitCount = 0;
  233. myVec.ForEach<IntType>([&](int32_t value) -> bool
  234. {
  235. intValues.erase(value);
  236. ++visitCount;
  237. return true; // keep iterating
  238. });
  239. // All ints should have been visited and found in the set
  240. EXPECT_EQ(visitCount, Count);
  241. EXPECT_EQ(intValues.size(), 0);
  242. visitCount = 0;
  243. myVec.ForEach<FloatType>([&](float value) -> bool
  244. {
  245. floatValues.erase(value);
  246. ++visitCount;
  247. return true; // keep iterating
  248. });
  249. // All floats should have been visited and found in the set
  250. EXPECT_EQ(visitCount, Count);
  251. EXPECT_EQ(floatValues.size(), 0);
  252. visitCount = 0;
  253. myVec.ForEach<IntType>([&]([[maybe_unused]] int32_t value) -> bool
  254. {
  255. ++visitCount;
  256. return false; // stop iterating
  257. });
  258. // Since false is immediately returned, only one element should have been visited.
  259. EXPECT_EQ(visitCount, 1);
  260. }
  261. }