aboutsummaryrefslogtreecommitdiff
path: root/src/test/loopTimerTest.cpp
blob: d2f7d9beeada6d63f64e9cae1ebe010826d2bc4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <gtest/gtest.h>
#include <chrono>
#include <thread>
#include "api/LoopTimer.h"

using namespace std::chrono;
using namespace crepe;

class LoopTimerTest : public ::testing::Test {
protected:
    LoopTimer loop_timer;

    void SetUp() override {
        loop_timer.start(); // Reset loop timer before each test.
    }
};
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();

    // 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
}