test_dlist.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #include <catch2/catch_test_macros.hpp>
  2. #include <mcl/containers/dlist.hpp>
  3. #pragma region constructors
  4. TEST_CASE("Default constructor", "[DList][constructor][default]") {
  5. mcl::DList<int> lst;
  6. REQUIRE(lst.get_number() == 0);
  7. }
  8. TEST_CASE("Allocator constructor", "[DList][constructor][allocator]") {
  9. SECTION("lvalue") {
  10. mcl::mem::Allocator<int> alloc;
  11. mcl::DList<int> lst(alloc);
  12. REQUIRE(lst.get_number() == 0);
  13. }
  14. SECTION("rvalue") {
  15. mcl::mem::Allocator<int> alloc;
  16. mcl::DList<int> lst(mcl::value::rvalue_cast(alloc));
  17. REQUIRE(lst.get_number() == 0);
  18. }
  19. }
  20. TEST_CASE("Initializer list constructor", "[DList][constructor][initializer_list]") {
  21. mcl::DList<int> lst{1,2,3,4,5};
  22. REQUIRE(lst.get_number() == 5);
  23. }
  24. TEST_CASE("Functor filler constructor", "[DList][constructor][fill][function][generator]") {
  25. mcl::DList<int> lst(3, [](){ return 1; });
  26. REQUIRE(lst.get_number() == 3);
  27. }
  28. TEST_CASE("Functor filler constructor with start & generator", "[DList][constructor][fill][function][generator][start]") {
  29. mcl::DList<int> lst(10, 0, [](const int& p){ return p + 2; });
  30. int prev = -2;
  31. for (int x : lst) {
  32. REQUIRE(x-2 == prev);
  33. prev = x;
  34. }
  35. }
  36. TEST_CASE("Fill from args... constructor", "[DList][constructor][fill][args][variadic][template]") {
  37. SECTION("With args") {
  38. mcl::DList<int> lst(4, 5);
  39. REQUIRE(lst.get_number() == 4);
  40. }
  41. SECTION("Without args") {
  42. mcl::DList<int> lst(6);
  43. REQUIRE(lst.get_number() == 6);
  44. }
  45. }
  46. TEST_CASE("Copy constructor", "[DList][constructor][copy][assignment]") {
  47. mcl::DList<int> a(3, 4);
  48. mcl::DList<int> b(a);
  49. REQUIRE(a.get_number() == 3);
  50. REQUIRE(b.get_number() == 3);
  51. REQUIRE(b.get_number() == a.get_number());
  52. }
  53. TEST_CASE("Move constructor", "[DList][constructor][move][assignment]") {
  54. mcl::DList<int> a(3, 4);
  55. mcl::DList<int> b(std::move(a));
  56. REQUIRE(a.get_number() == 0);
  57. REQUIRE(b.get_number() == 3);
  58. REQUIRE(b.get_number() != a.get_number());
  59. }
  60. TEST_CASE("Iterators assignment constructor", "[DList][constructor][iterators][assignment]") {
  61. SECTION("from iters") {
  62. mcl::DList<int> l1{3,2,1};
  63. mcl::DList<int> l2(l1.begin(), l1.end());
  64. REQUIRE(l1.get_number() == 3);
  65. REQUIRE(l2.get_number() == 3);
  66. }
  67. SECTION("from pointers") {
  68. int arr[] = {1,2,3,4,5};
  69. mcl::DList<int> lst(arr, arr+5);
  70. REQUIRE(lst.get_number() == 5);
  71. }
  72. }
  73. #pragma endregion
  74. #pragma region assignment operators
  75. TEST_CASE("Move assignment operator", "[DList][operator][assignment][move]") {
  76. mcl::DList<int> l1{1,2,3,4,5};
  77. mcl::DList<int> l2;
  78. REQUIRE(l1.get_number() == 5);
  79. REQUIRE(l2.get_number() == 0);
  80. l2 = std::move(l1);
  81. REQUIRE(l1.get_number() == 0);
  82. REQUIRE(l2.get_number() == 5);
  83. }
  84. TEST_CASE("Copy assignment operator", "[DList][operator][assignment][copy]") {
  85. mcl::DList<int> l1{1,2,3,4,5};
  86. mcl::DList<int> l2;
  87. REQUIRE(l1.get_number() == 5);
  88. REQUIRE(l2.get_number() == 0);
  89. l2 = l1;
  90. REQUIRE(l1.get_number() == 5);
  91. REQUIRE(l2.get_number() == 5);
  92. }
  93. TEST_CASE("Copy extend assignment operator", "[DList][operator][assignment][copy][extend]") {
  94. mcl::DList<int> l1{1,2,3,4,5};
  95. mcl::DList<int> l2{6,7,8,9,0};
  96. REQUIRE(l1.get_number() == 5);
  97. REQUIRE(l2.get_number() == 5);
  98. l2 += l1;
  99. REQUIRE(l1.get_number() == 5);
  100. REQUIRE(l2.get_number() == 10);
  101. }
  102. TEST_CASE("Move extend assignment operator", "[DList][operator][assignment][move][extend]") {
  103. mcl::DList<int> l1{1,2,3,4,5};
  104. mcl::DList<int> l2{6,7,8,9,0};
  105. REQUIRE(l1.get_number() == 5);
  106. REQUIRE(l2.get_number() == 5);
  107. l2 += std::move(l1);
  108. REQUIRE(l1.get_number() == 0);
  109. REQUIRE(l2.get_number() == 10);
  110. }
  111. TEST_CASE("Initializer list assignment operator", "[DList][operator][assignment][initializer_list]") {
  112. mcl::DList<int> lst{1,2,3};
  113. REQUIRE(lst.get_number() == 3);
  114. lst = {5,4,3,2,1};
  115. REQUIRE(lst.get_number() == 5);
  116. }
  117. TEST_CASE("Initializer list extend operator", "[DList][operator][assignment][initializer_list][extend]") {
  118. mcl::DList<int> lst{1,2,3,4,5};
  119. REQUIRE(lst.get_number() == 5);
  120. lst += {6,7,8,9,0};
  121. REQUIRE(lst.get_number() == 10);
  122. }
  123. #pragma endregion
  124. #pragma region comparative operators
  125. TEST_CASE("Equality operator", "[DList][operator][comparative][equality]") {
  126. mcl::DList<int> l1{1,2,3};
  127. mcl::DList<int> l2(l1);
  128. mcl::DList<int> l3{3,4};
  129. mcl::DList<int> l4(3, 0);
  130. REQUIRE(l1 == l2);
  131. REQUIRE(l1 != l3);
  132. REQUIRE(!(l1 == l4));
  133. }
  134. #pragma endregion
  135. #pragma region assignment methods
  136. TEST_CASE("Copy assignment method", "[DList][method][assign][copy]") {
  137. mcl::DList<int> l1;
  138. mcl::DList<int> l2{1,2,3};
  139. REQUIRE(l1.get_number() == 0);
  140. REQUIRE(l2.get_number() == 3);
  141. l1.assign(l2);
  142. REQUIRE(l1.get_number() == 3);
  143. REQUIRE(l2.get_number() == 3);
  144. }
  145. TEST_CASE("Move assignment method", "[DList][method][assign][move]") {
  146. mcl::DList<int> l1;
  147. mcl::DList<int> l2{1,2,3};
  148. REQUIRE(l1.get_number() == 0);
  149. REQUIRE(l2.get_number() == 3);
  150. l1.assign(std::move(l2));
  151. REQUIRE(l1.get_number() == 3);
  152. REQUIRE(l2.get_number() == 0);
  153. }
  154. TEST_CASE("Iterators assign method", "[DList][method][assign][iterators]") {
  155. mcl::DList<int> l1{3,2,1};
  156. mcl::DList<int> l2;
  157. l2.assign(l1.begin(), l1.end());
  158. REQUIRE(l1.get_number() == 3);
  159. REQUIRE(l2.get_number() == 3);
  160. }
  161. TEST_CASE("Copy extend method", "[DList][method][extend][copy]") {
  162. mcl::DList<int> l1{1,2,3,4,5};
  163. mcl::DList<int> l2{6,7,8,9};
  164. l1.extend(l2);
  165. REQUIRE(l1.get_number() == 9);
  166. REQUIRE(l2.get_number() == 4);
  167. }
  168. TEST_CASE("Move extend method", "[DList][method][extend][move]") {
  169. mcl::DList<int> l1{1,2,3,4,5};
  170. mcl::DList<int> l2{6,7,8,9};
  171. l1.extend(std::move(l2));
  172. REQUIRE(l1.get_number() == 9);
  173. REQUIRE(l2.get_number() == 0);
  174. }
  175. TEST_CASE("Functor refiller method", "[DList][method][refill][functor]") {
  176. mcl::DList<int> lst;
  177. lst.refill(3, [](){ return 1; });
  178. REQUIRE(lst.get_number() == 3);
  179. }
  180. TEST_CASE("Functor refiller func with start & generator", "[DList][method][refill][start][generator]") {
  181. mcl::DList<int> lst;
  182. lst.refill(10, 0, [](const int& p){ return p + 2; });
  183. int prev = -2;
  184. for (int x : lst) {
  185. REQUIRE(x-2 == prev);
  186. prev = x;
  187. }
  188. }
  189. TEST_CASE("Refill from args... method", "[DList][method][args][variadic]") {
  190. mcl::DList<int> lst;
  191. lst.refill(4, 5);
  192. REQUIRE(lst.get_number() == 4);
  193. }
  194. #pragma endregion
  195. #pragma region getters
  196. TEST_CASE("Get first & last method", "[DList][method][getter][first][last]") {
  197. mcl::DList<int> lst{1,2,3,4,5};
  198. REQUIRE(lst.get_first() == 1);
  199. REQUIRE(lst.get_last() == 5);
  200. }
  201. TEST_CASE("Get number & empty check", "[DList][method][getter][number][empty][to_emplry][get_number]") {
  202. mcl::DList<int> lst{1,2,3};
  203. REQUIRE(lst.get_number() == 3);
  204. REQUIRE(!lst.is_empty());
  205. lst.to_empty();
  206. REQUIRE(lst.get_number() == 0);
  207. REQUIRE(lst.is_empty());
  208. }
  209. TEST_CASE("Find item", "[DList][method][getter][find][item][Iterator]") {
  210. mcl::DList<int> lst{2,5,3,1,5};
  211. const int i = 5;
  212. SECTION("Filled list") {
  213. REQUIRE(lst.find(i) != lst.end());
  214. REQUIRE(lst.find(0) == lst.end());
  215. }
  216. lst.to_empty();
  217. SECTION("Empty list") {
  218. REQUIRE(lst.find(i) == lst.end());
  219. REQUIRE(lst.find(0) == lst.end());
  220. }
  221. }
  222. TEST_CASE("Find item RIterator", "[DList][method][getter][find][rfind][RIterator][reverse]") {
  223. mcl::DList<int> lst{2,5,3,1,5};
  224. const int i = 5;
  225. SECTION("Filled list") {
  226. REQUIRE(lst.rfind(i) != lst.rend());
  227. REQUIRE(lst.rfind(0) == lst.rend());
  228. }
  229. lst.to_empty();
  230. SECTION("Empty list") {
  231. REQUIRE(lst.rfind(i) == lst.rend());
  232. REQUIRE(lst.rfind(0) == lst.rend());
  233. }
  234. }
  235. TEST_CASE("Contains", "[DList][method][check][containes]") {
  236. mcl::DList<int> lst{4,6,32,5,1};
  237. REQUIRE(lst.contains(5));
  238. REQUIRE(!lst.contains(0));
  239. }
  240. #pragma endregion
  241. #pragma region iterators
  242. TEST_CASE("Iterator", "[DList][iterators][Iterator]") {
  243. SECTION("Reqular") {
  244. mcl::DList<int> lst{5,2,6,7,5};
  245. for (auto it=lst.begin(), end=lst.end(); it!=end; ++it)
  246. REQUIRE(*it);
  247. }
  248. SECTION("Const") {
  249. const mcl::DList<int> lst{5,2,6,7,5};
  250. for (auto it=lst.begin(), end=lst.end(); it!=end; ++it)
  251. REQUIRE(*it);
  252. }
  253. }
  254. TEST_CASE("RIterator", "[DList][iterators][RIterator][ireverse]") {
  255. SECTION("Regular") {
  256. mcl::DList<int> lst{5,2,6,7,5};
  257. for (auto it=lst.rbegin(), end=lst.rend(); it!=end; ++it)
  258. REQUIRE(*it);
  259. }
  260. SECTION("Const") {
  261. const mcl::DList<int> lst{5,2,6,7,5};
  262. for (auto it=lst.crbegin(), end=lst.crend(); it!=end; ++it)
  263. REQUIRE(*it);
  264. }
  265. }
  266. #pragma endregion
  267. #pragma region insert
  268. TEST_CASE("Insert with Iterator", "[DList][method][insert][Iterator][iterators]") {
  269. mcl::DList<int> lst{4,2,7,1,8};
  270. REQUIRE(lst.get_number() == 5);
  271. auto it = lst.find(7);
  272. lst.insert(it, 6);
  273. REQUIRE(lst.get_number() == 6);
  274. REQUIRE_THROWS_AS(lst.insert(lst.end(), 5), std::invalid_argument);
  275. }
  276. TEST_CASE("Insert with RIterator", "[DList][method][insert][RIterator][iterators]") {
  277. mcl::DList<int> lst{4,2,7,1,8};
  278. REQUIRE(lst.get_number() == 5);
  279. auto it = ++lst.rbegin();
  280. lst.insert(it, 6);
  281. REQUIRE(lst.get_number() == 6);
  282. REQUIRE_THROWS_AS(lst.insert(lst.rend(), 5), std::invalid_argument);
  283. }
  284. TEST_CASE("Add back", "[DList][method][insert][add][back]") {
  285. mcl::DList<int> lst{5,6,8,3};
  286. REQUIRE(lst.get_number() == 4);
  287. lst.add_back(5,2,7,1);
  288. REQUIRE(lst.get_number() == 8);
  289. }
  290. TEST_CASE("Add front", "[DList][method][insert][add][front]") {
  291. mcl::DList<int> lst{5,0,1,3};
  292. REQUIRE(lst.get_number() == 4);
  293. lst.add_front(1,7,5,1);
  294. REQUIRE(lst.get_number() == 8);
  295. }
  296. #pragma endregion
  297. #pragma region create
  298. TEST_CASE("Create back", "[DList][method][insert][create][back]") {
  299. mcl::DList<int> lst;
  300. REQUIRE(lst.get_number() == 0);
  301. lst.create_back(5);
  302. lst.create_back(4);
  303. REQUIRE(lst.get_number() == 2);
  304. }
  305. TEST_CASE("Create front", "[DList][method][insert][create][front]") {
  306. mcl::DList<int> lst;
  307. REQUIRE(lst.get_number() == 0);
  308. lst.create_front(5);
  309. lst.create_front(4);
  310. REQUIRE(lst.get_number() == 2);
  311. }
  312. TEST_CASE("Create with Iterator", "[DList][method][insert][create][Iterator][iterators]") {
  313. mcl::DList<int> lst{1,2,3,4,5};
  314. lst.create(lst.find(3), 0);
  315. REQUIRE(lst.get_number() == 6);
  316. REQUIRE_THROWS(lst.create(lst.end(), 3));
  317. }
  318. TEST_CASE("Create with RIterator", "[DList][method][insert][create][RIterator][iterators]") {
  319. mcl::DList<int> lst{1,2,3,4,5};
  320. lst.create(lst.rbegin(), 0);
  321. REQUIRE(lst.get_number() == 6);
  322. REQUIRE_THROWS(lst.create(lst.rend(), 3));
  323. }
  324. #pragma endregion
  325. #pragma region replace
  326. TEST_CASE("Replace with iter", "[DList][method][replace][Iterator][iterators]") {
  327. mcl::DList<int> lst{1,2,3,4,5};
  328. auto it = lst.find(3);
  329. REQUIRE(*it == 3);
  330. lst.replace(it, 0);
  331. REQUIRE(*it == 0);
  332. }
  333. TEST_CASE("Replace first item", "[DList][method][replace][first]") {
  334. mcl::DList<int> lst{1,2,3,4,5};
  335. REQUIRE(lst.get_first() == 1);
  336. lst.replace_first(0);
  337. REQUIRE(lst.get_first() == 0);
  338. }
  339. TEST_CASE("Replace last item", "[DList][method][replace][last]") {
  340. mcl::DList<int> lst{1,2,3,4,5};
  341. REQUIRE(lst.get_last() == 5);
  342. lst.replace_last(0);
  343. REQUIRE(lst.get_last() == 0);
  344. }
  345. #pragma endregion
  346. #pragma region transform
  347. TEST_CASE("Swap", "[DList][method][transform][swap]") {
  348. mcl::DList<int> l1{3,2,1};
  349. mcl::DList<int> l2{4,5,6,7,8,9,0};
  350. REQUIRE(l1.get_number() == 3);
  351. REQUIRE(l2.get_number() == 7);
  352. l1.swap(l2);
  353. REQUIRE(l1.get_number() == 7);
  354. REQUIRE(l2.get_number() == 3);
  355. }
  356. TEST_CASE("Unique", "[DList][method][transform][unique]") {
  357. mcl::DList<int> lst{5,2,6,1,1,2,7,6};
  358. REQUIRE(lst.get_number() == 8);
  359. lst.unique();
  360. REQUIRE(lst.get_number() == 5);
  361. }
  362. TEST_CASE("Sort", "[DList][method][transform][sort]") {
  363. mcl::DList<int> lst{5,7,2,1,6};
  364. lst.sort();
  365. int prev = 0;
  366. for (int x : lst) {
  367. REQUIRE(x >= prev);
  368. prev = x;
  369. }
  370. }
  371. #pragma endregion
  372. #pragma region exclude & include nodes
  373. TEST_CASE("Exclude & include nodes", "[DList][method][exclude][include][nodes]") {
  374. mcl::DList<int> ls1{1,2,3,4,5};
  375. mcl::DList<int> ls2{5,1,5,2};
  376. SECTION("Iterator") {
  377. auto it = ls1.find(2);
  378. auto* n = ls1.exclude(it);
  379. ls2.include(ls2.begin(), n);
  380. REQUIRE(*(++ls2.begin()) == 2);
  381. }
  382. SECTION("RIterator") {
  383. auto it = ls1.rfind(4);
  384. auto* n = ls1.exclude(it);
  385. ls2.include(ls2.rbegin(), n);
  386. REQUIRE(*(++ls2.rbegin()) == 4);
  387. }
  388. }
  389. #pragma endregion