ListTests.cpp 5.3 KB

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