Skip to content

Add "default" empty implementation for Timer and SleepMs() #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ source_group("" FILES ${headers_} ${sources_})
if (WIN32)
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
set(platformDir_ Win32)
else()
elseif(UNIX)
set(platformDir_ Posix)
else()
set(platformDir_ Default)
endif(WIN32)

file(GLOB platformHeaders_ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UnitTest++/${platformDir_}/*.h)
Expand Down
25 changes: 25 additions & 0 deletions UnitTest++/Default/TimeHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "TimeHelpers.h"
#include <unistd.h>

namespace UnitTest {
namespace Detail {

TimerImplDefault::TimerImplDefault()
{
}

void TimerImplDefault::Start()
{
}

double TimerImplDefault::GetTimeInMs() const
{
return 0;
}

void SleepMsImplDefault(int ms)
{
}

}
}
25 changes: 25 additions & 0 deletions UnitTest++/Default/TimeHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef UNITTEST_DEFAULT_TIMEHELPERS_H
#define UNITTEST_DEFAULT_TIMEHELPERS_H

#include <sys/time.h>

// An implementation on platforms that are not supported

namespace UnitTest {
namespace Detail {

class TimerImplDefault
{
public:
TimerImplDefault();
void Start();
double GetTimeInMs() const;

private:
};

void SleepMsImplDefault(int ms);
}
}

#endif
10 changes: 6 additions & 4 deletions UnitTest++/Posix/TimeHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
#include <unistd.h>

namespace UnitTest {
namespace Detail {

Timer::Timer()
TimerImplPosix::TimerImplPosix()
{
m_startTime.tv_sec = 0;
m_startTime.tv_usec = 0;
}

void Timer::Start()
void TimerImplPosix::Start()
{
gettimeofday(&m_startTime, 0);
}

double Timer::GetTimeInMs() const
double TimerImplPosix::GetTimeInMs() const
{
struct timeval currentTime;
gettimeofday(&currentTime, 0);
Expand All @@ -25,9 +26,10 @@ namespace UnitTest {
return (dsecs * 1000.0) + (dus / 1000.0);
}

void TimeHelpers::SleepMs(int ms)
void SleepMsImplPosix(int ms)
{
usleep(static_cast<useconds_t>(ms * 1000));
}

}
}
18 changes: 7 additions & 11 deletions UnitTest++/Posix/TimeHelpers.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#ifndef UNITTEST_TIMEHELPERS_H
#define UNITTEST_TIMEHELPERS_H
#ifndef UNITTEST_POSIX_TIMEHELPERS_H
#define UNITTEST_POSIX_TIMEHELPERS_H

#include <sys/time.h>

namespace UnitTest {
namespace Detail {

class Timer
class TimerImplPosix
{
public:
Timer();
TimerImplPosix();
void Start();
double GetTimeInMs() const;

private:
struct timeval m_startTime;
};


namespace TimeHelpers
{
void SleepMs(int ms);
}


void SleepMsImplPosix(int ms);
}
}

#endif
44 changes: 43 additions & 1 deletion UnitTest++/TimeHelpers.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,49 @@
#ifndef UNITTEST_TIMEHELPERS_H
#define UNITTEST_TIMEHELPERS_H

#include "Config.h"

#if defined UNITTEST_POSIX
#include "Posix/TimeHelpers.h"
#else
#elif defined UNITTEST_WIN32
#include "Win32/TimeHelpers.h"
#else
#include "Default/TimeHelpers.h"
#endif

namespace UnitTest {

class Timer
{
#if defined UNITTEST_POSIX
typedef UnitTest::Detail::TimerImplPosix TimerImpl;
#elif defined UNITTEST_WIN32
typedef UnitTest::Detail::TimerImplWin32 TimerImpl;
#else
typedef UnitTest::Detail::TimerImplDefault TimerImpl;
#endif

public:
Timer() {}
void Start() { m_timer.Start(); }
double GetTimeInMs() const { return m_timer.GetTimeInMs(); }

private:
TimerImpl m_timer;
};

namespace TimeHelpers {
static inline void SleepMs(int ms)
{
#if defined UNITTEST_POSIX
UnitTest::Detail::SleepMsImplPosix(ms);
#elif defined UNITTEST_WIN32
UnitTest::Detail::SleepMsImplWin32(ms);
#else
UnitTest::Detail::SleepMsImplDefault(ms);
#endif
}
}
}

#endif
12 changes: 7 additions & 5 deletions UnitTest++/Win32/TimeHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include <windows.h>

namespace UnitTest {
namespace Detail {

Timer::Timer()
TimerImplWin32::TimerImplWin32()
: m_threadHandle(::GetCurrentThread())
, m_startTime(0)
{
Expand All @@ -20,19 +21,19 @@ namespace UnitTest {
::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask);
}

void Timer::Start()
void TimerImplWin32::Start()
{
m_startTime = GetTime();
}

double Timer::GetTimeInMs() const
double TimerImplWin32::GetTimeInMs() const
{
__int64 const elapsedTime = GetTime() - m_startTime;
double const seconds = double(elapsedTime) / double(m_frequency);
return seconds * 1000.0;
}

__int64 Timer::GetTime() const
__int64 TimerImplWin32::GetTime() const
{
LARGE_INTEGER curTime;
::SetThreadAffinityMask(m_threadHandle, 1);
Expand All @@ -41,9 +42,10 @@ namespace UnitTest {
return curTime.QuadPart;
}

void TimeHelpers::SleepMs(int ms)
void SleepMsImplWin32(int ms)
{
::Sleep(ms);
}

}
}
17 changes: 7 additions & 10 deletions UnitTest++/Win32/TimeHelpers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef UNITTEST_TIMEHELPERS_H
#define UNITTEST_TIMEHELPERS_H
#ifndef UNITTEST_WIN32_TIMEHELPERS_H
#define UNITTEST_WIN32_TIMEHELPERS_H

#include "../Config.h"
#include "../HelperMacros.h"
Expand All @@ -11,11 +11,12 @@
#endif

namespace UnitTest {
namespace Detail {

class UNITTEST_LINKAGE Timer
class UNITTEST_LINKAGE TimerImplWin32
{
public:
Timer();
TimerImplWin32();
void Start();
double GetTimeInMs() const;

Expand All @@ -34,12 +35,8 @@ namespace UnitTest {
__int64 m_frequency;
};


namespace TimeHelpers
{
UNITTEST_LINKAGE void SleepMs(int ms);
}

UNITTEST_LINKAGE void SleepMsImplWin32(int ms);
}
}

#endif