LinkedListTests.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "tests/LinkedListTests.h"
  2. #include "data/LinkedList.h"
  3. #include "test/Test.h"
  4. struct LinkedListTester {
  5. int a;
  6. LinkedListTester(int a_) : a(a_) {
  7. }
  8. LinkedListTester(const LinkedListTester&) = delete;
  9. LinkedListTester(LinkedListTester&&) = delete;
  10. LinkedListTester& operator=(const LinkedListTester&) = delete;
  11. LinkedListTester& operator=(LinkedListTester&&) = delete;
  12. };
  13. using IntList = Core::LinkedList<int>;
  14. using String = Core::ArrayString<128>;
  15. template<typename T>
  16. static String build(Core::Test& test, const T& t) {
  17. String s;
  18. test.checkFalse(s.append(t), "append works");
  19. return s;
  20. }
  21. static void testWithoutCopyOrMove(Core::Test& test) {
  22. Core::LinkedList<LinkedListTester> list;
  23. test.checkFalse(list.add(3) == nullptr, "without copy or move");
  24. }
  25. static void testAdd(Core::Test& test) {
  26. IntList list;
  27. test.checkFalse(list.add(5) == nullptr, "add works 1");
  28. auto iter = list.begin();
  29. test.checkEqual(5, *iter, "contains added value");
  30. test.checkEqual(1, list.getLength(), "sizes is increased by add");
  31. }
  32. static void testMultipleAdd(Core::Test& test) {
  33. IntList list;
  34. test.checkFalse(list.add(4) == nullptr, "add works 2");
  35. test.checkFalse(list.add(3) == nullptr, "add works 3");
  36. test.checkFalse(list.add(2) == nullptr, "add works 4");
  37. auto iter = list.begin();
  38. test.checkEqual(4, *iter, "contains added value 1");
  39. test.checkEqual(3, *(++iter), "contains added value 2");
  40. test.checkEqual(2, *(++iter), "contains added value 3");
  41. test.checkEqual(3, list.getLength(), "sizes is increased by add");
  42. }
  43. static void testClear(Core::Test& test) {
  44. IntList list;
  45. test.checkFalse(list.add(5) == nullptr, "add works 6");
  46. test.checkFalse(list.add(4) == nullptr, "add works 7");
  47. list.clear();
  48. test.checkEqual(0, list.getLength(), "length is 0 after clear");
  49. test.checkFalse(list.begin() != list.end(),
  50. "iterator is at end after clear");
  51. }
  52. static void testBigAdd(Core::Test& test) {
  53. IntList list;
  54. for(int i = 0; i < 1000000; i++) {
  55. test.checkFalse(list.add(i) == nullptr, "add works in big add");
  56. }
  57. auto iter = list.begin();
  58. for(int i = 0; i < list.getLength(); i++) {
  59. test.checkEqual(i, *iter, "big add");
  60. ++iter;
  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) == nullptr, "add works 11");
  67. test.checkFalse(list.add(2) == nullptr, "add works 12");
  68. test.checkFalse(list.add(3) == nullptr, "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. auto iterA = list.begin();
  73. auto iterB = copy.begin();
  74. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  75. test.checkEqual(*iterA, *iterB, "copy has same values");
  76. ++iterA;
  77. ++iterB;
  78. }
  79. }
  80. static void testMove(Core::Test& test) {
  81. IntList list;
  82. test.checkFalse(list.add(1) == nullptr, "add works 14");
  83. test.checkFalse(list.add(2) == nullptr, "add works 15");
  84. test.checkFalse(list.add(3) == nullptr, "add works 16");
  85. const IntList move(Core::move(list));
  86. test.checkEqual(0, list.getLength(), "moved has length 0");
  87. test.checkEqual(3, move.getLength(), "moved passes length");
  88. auto iter = move.begin();
  89. test.checkEqual(1, *iter, "moved passes values");
  90. test.checkEqual(2, *(++iter), "moved passes values");
  91. test.checkEqual(3, *(++iter), "moved passes values");
  92. }
  93. static void testMoveAssignment(Core::Test& test) {
  94. IntList list;
  95. test.checkFalse(list.add(1) == nullptr, "add works 17");
  96. test.checkFalse(list.add(2) == nullptr, "add works 18");
  97. test.checkFalse(list.add(3) == nullptr, "add works 19");
  98. IntList move;
  99. move = Core::move(list);
  100. test.checkEqual(0, list.getLength(), "assignment moved has length 0");
  101. test.checkEqual(3, move.getLength(), "assignment moved passes length");
  102. auto iter = move.begin();
  103. test.checkEqual(1, *iter, "assignment moved passes values");
  104. test.checkEqual(2, *(++iter), "assignment moved passes values");
  105. test.checkEqual(3, *(++iter), "assignment moved passes values");
  106. }
  107. static void testToString1(Core::Test& test) {
  108. IntList list;
  109. test.checkFalse(list.add(1) == nullptr, "add works 20");
  110. test.checkFalse(list.add(243) == nullptr, "add works 21");
  111. test.checkFalse(list.add(-423) == nullptr, "add works 22");
  112. test.checkEqual(build(test, "[1, 243, -423]"), build(test, list),
  113. "to string 1");
  114. }
  115. static void testToString2(Core::Test& test) {
  116. IntList list;
  117. test.checkFalse(list.add(1) == nullptr, "add works 23");
  118. test.checkEqual(build(test, "[1]"), build(test, list), "to string 2");
  119. }
  120. static void testToString3(Core::Test& test) {
  121. IntList list;
  122. test.checkEqual(build(test, "[]"), build(test, list), "to string 3");
  123. }
  124. static void testRemove(Core::Test& test) {
  125. IntList list;
  126. IntList::Node* a = list.add(4);
  127. IntList::Node* b = list.add(3);
  128. IntList::Node* c = list.add(2);
  129. IntList::Node* d = list.add(1);
  130. test.checkFalse(a == nullptr, "add works 24");
  131. test.checkFalse(b == nullptr, "add works 25");
  132. test.checkFalse(c == nullptr, "add works 26");
  133. test.checkFalse(c == nullptr, "add works 27");
  134. list.remove(b);
  135. auto iter = list.begin();
  136. test.checkEqual(4, *iter, "value after remove 1");
  137. test.checkEqual(2, *(++iter), "value after remove 2");
  138. test.checkEqual(1, *(++iter), "value after remove 3");
  139. test.checkEqual(3, list.getLength(), "size after remove 1");
  140. list.remove(a);
  141. iter = list.begin();
  142. test.checkEqual(2, *iter, "value after remove 4");
  143. test.checkEqual(1, *(++iter), "value after remove 5");
  144. test.checkEqual(2, list.getLength(), "size after remove 2");
  145. list.remove(d);
  146. iter = list.begin();
  147. test.checkEqual(2, *iter, "value after remove 6");
  148. test.checkEqual(1, list.getLength(), "size after remove 3");
  149. list.remove(c);
  150. test.checkFalse(list.begin() != list.end(), "empty iterator after remove");
  151. test.checkEqual(0, list.getLength(), "size after remove 4");
  152. test.checkTrue(a == nullptr, "nodes are null after remove 1");
  153. test.checkTrue(b == nullptr, "nodes are null after remove 2");
  154. test.checkTrue(c == nullptr, "nodes are null after remove 3");
  155. test.checkTrue(c == nullptr, "nodes are null after remove 4");
  156. }
  157. static void testRemoveFirst(Core::Test& test) {
  158. IntList list;
  159. test.checkFalse(list.add(4) == nullptr, "add works 28");
  160. test.checkFalse(list.add(3) == nullptr, "add works 29");
  161. test.checkFalse(list.add(2) == nullptr, "add works 30");
  162. test.checkFalse(list.add(1) == nullptr, "add works 31");
  163. list.removeFirst();
  164. auto iter = list.begin();
  165. test.checkEqual(3, *iter, "value after remove first 1");
  166. test.checkEqual(2, *(++iter), "value after remove first 2");
  167. test.checkEqual(1, *(++iter), "value after remove first 3");
  168. test.checkEqual(3, list.getLength(), "size after remove first 1");
  169. list.removeFirst();
  170. iter = list.begin();
  171. test.checkEqual(2, *iter, "value after remove first 4");
  172. test.checkEqual(1, *(++iter), "value after remove first 5");
  173. test.checkEqual(2, list.getLength(), "size after remove first 2");
  174. list.removeFirst();
  175. iter = list.begin();
  176. test.checkEqual(1, *iter, "value after remove first 6");
  177. test.checkEqual(1, list.getLength(), "size after remove first 3");
  178. list.removeFirst();
  179. test.checkFalse(list.begin() != list.end(),
  180. "empty iterator after remove first");
  181. test.checkEqual(0, list.getLength(), "size after remove first 4");
  182. list.removeFirst();
  183. test.checkFalse(list.begin() != list.end(),
  184. "empty iterator after remove first");
  185. test.checkEqual(0, list.getLength(), "size after remove first 5");
  186. }
  187. static void testRemoveLast(Core::Test& test) {
  188. IntList list;
  189. test.checkFalse(list.add(4) == nullptr, "add works 32");
  190. test.checkFalse(list.add(3) == nullptr, "add works 33");
  191. test.checkFalse(list.add(2) == nullptr, "add works 34");
  192. test.checkFalse(list.add(1) == nullptr, "add works 35");
  193. list.removeLast();
  194. auto iter = list.begin();
  195. test.checkEqual(4, *iter, "value after remove last 1");
  196. test.checkEqual(3, *(++iter), "value after remove last 2");
  197. test.checkEqual(2, *(++iter), "value after remove last 3");
  198. test.checkEqual(3, list.getLength(), "size after remove last 1");
  199. list.removeLast();
  200. iter = list.begin();
  201. test.checkEqual(4, *iter, "value after remove last 4");
  202. test.checkEqual(3, *(++iter), "value after remove last 5");
  203. test.checkEqual(2, list.getLength(), "size after remove last 2");
  204. list.removeLast();
  205. iter = list.begin();
  206. test.checkEqual(4, *iter, "value after remove last 6");
  207. test.checkEqual(1, list.getLength(), "size after remove last 3");
  208. list.removeLast();
  209. test.checkFalse(list.begin() != list.end(),
  210. "empty iterator after remove last");
  211. test.checkEqual(0, list.getLength(), "size after remove last 4");
  212. list.removeLast();
  213. test.checkFalse(list.begin() != list.end(),
  214. "empty iterator after remove last");
  215. test.checkEqual(0, list.getLength(), "size after remove 5");
  216. }
  217. void Core::LinkedListTests::test() {
  218. Core::Test test("LinkedList");
  219. testWithoutCopyOrMove(test);
  220. testAdd(test);
  221. testMultipleAdd(test);
  222. testClear(test);
  223. testBigAdd(test);
  224. testCopy(test);
  225. testMove(test);
  226. testMoveAssignment(test);
  227. testToString1(test);
  228. testToString2(test);
  229. testToString3(test);
  230. testRemove(test);
  231. testRemoveFirst(test);
  232. testRemoveLast(test);
  233. test.finalize();
  234. }