aboutsummaryrefslogtreecommitdiff
path: root/src/test/LoopManagerTest.cpp
diff options
context:
space:
mode:
authorWboerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-11 19:40:58 +0100
committerGitHub <noreply@github.com>2024-12-11 19:40:58 +0100
commit59954bfc14cdb32997a3fb09e6ee1b393a4dc027 (patch)
treef1da5eeb955bc44fd4cf3bb22bcaf989116f0ef8 /src/test/LoopManagerTest.cpp
parent30c17c98e54c1534664de08ca3838c40c859d166 (diff)
parenta464096dfff2ee4605e77852020e6a5c0fa5aa58 (diff)
Merge pull request #63 from lonkaars/wouter/gameloop
gameloop improvements + Singelton removal
Diffstat (limited to 'src/test/LoopManagerTest.cpp')
-rw-r--r--src/test/LoopManagerTest.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp
new file mode 100644
index 0000000..af89d64
--- /dev/null
+++ b/src/test/LoopManagerTest.cpp
@@ -0,0 +1,76 @@
+#include <chrono>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <thread>
+#define private public
+#define protected public
+#include <crepe/api/LoopManager.h>
+#include <crepe/manager/EventManager.h>
+#include <crepe/manager/LoopTimerManager.h>
+using namespace std::chrono;
+using namespace crepe;
+
+class LoopManagerTest : public ::testing::Test {
+protected:
+ class TestGameLoop : public crepe::LoopManager {
+ public:
+ MOCK_METHOD(void, fixed_update, (), (override));
+ MOCK_METHOD(void, frame_update, (), (override));
+ };
+
+ TestGameLoop test_loop;
+ void SetUp() override {}
+};
+
+TEST_F(LoopManagerTest, FixedUpdate) {
+ // Arrange
+ test_loop.loop_timer.set_target_framerate(60);
+
+ // Set expectations for the mock calls
+ EXPECT_CALL(test_loop, frame_update).Times(::testing::Between(55, 65));
+ EXPECT_CALL(test_loop, fixed_update).Times(::testing::Between(48, 52));
+
+ // Start the loop in a separate thread
+ std::thread loop_thread([&]() { test_loop.start(); });
+
+ // Let the loop run for exactly 1 second
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+
+ // Stop the game loop
+ test_loop.game_running = false;
+ // Wait for the loop thread to finish
+ loop_thread.join();
+
+ // Test finished
+}
+TEST_F(LoopManagerTest, ScaledFixedUpdate) {
+ // Arrange
+ test_loop.loop_timer.set_target_framerate(60);
+
+ // Set expectations for the mock calls
+ EXPECT_CALL(test_loop, frame_update).Times(::testing::Between(55, 65));
+ EXPECT_CALL(test_loop, fixed_update).Times(::testing::Between(48, 52));
+
+ // Start the loop in a separate thread
+ std::thread loop_thread([&]() { test_loop.start(); });
+
+ // Let the loop run for exactly 1 second
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+
+ // Stop the game loop
+ test_loop.game_running = false;
+ // Wait for the loop thread to finish
+ loop_thread.join();
+
+ // Test finished
+}
+TEST_F(LoopManagerTest, ShutDown) {
+ // Arrange
+ test_loop.loop_timer.set_target_framerate(60);
+ // Start the loop in a separate thread
+ std::thread loop_thread([&]() { test_loop.start(); });
+ std::this_thread::sleep_for(std::chrono::milliseconds(1));
+ test_loop.event_manager.trigger_event<ShutDownEvent>(ShutDownEvent{});
+ // Wait for the loop thread to finish
+ loop_thread.join();
+}