Format source files

This commit is contained in:
2023-02-18 10:37:38 +01:00
parent 76d93bb1cf
commit d09d4d3d92
12 changed files with 793 additions and 850 deletions

View File

@@ -1,182 +1,183 @@
#include "../src/tree/binarytree.hpp"
#include "testutil.hpp"
#include <vector>
#include <unordered_set>
#include <vector>
void FillWithUniqueRandomNumbers(BinaryTree::Tree<unsigned> & tree,
std::unordered_set<unsigned> & control,
unsigned const count = 10000u)
void FillWithUniqueRandomNumbers(
BinaryTree::Tree<unsigned> & tree,
std::unordered_set<unsigned> & control,
unsigned const count = 10000u)
{
for(unsigned i = 0u; i < count; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.insert(value);
tree.InsertNoDuplicates(value);
}
for(unsigned i = 0u; i < count; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.insert(value);
tree.InsertNoDuplicates(value);
}
}
bool TestInsert()
{
unsigned const testSize = 4096;
unsigned const testSize = 4096;
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
for(unsigned i = 0u; i < testSize; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.push_back(value);
tree.Insert(value);
}
for(auto const & num : control)
{
if(!tree.Contains(num))
{
//std::printf("\tValue %u inserted but cannot be found!\n", num);
return false;
}
}
for(unsigned i = 0u; i < testSize; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.push_back(value);
tree.Insert(value);
}
return true;
for(auto const & num: control)
{
if(!tree.Contains(num))
{
//std::printf("\tValue %u inserted but cannot be found!\n", num);
return false;
}
}
return true;
}
bool TestInsertNoDuplicates()
{
std::unordered_set<unsigned> control;
BinaryTree::Tree<unsigned> tree;
std::unordered_set<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithUniqueRandomNumbers(tree, control);
for(auto const & num : control)
{
if(!tree.Contains(num))
{
//std::printf("\tValue %u inserted but cannot be found!\n", num);
return false;
}
}
FillWithUniqueRandomNumbers(tree, control);
return true;
for(auto const & num: control)
{
if(!tree.Contains(num))
{
//std::printf("\tValue %u inserted but cannot be found!\n", num);
return false;
}
}
return true;
}
bool TestDeletion()
{
std::unordered_set<unsigned> control;
BinaryTree::Tree<unsigned> tree;
std::unordered_set<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithUniqueRandomNumbers(tree, control);
FillWithUniqueRandomNumbers(tree, control);
std::vector<unsigned> deletedValues;
unsigned const toDeleteCount = control.size() / 4;
for(unsigned i = 0u; i < toDeleteCount; ++i)
{
auto const valueTodelete = Util::GetRandomNumber();
std::vector<unsigned> deletedValues;
unsigned const toDeleteCount = control.size() / 4;
for(unsigned i = 0u; i < toDeleteCount; ++i)
{
auto const valueTodelete = Util::GetRandomNumber();
auto controlElement = control.find(valueTodelete);
if(controlElement != control.end())
{
control.erase(controlElement);
tree.Delete(valueTodelete);
deletedValues.push_back(valueTodelete);
}
}
for(auto const & num : control)
{
if(!tree.Contains(num))
{
std::puts("[ERROR] Tree does not contain a value that it should.");
//std::printf("Value %u inserted but cannot be found!\n", num);
return false;
}
}
auto controlElement = control.find(valueTodelete);
if(controlElement != control.end())
{
control.erase(controlElement);
tree.Delete(valueTodelete);
deletedValues.push_back(valueTodelete);
}
}
for(auto const & num : deletedValues)
{
if(tree.Contains(num))
{
std::puts("[ERROR] Tree contains deleted value that it should not.");
return false;
}
}
for(auto const & num: control)
{
if(!tree.Contains(num))
{
std::puts("[ERROR] Tree does not contain a value that it should.");
//std::printf("Value %u inserted but cannot be found!\n", num);
return false;
}
}
return true;
for(auto const & num: deletedValues)
{
if(tree.Contains(num))
{
std::puts("[ERROR] Tree contains deleted value that it should not.");
return false;
}
}
return true;
}
bool TestDeletionCase(
std::vector<unsigned> const toInsert,
std::vector<unsigned> const toDelete,
std::vector<unsigned> const result)
std::vector<unsigned> const toInsert,
std::vector<unsigned> const toDelete,
std::vector<unsigned> const result)
{
BinaryTree::Tree<unsigned> tree;
for(auto n : toInsert)
{
tree.Insert(n);
}
BinaryTree::Tree<unsigned> tree;
for(auto n: toInsert)
{
tree.Insert(n);
}
for(auto n : toDelete)
{
tree.Delete(n);
}
bool ok = true;
for(auto n : result)
{
if(!tree.Contains(n))
{
std::printf("[ERROR] Tree does not contain value %u.\n", n);
ok = false;
}
}
for(auto n: toDelete)
{
tree.Delete(n);
}
for(auto n : toDelete)
{
if(tree.Contains(n))
{
std::printf("[ERROR] Tree contains deleted value %u.\n", n);
ok = false;
}
}
bool ok = true;
for(auto n: result)
{
if(!tree.Contains(n))
{
std::printf("[ERROR] Tree does not contain value %u.\n", n);
ok = false;
}
}
return ok;
for(auto n: toDelete)
{
if(tree.Contains(n))
{
std::printf("[ERROR] Tree contains deleted value %u.\n", n);
ok = false;
}
}
return ok;
}
bool TestDeletionCases()
{
std::puts("[INFO] Testing deleting root with single child right.");
TestDeletionCase(
std::vector<unsigned> { 10, 12, 11, 13},
std::vector<unsigned> { 10 },
std::vector<unsigned> { 12, 11, 13 });
std::puts("[INFO] Testing deleting root with single child left.");
TestDeletionCase(
std::vector<unsigned> { 15, 12, 11, 13},
std::vector<unsigned> { 15 },
std::vector<unsigned> { 12, 11, 13 });
std::puts("[INFO] Testing deleting root with 2 children but no child right left.");
TestDeletionCase(
std::vector<unsigned> { 15, 10, 20, 22, 25, 18, 5},
std::vector<unsigned> { 20 },
std::vector<unsigned> { 15, 10, 22, 25, 18, 5 });
std::puts("[INFO] Testing deleting root with 2 children.");
TestDeletionCase(
std::vector<unsigned> { 50, 40, 60, 75, 55, 45, 42, 58 },
std::vector<unsigned> { 50 },
std::vector<unsigned> { 40, 60, 75, 55, 45, 42, 58 });
std::puts("[INFO] Testing deleting root with single child right.");
TestDeletionCase(
std::vector<unsigned> {10, 12, 11, 13},
std::vector<unsigned> {10},
std::vector<unsigned> {12, 11, 13});
return true;
std::puts("[INFO] Testing deleting root with single child left.");
TestDeletionCase(
std::vector<unsigned> {15, 12, 11, 13},
std::vector<unsigned> {15},
std::vector<unsigned> {12, 11, 13});
std::puts("[INFO] Testing deleting root with 2 children but no child right left.");
TestDeletionCase(
std::vector<unsigned> {15, 10, 20, 22, 25, 18, 5},
std::vector<unsigned> {20},
std::vector<unsigned> {15, 10, 22, 25, 18, 5});
std::puts("[INFO] Testing deleting root with 2 children.");
TestDeletionCase(
std::vector<unsigned> {50, 40, 60, 75, 55, 45, 42, 58},
std::vector<unsigned> {50},
std::vector<unsigned> {40, 60, 75, 55, 45, 42, 58});
return true;
}
int main()
{
Test::Execute(TestInsert, "Insertion and find test");
Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test");
Test::Execute(TestDeletion, "Insertion and deletion test");
Test::Execute(TestDeletionCases, "Deletion cases test");
Test::Execute(TestInsert, "Insertion and find test");
Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test");
Test::Execute(TestDeletion, "Insertion and deletion test");
Test::Execute(TestDeletionCases, "Deletion cases test");
return 0;
return 0;
}

View File

@@ -3,135 +3,135 @@
bool TestAppending()
{
unsigned const testSize = 5;
unsigned const testSize = 5;
std::vector<unsigned> truth;
List::List<unsigned> list;
std::vector<unsigned> truth;
List::List<unsigned> list;
truth.resize(testSize);
for(unsigned i = 0; i < testSize; ++i)
{
unsigned const value = Util::GetRandomNumber();
truth[i] = value;
list.Append(value);
}
truth.resize(testSize);
for(unsigned i = 0; i < testSize; ++i)
{
unsigned const value = Util::GetRandomNumber();
truth[i] = value;
list.Append(value);
}
for(unsigned i = 0; i < testSize; ++i)
{
if(truth[i] != list[i])
{
return false;
}
}
for(unsigned i = 0; i < testSize; ++i)
{
if(truth[i] != list[i])
{
return false;
}
}
return true;
return true;
}
bool TestPrepending()
{
unsigned const testSize = 5;
unsigned const testSize = 5;
std::vector<unsigned> truth;
List::List<unsigned> list;
std::vector<unsigned> truth;
List::List<unsigned> list;
truth.resize(testSize);
for(unsigned i = testSize; i > 0; --i)
{
unsigned const value = Util::GetRandomNumber();
truth[i - 1] = value;
list.Prepend(value);
}
truth.resize(testSize);
for(unsigned i = testSize; i > 0; --i)
{
unsigned const value = Util::GetRandomNumber();
truth[i - 1] = value;
list.Prepend(value);
}
for(unsigned i = 0; i < testSize; ++i)
{
if(truth[i] != list[i])
{
return false;
}
}
for(unsigned i = 0; i < testSize; ++i)
{
if(truth[i] != list[i])
{
return false;
}
}
return true;
return true;
}
bool TestPrependAppendMix()
{
std::vector<unsigned> numbers = { 5, 6, 4, 7, 3, 8, 2, 9, 1, 10 };
List::List<unsigned> list;
std::vector<unsigned> numbers = {5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
List::List<unsigned> list;
for(unsigned i = 0; i < numbers.size(); i += 2)
{
list.Prepend(numbers[i]);
list.Append(numbers[i + 1]);
}
for(unsigned i = 0; i < numbers.size(); i += 2)
{
list.Prepend(numbers[i]);
list.Append(numbers[i + 1]);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
if(list[i] != i + 1)
{
return false;
}
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
if(list[i] != i + 1)
{
return false;
}
}
return true;
return true;
}
bool TestDeletion()
{
List::List<unsigned> list;
List::List<unsigned> list;
for(unsigned i = 1; i < 8; ++i)
{
list.Append(i);
}
for(unsigned i = 1; i < 8; ++i)
{
list.Append(i);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
list.Delete(i);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
list.Delete(i);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
if(list[i] % 2 != 0)
{
return false;
}
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
if(list[i] % 2 != 0)
{
return false;
}
}
return true;
return true;
}
bool TestInsertion()
{
unsigned const testSize = 11;
List::List<unsigned> list;
unsigned const testSize = 11;
List::List<unsigned> list;
for(unsigned i = 1; i < testSize; i += 2)
{
list.Append(i);
}
for(unsigned i = 1; i < testSize; i += 2)
{
list.Append(i);
}
for(unsigned i = 1; i < testSize - 2; i += 2)
{
list.Insert(i + 1, i);
}
for(unsigned i = 1; i < testSize - 2; i += 2)
{
list.Insert(i + 1, i);
}
for(unsigned i = 1; i <= list.GetSize(); ++i)
{
if(list[i - 1] != i)
{
return false;
}
}
for(unsigned i = 1; i <= list.GetSize(); ++i)
{
if(list[i - 1] != i)
{
return false;
}
}
return true;
return true;
}
int main()
{
Test::Execute(TestAppending, "Appending test");
Test::Execute(TestPrepending, "Prepending test");
Test::Execute(TestPrependAppendMix, "Prepend append mix test");
Test::Execute(TestDeletion, "Deletion test");
Test::Execute(TestInsertion, "Insertion test");
return 0;
Test::Execute(TestAppending, "Appending test");
Test::Execute(TestPrepending, "Prepending test");
Test::Execute(TestPrependAppendMix, "Prepend append mix test");
Test::Execute(TestDeletion, "Deletion test");
Test::Execute(TestInsertion, "Insertion test");
return 0;
}

View File

@@ -3,58 +3,58 @@
bool TestInsertion()
{
std::size_t const testSize = 512u;
std::size_t const testSize = 512u;
std::vector<unsigned> truth;
truth.resize(testSize);
RingBuffer<unsigned> ringBuffer(512u);
std::vector<unsigned> truth;
truth.resize(testSize);
RingBuffer<unsigned> ringBuffer(512u);
for(std::size_t i = 0; i < testSize; ++i)
{
truth[i] = Util::GetRandomNumber();
ringBuffer.Push(truth[i]);
}
for(std::size_t i = 0; i < testSize; ++i)
{
truth[i] = Util::GetRandomNumber();
ringBuffer.Push(truth[i]);
}
for(std::size_t i = 0; i < testSize; ++i)
{
if(truth[i] != ringBuffer.Pop())
{
return false;
}
}
for(std::size_t i = 0; i < testSize; ++i)
{
if(truth[i] != ringBuffer.Pop())
{
return false;
}
}
return true;
return true;
}
bool TestWrapAround()
{
std::size_t const testSize = 32u;
std::size_t const testSize = 32u;
RingBuffer<unsigned> ringBuffer(testSize);
for(std::size_t i = 0; i < testSize / 2u; ++i)
{
ringBuffer.Push(42u);
}
RingBuffer<unsigned> ringBuffer(testSize);
for(std::size_t i = 0; i < testSize / 2u; ++i)
{
ringBuffer.Push(42u);
}
for(std::size_t i = 0; i < testSize; ++i)
{
ringBuffer.Push(i);
}
for(std::size_t i = 0; i < testSize; ++i)
{
ringBuffer.Push(i);
}
for(std::size_t i = 0; i < testSize; ++i)
{
if(ringBuffer.Pop() != i)
{
return false;
}
}
for(std::size_t i = 0; i < testSize; ++i)
{
if(ringBuffer.Pop() != i)
{
return false;
}
}
return true;
return true;
}
int main()
{
Test::Execute(TestInsertion, "Insertion test");
Test::Execute(TestWrapAround, "Wrap around test");
return 0;
Test::Execute(TestInsertion, "Insertion test");
Test::Execute(TestWrapAround, "Wrap around test");
return 0;
}

View File

@@ -4,32 +4,32 @@
bool TestStack()
{
std::size_t testSize = 512ul;
std::size_t testSize = 512ul;
std::stack<unsigned> truth;
Stack<unsigned> stack;
std::stack<unsigned> truth;
Stack<unsigned> stack;
for(std::size_t i = 0; i < testSize; ++i)
{
unsigned const toInsert = Util::GetRandomNumber();
truth.push(toInsert);
stack.Push(toInsert);
}
for(std::size_t i = 0; i < testSize; ++i)
{
unsigned const toInsert = Util::GetRandomNumber();
truth.push(toInsert);
stack.Push(toInsert);
}
while(!truth.empty())
{
if(truth.top() != stack.Pop())
{
return false;
}
truth.pop();
}
while(!truth.empty())
{
if(truth.top() != stack.Pop())
{
return false;
}
truth.pop();
}
return true;
return true;
}
int main()
{
Test::Execute(TestStack, "Pop push test");
return 0;
Test::Execute(TestStack, "Pop push test");
return 0;
}

View File

@@ -7,57 +7,57 @@
namespace Util
{
unsigned static GetRandomNumber()
{
static std::default_random_engine eng;
static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u);
unsigned static GetRandomNumber()
{
static std::default_random_engine eng;
static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u);
return valueDist(eng);
}
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)(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
{
std::printf("[FAIL] %s\n", message);
for(auto & issue : issues)
{
std::printf(" Issue: %s\n", issue.c_str());
}
}
}
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
{
std::printf("[FAIL] %s\n", message);
for(auto & issue: issues)
{
std::printf(" Issue: %s\n", issue.c_str());
}
}
}
catch(std::exception & e)
{
std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what());
}
}
};

View File

@@ -1,111 +1,110 @@
#include "../src/sequential/vector.hpp"
#include "testutil.hpp"
void FillWithSequentialNumbers(Vector<unsigned> & vector,
unsigned const count = 1024u)
void FillWithSequentialNumbers(Vector<unsigned> & vector, unsigned const count = 1024u)
{
for(unsigned i = 0; i < count; ++i)
{
vector[i] = i;
}
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;
}
unsigned const targetSize = 1024u;
Vector<unsigned> vector;
try
{
vector.Resize(targetSize);
}
catch(std::exception & e)
{
return false;
}
FillWithSequentialNumbers(vector, targetSize);
FillWithSequentialNumbers(vector, targetSize);
for(unsigned i = 0; i < vector.GetSize(); ++i)
{
if(vector[i] != i)
{
return false;
}
}
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;
}
try
{
vector[targetSize + 1] = 42;
}
catch(std::exception & e)
{
return true;
}
return false;
return false;
}
bool TestResize()
{
Vector<unsigned> vector;
Vector<unsigned> vector;
vector.Resize(2048u);
FillWithSequentialNumbers(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() / 2u);
for(unsigned i = 0; i < vector.GetSize(); ++i)
{
if(vector[i] != i)
{
return false;
}
}
vector.Resize(vector.GetSize() * 16u);
vector.Resize(vector.GetSize() * 16u);
return true;
return true;
}
bool TestReserve()
{
std::size_t const testSize = 8096ul;
std::size_t const testSize = 8096ul;
Vector<unsigned> vector;
vector.Reserve(testSize);
if(vector.GetReserveSize() != testSize)
{
std::puts("[ERROR] Reserve size reported not equal to reserve size set.");
return false;
}
Vector<unsigned> vector;
vector.Reserve(testSize);
if(vector.GetReserveSize() != testSize)
{
std::puts("[ERROR] Reserve size reported not equal to reserve size set.");
return false;
}
for(std::size_t i = 0; i < testSize; ++i)
{
vector.Resize(i + 1ul);
vector[i] = i;
for(std::size_t i = 0; i < testSize; ++i)
{
vector.Resize(i + 1ul);
vector[i] = i;
if(vector.GetReserveSize() != testSize)
{
std::puts("[ERROR] Reserve size changed during resize.");
return false;
}
}
if(vector.GetReserveSize() != testSize)
{
std::puts("[ERROR] Reserve size changed during resize.");
return false;
}
}
for(std::size_t i = 0; i < testSize; ++i)
{
if(vector[i] != i)
{
return false;
}
}
for(std::size_t i = 0; i < testSize; ++i)
{
if(vector[i] != i)
{
return false;
}
}
return true;
return true;
}
int main()
{
Test::Execute(TestInsertion, "Insertion test");
Test::Execute(TestResize, "Resize test");
Test::Execute(TestReserve, "Reserve test");
Test::Execute(TestInsertion, "Insertion test");
Test::Execute(TestResize, "Resize test");
Test::Execute(TestReserve, "Reserve test");
return 0;
return 0;
}