diff --git a/makefile b/makefile index f64cfcc..530efc5 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,7 @@ CFLAGS = -std=c++17 -Wall -g -Wextra CPPS = $(wildcard test/*.cpp) EXES = $(patsubst %.cpp, %.out, $(CPPS)) -.PHONY: all clean rebuild check +.PHONY: all clean rebuild check format all: $(EXES) @@ -19,5 +19,11 @@ EXESQUOTED = $(patsubst %, "%", $(EXES)) check: all ./execute-all.sh +format: + find src/ -name *.cpp -exec clang-format -i {} \; + find src/ -name *.hpp -exec clang-format -i {} \; + find test/ -name *.cpp -exec clang-format -i {} \; + find test/ -name *.hpp -exec clang-format -i {} \; + %.out: %.cpp $(CC) $(CFLAGS) $< -o $@ \ No newline at end of file diff --git a/src/sequential/linkedlist.hpp b/src/sequential/linkedlist.hpp index 330d741..bde51d1 100644 --- a/src/sequential/linkedlist.hpp +++ b/src/sequential/linkedlist.hpp @@ -5,187 +5,174 @@ namespace List { -template -struct Node -{ - T value; - std::unique_ptr> next; + template struct Node + { + T value; + std::unique_ptr> next; - Node(T const & _value) - : value(_value), - next(nullptr) - {} -}; + Node(T const & _value) : value(_value), next(nullptr) { } + }; -template -class List -{ -private: - std::unique_ptr> root; - Node * tailPtr; - std::size_t size; + template class List { + private: + std::unique_ptr> root; + Node * tailPtr; + std::size_t size; -public: - void Append(T const & value) - { - if(size == 0) - { - root = std::make_unique>(value); - tailPtr = root.get(); - ++size; - return; - } + public: + void Append(T const & value) + { + if(size == 0) + { + root = std::make_unique>(value); + tailPtr = root.get(); + ++size; + return; + } - tailPtr->next = std::make_unique>(value); - tailPtr = tailPtr->next.get(); + tailPtr->next = std::make_unique>(value); + tailPtr = tailPtr->next.get(); - ++size; - } + ++size; + } - void Prepend(T const & value) - { - if(size == 0) - { - root = std::make_unique>(value); - tailPtr = root.get(); - ++size; - return; - } + void Prepend(T const & value) + { + if(size == 0) + { + root = std::make_unique>(value); + tailPtr = root.get(); + ++size; + return; + } - auto newRoot = std::make_unique>(value); - newRoot->next.swap(root); - root.swap(newRoot); + auto newRoot = std::make_unique>(value); + newRoot->next.swap(root); + root.swap(newRoot); - ++size; - } + ++size; + } - void Insert(T const & value, std::size_t const index) - { - if(index >= size) - { - throw std::out_of_range("Index is greater or equal to size."); - } + void Insert(T const & value, std::size_t const index) + { + if(index >= size) + { + throw std::out_of_range("Index is greater or equal to size."); + } - if(index == 0u) - { - Prepend(value); - return; - } + if(index == 0u) + { + Prepend(value); + return; + } - ++size; + ++size; - Node * prevPtr = root.get(); - Node * curPtr = root->next.get(); - std::size_t currentIndex = 1u; - while(currentIndex < index) - { - prevPtr = curPtr; - curPtr = curPtr->next.get(); - ++currentIndex; - } + Node * prevPtr = root.get(); + Node * curPtr = root->next.get(); + std::size_t currentIndex = 1u; + while(currentIndex < index) + { + prevPtr = curPtr; + curPtr = curPtr->next.get(); + ++currentIndex; + } - auto newNode = std::make_unique>(value); - newNode->next.swap(prevPtr->next); - prevPtr->next.swap(newNode); - } + auto newNode = std::make_unique>(value); + newNode->next.swap(prevPtr->next); + prevPtr->next.swap(newNode); + } - void Delete(std::size_t const index) - { - if(index >= size) - { - throw std::out_of_range("Index is greater or equal to size."); - } + void Delete(std::size_t const index) + { + if(index >= size) + { + throw std::out_of_range("Index is greater or equal to size."); + } - --size; + --size; - if(index == 0u) - { - if(size == 0u) - { - root.release(); - tailPtr = nullptr; - } - else - { - root = std::move(root->next); - } - return; - } + if(index == 0u) + { + if(size == 0u) + { + root.release(); + tailPtr = nullptr; + } + else + { + root = std::move(root->next); + } + return; + } - // Is index last element? Note that we subtracted 1 from size above - if(index == size) - { - Node * curPtr = root.get(); - while(curPtr->next.get() != tailPtr) - { - curPtr = curPtr->next.get(); - } + // Is index last element? Note that we subtracted 1 from size above + if(index == size) + { + Node * curPtr = root.get(); + while(curPtr->next.get() != tailPtr) + { + curPtr = curPtr->next.get(); + } - tailPtr = curPtr; - tailPtr->next.release(); - return; - } + tailPtr = curPtr; + tailPtr->next.release(); + return; + } - Node * prevPtr = root.get(); - Node * curPtr = prevPtr->next.get(); - std::size_t currentIndex = 1u; - while(currentIndex < index) - { - ++currentIndex; - prevPtr = curPtr; - curPtr = curPtr->next.get(); - } + Node * prevPtr = root.get(); + Node * curPtr = prevPtr->next.get(); + std::size_t currentIndex = 1u; + while(currentIndex < index) + { + ++currentIndex; + prevPtr = curPtr; + curPtr = curPtr->next.get(); + } - prevPtr->next = std::move(curPtr->next); - } + prevPtr->next = std::move(curPtr->next); + } - T & Front() - { - if(size == 0u) - { - throw std::out_of_range("List is empty."); - } + T & Front() + { + if(size == 0u) + { + throw std::out_of_range("List is empty."); + } - return root->value; - } + return root->value; + } - T & Back() - { - if(size == 0u) - { - throw std::out_of_range("List is empty."); - } + T & Back() + { + if(size == 0u) + { + throw std::out_of_range("List is empty."); + } - return tailPtr->value; - } + return tailPtr->value; + } - T & operator[](std::size_t const index) - { - if(index >= size) - { - throw std::out_of_range("Index is greater or equal to size."); - } + T & operator[](std::size_t const index) + { + if(index >= size) + { + throw std::out_of_range("Index is greater or equal to size."); + } - Node * currentNode = root.get(); - std::size_t currentIndex = 0u; - while(currentIndex < index) - { - currentNode = currentNode->next.get(); - ++currentIndex; - } + Node * currentNode = root.get(); + std::size_t currentIndex = 0u; + while(currentIndex < index) + { + currentNode = currentNode->next.get(); + ++currentIndex; + } - return currentNode->value; - } + return currentNode->value; + } - std::size_t GetSize() const - { - return size; - } + std::size_t GetSize() const { return size; } - List() - : root(nullptr), - tailPtr(nullptr), - size(0) - {} -}; + List() : root(nullptr), tailPtr(nullptr), size(0) { } + }; } diff --git a/src/sequential/ringbuffer.hpp b/src/sequential/ringbuffer.hpp index 1039042..18c625b 100644 --- a/src/sequential/ringbuffer.hpp +++ b/src/sequential/ringbuffer.hpp @@ -1,76 +1,65 @@ #pragma once #include "vector.hpp" -template -class RingBuffer -{ +template class RingBuffer { private: - Vector data; - std::size_t head, tail; + Vector data; + std::size_t head, tail; - void AdvanceHead() - { - ++head; - if(head >= data.GetSize()) - { - head = 0; - } - } + void AdvanceHead() + { + ++head; + if(head >= data.GetSize()) + { + head = 0; + } + } - void AdvanceTail() - { - ++tail; - if(tail >= data.GetSize()) - { - tail = 0; - } - if(tail == head) - { - AdvanceHead(); - } - } + void AdvanceTail() + { + ++tail; + if(tail >= data.GetSize()) + { + tail = 0; + } + if(tail == head) + { + AdvanceHead(); + } + } public: - void Push(T const & value) - { - data[tail] = value; - AdvanceTail(); - } + void Push(T const & value) + { + data[tail] = value; + AdvanceTail(); + } - T Pop() - { - if(head == tail) - { - throw std::out_of_range("Cannot retrieve value when size is 0"); - } + T Pop() + { + if(head == tail) + { + throw std::out_of_range("Cannot retrieve value when size is 0"); + } - T const & toReturn = data[head]; - AdvanceHead(); - return toReturn; - } + T const & toReturn = data[head]; + AdvanceHead(); + return toReturn; + } - std::size_t GetSize() const - { - if(head <= tail) - { - return tail - head; - } - else - { - return tail + (data.GetSize() - head); - } - } + std::size_t GetSize() const + { + if(head <= tail) + { + return tail - head; + } + else + { + return tail + (data.GetSize() - head); + } + } - bool IsEmpty() const - { - return head == tail; - } + bool IsEmpty() const { return head == tail; } - RingBuffer(std::size_t const size) - : data(), - head(0), - tail(0) - { - data.Resize(size + 1ul); - } + RingBuffer(std::size_t const size) : data(), head(0), tail(0) { data.Resize(size + 1ul); } }; \ No newline at end of file diff --git a/src/sequential/stack.hpp b/src/sequential/stack.hpp index 073af24..b29b97e 100644 --- a/src/sequential/stack.hpp +++ b/src/sequential/stack.hpp @@ -1,46 +1,38 @@ #pragma once #include "vector.hpp" -template -class Stack -{ +template class Stack { private: - Vector data; - std::size_t actualSize; + Vector data; + std::size_t actualSize; public: - void Push(T const & value) - { - ++actualSize; - if(actualSize > data.GetSize()) - { - data.Resize(actualSize); - } + void Push(T const & value) + { + ++actualSize; + if(actualSize > data.GetSize()) + { + data.Resize(actualSize); + } - data[actualSize - 1ul] = value; - } + data[actualSize - 1ul] = value; + } - T Pop() - { - if(actualSize == 0ul) - { - throw std::out_of_range("Cannot pop an empty stack."); - } - auto const retval = data[actualSize - 1ul]; - - --actualSize; - data.Resize(actualSize); + T Pop() + { + if(actualSize == 0ul) + { + throw std::out_of_range("Cannot pop an empty stack."); + } + auto const retval = data[actualSize - 1ul]; - return retval; - } + --actualSize; + data.Resize(actualSize); - std::size_t GetSize() const - { - return actualSize; - } + return retval; + } - Stack() - : actualSize(0) - { - } + std::size_t GetSize() const { return actualSize; } + + Stack() : actualSize(0) { } }; \ No newline at end of file diff --git a/src/sequential/vector.hpp b/src/sequential/vector.hpp index 8bb352b..8c5e480 100644 --- a/src/sequential/vector.hpp +++ b/src/sequential/vector.hpp @@ -2,85 +2,73 @@ #include #include -template -class Vector -{ +template class Vector { private: - T * data; - std::size_t reserveSize; - std::size_t size; + T * data; + std::size_t reserveSize; + std::size_t size; - void Allocate(std::size_t const allocationSize) - { - if(allocationSize == 0ul) - { - std::free(data); - data = nullptr; - reserveSize = 0ul; - return; - } + void Allocate(std::size_t const allocationSize) + { + if(allocationSize == 0ul) + { + std::free(data); + data = nullptr; + reserveSize = 0ul; + return; + } - if(allocationSize == size) - { - return; - } + if(allocationSize == size) + { + return; + } - void * result = std::realloc(data, sizeof(T) * allocationSize); - if(result == nullptr) - { - throw std::runtime_error("Cannot allocate the requested size of memory."); - } + void * result = std::realloc(data, sizeof(T) * allocationSize); + if(result == nullptr) + { + throw std::runtime_error("Cannot allocate the requested size of memory."); + } - reserveSize = allocationSize; - data = reinterpret_cast(result); - } + reserveSize = allocationSize; + data = reinterpret_cast(result); + } public: - void Resize(std::size_t const newSize) - { - if (newSize > reserveSize) - { - Allocate(newSize); - size = newSize; - return; - } + void Resize(std::size_t const newSize) + { + if(newSize > reserveSize) + { + Allocate(newSize); + size = newSize; + return; + } - size = newSize; - } + size = newSize; + } - void Reserve(std::size_t const newReserveSize) - { - if(newReserveSize < size) - { - Allocate(size); - return; - } + void Reserve(std::size_t const newReserveSize) + { + if(newReserveSize < size) + { + Allocate(size); + return; + } - Allocate(newReserveSize); - } + Allocate(newReserveSize); + } - T & operator[](std::size_t const index) - { - if(index >= size) - { - throw std::out_of_range("Index is greater or equal to size."); - } - return data[index]; - } + T & operator[](std::size_t const index) + { + if(index >= size) + { + throw std::out_of_range("Index is greater or equal to size."); + } + return data[index]; + } - std::size_t GetSize() const - { - return size; - } + std::size_t GetSize() const { return size; } - std::size_t GetReserveSize() const - { - return reserveSize; - } + std::size_t GetReserveSize() const { return reserveSize; } - Vector() - : data(nullptr), - reserveSize(0ul), - size(0ul) - {} + Vector() : data(nullptr), reserveSize(0ul), size(0ul) { } }; \ No newline at end of file diff --git a/src/tree/binarytree.hpp b/src/tree/binarytree.hpp index f3dc268..1003e81 100644 --- a/src/tree/binarytree.hpp +++ b/src/tree/binarytree.hpp @@ -3,145 +3,126 @@ namespace BinaryTree { -template -struct Node -{ - T value; + template struct Node + { + T value; - std::unique_ptr> left; - std::unique_ptr> right; + std::unique_ptr> left; + std::unique_ptr> right; - bool HasChildren() const - { - return static_cast(left) || static_cast(right); - } + bool HasChildren() const { return static_cast(left) || static_cast(right); } - bool IsLeaf() const - { - return !HasChildren(); - } + bool IsLeaf() const { return !HasChildren(); } - Node(T const &_value) - : value(_value), - left(nullptr), - right(nullptr) - { - } -}; + Node(T const & _value) : value(_value), left(nullptr), right(nullptr) { } + }; -template -class Tree -{ -private: - std::unique_ptr> root; + template class Tree { + private: + std::unique_ptr> root; - T ExtractSmallestLeaf(std::unique_ptr> &node) - { - std::unique_ptr> *nodePtr = &(node); - while ((*nodePtr)->HasChildren()) - { - if((*nodePtr)->left) - { - nodePtr = &((*nodePtr)->left); - } - else - { - T value = (*nodePtr)->value; - (*nodePtr) = std::move((*nodePtr)->right); - return value; - } - } + T ExtractSmallestLeaf(std::unique_ptr> & node) + { + std::unique_ptr> * nodePtr = &(node); + while((*nodePtr)->HasChildren()) + { + if((*nodePtr)->left) + { + nodePtr = &((*nodePtr)->left); + } + else + { + T value = (*nodePtr)->value; + (*nodePtr) = std::move((*nodePtr)->right); + return value; + } + } - T value = (*nodePtr)->value; - (*nodePtr).release(); - return value; - } + T value = (*nodePtr)->value; + (*nodePtr).release(); + return value; + } - void DeleteNode(std::unique_ptr> & node) - { - if(!node || node->IsLeaf()) - { - node.release(); - return; - } + void DeleteNode(std::unique_ptr> & node) + { + if(!node || node->IsLeaf()) + { + node.release(); + return; + } - if(!(node->left)) - { - node = std::move(node->right); - return; - } + if(!(node->left)) + { + node = std::move(node->right); + return; + } - if(!(node->right)) - { - node = std::move(node->left); - return; - } + if(!(node->right)) + { + node = std::move(node->left); + return; + } - // In order successor + // In order successor - auto newNodeValue = ExtractSmallestLeaf(node->right); - node->value = newNodeValue; - } + auto newNodeValue = ExtractSmallestLeaf(node->right); + node->value = newNodeValue; + } - std::unique_ptr> & FindNode(T const & value) - { - std::unique_ptr> *currentPtr = &root; - while (*currentPtr && (*currentPtr)->value != value) - { - if ((*currentPtr)->value > value) - { - currentPtr = &((*currentPtr)->left); - } - else - { - currentPtr = &((*currentPtr)->right); - } - } - return *currentPtr; - } + std::unique_ptr> & FindNode(T const & value) + { + std::unique_ptr> * currentPtr = &root; + while(*currentPtr && (*currentPtr)->value != value) + { + if((*currentPtr)->value > value) + { + currentPtr = &((*currentPtr)->left); + } + else + { + currentPtr = &((*currentPtr)->right); + } + } + return *currentPtr; + } -public: - void Insert(T const & value) - { - std::unique_ptr> *currentPtr = &root; - while (*currentPtr) - { - if ((*currentPtr)->value > value) - { - currentPtr = &((*currentPtr)->left); - } - else - { - currentPtr = &((*currentPtr)->right); - } - } - *currentPtr = std::make_unique>(value); - } + public: + void Insert(T const & value) + { + std::unique_ptr> * currentPtr = &root; + while(*currentPtr) + { + if((*currentPtr)->value > value) + { + currentPtr = &((*currentPtr)->left); + } + else + { + currentPtr = &((*currentPtr)->right); + } + } + *currentPtr = std::make_unique>(value); + } - void InsertNoDuplicates(T const & value) - { - auto & toInsertIn = FindNode(value); - if(toInsertIn) - { - return; - } + void InsertNoDuplicates(T const & value) + { + auto & toInsertIn = FindNode(value); + if(toInsertIn) + { + return; + } - toInsertIn = std::make_unique>(value); - } + toInsertIn = std::make_unique>(value); + } - bool Contains(T const & value) - { - return static_cast(FindNode(value)); - } + bool Contains(T const & value) { return static_cast(FindNode(value)); } - void Delete(T const & value) - { - auto & node = FindNode(value); - DeleteNode(node); - } + void Delete(T const & value) + { + auto & node = FindNode(value); + DeleteNode(node); + } - Tree() - : root(nullptr) - {} -}; + Tree() : root(nullptr) { } + }; } // namespace BinaryTree \ No newline at end of file diff --git a/test/binarytree.cpp b/test/binarytree.cpp index d703bc2..d727945 100644 --- a/test/binarytree.cpp +++ b/test/binarytree.cpp @@ -1,182 +1,183 @@ #include "../src/tree/binarytree.hpp" #include "testutil.hpp" -#include #include +#include -void FillWithUniqueRandomNumbers(BinaryTree::Tree & tree, - std::unordered_set & control, - unsigned const count = 10000u) +void FillWithUniqueRandomNumbers( + BinaryTree::Tree & tree, + std::unordered_set & 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 control; - BinaryTree::Tree tree; + std::vector control; + BinaryTree::Tree 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 control; - BinaryTree::Tree tree; + std::unordered_set control; + BinaryTree::Tree 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 control; - BinaryTree::Tree tree; + std::unordered_set control; + BinaryTree::Tree tree; - FillWithUniqueRandomNumbers(tree, control); + FillWithUniqueRandomNumbers(tree, control); - std::vector deletedValues; - unsigned const toDeleteCount = control.size() / 4; - for(unsigned i = 0u; i < toDeleteCount; ++i) - { - auto const valueTodelete = Util::GetRandomNumber(); + std::vector 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 const toInsert, - std::vector const toDelete, - std::vector const result) + std::vector const toInsert, + std::vector const toDelete, + std::vector const result) { - BinaryTree::Tree tree; - for(auto n : toInsert) - { - tree.Insert(n); - } + BinaryTree::Tree 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 { 10, 12, 11, 13}, - std::vector { 10 }, - std::vector { 12, 11, 13 }); - - std::puts("[INFO] Testing deleting root with single child left."); - TestDeletionCase( - std::vector { 15, 12, 11, 13}, - std::vector { 15 }, - std::vector { 12, 11, 13 }); - - std::puts("[INFO] Testing deleting root with 2 children but no child right left."); - TestDeletionCase( - std::vector { 15, 10, 20, 22, 25, 18, 5}, - std::vector { 20 }, - std::vector { 15, 10, 22, 25, 18, 5 }); - - std::puts("[INFO] Testing deleting root with 2 children."); - TestDeletionCase( - std::vector { 50, 40, 60, 75, 55, 45, 42, 58 }, - std::vector { 50 }, - std::vector { 40, 60, 75, 55, 45, 42, 58 }); + std::puts("[INFO] Testing deleting root with single child right."); + TestDeletionCase( + std::vector {10, 12, 11, 13}, + std::vector {10}, + std::vector {12, 11, 13}); - return true; + std::puts("[INFO] Testing deleting root with single child left."); + TestDeletionCase( + std::vector {15, 12, 11, 13}, + std::vector {15}, + std::vector {12, 11, 13}); + + std::puts("[INFO] Testing deleting root with 2 children but no child right left."); + TestDeletionCase( + std::vector {15, 10, 20, 22, 25, 18, 5}, + std::vector {20}, + std::vector {15, 10, 22, 25, 18, 5}); + + std::puts("[INFO] Testing deleting root with 2 children."); + TestDeletionCase( + std::vector {50, 40, 60, 75, 55, 45, 42, 58}, + std::vector {50}, + std::vector {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; } \ No newline at end of file diff --git a/test/linkedlist.cpp b/test/linkedlist.cpp index 0b2efef..8182d6d 100644 --- a/test/linkedlist.cpp +++ b/test/linkedlist.cpp @@ -3,135 +3,135 @@ bool TestAppending() { - unsigned const testSize = 5; + unsigned const testSize = 5; - std::vector truth; - List::List list; + std::vector truth; + List::List 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 truth; - List::List list; + std::vector truth; + List::List 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 numbers = { 5, 6, 4, 7, 3, 8, 2, 9, 1, 10 }; - List::List list; + std::vector numbers = {5, 6, 4, 7, 3, 8, 2, 9, 1, 10}; + List::List 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 list; + List::List 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 list; + unsigned const testSize = 11; + List::List 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; } \ No newline at end of file diff --git a/test/ringbuffer.cpp b/test/ringbuffer.cpp index b645c16..d01f166 100644 --- a/test/ringbuffer.cpp +++ b/test/ringbuffer.cpp @@ -3,58 +3,58 @@ bool TestInsertion() { - std::size_t const testSize = 512u; + std::size_t const testSize = 512u; - std::vector truth; - truth.resize(testSize); - RingBuffer ringBuffer(512u); + std::vector truth; + truth.resize(testSize); + RingBuffer 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 ringBuffer(testSize); - for(std::size_t i = 0; i < testSize / 2u; ++i) - { - ringBuffer.Push(42u); - } + RingBuffer 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; } \ No newline at end of file diff --git a/test/stack.cpp b/test/stack.cpp index 9a901d7..25c8dd0 100644 --- a/test/stack.cpp +++ b/test/stack.cpp @@ -4,32 +4,32 @@ bool TestStack() { - std::size_t testSize = 512ul; + std::size_t testSize = 512ul; - std::stack truth; - Stack stack; + std::stack truth; + Stack 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; } \ No newline at end of file diff --git a/test/testutil.hpp b/test/testutil.hpp index 82e5107..65abdcb 100644 --- a/test/testutil.hpp +++ b/test/testutil.hpp @@ -7,57 +7,57 @@ namespace Util { -unsigned static GetRandomNumber() -{ - static std::default_random_engine eng; - static std::uniform_int_distribution valueDist(0u, 8096u); + unsigned static GetRandomNumber() + { + static std::default_random_engine eng; + static std::uniform_int_distribution 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 &), char const * const message) -{ - std::vector 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 &), char const * const message) + { + std::vector 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()); + } + } }; \ No newline at end of file diff --git a/test/vector.cpp b/test/vector.cpp index f75b16c..2c537e7 100644 --- a/test/vector.cpp +++ b/test/vector.cpp @@ -1,111 +1,110 @@ #include "../src/sequential/vector.hpp" #include "testutil.hpp" -void FillWithSequentialNumbers(Vector & vector, - unsigned const count = 1024u) +void FillWithSequentialNumbers(Vector & 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 vector; - try - { - vector.Resize(targetSize); - } - catch(std::exception & e) - { - return false; - } + unsigned const targetSize = 1024u; + Vector 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 vector; + Vector 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 vector; - vector.Reserve(testSize); - if(vector.GetReserveSize() != testSize) - { - std::puts("[ERROR] Reserve size reported not equal to reserve size set."); - return false; - } + Vector 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; } \ No newline at end of file