LinkedListTests.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "tests/LinkedListTests.h"
  2. #include "data/LinkedList.h"
  3. #include "test/Test.h"
  4. struct LinkedListTester final {
  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. static void testWithoutCopyOrMove() {
  15. Core::LinkedList<LinkedListTester> list;
  16. CORE_TEST_ERROR(list.add(3));
  17. }
  18. static void testAdd() {
  19. IntList list;
  20. CORE_TEST_ERROR(list.add(5));
  21. auto iter = list.begin();
  22. if(CORE_TEST_TRUE(iter != list.end())) {
  23. CORE_TEST_EQUAL(5, *iter);
  24. }
  25. CORE_TEST_EQUAL(1, list.getLength());
  26. }
  27. static void testMultipleAdd() {
  28. IntList list;
  29. CORE_TEST_ERROR(list.add(4));
  30. CORE_TEST_ERROR(list.add(3));
  31. CORE_TEST_ERROR(list.add(2));
  32. auto iter = list.begin();
  33. CORE_TEST_EQUAL(4, *iter);
  34. CORE_TEST_EQUAL(3, *(++iter));
  35. CORE_TEST_EQUAL(2, *(++iter));
  36. CORE_TEST_EQUAL(3, list.getLength());
  37. }
  38. static void testClear() {
  39. IntList list;
  40. CORE_TEST_ERROR(list.add(5));
  41. CORE_TEST_ERROR(list.add(4));
  42. list.clear();
  43. CORE_TEST_EQUAL(0, list.getLength());
  44. CORE_TEST_FALSE(list.begin() != list.end());
  45. }
  46. static void testBigAdd() {
  47. IntList list;
  48. for(int i = 0; i < 100000; i++) {
  49. CORE_TEST_ERROR(list.add(i));
  50. }
  51. auto iter = list.begin();
  52. for(int i = 0; i < list.getLength(); i++) {
  53. CORE_TEST_EQUAL(i, *iter);
  54. ++iter;
  55. }
  56. CORE_TEST_EQUAL(100000, list.getLength());
  57. }
  58. static void testCopy() {
  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. CORE_TEST_ERROR(copy.copyFrom(list));
  65. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  66. auto iterA = list.begin();
  67. auto iterB = copy.begin();
  68. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  69. CORE_TEST_EQUAL(*iterA, *iterB);
  70. ++iterA;
  71. ++iterB;
  72. }
  73. }
  74. static void testMove() {
  75. IntList list;
  76. CORE_TEST_ERROR(list.add(1));
  77. CORE_TEST_ERROR(list.add(2));
  78. CORE_TEST_ERROR(list.add(3));
  79. const IntList move(Core::move(list));
  80. CORE_TEST_EQUAL(0, list.getLength());
  81. CORE_TEST_EQUAL(3, move.getLength());
  82. auto iter = move.begin();
  83. CORE_TEST_EQUAL(1, *iter);
  84. CORE_TEST_EQUAL(2, *(++iter));
  85. CORE_TEST_EQUAL(3, *(++iter));
  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. auto iter = move.begin();
  97. CORE_TEST_EQUAL(1, *iter);
  98. CORE_TEST_EQUAL(2, *(++iter));
  99. CORE_TEST_EQUAL(3, *(++iter));
  100. }
  101. static void testToString1() {
  102. IntList list;
  103. CORE_TEST_ERROR(list.add(1));
  104. CORE_TEST_ERROR(list.add(243));
  105. CORE_TEST_ERROR(list.add(-423));
  106. CORE_TEST_STRING("[1, 243, -423]", list);
  107. }
  108. static void testToString2() {
  109. IntList list;
  110. CORE_TEST_ERROR(list.add(1));
  111. CORE_TEST_STRING("[1]", list);
  112. }
  113. static void testToString3() {
  114. IntList list;
  115. CORE_TEST_STRING("[]", list);
  116. }
  117. static void testRemove() {
  118. IntList list;
  119. IntList::Node* a = nullptr;
  120. IntList::Node* b = nullptr;
  121. IntList::Node* c = nullptr;
  122. IntList::Node* d = nullptr;
  123. CORE_TEST_ERROR(list.put(a, 4));
  124. CORE_TEST_ERROR(list.put(b, 3));
  125. CORE_TEST_ERROR(list.put(c, 2));
  126. CORE_TEST_ERROR(list.put(d, 1));
  127. CORE_TEST_NOT_NULL(a);
  128. CORE_TEST_NOT_NULL(b);
  129. CORE_TEST_NOT_NULL(c);
  130. CORE_TEST_NOT_NULL(c);
  131. list.remove(b);
  132. auto iter = list.begin();
  133. CORE_TEST_EQUAL(4, *iter);
  134. CORE_TEST_EQUAL(2, *(++iter));
  135. CORE_TEST_EQUAL(1, *(++iter));
  136. CORE_TEST_EQUAL(3, list.getLength());
  137. list.remove(a);
  138. iter = list.begin();
  139. CORE_TEST_EQUAL(2, *iter);
  140. CORE_TEST_EQUAL(1, *(++iter));
  141. CORE_TEST_EQUAL(2, list.getLength());
  142. list.remove(d);
  143. iter = list.begin();
  144. CORE_TEST_EQUAL(2, *iter);
  145. CORE_TEST_EQUAL(1, list.getLength());
  146. list.remove(c);
  147. CORE_TEST_FALSE(list.begin() != list.end());
  148. CORE_TEST_EQUAL(0, list.getLength());
  149. CORE_TEST_NULL(a);
  150. CORE_TEST_NULL(b);
  151. CORE_TEST_NULL(c);
  152. CORE_TEST_NULL(c);
  153. }
  154. static void testRemoveFirst() {
  155. IntList list;
  156. CORE_TEST_ERROR(list.add(4));
  157. CORE_TEST_ERROR(list.add(3));
  158. CORE_TEST_ERROR(list.add(2));
  159. CORE_TEST_ERROR(list.add(1));
  160. list.removeFirst();
  161. auto iter = list.begin();
  162. if(CORE_TEST_TRUE(iter != list.end())) {
  163. CORE_TEST_EQUAL(3, *iter);
  164. ++iter;
  165. }
  166. if(CORE_TEST_TRUE(iter != list.end())) {
  167. CORE_TEST_EQUAL(2, *iter);
  168. ++iter;
  169. }
  170. if(CORE_TEST_TRUE(iter != list.end())) {
  171. CORE_TEST_EQUAL(1, *iter);
  172. }
  173. CORE_TEST_EQUAL(3, list.getLength());
  174. list.removeFirst();
  175. iter = list.begin();
  176. if(CORE_TEST_TRUE(iter != list.end())) {
  177. CORE_TEST_EQUAL(2, *iter);
  178. ++iter;
  179. }
  180. if(CORE_TEST_TRUE(iter != list.end())) {
  181. CORE_TEST_EQUAL(1, *iter);
  182. }
  183. CORE_TEST_EQUAL(2, list.getLength());
  184. list.removeFirst();
  185. iter = list.begin();
  186. if(CORE_TEST_TRUE(iter != list.end())) {
  187. CORE_TEST_EQUAL(1, *iter);
  188. }
  189. CORE_TEST_EQUAL(1, list.getLength());
  190. list.removeFirst();
  191. CORE_TEST_FALSE(list.begin() != list.end());
  192. CORE_TEST_EQUAL(0, list.getLength());
  193. list.removeFirst();
  194. CORE_TEST_FALSE(list.begin() != list.end());
  195. CORE_TEST_EQUAL(0, list.getLength());
  196. }
  197. static void testRemoveLast() {
  198. IntList list;
  199. CORE_TEST_ERROR(list.add(4));
  200. CORE_TEST_ERROR(list.add(3));
  201. CORE_TEST_ERROR(list.add(2));
  202. CORE_TEST_ERROR(list.add(1));
  203. list.removeLast();
  204. auto iter = list.begin();
  205. if(CORE_TEST_TRUE(iter != list.end())) {
  206. CORE_TEST_EQUAL(4, *iter);
  207. ++iter;
  208. }
  209. if(CORE_TEST_TRUE(iter != list.end())) {
  210. CORE_TEST_EQUAL(3, *iter);
  211. ++iter;
  212. }
  213. if(CORE_TEST_TRUE(iter != list.end())) {
  214. CORE_TEST_EQUAL(2, *iter);
  215. }
  216. CORE_TEST_EQUAL(3, list.getLength());
  217. list.removeLast();
  218. iter = list.begin();
  219. if(CORE_TEST_TRUE(iter != list.end())) {
  220. CORE_TEST_EQUAL(4, *iter);
  221. ++iter;
  222. }
  223. if(CORE_TEST_TRUE(iter != list.end())) {
  224. CORE_TEST_EQUAL(3, *iter);
  225. }
  226. CORE_TEST_EQUAL(2, list.getLength());
  227. list.removeLast();
  228. iter = list.begin();
  229. if(CORE_TEST_TRUE(iter != list.end())) {
  230. CORE_TEST_EQUAL(4, *iter);
  231. }
  232. CORE_TEST_EQUAL(1, list.getLength());
  233. list.removeLast();
  234. CORE_TEST_FALSE(list.begin() != list.end());
  235. CORE_TEST_EQUAL(0, list.getLength());
  236. list.removeLast();
  237. CORE_TEST_FALSE(list.begin() != list.end());
  238. CORE_TEST_EQUAL(0, list.getLength());
  239. }
  240. void Core::LinkedListTests::test() {
  241. testWithoutCopyOrMove();
  242. testAdd();
  243. testMultipleAdd();
  244. testClear();
  245. testBigAdd();
  246. testCopy();
  247. testMove();
  248. testMoveAssignment();
  249. testToString1();
  250. testToString2();
  251. testToString3();
  252. testRemove();
  253. testRemoveFirst();
  254. testRemoveLast();
  255. }