ClockTests.cpp 1.5 KB

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