SFML/test/System/Time.test.cpp

297 lines
11 KiB
C++
Raw Normal View History

2021-12-18 23:32:40 +08:00
#include <SFML/System/Time.hpp>
2021-12-24 21:31:27 +08:00
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-07-05 00:20:58 +08:00
#include <SystemUtil.hpp>
#include <type_traits>
using namespace std::chrono_literals;
2023-01-18 12:51:08 +08:00
TEST_CASE("[System] sf::Time")
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(std::is_copy_constructible_v<sf::Time>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Time>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Time>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Time>);
}
2023-01-18 12:51:08 +08:00
SECTION("Construction")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Default constructor")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time;
STATIC_CHECK(time.asSeconds() == 0.0f);
STATIC_CHECK(time.asMilliseconds() == 0);
STATIC_CHECK(time.asMicroseconds() == 0);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Construct from seconds")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = sf::seconds(123);
STATIC_CHECK(time.asSeconds() == 123.0f);
STATIC_CHECK(time.asMilliseconds() == 123'000);
STATIC_CHECK(time.asMicroseconds() == 123'000'000);
STATIC_CHECK(sf::seconds(1'000.0f).asMicroseconds() == 1'000'000'000);
STATIC_CHECK(sf::seconds(0.0000009f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(0.0000001f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(0.00000001f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(0.000000001f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(-0.000000001f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(-0.00000001f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(-0.0000001f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(-0.0000009f).asMicroseconds() == 0);
STATIC_CHECK(sf::seconds(-1'000.0f).asMicroseconds() == -1'000'000'000);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Construct from milliseconds")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = sf::milliseconds(42);
STATIC_CHECK(time.asSeconds() == 0.042f);
STATIC_CHECK(time.asMilliseconds() == 42);
STATIC_CHECK(time.asMicroseconds() == 42'000);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Construct from microseconds")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = sf::microseconds(987654);
STATIC_CHECK(time.asSeconds() == 0.987654f);
STATIC_CHECK(time.asMilliseconds() == 987);
STATIC_CHECK(time.asMicroseconds() == 987'654);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Convert from chrono duration")
{
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = 3min;
STATIC_CHECK(time.asSeconds() == 180.f);
STATIC_CHECK(time.asMilliseconds() == 180'000);
STATIC_CHECK(time.asMicroseconds() == 180'000'000);
}
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = 1s;
STATIC_CHECK(time.asSeconds() == 1.f);
STATIC_CHECK(time.asMilliseconds() == 1'000);
STATIC_CHECK(time.asMicroseconds() == 1'000'000);
}
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = 10ms;
STATIC_CHECK(time.asSeconds() == 0.01f);
STATIC_CHECK(time.asMilliseconds() == 10);
STATIC_CHECK(time.asMicroseconds() == 10'000);
}
{
2023-01-18 12:51:08 +08:00
constexpr sf::Time time = 2048us;
STATIC_CHECK(time.asSeconds() == 0.002048f);
STATIC_CHECK(time.asMilliseconds() == 2);
STATIC_CHECK(time.asMicroseconds() == 2048);
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("toDuration()")
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(0).toDuration() == 0s);
STATIC_CHECK(sf::milliseconds(0).toDuration() == 0ms);
STATIC_CHECK(sf::microseconds(0).toDuration() == 0us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(-1).toDuration() == -1s);
STATIC_CHECK(sf::milliseconds(-1).toDuration() == -1ms);
STATIC_CHECK(sf::microseconds(-1).toDuration() == -1us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(1).toDuration() == 1s);
STATIC_CHECK(sf::milliseconds(1).toDuration() == 1ms);
STATIC_CHECK(sf::microseconds(1).toDuration() == 1us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(-10).toDuration() == -10s);
STATIC_CHECK(sf::milliseconds(-10).toDuration() == -10ms);
STATIC_CHECK(sf::microseconds(-10).toDuration() == -10us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(10).toDuration() == 10s);
STATIC_CHECK(sf::milliseconds(10).toDuration() == 10ms);
STATIC_CHECK(sf::microseconds(10).toDuration() == 10us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Time(1s).toDuration() == 1s);
STATIC_CHECK(sf::Time(1ms).toDuration() == 1ms);
STATIC_CHECK(sf::Time(1us).toDuration() == 1us);
}
2023-01-18 12:51:08 +08:00
SECTION("Implicit conversion to duration")
{
const auto toDuration = [](const std::chrono::microseconds& duration) { return duration; };
2023-01-18 12:51:08 +08:00
STATIC_CHECK(toDuration(sf::seconds(0)) == 0s);
STATIC_CHECK(toDuration(sf::milliseconds(0)) == 0ms);
STATIC_CHECK(toDuration(sf::microseconds(0)) == 0us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(toDuration(sf::seconds(-1)) == -1s);
STATIC_CHECK(toDuration(sf::milliseconds(-1)) == -1ms);
STATIC_CHECK(toDuration(sf::microseconds(-1)) == -1us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(toDuration(sf::seconds(1)) == 1s);
STATIC_CHECK(toDuration(sf::milliseconds(1)) == 1ms);
STATIC_CHECK(toDuration(sf::microseconds(1)) == 1us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(toDuration(sf::seconds(-10)) == -10s);
STATIC_CHECK(toDuration(sf::milliseconds(-10)) == -10ms);
STATIC_CHECK(toDuration(sf::microseconds(-10)) == -10us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(toDuration(sf::seconds(10)) == 10s);
STATIC_CHECK(toDuration(sf::milliseconds(10)) == 10ms);
STATIC_CHECK(toDuration(sf::microseconds(10)) == 10us);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(toDuration(sf::Time(1s)) == 1s);
STATIC_CHECK(toDuration(sf::Time(1ms)) == 1ms);
STATIC_CHECK(toDuration(sf::Time(1us)) == 1us);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Zero time")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Time::Zero.asSeconds() == 0.0f);
STATIC_CHECK(sf::Time::Zero.asMilliseconds() == 0);
STATIC_CHECK(sf::Time::Zero.asMicroseconds() == 0);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Operators")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("operator==")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Time() == sf::Time());
STATIC_CHECK(sf::seconds(1) == sf::seconds(1));
STATIC_CHECK(sf::seconds(10) == sf::milliseconds(10'000));
STATIC_CHECK(sf::milliseconds(450'450) == sf::microseconds(450'450'000));
STATIC_CHECK(sf::seconds(0.5f) == sf::microseconds(500'000));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator!=")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(10.12f) != sf::milliseconds(10'121));
STATIC_CHECK(sf::microseconds(123'456) != sf::milliseconds(123));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator<")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(54.999f) < sf::seconds(55));
STATIC_CHECK(sf::microseconds(10) < sf::milliseconds(10));
STATIC_CHECK(sf::milliseconds(1'000) < sf::microseconds(1'000'001));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator>")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(55.001f) > sf::seconds(55));
STATIC_CHECK(sf::microseconds(1) > sf::seconds(0.0000001f));
STATIC_CHECK(sf::microseconds(1'000'001) > sf::milliseconds(1'000));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator<=")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::milliseconds(100) <= sf::milliseconds(100));
STATIC_CHECK(sf::seconds(0.0012f) <= sf::microseconds(1'201));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator>=")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::milliseconds(100) >= sf::milliseconds(-100));
STATIC_CHECK(sf::microseconds(1'201) >= sf::seconds(0.0012f));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator-")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(-1) == -sf::seconds(1));
STATIC_CHECK(sf::microseconds(1'234) == -sf::microseconds(-1'234));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator+")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(1) + sf::seconds(1) == sf::seconds(2));
STATIC_CHECK(sf::milliseconds(400) + sf::microseconds(400) == sf::microseconds(400400));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator+=")
2021-12-18 23:32:40 +08:00
{
sf::Time time = sf::seconds(1.5f);
time += sf::seconds(1);
CHECK(time == sf::seconds(2.5f));
}
2023-01-18 12:51:08 +08:00
SECTION("operator-")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(1) - sf::seconds(1) == sf::seconds(0));
STATIC_CHECK(sf::milliseconds(400) - sf::microseconds(400) == sf::microseconds(399600));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator-=")
2021-12-18 23:32:40 +08:00
{
sf::Time time = sf::seconds(1.5f);
time -= sf::seconds(10);
CHECK(time == sf::seconds(-8.5f));
}
2023-01-18 12:51:08 +08:00
SECTION("operator*")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(1) * 2.0f == sf::seconds(2));
STATIC_CHECK(sf::seconds(12) * 0.5f == sf::seconds(6));
STATIC_CHECK(sf::seconds(1) * std::int64_t{2} == sf::seconds(2));
STATIC_CHECK(sf::seconds(42) * std::int64_t{2} == sf::seconds(84));
STATIC_CHECK(2.0f * sf::seconds(1) == sf::seconds(2));
STATIC_CHECK(0.5f * sf::seconds(12) == sf::seconds(6));
STATIC_CHECK(std::int64_t{2} * sf::seconds(1) == sf::seconds(2));
STATIC_CHECK(std::int64_t{2} * sf::seconds(42) == sf::seconds(84));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator*=")
2021-12-18 23:32:40 +08:00
{
2022-04-05 02:35:35 +08:00
sf::Time time = sf::milliseconds(1'000);
2024-05-17 10:56:30 +08:00
time *= std::int64_t{10};
2022-04-05 02:35:35 +08:00
CHECK(time == sf::milliseconds(10'000));
2021-12-18 23:32:40 +08:00
time *= 0.1f;
2022-04-05 02:35:35 +08:00
CHECK(time.asMilliseconds() == 1'000);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator/")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(1) / 2.0f == sf::seconds(0.5f));
STATIC_CHECK(sf::seconds(12) / 0.5f == sf::seconds(24));
STATIC_CHECK(sf::seconds(1) / std::int64_t{2} == sf::seconds(0.5f));
STATIC_CHECK(sf::seconds(42) / std::int64_t{2} == sf::seconds(21));
STATIC_CHECK(sf::seconds(1) / sf::seconds(1) == 1.0f);
2022-07-18 06:18:40 +08:00
CHECK(sf::milliseconds(10) / sf::microseconds(1) == Approx(10'000.f));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator/=")
2021-12-18 23:32:40 +08:00
{
2022-04-05 02:35:35 +08:00
sf::Time time = sf::milliseconds(1'000);
2024-05-17 10:56:30 +08:00
time /= std::int64_t{2};
2022-04-05 02:35:35 +08:00
CHECK(time == sf::milliseconds(500));
time /= 0.5f;
CHECK(time.asMilliseconds() == 1'000);
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator%")
2021-12-18 23:32:40 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::seconds(10) % sf::seconds(3) == sf::seconds(1));
STATIC_CHECK(sf::milliseconds(100) % sf::microseconds(10) == sf::seconds(0));
2021-12-18 23:32:40 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator%=")
2021-12-18 23:32:40 +08:00
{
sf::Time time = sf::milliseconds(100);
time %= sf::milliseconds(99);
CHECK(time == sf::milliseconds(1));
}
}
2021-12-24 22:12:50 +08:00
2023-01-18 12:51:08 +08:00
SECTION("Constexpr support")
2021-12-24 22:12:50 +08:00
{
constexpr auto result = []
{
sf::Time time = sf::milliseconds(100);
time %= sf::milliseconds(99);
return time;
}();
2023-01-18 12:51:08 +08:00
STATIC_CHECK(result == sf::milliseconds(1));
2021-12-24 22:12:50 +08:00
}
2021-12-18 23:32:40 +08:00
}