From d9e46281c1e24a5f23d779d314e5df87fa3317a3 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 4 Dec 2024 21:33:38 +0100 Subject: tests --- src/test/CMakeLists.txt | 1 + src/test/loopManagerTest.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/test/loopTimerTest.cpp | 36 ++++++++++++++++++++++++++----- 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 src/test/loopManagerTest.cpp (limited to 'src/test') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index d310f6a..b126add 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,4 +12,5 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp + loopTimerTest.cpp ) diff --git a/src/test/loopManagerTest.cpp b/src/test/loopManagerTest.cpp new file mode 100644 index 0000000..6e66ce7 --- /dev/null +++ b/src/test/loopManagerTest.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#define private public +#define protected public +#include "api/LoopTimer.h" +#include "api/LoopManager.h" + +using namespace std::chrono; +using namespace crepe; + +class LoopTimerTest : public ::testing::Test { +protected: + LoopManager loop_manager; + + void SetUp() override { + // Setting up loop manager and start the loop + loop_manager.setup(); + loop_manager.loop_timer->set_target_fps(60); + } +}; + +// Test to check if exactly 5 fixed updates are done every second (50Hz) +TEST_F(LoopTimerTest, FixedUpdateCalledAt50Hz) { + // Set target fixed delta time to 20ms (50Hz fixed updates) + loop_manager.loop_timer->set_fixed_delta_time(milliseconds(20)); + + int fixed_update_count = 0; + + // We want to simulate the game loop for about 1 second + auto start_time = steady_clock::now(); + + // Simulate the game loop for 1 second + while (duration_cast(steady_clock::now() - start_time).count() < 1) { + loop_manager.loop_timer->update(); + + // Simulate processing fixed updates while there's lag to advance + while (loop_manager.loop_timer->get_lag() >= loop_manager.loop_timer->get_fixed_delta_time()) { + loop_manager.fixed_update(); // Process fixed update + fixed_update_count++; // Count the number of fixed updates + loop_manager.loop_timer->advance_fixed_update(); + } + + // We do not need to call render or update for this test + loop_manager.loop_timer->enforce_frame_rate(); // Enforce the frame rate (this would normally go to the display) + } + + // We expect 5 fixed updates to occur in 1 second at 50Hz + ASSERT_EQ(fixed_update_count, 5); +} diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp index d2f7d9b..9bbbff3 100644 --- a/src/test/loopTimerTest.cpp +++ b/src/test/loopTimerTest.cpp @@ -1,6 +1,8 @@ #include #include #include +#define private public +#define protected public #include "api/LoopTimer.h" using namespace std::chrono; @@ -11,23 +13,47 @@ protected: LoopTimer loop_timer; void SetUp() override { - loop_timer.start(); // Reset loop timer before each test. + loop_timer.start(); } }; 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 + loop_timer.enforce_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(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 } +TEST_F(LoopTimerTest, SetTargetFps) { + // Set the target FPS to 120 + loop_timer.set_target_fps(120); + + // Calculate the expected frame time (~8.33ms per frame) + auto expected_frame_time = std::chrono::duration(1.0 / 120.0); + + ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); +} +TEST_F(LoopTimerTest, DeltaTimeCalculation) { + // Set the target FPS to 60 (16.67 ms per frame) + loop_timer.set_target_fps(60); + + auto start_time = steady_clock::now(); + loop_timer.update(); + auto end_time = steady_clock::now(); + + // Check the delta time + double delta_time = loop_timer.get_delta_time(); + + auto elapsed_time = duration_cast(end_time - start_time).count(); + + // Assert that delta_time is close to the elapsed time + ASSERT_GE(delta_time, elapsed_time / 1000.0); + ASSERT_LE(delta_time, (elapsed_time + 2) / 1000.0); +} + -- cgit v1.2.3