From 54b8a691d196c8afd4c842698aa294874437207e Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 18 Dec 2021 08:32:40 -0700 Subject: [PATCH] Add unit tests for sf::Time --- test/CMakeLists.txt | 1 + test/src/System/Time.cpp | 201 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 test/src/System/Time.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b116d030..1b85127f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,6 +20,7 @@ endif() # System is always built SET(SYSTEM_SRC + "${SRCROOT}/System/Time.cpp" "${SRCROOT}/System/Vector2.cpp" "${SRCROOT}/System/Vector3.cpp" "${SRCROOT}/TestUtilities/SystemUtil.hpp" diff --git a/test/src/System/Time.cpp b/test/src/System/Time.cpp new file mode 100644 index 00000000..4e5a43e0 --- /dev/null +++ b/test/src/System/Time.cpp @@ -0,0 +1,201 @@ +#include + +#include + +namespace sf +{ +std::ostream& operator <<(std::ostream& os, const sf::Time& time) +{ + os << time.asMicroseconds() << "us"; + return os; +} +} + +TEST_CASE("sf::Time class", "[system]") +{ + SECTION("Construction") + { + SECTION("Default constructor") + { + const sf::Time time; + CHECK(time.asSeconds() == 0.0f); + CHECK(time.asMilliseconds() == 0); + CHECK(time.asMicroseconds() == 0); + } + + SECTION("Construct from seconds") + { + const sf::Time time = sf::seconds(123); + CHECK(time.asSeconds() == 123.0f); + CHECK(time.asMilliseconds() == 123'000); + CHECK(time.asMicroseconds() == 123'000'000); + + CHECK(sf::seconds(1'000.0f).asMicroseconds() == 1'000'000'000); + CHECK(sf::seconds(0.000002f).asMicroseconds() == Approx(2).margin(1)); + CHECK(sf::seconds(0.000001f).asMicroseconds() == Approx(1).margin(1)); + CHECK(sf::seconds(0.0000009f).asMicroseconds() == 0); + CHECK(sf::seconds(0.0000001f).asMicroseconds() == 0); + CHECK(sf::seconds(0.00000001f).asMicroseconds() == 0); + CHECK(sf::seconds(0.000000001f).asMicroseconds() == 0); + CHECK(sf::seconds(-0.000000001f).asMicroseconds() == 0); + CHECK(sf::seconds(-0.00000001f).asMicroseconds() == 0); + CHECK(sf::seconds(-0.0000001f).asMicroseconds() == 0); + CHECK(sf::seconds(-0.0000009f).asMicroseconds() == 0); + CHECK(sf::seconds(-0.000001f).asMicroseconds() == Approx(-1).margin(1)); + CHECK(sf::seconds(-0.000002f).asMicroseconds() == Approx(-2).margin(1)); + CHECK(sf::seconds(-1'000.0f).asMicroseconds() == -1'000'000'000); + } + + SECTION("Construct from milliseconds") + { + const sf::Time time = sf::milliseconds(42); + CHECK(time.asSeconds() == 0.042f); + CHECK(time.asMilliseconds() == 42); + CHECK(time.asMicroseconds() == 42'000); + } + + SECTION("Construct from microseconds") + { + const sf::Time time = sf::microseconds(987654); + CHECK(time.asSeconds() == 0.987654f); + CHECK(time.asMilliseconds() == 987); + CHECK(time.asMicroseconds() == 987'654); + } + } + + SECTION("Zero time") + { + CHECK(sf::Time::Zero.asSeconds() == 0.0f); + CHECK(sf::Time::Zero.asMilliseconds() == 0); + CHECK(sf::Time::Zero.asMicroseconds() == 0); + } + + SECTION("Operators") + { + SECTION("operator==") + { + CHECK(sf::Time() == sf::Time()); + CHECK(sf::seconds(1) == sf::seconds(1)); + CHECK(sf::seconds(10) == sf::milliseconds(10'000)); + CHECK(sf::milliseconds(450'450) == sf::microseconds(450'450'000)); + CHECK(sf::seconds(0.5f) == sf::microseconds(500'000)); + } + + SECTION("operator!=") + { + CHECK(sf::seconds(10.12f) != sf::milliseconds(10'121)); + CHECK(sf::microseconds(123'456) != sf::milliseconds(123)); + } + + SECTION("operator<") + { + CHECK(sf::seconds(54.999f) < sf::seconds(55)); + CHECK(sf::microseconds(10) < sf::milliseconds(10)); + CHECK(sf::milliseconds(1'000) < sf::microseconds(1'000'001)); + } + + SECTION("operator>") + { + CHECK(sf::seconds(55.001f) > sf::seconds(55)); + CHECK(sf::microseconds(1) > sf::seconds(0.0000001f)); + CHECK(sf::microseconds(1'000'001) > sf::milliseconds(1'000)); + } + + SECTION("operator<=") + { + CHECK(sf::milliseconds(100) <= sf::milliseconds(100)); + CHECK(sf::seconds(0.0012f) <= sf::microseconds(1'201)); + } + + SECTION("operator>=") + { + CHECK(sf::milliseconds(100) >= sf::milliseconds(-100)); + CHECK(sf::microseconds(1'201) >= sf::seconds(0.0012f)); + } + + SECTION("operator-") + { + CHECK(sf::seconds(-1) == -sf::seconds(1)); + CHECK(sf::microseconds(1'234) == -sf::microseconds(-1'234)); + } + + SECTION("operator+") + { + CHECK(sf::seconds(1) + sf::seconds(1) == sf::seconds(2)); + CHECK(sf::milliseconds(400) + sf::microseconds(400) == sf::microseconds(400400)); + } + + SECTION("operator+=") + { + sf::Time time = sf::seconds(1.5f); + time += sf::seconds(1); + CHECK(time == sf::seconds(2.5f)); + } + + SECTION("operator-") + { + CHECK(sf::seconds(1) - sf::seconds(1) == sf::seconds(0)); + CHECK(sf::milliseconds(400) - sf::microseconds(400) == sf::microseconds(399600)); + } + + SECTION("operator-=") + { + sf::Time time = sf::seconds(1.5f); + time -= sf::seconds(10); + CHECK(time == sf::seconds(-8.5f)); + } + + SECTION("operator*") + { + CHECK(sf::seconds(1) * 2.0f == sf::seconds(2)); + CHECK(sf::seconds(12) * 0.5f == sf::seconds(6)); + CHECK(sf::seconds(1) * static_cast(2) == sf::seconds(2)); + CHECK(sf::seconds(42) * static_cast(2) == sf::seconds(84)); + CHECK(2.0f * sf::seconds(1) == sf::seconds(2)); + CHECK(0.5f * sf::seconds(12) == sf::seconds(6)); + CHECK(static_cast(2) * sf::seconds(1) == sf::seconds(2)); + CHECK(static_cast(2) * sf::seconds(42) == sf::seconds(84)); + } + + SECTION("operator*=") + { + sf::Time time = sf::milliseconds(420); + time *= static_cast(10); + CHECK(time == sf::milliseconds(4'200)); + time *= 0.1f; + CHECK(time.asMicroseconds() == Approx(420'000).margin(1)); + } + + SECTION("operator/") + { + CHECK(sf::seconds(1) / 2.0f == sf::seconds(0.5f)); + CHECK(sf::seconds(12) / 0.5f == sf::seconds(24)); + CHECK(sf::seconds(1) / static_cast(2) == sf::seconds(0.5f)); + CHECK(sf::seconds(42) / static_cast(2) == sf::seconds(21)); + CHECK(sf::seconds(1) / sf::seconds(1) == 1.0f); + CHECK(sf::milliseconds(10) / sf::microseconds(1) == Approx(10'000.0f).epsilon(1e-6f)); + } + + SECTION("operator/=") + { + sf::Time time = sf::milliseconds(420); + time /= static_cast(42); + CHECK(time == sf::milliseconds(10)); + time /= 10.0f; + CHECK(time.asMicroseconds() == Approx(1'000).margin(1)); + } + + SECTION("operator%") + { + CHECK(sf::seconds(10) % sf::seconds(3) == sf::seconds(1)); + CHECK(sf::milliseconds(100) % sf::microseconds(10) == sf::seconds(0)); + } + + SECTION("operator%=") + { + sf::Time time = sf::milliseconds(100); + time %= sf::milliseconds(99); + CHECK(time == sf::milliseconds(1)); + } + } +}