diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/LoopManagerTest.cpp | 46 | ||||
-rw-r--r-- | src/test/LoopTimerTest.cpp | 87 |
2 files changed, 65 insertions, 68 deletions
diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index 7937649..5897906 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -1,43 +1,45 @@ -#include <gtest/gtest.h> #include <chrono> +#include <gtest/gtest.h> #include <thread> #define private public #define protected public -#include "api/LoopTimer.h" #include "api/LoopManager.h" +#include "api/LoopTimer.h" using namespace std::chrono; using namespace crepe; class LoopManagerTest : public ::testing::Test { protected: - LoopManager loop_manager; + LoopManager loop_manager; - void SetUp() override { - // Setting up loop manager and start the loop - loop_manager.loop_timer->set_target_fps(60); - } + void SetUp() override { + // Setting up loop manager and start the loop + loop_manager.loop_timer->set_target_fps(60); + } }; //Test to check if exactly 5 fixed updates are done every second (50Hz) TEST_F(LoopManagerTest, FixedUpdate) { - loop_manager.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); + loop_manager.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); loop_manager.loop_timer->set_target_fps(50); - int fixed_update_count = 0; + int fixed_update_count = 0; loop_manager.loop_timer->start(); - // We want to simulate the game loop for about 1 second - auto start_time = steady_clock::now(); + // 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<milliseconds>(steady_clock::now() - start_time) < std::chrono::milliseconds(1000)) { + // Simulate the game loop for 1 second + while (duration_cast<milliseconds>(steady_clock::now() - start_time) + < std::chrono::milliseconds(1000)) { 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()) { - fixed_update_count++; - loop_manager.loop_timer->advance_fixed_update(); - } - - loop_manager.loop_timer->enforce_frame_rate(); - } - ASSERT_EQ(fixed_update_count, 50); + // 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()) { + fixed_update_count++; + loop_manager.loop_timer->advance_fixed_update(); + } + + loop_manager.loop_timer->enforce_frame_rate(); + } + ASSERT_EQ(fixed_update_count, 50); } diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 6e3f118..9068c72 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -1,5 +1,5 @@ -#include <gtest/gtest.h> #include <chrono> +#include <gtest/gtest.h> #include <thread> #define private public #define protected public @@ -10,72 +10,67 @@ using namespace crepe; class LoopTimerTest : public ::testing::Test { protected: - LoopTimer loop_timer; + LoopTimer loop_timer; - void SetUp() override { - loop_timer.start(); - } + void SetUp() override { 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); + // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) + loop_timer.set_target_fps(60); - auto start_time = steady_clock::now(); - loop_timer.enforce_frame_rate(); + auto start_time = steady_clock::now(); + loop_timer.enforce_frame_rate(); - auto elapsed_time = steady_clock::now() - start_time; - auto elapsed_ms = duration_cast<milliseconds>(elapsed_time).count(); + auto elapsed_time = steady_clock::now() - start_time; + auto elapsed_ms = duration_cast<milliseconds>(elapsed_time).count(); - // 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 + // 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<double>(1.0 / 120.0); + // Set the target FPS to 120 + loop_timer.set_target_fps(120); - ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001); + // Calculate the expected frame time (~8.33ms per frame) + auto expected_frame_time = std::chrono::duration<double>(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); + // 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(); - 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(); - // Check the delta time - double delta_time = loop_timer.get_delta_time(); + auto elapsed_time = duration_cast<milliseconds>(end_time - start_time).count(); - auto elapsed_time = duration_cast<milliseconds>(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); + // 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); } TEST_F(LoopTimerTest, getCurrentTime) { - // Set the target FPS to 60 (16.67 ms per frame) - loop_timer.set_target_fps(60); + // Set the target FPS to 60 (16.67 ms per frame) + loop_timer.set_target_fps(60); - auto start_time = steady_clock::now(); + auto start_time = steady_clock::now(); - // Sleep for 500 milliseconds - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // Sleep for 500 milliseconds + std::this_thread::sleep_for(std::chrono::milliseconds(100)); - loop_timer.update(); + loop_timer.update(); - auto end_time = steady_clock::now(); + auto end_time = steady_clock::now(); - // Get the elapsed time in seconds as a double - auto elapsed_time = duration_cast<std::chrono::duration<double>>(end_time - start_time).count(); + // Get the elapsed time in seconds as a double + auto elapsed_time + = duration_cast<std::chrono::duration<double>>(end_time - start_time).count(); - ASSERT_NEAR(loop_timer.get_current_time(), elapsed_time, 0.001); - - + ASSERT_NEAR(loop_timer.get_current_time(), elapsed_time, 0.001); } - - |