mirror of
https://github.com/unittest-cpp/unittest-cpp
synced 2026-09-26 16:19:30 +03:00
Add "default" empty implementation for Timer and SleepMs()
This commit is contained in:
parent
bc5d87f484
commit
452a6c7181
8 changed files with 123 additions and 32 deletions
|
|
@ -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)
|
||||
|
|
|
|||
25
UnitTest++/Default/TimeHelpers.cpp
Normal file
25
UnitTest++/Default/TimeHelpers.cpp
Normal file
|
|
@ -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
UnitTest++/Default/TimeHelpers.h
Normal file
25
UnitTest++/Default/TimeHelpers.h
Normal file
|
|
@ -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
|
||||
|
|
@ -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(¤tTime, 0);
|
||||
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
#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;
|
||||
|
||||
|
|
@ -16,13 +17,8 @@ namespace UnitTest {
|
|||
struct timeval m_startTime;
|
||||
};
|
||||
|
||||
|
||||
namespace TimeHelpers
|
||||
{
|
||||
void SleepMs(int ms);
|
||||
}
|
||||
|
||||
|
||||
void SleepMsImplPosix(int ms);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -4,8 +4,9 @@
|
|||
#include <windows.h>
|
||||
|
||||
namespace UnitTest {
|
||||
namespace Detail {
|
||||
|
||||
Timer::Timer()
|
||||
TimerImplWin32::TimerImplWin32()
|
||||
: m_threadHandle(::GetCurrentThread())
|
||||
, m_startTime(0)
|
||||
{
|
||||
|
|
@ -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);
|
||||
|
|
@ -41,9 +42,10 @@ namespace UnitTest {
|
|||
return curTime.QuadPart;
|
||||
}
|
||||
|
||||
void TimeHelpers::SleepMs(int ms)
|
||||
void SleepMsImplWin32(int ms)
|
||||
{
|
||||
::Sleep(ms);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -34,12 +35,8 @@ namespace UnitTest {
|
|||
__int64 m_frequency;
|
||||
};
|
||||
|
||||
|
||||
namespace TimeHelpers
|
||||
{
|
||||
UNITTEST_LINKAGE void SleepMs(int ms);
|
||||
}
|
||||
|
||||
UNITTEST_LINKAGE void SleepMsImplWin32(int ms);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue