From 2c726f144f4182e38fe17bc9d45cd003a05af565 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Sun, 28 Apr 2019 19:48:48 +0200 Subject: [PATCH] Vector tests added --- tests/binarytree.cpp | 13 ++-------- tests/testutil.hpp | 28 ++++++++++++++++++--- tests/vector.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 tests/vector.cpp diff --git a/tests/binarytree.cpp b/tests/binarytree.cpp index 71ba9fa..44a7c95 100644 --- a/tests/binarytree.cpp +++ b/tests/binarytree.cpp @@ -1,23 +1,14 @@ #include "../binary-trees/binarytree.hpp" -#include #include "testutil.hpp" #include -unsigned static GetRandomNumber() -{ - static std::default_random_engine eng; - static std::uniform_int_distribution valueDist(0u, 8096u); - - return valueDist(eng); -} - void FillWithRandomNumbers(BinaryTree::Tree & tree, std::unordered_set & 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()) { diff --git a/tests/testutil.hpp b/tests/testutil.hpp index 04fe8eb..ac02174 100644 --- a/tests/testutil.hpp +++ b/tests/testutil.hpp @@ -1,16 +1,36 @@ #include +#include +#include + +namespace Util +{ +unsigned static GetRandomNumber() +{ + static std::default_random_engine eng; + static std::uniform_int_distribution 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()); } } }; \ No newline at end of file diff --git a/tests/vector.cpp b/tests/vector.cpp new file mode 100644 index 0000000..7e2bc97 --- /dev/null +++ b/tests/vector.cpp @@ -0,0 +1,59 @@ +#include "../random-access-containers/vector.hpp" +#include "testutil.hpp" + +void FillWithSequentialNumbers(Vector & vector, + unsigned const count = 1024u) +{ + for(unsigned i = 0; i < count; ++i) + { + vector[i] = i; + } +} + +bool TestInsertion() +{ + unsigned const targetSize = 1024u; + Vector 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; +} \ No newline at end of file