ListTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "tests/ListTests.h"
  2. #include "data/List.h"
  3. #include "test/Test.h"
  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() {
  50. IntList list;
  51. for(int i = 0; i < 100000; i++) {
  52. CORE_TEST_ERROR(list.add(i));
  53. }
  54. for(int i = 0; i < list.getLength(); i++) {
  55. CORE_TEST_EQUAL(i, list[i]);
  56. }
  57. CORE_TEST_EQUAL(100000, list.getLength());
  58. }
  59. static void testCopy() {
  60. IntList list;
  61. CORE_TEST_ERROR(list.add(1));
  62. CORE_TEST_ERROR(list.add(2));
  63. CORE_TEST_ERROR(list.add(3));
  64. IntList copy;
  65. CORE_TEST_ERROR(copy.copyFrom(list));
  66. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  67. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  68. CORE_TEST_EQUAL(list[i], copy[i]);
  69. }
  70. }
  71. static void testMove() {
  72. IntList list;
  73. CORE_TEST_ERROR(list.add(1));
  74. CORE_TEST_ERROR(list.add(2));
  75. CORE_TEST_ERROR(list.add(3));
  76. IntList move(Core::move(list));
  77. CORE_TEST_EQUAL(0, list.getLength());
  78. CORE_TEST_EQUAL(3, move.getLength());
  79. CORE_TEST_EQUAL(1, move[0]);
  80. CORE_TEST_EQUAL(2, move[1]);
  81. CORE_TEST_EQUAL(3, move[2]);
  82. }
  83. static void testMoveAssignment() {
  84. IntList list;
  85. CORE_TEST_ERROR(list.add(1));
  86. CORE_TEST_ERROR(list.add(2));
  87. CORE_TEST_ERROR(list.add(3));
  88. IntList move;
  89. move = Core::move(list);
  90. CORE_TEST_EQUAL(0, list.getLength());
  91. CORE_TEST_EQUAL(3, move.getLength());
  92. CORE_TEST_EQUAL(1, move[0]);
  93. CORE_TEST_EQUAL(2, move[1]);
  94. CORE_TEST_EQUAL(3, move[2]);
  95. }
  96. static void testToString1() {
  97. IntList list;
  98. CORE_TEST_ERROR(list.add(1));
  99. CORE_TEST_ERROR(list.add(243));
  100. CORE_TEST_ERROR(list.add(-423));
  101. CORE_TEST_STRING("[1, 243, -423]", list);
  102. }
  103. static void testToString2() {
  104. IntList list;
  105. CORE_TEST_ERROR(list.add(1));
  106. CORE_TEST_STRING("[1]", list);
  107. }
  108. static void testToString3() {
  109. IntList list;
  110. CORE_TEST_STRING("[]", list);
  111. }
  112. static void testRemoveBySwap() {
  113. IntList list;
  114. CORE_TEST_ERROR(list.add(4));
  115. CORE_TEST_ERROR(list.add(3));
  116. CORE_TEST_ERROR(list.add(2));
  117. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(-1));
  118. CORE_TEST_ERROR(list.removeBySwap(0));
  119. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(2));
  120. CORE_TEST_EQUAL(2, list[0]);
  121. CORE_TEST_EQUAL(3, list[1]);
  122. CORE_TEST_EQUAL(2, list.getLength());
  123. CORE_TEST_ERROR(list.removeBySwap(1));
  124. CORE_TEST_EQUAL(2, list[0]);
  125. CORE_TEST_EQUAL(1, list.getLength());
  126. CORE_TEST_ERROR(list.removeBySwap(0));
  127. CORE_TEST_EQUAL(0, list.getLength());
  128. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(0));
  129. }
  130. static void testRemove() {
  131. IntList list;
  132. CORE_TEST_ERROR(list.add(4));
  133. CORE_TEST_ERROR(list.add(3));
  134. CORE_TEST_ERROR(list.add(2));
  135. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(-1));
  136. CORE_TEST_ERROR(list.remove(0));
  137. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(2));
  138. CORE_TEST_EQUAL(3, list[0]);
  139. CORE_TEST_EQUAL(2, list[1]);
  140. CORE_TEST_EQUAL(2, list.getLength());
  141. CORE_TEST_ERROR(list.remove(1));
  142. CORE_TEST_EQUAL(3, list[0]);
  143. CORE_TEST_EQUAL(1, list.getLength());
  144. CORE_TEST_ERROR(list.remove(0));
  145. CORE_TEST_EQUAL(0, list.getLength());
  146. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(0));
  147. }
  148. static void testResize() {
  149. IntList list;
  150. CORE_TEST_ERROR(list.resize(5, 10));
  151. CORE_TEST_EQUAL(5, list.getLength());
  152. for(int i = 0; i < 5; i++) {
  153. CORE_TEST_EQUAL(10, list[i]);
  154. }
  155. }
  156. static void testDefaultResize() {
  157. IntList list;
  158. CORE_TEST_ERROR(list.resize(5));
  159. CORE_TEST_EQUAL(5, list.getLength());
  160. for(int i = 0; i < 5; i++) {
  161. CORE_TEST_EQUAL(0, list[i]);
  162. }
  163. }
  164. void Core::ListTests::test() {
  165. testAdd();
  166. testMultipleAdd();
  167. testAddReplace();
  168. testClear();
  169. testShrink();
  170. testBigAdd();
  171. testCopy();
  172. testMove();
  173. testMoveAssignment();
  174. testToString1();
  175. testToString2();
  176. testToString3();
  177. testRemoveBySwap();
  178. testRemove();
  179. testResize();
  180. testDefaultResize();
  181. }