additional tests

This commit is contained in:
Robin Müller 2022-08-30 16:02:50 +02:00
parent d675a789a2
commit 8d1777fa0c
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 56 additions and 12 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,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<const uint8_t*, size_t>;
[[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;

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;
}
}
}