fsfw/unittests/container/TestFixedOrderedMultimap.cpp

222 lines
8.0 KiB
C++
Raw Normal View History

2020-12-28 01:17:03 +01:00
#include <fsfw/container/FixedOrderedMultimap.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"
2022-02-02 10:29:30 +01:00
TEST_CASE("FixedOrderedMultimap Tests", "[TestFixedOrderedMultimap]") {
INFO("FixedOrderedMultimap Tests");
FixedOrderedMultimap<unsigned int, unsigned short> map(30);
REQUIRE(map.size() == 0);
REQUIRE(map.maxSize() == 30);
SECTION("Test insert, find, exists") {
for (uint16_t i = 0; i < 30; i++) {
REQUIRE(map.insert(std::make_pair(i, i + 1)) ==
2022-08-15 20:28:16 +02:00
static_cast<int>(returnvalue::OK));
REQUIRE(map.exists(i) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(map.find(i)->second == i + 1);
}
REQUIRE(map.insert(0, 0) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::MAP_FULL));
REQUIRE(map.exists(31) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.size() == 30);
{
uint16_t* ptr;
2022-08-15 20:28:16 +02:00
REQUIRE(map.find(5, &ptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(*ptr == 6);
REQUIRE(map.find(31, &ptr) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
}
2022-08-15 20:28:16 +02:00
REQUIRE(map.erase(2) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(map.erase(31) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.exists(2) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.size() == 29);
for (auto element : map) {
if (element.first == 5) {
REQUIRE(element.second == 6);
}
}
for (FixedOrderedMultimap<uint32_t, uint16_t>::Iterator it = map.begin(); it != map.end();
it++) {
REQUIRE(it->second == it->first + 1);
REQUIRE((*it).second == (*it).first + 1);
it->second = it->second + 1;
REQUIRE(it->second == it->first + 2);
}
{
FixedOrderedMultimap<uint32_t, uint16_t>::Iterator it = map.begin();
while (it != map.end()) {
2022-08-15 20:28:16 +02:00
REQUIRE(map.erase(&it) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
}
REQUIRE(map.size() == 0);
}
for (FixedOrderedMultimap<uint32_t, uint16_t>::Iterator it = map.begin(); it != map.end();
it++) {
// This line should never executed if begin and end is correct
FAIL("Should never be reached, Iterators invalid");
}
};
SECTION("Test different insert variants") {
FixedOrderedMultimap<uint32_t, uint16_t>::Iterator it = map.end();
2022-08-15 20:28:16 +02:00
REQUIRE(map.insert(36, 37, &it) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(it->first == 36);
REQUIRE(it->second == 37);
REQUIRE(map.size() == 1);
2022-08-15 20:28:16 +02:00
REQUIRE(map.insert(37, 38, nullptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(map.find(37)->second == 38);
REQUIRE(map.size() == 2);
2022-08-15 20:28:16 +02:00
REQUIRE(map.insert(37, 24, nullptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(map.find(37)->second == 38);
2022-08-15 20:28:16 +02:00
REQUIRE(map.insert(0, 1, nullptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(map.find(0)->second == 1);
REQUIRE(map.size() == 4);
map.clear();
REQUIRE(map.size() == 0);
}
SECTION("Test different erase and find with no entries") {
FixedOrderedMultimap<uint32_t, uint16_t>::Iterator it;
it = map.end();
REQUIRE(map.erase(&it) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.find(1) == map.end());
}
2020-10-20 17:11:23 +02:00
}
2022-02-02 10:29:30 +01:00
TEST_CASE("FixedOrderedMultimap Non Trivial Type", "[TestFixedOrderedMultimapNonTrivial]") {
INFO("FixedOrderedMultimap Non Trivial Type");
class TestClass {
public:
TestClass(){};
TestClass(uint32_t number1, uint64_t number2) : number1(number1), number2(number2){};
~TestClass(){};
bool operator==(const TestClass& lhs) {
return ((this->number1 == lhs.number1) and (this->number2 == lhs.number2));
}
bool operator!=(const TestClass& lhs) { return not(this->operator==(lhs)); }
TestClass(const TestClass& other) {
this->number1 = other.number1;
this->number2 = other.number2;
};
TestClass& operator=(const TestClass& other) {
this->number1 = other.number1;
this->number2 = other.number2;
return *this;
};
private:
uint32_t number1 = 0;
uint64_t number2 = 5;
};
FixedOrderedMultimap<unsigned int, TestClass> map(30);
REQUIRE(map.size() == 0);
REQUIRE(map.maxSize() == 30);
SECTION("Test insert, find, exists") {
for (uint16_t i = 0; i < 30; i++) {
REQUIRE(map.insert(std::make_pair(i, TestClass(i + 1, i))) ==
2022-08-15 20:28:16 +02:00
static_cast<int>(returnvalue::OK));
REQUIRE(map.exists(i) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
bool compare = map.find(i)->second == TestClass(i + 1, i);
REQUIRE(compare);
}
REQUIRE(map.insert(0, TestClass()) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::MAP_FULL));
REQUIRE(map.exists(31) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.size() == 30);
{
TestClass* ptr = nullptr;
2022-08-15 20:28:16 +02:00
REQUIRE(map.find(5, &ptr) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
bool compare = *ptr == TestClass(6, 5);
REQUIRE(compare);
REQUIRE(map.find(31, &ptr) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
}
2022-08-15 20:28:16 +02:00
REQUIRE(map.erase(2) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(map.erase(31) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.exists(2) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, uint16_t>::KEY_DOES_NOT_EXIST));
REQUIRE(map.size() == 29);
for (auto element : map) {
if (element.first == 5) {
bool compare = element.second == TestClass(6, 5);
REQUIRE(compare);
}
}
for (FixedOrderedMultimap<uint32_t, TestClass>::Iterator it = map.begin(); it != map.end();
it++) {
bool compare = it->second == TestClass(it->first + 1, it->first);
REQUIRE(compare);
compare = (*it).second == TestClass((*it).first + 1, (*it).first);
REQUIRE(compare);
it->second = TestClass(it->first + 2, it->first);
compare = it->second == TestClass(it->first + 2, it->first);
REQUIRE(compare);
}
{
FixedOrderedMultimap<uint32_t, TestClass>::Iterator it = map.begin();
while (it != map.end()) {
2022-08-15 20:28:16 +02:00
REQUIRE(map.erase(&it) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
}
REQUIRE(map.size() == 0);
}
for (FixedOrderedMultimap<uint32_t, TestClass>::Iterator it = map.begin(); it != map.end();
it++) {
// This line should never executed if begin and end is correct
FAIL("Should never be reached, Iterators invalid");
}
};
SECTION("Test different insert variants") {
FixedOrderedMultimap<uint32_t, TestClass>::Iterator it = map.end();
REQUIRE(map.insert(36, TestClass(37, 36), &it) ==
2022-08-15 20:28:16 +02:00
static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(it->first == 36);
bool compare = it->second == TestClass(37, 36);
REQUIRE(compare);
REQUIRE(map.size() == 1);
REQUIRE(map.insert(37, TestClass(38, 37), nullptr) ==
2022-08-15 20:28:16 +02:00
static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
compare = map.find(37)->second == TestClass(38, 37);
REQUIRE(compare);
REQUIRE(map.size() == 2);
REQUIRE(map.insert(37, TestClass(24, 37), nullptr) ==
2022-08-15 20:28:16 +02:00
static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
compare = map.find(37)->second == TestClass(38, 37);
REQUIRE(compare);
REQUIRE(map.insert(0, TestClass(1, 0), nullptr) ==
2022-08-15 20:28:16 +02:00
static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
compare = map.find(0)->second == TestClass(1, 0);
REQUIRE(compare);
REQUIRE(map.size() == 4);
map.clear();
REQUIRE(map.size() == 0);
}
SECTION("Test different erase and find with no entries") {
FixedOrderedMultimap<uint32_t, TestClass>::Iterator it;
it = map.end();
REQUIRE(map.erase(&it) ==
static_cast<int>(FixedOrderedMultimap<uint32_t, TestClass>::KEY_DOES_NOT_EXIST));
REQUIRE(map.find(1) == map.end());
}
2020-10-20 17:11:23 +02:00
}