aboutsummaryrefslogtreecommitdiff
path: root/src/test/loopTimerTest.cpp
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-04 16:38:15 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-04 16:38:15 +0100
commit5a43793e247fbffec590d334b89cc34d19049f45 (patch)
tree9447beee8ac21e4343927c595da4890c23223164 /src/test/loopTimerTest.cpp
parent1e72559664cb7cc68c1c404f1709d679d35a66e2 (diff)
gameloop tests
Diffstat (limited to 'src/test/loopTimerTest.cpp')
-rw-r--r--src/test/loopTimerTest.cpp45
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
}