Skip to content

Commit

Permalink
Rename ringbuffer to ringarray
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehue committed Aug 24, 2024
1 parent be020c1 commit 5646472
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
10 changes: 8 additions & 2 deletions Sts1CobcSw/Periphery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ target_link_libraries(Sts1CobcSw_Periphery PUBLIC rodos::rodos Sts1CobcSw_Serial

if(CMAKE_SYSTEM_NAME STREQUAL Generic)
target_sources(
Sts1CobcSw_Periphery PRIVATE PersistentState.cpp Flash.cpp Fram.cpp Rf.cpp Eps.cpp
FramEpsSpi.cpp
Sts1CobcSw_Periphery
PRIVATE PersistentState.cpp
Flash.cpp
Fram.cpp
Rf.cpp
Eps.cpp
FramEpsSpi.cpp
FramRingArray.cpp
)
target_link_libraries(Sts1CobcSw_Periphery PRIVATE Sts1CobcSw_Utility)
target_link_libraries(Sts1CobcSw_Periphery PUBLIC Sts1CobcSw_Hal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
namespace sts1cobcsw::fram
{
template<typename T, std::size_t size, Address startAddress>
class RingBuffer
class RingArray
{
public:
RingBuffer() : bufferSize_(size + 1U), offset_(sizeof(std::size_t) * 2)
RingArray() : bufferSize_(size + 1U), offset_(sizeof(std::size_t) * 2)
{
Initialize();
};
Expand All @@ -32,13 +32,13 @@ class RingBuffer
auto Front() -> T;
auto Back() -> T;

//! @brief Returns the current size of the ringbuffer
//! @brief Returns the current size of the ring array
auto Size() -> std::size_t;

//! @brief Returns the capacity of the ringbuffer
//! @brief Returns the capacity of the ring array
auto Capacity() -> std::size_t;

// @brief Initializes the ringbuffer by reading indices from FRAM
// @brief Initializes the ring array by reading indices from FRAM
auto Initialize() -> void;

private:
Expand All @@ -53,4 +53,4 @@ class RingBuffer
}


#include <Sts1CobcSw/Periphery/FramRingBuffer.ipp> // IWYU pragma: keep
#include <Sts1CobcSw/Periphery/FramRingArray.ipp> // IWYU pragma: keep
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once


#include <Sts1CobcSw/Periphery/FramRingBuffer.hpp>
#include <Sts1CobcSw/Periphery/FramRingArray.hpp>


namespace sts1cobcsw::fram
{
template<typename T, std::size_t size, Address startAddress>
void RingBuffer<T, size, startAddress>::Push(T const & newData)
void RingArray<T, size, startAddress>::Push(T const & newData)
{
auto const rawAddress = offset_ + value_of(startAddress) + (iEnd_ * serialSize<T>);
fram::WriteTo(fram::Address(rawAddress), Span(Serialize(newData)), 0);
Expand All @@ -23,7 +23,7 @@ void RingBuffer<T, size, startAddress>::Push(T const & newData)


template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::Front() -> T
auto RingArray<T, size, startAddress>::Front() -> T
{
auto const rawAddress = offset_ + value_of(startAddress) + (iBegin_ * serialSize<T>);
auto readData = fram::ReadFrom<serialSize<T>>(fram::Address(rawAddress), 0);
Expand All @@ -34,7 +34,7 @@ auto RingBuffer<T, size, startAddress>::Front() -> T


template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::Back() -> T
auto RingArray<T, size, startAddress>::Back() -> T
{
std::uint32_t readIndex = 0;
if(iEnd_ == 0)
Expand All @@ -55,7 +55,7 @@ auto RingBuffer<T, size, startAddress>::Back() -> T


template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::operator[](std::size_t index) -> T
auto RingArray<T, size, startAddress>::operator[](std::size_t index) -> T
{
auto const rawAddress =
offset_ + value_of(startAddress) + ((iBegin_ + index) % bufferSize_) * serialSize<T>;
Expand All @@ -67,7 +67,7 @@ auto RingBuffer<T, size, startAddress>::operator[](std::size_t index) -> T


template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::Size() -> std::size_t
auto RingArray<T, size, startAddress>::Size() -> std::size_t
{
if(iEnd_ >= iBegin_)
{
Expand All @@ -77,19 +77,19 @@ auto RingBuffer<T, size, startAddress>::Size() -> std::size_t
}

template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::Capacity() -> std::size_t
auto RingArray<T, size, startAddress>::Capacity() -> std::size_t
{
return (bufferSize_ - 1U);
}

template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::Initialize() -> void
auto RingArray<T, size, startAddress>::Initialize() -> void
{
ReadIndices();
}

template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::WriteIndices() -> void
auto RingArray<T, size, startAddress>::WriteIndices() -> void
{
auto beginAddress = value_of(startAddress);
auto endAddress = beginAddress + sizeof(std::size_t);
Expand All @@ -99,7 +99,7 @@ auto RingBuffer<T, size, startAddress>::WriteIndices() -> void
}

template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::ReadIndices() -> void
auto RingArray<T, size, startAddress>::ReadIndices() -> void
{
auto beginAddress = value_of(startAddress);
auto endAddress = beginAddress + sizeof(std::size_t);
Expand Down
6 changes: 3 additions & 3 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ target_link_libraries(
Sts1CobcSw_Utility
)

add_program(FramRingBuffer FramRingBuffer.test.cpp)
add_program(FramRingArray FramRingArray.test.cpp)
target_link_libraries(
Sts1CobcSwTests_FramRingBuffer PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery
Sts1CobcSw_Serial Sts1CobcSw_Edu
Sts1CobcSwTests_FramRingArray PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery
Sts1CobcSw_Serial Sts1CobcSw_Edu
)

# TODO: Enable again once problem with segmentation violation on CI is fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <Sts1CobcSw/Edu/ProgramStatusHistory.hpp>
#include <Sts1CobcSw/Periphery/Fram.hpp>
#include <Sts1CobcSw/Periphery/FramMock.hpp>
#include <Sts1CobcSw/Periphery/FramRingBuffer.hpp>
#include <Sts1CobcSw/Periphery/FramRingArray.hpp>
#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>

Expand All @@ -20,15 +20,15 @@ using sts1cobcsw::operator""_b; // NOLINT(misc-unused-using-decls)

TEST_CASE("Initial State ringbuffer")
{
fram::RingBuffer<int, 10, fram::Address{0}> buffer;
fram::RingArray<int, 10, fram::Address{0}> buffer;

REQUIRE(buffer.Size() == 0);
REQUIRE(buffer.Capacity() == 10); // Fixed from 0 to 10 to reflect actual buffer capacity
}

TEST_CASE("FramRingBuffer Push function")
TEST_CASE("FramRingArray Push function")
{
fram::RingBuffer<int, 10, fram::Address{0}> buffer;
fram::RingArray<int, 10, fram::Address{0}> buffer;

buffer.Push(1);
buffer.Push(2);
Expand All @@ -37,7 +37,7 @@ TEST_CASE("FramRingBuffer Push function")
REQUIRE(buffer.Size() == 3);
}

TEMPLATE_TEST_CASE_SIG("FramRingBuffer Front Address",
TEMPLATE_TEST_CASE_SIG("FramRingArray Front Address",
"",
((typename T, size_t S, fram::Address A), T, S, A),
(int, 10U, fram::Address{0}),
Expand All @@ -47,7 +47,7 @@ TEMPLATE_TEST_CASE_SIG("FramRingBuffer Front Address",
fram::ram::memory.fill(0x00_b);
fram::Initialize();

fram::RingBuffer<T, S, A> buffer;
fram::RingArray<T, S, A> buffer;

// FIXME: [] not working
buffer.Push(10);
Expand All @@ -63,13 +63,13 @@ TEMPLATE_TEST_CASE_SIG("FramRingBuffer Front Address",
REQUIRE(buffer[2] == 30);
}

TEST_CASE("FramRingBuffer Back() and Front() methods")
TEST_CASE("FramRingArray Back() and Front() methods")
{
fram::ram::SetAllDoFunctions();
fram::ram::memory.fill(0x00_b);
fram::Initialize();

auto buffer = fram::RingBuffer<int, 5, fram::Address{0}>();
auto buffer = fram::RingArray<int, 5, fram::Address{0}>();
etl::circular_buffer<int, 5U> etlBuffer;

// NOLINTNEXTLINE (readability-container-size-empty)
Expand Down Expand Up @@ -121,13 +121,13 @@ TEST_CASE("FramRingBuffer Back() and Front() methods")
REQUIRE(buffer[2] == 4);
}

TEST_CASE("FramRingBuffer Full and Empty conditions")
TEST_CASE("FramRingArray Full and Empty conditions")
{
fram::ram::SetAllDoFunctions();
fram::ram::memory.fill(0x00_b);
fram::Initialize();

auto buffer = fram::RingBuffer<int, 3, fram::Address(0)>{};
auto buffer = fram::RingArray<int, 3, fram::Address(0)>{};

REQUIRE(buffer.Size() == 0);
REQUIRE(buffer.Capacity() == 3);
Expand All @@ -153,13 +153,13 @@ TEST_CASE("FramRingBuffer Full and Empty conditions")
REQUIRE(buffer.Back() == 10);
}

TEST_CASE("FramRingBuffer and ETL Circular Buffer")
TEST_CASE("FramRingArray and ETL Circular Buffer")
{
fram::ram::SetAllDoFunctions();
fram::ram::memory.fill(0x00_b);
fram::Initialize();

fram::RingBuffer<int, 5U, fram::Address{0}> framBuffer{};
fram::RingArray<int, 5U, fram::Address{0}> framBuffer{};
etl::circular_buffer<int, 5U> etlBuffer;

for(int i = 0; i < 5; ++i)
Expand All @@ -181,13 +181,13 @@ TEST_CASE("FramRingBuffer and ETL Circular Buffer")
REQUIRE(framBuffer.Back() == etlBuffer.back());
}

TEST_CASE("FramRingBuffer Stress Test")
TEST_CASE("FramRingArray Stress Test")
{
fram::ram::SetAllDoFunctions();
fram::ram::memory.fill(0x00_b);
fram::Initialize();

auto buffer = fram::RingBuffer<int, 10000, fram::Address{0}>();
auto buffer = fram::RingArray<int, 10000, fram::Address{0}>();

for(int i = 0; i < 10000; ++i)
{
Expand All @@ -209,7 +209,7 @@ TEST_CASE("Custom Type")
fram::Initialize();


fram::RingBuffer<sts1cobcsw::edu::ProgramStatusHistoryEntry, 10U, fram::Address{0}> buffer;
fram::RingArray<sts1cobcsw::edu::ProgramStatusHistoryEntry, 10U, fram::Address{0}> buffer;

auto pshEntry = sts1cobcsw::edu::ProgramStatusHistoryEntry{
.programId = sts1cobcsw::ProgramId(0),
Expand All @@ -230,14 +230,14 @@ TEST_CASE("Reset mechanism")
fram::Initialize();

{
sts1cobcsw::fram::RingBuffer<int, 10, sts1cobcsw::fram::Address{1234}> buffer;
sts1cobcsw::fram::RingArray<int, 10, sts1cobcsw::fram::Address{1234}> buffer;
buffer.Push(1);
buffer.Push(2);
buffer.Push(3);
}

// Simulate a reset by creating a new buffer instance
fram::RingBuffer<int, 10, fram::Address{1234}> buffer;
fram::RingArray<int, 10, fram::Address{1234}> buffer;

REQUIRE(buffer.Size() == 3);
REQUIRE(buffer.Front() == 1);
Expand Down

0 comments on commit 5646472

Please sign in to comment.