2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <ranges>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <test/test.hpp>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <time/timer.hpp>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								namespace lt {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								using lt::test::Case;
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								using lt::test::expect_le;
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								using lt::test::Suite;
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-17 08:20:48 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// error margin is high since run-time may slow down extremely due to
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// sanitization/debugging or execution through valgrind...
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								//
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// <1us error margin is tested manually in release builds and it works fine.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								constexpr auto max_error_margin = std::chrono::milliseconds { 1 };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								Suite raii = "raii"_suite = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									using std::chrono::microseconds;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "default" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										Timer {};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "unhappy path throws" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "plenty" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-25 15:07:13 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										for (auto idx : std::views::iota(0, 100'001))
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										{
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											Timer {};
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								Suite reset_and_elapsed_time = "reset_and_elapsed_time"_suite = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									using std::chrono::hours;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									using std::chrono::microseconds;
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "won't throw" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										Timer {}.reset();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										std::ignore = Timer {}.elapsed_time();
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "elapsed time is sane" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-17 08:20:48 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										expect_le(Timer {}.elapsed_time(), max_error_margin);
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "elapsed time is sane - constructed with old now" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										const auto timepoint = Timer::Clock::now() - hours { 1 };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										// This fails sometimes in debug if error-range is under 10us
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-17 08:20:48 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										expect_le(Timer { timepoint }.elapsed_time(), hours { 1 } + max_error_margin);
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "reset -> elapsed time is sane - constructed with old now" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										auto timer = Timer { Timer::Clock::now() - microseconds { 100 } };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										const auto old_elapsed_time = timer.elapsed_time();
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										timer.reset();
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-17 08:20:48 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										expect_le(timer.elapsed_time() - old_elapsed_time, microseconds { 100 } + max_error_margin);
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-09-30 06:44:09 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Case { "reset -> elapsed time is sane - reset with future now" } = [] {
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										auto timer = Timer {};
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:53:30 +03:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										const auto old_elapsed_time = timer.elapsed_time();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										timer.reset(Timer::Clock::now() + microseconds { 100 });
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-17 08:20:48 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										expect_le(timer.elapsed_time() - old_elapsed_time, microseconds { 100 } + max_error_margin);
							 | 
						
					
						
							
								
									
										
										
										
											2025-07-15 15:11:03 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								} // namespace lt
							 |