From 11020363b1338c05b421cdffaac2c57c20750a09 Mon Sep 17 00:00:00 2001 From: Pawel Paruzel Date: Sun, 5 Dec 2021 23:27:48 +0100 Subject: [PATCH] Fix wrong cast in sf::Packet Add unit tests --- src/SFML/Network/Packet.cpp | 4 +-- test/CMakeLists.txt | 10 ++++++- test/src/Network/Packet.cpp | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/src/Network/Packet.cpp diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index 22315d8b7..7c54e7456 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -178,7 +178,7 @@ Packet& Packet::operator >>(Int32& data) if (checkSize(sizeof(data))) { std::memcpy(&data, &m_data[m_readPos], sizeof(data)); - data = static_cast(ntohl(static_cast(data))); + data = static_cast(ntohl(static_cast(data))); m_readPos += sizeof(data); } @@ -430,7 +430,7 @@ Packet& Packet::operator <<(Uint16 data) //////////////////////////////////////////////////////////// Packet& Packet::operator <<(Int32 data) { - Int32 toWrite = static_cast(htonl(static_cast(data))); + Int32 toWrite = static_cast(htonl(static_cast(data))); append(&toWrite, sizeof(toWrite)); return *this; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9e9f6111a..05b0223f4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,9 +33,17 @@ if(SFML_BUILD_GRAPHICS) sfml_add_test(test-sfml-graphics "${GRAPHICS_SRC}" sfml-graphics) endif() +if(SFML_BUILD_NETWORK) + SET(NETWORK_SRC + "${SRCROOT}/CatchMain.cpp" + "${SRCROOT}/Network/Packet.cpp" + ) + sfml_add_test(test-sfml-network "${NETWORK_SRC}" sfml-network) +endif() + # Automatically run the tests at the end of the build add_custom_target(runtests ALL - DEPENDS test-sfml-system test-sfml-window test-sfml-graphics + DEPENDS test-sfml-system test-sfml-window test-sfml-graphics test-sfml-network ) add_custom_command(TARGET runtests diff --git a/test/src/Network/Packet.cpp b/test/src/Network/Packet.cpp new file mode 100644 index 000000000..6bd89cb68 --- /dev/null +++ b/test/src/Network/Packet.cpp @@ -0,0 +1,52 @@ +#include + +#include +#include + +template +static void testPacketStreamOperators(IntegerType expected) +{ + sf::Packet packet; + packet << expected; + IntegerType received; + packet >> received; + CHECK(expected == received); +} + +TEST_CASE("sf::Packet class", "[network]") +{ + SECTION("Stream operators") + { + SECTION("Int8") + { + testPacketStreamOperators(sf::Int8(0)); + testPacketStreamOperators(sf::Int8(1)); + testPacketStreamOperators(std::numeric_limits::min()); + testPacketStreamOperators(std::numeric_limits::max()); + } + + SECTION("Int16") + { + testPacketStreamOperators(sf::Int16(0)); + testPacketStreamOperators(sf::Int16(1)); + testPacketStreamOperators(std::numeric_limits::min()); + testPacketStreamOperators(std::numeric_limits::max()); + } + + SECTION("Int32") + { + testPacketStreamOperators(sf::Int32(0)); + testPacketStreamOperators(sf::Int32(1)); + testPacketStreamOperators(std::numeric_limits::min()); + testPacketStreamOperators(std::numeric_limits::max()); + } + + SECTION("Int64") + { + testPacketStreamOperators(sf::Int64(0)); + testPacketStreamOperators(sf::Int64(1)); + testPacketStreamOperators(std::numeric_limits::min()); + testPacketStreamOperators(std::numeric_limits::max()); + } + } +}