Format source files
This commit is contained in:
8
makefile
8
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 $@
|
||||
@@ -5,187 +5,174 @@
|
||||
namespace List
|
||||
{
|
||||
|
||||
template<class T>
|
||||
struct Node
|
||||
{
|
||||
T value;
|
||||
std::unique_ptr<Node<T>> next;
|
||||
template<class T> struct Node
|
||||
{
|
||||
T value;
|
||||
std::unique_ptr<Node<T>> next;
|
||||
|
||||
Node(T const & _value)
|
||||
: value(_value),
|
||||
next(nullptr)
|
||||
{}
|
||||
};
|
||||
Node(T const & _value) : value(_value), next(nullptr) { }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class List
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<Node<T>> root;
|
||||
Node<T> * tailPtr;
|
||||
std::size_t size;
|
||||
template<class T> class List {
|
||||
private:
|
||||
std::unique_ptr<Node<T>> root;
|
||||
Node<T> * tailPtr;
|
||||
std::size_t size;
|
||||
|
||||
public:
|
||||
void Append(T const & value)
|
||||
{
|
||||
if(size == 0)
|
||||
{
|
||||
root = std::make_unique<Node<T>>(value);
|
||||
tailPtr = root.get();
|
||||
++size;
|
||||
return;
|
||||
}
|
||||
public:
|
||||
void Append(T const & value)
|
||||
{
|
||||
if(size == 0)
|
||||
{
|
||||
root = std::make_unique<Node<T>>(value);
|
||||
tailPtr = root.get();
|
||||
++size;
|
||||
return;
|
||||
}
|
||||
|
||||
tailPtr->next = std::make_unique<Node<T>>(value);
|
||||
tailPtr = tailPtr->next.get();
|
||||
tailPtr->next = std::make_unique<Node<T>>(value);
|
||||
tailPtr = tailPtr->next.get();
|
||||
|
||||
++size;
|
||||
}
|
||||
++size;
|
||||
}
|
||||
|
||||
void Prepend(T const & value)
|
||||
{
|
||||
if(size == 0)
|
||||
{
|
||||
root = std::make_unique<Node<T>>(value);
|
||||
tailPtr = root.get();
|
||||
++size;
|
||||
return;
|
||||
}
|
||||
void Prepend(T const & value)
|
||||
{
|
||||
if(size == 0)
|
||||
{
|
||||
root = std::make_unique<Node<T>>(value);
|
||||
tailPtr = root.get();
|
||||
++size;
|
||||
return;
|
||||
}
|
||||
|
||||
auto newRoot = std::make_unique<Node<T>>(value);
|
||||
newRoot->next.swap(root);
|
||||
root.swap(newRoot);
|
||||
auto newRoot = std::make_unique<Node<T>>(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<T> * prevPtr = root.get();
|
||||
Node<T> * curPtr = root->next.get();
|
||||
std::size_t currentIndex = 1u;
|
||||
while(currentIndex < index)
|
||||
{
|
||||
prevPtr = curPtr;
|
||||
curPtr = curPtr->next.get();
|
||||
++currentIndex;
|
||||
}
|
||||
Node<T> * prevPtr = root.get();
|
||||
Node<T> * curPtr = root->next.get();
|
||||
std::size_t currentIndex = 1u;
|
||||
while(currentIndex < index)
|
||||
{
|
||||
prevPtr = curPtr;
|
||||
curPtr = curPtr->next.get();
|
||||
++currentIndex;
|
||||
}
|
||||
|
||||
auto newNode = std::make_unique<Node<T>>(value);
|
||||
newNode->next.swap(prevPtr->next);
|
||||
prevPtr->next.swap(newNode);
|
||||
}
|
||||
auto newNode = std::make_unique<Node<T>>(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<T> * 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<T> * 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<T> * prevPtr = root.get();
|
||||
Node<T> * curPtr = prevPtr->next.get();
|
||||
std::size_t currentIndex = 1u;
|
||||
while(currentIndex < index)
|
||||
{
|
||||
++currentIndex;
|
||||
prevPtr = curPtr;
|
||||
curPtr = curPtr->next.get();
|
||||
}
|
||||
Node<T> * prevPtr = root.get();
|
||||
Node<T> * 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<T> * currentNode = root.get();
|
||||
std::size_t currentIndex = 0u;
|
||||
while(currentIndex < index)
|
||||
{
|
||||
currentNode = currentNode->next.get();
|
||||
++currentIndex;
|
||||
}
|
||||
Node<T> * 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) { }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,76 +1,65 @@
|
||||
#pragma once
|
||||
#include "vector.hpp"
|
||||
|
||||
template<class T>
|
||||
class RingBuffer
|
||||
{
|
||||
template<class T> class RingBuffer {
|
||||
private:
|
||||
Vector<T> data;
|
||||
std::size_t head, tail;
|
||||
Vector<T> 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); }
|
||||
};
|
||||
@@ -1,46 +1,38 @@
|
||||
#pragma once
|
||||
#include "vector.hpp"
|
||||
|
||||
template<class T>
|
||||
class Stack
|
||||
{
|
||||
template<class T> class Stack {
|
||||
private:
|
||||
Vector<T> data;
|
||||
std::size_t actualSize;
|
||||
Vector<T> 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) { }
|
||||
};
|
||||
@@ -2,85 +2,73 @@
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
|
||||
template<class T>
|
||||
class Vector
|
||||
{
|
||||
template<class T> 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<T *>(result);
|
||||
}
|
||||
reserveSize = allocationSize;
|
||||
data = reinterpret_cast<T *>(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) { }
|
||||
};
|
||||
@@ -3,145 +3,126 @@
|
||||
|
||||
namespace BinaryTree
|
||||
{
|
||||
template <class T>
|
||||
struct Node
|
||||
{
|
||||
T value;
|
||||
template<class T> struct Node
|
||||
{
|
||||
T value;
|
||||
|
||||
std::unique_ptr<Node<T>> left;
|
||||
std::unique_ptr<Node<T>> right;
|
||||
std::unique_ptr<Node<T>> left;
|
||||
std::unique_ptr<Node<T>> right;
|
||||
|
||||
bool HasChildren() const
|
||||
{
|
||||
return static_cast<bool>(left) || static_cast<bool>(right);
|
||||
}
|
||||
bool HasChildren() const { return static_cast<bool>(left) || static_cast<bool>(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 T>
|
||||
class Tree
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<Node<T>> root;
|
||||
template<class T> class Tree {
|
||||
private:
|
||||
std::unique_ptr<Node<T>> root;
|
||||
|
||||
T ExtractSmallestLeaf(std::unique_ptr<Node<T>> &node)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *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<T>> & node)
|
||||
{
|
||||
std::unique_ptr<Node<T>> * 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<T>> & node)
|
||||
{
|
||||
if(!node || node->IsLeaf())
|
||||
{
|
||||
node.release();
|
||||
return;
|
||||
}
|
||||
void DeleteNode(std::unique_ptr<Node<T>> & 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<Node<T>> & FindNode(T const & value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *currentPtr = &root;
|
||||
while (*currentPtr && (*currentPtr)->value != value)
|
||||
{
|
||||
if ((*currentPtr)->value > value)
|
||||
{
|
||||
currentPtr = &((*currentPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPtr = &((*currentPtr)->right);
|
||||
}
|
||||
}
|
||||
return *currentPtr;
|
||||
}
|
||||
std::unique_ptr<Node<T>> & FindNode(T const & value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> * 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<Node<T>> *currentPtr = &root;
|
||||
while (*currentPtr)
|
||||
{
|
||||
if ((*currentPtr)->value > value)
|
||||
{
|
||||
currentPtr = &((*currentPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPtr = &((*currentPtr)->right);
|
||||
}
|
||||
}
|
||||
*currentPtr = std::make_unique<Node<T>>(value);
|
||||
}
|
||||
public:
|
||||
void Insert(T const & value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> * currentPtr = &root;
|
||||
while(*currentPtr)
|
||||
{
|
||||
if((*currentPtr)->value > value)
|
||||
{
|
||||
currentPtr = &((*currentPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPtr = &((*currentPtr)->right);
|
||||
}
|
||||
}
|
||||
*currentPtr = std::make_unique<Node<T>>(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<Node<T>>(value);
|
||||
}
|
||||
toInsertIn = std::make_unique<Node<T>>(value);
|
||||
}
|
||||
|
||||
bool Contains(T const & value)
|
||||
{
|
||||
return static_cast<bool>(FindNode(value));
|
||||
}
|
||||
bool Contains(T const & value) { return static_cast<bool>(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
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
};
|
||||
151
test/vector.cpp
151
test/vector.cpp
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user