ListTests.cpp 6.3 KB

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