diff --git a/src/lol/sys/timer.h b/src/lol/sys/timer.h index e2285021..cfee3797 100644 --- a/src/lol/sys/timer.h +++ b/src/lol/sys/timer.h @@ -37,10 +37,13 @@ private: float GetSeconds(bool reset) { - std::chrono::steady_clock::time_point tp, tp0 = m_tp; - tp = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point + tp = std::chrono::steady_clock::now(), + tp0 = m_tp; + if (reset) m_tp = tp; + return std::chrono::duration_cast>(tp - tp0).count(); } diff --git a/src/t/Makefile.am b/src/t/Makefile.am index baac5afb..be4c22c0 100644 --- a/src/t/Makefile.am +++ b/src/t/Makefile.am @@ -28,7 +28,7 @@ test_math_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/tools/lolunit test_math_DEPENDENCIES = @LOL_DEPS@ test_sys_SOURCES = test-common.cpp \ - sys/thread.cpp + sys/thread.cpp sys/timer.cpp test_sys_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/tools/lolunit test_sys_DEPENDENCIES = @LOL_DEPS@ diff --git a/src/t/sys/timer.cpp b/src/t/sys/timer.cpp new file mode 100644 index 00000000..e96b33ae --- /dev/null +++ b/src/t/sys/timer.cpp @@ -0,0 +1,49 @@ +// +// Lol Engine — Unit tests +// +// Copyright © 2010—2015 Sam Hocevar +// © 2014—2015 Benjamin “Touky” Huet +// © 2014—2015 Guillaume Bittoun +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#include + +#include + +#include +namespace lol +{ + +lolunit_declare_fixture(timer_test) +{ + void setup() + { + } + + void teardown() + { + } + + lolunit_declare_test(timers) + { + Timer timer0, timer1; + + timer0.Wait(1.5); + + float t0 = timer1.Poll(); + float t1 = timer1.Get(); + lolunit_assert_doubles_equal(t0, t1, 1e-5); + lolunit_assert_doubles_equal(1.5, t1, 1e-3); + + timer1.Wait(1.5); + lolunit_assert_doubles_equal(3.0, timer0.Get(), 1e-3); + } +}; + +} \ No newline at end of file