Files
datastructures/test/binarytree.cpp

182 lines
3.8 KiB
C++

#include "../src/tree/binarytree.hpp"
#include "testutil.hpp"
#include <vector>
#include <unordered_set>
void FillWithUniqueRandomNumbers(BinaryTree::Tree<unsigned> & tree,
std::unordered_set<unsigned> & control,
unsigned const count = 10000u)
{
for(unsigned i = 0u; i < count; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.insert(value);
tree.InsertNoDuplicates(value);
}
}
bool TestInsert()
{
unsigned const testSize = 4096;
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
for(unsigned i = 0u; i < testSize; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.push_back(value);
tree.Insert(value);
}
for(auto const & num : control)
{
if(!tree.Contains(num))
{
//std::printf("\tValue %u inserted but cannot be found!\n", num);
return false;
}
}
return true;
}
bool TestInsertNoDuplicates()
{
std::unordered_set<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithUniqueRandomNumbers(tree, control);
for(auto const & num : control)
{
if(!tree.Contains(num))
{
//std::printf("\tValue %u inserted but cannot be found!\n", num);
return false;
}
}
return true;
}
bool TestDeletion()
{
std::unordered_set<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithUniqueRandomNumbers(tree, control);
std::vector<unsigned> deletedValues;
unsigned const toDeleteCount = control.size() / 4;
for(unsigned i = 0u; i < toDeleteCount; ++i)
{
auto const valueTodelete = Util::GetRandomNumber();
auto controlElement = control.find(valueTodelete);
if(controlElement != control.end())
{
control.erase(controlElement);
tree.Delete(valueTodelete);
deletedValues.push_back(valueTodelete);
}
}
for(auto const & num : control)
{
if(!tree.Contains(num))
{
std::puts("[INFO] Tree does not contain a value that it should.");
//std::printf("Value %u inserted but cannot be found!\n", num);
return false;
}
}
for(auto const & num : deletedValues)
{
if(tree.Contains(num))
{
std::puts("[INFO] Tree contains deleted value that it should not.");
return false;
}
}
return true;
}
bool TestDeletionCase(
std::vector<unsigned> const toInsert,
std::vector<unsigned> const toDelete,
std::vector<unsigned> const result)
{
BinaryTree::Tree<unsigned> tree;
for(auto n : toInsert)
{
tree.Insert(n);
}
for(auto n : toDelete)
{
tree.Delete(n);
}
bool ok = true;
for(auto n : result)
{
if(!tree.Contains(n))
{
std::printf("\tError, tree does not contain value %u.\n", n);
ok = false;
}
}
for(auto n : toDelete)
{
if(tree.Contains(n))
{
std::printf("\tError, tree contains deleted value %u.\n", n);
ok = false;
}
}
return ok;
}
bool TestDeletionCases()
{
std::puts("Testing deleting root with single child right.");
TestDeletionCase(
std::vector<unsigned> { 10, 12, 11, 13},
std::vector<unsigned> { 10 },
std::vector<unsigned> { 12, 11, 13 });
std::puts("Testing deleting root with single child left.");
TestDeletionCase(
std::vector<unsigned> { 15, 12, 11, 13},
std::vector<unsigned> { 15 },
std::vector<unsigned> { 12, 11, 13 });
std::puts("Testing deleting root with 2 children but no child right left.");
TestDeletionCase(
std::vector<unsigned> { 15, 10, 20, 22, 25, 18, 5},
std::vector<unsigned> { 20 },
std::vector<unsigned> { 15, 10, 22, 25, 18, 5 });
std::puts("Testing deleting root with 2 children.");
TestDeletionCase(
std::vector<unsigned> { 50, 40, 60, 75, 55, 45, 42, 58 },
std::vector<unsigned> { 50 },
std::vector<unsigned> { 40, 60, 75, 55, 45, 42, 58 });
return true;
}
int main()
{
Test::Execute(TestInsert, "Insertion and find test");
Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test");
Test::Execute(TestDeletion, "Insertion and deletion test");
Test::Execute(TestDeletionCases, "Deletion cases test");
return 0;
}