From 8d1777fa0c7b330fedfdc834fa15da50bcd9df17 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 30 Aug 2022 16:02:50 +0200 Subject: [PATCH] additional tests --- src/fsfw/util/dataWrapper.h | 21 +++++++++++-- unittests/util/testDataWrapper.cpp | 47 +++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/fsfw/util/dataWrapper.h b/src/fsfw/util/dataWrapper.h index 440f9501..ac4404f1 100644 --- a/src/fsfw/util/dataWrapper.h +++ b/src/fsfw/util/dataWrapper.h @@ -9,6 +9,8 @@ namespace util { +using BufPair = std::pair; + struct RawData { RawData() = default; const uint8_t* data = nullptr; @@ -23,9 +25,24 @@ union DataUnion { }; struct DataWrapper { + + + DataWrapper() = default; + + DataWrapper(const uint8_t* data, size_t size): type(DataTypes::RAW) { + setRawData({data, size}); + } + + explicit DataWrapper(BufPair raw): type(DataTypes::RAW) { + setRawData(raw); + } + + explicit DataWrapper(SerializeIF& serializable): type(DataTypes::SERIALIZABLE) { + setSerializable(serializable); + } + DataTypes type = DataTypes::NONE; DataUnion dataUnion; - using BufPairT = std::pair; [[nodiscard]] size_t getLength() const { if (type == DataTypes::RAW) { @@ -43,7 +60,7 @@ struct DataWrapper { } return false; } - void setRawData(BufPairT bufPair) { + void setRawData(BufPair bufPair) { type = DataTypes::RAW; dataUnion.raw.data = bufPair.first; dataUnion.raw.len = bufPair.second; diff --git a/unittests/util/testDataWrapper.cpp b/unittests/util/testDataWrapper.cpp index 14ba287a..72931d90 100644 --- a/unittests/util/testDataWrapper.cpp +++ b/unittests/util/testDataWrapper.cpp @@ -12,21 +12,48 @@ TEST_CASE("Data Wrapper", "[util]") { } SECTION("Set Raw Data") { + util::DataWrapper* instance = &wrapper; + bool deleteInst = false; 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("Setter") { + wrapper.setRawData({data.data(), data.size()}); + } + SECTION("Direct Construction Pair") { + instance = new util::DataWrapper(util::BufPair(data.data(), data.size())); + deleteInst = true; + } + SECTION("Direct Construction Single Args") { + instance = new util::DataWrapper(data.data(), data.size()); + deleteInst = true; + } + REQUIRE(not instance->isNull()); + REQUIRE(instance->type == util::DataTypes::RAW); + REQUIRE(instance->dataUnion.raw.data == data.data()); + REQUIRE(instance->dataUnion.raw.len == data.size()); + if(deleteInst) { + delete instance; + } } SECTION("Simple Serializable") { - REQUIRE(wrapper.isNull()); + util::DataWrapper* instance = &wrapper; + bool deleteInst = false; + REQUIRE(instance->isNull()); SimpleSerializable serializable; - wrapper.setSerializable(serializable); - REQUIRE(not wrapper.isNull()); - REQUIRE(wrapper.type == util::DataTypes::SERIALIZABLE); - REQUIRE(wrapper.dataUnion.serializable == &serializable); + SECTION("Setter") { + wrapper.setSerializable(serializable); + } + SECTION("Direct Construction") { + instance = new util::DataWrapper(serializable); + deleteInst = true; + } + + REQUIRE(not instance->isNull()); + REQUIRE(instance->type == util::DataTypes::SERIALIZABLE); + REQUIRE(instance->dataUnion.serializable == &serializable); + if(deleteInst) { + delete instance; + } } } \ No newline at end of file