fsfw/unittests/container/RingBufferTest.cpp

327 lines
13 KiB
C++
Raw Permalink Normal View History

#include <fsfw/container/SimpleRingBuffer.h>
2020-10-20 17:11:23 +02:00
#include <catch2/catch_test_macros.hpp>
2020-10-20 17:11:23 +02:00
#include <cstring>
2022-07-18 11:58:55 +02:00
#include "CatchDefinitions.h"
2020-10-20 17:11:23 +02:00
2022-08-08 12:35:58 +02:00
TEST_CASE("Ring Buffer Test", "[containers]") {
2022-02-02 10:29:30 +01:00
uint8_t testData[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
SimpleRingBuffer ringBuffer(10, false, 5);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Simple Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.writeData(testData, 3) == returnvalue::FAILED);
REQUIRE(ringBuffer.readData(readBuffer, 5, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
REQUIRE(ringBuffer.availableWriteSpace() == 5);
ringBuffer.clear();
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 4) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 4, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 4; i++) {
CHECK(readBuffer[i] == i);
}
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 9, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 9; i++) {
CHECK(readBuffer[i] == i);
}
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 1024) == returnvalue::FAILED);
REQUIRE(ringBuffer.writeData(nullptr, 5) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Get Free Element Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 8) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 1);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 8, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 9);
uint8_t *testPtr = nullptr;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == returnvalue::FAILED);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.writeTillWrap() == 2);
// too many excess bytes.
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == returnvalue::FAILED);
REQUIRE(ringBuffer.getFreeElement(&testPtr, 5) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getExcessBytes() == 3);
std::memcpy(testPtr, testData, 5);
ringBuffer.confirmBytesWritten(5);
REQUIRE(ringBuffer.getAvailableReadData() == 5);
ringBuffer.readData(readBuffer, 5, true);
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Read Remaining Test") {
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 3) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getAvailableReadData() == 3);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
size_t trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
for (uint8_t i = 0; i < 3; i++) {
CHECK(readBuffer[i] == i);
}
trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 0);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
}
2020-10-20 17:11:23 +02:00
}
2022-02-02 10:29:30 +01:00
TEST_CASE("Ring Buffer Test2", "[RingBufferTest2]") {
uint8_t testData[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
uint8_t *newBuffer = new uint8_t[15];
SimpleRingBuffer ringBuffer(newBuffer, 10, true, 5);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Simple Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 5, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
REQUIRE(ringBuffer.availableWriteSpace() == 5);
ringBuffer.clear();
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 4) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 4, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 4; i++) {
CHECK(readBuffer[i] == i);
}
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 9, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 9; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Get Free Element Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 8) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 1);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 8, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 9);
uint8_t *testPtr = nullptr;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == returnvalue::FAILED);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.writeTillWrap() == 2);
// too many excess bytes.
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == returnvalue::FAILED);
REQUIRE(ringBuffer.getFreeElement(&testPtr, 5) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getExcessBytes() == 3);
std::memcpy(testPtr, testData, 5);
ringBuffer.confirmBytesWritten(5);
REQUIRE(ringBuffer.getAvailableReadData() == 5);
ringBuffer.readData(readBuffer, 5, true);
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Read Remaining Test") {
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 3) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getAvailableReadData() == 3);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
size_t trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
for (uint8_t i = 0; i < 3; i++) {
CHECK(readBuffer[i] == i);
}
trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 0);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Overflow") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-03-25 15:20:06 +01:00
// We don't allow writing of Data that is larger than the ring buffer in total
REQUIRE(ringBuffer.getMaxSize() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 13) == returnvalue::FAILED);
2022-03-25 15:20:06 +01:00
REQUIRE(ringBuffer.getAvailableReadData() == 0);
2022-02-02 10:29:30 +01:00
ringBuffer.clear();
uint8_t *ptr = nullptr;
2022-03-25 15:20:06 +01:00
// With excess Bytes 13 Bytes can be written to this Buffer
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&ptr, 13) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ptr != nullptr);
memcpy(ptr, testData, 13);
ringBuffer.confirmBytesWritten(13);
REQUIRE(ringBuffer.getAvailableReadData() == 3);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 3, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (auto i = 0; i < 3; i++) {
REQUIRE(readBuffer[i] == testData[i + 10]);
}
}
2020-10-20 17:11:23 +02:00
}
2022-02-02 10:29:30 +01:00
TEST_CASE("Ring Buffer Test3", "[RingBufferTest3]") {
uint8_t testData[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
uint8_t *newBuffer = new uint8_t[25];
SimpleRingBuffer ringBuffer(newBuffer, 10, true, 15);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Simple Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 5, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
REQUIRE(ringBuffer.availableWriteSpace() == 5);
ringBuffer.clear();
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 4) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 4, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 4; i++) {
CHECK(readBuffer[i] == i);
}
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 9, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 9; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Get Free Element Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 8) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 1);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 8, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 9);
uint8_t *testPtr = nullptr;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getExcessBytes() == 8);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.writeTillWrap() == 2);
// too many excess bytes.
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
// Less Execss bytes overwrites before
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 3) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getExcessBytes() == 1);
std::memcpy(testPtr, testData, 3);
ringBuffer.confirmBytesWritten(3);
REQUIRE(ringBuffer.getAvailableReadData() == 3);
ringBuffer.readData(readBuffer, 3, true);
for (uint8_t i = 0; i < 3; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Read Remaining Test") {
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 3) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getAvailableReadData() == 3);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
size_t trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
for (uint8_t i = 0; i < 3; i++) {
CHECK(readBuffer[i] == i);
}
trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 0);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Overflow") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-07-25 14:36:18 +02:00
// Writing more than the buffer is large.
2022-03-25 15:20:06 +01:00
// This write will be rejected and is seen as a configuration mistake
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 13) == returnvalue::FAILED);
2022-03-25 15:20:06 +01:00
REQUIRE(ringBuffer.getAvailableReadData() == 0);
2022-02-02 10:29:30 +01:00
ringBuffer.clear();
2022-07-25 14:36:18 +02:00
// Using FreeElement allows the usage of excessBytes but
2022-03-25 15:20:06 +01:00
// should be used with caution
2022-02-02 10:29:30 +01:00
uint8_t *ptr = nullptr;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&ptr, 13) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ptr != nullptr);
memcpy(ptr, testData, 13);
ringBuffer.confirmBytesWritten(13);
REQUIRE(ringBuffer.getAvailableReadData() == 3);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 3, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (auto i = 0; i < 3; i++) {
REQUIRE(readBuffer[i] == testData[i + 10]);
}
}
2020-10-20 17:11:23 +02:00
}
2022-02-02 10:29:30 +01:00
TEST_CASE("Ring Buffer Test4", "[RingBufferTest4]") {
uint8_t testData[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
SimpleRingBuffer ringBuffer(10, false, 15);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Simple Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.writeData(testData, 3) == returnvalue::FAILED);
REQUIRE(ringBuffer.readData(readBuffer, 5, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
REQUIRE(ringBuffer.availableWriteSpace() == 5);
ringBuffer.clear();
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 4) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 4, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 4; i++) {
CHECK(readBuffer[i] == i);
}
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 9) == returnvalue::OK);
REQUIRE(ringBuffer.readData(readBuffer, 9, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
for (uint8_t i = 0; i < 9; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Get Free Element Test") {
REQUIRE(ringBuffer.availableWriteSpace() == 9);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 8) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 1);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 8, true) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.availableWriteSpace() == 9);
uint8_t *testPtr = nullptr;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == returnvalue::FAILED);
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.writeTillWrap() == 2);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == returnvalue::OK);
REQUIRE(ringBuffer.getFreeElement(&testPtr, 5) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getExcessBytes() == 3);
std::memcpy(testPtr, testData, 5);
ringBuffer.confirmBytesWritten(5);
REQUIRE(ringBuffer.getAvailableReadData() == 5);
ringBuffer.readData(readBuffer, 5, true);
for (uint8_t i = 0; i < 5; i++) {
CHECK(readBuffer[i] == i);
}
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Read Remaining Test") {
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.writeData(testData, 3) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(ringBuffer.getAvailableReadData() == 3);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
size_t trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
for (uint8_t i = 0; i < 3; i++) {
CHECK(readBuffer[i] == i);
}
trueSize = 0;
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == returnvalue::FAILED);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 0);
2022-08-16 01:08:26 +02:00
REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == returnvalue::OK);
2022-02-02 10:29:30 +01:00
REQUIRE(trueSize == 3);
}
2020-10-20 17:11:23 +02:00
}