diff options
Diffstat (limited to 'src/test/loopTimerTest.cpp')
-rw-r--r-- | src/test/loopTimerTest.cpp | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp index a3b1646..d2f7d9b 100644 --- a/src/test/loopTimerTest.cpp +++ b/src/test/loopTimerTest.cpp @@ -1,32 +1,33 @@ -#define private public -#define protected public -#include "api/LoopManager.h" -#include "api/LoopTimer.h" -#include <gmock/gmock.h> #include <gtest/gtest.h> +#include <chrono> +#include <thread> +#include "api/LoopTimer.h" -using namespace std; -using namespace std::chrono_literals; +using namespace std::chrono; using namespace crepe; class LoopTimerTest : public ::testing::Test { -public: -LoopTimer loop_timer = LoopTimer::get_instance(); protected: - void SetUp() override { - loop_timer.start(); - } + LoopTimer loop_timer; - void TearDown() override { - - } + void SetUp() override { + loop_timer.start(); // Reset loop timer before each test. + } }; -TEST_F(LoopTimerTest, TestDeltaTime) { - auto start_time = std::chrono::steady_clock::now(); - - loop_timer.update(); - double delta_time = loop_timer.get_delta_time(); +TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { + // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) + loop_timer.set_target_fps(60); + + // Simulate a short update (frame duration less than the target frame time) + auto start_time = steady_clock::now(); + loop_timer.enforce_frame_rate(); // Enforce the frame rate + + // Check that the loop timer's current time is greater than or equal to the target frame time + auto elapsed_time = steady_clock::now() - start_time; + auto elapsed_ms = duration_cast<milliseconds>(elapsed_time).count(); - auto elapsed_time = std::chrono::steady_clock::now() - start_time; - EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count()); + // Assert that the elapsed time is close to the target frame time + // For 60 FPS, the target frame time is around 16.67ms + ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) + ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer } |