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

76 lines
2.3 KiB
C++

#include "DynamicArrayList.h"
#include <boost/test/unit_test.hpp>
using namespace Kylin;
class DynamicArrayListTest {
public:
DynamicArrayList<size_t> list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
};
BOOST_AUTO_TEST_SUITE(DynamicArrayListTestCase)
BOOST_FIXTURE_TEST_CASE(At, DynamicArrayListTest) { BOOST_CHECK_EQUAL(list.at(4), 4); }
BOOST_FIXTURE_TEST_CASE(Size, DynamicArrayListTest) { BOOST_CHECK_EQUAL(list.size(), 10); }
BOOST_FIXTURE_TEST_CASE(Capacity, DynamicArrayListTest) { BOOST_CHECK_EQUAL(list.capacity(), 10); }
BOOST_FIXTURE_TEST_CASE(Last, DynamicArrayListTest) { BOOST_CHECK_EQUAL(list.last(), 9); }
BOOST_FIXTURE_TEST_CASE(CopyConstructor, DynamicArrayListTest) {
auto list1(list);
BOOST_CHECK_EQUAL(list1.size(), 10);
BOOST_CHECK_EQUAL(list1.capacity(), 10);
BOOST_CHECK_EQUAL(list1.at(4), 4);
}
BOOST_FIXTURE_TEST_CASE(MoveConstructor, DynamicArrayListTest) {
auto list1(std::move(list));
BOOST_CHECK_EQUAL(list.size(), 0);
BOOST_CHECK_EQUAL(list1.size(), 10);
BOOST_CHECK_EQUAL(list.capacity(), 0);
BOOST_CHECK_EQUAL(list1.capacity(), 10);
BOOST_CHECK_EQUAL(list1.at(4), 4);
}
BOOST_FIXTURE_TEST_CASE(Append, DynamicArrayListTest) {
list.append(456);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list[10], 456);
BOOST_CHECK_EQUAL(list.capacity(), 20); // 此时append会引发扩容2倍空间
}
BOOST_FIXTURE_TEST_CASE(Insert, DynamicArrayListTest) {
list.insert(4, 789);
BOOST_CHECK_EQUAL(list.length(), 11);
BOOST_CHECK_EQUAL(list[4], 789);
BOOST_CHECK_EQUAL(list[5], 4);
BOOST_CHECK_EQUAL(list.capacity(), 20); // 此时insert会引发扩容2倍空间
}
BOOST_FIXTURE_TEST_CASE(RemoveAt, DynamicArrayListTest) {
list.removeAt(3);
BOOST_CHECK_EQUAL(list[3], 4);
BOOST_CHECK_EQUAL(list.length(), 9);
}
BOOST_FIXTURE_TEST_CASE(Clear, DynamicArrayListTest) {
list.clear();
BOOST_CHECK_EQUAL(list.length(), 0);
BOOST_CHECK_EQUAL(list.capacity(), 10);
}
BOOST_FIXTURE_TEST_CASE(IndexOf, DynamicArrayListTest) { BOOST_CHECK_EQUAL(list.indexOf(5), 5); }
BOOST_FIXTURE_TEST_CASE(ForEach, DynamicArrayListTest) {
size_t index = 0;
for (auto value : list) {
BOOST_CHECK_EQUAL(value, index++);
}
BOOST_CHECK_EQUAL(10, index);
}
BOOST_AUTO_TEST_SUITE_END()