#include "tests/ClockTests.h" #include "tests/Test.h" #include "utils/Clock.h" static void testUpdate(Test& test) { Clock c; Clock::Nanos n1 = 0; Clock::Nanos n2 = 0; Clock::Nanos n3 = 0; Clock::Nanos n4 = 0; Error e1 = c.update(n1); Error e2 = c.update(n2); Error e3 = c.update(n3); Error e4 = c.update(n4); test.checkFalse(e1.has(), "time measurement worked 1"); test.checkFalse(e2.has(), "time measurement worked 2"); test.checkFalse(e3.has(), "time measurement worked 3"); test.checkFalse(e4.has(), "time measurement worked 4"); test.checkTrue(n1 == 0, "time measurement is 0 the first time"); test.checkTrue(n2 > 0, "time measurement is positive 2"); test.checkTrue(n3 > 0, "time measurement is positive 3"); test.checkTrue(n4 > 0, "time measurement is positive 4"); } static void testUpdatesPerSecond(Test& test) { Clock c; for(int i = 0; i < 1000; i++) { Clock::Nanos n = 0; Error e = c.update(n); test.checkFalse(e.has(), "time measurement worked per second"); } test.checkTrue(c.getUpdatesPerSecond() > 0.0f, "updates per second are a positive"); } static void testWait(Test& test, Clock::Nanos wait) { Clock c; Clock::Nanos n = 0; Error e = c.update(n); test.checkFalse(e.has(), "time measurement worked for wait 1"); test.checkFalse(c.wait(wait).has(), "wait worked"); Clock::Nanos n2 = 0; e = c.update(n2); test.checkFalse(e.has(), "time measurement worked for wait 2"); test.checkEqual(true, n2 >= wait && n2 <= wait * 11 / 10, "wait passes time but not too much"); } void ClockTests::test() { Test test("Clock"); testUpdate(test); testUpdatesPerSecond(test); testWait(test, 50'000'000); testWait(test, 1'300'000'000); test.finalize(); }