123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- #include <catch2/catch_test_macros.hpp>
- #include "containers/dlist.hpp"
- #pragma region constructors
- TEST_CASE("Default constructor", "[DList][constructor][default]") {
- mcl::DList<int> lst;
- REQUIRE(lst.get_number() == 0);
- }
- TEST_CASE("Allocator constructor", "[DList][constructor][allocator]") {
- SECTION("lvalue") {
- mcl::mem::Allocator<int> alloc;
- mcl::DList<int> lst(alloc);
- REQUIRE(lst.get_number() == 0);
- }
- SECTION("rvalue") {
- mcl::mem::Allocator<int> alloc;
- mcl::DList<int> lst(mcl::value::rvalue_cast(alloc));
- REQUIRE(lst.get_number() == 0);
- }
- }
- TEST_CASE("Initializer list constructor", "[DList][constructor][initializer_list]") {
- mcl::DList<int> lst{1,2,3,4,5};
- REQUIRE(lst.get_number() == 5);
- }
- TEST_CASE("Functor filler constructor", "[DList][constructor][fill][function][generator]") {
- mcl::DList<int> lst(3, [](){ return 1; });
- REQUIRE(lst.get_number() == 3);
- }
- TEST_CASE("Functor filler constructor with start & generator", "[DList][constructor][fill][function][generator][start]") {
- mcl::DList<int> lst(10, 0, [](const int& p){ return p + 2; });
- int prev = -2;
- for (int x : lst) {
- REQUIRE(x-2 == prev);
- prev = x;
- }
- }
- TEST_CASE("Fill from args... constructor", "[DList][constructor][fill][args][variadic][template]") {
- SECTION("With args") {
- mcl::DList<int> lst(4, 5);
- REQUIRE(lst.get_number() == 4);
- }
- SECTION("Without args") {
- mcl::DList<int> lst(6);
- REQUIRE(lst.get_number() == 6);
- }
- }
- TEST_CASE("Copy constructor", "[DList][constructor][copy][assignment]") {
- mcl::DList<int> a(3, 4);
- mcl::DList<int> b(a);
- REQUIRE(a.get_number() == 3);
- REQUIRE(b.get_number() == 3);
- REQUIRE(b.get_number() == a.get_number());
- }
- TEST_CASE("Move constructor", "[DList][constructor][move][assignment]") {
- mcl::DList<int> a(3, 4);
- mcl::DList<int> b(std::move(a));
- REQUIRE(a.get_number() == 0);
- REQUIRE(b.get_number() == 3);
- REQUIRE(b.get_number() != a.get_number());
- }
- TEST_CASE("Iterators assignment constructor", "[DList][constructor][iterators][assignment]") {
- SECTION("from iters") {
- mcl::DList<int> l1{3,2,1};
- mcl::DList<int> l2(l1.begin(), l1.end());
- REQUIRE(l1.get_number() == 3);
- REQUIRE(l2.get_number() == 3);
- }
- SECTION("from pointers") {
- int arr[] = {1,2,3,4,5};
- mcl::DList<int> lst(arr, arr+5);
- REQUIRE(lst.get_number() == 5);
- }
- }
- #pragma endregion
- #pragma region assignment operators
- TEST_CASE("Move assignment operator", "[DList][operator][assignment][move]") {
- mcl::DList<int> l1{1,2,3,4,5};
- mcl::DList<int> l2;
- REQUIRE(l1.get_number() == 5);
- REQUIRE(l2.get_number() == 0);
- l2 = std::move(l1);
- REQUIRE(l1.get_number() == 0);
- REQUIRE(l2.get_number() == 5);
- }
- TEST_CASE("Copy assignment operator", "[DList][operator][assignment][copy]") {
- mcl::DList<int> l1{1,2,3,4,5};
- mcl::DList<int> l2;
- REQUIRE(l1.get_number() == 5);
- REQUIRE(l2.get_number() == 0);
- l2 = l1;
- REQUIRE(l1.get_number() == 5);
- REQUIRE(l2.get_number() == 5);
- }
- TEST_CASE("Copy extend assignment operator", "[DList][operator][assignment][copy][extend]") {
- mcl::DList<int> l1{1,2,3,4,5};
- mcl::DList<int> l2{6,7,8,9,0};
- REQUIRE(l1.get_number() == 5);
- REQUIRE(l2.get_number() == 5);
- l2 += l1;
- REQUIRE(l1.get_number() == 5);
- REQUIRE(l2.get_number() == 10);
- }
- TEST_CASE("Move extend assignment operator", "[DList][operator][assignment][move][extend]") {
- mcl::DList<int> l1{1,2,3,4,5};
- mcl::DList<int> l2{6,7,8,9,0};
- REQUIRE(l1.get_number() == 5);
- REQUIRE(l2.get_number() == 5);
- l2 += std::move(l1);
- REQUIRE(l1.get_number() == 0);
- REQUIRE(l2.get_number() == 10);
- }
- TEST_CASE("Initializer list assignment operator", "[DList][operator][assignment][initializer_list]") {
- mcl::DList<int> lst{1,2,3};
- REQUIRE(lst.get_number() == 3);
- lst = {5,4,3,2,1};
- REQUIRE(lst.get_number() == 5);
- }
- TEST_CASE("Initializer list extend operator", "[DList][operator][assignment][initializer_list][extend]") {
- mcl::DList<int> lst{1,2,3,4,5};
- REQUIRE(lst.get_number() == 5);
- lst += {6,7,8,9,0};
- REQUIRE(lst.get_number() == 10);
- }
- #pragma endregion
- #pragma region comparative operators
- TEST_CASE("Equality operator", "[DList][operator][comparative][equality]") {
- mcl::DList<int> l1{1,2,3};
- mcl::DList<int> l2(l1);
- mcl::DList<int> l3{3,4};
- mcl::DList<int> l4(3, 0);
- REQUIRE(l1 == l2);
- REQUIRE(l1 != l3);
- REQUIRE(!(l1 == l4));
- }
- #pragma endregion
- #pragma region assignment methods
- TEST_CASE("Copy assignment method", "[DList][method][assign][copy]") {
- mcl::DList<int> l1;
- mcl::DList<int> l2{1,2,3};
- REQUIRE(l1.get_number() == 0);
- REQUIRE(l2.get_number() == 3);
- l1.assign(l2);
- REQUIRE(l1.get_number() == 3);
- REQUIRE(l2.get_number() == 3);
- }
- TEST_CASE("Move assignment method", "[DList][method][assign][move]") {
- mcl::DList<int> l1;
- mcl::DList<int> l2{1,2,3};
- REQUIRE(l1.get_number() == 0);
- REQUIRE(l2.get_number() == 3);
- l1.assign(std::move(l2));
- REQUIRE(l1.get_number() == 3);
- REQUIRE(l2.get_number() == 0);
- }
- TEST_CASE("Iterators assign method", "[DList][method][assign][iterators]") {
- mcl::DList<int> l1{3,2,1};
- mcl::DList<int> l2;
- l2.assign(l1.begin(), l1.end());
- REQUIRE(l1.get_number() == 3);
- REQUIRE(l2.get_number() == 3);
- }
- TEST_CASE("Copy extend method", "[DList][method][extend][copy]") {
- mcl::DList<int> l1{1,2,3,4,5};
- mcl::DList<int> l2{6,7,8,9};
- l1.extend(l2);
- REQUIRE(l1.get_number() == 9);
- REQUIRE(l2.get_number() == 4);
- }
- TEST_CASE("Move extend method", "[DList][method][extend][move]") {
- mcl::DList<int> l1{1,2,3,4,5};
- mcl::DList<int> l2{6,7,8,9};
- l1.extend(std::move(l2));
- REQUIRE(l1.get_number() == 9);
- REQUIRE(l2.get_number() == 0);
- }
- TEST_CASE("Functor filler method", "[DList][method][fill][functor]") {
- mcl::DList<int> lst;
- lst.fill(3, [](){ return 1; });
- REQUIRE(lst.get_number() == 3);
- }
- TEST_CASE("Functor filler func with start & generator", "[DList][method][fill][start][generator]") {
- mcl::DList<int> lst;
- lst.fill(10, 0, [](const int& p){ return p + 2; });
- int prev = -2;
- for (int x : lst) {
- REQUIRE(x-2 == prev);
- prev = x;
- }
- }
- TEST_CASE("Fill from args... method", "[DList][method][args][variadic]") {
- mcl::DList<int> lst;
- lst.fill(4, 5);
- REQUIRE(lst.get_number() == 4);
- }
- #pragma endregion
- #pragma region getters
- TEST_CASE("Get first & last method", "[DList][method][getter][first][last]") {
- mcl::DList<int> lst{1,2,3,4,5};
- REQUIRE(lst.get_first() == 1);
- REQUIRE(lst.get_last() == 5);
- }
- TEST_CASE("Get number & empty check", "[DList][method][getter][number][empty][to_emplry][get_number]") {
- mcl::DList<int> lst{1,2,3};
- REQUIRE(lst.get_number() == 3);
- REQUIRE(!lst.is_empty());
- lst.to_empty();
- REQUIRE(lst.get_number() == 0);
- REQUIRE(lst.is_empty());
- }
- TEST_CASE("Find item", "[DList][method][getter][find][item][Iterator]") {
- mcl::DList<int> lst{2,5,3,1,5};
- const int i = 5;
- SECTION("Filled list") {
- REQUIRE(lst.find(i) != lst.end());
- REQUIRE(lst.find(0) == lst.end());
- }
- lst.to_empty();
- SECTION("Empty list") {
- REQUIRE(lst.find(i) == lst.end());
- REQUIRE(lst.find(0) == lst.end());
- }
- }
- TEST_CASE("Find item RIterator", "[DList][method][getter][find][rfind][RIterator][reverse]") {
- mcl::DList<int> lst{2,5,3,1,5};
- const int i = 5;
- SECTION("Filled list") {
- REQUIRE(lst.rfind(i) != lst.rend());
- REQUIRE(lst.rfind(0) == lst.rend());
- }
- lst.to_empty();
- SECTION("Empty list") {
- REQUIRE(lst.rfind(i) == lst.rend());
- REQUIRE(lst.rfind(0) == lst.rend());
- }
- }
- TEST_CASE("Contains", "[DList][method][check][containes]") {
- mcl::DList<int> lst{4,6,32,5,1};
- REQUIRE(lst.contains(5));
- REQUIRE(!lst.contains(0));
- }
- #pragma endregion
- #pragma region iterators
- TEST_CASE("Iterator", "[DList][iterators][Iterator]") {
- SECTION("Reqular") {
- mcl::DList<int> lst{5,2,6,7,5};
- for (auto it=lst.begin(), end=lst.end(); it!=end; ++it)
- REQUIRE(*it);
- }
- SECTION("Const") {
- const mcl::DList<int> lst{5,2,6,7,5};
- for (auto it=lst.begin(), end=lst.end(); it!=end; ++it)
- REQUIRE(*it);
- }
- }
- TEST_CASE("RIterator", "[DList][iterators][RIterator][ireverse]") {
- SECTION("Regular") {
- mcl::DList<int> lst{5,2,6,7,5};
- for (auto it=lst.rbegin(), end=lst.rend(); it!=end; ++it)
- REQUIRE(*it);
- }
- SECTION("Const") {
- const mcl::DList<int> lst{5,2,6,7,5};
- for (auto it=lst.crbegin(), end=lst.crend(); it!=end; ++it)
- REQUIRE(*it);
- }
- }
- #pragma endregion
- #pragma region insert
- TEST_CASE("Insert with Iterator", "[DList][method][insert][Iterator][iterators]") {
- mcl::DList<int> lst{4,2,7,1,8};
- REQUIRE(lst.get_number() == 5);
- auto it = lst.find(7);
- lst.insert(it, 6);
- REQUIRE(lst.get_number() == 6);
- REQUIRE_THROWS_AS(lst.insert(lst.end(), 5), std::invalid_argument);
- }
- TEST_CASE("Insert with RIterator", "[DList][method][insert][RIterator][iterators]") {
- mcl::DList<int> lst{4,2,7,1,8};
- REQUIRE(lst.get_number() == 5);
- auto it = ++lst.rbegin();
- lst.insert(it, 6);
- REQUIRE(lst.get_number() == 6);
- REQUIRE_THROWS_AS(lst.insert(lst.rend(), 5), std::invalid_argument);
- }
- TEST_CASE("Add back", "[DList][method][insert][add][back]") {
- mcl::DList<int> lst{5,6,8,3};
- REQUIRE(lst.get_number() == 4);
- lst.add_back(5,2,7,1);
- REQUIRE(lst.get_number() == 8);
- }
- TEST_CASE("Add front", "[DList][method][insert][add][front]") {
- mcl::DList<int> lst{5,0,1,3};
- REQUIRE(lst.get_number() == 4);
- lst.add_front(1,7,5,1);
- REQUIRE(lst.get_number() == 8);
- }
- #pragma endregion
- #pragma region create
- TEST_CASE("Create back", "[DList][method][insert][create][back]") {
- mcl::DList<int> lst;
- REQUIRE(lst.get_number() == 0);
- lst.create_back(5);
- lst.create_back(4);
- REQUIRE(lst.get_number() == 2);
- }
- TEST_CASE("Create front", "[DList][method][insert][create][front]") {
- mcl::DList<int> lst;
- REQUIRE(lst.get_number() == 0);
- lst.create_front(5);
- lst.create_front(4);
- REQUIRE(lst.get_number() == 2);
- }
- TEST_CASE("Create with Iterator", "[DList][method][insert][create][Iterator][iterators]") {
- mcl::DList<int> lst{1,2,3,4,5};
- lst.create(lst.find(3), 0);
- REQUIRE(lst.get_number() == 6);
- REQUIRE_THROWS(lst.create(lst.end(), 3));
- }
- TEST_CASE("Create with RIterator", "[DList][method][insert][create][RIterator][iterators]") {
- mcl::DList<int> lst{1,2,3,4,5};
- lst.create(lst.rbegin(), 0);
- REQUIRE(lst.get_number() == 6);
- REQUIRE_THROWS(lst.create(lst.rend(), 3));
- }
- #pragma endregion
- #pragma region replace
- TEST_CASE("Replace with iter", "[DList][method][replace][Iterator][iterators]") {
- mcl::DList<int> lst{1,2,3,4,5};
- auto it = lst.find(3);
- REQUIRE(*it == 3);
- lst.replace(it, 0);
- REQUIRE(*it == 0);
- }
- TEST_CASE("Replace first item", "[DList][method][replace][first]") {
- mcl::DList<int> lst{1,2,3,4,5};
- REQUIRE(lst.get_first() == 1);
- lst.replace_first(0);
- REQUIRE(lst.get_first() == 0);
- }
- TEST_CASE("Replace last item", "[DList][method][replace][last]") {
- mcl::DList<int> lst{1,2,3,4,5};
- REQUIRE(lst.get_last() == 5);
- lst.replace_last(0);
- REQUIRE(lst.get_last() == 0);
- }
- #pragma endregion
- #pragma region transform
- TEST_CASE("Swap", "[DList][method][transform][swap]") {
- mcl::DList<int> l1{3,2,1};
- mcl::DList<int> l2{4,5,6,7,8,9,0};
- REQUIRE(l1.get_number() == 3);
- REQUIRE(l2.get_number() == 7);
- l1.swap(l2);
- REQUIRE(l1.get_number() == 7);
- REQUIRE(l2.get_number() == 3);
- }
- TEST_CASE("Unique", "[DList][method][transform][unique]") {
- mcl::DList<int> lst{5,2,6,1,1,2,7,6};
- REQUIRE(lst.get_number() == 8);
- lst.unique();
- REQUIRE(lst.get_number() == 5);
- }
- TEST_CASE("Sort", "[DList][method][transform][sort]") {
- mcl::DList<int> lst{5,7,2,1,6};
- lst.sort();
- int prev = 0;
- for (int x : lst) {
- REQUIRE(x >= prev);
- prev = x;
- }
- }
- #pragma endregion
- #pragma region exclude & include nodes
- TEST_CASE("Exclude & include nodes", "[DList][method][exclude][include][nodes]") {
- mcl::DList<int> ls1{1,2,3,4,5};
- mcl::DList<int> ls2{5,1,5,2};
- SECTION("Iterator") {
- auto it = ls1.find(2);
- auto* n = ls1.exclude(it);
- ls2.include(ls2.begin(), n);
- REQUIRE(*(++ls2.begin()) == 2);
- }
- SECTION("RIterator") {
- auto it = ls1.rfind(4);
- auto* n = ls1.exclude(it);
- ls2.include(ls2.rbegin(), n);
- REQUIRE(*(++ls2.rbegin()) == 4);
- }
- }
- #pragma endregion
|