LinkedListTests.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/LinkedList.hpp"
  4. template struct Core::LinkedList<size_t>;
  5. using IntList = Core::LinkedList<size_t>;
  6. struct LinkedListTester final {
  7. int a;
  8. LinkedListTester(int a_) : a(a_) {
  9. }
  10. LinkedListTester(const LinkedListTester&) = delete;
  11. LinkedListTester(LinkedListTester&&) = delete;
  12. LinkedListTester& operator=(const LinkedListTester&) = delete;
  13. LinkedListTester& operator=(LinkedListTester&&) = delete;
  14. };
  15. static void testWithoutCopyOrMove() {
  16. Core::LinkedList<LinkedListTester> list;
  17. list.add(3);
  18. }
  19. static void testAdd() {
  20. IntList list;
  21. list.add(5u);
  22. auto iter = list.begin();
  23. if(CORE_TEST_TRUE(iter != list.end())) {
  24. CORE_TEST_EQUAL(5, *iter);
  25. }
  26. CORE_TEST_EQUAL(1, list.getLength());
  27. }
  28. static void testMultipleAdd() {
  29. IntList list;
  30. list.add(4u).add(3u).add(2u);
  31. auto iter = list.begin();
  32. CORE_TEST_EQUAL(4, *iter);
  33. CORE_TEST_EQUAL(3, *(++iter));
  34. CORE_TEST_EQUAL(2, *(++iter));
  35. CORE_TEST_EQUAL(3, list.getLength());
  36. }
  37. static void testClear() {
  38. IntList list;
  39. list.add(5u).add(4u);
  40. list.clear();
  41. CORE_TEST_EQUAL(0, list.getLength());
  42. CORE_TEST_FALSE(list.begin() != list.end());
  43. }
  44. static void testBigAdd(bool light) {
  45. size_t limit = light ? 10000 : 100000;
  46. IntList list;
  47. for(size_t i = 0; i < limit; i++) {
  48. list.add(i);
  49. }
  50. auto iter = list.begin();
  51. for(size_t i = 0; i < list.getLength(); i++) {
  52. CORE_TEST_EQUAL(i, *iter);
  53. ++iter;
  54. }
  55. CORE_TEST_EQUAL(limit, list.getLength());
  56. }
  57. static void testCopy() {
  58. IntList list;
  59. list.add(1u).add(2u).add(3u);
  60. IntList copy = list;
  61. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  62. auto iterA = list.begin();
  63. auto iterB = copy.begin();
  64. for(size_t i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  65. CORE_TEST_EQUAL(*iterA, *iterB);
  66. ++iterA;
  67. ++iterB;
  68. }
  69. }
  70. static void testMove() {
  71. IntList list;
  72. list.add(1u).add(2u).add(3u);
  73. const IntList move(Core::move(list));
  74. CORE_TEST_EQUAL(0, list.getLength());
  75. CORE_TEST_EQUAL(3, move.getLength());
  76. auto iter = move.begin();
  77. CORE_TEST_EQUAL(1, *iter);
  78. CORE_TEST_EQUAL(2, *(++iter));
  79. CORE_TEST_EQUAL(3, *(++iter));
  80. }
  81. static void testMoveAssignment() {
  82. IntList list;
  83. list.add(1u).add(2u).add(3u);
  84. IntList move;
  85. move = Core::move(list);
  86. CORE_TEST_EQUAL(0, list.getLength());
  87. CORE_TEST_EQUAL(3, move.getLength());
  88. auto iter = move.begin();
  89. CORE_TEST_EQUAL(1, *iter);
  90. CORE_TEST_EQUAL(2, *(++iter));
  91. CORE_TEST_EQUAL(3, *(++iter));
  92. }
  93. static void testToString1() {
  94. IntList list;
  95. list.add(1u).add(243u).add(423u);
  96. CORE_TEST_STRING("[1, 243, 423]", list);
  97. }
  98. static void testToString2() {
  99. IntList list;
  100. list.add(1u);
  101. CORE_TEST_STRING("[1]", list);
  102. }
  103. static void testToString3() {
  104. IntList list;
  105. CORE_TEST_STRING("[]", list);
  106. }
  107. static void testRemove() {
  108. IntList list;
  109. IntList::Node* a = list.put(4u);
  110. IntList::Node* b = list.put(3u);
  111. IntList::Node* c = list.put(2u);
  112. IntList::Node* d = list.put(1u);
  113. CORE_TEST_NOT_NULL(a);
  114. CORE_TEST_NOT_NULL(b);
  115. CORE_TEST_NOT_NULL(c);
  116. CORE_TEST_NOT_NULL(c);
  117. list.remove(b);
  118. auto iter = list.begin();
  119. CORE_TEST_EQUAL(4, *iter);
  120. CORE_TEST_EQUAL(2, *(++iter));
  121. CORE_TEST_EQUAL(1, *(++iter));
  122. CORE_TEST_EQUAL(3, list.getLength());
  123. list.remove(a);
  124. iter = list.begin();
  125. CORE_TEST_EQUAL(2, *iter);
  126. CORE_TEST_EQUAL(1, *(++iter));
  127. CORE_TEST_EQUAL(2, list.getLength());
  128. list.remove(d);
  129. iter = list.begin();
  130. CORE_TEST_EQUAL(2, *iter);
  131. CORE_TEST_EQUAL(1, list.getLength());
  132. list.remove(c);
  133. CORE_TEST_FALSE(list.begin() != list.end());
  134. CORE_TEST_EQUAL(0, list.getLength());
  135. CORE_TEST_NULL(a);
  136. CORE_TEST_NULL(b);
  137. CORE_TEST_NULL(c);
  138. CORE_TEST_NULL(c);
  139. }
  140. static void testRemoveFirst() {
  141. IntList list;
  142. list.add(4u).add(3u).add(2u).add(1u);
  143. list.removeFirst();
  144. auto iter = list.begin();
  145. if(CORE_TEST_TRUE(iter != list.end())) {
  146. CORE_TEST_EQUAL(3, *iter);
  147. ++iter;
  148. }
  149. if(CORE_TEST_TRUE(iter != list.end())) {
  150. CORE_TEST_EQUAL(2, *iter);
  151. ++iter;
  152. }
  153. if(CORE_TEST_TRUE(iter != list.end())) {
  154. CORE_TEST_EQUAL(1, *iter);
  155. }
  156. CORE_TEST_EQUAL(3, list.getLength());
  157. list.removeFirst();
  158. iter = list.begin();
  159. if(CORE_TEST_TRUE(iter != list.end())) {
  160. CORE_TEST_EQUAL(2, *iter);
  161. ++iter;
  162. }
  163. if(CORE_TEST_TRUE(iter != list.end())) {
  164. CORE_TEST_EQUAL(1, *iter);
  165. }
  166. CORE_TEST_EQUAL(2, list.getLength());
  167. list.removeFirst();
  168. iter = list.begin();
  169. if(CORE_TEST_TRUE(iter != list.end())) {
  170. CORE_TEST_EQUAL(1, *iter);
  171. }
  172. CORE_TEST_EQUAL(1, list.getLength());
  173. list.removeFirst();
  174. CORE_TEST_FALSE(list.begin() != list.end());
  175. CORE_TEST_EQUAL(0, list.getLength());
  176. list.removeFirst();
  177. CORE_TEST_FALSE(list.begin() != list.end());
  178. CORE_TEST_EQUAL(0, list.getLength());
  179. }
  180. static void testRemoveLast() {
  181. IntList list;
  182. list.add(4u).add(3u).add(2u).add(1u);
  183. list.removeLast();
  184. auto iter = list.begin();
  185. if(CORE_TEST_TRUE(iter != list.end())) {
  186. CORE_TEST_EQUAL(4, *iter);
  187. ++iter;
  188. }
  189. if(CORE_TEST_TRUE(iter != list.end())) {
  190. CORE_TEST_EQUAL(3, *iter);
  191. ++iter;
  192. }
  193. if(CORE_TEST_TRUE(iter != list.end())) {
  194. CORE_TEST_EQUAL(2, *iter);
  195. }
  196. CORE_TEST_EQUAL(3, list.getLength());
  197. list.removeLast();
  198. iter = list.begin();
  199. if(CORE_TEST_TRUE(iter != list.end())) {
  200. CORE_TEST_EQUAL(4, *iter);
  201. ++iter;
  202. }
  203. if(CORE_TEST_TRUE(iter != list.end())) {
  204. CORE_TEST_EQUAL(3, *iter);
  205. }
  206. CORE_TEST_EQUAL(2, list.getLength());
  207. list.removeLast();
  208. iter = list.begin();
  209. if(CORE_TEST_TRUE(iter != list.end())) {
  210. CORE_TEST_EQUAL(4, *iter);
  211. }
  212. CORE_TEST_EQUAL(1, list.getLength());
  213. list.removeLast();
  214. CORE_TEST_FALSE(list.begin() != list.end());
  215. CORE_TEST_EQUAL(0, list.getLength());
  216. list.removeLast();
  217. CORE_TEST_FALSE(list.begin() != list.end());
  218. CORE_TEST_EQUAL(0, list.getLength());
  219. }
  220. void Core::testLinkedList(bool light) {
  221. testWithoutCopyOrMove();
  222. testAdd();
  223. testMultipleAdd();
  224. testClear();
  225. testBigAdd(light);
  226. testCopy();
  227. testMove();
  228. testMoveAssignment();
  229. testToString1();
  230. testToString2();
  231. testToString3();
  232. testRemove();
  233. testRemoveFirst();
  234. testRemoveLast();
  235. }