ClockTests.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/utils/Clock.hpp"
  4. static void testUpdate() {
  5. Core::Clock c;
  6. Core::Clock::Nanos n1 = 0;
  7. Core::Clock::Nanos n2 = 0;
  8. Core::Clock::Nanos n3 = 0;
  9. Core::Clock::Nanos n4 = 0;
  10. CORE_TEST_ERROR(c.update(n1));
  11. CORE_TEST_ERROR(c.update(n2));
  12. CORE_TEST_ERROR(c.update(n3));
  13. CORE_TEST_ERROR(c.update(n4));
  14. CORE_TEST_TRUE(n1 == 0);
  15. CORE_TEST_TRUE(n2 > 0);
  16. CORE_TEST_TRUE(n3 > 0);
  17. CORE_TEST_TRUE(n4 > 0);
  18. }
  19. static void testUpdatesPerSecond() {
  20. Core::Clock c;
  21. for(int i = 0; i < 1000; i++) {
  22. Core::Clock::Nanos n = 0;
  23. CORE_TEST_ERROR(c.update(n));
  24. }
  25. CORE_TEST_TRUE(c.getUpdatesPerSecond() > 0.0f);
  26. }
  27. static void testWait(Core::Clock::Nanos wait) {
  28. Core::Clock c;
  29. Core::Clock::Nanos n = 0;
  30. CORE_TEST_ERROR(c.update(n));
  31. CORE_TEST_ERROR(Core::Clock::wait(wait));
  32. Core::Clock::Nanos n2 = 0;
  33. CORE_TEST_ERROR(c.update(n2));
  34. CORE_TEST_TRUE(n2 >= wait && n2 <= wait * 13 / 10);
  35. }
  36. static void testFail() {
  37. #ifdef ERROR_SIMULATOR
  38. Core::Clock::Nanos n = 0;
  39. Core::Fail::timeGet = true;
  40. CORE_TEST_EQUAL(Core::Error::TIME_NOT_AVAILABLE, Core::Clock::getNanos(n));
  41. Core::Fail::timeGet = false;
  42. #endif
  43. }
  44. void Core::testClock(bool light) {
  45. testUpdate();
  46. testUpdatesPerSecond();
  47. testWait(light ? 5'000'000 : 50'000'000);
  48. testWait(light ? 50'000'000 : 1'300'000'000);
  49. testFail();
  50. }