Test for correct network ordering of packets

This commit is contained in:
Chris Thrasher 2023-01-26 21:33:05 -07:00
parent e5b85fc9b7
commit 865a50899c

View File

@ -5,6 +5,9 @@
#include <array> #include <array>
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include <vector>
#include <cstddef>
#define CHECK_PACKET_STREAM_OPERATORS(expected) \ #define CHECK_PACKET_STREAM_OPERATORS(expected) \
do \ do \
@ -67,6 +70,54 @@ TEST_CASE("[Network] sf::Packet")
CHECK(static_cast<bool>(packet)); CHECK(static_cast<bool>(packet));
} }
SECTION("Network ordering")
{
sf::Packet packet;
SECTION("16 bit int")
{
packet << std::uint16_t{12'345};
const auto* dataPtr = static_cast<const std::byte*>(packet.getData());
const std::vector bytes(dataPtr, dataPtr + packet.getDataSize());
const std::vector expectedBytes{std::byte{0x30}, std::byte{0x39}};
CHECK(bytes == expectedBytes);
}
SECTION("32 bit int")
{
packet << std::uint32_t{1'234'567'890};
const auto* dataPtr = static_cast<const std::byte*>(packet.getData());
const std::vector bytes(dataPtr, dataPtr + packet.getDataSize());
const std::vector expectedBytes{std::byte{0x49}, std::byte{0x96}, std::byte{0x02}, std::byte{0xD2}};
CHECK(bytes == expectedBytes);
}
SECTION("float")
{
packet << 123.456f;
const auto* dataPtr = static_cast<const std::byte*>(packet.getData());
const std::vector bytes(dataPtr, dataPtr + packet.getDataSize());
const std::vector expectedBytes{std::byte{0x79}, std::byte{0xe9}, std::byte{0xf6}, std::byte{0x42}};
CHECK(bytes == expectedBytes);
}
SECTION("double")
{
packet << 789.123;
const auto* dataPtr = static_cast<const std::byte*>(packet.getData());
const std::vector bytes(dataPtr, dataPtr + packet.getDataSize());
const std::vector expectedBytes{std::byte{0x44},
std::byte{0x8b},
std::byte{0x6c},
std::byte{0xe7},
std::byte{0xfb},
std::byte{0xa8},
std::byte{0x88},
std::byte{0x40}};
CHECK(bytes == expectedBytes);
}
}
SECTION("Stream operators") SECTION("Stream operators")
{ {
SECTION("std::int8_t") SECTION("std::int8_t")