Kylin/UnitTest/DataStructure/CircularDoublyLinkedListTest.cpp

109 lines
3.4 KiB
C++
Raw Normal View History

2023-12-27 10:29:16 +08:00
#include "CircularDoublyLinkedList.h"
#include <boost/test/unit_test.hpp>
#include <iostream>
using namespace Kylin;
class CircularDoublyLinkedListTest {
public:
CircularDoublyLinkedList<size_t> list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
};
BOOST_AUTO_TEST_SUITE(CircularDoublyLinkedListTestCase)
BOOST_FIXTURE_TEST_CASE(At, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.at(5), 5); }
BOOST_FIXTURE_TEST_CASE(Size, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.size(), 10); }
BOOST_FIXTURE_TEST_CASE(Last, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.last(), 9); }
BOOST_FIXTURE_TEST_CASE(EmptyListCallLastCauseException, CircularDoublyLinkedListTest) {
CircularDoublyLinkedList<size_t> emptyList;
BOOST_CHECK_THROW(emptyList.last(), InvalidOperationException);
}
BOOST_FIXTURE_TEST_CASE(CallLastAfterRemoveLastElement, CircularDoublyLinkedListTest) {
list.removeAt(list.size() - 1);
BOOST_CHECK_EQUAL(list.last(), 8);
}
BOOST_FIXTURE_TEST_CASE(RemoveIndexLessThenSize, CircularDoublyLinkedListTest) {
list.removeAt(3);
BOOST_CHECK_EQUAL(list[3], 4);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(RemoveIndexBigThenSize, CircularDoublyLinkedListTest) {
list.removeAt(15);
BOOST_CHECK_EQUAL(list[5], 6);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(Clear, CircularDoublyLinkedListTest) {
list.clear();
BOOST_CHECK_EQUAL(list.length(), 0);
}
BOOST_FIXTURE_TEST_CASE(IndexOf, CircularDoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.indexOf(5), 5); }
BOOST_FIXTURE_TEST_CASE(Append, CircularDoublyLinkedListTest) {
list.append(198);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(list.size() - 1), 198);
}
BOOST_FIXTURE_TEST_CASE(Swap, CircularDoublyLinkedListTest) {
CircularDoublyLinkedList<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(InsertToIndexLessThenSize, CircularDoublyLinkedListTest) {
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, CircularDoublyLinkedListTest) {
list.insert(list.size(), 456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list.at(list.size() - 1), 456);
}
BOOST_FIXTURE_TEST_CASE(CopyConstructor, CircularDoublyLinkedListTest) {
auto list2(list);
BOOST_CHECK_EQUAL(list2.size(), 10);
list[9] = 2; //测试是否为浅拷贝
BOOST_CHECK_EQUAL(list2[9], 9);
}
BOOST_FIXTURE_TEST_CASE(MoveConstructor, CircularDoublyLinkedListTest) {
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, CircularDoublyLinkedListTest) {
size_t i = 0;
for (const auto &value : list) {
BOOST_CHECK_EQUAL(value, i++);
}
BOOST_CHECK_EQUAL(list.size(), i);
}
BOOST_AUTO_TEST_SUITE_END()