Skip to content

Commit f70aeb8

Browse files
committed
Add "default" empty implementation for Timer and SleepMs()
1 parent bc5d87f commit f70aeb8

File tree

7 files changed

+120
-31
lines changed

7 files changed

+120
-31
lines changed

UnitTest++/Default/TimeHelpers.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

UnitTest++/Default/TimeHelpers.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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

UnitTest++/Posix/TimeHelpers.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
#include <unistd.h>
33

44
namespace UnitTest {
5+
namespace Detail {
56

6-
Timer::Timer()
7+
TimerImplPosix::TimerImplPosix()
78
{
89
m_startTime.tv_sec = 0;
910
m_startTime.tv_usec = 0;
1011
}
1112

12-
void Timer::Start()
13+
void TimerImplPosix::Start()
1314
{
1415
gettimeofday(&m_startTime, 0);
1516
}
1617

17-
double Timer::GetTimeInMs() const
18+
double TimerImplPosix::GetTimeInMs() const
1819
{
1920
struct timeval currentTime;
2021
gettimeofday(&currentTime, 0);
@@ -25,9 +26,10 @@ namespace UnitTest {
2526
return (dsecs * 1000.0) + (dus / 1000.0);
2627
}
2728

28-
void TimeHelpers::SleepMs(int ms)
29+
void SleepMsImplPosix(int ms)
2930
{
3031
usleep(static_cast<useconds_t>(ms * 1000));
3132
}
3233

3334
}
35+
}

UnitTest++/Posix/TimeHelpers.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
#ifndef UNITTEST_TIMEHELPERS_H
2-
#define UNITTEST_TIMEHELPERS_H
1+
#ifndef UNITTEST_POSIX_TIMEHELPERS_H
2+
#define UNITTEST_POSIX_TIMEHELPERS_H
33

44
#include <sys/time.h>
55

66
namespace UnitTest {
7+
namespace Detail {
78

8-
class Timer
9+
class TimerImplPosix
910
{
1011
public:
11-
Timer();
12+
TimerImplPosix();
1213
void Start();
1314
double GetTimeInMs() const;
1415

1516
private:
1617
struct timeval m_startTime;
1718
};
1819

19-
20-
namespace TimeHelpers
21-
{
22-
void SleepMs(int ms);
23-
}
24-
25-
20+
void SleepMsImplPosix(int ms);
21+
}
2622
}
2723

2824
#endif

UnitTest++/TimeHelpers.h

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1+
#ifndef UNITTEST_TIMEHELPERS_H
2+
#define UNITTEST_TIMEHELPERS_H
3+
14
#include "Config.h"
25

36
#if defined UNITTEST_POSIX
47
#include "Posix/TimeHelpers.h"
5-
#else
8+
#elif defined UNITTEST_WIN32
69
#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+
749
#endif

UnitTest++/Win32/TimeHelpers.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include <windows.h>
55

66
namespace UnitTest {
7+
namespace Detail {
78

8-
Timer::Timer()
9+
TimerImplWin32::TimerImplWin32()
910
: m_threadHandle(::GetCurrentThread())
1011
, m_startTime(0)
1112
{
@@ -20,19 +21,19 @@ namespace UnitTest {
2021
::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask);
2122
}
2223

23-
void Timer::Start()
24+
void TimerImplWin32::Start()
2425
{
2526
m_startTime = GetTime();
2627
}
2728

28-
double Timer::GetTimeInMs() const
29+
double TimerImplWin32::GetTimeInMs() const
2930
{
3031
__int64 const elapsedTime = GetTime() - m_startTime;
3132
double const seconds = double(elapsedTime) / double(m_frequency);
3233
return seconds * 1000.0;
3334
}
3435

35-
__int64 Timer::GetTime() const
36+
__int64 TimerImplWin32::GetTime() const
3637
{
3738
LARGE_INTEGER curTime;
3839
::SetThreadAffinityMask(m_threadHandle, 1);
@@ -41,9 +42,10 @@ namespace UnitTest {
4142
return curTime.QuadPart;
4243
}
4344

44-
void TimeHelpers::SleepMs(int ms)
45+
void SleepMsImplWin32(int ms)
4546
{
4647
::Sleep(ms);
4748
}
4849

4950
}
51+
}

UnitTest++/Win32/TimeHelpers.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef UNITTEST_TIMEHELPERS_H
2-
#define UNITTEST_TIMEHELPERS_H
1+
#ifndef UNITTEST_WIN32_TIMEHELPERS_H
2+
#define UNITTEST_WIN32_TIMEHELPERS_H
33

44
#include "../Config.h"
55
#include "../HelperMacros.h"
@@ -11,11 +11,12 @@
1111
#endif
1212

1313
namespace UnitTest {
14+
namespace Detail {
1415

15-
class UNITTEST_LINKAGE Timer
16+
class UNITTEST_LINKAGE TimerImplWin32
1617
{
1718
public:
18-
Timer();
19+
TimerImplWin32();
1920
void Start();
2021
double GetTimeInMs() const;
2122

@@ -34,12 +35,8 @@ namespace UnitTest {
3435
__int64 m_frequency;
3536
};
3637

37-
38-
namespace TimeHelpers
39-
{
40-
UNITTEST_LINKAGE void SleepMs(int ms);
41-
}
42-
38+
UNITTEST_LINKAGE void SleepMsImplWin32(int ms);
39+
}
4340
}
4441

4542
#endif

0 commit comments

Comments
 (0)