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

70 lines
2.0 KiB
C++

#include "BinarySearchTree.h"
#include <boost/test/unit_test.hpp>
using namespace Kylin;
class BinarySearchTreeTest {
public:
BinarySearchTreeTest() {
for (size_t i = 0; i < 11; i++) {
tree.insert(unsorted[i]);
}
}
BinarySearchTree<size_t> tree;
size_t sorted[11]{6, 8, 9, 10, 11, 15, 17, 18, 19, 20, 22};
size_t unsorted[11]{15, 9, 20, 6, 10, 18, 22, 8, 11, 17, 19};
};
/*
15
9 20
6 10 18 22
8 11 17 19
*/
BOOST_FIXTURE_TEST_CASE(Find, BinarySearchTreeTest) {
auto node = tree.find(18);
BOOST_CHECK_EQUAL(node->value, 18);
}
BOOST_FIXTURE_TEST_CASE(Maximum, BinarySearchTreeTest) { BOOST_CHECK_EQUAL(tree.maximum(), 22); }
BOOST_FIXTURE_TEST_CASE(Minimum, BinarySearchTreeTest) { BOOST_CHECK_EQUAL(tree.minimum(), 6); }
BOOST_FIXTURE_TEST_CASE(PredecessorIsLeft, BinarySearchTreeTest) {
auto node = tree.find(18);
auto predecessor = tree.predecessor(node);
BOOST_CHECK_EQUAL(predecessor->value, 17);
}
BOOST_FIXTURE_TEST_CASE(PredecessorIsNotLeft, BinarySearchTreeTest) {
auto node = tree.find(17);
auto predecessor = tree.predecessor(node);
BOOST_CHECK_EQUAL(predecessor->value, 15);
}
BOOST_FIXTURE_TEST_CASE(SuccessorIsRight, BinarySearchTreeTest) {
auto node = tree.find(20);
auto successor = tree.successor(node);
BOOST_CHECK_EQUAL(successor->value, 22);
}
BOOST_FIXTURE_TEST_CASE(SuccessorIsNotRight, BinarySearchTreeTest) {
auto node = tree.find(17);
auto successor = tree.successor(node);
BOOST_CHECK_EQUAL(successor->value, 18);
}
BOOST_FIXTURE_TEST_CASE(SuccessorIsGrandParent, BinarySearchTreeTest) {
auto node = tree.find(19);
auto successor = tree.successor(node);
BOOST_CHECK_EQUAL(successor->value, 20);
}
BOOST_FIXTURE_TEST_CASE(iterator, BinarySearchTreeTest) {
size_t i = 0;
for (const auto &iter : tree) {
BOOST_CHECK_EQUAL(iter, sorted[i++]);
}
}