tests: add tests for time module & refactor: time module
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	continuous-integration/drone/push Build is failing
				
			This commit is contained in:
		
							parent
							
								
									bddef4238d
								
							
						
					
					
						commit
						ee4483bfbb
					
				
					 5 changed files with 70 additions and 16 deletions
				
			
		| 
						 | 
					@ -103,7 +103,8 @@ void Application::update_layers()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	for (auto &it : *m_layer_stack)
 | 
						for (auto &it : *m_layer_stack)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		it->on_update(m_timer.get_elapsed_time());
 | 
							// narrowing double -> float
 | 
				
			||||||
 | 
							it->on_update(static_cast<float>(m_timer.elapsed_time().count()));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO(Light): each layer should have their own "delta time"
 | 
						// TODO(Light): each layer should have their own "delta time"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1 +1,6 @@
 | 
				
			||||||
add_library_module(time timer.cpp)
 | 
					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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,28 +4,22 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace lt {
 | 
					namespace lt {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** Simple timer class to keep track of the elapsed time */
 | 
				
			||||||
class Timer
 | 
					class Timer
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
	Timer();
 | 
						using Timepoint = std::chrono::time_point<std::chrono::steady_clock>;
 | 
				
			||||||
 | 
						using Duration = std::chrono::duration<double>;
 | 
				
			||||||
 | 
						using Clock = std::chrono::steady_clock;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	[[nodiscard]] auto get_elapsed_time() const -> float
 | 
						Timer(Timepoint start = Clock::now());
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		using std::chrono::duration_cast;
 | 
					 | 
				
			||||||
		using std::chrono::milliseconds;
 | 
					 | 
				
			||||||
		using std::chrono::steady_clock;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		auto rep = duration_cast<milliseconds>(steady_clock::now() - m_start).count();
 | 
						void reset(Timepoint start = Clock::now());
 | 
				
			||||||
		return static_cast<float>(rep) / 1000.f;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void reset()
 | 
						[[nodiscard]] auto elapsed_time() const -> Duration;
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		m_start = std::chrono::steady_clock::now();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	std::chrono::time_point<std::chrono::steady_clock> m_start;
 | 
						Timepoint m_start;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} // namespace lt
 | 
					} // namespace lt
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,8 +2,18 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace lt {
 | 
					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
 | 
					} // namespace lt
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										44
									
								
								modules/time/src/timer.tests.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								modules/time/src/timer.tests.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,44 @@
 | 
				
			||||||
 | 
					#include <ranges>
 | 
				
			||||||
 | 
					#include <test/test.hpp>
 | 
				
			||||||
 | 
					#include <time/timer.hpp>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace lt {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using lt::test::expect_le;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					lt::test::Suite raii = [] {
 | 
				
			||||||
 | 
						lt::test::Case { "default" } = [] {
 | 
				
			||||||
 | 
							auto timer = Timer {};
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lt::test::Case { "plenty" } = [] {
 | 
				
			||||||
 | 
							for (auto i : std::views::iota(0, 101))
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								auto timer = Timer {};
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lt::test::Case { "unhappy" } = [] {
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lt::test::Case { "has sane elapsed time" } = [] {
 | 
				
			||||||
 | 
							auto elapsed_time = Timer {}.elapsed_time();
 | 
				
			||||||
 | 
							expect_le(elapsed_time, std::chrono::seconds { 1 });
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					lt::test::Suite reset = [] {
 | 
				
			||||||
 | 
						lt::test::Case { "non-throwing" } = [] {
 | 
				
			||||||
 | 
							auto timer = Timer {};
 | 
				
			||||||
 | 
							timer.reset();
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lt::test::Case { "resets elapsed time" } = [] {
 | 
				
			||||||
 | 
							auto timer = Timer {};
 | 
				
			||||||
 | 
							auto elapsed_time = timer.elapsed_time();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							timer.reset();
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace lt
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue