#include <fsfw/storagemanager/LocalPool.h>
#include <array>
#include <catch2/catch_test_macros.hpp>
#include <cstring>
#include "CatchDefinitions.h"
TEST_CASE("New Accessor", "[NewAccessor]") {
LocalPool::LocalPoolConfig poolCfg = {{1, 10}};
LocalPool SimplePool = LocalPool(0, poolCfg);
std::array<uint8_t, 20> testDataArray;
std::array<uint8_t, 20> receptionArray;
store_address_t testStoreId;
ReturnValue_t result = retval::CATCH_FAILED;
for (size_t i = 0; i < testDataArray.size(); i++) {
testDataArray[i] = i;
}
size_t size = 10;
SECTION("Simple tests getter functions") {
result = SimplePool.addData(&testStoreId, testDataArray.data(), size);
REQUIRE(result == retval::CATCH_OK);
auto resultPair = SimplePool.getData(testStoreId);
REQUIRE(resultPair.first == retval::CATCH_OK);
resultPair.second.getDataCopy(receptionArray.data(), 20);
CHECK(resultPair.second.getId() == testStoreId);
CHECK(resultPair.second.size() == 10);
for (size_t i = 0; i < size; i++) {
CHECK(receptionArray[i] == i);
std::copy(resultPair.second.data(), resultPair.second.data() + resultPair.second.size(),
receptionArray.data());
{
auto resultPairLoc = SimplePool.getData(testStoreId);
REQUIRE(resultPairLoc.first == retval::CATCH_OK);
// data should be deleted when accessor goes out of scope.
resultPair = SimplePool.getData(testStoreId);
REQUIRE(resultPair.first == (int)StorageManagerIF::DATA_DOES_NOT_EXIST);
ConstStorageAccessor constAccessor(testStoreId);
result = SimplePool.getData(testStoreId, constAccessor);
constAccessor.getDataCopy(receptionArray.data(), 20);
// likewise, data should be deleted when accessor gets out of scope.
resultPair.second.release();
// now data should not be deleted anymore
SECTION("Simple tests modify functions") {
StorageAccessor accessor(testStoreId);
result = SimplePool.modifyData(testStoreId, accessor);
CHECK(accessor.getId() == testStoreId);
CHECK(accessor.size() == 10);
accessor.getDataCopy(receptionArray.data(), 20);
std::copy(accessor.data(), accessor.data() + accessor.size(), receptionArray.data());
// data should be deleted when accessor goes out of scope
auto resultPairLoc = SimplePool.modifyData(testStoreId);
CHECK(resultPairLoc.second.getId() == testStoreId);
CHECK(resultPairLoc.second.size() == 10);
resultPairLoc.second.getDataCopy(receptionArray.data(), 20);
std::copy(resultPairLoc.second.data(),
resultPairLoc.second.data() + resultPairLoc.second.size(), receptionArray.data());
resultPairLoc.second.release();
// data should not be deleted when accessor goes out of scope
SECTION("Write tests") {
auto resultPair = SimplePool.modifyData(testStoreId);
testDataArray[9] = 42;
resultPair.second.write(testDataArray.data(), 10, 0);
// now data should not be deleted
auto resultConstPair = SimplePool.getData(testStoreId);
REQUIRE(resultConstPair.first == retval::CATCH_OK);
resultConstPair.second.getDataCopy(receptionArray.data(), 10);
for (size_t i = 0; i < size - 1; i++) {
CHECK(receptionArray[9] == 42);
result = resultPair.second.write(testDataArray.data(), 20, 0);
REQUIRE(result == retval::CATCH_FAILED);
result = resultPair.second.write(testDataArray.data(), 10, 5);
std::memset(testDataArray.data(), 42, 5);
result = resultPair.second.write(testDataArray.data(), 5, 5);
resultConstPair = SimplePool.getData(testStoreId);
for (size_t i = 5; i < 10; i++) {
CHECK(receptionArray[i] == 42);
SECTION("Operators") {
StorageAccessor accessor2(0);
accessor2 = std::move(accessor);
REQUIRE(accessor.data() == nullptr);
std::array<uint8_t, 6> data;
size_t size = 6;
result = accessor.write(data.data(), data.size());
REQUIRE(result == HasReturnvaluesIF::RETURN_FAILED);
result = SimplePool.modifyData(testStoreId, accessor2);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
CHECK(accessor2.getId() == testStoreId);
CHECK(accessor2.size() == 10);
std::array<uint8_t, 10> newData;
// Expect data to be invalid so this must return RETURN_FAILED
result = accessor.getDataCopy(newData.data(), newData.size());
// Expect data to be too small
result = accessor2.getDataCopy(data.data(), data.size());