Remove central tracking of SystemTimeStamp
SystemTimeStamp used to be an opaque class, as it was provided by `system_{linux/other/windows}.h`. Because of this, code dealt in SystemTimeStamp pointers, and getting the current timestamp required a memory allocation. Now SystemTimeStamp is just a `std::chrono::time_point`, we can make the code cleaner and faster by just directly keeping SystemTimeStamp instead of pointers around.fix-squashed-planets
parent
285350464f
commit
4119e669d1
|
@ -135,14 +135,6 @@ CApplication::CApplication(CSystemUtils* systemUtils)
|
|||
m_absTime = 0.0f;
|
||||
m_relTime = 0.0f;
|
||||
|
||||
m_baseTimeStamp = m_systemUtils->CreateTimeStamp();
|
||||
m_curTimeStamp = m_systemUtils->CreateTimeStamp();
|
||||
m_lastTimeStamp = m_systemUtils->CreateTimeStamp();
|
||||
|
||||
m_manualFrameLast = m_systemUtils->CreateTimeStamp();
|
||||
m_manualFrameTime = m_systemUtils->CreateTimeStamp();
|
||||
|
||||
|
||||
m_joystickEnabled = false;
|
||||
|
||||
m_mouseMode = MOUSE_SYSTEM;
|
||||
|
@ -159,13 +151,6 @@ CApplication::CApplication(CSystemUtils* systemUtils)
|
|||
|
||||
CApplication::~CApplication()
|
||||
{
|
||||
m_systemUtils->DestroyTimeStamp(m_baseTimeStamp);
|
||||
m_systemUtils->DestroyTimeStamp(m_curTimeStamp);
|
||||
m_systemUtils->DestroyTimeStamp(m_lastTimeStamp);
|
||||
|
||||
m_systemUtils->DestroyTimeStamp(m_manualFrameLast);
|
||||
m_systemUtils->DestroyTimeStamp(m_manualFrameTime);
|
||||
|
||||
m_joystickEnabled = false;
|
||||
|
||||
m_controller.reset();
|
||||
|
@ -672,13 +657,11 @@ bool CApplication::Create()
|
|||
|
||||
std::thread{[this]() {
|
||||
GetLogger()->Debug("Cache sounds...\n");
|
||||
SystemTimeStamp* musicLoadStart = m_systemUtils->CreateTimeStamp();
|
||||
m_systemUtils->GetCurrentTimeStamp(musicLoadStart);
|
||||
SystemTimeStamp musicLoadStart = m_systemUtils->GetCurrentTimeStamp();
|
||||
|
||||
m_sound->CacheAll();
|
||||
|
||||
SystemTimeStamp* musicLoadEnd = m_systemUtils->CreateTimeStamp();
|
||||
m_systemUtils->GetCurrentTimeStamp(musicLoadEnd);
|
||||
SystemTimeStamp musicLoadEnd = m_systemUtils->GetCurrentTimeStamp();
|
||||
float musicLoadTime = m_systemUtils->TimeStampDiff(musicLoadStart, musicLoadEnd, STU_MSEC);
|
||||
GetLogger()->Debug("Sound loading took %.2f ms\n", musicLoadTime);
|
||||
}}.detach();
|
||||
|
@ -984,9 +967,9 @@ int CApplication::Run()
|
|||
{
|
||||
m_active = true;
|
||||
|
||||
m_systemUtils->GetCurrentTimeStamp(m_baseTimeStamp);
|
||||
m_systemUtils->GetCurrentTimeStamp(m_lastTimeStamp);
|
||||
m_systemUtils->GetCurrentTimeStamp(m_curTimeStamp);
|
||||
m_baseTimeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_lastTimeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_curTimeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
|
||||
MoveMouse(Math::Point(0.5f, 0.5f)); // center mouse on start
|
||||
|
||||
|
@ -1410,13 +1393,13 @@ void CApplication::Render()
|
|||
|
||||
void CApplication::RenderIfNeeded(int updateRate)
|
||||
{
|
||||
m_systemUtils->GetCurrentTimeStamp(m_manualFrameTime);
|
||||
m_manualFrameTime = m_systemUtils->GetCurrentTimeStamp();
|
||||
long long diff = m_systemUtils->TimeStampExactDiff(m_manualFrameLast, m_manualFrameTime);
|
||||
if (diff < 1e9f / updateRate)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_systemUtils->CopyTimeStamp(m_manualFrameLast, m_manualFrameTime);
|
||||
m_manualFrameLast = m_manualFrameTime;
|
||||
|
||||
Render();
|
||||
}
|
||||
|
@ -1444,8 +1427,8 @@ void CApplication::ResetTimeAfterLoading()
|
|||
|
||||
void CApplication::InternalResumeSimulation()
|
||||
{
|
||||
m_systemUtils->GetCurrentTimeStamp(m_baseTimeStamp);
|
||||
m_systemUtils->CopyTimeStamp(m_curTimeStamp, m_baseTimeStamp);
|
||||
m_baseTimeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_curTimeStamp = m_baseTimeStamp;
|
||||
m_realAbsTimeBase = m_realAbsTime;
|
||||
m_absTimeBase = m_exactAbsTime;
|
||||
}
|
||||
|
@ -1459,7 +1442,7 @@ void CApplication::SetSimulationSpeed(float speed)
|
|||
{
|
||||
m_simulationSpeed = speed;
|
||||
|
||||
m_systemUtils->GetCurrentTimeStamp(m_baseTimeStamp);
|
||||
m_baseTimeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_realAbsTimeBase = m_realAbsTime;
|
||||
m_absTimeBase = m_exactAbsTime;
|
||||
|
||||
|
@ -1471,8 +1454,8 @@ Event CApplication::CreateUpdateEvent()
|
|||
if (m_simulationSuspended)
|
||||
return Event(EVENT_NULL);
|
||||
|
||||
m_systemUtils->CopyTimeStamp(m_lastTimeStamp, m_curTimeStamp);
|
||||
m_systemUtils->GetCurrentTimeStamp(m_curTimeStamp);
|
||||
m_lastTimeStamp = m_curTimeStamp;
|
||||
m_curTimeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
|
||||
long long absDiff = m_systemUtils->TimeStampExactDiff(m_baseTimeStamp, m_curTimeStamp);
|
||||
long long newRealAbsTime = m_realAbsTimeBase + absDiff;
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "common/event.h"
|
||||
#include "common/language.h"
|
||||
#include "common/singleton.h"
|
||||
#include "common/system/system.h"
|
||||
|
||||
#include "graphics/core/device.h"
|
||||
|
||||
|
@ -45,7 +46,6 @@ class CInput;
|
|||
class CPathManager;
|
||||
class CConfigFile;
|
||||
class CSystemUtils;
|
||||
struct SystemTimeStamp;
|
||||
|
||||
namespace Gfx
|
||||
{
|
||||
|
@ -339,9 +339,9 @@ protected:
|
|||
|
||||
//! Animation time stamps, etc.
|
||||
//@{
|
||||
SystemTimeStamp* m_baseTimeStamp;
|
||||
SystemTimeStamp* m_lastTimeStamp;
|
||||
SystemTimeStamp* m_curTimeStamp;
|
||||
SystemTimeStamp m_baseTimeStamp;
|
||||
SystemTimeStamp m_lastTimeStamp;
|
||||
SystemTimeStamp m_curTimeStamp;
|
||||
|
||||
long long m_realAbsTimeBase;
|
||||
long long m_realAbsTime;
|
||||
|
@ -358,8 +358,8 @@ protected:
|
|||
bool m_simulationSuspended;
|
||||
//@}
|
||||
|
||||
SystemTimeStamp* m_manualFrameLast;
|
||||
SystemTimeStamp* m_manualFrameTime;
|
||||
SystemTimeStamp m_manualFrameLast;
|
||||
SystemTimeStamp m_manualFrameTime;
|
||||
|
||||
//! Graphics device to use
|
||||
bool m_graphicsOverride = false;
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
CSystemUtils* CProfiler::m_systemUtils = nullptr;
|
||||
long long CProfiler::m_performanceCounters[PCNT_MAX] = {0};
|
||||
long long CProfiler::m_prevPerformanceCounters[PCNT_MAX] = {0};
|
||||
std::stack<SystemTimeStamp*> CProfiler::m_runningPerformanceCounters;
|
||||
std::stack<SystemTimeStamp> CProfiler::m_runningPerformanceCounters;
|
||||
std::stack<PerformanceCounter> CProfiler::m_runningPerformanceCountersType;
|
||||
|
||||
void CProfiler::SetSystemUtils(CSystemUtils* systemUtils)
|
||||
|
@ -39,8 +39,7 @@ void CProfiler::StartPerformanceCounter(PerformanceCounter counter)
|
|||
if (counter == PCNT_ALL)
|
||||
ResetPerformanceCounters();
|
||||
|
||||
SystemTimeStamp* timeStamp = m_systemUtils->CreateTimeStamp();
|
||||
m_systemUtils->GetCurrentTimeStamp(timeStamp);
|
||||
SystemTimeStamp timeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_runningPerformanceCounters.push(timeStamp);
|
||||
m_runningPerformanceCountersType.push(counter);
|
||||
}
|
||||
|
@ -50,11 +49,8 @@ void CProfiler::StopPerformanceCounter(PerformanceCounter counter)
|
|||
assert(m_runningPerformanceCountersType.top() == counter);
|
||||
m_runningPerformanceCountersType.pop();
|
||||
|
||||
SystemTimeStamp* timeStamp = m_systemUtils->CreateTimeStamp();
|
||||
m_systemUtils->GetCurrentTimeStamp(timeStamp);
|
||||
SystemTimeStamp timeStamp = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_performanceCounters[counter] += m_systemUtils->TimeStampExactDiff(m_runningPerformanceCounters.top(), timeStamp);
|
||||
m_systemUtils->DestroyTimeStamp(timeStamp);
|
||||
m_systemUtils->DestroyTimeStamp(m_runningPerformanceCounters.top());
|
||||
m_runningPerformanceCounters.pop();
|
||||
|
||||
if (counter == PCNT_ALL)
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
#pragma once
|
||||
|
||||
class CSystemUtils;
|
||||
struct SystemTimeStamp;
|
||||
|
||||
#include "common/system/system.h"
|
||||
#include <stack>
|
||||
|
||||
/**
|
||||
|
@ -73,7 +73,7 @@ private:
|
|||
|
||||
static long long m_performanceCounters[PCNT_MAX];
|
||||
static long long m_prevPerformanceCounters[PCNT_MAX];
|
||||
static std::stack<SystemTimeStamp*> m_runningPerformanceCounters;
|
||||
static std::stack<SystemTimeStamp> m_runningPerformanceCounters;
|
||||
static std::stack<PerformanceCounter> m_runningPerformanceCountersType;
|
||||
};
|
||||
|
||||
|
|
|
@ -144,35 +144,17 @@ SystemDialogResult CSystemUtils::ConsoleSystemDialog(SystemDialogType type, cons
|
|||
return result;
|
||||
}
|
||||
|
||||
SystemTimeStamp* CSystemUtils::CreateTimeStamp()
|
||||
SystemTimeStamp CSystemUtils::GetCurrentTimeStamp()
|
||||
{
|
||||
auto timeStamp = MakeUnique<SystemTimeStamp>();
|
||||
SystemTimeStamp* timeStampPtr = timeStamp.get();
|
||||
m_timeStamps.push_back(std::move(timeStamp));
|
||||
return timeStampPtr;
|
||||
return std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void CSystemUtils::DestroyTimeStamp(SystemTimeStamp *stamp)
|
||||
long long CSystemUtils::TimeStampExactDiff(SystemTimeStamp before, SystemTimeStamp after)
|
||||
{
|
||||
m_timeStamps.erase(std::remove_if(m_timeStamps.begin(), m_timeStamps.end(), [&](const std::unique_ptr<SystemTimeStamp>& timeStamp) { return timeStamp.get() == stamp; }));
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(after - before).count();
|
||||
}
|
||||
|
||||
void CSystemUtils::CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src)
|
||||
{
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
void CSystemUtils::GetCurrentTimeStamp(SystemTimeStamp *stamp)
|
||||
{
|
||||
*stamp = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
long long CSystemUtils::TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after)
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(*after - *before).count();
|
||||
}
|
||||
|
||||
float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit)
|
||||
float CSystemUtils::TimeStampDiff(SystemTimeStamp before, SystemTimeStamp after, SystemTimeUnit unit)
|
||||
{
|
||||
long long exact = TimeStampExactDiff(before, after);
|
||||
|
||||
|
|
|
@ -101,25 +101,16 @@ public:
|
|||
//! Displays a fallback system dialog using console
|
||||
TEST_VIRTUAL SystemDialogResult ConsoleSystemDialog(SystemDialogType type, const std::string& title, const std::string& message);
|
||||
|
||||
//! Creates a new time stamp object
|
||||
TEST_VIRTUAL SystemTimeStamp* CreateTimeStamp();
|
||||
|
||||
//! Destroys a time stamp object
|
||||
TEST_VIRTUAL void DestroyTimeStamp(SystemTimeStamp *stamp);
|
||||
|
||||
//! Copies the time stamp from \a src to \a dst
|
||||
TEST_VIRTUAL void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src);
|
||||
|
||||
//! Returns a time stamp associated with current time
|
||||
TEST_VIRTUAL void GetCurrentTimeStamp(SystemTimeStamp *stamp);
|
||||
TEST_VIRTUAL SystemTimeStamp GetCurrentTimeStamp();
|
||||
|
||||
//! Returns a difference between two timestamps in given time unit
|
||||
/** The difference is \a after - \a before. */
|
||||
TEST_VIRTUAL float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit = STU_SEC);
|
||||
float TimeStampDiff(SystemTimeStamp before, SystemTimeStamp after, SystemTimeUnit unit = STU_SEC);
|
||||
|
||||
//! Returns the exact (in nanosecond units) difference between two timestamps
|
||||
/** The difference is \a after - \a before. */
|
||||
virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after);
|
||||
long long TimeStampExactDiff(SystemTimeStamp before, SystemTimeStamp after);
|
||||
|
||||
//! Returns the data path (containing textures, levels, helpfiles, etc)
|
||||
virtual std::string GetDataPath();
|
||||
|
@ -131,8 +122,5 @@ public:
|
|||
virtual std::string GetSaveDir();
|
||||
|
||||
//! Sleep for given amount of microseconds
|
||||
virtual void Usleep(int usecs);
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<SystemTimeStamp>> m_timeStamps;
|
||||
void Usleep(int usecs);
|
||||
};
|
||||
|
|
|
@ -216,8 +216,6 @@ CEngine::CEngine(CApplication *app, CSystemUtils* systemUtils)
|
|||
m_mouseType = ENG_MOUSE_NORM;
|
||||
|
||||
m_fpsCounter = 0;
|
||||
m_lastFrameTime = m_systemUtils->CreateTimeStamp();
|
||||
m_currentFrameTime = m_systemUtils->CreateTimeStamp();
|
||||
|
||||
m_shadowColor = 0.5f;
|
||||
|
||||
|
@ -241,10 +239,6 @@ CEngine::CEngine(CApplication *app, CSystemUtils* systemUtils)
|
|||
|
||||
CEngine::~CEngine()
|
||||
{
|
||||
m_systemUtils->DestroyTimeStamp(m_lastFrameTime);
|
||||
m_lastFrameTime = nullptr;
|
||||
m_systemUtils->DestroyTimeStamp(m_currentFrameTime);
|
||||
m_currentFrameTime = nullptr;
|
||||
}
|
||||
|
||||
void CEngine::SetDevice(CDevice *device)
|
||||
|
@ -361,8 +355,8 @@ bool CEngine::Create()
|
|||
params.mipmap = false;
|
||||
m_miceTexture = LoadTexture("textures/interface/mouse.png", params);
|
||||
|
||||
m_systemUtils->GetCurrentTimeStamp(m_currentFrameTime);
|
||||
m_systemUtils->GetCurrentTimeStamp(m_lastFrameTime);
|
||||
m_currentFrameTime = m_systemUtils->GetCurrentTimeStamp();
|
||||
m_lastFrameTime = m_systemUtils->GetCurrentTimeStamp();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -3176,11 +3170,11 @@ void CEngine::Render()
|
|||
{
|
||||
m_fpsCounter++;
|
||||
|
||||
m_systemUtils->GetCurrentTimeStamp(m_currentFrameTime);
|
||||
m_currentFrameTime = m_systemUtils->GetCurrentTimeStamp();
|
||||
float diff = m_systemUtils->TimeStampDiff(m_lastFrameTime, m_currentFrameTime, STU_SEC);
|
||||
if (diff > 1.0f)
|
||||
{
|
||||
m_systemUtils->CopyTimeStamp(m_lastFrameTime, m_currentFrameTime);
|
||||
m_lastFrameTime = m_currentFrameTime;
|
||||
|
||||
m_fps = m_fpsCounter / diff;
|
||||
m_fpsCounter = 0;
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "common/singleton.h"
|
||||
#include "common/system/system.h"
|
||||
|
||||
#include "graphics/core/color.h"
|
||||
#include "graphics/core/material.h"
|
||||
|
@ -50,7 +51,6 @@ class CApplication;
|
|||
class CSoundInterface;
|
||||
class CImage;
|
||||
class CSystemUtils;
|
||||
struct SystemTimeStamp;
|
||||
struct Event;
|
||||
|
||||
|
||||
|
@ -1303,8 +1303,8 @@ protected:
|
|||
//! Last encountered error
|
||||
std::string m_error;
|
||||
|
||||
SystemTimeStamp* m_lastFrameTime;
|
||||
SystemTimeStamp* m_currentFrameTime;
|
||||
SystemTimeStamp m_lastFrameTime;
|
||||
SystemTimeStamp m_currentFrameTime;
|
||||
int m_fpsCounter;
|
||||
float m_fps;
|
||||
|
||||
|
|
|
@ -1,10 +1,3 @@
|
|||
# Platform-dependent tests
|
||||
if(PLATFORM_WINDOWS)
|
||||
set(PLATFORM_TESTS common/system/system_windows_test.cpp)
|
||||
elseif(PLATFORM_LINUX)
|
||||
set(PLATFORM_TESTS common/system/system_linux_test.cpp)
|
||||
endif()
|
||||
|
||||
# Sources
|
||||
set(UT_SOURCES
|
||||
main.cpp
|
||||
|
@ -12,12 +5,12 @@ set(UT_SOURCES
|
|||
CBot/CBotToken_test.cpp
|
||||
CBot/CBot_test.cpp
|
||||
common/config_file_test.cpp
|
||||
common/system/system_test.cpp
|
||||
graphics/engine/lightman_test.cpp
|
||||
math/func_test.cpp
|
||||
math/geometry_test.cpp
|
||||
math/matrix_test.cpp
|
||||
math/vector_test.cpp
|
||||
${PLATFORM_TESTS}
|
||||
)
|
||||
|
||||
# Includes
|
||||
|
|
|
@ -32,14 +32,6 @@
|
|||
using namespace HippoMocks;
|
||||
namespace ph = std::placeholders;
|
||||
|
||||
struct FakeSystemTimeStamp : public SystemTimeStamp
|
||||
{
|
||||
FakeSystemTimeStamp(int uid) : uid(uid), time(0) {}
|
||||
|
||||
int uid;
|
||||
long long time;
|
||||
};
|
||||
|
||||
class CApplicationWrapper : public CApplication
|
||||
{
|
||||
public:
|
||||
|
@ -66,7 +58,6 @@ class CApplicationUT : public testing::Test
|
|||
protected:
|
||||
CApplicationUT() :
|
||||
m_systemUtils(nullptr),
|
||||
m_stampUid(0),
|
||||
m_currentTime(0)
|
||||
{}
|
||||
|
||||
|
@ -78,11 +69,7 @@ protected:
|
|||
|
||||
void NextInstant(long long diff);
|
||||
|
||||
SystemTimeStamp* CreateTimeStamp();
|
||||
void DestroyTimeStamp(SystemTimeStamp *stamp);
|
||||
void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src);
|
||||
void GetCurrentTimeStamp(SystemTimeStamp *stamp);
|
||||
long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after);
|
||||
SystemTimeStamp GetCurrentTimeStamp();
|
||||
|
||||
void TestCreateUpdateEvent(long long relTimeExact, long long absTimeExact,
|
||||
float relTime, float absTime,
|
||||
|
@ -92,10 +79,8 @@ protected:
|
|||
std::unique_ptr<CApplicationWrapper> m_app;
|
||||
MockRepository m_mocks;
|
||||
CSystemUtils* m_systemUtils;
|
||||
std::vector<std::unique_ptr<FakeSystemTimeStamp>> m_timeStamps;
|
||||
|
||||
private:
|
||||
int m_stampUid;
|
||||
long long m_currentTime;
|
||||
};
|
||||
|
||||
|
@ -107,11 +92,7 @@ void CApplicationUT::SetUp()
|
|||
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetLangPath).Return("");
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetSaveDir).Return("");
|
||||
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::CreateTimeStamp).Do(std::bind(&CApplicationUT::CreateTimeStamp, this));
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::DestroyTimeStamp).Do(std::bind(&CApplicationUT::DestroyTimeStamp, this, ph::_1));
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::CopyTimeStamp).Do(std::bind(&CApplicationUT::CopyTimeStamp, this, ph::_1, ph::_2));
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetCurrentTimeStamp).Do(std::bind(&CApplicationUT::GetCurrentTimeStamp, this, ph::_1));
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::TimeStampExactDiff).Do(std::bind(&CApplicationUT::TimeStampExactDiff, this, ph::_1, ph::_2));
|
||||
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetCurrentTimeStamp).Do(std::bind(&CApplicationUT::GetCurrentTimeStamp, this));
|
||||
|
||||
m_app = MakeUnique<CApplicationWrapper>(m_systemUtils);
|
||||
}
|
||||
|
@ -121,31 +102,10 @@ void CApplicationUT::TearDown()
|
|||
m_app.reset();
|
||||
}
|
||||
|
||||
SystemTimeStamp* CApplicationUT::CreateTimeStamp()
|
||||
{
|
||||
auto stamp = MakeUnique<FakeSystemTimeStamp>(++m_stampUid);
|
||||
auto stampPtr = stamp.get();
|
||||
m_timeStamps.push_back(std::move(stamp));
|
||||
return stampPtr;
|
||||
}
|
||||
|
||||
void CApplicationUT::DestroyTimeStamp(SystemTimeStamp *stamp)
|
||||
SystemTimeStamp CApplicationUT::GetCurrentTimeStamp()
|
||||
{
|
||||
}
|
||||
|
||||
void CApplicationUT::CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src)
|
||||
{
|
||||
*static_cast<FakeSystemTimeStamp*>(dst) = *static_cast<FakeSystemTimeStamp*>(src);
|
||||
}
|
||||
|
||||
void CApplicationUT::GetCurrentTimeStamp(SystemTimeStamp *stamp)
|
||||
{
|
||||
static_cast<FakeSystemTimeStamp*>(stamp)->time = m_currentTime;
|
||||
}
|
||||
|
||||
long long CApplicationUT::TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after)
|
||||
{
|
||||
return static_cast<FakeSystemTimeStamp*>(after)->time - static_cast<FakeSystemTimeStamp*>(before)->time;
|
||||
return SystemTimeStamp{SystemTimeStamp::duration{m_currentTime}};
|
||||
}
|
||||
|
||||
void CApplicationUT::NextInstant(long long diff)
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
/*
|
||||
* This file is part of the Colobot: Gold Edition source code
|
||||
* Copyright (C) 2001-2018, Daniel Roux, EPSITEC SA & TerranovaTeam
|
||||
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see http://gnu.org/licenses
|
||||
*/
|
||||
|
||||
#include "common/system/system.h"
|
||||
#include "common/system/system_linux.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class CSystemUtilsLinuxUT : public testing::Test
|
||||
{
|
||||
protected:
|
||||
static const long long SEC = 1000000000;
|
||||
|
||||
CSystemUtilsLinux m_systemUtils;
|
||||
};
|
||||
|
||||
|
||||
TEST_F(CSystemUtilsLinuxUT, TimeStampDiff)
|
||||
{
|
||||
SystemTimeStamp before, after;
|
||||
|
||||
before.clockTime.tv_sec = 1;
|
||||
before.clockTime.tv_nsec = 100;
|
||||
|
||||
after.clockTime.tv_sec = 1;
|
||||
after.clockTime.tv_nsec = 900;
|
||||
|
||||
long long tDiff = m_systemUtils.TimeStampExactDiff(&before, &after);
|
||||
EXPECT_EQ( 800, tDiff);
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&after, &before);
|
||||
EXPECT_EQ(-800, tDiff);
|
||||
|
||||
// -------
|
||||
|
||||
before.clockTime.tv_sec = 2;
|
||||
before.clockTime.tv_nsec = 200;
|
||||
|
||||
after.clockTime.tv_sec = 3;
|
||||
after.clockTime.tv_nsec = 500;
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&before, &after);
|
||||
EXPECT_EQ( SEC + 300, tDiff);
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&after, &before);
|
||||
EXPECT_EQ(-SEC - 300, tDiff);
|
||||
|
||||
// -------
|
||||
|
||||
before.clockTime.tv_sec = 3;
|
||||
before.clockTime.tv_nsec = 200;
|
||||
|
||||
after.clockTime.tv_sec = 4;
|
||||
after.clockTime.tv_nsec = 100;
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&before, &after);
|
||||
EXPECT_EQ( SEC - 100, tDiff);
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&after, &before);
|
||||
EXPECT_EQ(-SEC + 100, tDiff);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* This file is part of the Colobot: Gold Edition source code
|
||||
* Copyright (C) 2018, Daniel Roux, EPSITEC SA & TerranovaTeam
|
||||
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see http://gnu.org/licenses
|
||||
*/
|
||||
|
||||
#include "common/system/system_other.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
struct SystemTest : ::testing::Test {
|
||||
CSystemUtilsOther system;
|
||||
};
|
||||
|
||||
TEST_F(SystemTest, TimeStampExactDiff) {
|
||||
auto epoch = SystemTimeStamp{};
|
||||
EXPECT_EQ(system.TimeStampExactDiff(epoch, epoch), 0);
|
||||
|
||||
auto duration = std::chrono::microseconds{123456789L};
|
||||
auto before = std::chrono::high_resolution_clock::now();
|
||||
auto after = before + duration;
|
||||
EXPECT_EQ(system.TimeStampExactDiff(before, after), std::chrono::nanoseconds{duration}.count());
|
||||
EXPECT_EQ(system.TimeStampExactDiff(after, before), -std::chrono::nanoseconds{duration}.count());
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
/*
|
||||
* This file is part of the Colobot: Gold Edition source code
|
||||
* Copyright (C) 2001-2018, Daniel Roux, EPSITEC SA & TerranovaTeam
|
||||
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see http://gnu.org/licenses
|
||||
*/
|
||||
|
||||
#include "common/system/system.h"
|
||||
#include "common/system/system_windows.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class CSystemUtilsWindowsWrapper : public CSystemUtilsWindows
|
||||
{
|
||||
public:
|
||||
void SetFrequency(long long frequency)
|
||||
{
|
||||
m_counterFrequency = frequency;
|
||||
}
|
||||
};
|
||||
|
||||
class CSystemUtilsWindowsUT : public testing::Test
|
||||
{
|
||||
protected:
|
||||
static const long long SEC = 1000000000;
|
||||
|
||||
CSystemUtilsWindowsWrapper m_systemUtils;
|
||||
};
|
||||
|
||||
TEST_F(CSystemUtilsWindowsUT, TimeStampDiff)
|
||||
{
|
||||
m_systemUtils.SetFrequency(SEC);
|
||||
|
||||
SystemTimeStamp before, after;
|
||||
|
||||
before.counterValue = 100;
|
||||
after.counterValue = 200;
|
||||
|
||||
long long tDiff = m_systemUtils.TimeStampExactDiff(&before, &after);
|
||||
EXPECT_EQ( 100, tDiff);
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&after, &before);
|
||||
EXPECT_EQ(-100, tDiff);
|
||||
|
||||
// -------
|
||||
|
||||
m_systemUtils.SetFrequency(SEC/3);
|
||||
|
||||
before.counterValue = 200;
|
||||
after.counterValue = 400;
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&before, &after);
|
||||
EXPECT_EQ( 200*3, tDiff);
|
||||
|
||||
tDiff = m_systemUtils.TimeStampExactDiff(&after, &before);
|
||||
EXPECT_EQ(-200*3, tDiff);
|
||||
}
|
Loading…
Reference in New Issue