ClockTests.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. module Tests;
  2. import Core.Clock;
  3. import Core.Test;
  4. import Core.Types;
  5. import ErrorSimulator;
  6. using Core::Clock;
  7. static void testUpdate() {
  8. Clock c;
  9. Core::testTrue(c.update() == 0);
  10. Core::testTrue(c.update() >= 0);
  11. Core::testTrue(c.update() >= 0);
  12. Core::testTrue(c.update() >= 0);
  13. }
  14. static void testUpdatesPerSecond() {
  15. Clock c;
  16. for(int i = 0; i < 1000; i++) {
  17. Core::testTrue(c.update() >= 0);
  18. }
  19. Core::testTrue(c.getUpdatesPerSecond() > 0.0f);
  20. }
  21. static void testSleep(i64 nanos) {
  22. Clock c;
  23. Core::testTrue(c.update() >= 0);
  24. Core::testFalse(Clock::sleepNanos(nanos));
  25. i64 n = c.update();
  26. Core::testTrue(n >= nanos && n <= nanos * 13 / 10);
  27. }
  28. static void testSleepMillis(i64 millis) {
  29. Clock c;
  30. Core::testTrue(c.update() >= 0);
  31. Core::testFalse(Clock::sleepMillis(millis));
  32. i64 n = c.update();
  33. i64 nanos = millis * 1'000'000;
  34. Core::testTrue(n >= nanos && n <= nanos * 13 / 10);
  35. }
  36. static void testFail() {
  37. Clock c;
  38. #ifdef ERROR_SIMULATOR
  39. failStepThrow = 1;
  40. Core::test(-1l, c.update());
  41. failStepThrow = 1;
  42. Core::test(-1l, Clock::getNanos());
  43. failStepThrow = 1;
  44. Core::testTrue(Clock::sleepMillis(5));
  45. failStepThrow = 0;
  46. #endif
  47. }
  48. void testClock(bool light) {
  49. testUpdate();
  50. testUpdatesPerSecond();
  51. testSleep(light ? 5'000'000 : 50'000'000);
  52. testSleep(light ? 50'000'000 : 1'300'000'000);
  53. testSleepMillis(light ? 5 : 50);
  54. testSleepMillis(light ? 50 : 1300);
  55. testFail();
  56. }