ArrayListTests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "../Tests.hpp"
  2. #include "core/data/ArrayList.hpp"
  3. template class Core::ArrayList<int, 20>;
  4. using IntList = Core::ArrayList<int, 20>;
  5. static void testAdd() {
  6. IntList list;
  7. CORE_TEST_ERROR(list.add(5));
  8. CORE_TEST_EQUAL(5, list[0]);
  9. CORE_TEST_EQUAL(1, list.getLength());
  10. }
  11. static void testMultipleAdd() {
  12. IntList list;
  13. CORE_TEST_ERROR(list.add(4));
  14. CORE_TEST_ERROR(list.add(3));
  15. CORE_TEST_ERROR(list.add(2));
  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. CORE_TEST_ERROR(list.add(5));
  24. list[0] = 3;
  25. CORE_TEST_EQUAL(3, list[0]);
  26. }
  27. static void testClear() {
  28. IntList list;
  29. CORE_TEST_ERROR(list.add(5));
  30. CORE_TEST_ERROR(list.add(4));
  31. list.clear();
  32. CORE_TEST_EQUAL(0, list.getLength());
  33. }
  34. static void testOverflow(bool light) {
  35. IntList list;
  36. for(int i = 0; i < 20; i++) {
  37. CORE_TEST_ERROR(list.add(i));
  38. }
  39. int limit = light ? 1000 : 100000;
  40. for(int i = 0; i < limit; i++) {
  41. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, list.add(i));
  42. }
  43. for(int i = 0; i < list.getLength(); i++) {
  44. CORE_TEST_EQUAL(i, list[i]);
  45. }
  46. }
  47. static void testCopy() {
  48. IntList list;
  49. CORE_TEST_ERROR(list.add(1));
  50. CORE_TEST_ERROR(list.add(2));
  51. CORE_TEST_ERROR(list.add(3));
  52. IntList copy(list);
  53. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  54. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  55. CORE_TEST_EQUAL(list[i], copy[i]);
  56. }
  57. }
  58. static void testCopyAssignment() {
  59. IntList list;
  60. CORE_TEST_ERROR(list.add(1));
  61. CORE_TEST_ERROR(list.add(2));
  62. CORE_TEST_ERROR(list.add(3));
  63. IntList copy;
  64. copy = list;
  65. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  66. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  67. CORE_TEST_EQUAL(list[i], copy[i]);
  68. }
  69. }
  70. static void testMove() {
  71. IntList list;
  72. CORE_TEST_ERROR(list.add(1));
  73. CORE_TEST_ERROR(list.add(2));
  74. CORE_TEST_ERROR(list.add(3));
  75. IntList move(Core::move(list));
  76. CORE_TEST_EQUAL(0, list.getLength());
  77. CORE_TEST_EQUAL(3, move.getLength());
  78. CORE_TEST_EQUAL(1, move[0]);
  79. CORE_TEST_EQUAL(2, move[1]);
  80. CORE_TEST_EQUAL(3, move[2]);
  81. }
  82. static void testMoveAssignment() {
  83. IntList list;
  84. CORE_TEST_ERROR(list.add(1));
  85. CORE_TEST_ERROR(list.add(2));
  86. CORE_TEST_ERROR(list.add(3));
  87. IntList move;
  88. move = Core::move(list);
  89. CORE_TEST_EQUAL(0, list.getLength());
  90. CORE_TEST_EQUAL(3, move.getLength());
  91. CORE_TEST_EQUAL(1, move[0]);
  92. CORE_TEST_EQUAL(2, move[1]);
  93. CORE_TEST_EQUAL(3, move[2]);
  94. }
  95. static void testToString1() {
  96. IntList list;
  97. CORE_TEST_ERROR(list.add(1));
  98. CORE_TEST_ERROR(list.add(243));
  99. CORE_TEST_ERROR(list.add(-423));
  100. CORE_TEST_STRING("[1, 243, -423]", list);
  101. }
  102. static void testToString2() {
  103. IntList list;
  104. CORE_TEST_ERROR(list.add(1));
  105. CORE_TEST_STRING("[1]", list);
  106. }
  107. static void testToString3() {
  108. IntList list;
  109. CORE_TEST_STRING("[]", list);
  110. }
  111. static void testRemove() {
  112. IntList list;
  113. CORE_TEST_ERROR(list.add(4));
  114. CORE_TEST_ERROR(list.add(3));
  115. CORE_TEST_ERROR(list.add(2));
  116. CORE_TEST_ERROR(list.removeBySwap(0));
  117. CORE_TEST_EQUAL(2, list[0]);
  118. CORE_TEST_EQUAL(3, list[1]);
  119. CORE_TEST_EQUAL(2, list.getLength());
  120. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(4));
  121. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(-1));
  122. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(-5));
  123. CORE_TEST_EQUAL(2, list[0]);
  124. CORE_TEST_EQUAL(3, list[1]);
  125. CORE_TEST_EQUAL(2, list.getLength());
  126. CORE_TEST_ERROR(list.removeBySwap(1));
  127. CORE_TEST_EQUAL(2, list[0]);
  128. CORE_TEST_EQUAL(1, list.getLength());
  129. CORE_TEST_ERROR(list.removeBySwap(0));
  130. CORE_TEST_EQUAL(0, list.getLength());
  131. }
  132. static void testForRange() {
  133. IntList list;
  134. CORE_TEST_ERROR(list.add(1));
  135. CORE_TEST_ERROR(list.add(2));
  136. CORE_TEST_ERROR(list.add(3));
  137. for(int& i : list) {
  138. i++;
  139. }
  140. for(int i = 0; i < list.getLength(); i++) {
  141. CORE_TEST_EQUAL(i + 2, list[i]);
  142. }
  143. }
  144. void Core::testArrayList(bool light) {
  145. testAdd();
  146. testMultipleAdd();
  147. testAddReplace();
  148. testClear();
  149. testOverflow(light);
  150. testCopy();
  151. testCopyAssignment();
  152. testMove();
  153. testMoveAssignment();
  154. testToString1();
  155. testToString2();
  156. testToString3();
  157. testRemove();
  158. testForRange();
  159. }