251 lines
5.4 KiB
C++
251 lines
5.4 KiB
C++
//
|
|
// Created by erki on 13.03.21.
|
|
//
|
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
#include "utility_ringbuffer.hpp"
|
|
|
|
template<size_t N>
|
|
using Ringbuffer = Utility::Ringbuffer<int, N>;
|
|
|
|
TEST_CASE("Ringbuffer iterator", "[ringbuffer],[iterator]")
|
|
{
|
|
using iterator = Ringbuffer<10>::iterator;
|
|
const auto begin = iterator::pointer(10);
|
|
const auto end = iterator::pointer(21);
|
|
iterator it = iterator(iterator::pointer(15), begin, end);
|
|
|
|
SECTION("Incrementing postfix increases its pointer and returns initial.")
|
|
{
|
|
iterator it_second = it++;
|
|
REQUIRE(it - it_second == iterator::difference_type(1));
|
|
}
|
|
|
|
SECTION("Incrementing prefix increases its pointer and returns new.")
|
|
{
|
|
const iterator original = it;
|
|
iterator it_second = ++it;
|
|
REQUIRE(it_second == it);
|
|
REQUIRE(it - original == iterator::difference_type(1));
|
|
}
|
|
|
|
SECTION("Subtracting 1 decreases its pointer.")
|
|
{
|
|
iterator it_second = it - 1;
|
|
REQUIRE(it - it_second == iterator::difference_type(1));
|
|
}
|
|
|
|
SECTION("Adding 1 increases its pointer.")
|
|
{
|
|
iterator it_second = it + 1;
|
|
REQUIRE(it_second - it == iterator::difference_type(1));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Ringbuffer iterator at the end", "[ringbuffer],[iterator]")
|
|
{
|
|
using iterator = Ringbuffer<10>::iterator;
|
|
const auto begin = iterator::pointer(10);
|
|
const auto end = iterator::pointer(21);
|
|
iterator it = iterator(end - 1, begin, end);
|
|
|
|
const auto it_begin = iterator(begin, begin, end);
|
|
|
|
SECTION("Incrementing postfix sets the pointer to begin().")
|
|
{
|
|
it++;
|
|
REQUIRE(it == it_begin);
|
|
}
|
|
|
|
SECTION("Incrementing prefix sets the pointer to begin().")
|
|
{
|
|
++it;
|
|
REQUIRE(it == it_begin);
|
|
}
|
|
|
|
SECTION("Adding 1 sets the pointer to begin().")
|
|
{
|
|
const iterator it_second = it + 1;
|
|
REQUIRE(it_second == it_begin);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Ringbuffer iterator at the beginning", "[ringbuffer],[iterator]")
|
|
{
|
|
using iterator = Ringbuffer<10>::iterator;
|
|
const auto begin = iterator::pointer(10);
|
|
const auto end = iterator::pointer(21);
|
|
iterator it = iterator(begin, begin, end);
|
|
|
|
const auto it_last = iterator(end - 1, begin, end);
|
|
|
|
SECTION("Subtracting 1 sets the pointer to last element.")
|
|
{
|
|
const iterator it_second = it - 1;
|
|
REQUIRE(it_last == it_second);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Constructed buffer is empty.", "[ringbuffer]")
|
|
{
|
|
Ringbuffer<10> buffer;
|
|
|
|
REQUIRE(buffer.begin() == buffer.end());
|
|
REQUIRE(buffer.size() == 0);
|
|
REQUIRE(buffer.empty());
|
|
}
|
|
|
|
TEST_CASE("Adding single element.", "[ringbuffer]")
|
|
{
|
|
Ringbuffer<10> buffer;
|
|
const auto old_end = buffer.end();
|
|
const auto old_begin = buffer.begin();
|
|
|
|
buffer.push_back(1);
|
|
|
|
SECTION("Increases size and empty appropriately.")
|
|
{
|
|
REQUIRE(buffer.size() == 1);
|
|
REQUIRE(!buffer.empty());
|
|
}
|
|
|
|
SECTION("Updates end() appropriately.")
|
|
{
|
|
REQUIRE(old_end != buffer.end());
|
|
}
|
|
|
|
SECTION("begin() remains the same.")
|
|
{
|
|
REQUIRE(old_begin == buffer.begin());
|
|
}
|
|
|
|
SECTION("Makes begin() refer to the inserted member.")
|
|
{
|
|
REQUIRE(*old_begin == 1);
|
|
REQUIRE(*buffer.begin() == 1);
|
|
}
|
|
|
|
SECTION("Distance between begin and end is 1.")
|
|
{
|
|
REQUIRE(buffer.end() - buffer.begin() == 1);
|
|
}
|
|
|
|
SECTION("Makes back() refer to the first element.")
|
|
{
|
|
REQUIRE(buffer.back() == 1);
|
|
}
|
|
|
|
SECTION("Makes front() refer to the first element.")
|
|
{
|
|
REQUIRE(buffer.front() == 1);
|
|
}
|
|
|
|
SECTION("Makes front() and back() refer to the same memory address.")
|
|
{
|
|
REQUIRE(&buffer.front() == &buffer.back());
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Adding multiple elements.", "[ringbuffer]")
|
|
{
|
|
Ringbuffer<10> buffer;
|
|
const auto old_begin = buffer.begin();
|
|
|
|
buffer.push_back(1);
|
|
buffer.push_back(2);
|
|
buffer.push_back(3);
|
|
|
|
SECTION("Increases size and empty appropriately.")
|
|
{
|
|
REQUIRE(buffer.size() == 3);
|
|
REQUIRE(!buffer.empty());
|
|
}
|
|
|
|
SECTION("Makes front() refer to the first element.")
|
|
{
|
|
REQUIRE(buffer.front() == 1);
|
|
}
|
|
|
|
SECTION("Makes back() refer to the last element.")
|
|
{
|
|
REQUIRE(buffer.back() == 3);
|
|
}
|
|
|
|
SECTION("Updates begin() and end() appropriately.")
|
|
{
|
|
REQUIRE(old_begin == buffer.begin());
|
|
REQUIRE(buffer.end() - buffer.begin() == 3);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Removing elements from the ringbuffer.", "[ringbuffer]")
|
|
{
|
|
Ringbuffer<10> buffer;
|
|
const auto old_begin = buffer.begin();
|
|
|
|
buffer.push_back(1);
|
|
buffer.push_back(2);
|
|
buffer.push_back(3);
|
|
|
|
buffer.pop_front();
|
|
|
|
SECTION("Updates the front() appropriately.")
|
|
{
|
|
REQUIRE(buffer.front() == 2);
|
|
}
|
|
|
|
SECTION("Updates begin() appropriately.")
|
|
{
|
|
REQUIRE(buffer.begin() - old_begin == 1);
|
|
}
|
|
|
|
SECTION("Updates size() appropriately.")
|
|
{
|
|
REQUIRE(buffer.size() == 2);
|
|
}
|
|
|
|
SECTION("Erasing remaining elements")
|
|
{
|
|
buffer.pop_front();
|
|
buffer.pop_front();
|
|
|
|
SECTION("Updates empty() appropriately.")
|
|
{
|
|
REQUIRE(buffer.empty());
|
|
}
|
|
|
|
SECTION("Updates begin() and end() appropriately.")
|
|
{
|
|
REQUIRE(buffer.begin() == buffer.end());
|
|
}
|
|
|
|
SECTION("Updates size() appropriately.")
|
|
{
|
|
REQUIRE(buffer.size() == 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Clearing a ringbuffer works.", "[ringbuffer]")
|
|
{
|
|
Ringbuffer<10> buffer;
|
|
|
|
buffer.push_back(1);
|
|
buffer.push_back(2);
|
|
buffer.push_back(3);
|
|
|
|
buffer.clear();
|
|
|
|
SECTION("Updates size() and empty() appropriately.")
|
|
{
|
|
REQUIRE(buffer.empty());
|
|
REQUIRE(buffer.size() == 0);
|
|
}
|
|
|
|
SECTION("Sets begin() and end() pointers appropriately.")
|
|
{
|
|
REQUIRE(buffer.begin() == buffer.end());
|
|
}
|
|
}
|