Files
datastructures/test/binarytree.cpp

73 lines
1.4 KiB
C++

#include "../tree/binarytree.hpp"
#include "testutil.hpp"
#include <vector>
void FillWithRandomNumbers(BinaryTree::Tree<unsigned> & tree,
std::vector<unsigned> & control,
unsigned const count = 512u)
{
for(unsigned i = 0u; i < count; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.push_back(value);
tree.Insert(value);
}
}
bool TestInsertContains()
{
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithRandomNumbers(tree, control);
bool good = true;
for(auto const & num : control)
{
if(!tree.Contains(num))
{
std::printf("\tValue %u inserted but cannot be found!\n", num);
good = false;
}
}
return good;
}
bool TestDeletion()
{
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithRandomNumbers(tree, control);
unsigned const toDeleteCount = control.size() / 4;
for(unsigned i = 0u; i < toDeleteCount; ++i)
{
auto const toDeleteIndex = Util::GetRandomNumber() % control.size();
auto const toDeleteValue = control[toDeleteIndex];
control.erase(control.begin() + toDeleteIndex);
tree.Delete(toDeleteValue);
}
bool good = true;
for(auto const & num : control)
{
if(!tree.Contains(num))
{
std::printf("Value %u inserted but cannot be found!\n", num);
good = false;
}
}
return good;
}
int main()
{
Test::Execute(TestInsertContains, "Insertion and find test");
Test::Execute(TestDeletion, "Insertion and deletion test");
return 0;
}