ListTests.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. i64 limit = Core::Math::min(copy.getLength(), list.getLength());
  71. for(i64 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. void Core::testList(bool light) {
  191. testAdd();
  192. testMultipleAdd();
  193. testAddReplace();
  194. testClear();
  195. testShrink();
  196. testBigAdd(light);
  197. testCopy();
  198. testMove();
  199. testMoveAssignment();
  200. testToString1();
  201. testToString2();
  202. testToString3();
  203. testRemoveBySwap();
  204. testRemove();
  205. testResize();
  206. testDefaultResize();
  207. testInvalidReserve();
  208. testShrinkExact();
  209. testShrinkResize();
  210. testCopyEmpty();
  211. }