Replace float/UInt64 durations by a more precise Time class (#388)
Improve Clock class with atomic RestartIfOver method and allows to choose required precision
This commit is contained in:
@@ -8,14 +8,14 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||
{
|
||||
GIVEN("A clock paused")
|
||||
{
|
||||
Nz::UInt64 initialTime = 100;
|
||||
Nz::Clock clock(initialTime, true);
|
||||
Nz::Time initialTime = Nz::Time::Microseconds(100);
|
||||
Nz::HighPrecisionClock clock(initialTime, true);
|
||||
|
||||
WHEN("We get time since it is paused")
|
||||
{
|
||||
THEN("Time must be the initialTime")
|
||||
{
|
||||
CHECK(clock.GetMicroseconds() == initialTime);
|
||||
CHECK(clock.GetElapsedTime() == initialTime);
|
||||
CHECK(clock.IsPaused());
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,11 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||
THEN("Time must not be the initialTime")
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
Nz::UInt64 microSeconds = clock.GetMicroseconds();
|
||||
CHECK(microSeconds != initialTime);
|
||||
CHECK(microSeconds / 1000 <= clock.GetMilliseconds());
|
||||
CHECK(microSeconds / (1000.f * 1000.f) <= clock.GetSeconds());
|
||||
Nz::Time elapsedTime = clock.GetElapsedTime();
|
||||
Nz::Int64 microseconds = elapsedTime.AsMicroseconds();
|
||||
CHECK(microseconds != initialTime.AsMicroseconds());
|
||||
CHECK(microseconds / 1000 <= elapsedTime.AsMilliseconds());
|
||||
CHECK(microseconds / (1000.f * 1000.f) <= elapsedTime.AsSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +45,7 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||
CHECK(!clock.IsPaused());
|
||||
clock.Pause();
|
||||
CHECK(clock.IsPaused());
|
||||
CHECK(clock.GetMicroseconds() != initialTime);
|
||||
CHECK(clock.GetElapsedTime() != initialTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
52
tests/UnitTests/Engine/Core/TimeTest.cpp
Normal file
52
tests/UnitTests/Engine/Core/TimeTest.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
SCENARIO("Time", "[CORE][Time]")
|
||||
{
|
||||
auto ToString = [](Nz::Time time) -> std::string
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << time;
|
||||
return std::move(ss).str();
|
||||
};
|
||||
|
||||
GIVEN("One second")
|
||||
{
|
||||
Nz::Time time = Nz::Time::Second();
|
||||
CHECK(time.AsDuration<std::chrono::seconds>() == std::chrono::seconds(1));
|
||||
CHECK(time.AsDuration<std::chrono::milliseconds>() == std::chrono::milliseconds(1'000));
|
||||
CHECK(time.AsDuration<std::chrono::microseconds>() == std::chrono::microseconds(1'000'000));
|
||||
CHECK(time.AsMicroseconds() == 1'000'000);
|
||||
CHECK(time.AsMilliseconds() == 1'000);
|
||||
CHECK(time.AsSeconds() == 1.f);
|
||||
CHECK(time == Nz::Time::Second());
|
||||
CHECK(time == Nz::Time::Seconds(1.f));
|
||||
CHECK(time == Nz::Time::Milliseconds(1'000));
|
||||
CHECK(time == Nz::Time::Seconds(2) - Nz::Time::Milliseconds(1'000));
|
||||
|
||||
CHECK(ToString(time) == "1000ms");
|
||||
}
|
||||
|
||||
GIVEN("One arbitrary duration")
|
||||
{
|
||||
Nz::Time time = Nz::Time::FromDuration(std::chrono::nanoseconds(1'234'567'890ULL)) - Nz::Time::Nanoseconds(890);
|
||||
CHECK(time == Nz::Time::Microseconds(1'234'567));
|
||||
CHECK_FALSE(time == Nz::Time::Microseconds(1'234'568));
|
||||
CHECK(time != Nz::Time::Microseconds(1'234'566));
|
||||
CHECK_FALSE(time != Nz::Time::Microseconds(1'234'567));
|
||||
CHECK(time > Nz::Time::Microseconds(1'234'000));
|
||||
CHECK(time > Nz::Time::Zero());
|
||||
CHECK(time >= Nz::Time::Microseconds(1'234'567));
|
||||
CHECK(time >= Nz::Time::Microseconds(1'234'000));
|
||||
CHECK(time < Nz::Time::Milliseconds(1'235));
|
||||
CHECK(time <= Nz::Time::Microseconds(1'234'567));
|
||||
CHECK(time <= Nz::Time::Seconds(2));
|
||||
|
||||
CHECK(ToString(time) == "1.23457s");
|
||||
CHECK(ToString(time - Nz::Time::Second()) == "234.567ms");
|
||||
CHECK(ToString(time - Nz::Time::Seconds(1.234f)) == "567us");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user