Restructure project directories
This commit is contained in:
77
test/binarytree.cpp
Normal file
77
test/binarytree.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "../tree/binarytree.hpp"
|
||||
#include "testutil.hpp"
|
||||
#include <unordered_set>
|
||||
|
||||
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 = Util::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 = Util::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
test/makefile
Normal file
16
test/makefile
Normal file
@@ -0,0 +1,16 @@
|
||||
CC = g++
|
||||
CFLAGS = -std=c++17 -Wall
|
||||
|
||||
CPPS = $(wildcard *.cpp)
|
||||
EXES = $(patsubst %.cpp, %.out, $(CPPS))
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
%.out: %.cpp
|
||||
$(CC) $(CFLAGS) $< -o $@
|
||||
|
||||
all: $(EXES)
|
||||
|
||||
clean:
|
||||
-rm *.o
|
||||
-rm *.out
|
||||
62
test/testutil.hpp
Normal file
62
test/testutil.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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())
|
||||
{
|
||||
std::printf("[PASS] %s\n", message);
|
||||
}
|
||||
else
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
void Execute(bool (*testFunction)(std::vector<std::string> &), char const * const message)
|
||||
{
|
||||
std::vector<std::string> issues;
|
||||
try
|
||||
{
|
||||
if(testFunction(issues))
|
||||
{
|
||||
std::printf("[PASS] %s\n", message);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(auto & issue : issues)
|
||||
{
|
||||
std::printf(" Issue: %s\n", issue.c_str());
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
};
|
||||
75
test/vector.cpp
Normal file
75
test/vector.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "../sequential/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()
|
||||
{
|
||||
Vector<unsigned> vector;
|
||||
|
||||
vector.Resize(2048u);
|
||||
FillWithSequentialNumbers(vector);
|
||||
|
||||
vector.Resize(vector.GetSize() / 2u);
|
||||
for(unsigned i = 0; i < vector.GetSize(); ++i)
|
||||
{
|
||||
if(vector[i] != i)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
vector.Resize(vector.GetSize() * 16u);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Test::Execute(TestInsertion, "Insertion test");
|
||||
Test::Execute(TestResize, "Resize test");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user