Kylin/UnitTest/DataStructure/CircularLinkedListTest.cpp
2023-12-27 10:29:16 +08:00

104 lines
3.0 KiB
C++

#include "CircularLinkedList.h"
#include <boost/test/unit_test.hpp>
using namespace Kylin;
class CircularListTest {
public:
CircularLinkedList<size_t> list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
};
BOOST_AUTO_TEST_SUITE(CircularListTestCase)
BOOST_FIXTURE_TEST_CASE(AtIndexLessThanSize, CircularListTest) { BOOST_CHECK_EQUAL(list.at(5), 5); }
BOOST_FIXTURE_TEST_CASE(AtIndexBiggerThanSize, CircularListTest) { BOOST_CHECK_EQUAL(list.at(15), 5); }
BOOST_FIXTURE_TEST_CASE(Size, CircularListTest) { BOOST_CHECK_EQUAL(list.size(), 10); }
BOOST_FIXTURE_TEST_CASE(Last, CircularListTest) { BOOST_CHECK_EQUAL(list.last(), 9); }
BOOST_FIXTURE_TEST_CASE(EmptyListCallLastCauseException, CircularListTest) {
CircularLinkedList<size_t> emptyList;
BOOST_CHECK_THROW(emptyList.last(), InvalidOperationException);
}
BOOST_FIXTURE_TEST_CASE(RemoveAtIndexLessThanSize, CircularListTest) {
list.removeAt(3);
BOOST_CHECK_EQUAL(list.at(3), 4);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(RemoveAtIndexBiggerThanSize, CircularListTest) {
list.removeAt(15);
BOOST_CHECK_EQUAL(list.length(), 9);
BOOST_CHECK_EQUAL(list.at(15), 7);
}
BOOST_FIXTURE_TEST_CASE(Clear, CircularListTest) {
list.clear();
BOOST_CHECK_EQUAL(list.length(), 0);
}
BOOST_FIXTURE_TEST_CASE(IndexOf, CircularListTest) { BOOST_CHECK_EQUAL(list.indexOf(5), 5); }
BOOST_FIXTURE_TEST_CASE(Append, CircularListTest) {
list.append(198);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(10), 198);
}
BOOST_FIXTURE_TEST_CASE(InsertToIndexLessThenSize, CircularListTest) {
list.insert(4, 456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(4), 456);
BOOST_CHECK_EQUAL(list.at(5), 4);
}
BOOST_FIXTURE_TEST_CASE(InsertToIndexBiggerSize, CircularListTest) {
list.insert(15, 456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(4), 456);
}
BOOST_FIXTURE_TEST_CASE(Swap, CircularListTest) {
CircularLinkedList<size_t> list2;
for (size_t i = 0; i < 5; i++) {
list2.insert(list2.length(), i + 10);
}
list2.swap(list);
BOOST_CHECK_EQUAL(list.size(), 5);
BOOST_CHECK_EQUAL(list2.size(), 10);
for (size_t i = 0; i < 5; i++) {
BOOST_CHECK_EQUAL(list.at(i), i + 10);
}
for (size_t i = 0; i < 10; i++) {
BOOST_CHECK_EQUAL(list2.at(i), i);
}
}
BOOST_FIXTURE_TEST_CASE(CopyConstructor, CircularListTest) {
auto list2(list);
BOOST_CHECK_EQUAL(list2.size(), 10);
BOOST_CHECK_EQUAL(list2[9], 9);
}
BOOST_FIXTURE_TEST_CASE(MoveConstructor, CircularListTest) {
auto list2(std::move(list));
BOOST_CHECK_EQUAL(list.empty(), true);
BOOST_CHECK_EQUAL(list2.size(), 10);
BOOST_CHECK_EQUAL(list2[9], 9);
}
BOOST_FIXTURE_TEST_CASE(Iterator, CircularListTest) {
size_t value = 0;
for (const auto &v : list) {
BOOST_CHECK_EQUAL(v, value++);
}
BOOST_CHECK_EQUAL(value, 10);
}
BOOST_AUTO_TEST_SUITE_END()