2
0

DynamicArray.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <anki/util/DynamicArray.h>
  7. #include <vector>
  8. #include <ctime>
  9. namespace anki
  10. {
  11. static I64 constructor0Count = 0;
  12. static I64 constructor1Count = 0;
  13. static I64 constructor2Count = 0;
  14. static I64 constructor3Count = 0;
  15. static I64 destructorCount = 0;
  16. static I64 copyCount = 0;
  17. static I64 moveCount = 0;
  18. class DynamicArrayFoo
  19. {
  20. public:
  21. int m_x;
  22. DynamicArrayFoo()
  23. : m_x(0)
  24. {
  25. ++constructor0Count;
  26. }
  27. DynamicArrayFoo(int x)
  28. : m_x(x)
  29. {
  30. ++constructor1Count;
  31. }
  32. DynamicArrayFoo(const DynamicArrayFoo& b)
  33. : m_x(b.m_x)
  34. {
  35. ++constructor2Count;
  36. }
  37. DynamicArrayFoo(DynamicArrayFoo&& b)
  38. : m_x(b.m_x)
  39. {
  40. b.m_x = 0;
  41. ++constructor3Count;
  42. }
  43. ~DynamicArrayFoo()
  44. {
  45. ++destructorCount;
  46. }
  47. DynamicArrayFoo& operator=(const DynamicArrayFoo& b)
  48. {
  49. m_x = b.m_x;
  50. ++copyCount;
  51. return *this;
  52. }
  53. DynamicArrayFoo& operator=(DynamicArrayFoo&& b)
  54. {
  55. m_x = b.m_x;
  56. b.m_x = 0;
  57. ++moveCount;
  58. return *this;
  59. }
  60. };
  61. } // end namespace anki
  62. ANKI_TEST(Util, DynamicArray)
  63. {
  64. {
  65. HeapAllocator<U8> alloc(allocAligned, nullptr);
  66. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  67. arr.resize(0);
  68. arr.resize(2, 1);
  69. arr.resize(3, 2);
  70. arr.resize(4, 3);
  71. ANKI_TEST_EXPECT_EQ(arr.getSize(), 4);
  72. ANKI_TEST_EXPECT_EQ(arr[0].m_x, 1);
  73. ANKI_TEST_EXPECT_EQ(arr[1].m_x, 1);
  74. ANKI_TEST_EXPECT_EQ(arr[2].m_x, 2);
  75. ANKI_TEST_EXPECT_EQ(arr[3].m_x, 3);
  76. arr.emplaceBack(4);
  77. ANKI_TEST_EXPECT_EQ(arr.getSize(), 5);
  78. ANKI_TEST_EXPECT_EQ(arr[4].m_x, 4);
  79. arr.resize(1);
  80. arr.resize(0);
  81. }
  82. // Fuzzy
  83. {
  84. srand(U32(time(nullptr)));
  85. HeapAllocator<U8> alloc(allocAligned, nullptr);
  86. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  87. std::vector<DynamicArrayFoo> vec;
  88. const U ITERATIONS = 1000000;
  89. for(U i = 0; i < ITERATIONS; ++i)
  90. {
  91. const Bool grow = arr.getSize() > 0 && (rand() & 1);
  92. U32 newSize;
  93. U32 value = rand();
  94. if(grow)
  95. {
  96. newSize = U32(vec.size()) * getRandomRange(1, 4);
  97. }
  98. else
  99. {
  100. newSize = U32(F32(vec.size()) * getRandomRange(0.0, 0.9));
  101. }
  102. vec.resize(newSize, value);
  103. arr.resize(newSize, value);
  104. // Validate
  105. ANKI_TEST_EXPECT_EQ(arr.getSize(), vec.size());
  106. for(U32 j = 0; j < arr.getSize(); ++j)
  107. {
  108. ANKI_TEST_EXPECT_EQ(arr[j].m_x, vec[j].m_x);
  109. }
  110. arr.validate();
  111. }
  112. arr = DynamicArrayAuto<DynamicArrayFoo>(alloc);
  113. vec = std::vector<DynamicArrayFoo>();
  114. ANKI_TEST_EXPECT_GT(destructorCount, 0);
  115. ANKI_TEST_EXPECT_EQ(constructor0Count + constructor1Count + constructor2Count + constructor3Count,
  116. destructorCount);
  117. }
  118. }
  119. ANKI_TEST(Util, DynamicArrayEmplaceAt)
  120. {
  121. HeapAllocator<U8> alloc(allocAligned, nullptr);
  122. // Empty & add to the end
  123. {
  124. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  125. arr.emplaceAt(arr.getEnd(), 12);
  126. ANKI_TEST_EXPECT_EQ(arr[0].m_x, 12);
  127. }
  128. // 1 element & add to he end
  129. {
  130. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  131. arr.emplaceBack(12);
  132. arr.emplaceAt(arr.getEnd(), 34);
  133. ANKI_TEST_EXPECT_EQ(arr[0].m_x, 12);
  134. ANKI_TEST_EXPECT_EQ(arr[1].m_x, 34);
  135. }
  136. // 1 element & add to 0
  137. {
  138. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  139. arr.emplaceBack(12);
  140. arr.emplaceAt(arr.getBegin(), 34);
  141. ANKI_TEST_EXPECT_EQ(arr[0].m_x, 34);
  142. ANKI_TEST_EXPECT_EQ(arr[1].m_x, 12);
  143. }
  144. // A bit more complex
  145. {
  146. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  147. for(I32 i = 0; i < 10; ++i)
  148. {
  149. arr.emplaceBack(i);
  150. }
  151. arr.emplaceAt(arr.getBegin() + 4, 666);
  152. for(I32 i = 0; i < 10 + 1; ++i)
  153. {
  154. if(i < 4)
  155. {
  156. ANKI_TEST_EXPECT_EQ(arr[i].m_x, i);
  157. }
  158. else if(i == 4)
  159. {
  160. ANKI_TEST_EXPECT_EQ(arr[i].m_x, 666);
  161. }
  162. else
  163. {
  164. ANKI_TEST_EXPECT_EQ(arr[i].m_x, i - 1);
  165. }
  166. }
  167. }
  168. // Fuzzy
  169. {
  170. srand(U32(time(nullptr)));
  171. DynamicArrayAuto<DynamicArrayFoo> arr(alloc);
  172. std::vector<DynamicArrayFoo> vec;
  173. const I ITERATIONS = 10000;
  174. for(I i = 0; i < ITERATIONS; ++i)
  175. {
  176. I32 randNum = rand();
  177. I32 op = rand() % 3;
  178. switch(op)
  179. {
  180. case 0:
  181. // Push back
  182. arr.emplaceBack(randNum);
  183. vec.emplace_back(randNum);
  184. break;
  185. case 1:
  186. // Insert somewhere
  187. if(!arr.isEmpty())
  188. {
  189. I pos = rand() % arr.getSize();
  190. arr.emplaceAt(arr.getBegin() + pos, randNum);
  191. vec.emplace(vec.begin() + pos, randNum);
  192. }
  193. break;
  194. default:
  195. // Insert at the end
  196. arr.emplaceAt(arr.getEnd(), randNum);
  197. vec.emplace(vec.end(), randNum);
  198. break;
  199. }
  200. }
  201. // Check
  202. ANKI_TEST_EXPECT_EQ(arr.getSize(), vec.size());
  203. for(U32 i = 0; i < arr.getSize(); ++i)
  204. {
  205. ANKI_TEST_EXPECT_EQ(arr[i].m_x, vec[i].m_x);
  206. }
  207. arr.destroy();
  208. vec.resize(0);
  209. ANKI_TEST_EXPECT_EQ(constructor0Count + constructor1Count + constructor2Count + constructor3Count,
  210. destructorCount);
  211. }
  212. }