Skip to content

Commit

Permalink
Test for correct network ordering of packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Jan 27, 2023
1 parent 0f2671a commit 26ae62e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/Network/Packet.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@
#include <doctest/doctest.h>

#include <array>
#include <cassert>
#include <cstddef>
#include <limits>
#include <sstream>
#include <type_traits>
#include <vector>

static_assert(std::is_copy_constructible_v<sf::Packet>);
static_assert(std::is_copy_assignable_v<sf::Packet>);
static_assert(std::is_nothrow_move_constructible_v<sf::Packet>);
static_assert(std::is_nothrow_move_assignable_v<sf::Packet>);

// Use StringMaker to avoid opening namespace std
namespace doctest
{
template <>
struct StringMaker<std::vector<std::byte>>
{
static String convert(const std::vector<std::byte>& vector)
{
assert(!vector.empty());
std::ostringstream out;
out << std::hex << "{ ";
for (std::size_t i = 0; i + 1 < vector.size(); ++i)
out << std::to_integer<int>(vector[i]) << ", ";
out << std::to_integer<int>(vector.back()) << " }";
return String(out.str().c_str());
}
};
} // namespace doctest

#define CHECK_PACKET_STREAM_OPERATORS(expected) \
do \
{ \
Expand Down Expand Up @@ -64,6 +87,45 @@ TEST_CASE("[Network] sf::Packet")
CHECK(static_cast<bool>(packet));
}

SUBCASE("Network ordering")
{
sf::Packet packet;

SUBCASE("32 bit int")
{
packet << std::uint32_t(1337);
const std::byte* dataPtr = static_cast<const std::byte*>(packet.getData());
const std::vector bytes(dataPtr, dataPtr + packet.getDataSize());
const std::vector expectedBytes{std::byte{0x00}, std::byte{0x00}, std::byte{0x05}, std::byte{0x39}};
CHECK(bytes == expectedBytes);
}

SUBCASE("float")
{
packet << 123.456f;
const std::byte* 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);
}

SUBCASE("double")
{
packet << 789.123;
const std::byte* 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);
}
}

SUBCASE("Stream operators")
{
SUBCASE("std::int8_t")
Expand Down

0 comments on commit 26ae62e

Please sign in to comment.