ListTests.cpp 5.9 KB

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