Format source files
This commit is contained in:
@@ -1,182 +1,183 @@
|
||||
#include "../src/tree/binarytree.hpp"
|
||||
#include "testutil.hpp"
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
void FillWithUniqueRandomNumbers(BinaryTree::Tree<unsigned> & tree,
|
||||
std::unordered_set<unsigned> & control,
|
||||
unsigned const count = 10000u)
|
||||
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);
|
||||
}
|
||||
for(unsigned i = 0u; i < count; ++i)
|
||||
{
|
||||
unsigned const value = Util::GetRandomNumber();
|
||||
control.insert(value);
|
||||
tree.InsertNoDuplicates(value);
|
||||
}
|
||||
}
|
||||
|
||||
bool TestInsert()
|
||||
{
|
||||
unsigned const testSize = 4096;
|
||||
unsigned const testSize = 4096;
|
||||
|
||||
std::vector<unsigned> control;
|
||||
BinaryTree::Tree<unsigned> tree;
|
||||
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;
|
||||
}
|
||||
}
|
||||
for(unsigned i = 0u; i < testSize; ++i)
|
||||
{
|
||||
unsigned const value = Util::GetRandomNumber();
|
||||
control.push_back(value);
|
||||
tree.Insert(value);
|
||||
}
|
||||
|
||||
return true;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
FillWithUniqueRandomNumbers(tree, control);
|
||||
|
||||
return true;
|
||||
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;
|
||||
std::unordered_set<unsigned> control;
|
||||
BinaryTree::Tree<unsigned> tree;
|
||||
|
||||
FillWithUniqueRandomNumbers(tree, control);
|
||||
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();
|
||||
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("[ERROR] Tree does not contain a value that it should.");
|
||||
//std::printf("Value %u inserted but cannot be found!\n", num);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto controlElement = control.find(valueTodelete);
|
||||
if(controlElement != control.end())
|
||||
{
|
||||
control.erase(controlElement);
|
||||
tree.Delete(valueTodelete);
|
||||
deletedValues.push_back(valueTodelete);
|
||||
}
|
||||
}
|
||||
|
||||
for(auto const & num : deletedValues)
|
||||
{
|
||||
if(tree.Contains(num))
|
||||
{
|
||||
std::puts("[ERROR] Tree contains deleted value that it should not.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for(auto const & num: control)
|
||||
{
|
||||
if(!tree.Contains(num))
|
||||
{
|
||||
std::puts("[ERROR] Tree does not contain a value that it should.");
|
||||
//std::printf("Value %u inserted but cannot be found!\n", num);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
for(auto const & num: deletedValues)
|
||||
{
|
||||
if(tree.Contains(num))
|
||||
{
|
||||
std::puts("[ERROR] 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)
|
||||
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);
|
||||
}
|
||||
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("[ERROR] Tree does not contain value %u.\n", n);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
for(auto n: toDelete)
|
||||
{
|
||||
tree.Delete(n);
|
||||
}
|
||||
|
||||
for(auto n : toDelete)
|
||||
{
|
||||
if(tree.Contains(n))
|
||||
{
|
||||
std::printf("[ERROR] Tree contains deleted value %u.\n", n);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
bool ok = true;
|
||||
for(auto n: result)
|
||||
{
|
||||
if(!tree.Contains(n))
|
||||
{
|
||||
std::printf("[ERROR] Tree does not contain value %u.\n", n);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
for(auto n: toDelete)
|
||||
{
|
||||
if(tree.Contains(n))
|
||||
{
|
||||
std::printf("[ERROR] Tree contains deleted value %u.\n", n);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool TestDeletionCases()
|
||||
{
|
||||
std::puts("[INFO] 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("[INFO] 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("[INFO] 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("[INFO] 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 });
|
||||
std::puts("[INFO] 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});
|
||||
|
||||
return true;
|
||||
std::puts("[INFO] 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("[INFO] 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("[INFO] 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");
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user