From 63088c1bf151f56252dcb2b3ed243f262b2deed2 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 27 Aug 2023 00:26:00 -0600 Subject: [PATCH] Add tests for `sf::SocketSelector` I added move semantics while I was at it --- include/SFML/Network/SocketSelector.hpp | 32 +++++++++++++++++-------- src/SFML/Network/SocketSelector.cpp | 30 +++++++++++++---------- test/Network/SocketSelector.test.cpp | 29 +++++++++++++++++----- 3 files changed, 63 insertions(+), 28 deletions(-) diff --git a/include/SFML/Network/SocketSelector.hpp b/include/SFML/Network/SocketSelector.hpp index 765b7133c..43d3dede2 100644 --- a/include/SFML/Network/SocketSelector.hpp +++ b/include/SFML/Network/SocketSelector.hpp @@ -65,6 +65,28 @@ public: //////////////////////////////////////////////////////////// SocketSelector(const SocketSelector& copy); + //////////////////////////////////////////////////////////// + /// \brief Overload of assignment operator + /// + /// \param right Instance to assign + /// + /// \return Reference to self + /// + //////////////////////////////////////////////////////////// + SocketSelector& operator=(const SocketSelector& right); + + //////////////////////////////////////////////////////////// + /// \brief Move constructor + /// + //////////////////////////////////////////////////////////// + SocketSelector(SocketSelector&&) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Move assignment + /// + //////////////////////////////////////////////////////////// + SocketSelector& operator=(SocketSelector&&) noexcept; + //////////////////////////////////////////////////////////// /// \brief Add a new socket to the selector /// @@ -142,16 +164,6 @@ public: //////////////////////////////////////////////////////////// bool isReady(Socket& socket) const; - //////////////////////////////////////////////////////////// - /// \brief Overload of assignment operator - /// - /// \param right Instance to assign - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - SocketSelector& operator=(const SocketSelector& right); - private: struct SocketSelectorImpl; diff --git a/src/SFML/Network/SocketSelector.cpp b/src/SFML/Network/SocketSelector.cpp index 6c436d86e..f42d13a1c 100644 --- a/src/SFML/Network/SocketSelector.cpp +++ b/src/SFML/Network/SocketSelector.cpp @@ -60,6 +60,10 @@ SocketSelector::SocketSelector() : m_impl(std::make_unique() } +//////////////////////////////////////////////////////////// +SocketSelector::~SocketSelector() = default; + + //////////////////////////////////////////////////////////// SocketSelector::SocketSelector(const SocketSelector& copy) : m_impl(std::make_unique(*copy.m_impl)) { @@ -67,7 +71,20 @@ SocketSelector::SocketSelector(const SocketSelector& copy) : m_impl(std::make_un //////////////////////////////////////////////////////////// -SocketSelector::~SocketSelector() = default; +SocketSelector& SocketSelector::operator=(const SocketSelector& right) +{ + SocketSelector temp(right); + std::swap(m_impl, temp.m_impl); + return *this; +} + + +//////////////////////////////////////////////////////////// +SocketSelector::SocketSelector(SocketSelector&&) noexcept = default; + + +////////////////////////////////////////////////////////////] +SocketSelector& SocketSelector::operator=(SocketSelector&&) noexcept = default; //////////////////////////////////////////////////////////// @@ -189,15 +206,4 @@ bool SocketSelector::isReady(Socket& socket) const return false; } - -//////////////////////////////////////////////////////////// -SocketSelector& SocketSelector::operator=(const SocketSelector& right) -{ - SocketSelector temp(right); - - std::swap(m_impl, temp.m_impl); - - return *this; -} - } // namespace sf diff --git a/test/Network/SocketSelector.test.cpp b/test/Network/SocketSelector.test.cpp index 0a7eee6c1..4d3ea46ad 100644 --- a/test/Network/SocketSelector.test.cpp +++ b/test/Network/SocketSelector.test.cpp @@ -1,10 +1,27 @@ #include +// Other 1st party headers +#include + +#include + #include -static_assert(std::is_copy_constructible_v); -static_assert(std::is_copy_assignable_v); -static_assert(std::is_move_constructible_v); -static_assert(!std::is_nothrow_move_constructible_v); -static_assert(std::is_move_assignable_v); -static_assert(!std::is_nothrow_move_assignable_v); +TEST_CASE("[Network] sf::SocketSelector") +{ + SECTION("Type traits") + { + 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); + } + + sf::UdpSocket socket; + + SECTION("Construction") + { + const sf::SocketSelector socketSelector; + CHECK(!socketSelector.isReady(socket)); + } +}