2022-09-04 07:47:37 +08:00
|
|
|
#include <SFML/Network/Packet.hpp>
|
2021-12-06 06:27:48 +08:00
|
|
|
|
2022-07-18 06:18:40 +08:00
|
|
|
#include <doctest/doctest.h>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
2022-09-04 08:26:23 +08:00
|
|
|
#include <array>
|
2021-12-06 06:27:48 +08:00
|
|
|
#include <limits>
|
|
|
|
|
2022-09-04 08:26:23 +08:00
|
|
|
#define CHECK_PACKET_STREAM_OPERATORS(expected) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
sf::Packet packet; \
|
|
|
|
packet << expected; \
|
|
|
|
CHECK(packet.getReadPosition() == 0); \
|
|
|
|
CHECK(packet.getData() != nullptr); \
|
|
|
|
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); \
|
2022-09-04 07:47:52 +08:00
|
|
|
} while (false)
|
2021-12-06 06:27:48 +08:00
|
|
|
|
2022-07-05 13:51:18 +08:00
|
|
|
TEST_CASE("[Network] sf::Packet")
|
2021-12-06 06:27:48 +08:00
|
|
|
{
|
2022-09-04 08:26:23 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Stream operators")
|
2021-12-06 06:27:48 +08:00
|
|
|
{
|
2022-09-04 03:58:02 +08:00
|
|
|
SUBCASE("std::int8_t")
|
2021-12-06 06:27:48 +08:00
|
|
|
{
|
2022-09-04 07:47:52 +08:00
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int8_t(0));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int8_t(1));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int8_t>::min());
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int8_t>::max());
|
2021-12-06 06:27:48 +08:00
|
|
|
}
|
|
|
|
|
2022-09-04 07:36:41 +08:00
|
|
|
SUBCASE("std::int16_t")
|
2021-12-06 06:27:48 +08:00
|
|
|
{
|
2022-09-04 07:47:52 +08:00
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int16_t(0));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int16_t(1));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int16_t>::min());
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int16_t>::max());
|
2021-12-06 06:27:48 +08:00
|
|
|
}
|
|
|
|
|
2022-09-13 04:12:53 +08:00
|
|
|
SUBCASE("std::int32_t")
|
2021-12-06 06:27:48 +08:00
|
|
|
{
|
2022-09-04 07:47:52 +08:00
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int32_t(0));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int32_t(1));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int32_t>::min());
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int32_t>::max());
|
2021-12-06 06:27:48 +08:00
|
|
|
}
|
|
|
|
|
2022-09-13 05:44:40 +08:00
|
|
|
SUBCASE("std::int64_t")
|
2021-12-06 06:27:48 +08:00
|
|
|
{
|
2022-09-04 07:47:52 +08:00
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int64_t(0));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::int64_t(1));
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int64_t>::min());
|
|
|
|
CHECK_PACKET_STREAM_OPERATORS(std::numeric_limits<std::int64_t>::max());
|
2021-12-06 06:27:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|