Vector tests added
This commit is contained in:
@@ -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())
|
||||
{
|
||||
|
||||
@@ -1,16 +1,36 @@
|
||||
#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)
|
||||
{
|
||||
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