fsfw/unittests/container/TestArrayList.cpp

92 lines
2.7 KiB
C++
Raw Permalink Normal View History

#include <fsfw/container/ArrayList.h>
2022-08-16 12:48:22 +02:00
#include <fsfw/returnvalues/returnvalue.h>
2020-12-01 16:55:24 +01: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
2020-10-20 17:11:23 +02:00
/**
* @brief Array List test
*/
2022-08-08 12:35:58 +02:00
TEST_CASE("Array List", "[containers]") {
2022-02-02 10:29:30 +01:00
// perform set-up here
ArrayList<uint16_t> list(20);
struct TestClass {
public:
TestClass(){};
TestClass(uint32_t number1, uint64_t number2) : number1(number1), number2(number2){};
uint32_t number1 = -1;
uint64_t number2 = -1;
bool operator==(const TestClass& other) {
return ((this->number1 == other.number1) and (this->number2 == other.number2));
};
};
ArrayList<TestClass> complexList(20);
SECTION("SimpleTest") {
REQUIRE(list.maxSize() == 20);
REQUIRE(list.size == 0);
2022-08-15 20:28:16 +02:00
REQUIRE(list.insert(10) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
REQUIRE(list[0] == 10);
REQUIRE(list.front() != nullptr);
REQUIRE((*list.front()) == 10);
REQUIRE(list.back() != nullptr);
REQUIRE((*list.back()) == 10);
// Need to test the const version of back as well
const uint16_t* number = const_cast<const ArrayList<uint16_t>*>(&list)->back();
REQUIRE(*number == 10);
list.clear();
REQUIRE(list.size == 0);
}
SECTION("Fill and check") {
// This is an invalid element but its not a nullptr
REQUIRE(list.back() != nullptr);
for (auto i = 0; i < 20; i++) {
2022-08-15 20:28:16 +02:00
REQUIRE(list.insert(i) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
}
2022-11-10 16:11:05 +01:00
REQUIRE(list.insert(20) == static_cast<int>(containers::LIST_FULL));
2022-02-02 10:29:30 +01:00
ArrayList<uint16_t>::Iterator it = list.begin();
REQUIRE((*it) == 0);
it++;
REQUIRE((*it) == 1);
it--;
REQUIRE((*it) == 0);
it++;
for (auto it2 = list.begin(); it2 != list.end(); it2++) {
if (it == it2) {
REQUIRE((*it) == (*it2));
break;
} else {
REQUIRE((*it2) == 0);
REQUIRE(it2 != it);
}
}
}
SECTION("Const Iterator") {
ArrayList<uint16_t>::Iterator it = list.begin();
for (auto i = 0; i < 10; i++) {
2022-08-15 20:28:16 +02:00
REQUIRE(list.insert(i) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
}
it++;
const uint16_t* number = it.value;
REQUIRE(*number == 1);
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
SECTION("Const Iterator") {
ArrayList<TestClass>::Iterator it = complexList.begin();
for (auto i = 0; i < 10; i++) {
2022-08-22 15:02:16 +02:00
REQUIRE(complexList.insert(TestClass(i, i + 1)) == static_cast<int>(returnvalue::OK));
2022-02-02 10:29:30 +01:00
}
it++;
const TestClass* secondTest = it.value;
bool compare = TestClass(1, 2) == *secondTest;
REQUIRE(compare);
it++;
REQUIRE(it->number1 == 2);
REQUIRE(it->number2 == 3);
const ArrayList<TestClass>::Iterator it4(&(complexList[2]));
REQUIRE(it4->number1 == 2);
REQUIRE((*it4).number2 == 3);
REQUIRE(complexList.remaining() == 10);
}
2020-10-20 17:11:23 +02:00
}