binary tree tests added
This commit is contained in:
86
tests/binarytree.cpp
Normal file
86
tests/binarytree.cpp
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#include "../binary-trees/binarytree.hpp"
|
||||||
|
#include <random>
|
||||||
|
#include "testutil.hpp"
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
unsigned static GetRandomNumber()
|
||||||
|
{
|
||||||
|
static std::default_random_engine eng;
|
||||||
|
static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u);
|
||||||
|
|
||||||
|
return valueDist(eng);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FillWithRandomNumbers(BinaryTree::Tree<unsigned> & tree,
|
||||||
|
std::unordered_set<unsigned> & control,
|
||||||
|
unsigned const count = 512u)
|
||||||
|
{
|
||||||
|
for(unsigned i = 0u; i < count; ++i)
|
||||||
|
{
|
||||||
|
unsigned const value = GetRandomNumber();
|
||||||
|
control.insert(value);
|
||||||
|
tree.Insert(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TestInsertContains()
|
||||||
|
{
|
||||||
|
std::unordered_set<unsigned> control;
|
||||||
|
BinaryTree::Tree<unsigned> tree;
|
||||||
|
|
||||||
|
FillWithRandomNumbers(tree, control);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TestDeletion()
|
||||||
|
{
|
||||||
|
std::unordered_set<unsigned> control;
|
||||||
|
BinaryTree::Tree<unsigned> tree;
|
||||||
|
|
||||||
|
FillWithRandomNumbers(tree, control);
|
||||||
|
|
||||||
|
unsigned i = 0u;
|
||||||
|
unsigned const toDeleteCount = control.size() / 4;
|
||||||
|
while(i < toDeleteCount)
|
||||||
|
{
|
||||||
|
auto const toDelete = GetRandomNumber();
|
||||||
|
auto iter = control.find(toDelete);
|
||||||
|
if(iter != control.end())
|
||||||
|
{
|
||||||
|
control.erase(iter);
|
||||||
|
tree.Delete(i);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
16
tests/testutil.hpp
Normal file
16
tests/testutil.hpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
void Execute(bool (*testFunction)(void), char const * const message)
|
||||||
|
{
|
||||||
|
if(testFunction())
|
||||||
|
{
|
||||||
|
std::printf("[PASS] %s\n", message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::printf("[FAIL] %s\n", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user