platformTimerTest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/platformTimer.h"
  25. #include "core/util/journal/process.h"
  26. #include "math/mMath.h"
  27. TEST(Platform, AdvanceTime)
  28. {
  29. U32 time = Platform::getVirtualMilliseconds();
  30. Platform::advanceTime(10);
  31. U32 newTime = Platform::getVirtualMilliseconds();
  32. EXPECT_EQ(10, newTime - time)
  33. << "We advanced 10ms but didn't get a 10ms delta!";
  34. }
  35. TEST(Platform, Sleep)
  36. {
  37. U32 start = Platform::getRealMilliseconds();
  38. Platform::sleep(500);
  39. U32 end = Platform::getRealMilliseconds();
  40. EXPECT_GE(end - start, 500)
  41. << "We didn't sleep at least as long as we requested!";
  42. };
  43. TEST(TimeManager, BasicAPI)
  44. {
  45. struct handle
  46. {
  47. S32 mElapsedTime;
  48. S32 mNumberCalls;
  49. void timeEvent(S32 timeDelta)
  50. {
  51. mElapsedTime += timeDelta;
  52. mNumberCalls++;
  53. if(mElapsedTime >= 1000)
  54. Process::requestShutdown();
  55. }
  56. } handler;
  57. handler.mElapsedTime = handler.mNumberCalls = 0;
  58. // Initialize the time manager...
  59. TimeManager time;
  60. time.timeEvent.notify(&handler, &handle::timeEvent);
  61. // Event loop till at least one second has passed.
  62. const U32 start = Platform::getRealMilliseconds();
  63. while(Process::processEvents())
  64. {
  65. // If we go too long, kill it off...
  66. if(Platform::getRealMilliseconds() - start > 30*1000)
  67. {
  68. EXPECT_TRUE(false)
  69. << "Terminated process loop due to watchdog, not due to time manager event, after 30 seconds.";
  70. Process::requestShutdown();
  71. }
  72. }
  73. const U32 end = Platform::getRealMilliseconds();
  74. // Now, confirm we have approximately similar elapsed times.
  75. S32 elapsedRealTime = end - start;
  76. EXPECT_LT(mAbs(elapsedRealTime - handler.mElapsedTime), 50)
  77. << "Failed to elapse time to within the desired tolerance.";
  78. EXPECT_GT(handler.mNumberCalls, 0)
  79. << "Somehow got no event callbacks from TimeManager?";
  80. };
  81. #endif