Vector tests added
This commit is contained in:
@@ -1,23 +1,14 @@
|
|||||||
#include "../binary-trees/binarytree.hpp"
|
#include "../binary-trees/binarytree.hpp"
|
||||||
#include <random>
|
|
||||||
#include "testutil.hpp"
|
#include "testutil.hpp"
|
||||||
#include <unordered_set>
|
#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,
|
void FillWithRandomNumbers(BinaryTree::Tree<unsigned> & tree,
|
||||||
std::unordered_set<unsigned> & control,
|
std::unordered_set<unsigned> & control,
|
||||||
unsigned const count = 512u)
|
unsigned const count = 512u)
|
||||||
{
|
{
|
||||||
for(unsigned i = 0u; i < count; ++i)
|
for(unsigned i = 0u; i < count; ++i)
|
||||||
{
|
{
|
||||||
unsigned const value = GetRandomNumber();
|
unsigned const value = Util::GetRandomNumber();
|
||||||
control.insert(value);
|
control.insert(value);
|
||||||
tree.Insert(value);
|
tree.Insert(value);
|
||||||
}
|
}
|
||||||
@@ -54,7 +45,7 @@ bool TestDeletion()
|
|||||||
unsigned const toDeleteCount = control.size() / 4;
|
unsigned const toDeleteCount = control.size() / 4;
|
||||||
while(i < toDeleteCount)
|
while(i < toDeleteCount)
|
||||||
{
|
{
|
||||||
auto const toDelete = GetRandomNumber();
|
auto const toDelete = Util::GetRandomNumber();
|
||||||
auto iter = control.find(toDelete);
|
auto iter = control.find(toDelete);
|
||||||
if(iter != control.end())
|
if(iter != control.end())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,16 +1,36 @@
|
|||||||
#include <cstdio>
|
#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
|
namespace Test
|
||||||
{
|
{
|
||||||
void Execute(bool (*testFunction)(void), char const * const message)
|
void Execute(bool (*testFunction)(void), char const * const message)
|
||||||
{
|
{
|
||||||
if(testFunction())
|
try
|
||||||
{
|
{
|
||||||
std::printf("[PASS] %s\n", message);
|
if(testFunction())
|
||||||
|
{
|
||||||
|
std::printf("[PASS] %s\n", message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::printf("[FAIL] %s\n", message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch(std::exception & e)
|
||||||
{
|
{
|
||||||
std::printf("[FAIL] %s\n", message);
|
std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
59
tests/vector.cpp
Normal file
59
tests/vector.cpp
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user