Vector tests added

This commit is contained in:
2019-04-28 19:48:48 +02:00
parent 3dbc5c211a
commit 2c726f144f
3 changed files with 85 additions and 15 deletions

View File

@@ -1,23 +1,14 @@
#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();
unsigned const value = Util::GetRandomNumber();
control.insert(value);
tree.Insert(value);
}
@@ -54,7 +45,7 @@ bool TestDeletion()
unsigned const toDeleteCount = control.size() / 4;
while(i < toDeleteCount)
{
auto const toDelete = GetRandomNumber();
auto const toDelete = Util::GetRandomNumber();
auto iter = control.find(toDelete);
if(iter != control.end())
{

View File

@@ -1,8 +1,23 @@
#include <cstdio>
#include <exception>
#include <random>
namespace Util
{
unsigned static GetRandomNumber()
{
static std::default_random_engine eng;
static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u);
return valueDist(eng);
}
};
namespace Test
{
void Execute(bool (*testFunction)(void), char const * const message)
{
try
{
if(testFunction())
{
@@ -13,4 +28,9 @@ void Execute(bool (*testFunction)(void), char const * const message)
std::printf("[FAIL] %s\n", message);
}
}
catch(std::exception & e)
{
std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what());
}
}
};

59
tests/vector.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include "../random-access-containers/vector.hpp"
#include "testutil.hpp"
void FillWithSequentialNumbers(Vector<unsigned> & vector,
unsigned const count = 1024u)
{
for(unsigned i = 0; i < count; ++i)
{
vector[i] = i;
}
}
bool TestInsertion()
{
unsigned const targetSize = 1024u;
Vector<unsigned> vector;
try
{
vector.Resize(targetSize);
}
catch(std::exception & e)
{
return false;
}
FillWithSequentialNumbers(vector, targetSize);
for(unsigned i = 0; i < vector.GetSize(); ++i)
{
if(vector[i] != i)
{
return false;
}
}
try
{
vector[targetSize + 1] = 42;
}
catch(std::exception & e)
{
return true;
}
return false;
}
bool TestResize()
{
return false;
}
int main()
{
Test::Execute(TestInsertion, "Insertion test");
Test::Execute(TestResize, "Resize test");
return 0;
}