From 29ed8bf3a062a9e18d76be092f0e9d80de37afec Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 21 Aug 2023 21:38:10 -0600 Subject: [PATCH] Add tests for `sf::Socket` --- test/Network/Socket.test.cpp | 80 +++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 5 deletions(-) diff --git a/test/Network/Socket.test.cpp b/test/Network/Socket.test.cpp index 7493a55e..ec819eb4 100644 --- a/test/Network/Socket.test.cpp +++ b/test/Network/Socket.test.cpp @@ -1,9 +1,79 @@ #include +#include + #include -static_assert(!std::is_constructible_v); -static_assert(!std::is_copy_constructible_v); -static_assert(!std::is_copy_assignable_v); -static_assert(!std::is_nothrow_move_constructible_v); -static_assert(!std::is_nothrow_move_assignable_v); +class TestSocket : public sf::Socket +{ +public: + TestSocket() : sf::Socket(sf::Socket::Type::Udp) + { + } + + using sf::Socket::close; + using sf::Socket::create; + using sf::Socket::getHandle; +}; + +TEST_CASE("[Network] sf::Socket") +{ + SECTION("Type traits") + { + STATIC_CHECK(!std::is_constructible_v); + STATIC_CHECK(!std::is_copy_constructible_v); + STATIC_CHECK(!std::is_copy_assignable_v); + STATIC_CHECK(!std::is_nothrow_move_constructible_v); + STATIC_CHECK(!std::is_nothrow_move_assignable_v); + } + + SECTION("Constants") + { + STATIC_CHECK(sf::Socket::AnyPort == 0); + } + + const auto invalidHandle = static_cast(-1); + + SECTION("Construction") + { + const TestSocket testSocket; + CHECK(testSocket.isBlocking()); + CHECK(testSocket.getHandle() == invalidHandle); + } + + SECTION("Set/get blocking") + { + TestSocket testSocket; + testSocket.setBlocking(false); + CHECK(!testSocket.isBlocking()); + } + + SECTION("create()") + { + TestSocket testSocket; + testSocket.create(); + CHECK(testSocket.isBlocking()); + CHECK(testSocket.getHandle() != invalidHandle); + + // Recreate socket to ensure nothing changed + testSocket.create(); + CHECK(testSocket.isBlocking()); + CHECK(testSocket.getHandle() != invalidHandle); + } + + SECTION("close()") + { + TestSocket testSocket; + testSocket.create(); + CHECK(testSocket.isBlocking()); + CHECK(testSocket.getHandle() != invalidHandle); + testSocket.close(); + CHECK(testSocket.isBlocking()); + CHECK(testSocket.getHandle() == invalidHandle); + + // Reclose socket to ensure nothing changed + testSocket.close(); + CHECK(testSocket.isBlocking()); + CHECK(testSocket.getHandle() == invalidHandle); + } +}