SFML/test/Network/Socket.test.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

#include <SFML/Network/Socket.hpp>
2023-08-22 11:38:10 +08:00
#include <catch2/catch_test_macros.hpp>
#include <type_traits>
2023-08-22 11:38:10 +08:00
class TestSocket : public sf::Socket
{
public:
TestSocket() : sf::Socket(sf::Socket::Type::Udp)
{
}
using sf::Socket::close;
using sf::Socket::create;
using sf::Socket::getNativeHandle;
2023-08-22 11:38:10 +08:00
};
TEST_CASE("[Network] sf::Socket")
{
SECTION("Type traits")
{
STATIC_CHECK(!std::is_constructible_v<sf::Socket>);
STATIC_CHECK(!std::is_copy_constructible_v<sf::Socket>);
STATIC_CHECK(!std::is_copy_assignable_v<sf::Socket>);
2023-08-22 11:29:39 +08:00
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Socket>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Socket>);
2023-08-22 11:38:10 +08:00
}
SECTION("Constants")
{
STATIC_CHECK(sf::Socket::AnyPort == 0);
}
const auto invalidHandle = static_cast<sf::SocketHandle>(-1);
SECTION("Construction")
{
const TestSocket testSocket;
CHECK(testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() == invalidHandle);
2023-08-22 11:38:10 +08:00
}
2023-08-22 11:29:39 +08:00
SECTION("Move semantics")
{
SECTION("Construction")
{
TestSocket movedTestSocket;
movedTestSocket.setBlocking(false);
movedTestSocket.create();
const TestSocket testSocket(std::move(movedTestSocket));
CHECK(!testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() != invalidHandle);
2023-08-22 11:29:39 +08:00
}
SECTION("Assignment")
{
TestSocket movedTestSocket;
movedTestSocket.setBlocking(false);
movedTestSocket.create();
TestSocket testSocket;
testSocket = std::move(movedTestSocket);
CHECK(!testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() != invalidHandle);
2023-08-22 11:29:39 +08:00
}
}
2023-08-22 11:38:10 +08:00
SECTION("Set/get blocking")
{
TestSocket testSocket;
testSocket.setBlocking(false);
CHECK(!testSocket.isBlocking());
}
SECTION("create()")
{
TestSocket testSocket;
testSocket.create();
CHECK(testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() != invalidHandle);
2023-08-22 11:38:10 +08:00
// Recreate socket to ensure nothing changed
testSocket.create();
CHECK(testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() != invalidHandle);
2023-08-22 11:38:10 +08:00
}
SECTION("close()")
{
TestSocket testSocket;
testSocket.create();
CHECK(testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() != invalidHandle);
2023-08-22 11:38:10 +08:00
testSocket.close();
CHECK(testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() == invalidHandle);
2023-08-22 11:38:10 +08:00
// Reclose socket to ensure nothing changed
testSocket.close();
CHECK(testSocket.isBlocking());
CHECK(testSocket.getNativeHandle() == invalidHandle);
2023-08-22 11:38:10 +08:00
}
}