Add more tests for sf::Packet

This commit is contained in:
Chris Thrasher 2022-09-03 18:26:23 -06:00
parent 12aaa4d08a
commit 28a4a88832

View File

@ -2,20 +2,62 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include <array>
#include <limits> #include <limits>
#define CHECK_PACKET_STREAM_OPERATORS(expected) \ #define CHECK_PACKET_STREAM_OPERATORS(expected) \
do \ do \
{ \ { \
sf::Packet packet; \ sf::Packet packet; \
packet << expected; \ packet << expected; \
decltype(expected) received; \ CHECK(packet.getReadPosition() == 0); \
packet >> received; \ CHECK(packet.getData() != nullptr); \
CHECK(expected == received); \ CHECK(packet.getDataSize() == sizeof(expected)); \
CHECK(!packet.endOfPacket()); \
CHECK(static_cast<bool>(packet)); \
\
decltype(expected) received; \
packet >> received; \
CHECK(packet.getReadPosition() == sizeof(expected)); \
CHECK(packet.getData() != nullptr); \
CHECK(packet.getDataSize() == sizeof(expected)); \
CHECK(packet.endOfPacket()); \
CHECK(static_cast<bool>(packet)); \
CHECK(expected == received); \
} while (false) } while (false)
TEST_CASE("sf::Packet class - [network]") TEST_CASE("sf::Packet class - [network]")
{ {
SUBCASE("Default constructor")
{
const sf::Packet packet;
CHECK(packet.getReadPosition() == 0);
CHECK(packet.getData() == nullptr);
CHECK(packet.getDataSize() == 0);
CHECK(packet.endOfPacket());
CHECK(static_cast<bool>(packet));
}
SUBCASE("Append and clear")
{
constexpr std::array data = {1, 2, 3, 4, 5, 6};
sf::Packet packet;
packet.append(data.data(), data.size());
CHECK(packet.getReadPosition() == 0);
CHECK(packet.getData() != nullptr);
CHECK(packet.getDataSize() == data.size());
CHECK(!packet.endOfPacket());
CHECK(static_cast<bool>(packet));
packet.clear();
CHECK(packet.getReadPosition() == 0);
CHECK(packet.getData() == nullptr);
CHECK(packet.getDataSize() == 0);
CHECK(packet.endOfPacket());
CHECK(static_cast<bool>(packet));
}
SUBCASE("Stream operators") SUBCASE("Stream operators")
{ {
SUBCASE("std::int8_t") SUBCASE("std::int8_t")