TArrayTest.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkRefCnt.h"
  8. #include "include/private/SkTArray.h"
  9. #include "include/utils/SkRandom.h"
  10. #include "tests/Test.h"
  11. // Tests the SkTArray<T> class template.
  12. template <bool MEM_MOVE>
  13. static void TestTSet_basic(skiatest::Reporter* reporter) {
  14. SkTArray<int, MEM_MOVE> a;
  15. // Starts empty.
  16. REPORTER_ASSERT(reporter, a.empty());
  17. REPORTER_ASSERT(reporter, a.count() == 0);
  18. // { }, add a default constructed element
  19. a.push_back() = 0;
  20. REPORTER_ASSERT(reporter, !a.empty());
  21. REPORTER_ASSERT(reporter, a.count() == 1);
  22. // { 0 }, removeShuffle the only element.
  23. a.removeShuffle(0);
  24. REPORTER_ASSERT(reporter, a.empty());
  25. REPORTER_ASSERT(reporter, a.count() == 0);
  26. // { }, add a default, add a 1, remove first
  27. a.push_back() = 0;
  28. REPORTER_ASSERT(reporter, a.push_back() = 1);
  29. a.removeShuffle(0);
  30. REPORTER_ASSERT(reporter, !a.empty());
  31. REPORTER_ASSERT(reporter, a.count() == 1);
  32. REPORTER_ASSERT(reporter, a[0] == 1);
  33. // { 1 }, replace with new array
  34. int b[5] = { 0, 1, 2, 3, 4 };
  35. a.reset(b, SK_ARRAY_COUNT(b));
  36. REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b));
  37. REPORTER_ASSERT(reporter, a[2] == 2);
  38. REPORTER_ASSERT(reporter, a[4] == 4);
  39. // { 0, 1, 2, 3, 4 }, removeShuffle the last
  40. a.removeShuffle(4);
  41. REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b) - 1);
  42. REPORTER_ASSERT(reporter, a[3] == 3);
  43. // { 0, 1, 2, 3 }, remove a middle, note shuffle
  44. a.removeShuffle(1);
  45. REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b) - 2);
  46. REPORTER_ASSERT(reporter, a[0] == 0);
  47. REPORTER_ASSERT(reporter, a[1] == 3);
  48. REPORTER_ASSERT(reporter, a[2] == 2);
  49. // {0, 3, 2 }
  50. }
  51. template <typename T> static void test_swap(skiatest::Reporter* reporter,
  52. SkTArray<T>* (&arrays)[4],
  53. int (&sizes)[7])
  54. {
  55. for (auto a : arrays) {
  56. for (auto b : arrays) {
  57. if (a == b) {
  58. continue;
  59. }
  60. for (auto sizeA : sizes) {
  61. for (auto sizeB : sizes) {
  62. a->reset();
  63. b->reset();
  64. int curr = 0;
  65. for (int i = 0; i < sizeA; i++) { a->push_back(curr++); }
  66. for (int i = 0; i < sizeB; i++) { b->push_back(curr++); }
  67. a->swap(*b);
  68. REPORTER_ASSERT(reporter, b->count() == sizeA);
  69. REPORTER_ASSERT(reporter, a->count() == sizeB);
  70. curr = 0;
  71. for (auto&& x : *b) { REPORTER_ASSERT(reporter, x == curr++); }
  72. for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); }
  73. a->swap(*a);
  74. curr = sizeA;
  75. for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); }
  76. }}
  77. }}
  78. }
  79. static void test_swap(skiatest::Reporter* reporter) {
  80. int sizes[] = {0, 1, 5, 10, 15, 20, 25};
  81. SkTArray<int> arr;
  82. SkSTArray< 5, int> arr5;
  83. SkSTArray<10, int> arr10;
  84. SkSTArray<20, int> arr20;
  85. SkTArray<int>* arrays[] = { &arr, &arr5, &arr10, &arr20 };
  86. test_swap(reporter, arrays, sizes);
  87. struct MoveOnlyInt {
  88. MoveOnlyInt(int i) : fInt(i) {}
  89. MoveOnlyInt(MoveOnlyInt&& that) : fInt(that.fInt) {}
  90. bool operator==(int i) { return fInt == i; }
  91. int fInt;
  92. };
  93. SkTArray<MoveOnlyInt> moi;
  94. SkSTArray< 5, MoveOnlyInt> moi5;
  95. SkSTArray<10, MoveOnlyInt> moi10;
  96. SkSTArray<20, MoveOnlyInt> moi20;
  97. SkTArray<MoveOnlyInt>* arraysMoi[] = { &moi, &moi5, &moi10, &moi20 };
  98. test_swap(reporter, arraysMoi, sizes);
  99. }
  100. template <typename T, bool MEM_MOVE> int SkTArray<T, MEM_MOVE>::allocCntForTest() const {
  101. return fAllocCount;
  102. }
  103. void test_unnecessary_alloc(skiatest::Reporter* reporter) {
  104. {
  105. SkTArray<int> a;
  106. REPORTER_ASSERT(reporter, a.allocCntForTest() == 0);
  107. }
  108. {
  109. SkSTArray<10, int> a;
  110. REPORTER_ASSERT(reporter, a.allocCntForTest() == 10);
  111. }
  112. {
  113. SkTArray<int> a(1);
  114. REPORTER_ASSERT(reporter, a.allocCntForTest() >= 1);
  115. }
  116. {
  117. SkTArray<int> a, b;
  118. b = a;
  119. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  120. }
  121. {
  122. SkSTArray<10, int> a;
  123. SkTArray<int> b;
  124. b = a;
  125. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  126. }
  127. {
  128. SkTArray<int> a;
  129. SkTArray<int> b(a);
  130. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  131. }
  132. {
  133. SkSTArray<10, int> a;
  134. SkTArray<int> b(a);
  135. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  136. }
  137. {
  138. SkTArray<int> a;
  139. SkTArray<int> b(std::move(a));
  140. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  141. }
  142. {
  143. SkSTArray<10, int> a;
  144. SkTArray<int> b(std::move(a));
  145. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  146. }
  147. {
  148. SkTArray<int> a;
  149. SkTArray<int> b;
  150. b = std::move(a);
  151. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  152. }
  153. {
  154. SkSTArray<10, int> a;
  155. SkTArray<int> b;
  156. b = std::move(a);
  157. REPORTER_ASSERT(reporter, b.allocCntForTest() == 0);
  158. }
  159. }
  160. static void test_self_assignment(skiatest::Reporter* reporter) {
  161. SkTArray<int> a;
  162. a.push_back(1);
  163. REPORTER_ASSERT(reporter, !a.empty());
  164. REPORTER_ASSERT(reporter, a.count() == 1);
  165. REPORTER_ASSERT(reporter, a[0] == 1);
  166. a = static_cast<decltype(a)&>(a);
  167. REPORTER_ASSERT(reporter, !a.empty());
  168. REPORTER_ASSERT(reporter, a.count() == 1);
  169. REPORTER_ASSERT(reporter, a[0] == 1);
  170. }
  171. template <typename Array> static void test_array_reserve(skiatest::Reporter* reporter,
  172. Array* array, int reserveCount) {
  173. SkRandom random;
  174. REPORTER_ASSERT(reporter, array->allocCntForTest() >= reserveCount);
  175. array->push_back();
  176. REPORTER_ASSERT(reporter, array->allocCntForTest() >= reserveCount);
  177. array->pop_back();
  178. REPORTER_ASSERT(reporter, array->allocCntForTest() >= reserveCount);
  179. while (array->count() < reserveCount) {
  180. // Two steps forward, one step back
  181. if (random.nextULessThan(3) < 2) {
  182. array->push_back();
  183. } else if (array->count() > 0) {
  184. array->pop_back();
  185. }
  186. REPORTER_ASSERT(reporter, array->allocCntForTest() >= reserveCount);
  187. }
  188. }
  189. template<typename Array> static void test_reserve(skiatest::Reporter* reporter) {
  190. // Test that our allocated space stays >= to the reserve count until the array is filled to
  191. // the reserve count
  192. for (int reserveCount : {1, 2, 10, 100}) {
  193. // Test setting reserve in constructor.
  194. Array array1(reserveCount);
  195. test_array_reserve(reporter, &array1, reserveCount);
  196. // Test setting reserve after constructor.
  197. Array array2;
  198. array2.reserve(reserveCount);
  199. test_array_reserve(reporter, &array2, reserveCount);
  200. // Test increasing reserve after constructor.
  201. Array array3(reserveCount/2);
  202. array3.reserve(reserveCount);
  203. test_array_reserve(reporter, &array3, reserveCount);
  204. // Test setting reserve on non-empty array.
  205. Array array4;
  206. array4.push_back_n(reserveCount);
  207. array4.reserve(reserveCount);
  208. array4.pop_back_n(reserveCount);
  209. test_array_reserve(reporter, &array4, 2 * reserveCount);
  210. }
  211. }
  212. DEF_TEST(TArray, reporter) {
  213. TestTSet_basic<true>(reporter);
  214. TestTSet_basic<false>(reporter);
  215. test_swap(reporter);
  216. test_unnecessary_alloc(reporter);
  217. test_self_assignment(reporter);
  218. test_reserve<SkTArray<int>>(reporter);
  219. test_reserve<SkSTArray<1, int>>(reporter);
  220. test_reserve<SkSTArray<2, int>>(reporter);
  221. test_reserve<SkSTArray<16, int>>(reporter);
  222. }