ListTests.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/List.hpp"
  4. #include "core/math/Math.hpp"
  5. template class Core::List<size_t>;
  6. using IntList = Core::List<size_t>;
  7. static void testAdd() {
  8. IntList list;
  9. list.add(5u);
  10. CORE_TEST_EQUAL(5, list[0]);
  11. CORE_TEST_EQUAL(1, list.getLength());
  12. }
  13. static void testMultipleAdd() {
  14. IntList list;
  15. list.add(4u).add(3u).add(2u);
  16. CORE_TEST_EQUAL(4, list[0]);
  17. CORE_TEST_EQUAL(3, list[1]);
  18. CORE_TEST_EQUAL(2, list[2]);
  19. CORE_TEST_EQUAL(3, list.getLength());
  20. }
  21. static void testAddReplace() {
  22. IntList list;
  23. list.add(5u);
  24. list[0] = 3;
  25. CORE_TEST_EQUAL(3, list[0]);
  26. }
  27. static void testClear() {
  28. IntList list;
  29. list.add(5u).add(4u);
  30. list.clear();
  31. CORE_TEST_EQUAL(0, list.getLength());
  32. }
  33. static void testShrink() {
  34. IntList list;
  35. list.add(5u).add(4u).add(3u);
  36. CORE_TEST_TRUE(list.getCapacity() >= 3);
  37. list.shrink();
  38. CORE_TEST_TRUE(list.getLength() == 3);
  39. CORE_TEST_TRUE(list.getCapacity() == 3);
  40. CORE_TEST_EQUAL(5, list[0]);
  41. CORE_TEST_EQUAL(4, list[1]);
  42. CORE_TEST_EQUAL(3, list[2]);
  43. }
  44. static void testBigAdd(bool light) {
  45. size_t limit = light ? 10000 : 100000;
  46. IntList list;
  47. for(size_t i = 0; i < limit; i++) {
  48. list.add(i);
  49. }
  50. for(size_t i = 0; i < list.getLength(); i++) {
  51. CORE_TEST_EQUAL(i, list[i]);
  52. }
  53. CORE_TEST_EQUAL(limit, list.getLength());
  54. }
  55. static void testCopy() {
  56. IntList list;
  57. list.add(1u).add(2u).add(3u);
  58. IntList copy = list;
  59. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  60. size_t limit = Core::Math::min(copy.getLength(), list.getLength());
  61. for(size_t i = 0; i < limit; i++) {
  62. CORE_TEST_EQUAL(list[i], copy[i]);
  63. }
  64. }
  65. static void testMove() {
  66. IntList list;
  67. list.add(1u).add(2u).add(3u);
  68. IntList move(Core::move(list));
  69. CORE_TEST_EQUAL(0, list.getLength());
  70. CORE_TEST_EQUAL(3, move.getLength());
  71. CORE_TEST_EQUAL(1, move[0]);
  72. CORE_TEST_EQUAL(2, move[1]);
  73. CORE_TEST_EQUAL(3, move[2]);
  74. }
  75. static void testMoveAssignment() {
  76. IntList list;
  77. list.add(1u).add(2u).add(3u);
  78. IntList move;
  79. move = Core::move(list);
  80. CORE_TEST_EQUAL(0, list.getLength());
  81. CORE_TEST_EQUAL(3, move.getLength());
  82. CORE_TEST_EQUAL(1, move[0]);
  83. CORE_TEST_EQUAL(2, move[1]);
  84. CORE_TEST_EQUAL(3, move[2]);
  85. }
  86. static void testToString1() {
  87. IntList list;
  88. list.add(1u).add(243u).add(423u);
  89. CORE_TEST_STRING("[1, 243, 423]", list);
  90. }
  91. static void testToString2() {
  92. IntList list;
  93. list.add(1u);
  94. CORE_TEST_STRING("[1]", list);
  95. }
  96. static void testToString3() {
  97. IntList list;
  98. CORE_TEST_STRING("[]", list);
  99. }
  100. static void testRemoveBySwap() {
  101. IntList list;
  102. list.add(4u).add(3u).add(2u);
  103. list.removeBySwap(0);
  104. CORE_TEST_EQUAL(2, list[0]);
  105. CORE_TEST_EQUAL(3, list[1]);
  106. CORE_TEST_EQUAL(2, list.getLength());
  107. list.removeBySwap(1);
  108. CORE_TEST_EQUAL(2, list[0]);
  109. CORE_TEST_EQUAL(1, list.getLength());
  110. list.removeBySwap(0);
  111. CORE_TEST_EQUAL(0, list.getLength());
  112. }
  113. static void testRemove() {
  114. IntList list;
  115. list.add(4u).add(3u).add(2u);
  116. list.remove(0);
  117. CORE_TEST_EQUAL(3, list[0]);
  118. CORE_TEST_EQUAL(2, list[1]);
  119. CORE_TEST_EQUAL(2, list.getLength());
  120. list.remove(1);
  121. CORE_TEST_EQUAL(3, list[0]);
  122. CORE_TEST_EQUAL(1, list.getLength());
  123. list.remove(0);
  124. CORE_TEST_EQUAL(0, list.getLength());
  125. }
  126. static void testRemoveLast() {
  127. IntList list;
  128. list.add(4u).add(3u).add(2u);
  129. list.removeLast();
  130. CORE_TEST_EQUAL(4, list[0]);
  131. CORE_TEST_EQUAL(3, list[1]);
  132. CORE_TEST_EQUAL(2, list.getLength());
  133. list.removeLast();
  134. CORE_TEST_EQUAL(4, list[0]);
  135. CORE_TEST_EQUAL(1, list.getLength());
  136. list.removeLast();
  137. CORE_TEST_EQUAL(0, list.getLength());
  138. }
  139. static void testResize() {
  140. IntList list;
  141. list.resize(5, 10);
  142. CORE_TEST_EQUAL(5, list.getLength());
  143. for(size_t i = 0; i < 5; i++) {
  144. CORE_TEST_EQUAL(10, list[i]);
  145. }
  146. }
  147. static void testDefaultResize() {
  148. IntList list;
  149. list.resize(5);
  150. CORE_TEST_EQUAL(5, list.getLength());
  151. for(size_t i = 0; i < 5; i++) {
  152. CORE_TEST_EQUAL(0, list[i]);
  153. }
  154. }
  155. static void testInvalidReserve() {
  156. IntList list;
  157. list.reserve(0);
  158. }
  159. static void testShrinkExact() {
  160. IntList list;
  161. list.resize(50);
  162. list.shrink();
  163. }
  164. static void testShrinkResize() {
  165. IntList list;
  166. list.resize(50);
  167. list.resize(20, 5);
  168. list.resize(10);
  169. }
  170. static void testCopyEmpty() {
  171. IntList list;
  172. IntList copy = list;
  173. }
  174. void Core::testList(bool light) {
  175. testAdd();
  176. testMultipleAdd();
  177. testAddReplace();
  178. testClear();
  179. testShrink();
  180. testBigAdd(light);
  181. testCopy();
  182. testMove();
  183. testMoveAssignment();
  184. testToString1();
  185. testToString2();
  186. testToString3();
  187. testRemoveBySwap();
  188. testRemove();
  189. testRemoveLast();
  190. testResize();
  191. testDefaultResize();
  192. testInvalidReserve();
  193. testShrinkExact();
  194. testShrinkResize();
  195. testCopyEmpty();
  196. }