unittest now contained directly
This commit is contained in:
143
unittest/tests/serialize/TestSerialBufferAdapter.cpp
Normal file
143
unittest/tests/serialize/TestSerialBufferAdapter.cpp
Normal file
@ -0,0 +1,143 @@
|
||||
#include <fsfw/serialize/SerialBufferAdapter.h>
|
||||
|
||||
#include <fsfw/unittest/catch2/catch.hpp>
|
||||
#include <fsfw/unittest/core/CatchDefinitions.h>
|
||||
|
||||
|
||||
static bool test_value_bool = true;
|
||||
static uint16_t tv_uint16 {283};
|
||||
static std::array<uint8_t, 512> testArray;
|
||||
|
||||
TEST_CASE("Serial Buffer Adapter", "[single-file]") {
|
||||
size_t serialized_size = 0;
|
||||
test_value_bool = true;
|
||||
uint8_t * arrayPtr = testArray.data();
|
||||
std::array<uint8_t, 5> test_serial_buffer {5, 4, 3, 2, 1};
|
||||
SerialBufferAdapter<uint8_t> tv_serial_buffer_adapter =
|
||||
SerialBufferAdapter<uint8_t>(test_serial_buffer.data(),
|
||||
test_serial_buffer.size(), false);
|
||||
tv_uint16 = 16;
|
||||
|
||||
SECTION("Serialize without size field") {
|
||||
SerializeAdapter::serialize(&test_value_bool, &arrayPtr,
|
||||
&serialized_size, testArray.size(),
|
||||
SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_serial_buffer_adapter, &arrayPtr,
|
||||
&serialized_size, testArray.size(),
|
||||
SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint16, &arrayPtr, &serialized_size,
|
||||
testArray.size(), SerializeIF::Endianness::MACHINE);
|
||||
|
||||
REQUIRE(serialized_size == 8);
|
||||
REQUIRE(testArray[0] == true);
|
||||
REQUIRE(testArray[1] == 5);
|
||||
REQUIRE(testArray[2] == 4);
|
||||
REQUIRE(testArray[3] == 3);
|
||||
REQUIRE(testArray[4] == 2);
|
||||
REQUIRE(testArray[5] == 1);
|
||||
memcpy(&tv_uint16, testArray.data() + 6, sizeof(tv_uint16));
|
||||
REQUIRE(tv_uint16 == 16);
|
||||
}
|
||||
|
||||
SECTION("Serialize with size field") {
|
||||
SerialBufferAdapter<uint8_t> tv_serial_buffer_adapter_loc =
|
||||
SerialBufferAdapter<uint8_t>(test_serial_buffer.data(),
|
||||
test_serial_buffer.size(), true);
|
||||
serialized_size = 0;
|
||||
arrayPtr = testArray.data();
|
||||
SerializeAdapter::serialize(&test_value_bool, &arrayPtr,&serialized_size,
|
||||
testArray.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_serial_buffer_adapter_loc, &arrayPtr,
|
||||
&serialized_size, testArray.size(),
|
||||
SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint16, &arrayPtr, &serialized_size,
|
||||
testArray.size(), SerializeIF::Endianness::MACHINE);
|
||||
|
||||
REQUIRE(serialized_size == 9);
|
||||
REQUIRE(testArray[0] == true);
|
||||
REQUIRE(testArray[1] == 5);
|
||||
REQUIRE(testArray[2] == 5);
|
||||
REQUIRE(testArray[3] == 4);
|
||||
REQUIRE(testArray[4] == 3);
|
||||
REQUIRE(testArray[5] == 2);
|
||||
REQUIRE(testArray[6] == 1);
|
||||
memcpy(&tv_uint16, testArray.data() + 7, sizeof(tv_uint16));
|
||||
REQUIRE(tv_uint16 == 16);
|
||||
}
|
||||
|
||||
SECTION("Test set buffer function") {
|
||||
SerialBufferAdapter<uint8_t> tv_serial_buffer_adapter_loc =
|
||||
SerialBufferAdapter<uint8_t>((uint8_t*)nullptr,
|
||||
0, true);
|
||||
tv_serial_buffer_adapter_loc.setBuffer(test_serial_buffer.data(),
|
||||
test_serial_buffer.size());
|
||||
serialized_size = 0;
|
||||
arrayPtr = testArray.data();
|
||||
SerializeAdapter::serialize(&test_value_bool, &arrayPtr,&serialized_size,
|
||||
testArray.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_serial_buffer_adapter_loc, &arrayPtr,
|
||||
&serialized_size, testArray.size(),
|
||||
SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint16, &arrayPtr, &serialized_size,
|
||||
testArray.size(), SerializeIF::Endianness::MACHINE);
|
||||
REQUIRE(serialized_size == 9);
|
||||
REQUIRE(testArray[0] == true);
|
||||
REQUIRE(testArray[1] == 5);
|
||||
REQUIRE(testArray[2] == 5);
|
||||
REQUIRE(testArray[3] == 4);
|
||||
REQUIRE(testArray[4] == 3);
|
||||
REQUIRE(testArray[5] == 2);
|
||||
REQUIRE(testArray[6] == 1);
|
||||
memcpy(&tv_uint16, testArray.data() + 7, sizeof(tv_uint16));
|
||||
REQUIRE(tv_uint16 == 16);
|
||||
}
|
||||
|
||||
SECTION("Deserialization with size field") {
|
||||
size_t buffer_size = 4;
|
||||
memcpy(testArray.data(), &buffer_size, sizeof(uint16_t));
|
||||
testArray[2] = 1;
|
||||
testArray[3] = 1;
|
||||
testArray[4] = 1;
|
||||
testArray[5] = 0;
|
||||
std::array<uint8_t, 4> test_recv_array;
|
||||
arrayPtr = testArray.data();
|
||||
// copy testArray[1] to testArray[4] into receive buffer, skip
|
||||
// size field (testArray[0]) for deSerialization.
|
||||
SerialBufferAdapter<uint16_t> tv_serial_buffer_adapter3 =
|
||||
SerialBufferAdapter<uint16_t>(test_recv_array.data(), 4, true);
|
||||
// Deserialization
|
||||
size_t size = 6;
|
||||
auto result = tv_serial_buffer_adapter3.deSerialize(
|
||||
const_cast<const uint8_t**>(&arrayPtr), &size,
|
||||
SerializeIF::Endianness::MACHINE);
|
||||
REQUIRE(result == retval::CATCH_OK);
|
||||
CHECK(test_recv_array[0] == 1);
|
||||
CHECK(test_recv_array[1] == 1);
|
||||
CHECK(test_recv_array[2] == 1);
|
||||
CHECK(test_recv_array[3] == 0);
|
||||
}
|
||||
|
||||
SECTION("Deserialization without size field") {
|
||||
size_t buffer_size = 4;
|
||||
memcpy(testArray.data(), &buffer_size, sizeof(uint16_t));
|
||||
testArray[2] = 1;
|
||||
testArray[3] = 1;
|
||||
testArray[4] = 1;
|
||||
testArray[5] = 0;
|
||||
std::array<uint8_t, 4> test_recv_array;
|
||||
arrayPtr = testArray.data() + 2;
|
||||
// copy testArray[1] to testArray[4] into receive buffer, skip
|
||||
// size field (testArray[0])
|
||||
SerialBufferAdapter<uint16_t> tv_serial_buffer_adapter3 =
|
||||
SerialBufferAdapter<uint16_t>(test_recv_array.data(), 4, false);
|
||||
// Deserialization
|
||||
size_t size = 4;
|
||||
tv_serial_buffer_adapter3.deSerialize(
|
||||
const_cast<const uint8_t**>(&arrayPtr), &size,
|
||||
SerializeIF::Endianness::MACHINE);
|
||||
CHECK(test_recv_array[0] == 1);
|
||||
CHECK(test_recv_array[1] == 1);
|
||||
CHECK(test_recv_array[2] == 1);
|
||||
CHECK(test_recv_array[3] == 0);
|
||||
}
|
||||
}
|
73
unittest/tests/serialize/TestSerialLinkedPacket.cpp
Normal file
73
unittest/tests/serialize/TestSerialLinkedPacket.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
#include <fsfw/globalfunctions/arrayprinter.h>
|
||||
|
||||
#include <fsfw/unittest/catch2/catch.hpp>
|
||||
#include <fsfw/unittest/core/CatchDefinitions.h>
|
||||
#include <fsfw/unittest/tests/serialize/TestSerialLinkedPacket.h>
|
||||
|
||||
|
||||
TEST_CASE("Serial Linked Packet" , "[SerLinkPacket]") {
|
||||
// perform set-up here
|
||||
uint32_t header = 42;
|
||||
std::array<uint8_t, 3> testArray {1,2,3};
|
||||
uint32_t tail = 96;
|
||||
size_t packetMaxSize = 256;
|
||||
uint8_t packet [packetMaxSize] = {};
|
||||
size_t packetLen = 0;
|
||||
|
||||
SECTION("Test Deserialization with Serial Buffer Adapter.") {
|
||||
// This is a serialization of a packet, made "manually".
|
||||
// We generate a packet which store data big-endian by swapping some
|
||||
// values. (like coming from ground).
|
||||
header = EndianConverter::convertBigEndian(header);
|
||||
std::memcpy(packet, &header, sizeof(header));
|
||||
packetLen += sizeof(header);
|
||||
|
||||
std::copy(testArray.data(), testArray.data() + testArray.size(),
|
||||
packet + packetLen);
|
||||
packetLen += testArray.size();
|
||||
|
||||
tail = EndianConverter::convertBigEndian(tail);
|
||||
std::memcpy(packet + packetLen, &tail, sizeof(tail));
|
||||
packetLen += sizeof(tail);
|
||||
|
||||
//arrayprinter::print(packet, packetLen, OutputType::DEC);
|
||||
|
||||
// This is the buffer which will be filled when testClass.deSerialize
|
||||
// is called.
|
||||
std::array<uint8_t, 3> bufferAdaptee = {};
|
||||
TestPacket testClass(packet, packetLen, bufferAdaptee.data(),
|
||||
bufferAdaptee.size());
|
||||
const uint8_t* readOnlyPointer = packet;
|
||||
// Deserialize big endian packet by setting bigEndian to true.
|
||||
ReturnValue_t result = testClass.deSerialize(&readOnlyPointer,
|
||||
&packetLen, SerializeIF::Endianness::BIG);
|
||||
REQUIRE(result == retval::CATCH_OK);
|
||||
CHECK(testClass.getHeader() == 42);
|
||||
// Equivalent check.
|
||||
// CHECK(testClass.getBuffer()[0] == 1);
|
||||
CHECK(bufferAdaptee[0] == 1);
|
||||
CHECK(bufferAdaptee[1] == 2);
|
||||
CHECK(bufferAdaptee[2] == 3);
|
||||
CHECK(testClass.getTail() == 96);
|
||||
}
|
||||
|
||||
SECTION("Test Serialization") {
|
||||
// Same process as performed in setup, this time using the class
|
||||
// instead of doing it manually.
|
||||
TestPacket testClass(header, tail, testArray.data(), testArray.size());
|
||||
size_t serializedSize = 0;
|
||||
uint8_t* packetPointer = packet;
|
||||
// serialize for ground: bigEndian = true.
|
||||
ReturnValue_t result = testClass.serialize(&packetPointer,
|
||||
&serializedSize, packetMaxSize, SerializeIF::Endianness::BIG);
|
||||
REQUIRE(result == retval::CATCH_OK);
|
||||
// Result should be big endian now.
|
||||
CHECK(packet[3] == 42);
|
||||
CHECK(packet[4] == 1);
|
||||
CHECK(packet[5] == 2);
|
||||
CHECK(packet[6] == 3);
|
||||
CHECK(packet[10] == 96);
|
||||
}
|
||||
|
||||
// perform tear-down here
|
||||
}
|
61
unittest/tests/serialize/TestSerialLinkedPacket.h
Normal file
61
unittest/tests/serialize/TestSerialLinkedPacket.h
Normal file
@ -0,0 +1,61 @@
|
||||
#ifndef UNITTEST_HOSTED_TESTSERIALLINKEDPACKET_H_
|
||||
#define UNITTEST_HOSTED_TESTSERIALLINKEDPACKET_H_
|
||||
|
||||
#include <fsfw/objectmanager/SystemObjectIF.h>
|
||||
#include <fsfw/parameters/HasParametersIF.h>
|
||||
#include <fsfw/serialize/SerialBufferAdapter.h>
|
||||
#include <fsfw/serialize/SerialLinkedListAdapter.h>
|
||||
#include <cstdint>
|
||||
|
||||
class TestPacket: public SerialLinkedListAdapter<SerializeIF> {
|
||||
public:
|
||||
/**
|
||||
* For Deserialization
|
||||
*/
|
||||
TestPacket(const uint8_t *somePacket, size_t size, uint8_t * storePointer,
|
||||
size_t storeSize):
|
||||
buffer(storePointer, storeSize)
|
||||
{
|
||||
setLinks();
|
||||
}
|
||||
|
||||
/**
|
||||
* For Serialization
|
||||
*/
|
||||
TestPacket(uint32_t header, uint32_t tail,
|
||||
const uint8_t* parameters, size_t paramSize):
|
||||
header(header), buffer(parameters, paramSize),
|
||||
tail(tail) {
|
||||
setLinks();
|
||||
}
|
||||
|
||||
uint32_t getHeader() const {
|
||||
return header.entry;
|
||||
}
|
||||
|
||||
const uint8_t * getBuffer() {
|
||||
return buffer.entry.getConstBuffer();
|
||||
}
|
||||
|
||||
const size_t getBufferLength() {
|
||||
return buffer.getSerializedSize();
|
||||
}
|
||||
|
||||
|
||||
uint16_t getTail() const {
|
||||
return tail.entry;
|
||||
}
|
||||
private:
|
||||
void setLinks() {
|
||||
setStart(&header);
|
||||
header.setNext(&buffer);
|
||||
buffer.setNext(&tail);
|
||||
tail.setEnd();
|
||||
}
|
||||
|
||||
SerializeElement<uint32_t> header = 0;
|
||||
SerializeElement<SerialBufferAdapter<uint8_t>> buffer;
|
||||
SerializeElement<uint32_t> tail = 0;
|
||||
};
|
||||
|
||||
#endif /* UNITTEST_TESTFW_NEWTESTS_TESTTEMPLATE_H_ */
|
129
unittest/tests/serialize/TestSerialization.cpp
Normal file
129
unittest/tests/serialize/TestSerialization.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include <fsfw/serialize/SerializeAdapter.h>
|
||||
|
||||
#include "catch.hpp"
|
||||
#include <array>
|
||||
#include "core/CatchDefinitions.h"
|
||||
|
||||
static bool test_value_bool = true;
|
||||
static uint8_t tv_uint8 {5};
|
||||
static uint16_t tv_uint16 {283};
|
||||
static uint32_t tv_uint32 {929221};
|
||||
static uint64_t tv_uint64 {2929329429};
|
||||
|
||||
static int8_t tv_int8 {-16};
|
||||
static int16_t tv_int16 {-829};
|
||||
static int32_t tv_int32 {-2312};
|
||||
|
||||
static float tv_float {8.2149214};
|
||||
static float tv_sfloat = {-922.2321321};
|
||||
static double tv_double {9.2132142141e8};
|
||||
static double tv_sdouble {-2.2421e19};
|
||||
|
||||
static std::array<uint8_t, 512> test_array;
|
||||
|
||||
TEST_CASE( "Serialization size tests", "[TestSerialization]") {
|
||||
//REQUIRE(unitTestClass.test_autoserialization() == 0);
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&test_value_bool) ==
|
||||
sizeof(test_value_bool));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint8) ==
|
||||
sizeof(tv_uint8));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint16) ==
|
||||
sizeof(tv_uint16));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint32 ) ==
|
||||
sizeof(tv_uint32));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint64) ==
|
||||
sizeof(tv_uint64));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_int8) ==
|
||||
sizeof(tv_int8));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_int16) ==
|
||||
sizeof(tv_int16));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_int32) ==
|
||||
sizeof(tv_int32));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_float) ==
|
||||
sizeof(tv_float));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_sfloat) ==
|
||||
sizeof(tv_sfloat ));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_double) ==
|
||||
sizeof(tv_double));
|
||||
REQUIRE(SerializeAdapter::getSerializedSize(&tv_sdouble) ==
|
||||
sizeof(tv_sdouble));
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Auto Serialize Adapter testing", "[single-file]") {
|
||||
size_t serialized_size = 0;
|
||||
uint8_t * p_array = test_array.data();
|
||||
|
||||
SECTION("Serializing...") {
|
||||
SerializeAdapter::serialize(&test_value_bool, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint8, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint16, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint32, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_int8, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_int16, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_int32, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_uint64, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_float, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_double, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_sfloat, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::serialize(&tv_sdouble, &p_array,
|
||||
&serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE);
|
||||
REQUIRE (serialized_size == 47);
|
||||
}
|
||||
|
||||
SECTION("Deserializing") {
|
||||
p_array = test_array.data();
|
||||
size_t remaining_size = serialized_size;
|
||||
SerializeAdapter::deSerialize(&test_value_bool,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_uint8,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_uint16,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_uint32,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_int8,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_int16,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_int32,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_uint64,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_float,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_double,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_sfloat,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
SerializeAdapter::deSerialize(&tv_sdouble,
|
||||
const_cast<const uint8_t**>(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE);
|
||||
|
||||
REQUIRE(test_value_bool == true);
|
||||
REQUIRE(tv_uint8 == 5);
|
||||
REQUIRE(tv_uint16 == 283);
|
||||
REQUIRE(tv_uint32 == 929221);
|
||||
REQUIRE(tv_uint64 == 2929329429);
|
||||
REQUIRE(tv_int8 == -16);
|
||||
REQUIRE(tv_int16 == -829);
|
||||
REQUIRE(tv_int32 == -2312);
|
||||
|
||||
REQUIRE(tv_float == Approx(8.214921));
|
||||
REQUIRE(tv_double == Approx(9.2132142141e8));
|
||||
REQUIRE(tv_sfloat == Approx(-922.2321321));
|
||||
REQUIRE(tv_sdouble == Approx(-2.2421e19));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user