SFML/test/Network/Packet.test.cpp

105 lines
3.9 KiB
C++
Raw Normal View History

2022-09-04 07:47:37 +08:00
#include <SFML/Network/Packet.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-07-05 00:20:58 +08:00
2022-09-04 08:26:23 +08:00
#include <array>
#include <limits>
#include <type_traits>
2022-09-04 08:26:23 +08:00
#define CHECK_PACKET_STREAM_OPERATORS(expected) \
do \
{ \
sf::Packet packet; \
2023-02-03 14:31:48 +08:00
packet << (expected); \
2022-09-04 08:26:23 +08:00
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)); \
2023-02-03 14:31:48 +08:00
CHECK((expected) == received); \
} while (false)
TEST_CASE("[Network] sf::Packet")
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
STATIC_CHECK(std::is_copy_constructible_v<sf::Packet>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Packet>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Packet>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Packet>);
}
SECTION("Default constructor")
2022-09-04 08:26:23 +08:00
{
const sf::Packet packet;
CHECK(packet.getReadPosition() == 0);
CHECK(packet.getData() == nullptr);
CHECK(packet.getDataSize() == 0);
CHECK(packet.endOfPacket());
CHECK(static_cast<bool>(packet));
}
2023-01-18 12:51:08 +08:00
SECTION("Append and clear")
2022-09-04 08:26:23 +08:00
{
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));
}
2023-01-18 12:51:08 +08:00
SECTION("Stream operators")
{
2023-01-18 12:51:08 +08:00
SECTION("std::int8_t")
{
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());
}
2023-01-18 12:51:08 +08:00
SECTION("std::int16_t")
{
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());
}
2023-01-18 12:51:08 +08:00
SECTION("std::int32_t")
{
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());
}
2023-01-18 12:51:08 +08:00
SECTION("std::int64_t")
{
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());
}
}
}