#include "Test.h" #include "Timer.h" #include #include #include #include #include #include #include #include namespace Timer { namespace Test { void print() { Logging::Log(Logging::Severity::Info, "Print"); } void TestTimerAbort() { Timer timer; std::function f_print = print; std::stringstream ss; ss << "---" << std::endl; auto start = std::chrono::steady_clock::now(); std::clock_t begin = std::clock(); timer.StartContinuous(1000, f_print); timer.Stop(); timer.Wait(); auto elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start); ss << "Elapsed: " << elapsed.count() << std::endl; ss << "---" << std::endl << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); ss.str(""); ss << "---" << std::endl; start = std::chrono::steady_clock::now(); timer.StartContinuous(3000, f_print); timer.Abort(); timer.Wait(); elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start); ss << "Elapsed: " << elapsed.count() << std::endl; ss << "---" << std::endl << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); ss.str(""); ss << "---" << std::endl; start = std::chrono::steady_clock::now(); timer.StartContinuous(3000, f_print); sleep(1); timer.Abort(); timer.Wait(); elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start); ss << "Elapsed: " << elapsed.count() << std::endl; ss << "---" << std::endl << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); } template std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } void OnTimerEnd(const std::string& type, int id) { std::stringstream ss; ss << "OnTimerEnd() " << type << " " << id << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); } void StartNewTimer(std::deque& timers) { int delay(2000); std::string identifier("Blabla"); int id(10); // Protect! auto timer = make_unique(); timer->StartSingle(delay, static_cast>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id); timers.push_back(std::move(timer)); } void Cleanup(std::deque& timers) { timers.erase(std::remove_if(timers.begin(), timers.end(), [](TimerPointer& t){return !t->IsRunning();}), timers.end()); } void TestTimerDeque() { std::stringstream ss; std::deque timers; StartNewTimer(timers); ss << "Count: " << timers.size() << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); ss.str(""); StartNewTimer(timers); ss << "Count: " << timers.size() << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); sleep(5); ss.str(""); ss << "Count: " << timers.size() << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); ss.str(""); Cleanup(timers); ss << "Count: " << timers.size() << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); } bool TestTimer() { std::stringstream ss; ss << "----------------------------------" << std::endl; ss << "| TestTimer |" << std::endl; ss << "----------------------------------" << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); TestTimerAbort(); TestTimerDeque(); ss.str(""); ss << "==================================" << std::endl << std::endl; Logging::Log(Logging::Severity::Info, ss.str()); return true; } } // namespace Test } // namespace Timer /* */