platformTimerTest.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "testing/unitTesting.h"
  23. #include "platform/platformTimer.h"
  24. #include "core/util/journal/process.h"
  25. #include "math/mMath.h"
  26. TEST(PlatformTimerTest, AdvanceTime)
  27. {
  28. U32 time = Platform::getVirtualMilliseconds();
  29. Platform::advanceTime(10);
  30. U32 newTime = Platform::getVirtualMilliseconds();
  31. EXPECT_EQ(10, newTime - time)
  32. << "We advanced 10ms but didn't get a 10ms delta!";
  33. }
  34. TEST(PlatformTimerTest, Sleep)
  35. {
  36. U32 start = Platform::getRealMilliseconds();
  37. Platform::sleep(500);
  38. U32 end = Platform::getRealMilliseconds();
  39. EXPECT_GE(end - start, 500-10) // account for clock resolution
  40. << "We didn't sleep at least as long as we requested!";
  41. };
  42. struct handle
  43. {
  44. S32 mElapsedTime;
  45. S32 mNumberCalls;
  46. handle() : mElapsedTime(0), mNumberCalls(0) {}
  47. void timeEvent(S32 timeDelta)
  48. {
  49. mElapsedTime += timeDelta;
  50. mNumberCalls++;
  51. if(mElapsedTime >= 1000)
  52. Process::requestShutdown();
  53. }
  54. };
  55. TEST(PlatformTimerTest, BasicAPI)
  56. {
  57. handle handler;
  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. };