Torque3D Documentation / _generateds / platformTimerTest.cpp

platformTimerTest.cpp

Engine/source/platform/test/platformTimerTest.cpp

More...

Detailed Description

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