fsfw/unittests/container/TestDynamicFifo.cpp

143 lines
4.7 KiB
C++
Raw 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("Dynamic Fifo Tests", "[containers]") {
2022-02-02 10:29:30 +01:00
INFO("Dynamic 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;
}
};
DynamicFIFO<Test> fifo(3);
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-16 01:08:26 +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-16 01:08:26 +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-16 01:08:26 +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-16 01:08:26 +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-16 01:08:26 +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-16 01:08:26 +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;
2022-08-16 01:08:26 +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-16 01:08:26 +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-16 01:08:26 +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());
DynamicFIFO<Test> fifo2(fifo);
REQUIRE(fifo2.size() == 3);
REQUIRE(fifo2.full());
REQUIRE(not fifo2.empty());
};
SECTION("Assignment Test") {
2022-08-16 01:08:26 +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());
DynamicFIFO<Test> fifo2(6);
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-16 01:08:26 +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 Smaller") {
2022-08-16 01:08:26 +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());
DynamicFIFO<Test> fifo2(2);
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-16 01:08:26 +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
};