SFML/test/Network/IpAddress.test.cpp

189 lines
7.1 KiB
C++
Raw Permalink Normal View History

2022-06-18 04:57:24 +08:00
#include <SFML/Network/IpAddress.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-06-18 04:57:24 +08:00
#include <sstream>
2022-06-27 18:02:35 +08:00
#include <string_view>
#include <type_traits>
2022-06-18 04:57:24 +08:00
using namespace std::string_literals;
2022-06-27 18:02:35 +08:00
using namespace std::string_view_literals;
2022-06-18 04:57:24 +08:00
TEST_CASE("[Network] sf::IpAddress")
2022-06-18 04:57:24 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
STATIC_CHECK(std::is_copy_constructible_v<sf::IpAddress>);
STATIC_CHECK(std::is_copy_assignable_v<sf::IpAddress>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::IpAddress>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::IpAddress>);
}
SECTION("Construction")
2022-06-18 04:57:24 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("static 'create' function")
2022-06-18 04:57:24 +08:00
{
const auto ipAddress = sf::IpAddress::resolve("203.0.113.2"sv);
2022-06-27 18:02:35 +08:00
REQUIRE(ipAddress.has_value());
CHECK(ipAddress->toString() == "203.0.113.2"s);
CHECK(ipAddress->toInteger() == 0xCB007102);
2022-06-27 18:02:35 +08:00
CHECK(*ipAddress != sf::IpAddress::Any);
CHECK(*ipAddress != sf::IpAddress::Broadcast);
CHECK(*ipAddress != sf::IpAddress::LocalHost);
const auto broadcast = sf::IpAddress::resolve("255.255.255.255"sv);
REQUIRE(broadcast.has_value());
CHECK(broadcast->toString() == "255.255.255.255"s);
CHECK(broadcast->toInteger() == 0xFFFFFFFF);
CHECK(*broadcast == sf::IpAddress::Broadcast);
const auto any = sf::IpAddress::resolve("0.0.0.0"sv);
REQUIRE(any.has_value());
CHECK(any->toString() == "0.0.0.0"s);
CHECK(any->toInteger() == 0x00000000);
CHECK(*any == sf::IpAddress::Any);
const auto localHost = sf::IpAddress::resolve("localhost"s);
REQUIRE(localHost.has_value());
CHECK(localHost->toString() == "127.0.0.1"s);
CHECK(localHost->toInteger() == 0x7F000001);
CHECK(*localHost == sf::IpAddress::LocalHost);
CHECK(!sf::IpAddress::resolve("255.255.255.256"s).has_value());
CHECK(!sf::IpAddress::resolve("").has_value());
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Byte constructor")
2022-06-18 04:57:24 +08:00
{
const sf::IpAddress ipAddress(198, 51, 100, 234);
CHECK(ipAddress.toString() == "198.51.100.234"s);
CHECK(ipAddress.toInteger() == 0xC63364EA);
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("std::uint32_t constructor")
2022-06-18 04:57:24 +08:00
{
const sf::IpAddress ipAddress(0xCB00719A);
CHECK(ipAddress.toString() == "203.0.113.154"s);
CHECK(ipAddress.toInteger() == 0xCB00719A);
2022-06-18 04:57:24 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Static functions")
2022-06-18 04:57:24 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("getLocalAddress")
2022-06-18 04:57:24 +08:00
{
2022-06-27 18:02:35 +08:00
const std::optional<sf::IpAddress> ipAddress = sf::IpAddress::getLocalAddress();
REQUIRE(ipAddress.has_value());
CHECK(ipAddress->toString() != "0.0.0.0");
CHECK(ipAddress->toInteger() != 0);
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("getPublicAddress")
2022-06-18 04:57:24 +08:00
{
const std::optional<sf::IpAddress> ipAddress = sf::IpAddress::getPublicAddress(sf::seconds(1));
if (ipAddress.has_value())
{
CHECK(ipAddress->toString() != "0.0.0.0");
CHECK(ipAddress->toInteger() != 0);
}
2022-06-18 04:57:24 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Static constants")
2022-06-18 04:57:24 +08:00
{
CHECK(sf::IpAddress::Any.toString() == "0.0.0.0"s);
CHECK(sf::IpAddress::Any.toInteger() == 0);
CHECK(sf::IpAddress::LocalHost.toString() == "127.0.0.1"s);
CHECK(sf::IpAddress::LocalHost.toInteger() == 0x7F000001);
CHECK(sf::IpAddress::Broadcast.toString() == "255.255.255.255"s);
CHECK(sf::IpAddress::Broadcast.toInteger() == 0xFFFFFFFF);
}
2023-01-18 12:51:08 +08:00
SECTION("Operators")
2022-06-18 04:57:24 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("operator==")
2022-06-18 04:57:24 +08:00
{
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) == sf::IpAddress(0xC633647B));
CHECK(sf::IpAddress(0xCB0071D2) == sf::IpAddress(203, 0, 113, 210));
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator!=")
2022-06-18 04:57:24 +08:00
{
2022-06-27 18:02:35 +08:00
CHECK(sf::IpAddress(0x12344321) != sf::IpAddress(1234));
CHECK(sf::IpAddress(198, 51, 100, 1) != sf::IpAddress(198, 51, 100, 11));
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator<")
2022-06-18 04:57:24 +08:00
{
CHECK(sf::IpAddress(1) < sf::IpAddress(2));
CHECK(sf::IpAddress(0, 0, 0, 0) < sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(1, 0, 0, 0) < sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) < sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) < sf::IpAddress(0, 0, 0, 1));
CHECK(sf::IpAddress(0, 0, 0, 1) < sf::IpAddress(1, 0, 0, 1));
}
2023-01-18 12:51:08 +08:00
SECTION("operator>")
2022-06-18 04:57:24 +08:00
{
CHECK(sf::IpAddress(2) > sf::IpAddress(1));
CHECK(sf::IpAddress(1, 0, 0, 0) > sf::IpAddress(0, 0, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) > sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) > sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 0, 0, 1) > sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(1, 0, 0, 1) > sf::IpAddress(0, 0, 0, 1));
}
2023-01-18 12:51:08 +08:00
SECTION("operator<=")
2022-06-18 04:57:24 +08:00
{
CHECK(sf::IpAddress(1) <= sf::IpAddress(2));
CHECK(sf::IpAddress(0, 0, 0, 0) <= sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(1, 0, 0, 0) <= sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) <= sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) <= sf::IpAddress(0, 0, 0, 1));
CHECK(sf::IpAddress(0, 0, 0, 1) <= sf::IpAddress(1, 0, 0, 1));
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) <= sf::IpAddress(0xC633647B));
CHECK(sf::IpAddress(0xCB0071D2) <= sf::IpAddress(203, 0, 113, 210));
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator>=")
2022-06-18 04:57:24 +08:00
{
CHECK(sf::IpAddress(2) >= sf::IpAddress(1));
CHECK(sf::IpAddress(1, 0, 0, 0) >= sf::IpAddress(0, 0, 0, 0));
CHECK(sf::IpAddress(0, 1, 0, 0) >= sf::IpAddress(1, 0, 0, 0));
CHECK(sf::IpAddress(0, 0, 1, 0) >= sf::IpAddress(0, 1, 0, 0));
CHECK(sf::IpAddress(0, 0, 0, 1) >= sf::IpAddress(0, 0, 1, 0));
CHECK(sf::IpAddress(1, 0, 0, 1) >= sf::IpAddress(0, 0, 0, 1));
CHECK(sf::IpAddress(0xC6, 0x33, 0x64, 0x7B) >= sf::IpAddress(0xC633647B));
CHECK(sf::IpAddress(0xCB0071D2) >= sf::IpAddress(203, 0, 113, 210));
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator>>")
2022-06-18 04:57:24 +08:00
{
2022-06-27 18:02:35 +08:00
std::optional<sf::IpAddress> ipAddress;
std::istringstream("198.51.100.4") >> ipAddress;
2022-06-27 18:02:35 +08:00
REQUIRE(ipAddress.has_value());
CHECK(ipAddress->toString() == "198.51.100.4"s);
CHECK(ipAddress->toInteger() == 0xC6336404);
2022-06-18 04:57:24 +08:00
std::istringstream("203.0.113.72") >> ipAddress;
2022-06-27 18:02:35 +08:00
REQUIRE(ipAddress.has_value());
CHECK(ipAddress->toString() == "203.0.113.72"s);
CHECK(ipAddress->toInteger() == 0xCB007148);
2022-06-27 18:02:35 +08:00
std::istringstream("") >> ipAddress;
CHECK(!ipAddress.has_value());
2022-06-18 04:57:24 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator<<")
2022-06-18 04:57:24 +08:00
{
std::ostringstream out;
out << sf::IpAddress(192, 0, 2, 10);
CHECK(out.str() == "192.0.2.10"s);
2022-06-18 04:57:24 +08:00
}
}
}