IndexableListTests.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/IndexableList.h>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <gtest/gtest.h>
  13. #include <AzCore/Math/Random.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. namespace UnitTest
  16. {
  17. using namespace AZ;
  18. using namespace AZ::Render;
  19. class IndexableListTests
  20. : public UnitTest::LeakDetectionFixture
  21. {
  22. };
  23. TEST_F(IndexableListTests, TestBasics)
  24. {
  25. IndexableList<float> container;
  26. EXPECT_EQ(0, container.size());
  27. EXPECT_EQ(0, container.capacity());
  28. EXPECT_EQ(-1, container.begin());
  29. }
  30. TEST_F(IndexableListTests, TestReserveFromZero)
  31. {
  32. IndexableList<float> container;
  33. container.reserve(1);
  34. EXPECT_LE(1, container.capacity());
  35. EXPECT_EQ(0, container.size());
  36. EXPECT_EQ(-1, container.begin());
  37. }
  38. TEST_F(IndexableListTests, TestPushFront)
  39. {
  40. const float valueToInsert = 123.25f;
  41. IndexableList<float> container;
  42. int position = container.push_front(valueToInsert);
  43. EXPECT_EQ(1, container.size());
  44. EXPECT_EQ(valueToInsert, container[position]);
  45. }
  46. TEST_F(IndexableListTests, TestErase)
  47. {
  48. const float valueToInsert = 123.25f;
  49. IndexableList<float> container;
  50. int position = container.push_front(valueToInsert);
  51. container.erase(position);
  52. EXPECT_EQ(0, container.size());
  53. }
  54. TEST_F(IndexableListTests, TestBegin)
  55. {
  56. const int testValue = 123;
  57. IndexableList<int> container;
  58. container.push_front(testValue);
  59. int listHead = container.begin();
  60. EXPECT_EQ(testValue, container[listHead]);
  61. }
  62. TEST_F(IndexableListTests, TestNextOnce)
  63. {
  64. const int testValue0 = 123;
  65. const int testValue1 = 456;
  66. IndexableList<int> container;
  67. container.push_front(testValue0);
  68. container.push_front(testValue1);
  69. int iterator = container.begin();
  70. iterator = container.next(iterator);
  71. EXPECT_EQ(testValue0, container[iterator]);
  72. }
  73. TEST_F(IndexableListTests, TestNextMultiple)
  74. {
  75. IndexableList<int> container;
  76. container.push_front(0);
  77. container.push_front(1);
  78. int element2 = container.push_front(2);
  79. container.push_front(3);
  80. container.erase(element2);
  81. int numItemsIteratedThrough = 0;
  82. int iterator = container.begin();
  83. while (iterator != -1)
  84. {
  85. iterator = container.next(iterator);
  86. numItemsIteratedThrough++;
  87. }
  88. EXPECT_EQ(numItemsIteratedThrough, container.size());
  89. }
  90. TEST_F(IndexableListTests, TestMultipleReserve)
  91. {
  92. const int testValue0 = -9;
  93. const int testValue1 = 65;
  94. const int testValue2 = 32;
  95. IndexableList<int> container;
  96. container.reserve(2);
  97. int element0 = container.push_front(testValue0);
  98. container.reserve(4);
  99. int element1 = container.push_front(testValue1);
  100. container.reserve(6);
  101. int element2 = container.push_front(testValue2);
  102. EXPECT_LE(6, container.capacity());
  103. EXPECT_EQ(3, container.size());
  104. EXPECT_EQ(testValue0, container[element0]);
  105. EXPECT_EQ(testValue1, container[element1]);
  106. EXPECT_EQ(testValue2, container[element2]);
  107. }
  108. TEST_F(IndexableListTests, TestInsertToMaxThenReserve)
  109. {
  110. const int testValue0 = -9;
  111. const int testValue1 = 65;
  112. const int testValue2 = 32;
  113. const int testValue3 = 0;
  114. const int testValue4 = -1;
  115. const int testValue5 = 2;
  116. IndexableList<int> container;
  117. container.reserve(3);
  118. int element0 = container.push_front(testValue0);
  119. int element1 = container.push_front(testValue1);
  120. int element2 = container.push_front(testValue2);
  121. container.reserve(6);
  122. int element3 = container.push_front(testValue3);
  123. int element4 = container.push_front(testValue4);
  124. int element5 = container.push_front(testValue5);
  125. EXPECT_EQ(testValue0, container[element0]);
  126. EXPECT_EQ(testValue1, container[element1]);
  127. EXPECT_EQ(testValue2, container[element2]);
  128. EXPECT_EQ(testValue3, container[element3]);
  129. EXPECT_EQ(testValue4, container[element4]);
  130. EXPECT_EQ(testValue5, container[element5]);
  131. }
  132. TEST_F(IndexableListTests, TestHolesInList)
  133. {
  134. const int testValue0 = -9;
  135. const int testValue1 = 65;
  136. const int testValue2 = 32;
  137. const int testValue3 = 0;
  138. const int testValue4 = -1;
  139. const int testValue5 = 2;
  140. IndexableList<int> container;
  141. int element0 = container.push_front(testValue0);
  142. int element1 = container.push_front(testValue1);
  143. int element2 = container.push_front(testValue2);
  144. container.erase(element1);
  145. int element3 = container.push_front(testValue3);
  146. int element4 = container.push_front(testValue4);
  147. int element5 = container.push_front(testValue5);
  148. container.erase(element4);
  149. EXPECT_EQ(4, container.size());
  150. EXPECT_EQ(testValue0, container[element0]);
  151. EXPECT_EQ(testValue2, container[element2]);
  152. EXPECT_EQ(testValue3, container[element3]);
  153. EXPECT_EQ(testValue5, container[element5]);
  154. }
  155. TEST_F(IndexableListTests, TestArraySize)
  156. {
  157. const int testValue0 = 5;
  158. IndexableList<int> container;
  159. int element0 = container.push_front(testValue0);
  160. container.erase(element0);
  161. EXPECT_LE(container.size(), container.array_size());
  162. EXPECT_LE(1, container.array_size());
  163. }
  164. TEST_F(IndexableListTests, ClearTest)
  165. {
  166. const int testValue = 5;
  167. IndexableList<int> container;
  168. container.push_front(testValue);
  169. EXPECT_EQ(1, container.size());
  170. container.clear();
  171. EXPECT_EQ(0, container.size());
  172. EXPECT_EQ(-1, container.begin());
  173. }
  174. }