File tree Expand file tree Collapse file tree 7 files changed +120
-31
lines changed Expand file tree Collapse file tree 7 files changed +120
-31
lines changed Original file line number Diff line number Diff line change
1
+ #include " TimeHelpers.h"
2
+ #include < unistd.h>
3
+
4
+ namespace UnitTest {
5
+ namespace Detail {
6
+
7
+ TimerImplDefault::TimerImplDefault ()
8
+ {
9
+ }
10
+
11
+ void TimerImplDefault::Start ()
12
+ {
13
+ }
14
+
15
+ double TimerImplDefault::GetTimeInMs () const
16
+ {
17
+ return 0 ;
18
+ }
19
+
20
+ void SleepMsImplDefault (int ms)
21
+ {
22
+ }
23
+
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef UNITTEST_DEFAULT_TIMEHELPERS_H
2
+ #define UNITTEST_DEFAULT_TIMEHELPERS_H
3
+
4
+ #include < sys/time.h>
5
+
6
+ // An implementation on platforms that are not supported
7
+
8
+ namespace UnitTest {
9
+ namespace Detail {
10
+
11
+ class TimerImplDefault
12
+ {
13
+ public:
14
+ TimerImplDefault ();
15
+ void Start ();
16
+ double GetTimeInMs () const ;
17
+
18
+ private:
19
+ };
20
+
21
+ void SleepMsImplDefault (int ms);
22
+ }
23
+ }
24
+
25
+ #endif
Original file line number Diff line number Diff line change 2
2
#include < unistd.h>
3
3
4
4
namespace UnitTest {
5
+ namespace Detail {
5
6
6
- Timer::Timer ()
7
+ TimerImplPosix::TimerImplPosix ()
7
8
{
8
9
m_startTime.tv_sec = 0 ;
9
10
m_startTime.tv_usec = 0 ;
10
11
}
11
12
12
- void Timer ::Start ()
13
+ void TimerImplPosix ::Start ()
13
14
{
14
15
gettimeofday (&m_startTime, 0 );
15
16
}
16
17
17
- double Timer ::GetTimeInMs () const
18
+ double TimerImplPosix ::GetTimeInMs () const
18
19
{
19
20
struct timeval currentTime;
20
21
gettimeofday (¤tTime, 0 );
@@ -25,9 +26,10 @@ namespace UnitTest {
25
26
return (dsecs * 1000.0 ) + (dus / 1000.0 );
26
27
}
27
28
28
- void TimeHelpers::SleepMs (int ms)
29
+ void SleepMsImplPosix (int ms)
29
30
{
30
31
usleep (static_cast <useconds_t >(ms * 1000 ));
31
32
}
32
33
33
34
}
35
+ }
Original file line number Diff line number Diff line change 1
- #ifndef UNITTEST_TIMEHELPERS_H
2
- #define UNITTEST_TIMEHELPERS_H
1
+ #ifndef UNITTEST_POSIX_TIMEHELPERS_H
2
+ #define UNITTEST_POSIX_TIMEHELPERS_H
3
3
4
4
#include < sys/time.h>
5
5
6
6
namespace UnitTest {
7
+ namespace Detail {
7
8
8
- class Timer
9
+ class TimerImplPosix
9
10
{
10
11
public:
11
- Timer ();
12
+ TimerImplPosix ();
12
13
void Start ();
13
14
double GetTimeInMs () const ;
14
15
15
16
private:
16
17
struct timeval m_startTime;
17
18
};
18
19
19
-
20
- namespace TimeHelpers
21
- {
22
- void SleepMs (int ms);
23
- }
24
-
25
-
20
+ void SleepMsImplPosix (int ms);
21
+ }
26
22
}
27
23
28
24
#endif
Original file line number Diff line number Diff line change
1
+ #ifndef UNITTEST_TIMEHELPERS_H
2
+ #define UNITTEST_TIMEHELPERS_H
3
+
1
4
#include " Config.h"
2
5
3
6
#if defined UNITTEST_POSIX
4
7
#include " Posix/TimeHelpers.h"
5
- #else
8
+ #elif defined UNITTEST_WIN32
6
9
#include " Win32/TimeHelpers.h"
10
+ #else
11
+ #include " Default/TimeHelpers.h"
12
+ #endif
13
+
14
+ namespace UnitTest {
15
+
16
+ class Timer
17
+ {
18
+ #if defined UNITTEST_POSIX
19
+ typedef UnitTest::Detail::TimerImplPosix TimerImpl;
20
+ #elif defined UNITTEST_WIN32
21
+ typedef UnitTest::Detail::TimerImplWin32 TimerImpl;
22
+ #else
23
+ typedef UnitTest::Detail::TimerImplDefault TimerImpl;
24
+ #endif
25
+
26
+ public:
27
+ Timer () {}
28
+ void Start () { m_timer.Start (); }
29
+ double GetTimeInMs () const { return m_timer.GetTimeInMs (); }
30
+
31
+ private:
32
+ TimerImpl m_timer;
33
+ };
34
+
35
+ namespace TimeHelpers {
36
+ static inline void SleepMs (int ms)
37
+ {
38
+ #if defined UNITTEST_POSIX
39
+ UnitTest::Detail::SleepMsImplPosix (ms);
40
+ #elif defined UNITTEST_WIN32
41
+ UnitTest::Detail::SleepMsImplWin32 (ms);
42
+ #else
43
+ UnitTest::Detail::SleepMsImplDefault (ms);
44
+ #endif
45
+ }
46
+ }
47
+ }
48
+
7
49
#endif
Original file line number Diff line number Diff line change 4
4
#include < windows.h>
5
5
6
6
namespace UnitTest {
7
+ namespace Detail {
7
8
8
- Timer::Timer ()
9
+ TimerImplWin32::TimerImplWin32 ()
9
10
: m_threadHandle(::GetCurrentThread())
10
11
, m_startTime(0 )
11
12
{
@@ -20,19 +21,19 @@ namespace UnitTest {
20
21
::SetThreadAffinityMask (m_threadHandle, m_processAffinityMask);
21
22
}
22
23
23
- void Timer ::Start ()
24
+ void TimerImplWin32 ::Start ()
24
25
{
25
26
m_startTime = GetTime ();
26
27
}
27
28
28
- double Timer ::GetTimeInMs () const
29
+ double TimerImplWin32 ::GetTimeInMs () const
29
30
{
30
31
__int64 const elapsedTime = GetTime () - m_startTime;
31
32
double const seconds = double (elapsedTime) / double (m_frequency);
32
33
return seconds * 1000.0 ;
33
34
}
34
35
35
- __int64 Timer ::GetTime () const
36
+ __int64 TimerImplWin32 ::GetTime () const
36
37
{
37
38
LARGE_INTEGER curTime;
38
39
::SetThreadAffinityMask (m_threadHandle, 1 );
@@ -41,9 +42,10 @@ namespace UnitTest {
41
42
return curTime.QuadPart ;
42
43
}
43
44
44
- void TimeHelpers::SleepMs (int ms)
45
+ void SleepMsImplWin32 (int ms)
45
46
{
46
47
::Sleep (ms);
47
48
}
48
49
49
50
}
51
+ }
Original file line number Diff line number Diff line change 1
- #ifndef UNITTEST_TIMEHELPERS_H
2
- #define UNITTEST_TIMEHELPERS_H
1
+ #ifndef UNITTEST_WIN32_TIMEHELPERS_H
2
+ #define UNITTEST_WIN32_TIMEHELPERS_H
3
3
4
4
#include " ../Config.h"
5
5
#include " ../HelperMacros.h"
11
11
#endif
12
12
13
13
namespace UnitTest {
14
+ namespace Detail {
14
15
15
- class UNITTEST_LINKAGE Timer
16
+ class UNITTEST_LINKAGE TimerImplWin32
16
17
{
17
18
public:
18
- Timer ();
19
+ TimerImplWin32 ();
19
20
void Start ();
20
21
double GetTimeInMs () const ;
21
22
@@ -34,12 +35,8 @@ namespace UnitTest {
34
35
__int64 m_frequency;
35
36
};
36
37
37
-
38
- namespace TimeHelpers
39
- {
40
- UNITTEST_LINKAGE void SleepMs (int ms);
41
- }
42
-
38
+ UNITTEST_LINKAGE void SleepMsImplWin32 (int ms);
39
+ }
43
40
}
44
41
45
42
#endif
You can’t perform that action at this time.
0 commit comments