fsfw/unittests/container/TestFifo.cpp

134 lines
4.4 KiB
C++
Raw Permalink Normal View History

2020-12-27 14:20:26 +01:00
#include <fsfw/container/DynamicFIFO.h>
#include <fsfw/container/FIFO.h>
2022-08-16 12:48:22 +02:00
#include <fsfw/returnvalues/returnvalue.h>
2020-10-20 17:11:23 +02:00
#include <catch2/catch_test_macros.hpp>
2020-10-20 17:11:23 +02:00
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("Static Fifo Tests", "[containers]") {
2022-02-02 10:29:30 +01:00
INFO("Fifo Tests");
struct Test {
uint64_t number1;
uint32_t number2;
uint8_t number3;
bool operator==(struct Test& other) {
if ((other.number1 == this->number1) and (other.number1 == this->number1) and
(other.number1 == this->number1)) {
return true;
}
return false;
}
};
FIFO<Test, 3> fifo;
std::vector<Test> list;
struct Test structOne({UINT64_MAX, UINT32_MAX, UINT8_MAX});
struct Test structTwo({0, 1, 2});
struct Test structThree({42, 43, 44});
list.push_back(structThree);
list.push_back(structTwo);
list.push_back(structOne);
SECTION("Insert, retrieval test") {
REQUIRE(fifo.getMaxCapacity() == 3);
REQUIRE(fifo.size() == 0);
REQUIRE(fifo.empty());
REQUIRE(not fifo.full());
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.insert(structOne) == static_cast<int>(returnvalue::OK));
REQUIRE(fifo.insert(structTwo) == static_cast<int>(returnvalue::OK));
REQUIRE(fifo.insert(structThree) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.insert(structTwo) == static_cast<int>(FIFOBase<Test>::FULL));
struct Test testptr;
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.peek(&testptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
bool equal = testptr == structOne;
REQUIRE(equal);
REQUIRE(fifo.size() == 3);
REQUIRE(fifo.full());
REQUIRE(not fifo.empty());
for (size_t i = 2; i < 3; i--) {
testptr.number1 = 0;
testptr.number2 = 0;
testptr.number3 = 0;
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.retrieve(&testptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
equal = testptr == list[i];
REQUIRE(equal);
REQUIRE(fifo.size() == i);
}
testptr.number1 = 0;
testptr.number2 = 0;
testptr.number3 = 0;
REQUIRE(fifo.retrieve(&testptr) == static_cast<int>(FIFOBase<Test>::EMPTY));
REQUIRE(fifo.peek(&testptr) == static_cast<int>(FIFOBase<Test>::EMPTY));
REQUIRE(not fifo.full());
REQUIRE(fifo.empty());
REQUIRE(fifo.pop() == static_cast<int>(FIFOBase<Test>::EMPTY));
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.insert(structOne) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.size() == 1);
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.insert(structTwo) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.size() == 2);
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.pop() == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.size() == 1);
testptr.number1 = 0;
testptr.number2 = 0;
testptr.number3 = 0;
// Test that retrieve and peek will not cause a nullptr dereference
struct Test* ptr = nullptr;
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.retrieve(ptr) == static_cast<int>(returnvalue::FAILED));
REQUIRE(fifo.peek(ptr) == static_cast<int>(returnvalue::FAILED));
2022-02-02 10:29:30 +01:00
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.peek(&testptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
equal = testptr == structTwo;
REQUIRE(equal);
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.pop() == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.size() == 0);
REQUIRE(fifo.empty());
};
SECTION("Copy Test") {
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.insert(structOne) == static_cast<int>(returnvalue::OK));
REQUIRE(fifo.insert(structTwo) == static_cast<int>(returnvalue::OK));
REQUIRE(fifo.insert(structThree) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.size() == 3);
REQUIRE(fifo.full());
REQUIRE(not fifo.empty());
FIFO<Test, 3> fifo2(fifo);
REQUIRE(fifo2.size() == 3);
REQUIRE(fifo2.full());
REQUIRE(not fifo2.empty());
for (size_t i = 2; i < 3; i--) {
struct Test testptr = {0, 0, 0};
2022-08-15 20:28:16 +02:00
REQUIRE(fifo2.retrieve(&testptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
bool equal = testptr == list[i];
REQUIRE(equal);
REQUIRE(fifo2.size() == i);
}
};
SECTION("Assignment Test") {
2022-08-15 20:28:16 +02:00
REQUIRE(fifo.insert(structOne) == static_cast<int>(returnvalue::OK));
REQUIRE(fifo.insert(structTwo) == static_cast<int>(returnvalue::OK));
REQUIRE(fifo.insert(structThree) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(fifo.size() == 3);
REQUIRE(fifo.full());
REQUIRE(not fifo.empty());
FIFO<Test, 3> fifo2;
fifo2 = fifo;
REQUIRE(fifo2.size() == 3);
REQUIRE(fifo2.full());
REQUIRE(not fifo2.empty());
for (size_t i = 2; i < 3; i--) {
struct Test testptr = {0, 0, 0};
2022-08-15 20:28:16 +02:00
REQUIRE(fifo2.retrieve(&testptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
bool equal = testptr == list[i];
REQUIRE(equal);
REQUIRE(fifo2.size() == i);
}
};
2020-10-20 17:11:23 +02:00
};