ClockTests.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "tests/ClockTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/Clock.h"
  4. static void testUpdate(Test& test) {
  5. Clock c;
  6. Clock::Nanos n1 = 0;
  7. Clock::Nanos n2 = 0;
  8. Clock::Nanos n3 = 0;
  9. Clock::Nanos n4 = 0;
  10. Error e1 = c.update(n1);
  11. Error e2 = c.update(n2);
  12. Error e3 = c.update(n3);
  13. Error e4 = c.update(n4);
  14. test.checkFalse(e1.has(), "time measurement worked 1");
  15. test.checkFalse(e2.has(), "time measurement worked 2");
  16. test.checkFalse(e3.has(), "time measurement worked 3");
  17. test.checkFalse(e4.has(), "time measurement worked 4");
  18. test.checkTrue(n1 == 0, "time measurement is 0 the first time");
  19. test.checkTrue(n2 > 0, "time measurement is positive 2");
  20. test.checkTrue(n3 > 0, "time measurement is positive 3");
  21. test.checkTrue(n4 > 0, "time measurement is positive 4");
  22. }
  23. static void testUpdatesPerSecond(Test& test) {
  24. Clock c;
  25. for(int i = 0; i < 1000; i++) {
  26. Clock::Nanos n = 0;
  27. Error e = c.update(n);
  28. test.checkFalse(e.has(), "time measurement worked per second");
  29. }
  30. test.checkTrue(c.getUpdatesPerSecond() > 0.0f,
  31. "updates per second are a positive");
  32. }
  33. static void testWait(Test& test, Clock::Nanos wait) {
  34. Clock c;
  35. Clock::Nanos n = 0;
  36. Error e = c.update(n);
  37. test.checkFalse(e.has(), "time measurement worked for wait 1");
  38. test.checkFalse(c.wait(wait).has(), "wait worked");
  39. Clock::Nanos n2 = 0;
  40. e = c.update(n2);
  41. test.checkFalse(e.has(), "time measurement worked for wait 2");
  42. test.checkEqual(true, n2 >= wait && n2 <= wait * 11 / 10,
  43. "wait passes time but not too much");
  44. }
  45. void ClockTests::test() {
  46. Test test("Clock");
  47. testUpdate(test);
  48. testUpdatesPerSecond(test);
  49. testWait(test, 50'000'000);
  50. testWait(test, 1'300'000'000);
  51. test.finalize();
  52. }