Merge remote-tracking branch 'upstream/mueller/data-wrapper' into develop

This commit is contained in:
Robin Müller 2022-08-30 16:05:02 +02:00
commit 6ebd6a965b
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 44 additions and 13 deletions

View File

@ -9,6 +9,8 @@
namespace util {
using BufPair = std::pair<const uint8_t*, size_t>;
struct RawData {
RawData() = default;
const uint8_t* data = nullptr;
@ -23,7 +25,6 @@ union DataUnion {
};
struct DataWrapper {
using BufPairT = std::pair<const uint8_t*, size_t>;
DataWrapper() = default;
@ -31,7 +32,7 @@ struct DataWrapper {
setRawData({data, size});
}
explicit DataWrapper(BufPairT raw): type(DataTypes::RAW) {
explicit DataWrapper(BufPair raw): type(DataTypes::RAW) {
setRawData(raw);
}
@ -42,7 +43,6 @@ struct DataWrapper {
DataTypes type = DataTypes::NONE;
DataUnion dataUnion;
[[nodiscard]] size_t getLength() const {
if (type == DataTypes::RAW) {
return dataUnion.raw.len;
@ -59,8 +59,12 @@ struct DataWrapper {
}
return false;
}
<<<<<<< HEAD
void setRawData(BufPairT bufPair) {
=======
void setRawData(BufPair bufPair) {
>>>>>>> upstream/mueller/data-wrapper
type = DataTypes::RAW;
dataUnion.raw.data = bufPair.first;
dataUnion.raw.len = bufPair.second;

View File

@ -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<uint8_t, 4> 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;
}
}
}