#include #include #include using namespace Nuclex::Game::Timing; class TestTimer : Timer { public: TestTimer(const std::shared_ptr &clock) : Timer(clock) {} public: std::uint64_t GetElapsedMicroseconds() const { return Timer::GetElapsedMicroseconds(); } }; struct FastTimerTest { public: FastTimerTest() : manualClock(new ManualClock(1000001)) { this->testTimer.reset(new TestTimer(this->manualClock)); } protected: std::shared_ptr manualClock; protected: std::shared_ptr testTimer; }; struct SlowTimerTest { public: SlowTimerTest() : manualClock(new ManualClock(9)) { this->testTimer.reset(new TestTimer(this->manualClock)); } protected: std::shared_ptr manualClock; protected: std::shared_ptr testTimer; }; // --------------------------------------------------------------------------------------------- // TEST_FIXTURE(SlowTimerTest, CompensatesForIndivisibleClockFrequency) { this->manualClock->AddTime(9); // After 9 ticks, the end result must be 1 second CHECK_EQUAL(1000000, this->testTimer->GetElapsedMicroseconds()); } // --------------------------------------------------------------------------------------------- // TEST_FIXTURE(SlowTimerTest, TicksAreConvertedToMicroseconds) { this->manualClock->AddTime(5); // An implementation could place the compensation tick at any point, so both // 555,555 or 555,556 are acceptable results if(this->testTimer->GetElapsedMicroseconds() == 555556) { CHECK_EQUAL(555556, this->testTimer->GetElapsedMicroseconds()); } else { CHECK_EQUAL(555555, this->testTimer->GetElapsedMicroseconds()); } } // --------------------------------------------------------------------------------------------- // TEST_FIXTURE(FastTimerTest, CompensatesForIndivisibleClockFrequency) { this->manualClock->AddTime(1000001); // After 1000001 ticks, the end result must be 1 second CHECK_EQUAL(1000000, this->testTimer->GetElapsedMicroseconds()); } // --------------------------------------------------------------------------------------------- // TEST_FIXTURE(FastTimerTest, TicksAreConvertedToMicroseconds) { this->manualClock->AddTime(1000000); // After 1000000 ticks, the end result must be 0.999999 seconds, // no matter where the compensation microsecond is placed. CHECK_EQUAL(999999, this->testTimer->GetElapsedMicroseconds()); } // --------------------------------------------------------------------------------------------- //