LinkedListTests.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/LinkedList.hpp"
  4. template class Core::LinkedList<int>;
  5. using IntList = Core::LinkedList<int>;
  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. CORE_TEST_ERROR(list.add(3));
  18. }
  19. static void testAdd() {
  20. IntList list;
  21. CORE_TEST_ERROR(list.add(5));
  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. CORE_TEST_ERROR(list.add(4));
  31. CORE_TEST_ERROR(list.add(3));
  32. CORE_TEST_ERROR(list.add(2));
  33. auto iter = list.begin();
  34. CORE_TEST_EQUAL(4, *iter);
  35. CORE_TEST_EQUAL(3, *(++iter));
  36. CORE_TEST_EQUAL(2, *(++iter));
  37. CORE_TEST_EQUAL(3, list.getLength());
  38. }
  39. static void testClear() {
  40. IntList list;
  41. CORE_TEST_ERROR(list.add(5));
  42. CORE_TEST_ERROR(list.add(4));
  43. list.clear();
  44. CORE_TEST_EQUAL(0, list.getLength());
  45. CORE_TEST_FALSE(list.begin() != list.end());
  46. }
  47. static void testBigAdd(bool light) {
  48. int limit = light ? 10000 : 100000;
  49. IntList list;
  50. for(int i = 0; i < limit; i++) {
  51. CORE_TEST_ERROR(list.add(i));
  52. }
  53. auto iter = list.begin();
  54. for(int i = 0; i < list.getLength(); i++) {
  55. CORE_TEST_EQUAL(i, *iter);
  56. ++iter;
  57. }
  58. CORE_TEST_EQUAL(limit, list.getLength());
  59. }
  60. static void testCopy() {
  61. IntList list;
  62. CORE_TEST_ERROR(list.add(1));
  63. CORE_TEST_ERROR(list.add(2));
  64. CORE_TEST_ERROR(list.add(3));
  65. IntList copy;
  66. CORE_TEST_ERROR(copy.copyFrom(list));
  67. CORE_TEST_EQUAL(list.getLength(), copy.getLength());
  68. auto iterA = list.begin();
  69. auto iterB = copy.begin();
  70. for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
  71. CORE_TEST_EQUAL(*iterA, *iterB);
  72. ++iterA;
  73. ++iterB;
  74. }
  75. }
  76. static void testMove() {
  77. IntList list;
  78. CORE_TEST_ERROR(list.add(1));
  79. CORE_TEST_ERROR(list.add(2));
  80. CORE_TEST_ERROR(list.add(3));
  81. const IntList move(Core::move(list));
  82. CORE_TEST_EQUAL(0, list.getLength());
  83. CORE_TEST_EQUAL(3, move.getLength());
  84. auto iter = move.begin();
  85. CORE_TEST_EQUAL(1, *iter);
  86. CORE_TEST_EQUAL(2, *(++iter));
  87. CORE_TEST_EQUAL(3, *(++iter));
  88. }
  89. static void testMoveAssignment() {
  90. IntList list;
  91. CORE_TEST_ERROR(list.add(1));
  92. CORE_TEST_ERROR(list.add(2));
  93. CORE_TEST_ERROR(list.add(3));
  94. IntList move;
  95. move = Core::move(list);
  96. CORE_TEST_EQUAL(0, list.getLength());
  97. CORE_TEST_EQUAL(3, move.getLength());
  98. auto iter = move.begin();
  99. CORE_TEST_EQUAL(1, *iter);
  100. CORE_TEST_EQUAL(2, *(++iter));
  101. CORE_TEST_EQUAL(3, *(++iter));
  102. }
  103. static void testToString1() {
  104. IntList list;
  105. CORE_TEST_ERROR(list.add(1));
  106. CORE_TEST_ERROR(list.add(243));
  107. CORE_TEST_ERROR(list.add(-423));
  108. CORE_TEST_STRING("[1, 243, -423]", list);
  109. }
  110. static void testToString2() {
  111. IntList list;
  112. CORE_TEST_ERROR(list.add(1));
  113. CORE_TEST_STRING("[1]", list);
  114. }
  115. static void testToString3() {
  116. IntList list;
  117. CORE_TEST_STRING("[]", list);
  118. }
  119. static void testRemove() {
  120. IntList list;
  121. IntList::Node* a = nullptr;
  122. IntList::Node* b = nullptr;
  123. IntList::Node* c = nullptr;
  124. IntList::Node* d = nullptr;
  125. CORE_TEST_ERROR(list.put(a, 4));
  126. CORE_TEST_ERROR(list.put(b, 3));
  127. CORE_TEST_ERROR(list.put(c, 2));
  128. CORE_TEST_ERROR(list.put(d, 1));
  129. CORE_TEST_NOT_NULL(a);
  130. CORE_TEST_NOT_NULL(b);
  131. CORE_TEST_NOT_NULL(c);
  132. CORE_TEST_NOT_NULL(c);
  133. list.remove(b);
  134. auto iter = list.begin();
  135. CORE_TEST_EQUAL(4, *iter);
  136. CORE_TEST_EQUAL(2, *(++iter));
  137. CORE_TEST_EQUAL(1, *(++iter));
  138. CORE_TEST_EQUAL(3, list.getLength());
  139. list.remove(a);
  140. iter = list.begin();
  141. CORE_TEST_EQUAL(2, *iter);
  142. CORE_TEST_EQUAL(1, *(++iter));
  143. CORE_TEST_EQUAL(2, list.getLength());
  144. list.remove(d);
  145. iter = list.begin();
  146. CORE_TEST_EQUAL(2, *iter);
  147. CORE_TEST_EQUAL(1, list.getLength());
  148. list.remove(c);
  149. CORE_TEST_FALSE(list.begin() != list.end());
  150. CORE_TEST_EQUAL(0, list.getLength());
  151. CORE_TEST_NULL(a);
  152. CORE_TEST_NULL(b);
  153. CORE_TEST_NULL(c);
  154. CORE_TEST_NULL(c);
  155. }
  156. static void testRemoveFirst() {
  157. IntList list;
  158. CORE_TEST_ERROR(list.add(4));
  159. CORE_TEST_ERROR(list.add(3));
  160. CORE_TEST_ERROR(list.add(2));
  161. CORE_TEST_ERROR(list.add(1));
  162. list.removeFirst();
  163. auto iter = list.begin();
  164. if(CORE_TEST_TRUE(iter != list.end())) {
  165. CORE_TEST_EQUAL(3, *iter);
  166. ++iter;
  167. }
  168. if(CORE_TEST_TRUE(iter != list.end())) {
  169. CORE_TEST_EQUAL(2, *iter);
  170. ++iter;
  171. }
  172. if(CORE_TEST_TRUE(iter != list.end())) {
  173. CORE_TEST_EQUAL(1, *iter);
  174. }
  175. CORE_TEST_EQUAL(3, list.getLength());
  176. list.removeFirst();
  177. iter = list.begin();
  178. if(CORE_TEST_TRUE(iter != list.end())) {
  179. CORE_TEST_EQUAL(2, *iter);
  180. ++iter;
  181. }
  182. if(CORE_TEST_TRUE(iter != list.end())) {
  183. CORE_TEST_EQUAL(1, *iter);
  184. }
  185. CORE_TEST_EQUAL(2, list.getLength());
  186. list.removeFirst();
  187. iter = list.begin();
  188. if(CORE_TEST_TRUE(iter != list.end())) {
  189. CORE_TEST_EQUAL(1, *iter);
  190. }
  191. CORE_TEST_EQUAL(1, list.getLength());
  192. list.removeFirst();
  193. CORE_TEST_FALSE(list.begin() != list.end());
  194. CORE_TEST_EQUAL(0, list.getLength());
  195. list.removeFirst();
  196. CORE_TEST_FALSE(list.begin() != list.end());
  197. CORE_TEST_EQUAL(0, list.getLength());
  198. }
  199. static void testRemoveLast() {
  200. IntList list;
  201. CORE_TEST_ERROR(list.add(4));
  202. CORE_TEST_ERROR(list.add(3));
  203. CORE_TEST_ERROR(list.add(2));
  204. CORE_TEST_ERROR(list.add(1));
  205. list.removeLast();
  206. auto iter = list.begin();
  207. if(CORE_TEST_TRUE(iter != list.end())) {
  208. CORE_TEST_EQUAL(4, *iter);
  209. ++iter;
  210. }
  211. if(CORE_TEST_TRUE(iter != list.end())) {
  212. CORE_TEST_EQUAL(3, *iter);
  213. ++iter;
  214. }
  215. if(CORE_TEST_TRUE(iter != list.end())) {
  216. CORE_TEST_EQUAL(2, *iter);
  217. }
  218. CORE_TEST_EQUAL(3, list.getLength());
  219. list.removeLast();
  220. iter = list.begin();
  221. if(CORE_TEST_TRUE(iter != list.end())) {
  222. CORE_TEST_EQUAL(4, *iter);
  223. ++iter;
  224. }
  225. if(CORE_TEST_TRUE(iter != list.end())) {
  226. CORE_TEST_EQUAL(3, *iter);
  227. }
  228. CORE_TEST_EQUAL(2, list.getLength());
  229. list.removeLast();
  230. iter = list.begin();
  231. if(CORE_TEST_TRUE(iter != list.end())) {
  232. CORE_TEST_EQUAL(4, *iter);
  233. }
  234. CORE_TEST_EQUAL(1, list.getLength());
  235. list.removeLast();
  236. CORE_TEST_FALSE(list.begin() != list.end());
  237. CORE_TEST_EQUAL(0, list.getLength());
  238. list.removeLast();
  239. CORE_TEST_FALSE(list.begin() != list.end());
  240. CORE_TEST_EQUAL(0, list.getLength());
  241. }
  242. static void testOutOfMemory() {
  243. #ifdef ERROR_SIMULATOR
  244. IntList list;
  245. int memFails = 0;
  246. for(int i = 0; i < 40; i++) {
  247. Core::Fail::leftAllocations = i;
  248. Core::Error e = list.add(1);
  249. if(e == Core::Error::OUT_OF_MEMORY) {
  250. memFails++;
  251. }
  252. }
  253. if(CORE_TEST_TRUE(list.getLength() > 0)) {
  254. CORE_TEST_EQUAL(1, *list.begin());
  255. }
  256. Core::Fail::leftAllocations = -1;
  257. CORE_TEST_TRUE(memFails != 0);
  258. #endif
  259. }
  260. void Core::testLinkedList(bool light, bool outOfMemoryTest) {
  261. testWithoutCopyOrMove();
  262. testAdd();
  263. testMultipleAdd();
  264. testClear();
  265. testBigAdd(light);
  266. testCopy();
  267. testMove();
  268. testMoveAssignment();
  269. testToString1();
  270. testToString2();
  271. testToString3();
  272. testRemove();
  273. testRemoveFirst();
  274. testRemoveLast();
  275. if(outOfMemoryTest) {
  276. testOutOfMemory();
  277. }
  278. }