ClockTests.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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::ErrorCode::TIME_NOT_AVAILABLE,
  41. Core::Clock::getNanos(n));
  42. Core::Fail::timeGet = false;
  43. #endif
  44. }
  45. void Core::testClock(bool light) {
  46. testUpdate();
  47. testUpdatesPerSecond();
  48. testWait(light ? 5'000'000 : 50'000'000);
  49. testWait(light ? 50'000'000 : 1'300'000'000);
  50. testFail();
  51. }