102 lines
3.0 KiB
C++
102 lines
3.0 KiB
C++
|
#include "DoublyLinkedList.h"
|
||
|
#include <boost/test/unit_test.hpp>
|
||
|
|
||
|
using namespace Kylin;
|
||
|
|
||
|
class DoublyLinkedListTest {
|
||
|
public:
|
||
|
DoublyLinkedList<size_t> list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||
|
};
|
||
|
|
||
|
BOOST_AUTO_TEST_SUITE(DoublyLinkedListTestCase)
|
||
|
BOOST_FIXTURE_TEST_CASE(At, DoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.at(5), 5); }
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(Size, DoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.size(), 10); }
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(Last, DoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.last(), 9); }
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(EmptyListCallLastCauseException, DoublyLinkedListTest) {
|
||
|
DoublyLinkedList<size_t> emptyList;
|
||
|
BOOST_CHECK_THROW(emptyList.last(), InvalidOperationException);
|
||
|
}
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(CallLastAfterRemoveLastElement, DoublyLinkedListTest) {
|
||
|
list.removeAt(list.size() - 1);
|
||
|
BOOST_CHECK_EQUAL(list.last(), 8);
|
||
|
}
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(RemoveAt, DoublyLinkedListTest) {
|
||
|
list.removeAt(3);
|
||
|
BOOST_CHECK_EQUAL(list.at(3), 4);
|
||
|
BOOST_CHECK_EQUAL(list.length(), 9);
|
||
|
}
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(Clear, DoublyLinkedListTest) {
|
||
|
list.clear();
|
||
|
BOOST_CHECK_EQUAL(list.length(), 0);
|
||
|
}
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(IndexOf, DoublyLinkedListTest) { BOOST_CHECK_EQUAL(list.indexOf(5), 5); }
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(Append, DoublyLinkedListTest) {
|
||
|
list.append(198);
|
||
|
BOOST_CHECK_EQUAL(list.length(), 11);
|
||
|
BOOST_CHECK_EQUAL(list.at(list.size() - 1), 198);
|
||
|
}
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(Swap, DoublyLinkedListTest) {
|
||
|
Kylin::DoublyLinkedList<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, DoublyLinkedListTest) {
|
||
|
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, DoublyLinkedListTest) {
|
||
|
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, DoublyLinkedListTest) {
|
||
|
auto list2(list);
|
||
|
BOOST_CHECK_EQUAL(list2.size(), 10);
|
||
|
list[9] = 2; //测试是否为浅拷贝
|
||
|
BOOST_CHECK_EQUAL(list2[9], 9);
|
||
|
}
|
||
|
|
||
|
BOOST_FIXTURE_TEST_CASE(MoveConstructor, DoublyLinkedListTest) {
|
||
|
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, DoublyLinkedListTest) {
|
||
|
size_t value = 0;
|
||
|
for (const auto &v : list) {
|
||
|
BOOST_CHECK_EQUAL(v, value++);
|
||
|
}
|
||
|
BOOST_CHECK_EQUAL(value, list.size());
|
||
|
}
|
||
|
BOOST_AUTO_TEST_SUITE_END()
|