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

102 lines
2.8 KiB
C++

#include "LinkedList.h"
#include <boost/test/unit_test.hpp>
using namespace Kylin;
class LinkedListTest {
public:
LinkedList<size_t> list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
};
BOOST_AUTO_TEST_SUITE(LinkedListTestCase)
BOOST_FIXTURE_TEST_CASE(At, LinkedListTest) { BOOST_CHECK_EQUAL(list.at(5), 5); }
BOOST_FIXTURE_TEST_CASE(Size, LinkedListTest) { BOOST_CHECK_EQUAL(list.size(), 10); }
BOOST_FIXTURE_TEST_CASE(Last, LinkedListTest) { BOOST_CHECK_EQUAL(list.last(), 9); }
BOOST_FIXTURE_TEST_CASE(EmptyListCallLastCauseException, LinkedListTest) {
LinkedList<size_t> emptyList;
BOOST_CHECK_THROW(emptyList.last(), InvalidOperationException);
}
BOOST_FIXTURE_TEST_CASE(CallLastAfterRemoveLastElement, LinkedListTest) {
list.removeAt(list.size() - 1);
BOOST_CHECK_EQUAL(list.last(), 8);
}
BOOST_FIXTURE_TEST_CASE(RemoveAt, LinkedListTest) {
list.removeAt(3);
BOOST_CHECK_EQUAL(list.at(3), 4);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(Clear, LinkedListTest) {
list.clear();
BOOST_CHECK_EQUAL(list.length(), 0);
}
BOOST_FIXTURE_TEST_CASE(IndexOf, LinkedListTest) { BOOST_CHECK_EQUAL(list.indexOf(5), 5); }
BOOST_FIXTURE_TEST_CASE(Append, LinkedListTest) {
list.append(198);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(10), 198);
}
BOOST_FIXTURE_TEST_CASE(InsertToIndexLessThenSize, LinkedListTest) {
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(InsertToIndexEuqalSize, LinkedListTest) {
list.insert(list.size(), 456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(list.size() - 1), 456);
}
BOOST_FIXTURE_TEST_CASE(Swap, LinkedListTest) {
Kylin::LinkedList<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, LinkedListTest) {
auto list2(list);
BOOST_CHECK_EQUAL(list2.size(), 10);
BOOST_CHECK_EQUAL(list2[9], 9);
}
BOOST_FIXTURE_TEST_CASE(MoveConstructor, LinkedListTest) {
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, LinkedListTest) {
size_t value = 0;
for (const auto &v : list) {
BOOST_CHECK_EQUAL(v, value++);
}
BOOST_CHECK_EQUAL(value, 10);
}
BOOST_AUTO_TEST_SUITE_END()