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

@@ -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 $@

View File

@@ -5,27 +5,21 @@
namespace List
{
template<class T>
struct Node
{
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:
template<class T> class List {
private:
std::unique_ptr<Node<T>> root;
Node<T> * tailPtr;
std::size_t size;
public:
public:
void Append(T const & value)
{
if(size == 0)
@@ -177,15 +171,8 @@ public:
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) { }
};
}

View File

@@ -1,9 +1,7 @@
#pragma once
#include "vector.hpp"
template<class T>
class RingBuffer
{
template<class T> class RingBuffer {
private:
Vector<T> data;
std::size_t head, tail;
@@ -61,16 +59,7 @@ public:
}
}
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); }
};

View File

@@ -1,9 +1,7 @@
#pragma once
#include "vector.hpp"
template<class T>
class Stack
{
template<class T> class Stack {
private:
Vector<T> data;
std::size_t actualSize;
@@ -34,13 +32,7 @@ public:
return retval;
}
std::size_t GetSize() const
{
return actualSize;
}
std::size_t GetSize() const { return actualSize; }
Stack()
: actualSize(0)
{
}
Stack() : actualSize(0) { }
};

View File

@@ -2,9 +2,7 @@
#include <cstdlib>
#include <stdexcept>
template<class T>
class Vector
{
template<class T> class Vector {
private:
T * data;
std::size_t reserveSize;
@@ -38,7 +36,7 @@ private:
public:
void Resize(std::size_t const newSize)
{
if (newSize > reserveSize)
if(newSize > reserveSize)
{
Allocate(newSize);
size = newSize;
@@ -68,19 +66,9 @@ public:
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) { }
};

View File

@@ -3,42 +3,28 @@
namespace BinaryTree
{
template <class T>
struct Node
{
template<class T> struct Node
{
T value;
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:
template<class T> class Tree {
private:
std::unique_ptr<Node<T>> root;
T ExtractSmallestLeaf(std::unique_ptr<Node<T>> &node)
T ExtractSmallestLeaf(std::unique_ptr<Node<T>> & node)
{
std::unique_ptr<Node<T>> *nodePtr = &(node);
while ((*nodePtr)->HasChildren())
std::unique_ptr<Node<T>> * nodePtr = &(node);
while((*nodePtr)->HasChildren())
{
if((*nodePtr)->left)
{
@@ -85,10 +71,10 @@ private:
std::unique_ptr<Node<T>> & FindNode(T const & value)
{
std::unique_ptr<Node<T>> *currentPtr = &root;
while (*currentPtr && (*currentPtr)->value != value)
std::unique_ptr<Node<T>> * currentPtr = &root;
while(*currentPtr && (*currentPtr)->value != value)
{
if ((*currentPtr)->value > value)
if((*currentPtr)->value > value)
{
currentPtr = &((*currentPtr)->left);
}
@@ -100,13 +86,13 @@ private:
return *currentPtr;
}
public:
public:
void Insert(T const & value)
{
std::unique_ptr<Node<T>> *currentPtr = &root;
while (*currentPtr)
std::unique_ptr<Node<T>> * currentPtr = &root;
while(*currentPtr)
{
if ((*currentPtr)->value > value)
if((*currentPtr)->value > value)
{
currentPtr = &((*currentPtr)->left);
}
@@ -129,10 +115,7 @@ public:
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)
{
@@ -140,8 +123,6 @@ public:
DeleteNode(node);
}
Tree()
: root(nullptr)
{}
};
Tree() : root(nullptr) { }
};
} // namespace BinaryTree

View File

@@ -1,9 +1,10 @@
#include "../src/tree/binarytree.hpp"
#include "testutil.hpp"
#include <vector>
#include <unordered_set>
#include <vector>
void FillWithUniqueRandomNumbers(BinaryTree::Tree<unsigned> & tree,
void FillWithUniqueRandomNumbers(
BinaryTree::Tree<unsigned> & tree,
std::unordered_set<unsigned> & control,
unsigned const count = 10000u)
{
@@ -29,7 +30,7 @@ bool TestInsert()
tree.Insert(value);
}
for(auto const & num : control)
for(auto const & num: control)
{
if(!tree.Contains(num))
{
@@ -48,7 +49,7 @@ bool TestInsertNoDuplicates()
FillWithUniqueRandomNumbers(tree, control);
for(auto const & num : control)
for(auto const & num: control)
{
if(!tree.Contains(num))
{
@@ -82,7 +83,7 @@ bool TestDeletion()
}
}
for(auto const & num : control)
for(auto const & num: control)
{
if(!tree.Contains(num))
{
@@ -92,7 +93,7 @@ bool TestDeletion()
}
}
for(auto const & num : deletedValues)
for(auto const & num: deletedValues)
{
if(tree.Contains(num))
{
@@ -110,18 +111,18 @@ bool TestDeletionCase(
std::vector<unsigned> const result)
{
BinaryTree::Tree<unsigned> tree;
for(auto n : toInsert)
for(auto n: toInsert)
{
tree.Insert(n);
}
for(auto n : toDelete)
for(auto n: toDelete)
{
tree.Delete(n);
}
bool ok = true;
for(auto n : result)
for(auto n: result)
{
if(!tree.Contains(n))
{
@@ -130,7 +131,7 @@ bool TestDeletionCase(
}
}
for(auto n : toDelete)
for(auto n: toDelete)
{
if(tree.Contains(n))
{
@@ -146,27 +147,27 @@ 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::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::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::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::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;
}

View File

@@ -55,7 +55,7 @@ bool TestPrepending()
bool TestPrependAppendMix()
{
std::vector<unsigned> numbers = { 5, 6, 4, 7, 3, 8, 2, 9, 1, 10 };
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)

View File

@@ -7,19 +7,19 @@
namespace Util
{
unsigned static GetRandomNumber()
{
unsigned static GetRandomNumber()
{
static std::default_random_engine eng;
static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u);
return valueDist(eng);
}
}
};
namespace Test
{
void Execute(bool (*testFunction)(void), char const * const message)
{
void Execute(bool (*testFunction)(void), char const * const message)
{
try
{
if(testFunction())
@@ -35,10 +35,10 @@ void Execute(bool (*testFunction)(void), char const * const message)
{
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)
{
void Execute(bool (*testFunction)(std::vector<std::string> &), char const * const message)
{
std::vector<std::string> issues;
try
{
@@ -49,7 +49,7 @@ void Execute(bool (*testFunction)(std::vector<std::string> &), char const * cons
else
{
std::printf("[FAIL] %s\n", message);
for(auto & issue : issues)
for(auto & issue: issues)
{
std::printf(" Issue: %s\n", issue.c_str());
}
@@ -59,5 +59,5 @@ void Execute(bool (*testFunction)(std::vector<std::string> &), char const * cons
{
std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what());
}
}
}
};

View File

@@ -1,8 +1,7 @@
#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)
{