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

85 lines
2.5 KiB
C++

#include "GeneralTree.h"
#include <boost/test/unit_test.hpp>
#include <iostream>
using namespace Kylin;
class GeneralTreeTest {
public:
GeneralTreeTest() {
auto nodeA = new GeneralTreeNode<char>('A');
nodeA->value = 'A';
tree.insert(nodeA);
auto nodeB = tree.insert('B', nodeA);
nodeC = tree.insert('C', nodeA);
nodeD = tree.insert('D', nodeA);
nodeE = tree.insert('E', nodeB);
/*auto nodeF =*/tree.insert('F', nodeB);
/*auto nodeH =*/tree.insert('H', nodeD);
/*auto nodeI =*/tree.insert('I', nodeD);
/*auto nodeJ =*/tree.insert('J', nodeD);
}
char values[9]{'A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J'};
GeneralTree<char> tree;
GeneralTreeNode<char> *nodeE = nullptr;
GeneralTreeNode<char> *nodeC = nullptr;
GeneralTreeNode<char> *nodeD = nullptr;
};
BOOST_AUTO_TEST_SUITE(GeneralTreeTestCase)
BOOST_FIXTURE_TEST_CASE(Count, GeneralTreeTest) { BOOST_CHECK_EQUAL(tree.count(), 9); }
BOOST_FIXTURE_TEST_CASE(Height, GeneralTreeTest) { BOOST_CHECK_EQUAL(tree.height(), 3); }
BOOST_FIXTURE_TEST_CASE(Degree, GeneralTreeTest) { BOOST_CHECK_EQUAL(tree.degree(), 3); }
BOOST_FIXTURE_TEST_CASE(FindWithValue, GeneralTreeTest) {
auto node = tree.find('H');
BOOST_CHECK_EQUAL(node != nullptr, true);
BOOST_CHECK_EQUAL(node->value, 'H');
}
BOOST_FIXTURE_TEST_CASE(FindWithNode, GeneralTreeTest) {
auto result = tree.find(nodeE);
BOOST_CHECK_EQUAL(result, true);
}
BOOST_FIXTURE_TEST_CASE(InsertWithValue, GeneralTreeTest) {
auto nodeG = tree.insert('G', nodeC);
BOOST_CHECK_EQUAL(nodeG != nullptr, true);
BOOST_CHECK_EQUAL(nodeG->value, 'G');
}
BOOST_FIXTURE_TEST_CASE(InsertWithNode, GeneralTreeTest) {
auto nodeG = new GeneralTreeNode<char>('G', nodeC);
auto result = tree.insert(nodeG);
BOOST_CHECK_EQUAL(result, true);
}
BOOST_FIXTURE_TEST_CASE(Clear, GeneralTreeTest) {
tree.clear();
BOOST_CHECK_EQUAL(tree.count(), 0);
}
BOOST_FIXTURE_TEST_CASE(RemoveWithValue, GeneralTreeTest) {
auto &&removeTree = tree.remove('D');
BOOST_CHECK_EQUAL(removeTree.count(), 4);
}
BOOST_FIXTURE_TEST_CASE(RemoveWithNode, GeneralTreeTest) {
auto removeTree = tree.remove(nodeD);
BOOST_CHECK_EQUAL(removeTree.count(), 4);
}
BOOST_FIXTURE_TEST_CASE(Iterator, GeneralTreeTest) {
size_t index = 0;
for (auto v : tree) {
BOOST_CHECK_EQUAL(v, values[index++]);
}
BOOST_CHECK_EQUAL(index, 9);
}
BOOST_AUTO_TEST_SUITE_END()