Merge branch 'mueller/expand-serialize-if' into mueller/new-object-id-type

This commit is contained in:
Robin Müller 2022-07-29 14:16:22 +02:00
commit c9bec03f00
2 changed files with 63 additions and 27 deletions

View File

@ -125,22 +125,26 @@ class SerializeIF {
* @param streamEndianness
* @return
*/
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize,
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t &serLen, size_t maxSize,
Endianness streamEndianness) const {
size_t tmpSize = 0;
return serialize(&buffer, &tmpSize, maxSize, streamEndianness);
ReturnValue_t result = serialize(&buffer, &tmpSize, maxSize, streamEndianness);
serLen = tmpSize;
return result;
}
/**
* Forwards to regular @serialize call with big (network) endianness
*/
[[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t maxSize) const {
return serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
[[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t &serLen,
size_t maxSize) const {
return serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK);
}
/**
* If endianness is not explicitly specified, use machine endianness
*/
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize) const {
return serialize(buffer, maxSize, SerializeIF::Endianness::MACHINE);
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t &serLen,
size_t maxSize) const {
return serialize(buffer, serLen, maxSize, SerializeIF::Endianness::MACHINE);
}
/**
@ -151,21 +155,24 @@ class SerializeIF {
* @param streamEndianness
* @return
*/
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize,
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t &deserSize, size_t maxSize,
Endianness streamEndianness) {
return deSerialize(&buffer, &maxSize, streamEndianness);
size_t deserLen = maxSize;
ReturnValue_t result = deSerialize(&buffer, &deserLen, streamEndianness);
deserSize = maxSize - deserLen;
return result;
}
/**
* Forwards to regular @serialize call with big (network) endianness
*/
virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t maxSize) {
return deSerialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t &deserSize, size_t maxSize) {
return deSerialize(buffer, deserSize, maxSize, SerializeIF::Endianness::NETWORK);
}
/**
* If endianness is not explicitly specified, use machine endianness
*/
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize) {
return deSerialize(buffer, maxSize, SerializeIF::Endianness::MACHINE);
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t &deserSize, size_t maxSize) {
return deSerialize(buffer, deserSize, maxSize, SerializeIF::Endianness::MACHINE);
}
};

View File

@ -13,7 +13,8 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
size_t len = 0;
SECTION("Little Endian Normal") {
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::LITTLE) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 1);
CHECK(buf[1] == 3);
CHECK(buf[2] == 2);
@ -23,10 +24,14 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
}
SECTION("Little Endian Simple") {
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::LITTLE) == HasReturnvaluesIF::RETURN_OK);
size_t serLen = 0xff;
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 1);
CHECK(buf[1] == 3);
CHECK(buf[2] == 2);
CHECK(serLen == 3);
}
SECTION("Big Endian Normal") {
@ -35,7 +40,8 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 0") {
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.serialize(&ptr, &len, buf.size(), SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 1") {
REQUIRE(simpleSer.serializeBe(&ptr, &len, buf.size()) == HasReturnvaluesIF::RETURN_OK);
@ -50,19 +56,25 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
}
SECTION("Big Endian Simple") {
size_t serLen = 0xff;
SECTION("Explicit") {
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::BIG) ==
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::BIG) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 0") {
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 1") {
REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
}
CHECK(buf[0] == 1);
CHECK(buf[1] == 2);
CHECK(buf[2] == 3);
CHECK(serLen == 3);
}
SECTION("Machine Endian Implicit") {
@ -82,7 +94,8 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
}
SECTION("Machine Endian Simple Implicit") {
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size()) ==
size_t serLen = 0xff;
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 1);
#if BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
@ -92,6 +105,7 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
CHECK(buf[1] == 2);
CHECK(buf[2] == 3);
#endif
CHECK(serLen == 3);
}
}
@ -102,7 +116,8 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
size_t len = buf.size();
SECTION("Little Endian Normal") {
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::LITTLE) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 0x0100);
CHECK(ptr == buf.data() + 3);
@ -110,17 +125,23 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
}
SECTION("Little Endian Simple") {
REQUIRE(simpleSer.SerializeIF::deSerialize(ptr, len, SerializeIF::Endianness::LITTLE) == HasReturnvaluesIF::RETURN_OK);
size_t deserLen = 0xff;
REQUIRE(
simpleSer.SerializeIF::deSerialize(ptr, deserLen, len, SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 0x0100);
CHECK(deserLen == 3);
}
SECTION("Big Endian Normal") {
SECTION("Explicit") {
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::BIG) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::BIG) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 0") {
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.deSerialize(&ptr, &len, SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 1") {
REQUIRE(simpleSer.SerializeIF::deSerializeBe(&ptr, &len) == HasReturnvaluesIF::RETURN_OK);
@ -132,18 +153,24 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
}
SECTION("Big Endian Simple") {
size_t deserLen = 0xff;
SECTION("Explicit") {
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size(), SerializeIF::Endianness::BIG) ==
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(),
SerializeIF::Endianness::BIG) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 0") {
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size(), SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(),
SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 1") {
REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), deserLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
}
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 1);
CHECK(deserLen == 3);
}
SECTION("Machine Endian Implicit") {
@ -160,7 +187,8 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
}
SECTION("Machine Endian Simple Implicit") {
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size()) ==
size_t deserLen = 0xff;
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(simpleSer.getU8() == 5);
#if BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
@ -168,5 +196,6 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
#else
CHECK(simpleSer.getU16() == 1);
#endif
CHECK(deserLen == 3);
}
}