ListTests.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "tests/ListTests.h"
  2. #include "data/List.h"
  3. #include "test/Test.h"
  4. using IntList = Core::List<int>;
  5. using String = Core::ArrayString<128>;
  6. template<typename T>
  7. static String build(Core::Test& test, const T& t) {
  8. String s;
  9. test.checkFalse(s.append(t), "append works");
  10. return s;
  11. }
  12. static void testAdd(Core::Test& test) {
  13. IntList list;
  14. test.checkFalse(list.add(5), "add works 1");
  15. test.checkEqual(5, list[0], "contains added value");
  16. test.checkEqual(1, list.getLength(), "sizes is increased by add");
  17. }
  18. static void testMultipleAdd(Core::Test& test) {
  19. IntList list;
  20. test.checkFalse(list.add(4), "add works 2");
  21. test.checkFalse(list.add(3), "add works 3");
  22. test.checkFalse(list.add(2), "add works 4");
  23. test.checkEqual(4, list[0], "contains added value 1");
  24. test.checkEqual(3, list[1], "contains added value 2");
  25. test.checkEqual(2, list[2], "contains added value 3");
  26. test.checkEqual(3, list.getLength(), "sizes is increased by add");
  27. }
  28. static void testAddReplace(Core::Test& test) {
  29. IntList list;
  30. test.checkFalse(list.add(5), "add works 5");
  31. list[0] = 3;
  32. test.checkEqual(3, list[0], "value is overwritten");
  33. }
  34. static void testClear(Core::Test& test) {
  35. IntList list;
  36. test.checkFalse(list.add(5), "add works 6");
  37. test.checkFalse(list.add(4), "add works 7");
  38. list.clear();
  39. test.checkEqual(0, list.getLength(), "length is 0 after clear");
  40. }
  41. static void testShrink(Core::Test& test) {
  42. IntList list;
  43. test.checkFalse(list.add(5), "add works 8");
  44. test.checkFalse(list.add(4), "add works 9");
  45. test.checkFalse(list.add(3), "add works 10");
  46. test.checkTrue(list.getCapacity() >= 3, "capacity is >= 3 after adding");
  47. test.checkFalse(list.shrink(), "shrink works 1");
  48. test.checkTrue(list.getLength() == 3, "length is 3 after shrink");
  49. test.checkTrue(list.getCapacity() == 3, "capacity is 3 after shrink");
  50. test.checkEqual(5, list[0], "data ok after shrink 1");
  51. test.checkEqual(4, list[1], "data ok after shrink 2");
  52. test.checkEqual(3, list[2], "data ok after shrink 3");
  53. }
  54. static void testBigAdd(Core::Test& test) {
  55. IntList list;
  56. for(int i = 0; i < 1000000; i++) {
  57. test.checkFalse(list.add(i), "add works in big add");
  58. }
  59. for(int i = 0; i < list.getLength(); i++) {
  60. test.checkEqual(i, list[i], "big add");
  61. }
  62. test.checkEqual(1000000, list.getLength(), "big add length");
  63. }
  64. static void testCopy(Core::Test& test) {
  65. IntList list;
  66. test.checkFalse(list.add(1), "add works 11");
  67. test.checkFalse(list.add(2), "add works 12");
  68. test.checkFalse(list.add(3), "add works 13");
  69. IntList copy;
  70. test.checkFalse(copy.copyFrom(list), "copy works 1");
  71. test.checkEqual(list.getLength(), copy.getLength(), "copy has same length");
  72. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  73. test.checkEqual(list[i], copy[i], "copy has same values");
  74. }
  75. }
  76. static void testMove(Core::Test& test) {
  77. IntList list;
  78. test.checkFalse(list.add(1), "add works 14");
  79. test.checkFalse(list.add(2), "add works 15");
  80. test.checkFalse(list.add(3), "add works 16");
  81. IntList move(Core::move(list));
  82. test.checkEqual(0, list.getLength(), "moved has length 0");
  83. test.checkEqual(3, move.getLength(), "moved passes length");
  84. test.checkEqual(1, move[0], "moved passes values");
  85. test.checkEqual(2, move[1], "moved passes values");
  86. test.checkEqual(3, move[2], "moved passes values");
  87. }
  88. static void testMoveAssignment(Core::Test& test) {
  89. IntList list;
  90. test.checkFalse(list.add(1), "add works 17");
  91. test.checkFalse(list.add(2), "add works 18");
  92. test.checkFalse(list.add(3), "add works 19");
  93. IntList move;
  94. move = Core::move(list);
  95. test.checkEqual(0, list.getLength(), "assignment moved has length 0");
  96. test.checkEqual(3, move.getLength(), "assignment moved passes length");
  97. test.checkEqual(1, move[0], "assignment moved passes values");
  98. test.checkEqual(2, move[1], "assignment moved passes values");
  99. test.checkEqual(3, move[2], "assignment moved passes values");
  100. }
  101. static void testToString1(Core::Test& test) {
  102. IntList list;
  103. test.checkFalse(list.add(1), "add works 20");
  104. test.checkFalse(list.add(243), "add works 21");
  105. test.checkFalse(list.add(-423), "add works 22");
  106. test.checkEqual(build(test, "[1, 243, -423]"), build(test, list),
  107. "to string 1");
  108. }
  109. static void testToString2(Core::Test& test) {
  110. IntList list;
  111. test.checkFalse(list.add(1), "add works 23");
  112. test.checkEqual(build(test, "[1]"), build(test, list), "to string 2");
  113. }
  114. static void testToString3(Core::Test& test) {
  115. IntList list;
  116. test.checkEqual(build(test, "[]"), build(test, list), "to string 3");
  117. }
  118. static void testRemoveBySwap(Core::Test& test) {
  119. IntList list;
  120. test.checkFalse(list.add(4), "add works 24");
  121. test.checkFalse(list.add(3), "add works 25");
  122. test.checkFalse(list.add(2), "add works 26");
  123. test.checkTrue(list.removeBySwap(-1), "remove by swap does not work 1");
  124. test.checkFalse(list.removeBySwap(0), "remove by swap works 1");
  125. test.checkTrue(list.removeBySwap(2), "remove by swap does not work 2");
  126. test.checkEqual(2, list[0], "remove by swap 1");
  127. test.checkEqual(3, list[1], "remove by swap 2");
  128. test.checkEqual(2, list.getLength(), "remove by swap 3");
  129. test.checkFalse(list.removeBySwap(1), "remove by swap works 2");
  130. test.checkEqual(2, list[0], "remove by swap 4");
  131. test.checkEqual(1, list.getLength(), "remove by swap 5");
  132. test.checkFalse(list.removeBySwap(0), "remove by swap works 3");
  133. test.checkEqual(0, list.getLength(), "remove by swap 6");
  134. test.checkTrue(list.removeBySwap(0), "remove by swap does not work 3");
  135. }
  136. static void testRemove(Core::Test& test) {
  137. IntList list;
  138. test.checkFalse(list.add(4), "add works 27");
  139. test.checkFalse(list.add(3), "add works 28");
  140. test.checkFalse(list.add(2), "add works 29");
  141. test.checkTrue(list.remove(-1), "remove does not work 1");
  142. test.checkFalse(list.remove(0), "remove works 1");
  143. test.checkTrue(list.remove(2), "remove does not work 2");
  144. test.checkEqual(3, list[0], "remove 1");
  145. test.checkEqual(2, list[1], "remove 2");
  146. test.checkEqual(2, list.getLength(), "remove 3");
  147. test.checkFalse(list.remove(1), "remove works 2");
  148. test.checkEqual(3, list[0], "remove 4");
  149. test.checkEqual(1, list.getLength(), "remove 5");
  150. test.checkFalse(list.remove(0), "remove works 3");
  151. test.checkEqual(0, list.getLength(), "remove 6");
  152. test.checkTrue(list.remove(0), "remove does not work 3");
  153. }
  154. static void testResize(Core::Test& test) {
  155. IntList list;
  156. test.checkFalse(list.resize(5, 10), "resize works");
  157. test.checkEqual(5, list.getLength(), "correct size after resize");
  158. for(int i = 0; i < 5; i++) {
  159. test.checkEqual(10, list[i], "correct content after resize");
  160. }
  161. }
  162. static void testDefaultResize(Core::Test& test) {
  163. IntList list;
  164. test.checkFalse(list.resize(5), "default resize works 1");
  165. test.checkEqual(5, list.getLength(), "correct size after default resize");
  166. for(int i = 0; i < 5; i++) {
  167. test.checkEqual(0, list[i], "correct content after default resize");
  168. }
  169. }
  170. void Core::ListTests::test() {
  171. Core::Test test("List");
  172. testAdd(test);
  173. testMultipleAdd(test);
  174. testAddReplace(test);
  175. testClear(test);
  176. testShrink(test);
  177. testBigAdd(test);
  178. testCopy(test);
  179. testMove(test);
  180. testMoveAssignment(test);
  181. testToString1(test);
  182. testToString2(test);
  183. testToString3(test);
  184. testRemoveBySwap(test);
  185. testRemove(test);
  186. testResize(test);
  187. testDefaultResize(test);
  188. test.finalize();
  189. }