From ee4483bfbb1746727e44c323a4ac540b482d770d Mon Sep 17 00:00:00 2001 From: light7734 Date: Tue, 15 Jul 2025 15:11:03 +0330 Subject: [PATCH] tests: add tests for time module & refactor: time module --- modules/app/src/application.cpp | 3 +- modules/time/CMakeLists.txt | 5 ++++ modules/time/include/time/timer.hpp | 22 ++++++--------- modules/time/src/timer.cpp | 12 +++++++- modules/time/src/timer.tests.cpp | 44 +++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 modules/time/src/timer.tests.cpp diff --git a/modules/app/src/application.cpp b/modules/app/src/application.cpp index 514da8a..1138743 100644 --- a/modules/app/src/application.cpp +++ b/modules/app/src/application.cpp @@ -103,7 +103,8 @@ void Application::update_layers() { for (auto &it : *m_layer_stack) { - it->on_update(m_timer.get_elapsed_time()); + // narrowing double -> float + it->on_update(static_cast(m_timer.elapsed_time().count())); } // TODO(Light): each layer should have their own "delta time" diff --git a/modules/time/CMakeLists.txt b/modules/time/CMakeLists.txt index 2f77dbd..74d3bec 100644 --- a/modules/time/CMakeLists.txt +++ b/modules/time/CMakeLists.txt @@ -1 +1,6 @@ add_library_module(time timer.cpp) + +add_executable(timer_tests ${CMAKE_CURRENT_SOURCE_DIR}/src/timer.tests.cpp) +target_include_directories(timer_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_link_libraries(timer_tests PRIVATE time test) + diff --git a/modules/time/include/time/timer.hpp b/modules/time/include/time/timer.hpp index 54a603b..a6101de 100644 --- a/modules/time/include/time/timer.hpp +++ b/modules/time/include/time/timer.hpp @@ -4,28 +4,22 @@ namespace lt { +/** Simple timer class to keep track of the elapsed time */ class Timer { public: - Timer(); + using Timepoint = std::chrono::time_point; + using Duration = std::chrono::duration; + using Clock = std::chrono::steady_clock; - [[nodiscard]] auto get_elapsed_time() const -> float - { - using std::chrono::duration_cast; - using std::chrono::milliseconds; - using std::chrono::steady_clock; + Timer(Timepoint start = Clock::now()); - auto rep = duration_cast(steady_clock::now() - m_start).count(); - return static_cast(rep) / 1000.f; - } + void reset(Timepoint start = Clock::now()); - void reset() - { - m_start = std::chrono::steady_clock::now(); - } + [[nodiscard]] auto elapsed_time() const -> Duration; private: - std::chrono::time_point m_start; + Timepoint m_start; }; } // namespace lt diff --git a/modules/time/src/timer.cpp b/modules/time/src/timer.cpp index 2ce05bc..16bcd86 100644 --- a/modules/time/src/timer.cpp +++ b/modules/time/src/timer.cpp @@ -2,8 +2,18 @@ namespace lt { -Timer::Timer(): m_start(std::chrono::steady_clock::now()) +Timer::Timer(Timepoint start): m_start(start) { } +void Timer::reset(Timepoint start) +{ + m_start = start; +} + +[[nodiscard]] auto Timer::elapsed_time() const -> Duration +{ + return { std::chrono::steady_clock::now() - m_start }; +} + } // namespace lt diff --git a/modules/time/src/timer.tests.cpp b/modules/time/src/timer.tests.cpp new file mode 100644 index 0000000..0f960be --- /dev/null +++ b/modules/time/src/timer.tests.cpp @@ -0,0 +1,44 @@ +#include +#include +#include