fsfw/unittests/serialize/testSerializeIF.cpp

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

2022-07-23 10:34:19 +02:00
2022-07-23 11:10:44 +02:00
#include <array>
2022-07-23 10:34:19 +02:00
#include <catch2/catch_test_macros.hpp>
2022-07-23 11:10:44 +02:00
#include "mocks/SimpleSerializable.h"
using namespace std;
TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
auto simpleSer = SimpleSerializable();
array<uint8_t, 16> buf{};
uint8_t* ptr = buf.data();
size_t len = 0;
SECTION("Little Endian Normal") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::LITTLE) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
CHECK(buf[0] == 1);
CHECK(buf[1] == 3);
CHECK(buf[2] == 2);
// Verify pointer arithmetic and size increment
CHECK(ptr == buf.data() + 3);
CHECK(len == 3);
}
SECTION("Little Endian Simple") {
2022-07-27 21:32:22 +02:00
size_t serLen = 0xff;
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
2022-08-22 15:02:16 +02:00
SerializeIF::Endianness::LITTLE) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
CHECK(buf[0] == 1);
CHECK(buf[1] == 3);
CHECK(buf[2] == 2);
2022-07-27 21:32:22 +02:00
CHECK(serLen == 3);
2022-07-23 11:10:44 +02:00
}
SECTION("Big Endian Normal") {
SECTION("Explicit") {
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::BIG) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 0") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::NETWORK) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 1") {
2022-08-15 20:28:16 +02:00
REQUIRE(simpleSer.serializeBe(&ptr, &len, buf.size()) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
CHECK(buf[0] == 1);
CHECK(buf[1] == 2);
CHECK(buf[2] == 3);
// Verify pointer arithmetic and size increment
CHECK(ptr == buf.data() + 3);
CHECK(len == 3);
}
SECTION("Big Endian Simple") {
2022-07-27 21:32:22 +02:00
size_t serLen = 0xff;
2022-07-23 11:10:44 +02:00
SECTION("Explicit") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
2022-08-22 15:02:16 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 0") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::NETWORK) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 1") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
CHECK(buf[0] == 1);
CHECK(buf[1] == 2);
CHECK(buf[2] == 3);
2022-07-27 21:32:22 +02:00
CHECK(serLen == 3);
2022-07-23 11:10:44 +02:00
}
}
TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
auto simpleSer = SimpleSerializable();
array<uint8_t, 3> buf = {5, 0, 1};
const uint8_t* ptr = buf.data();
size_t len = buf.size();
SECTION("Little Endian Normal") {
2022-08-22 15:02:16 +02:00
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::LITTLE) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 0x0100);
CHECK(ptr == buf.data() + 3);
CHECK(len == 0);
}
SECTION("Little Endian Simple") {
2022-07-27 21:32:22 +02:00
size_t deserLen = 0xff;
2022-08-22 15:02:16 +02:00
REQUIRE(simpleSer.SerializeIF::deSerialize(ptr, deserLen, len,
SerializeIF::Endianness::LITTLE) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 0x0100);
2022-07-27 21:32:22 +02:00
CHECK(deserLen == 3);
2022-07-23 11:10:44 +02:00
}
SECTION("Big Endian Normal") {
SECTION("Explicit") {
2022-08-22 15:02:16 +02:00
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 0") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::NETWORK) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 1") {
2022-08-15 20:28:16 +02:00
REQUIRE(simpleSer.SerializeIF::deSerializeBe(&ptr, &len) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 1);
CHECK(ptr == buf.data() + 3);
CHECK(len == 0);
}
SECTION("Big Endian Simple") {
2022-07-27 21:32:22 +02:00
size_t deserLen = 0xff;
2022-07-23 11:10:44 +02:00
SECTION("Explicit") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(),
2022-08-22 15:02:16 +02:00
SerializeIF::Endianness::BIG) == returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 0") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(),
SerializeIF::Endianness::NETWORK) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
SECTION("Network 1") {
2022-07-27 21:32:22 +02:00
REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), deserLen, buf.size()) ==
2022-08-15 20:28:16 +02:00
returnvalue::OK);
2022-07-23 11:10:44 +02:00
}
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 1);
2022-07-27 21:32:22 +02:00
CHECK(deserLen == 3);
2022-07-23 11:10:44 +02:00
}
2022-07-23 10:34:19 +02:00
}