From bdd79d060df5ddde24da71b3a7df83ffbf486be9 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 30 Aug 2022 14:02:58 +0200 Subject: [PATCH] basic data wrapper unittests --- src/fsfw/util/dataWrapper.h | 5 ++-- unittests/util/CMakeLists.txt | 1 + unittests/util/testDataWrapper.cpp | 31 ++++++++++++++++++++++++ unittests/util/testUnsignedByteField.cpp | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 unittests/util/testDataWrapper.cpp diff --git a/src/fsfw/util/dataWrapper.h b/src/fsfw/util/dataWrapper.h index 8bb373e6..440f9501 100644 --- a/src/fsfw/util/dataWrapper.h +++ b/src/fsfw/util/dataWrapper.h @@ -10,6 +10,7 @@ namespace util { struct RawData { + RawData() = default; const uint8_t* data = nullptr; size_t len = 0; }; @@ -17,8 +18,8 @@ struct RawData { enum DataTypes { NONE, RAW, SERIALIZABLE }; union DataUnion { - RawData raw; - SerializeIF* serializable = nullptr; + RawData raw{}; + SerializeIF* serializable; }; struct DataWrapper { diff --git a/unittests/util/CMakeLists.txt b/unittests/util/CMakeLists.txt index d4caa4d5..dd9f6dc1 100644 --- a/unittests/util/CMakeLists.txt +++ b/unittests/util/CMakeLists.txt @@ -1,3 +1,4 @@ target_sources(${FSFW_TEST_TGT} PRIVATE testUnsignedByteField.cpp + testDataWrapper.cpp ) diff --git a/unittests/util/testDataWrapper.cpp b/unittests/util/testDataWrapper.cpp new file mode 100644 index 00000000..5a6b26d7 --- /dev/null +++ b/unittests/util/testDataWrapper.cpp @@ -0,0 +1,31 @@ +#include +#include + +#include "fsfw/util/dataWrapper.h" +#include "mocks/SimpleSerializable.h" + +TEST_CASE("Data Wrapper", "[util]") { + util::DataWrapper wrapper; + SECTION("State") { + REQUIRE(wrapper.isNull()); + } + + SECTION("Set Raw Data") { + REQUIRE(wrapper.isNull()); + std::array data = {1, 2, 3, 4}; + wrapper.setRawData({data.data(), data.size()}); + REQUIRE(not wrapper.isNull()); + REQUIRE(wrapper.type == util::DataTypes::RAW); + REQUIRE(wrapper.dataUnion.raw.data == data.data()); + REQUIRE(wrapper.dataUnion.raw.len == data.size()); + } + + SECTION("Simple Serializable") { + REQUIRE(wrapper.isNull()); + SimpleSerializable serializable; + wrapper.setSerializable(serializable); + REQUIRE(not wrapper.isNull()); + REQUIRE(wrapper.type == util::DataTypes::SERIALIZABLE); + REQUIRE(wrapper.dataUnion.serializable == &serializable); + } +} \ No newline at end of file diff --git a/unittests/util/testUnsignedByteField.cpp b/unittests/util/testUnsignedByteField.cpp index df1ff483..58a87a7d 100644 --- a/unittests/util/testUnsignedByteField.cpp +++ b/unittests/util/testUnsignedByteField.cpp @@ -4,7 +4,7 @@ #include "fsfw/util/UnsignedByteField.h" -TEST_CASE("Unsigned Byte Field", "[unsigned-byte-field]") { +TEST_CASE("Unsigned Byte Field", "[util]") { auto testByteField = UnsignedByteField(10); auto u32ByteField = U32ByteField(10); auto u16ByteField = U16ByteField(5);