Add Tests

This commit is contained in:
2020-06-10 10:16:28 +02:00
parent e7d247ca42
commit 709df81110
9 changed files with 335 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
../Makefile
+69
View File
@@ -0,0 +1,69 @@
#include "Test.h"
#include "Metronome.h"
#include <Logging.h>
#include <chrono>
#include <ctime>
#include <sstream>
#include <string>
#include <unistd.h>
namespace Timer {
namespace Test {
int g_callbacks = 0;
void Callback()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
char time[10];
if (0 < strftime(time, sizeof(time), "%X", std::localtime(&in_time_t)))
ss << time << " ";
ss << "Callback Triggered" << std::endl;
++g_callbacks;
Logging::Log(Logging::Severity::Info, ss.str());
}
bool TestMetronome()
{
std::stringstream ss;
ss << "----------------------------------" << std::endl;
ss << "| TestMetronome |" << std::endl;
ss << "----------------------------------" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
auto callback = std::bind(&Callback);
int iterations = 3;
int waitTime = 2;
Metronome metronome(Metronome::Quantity::Seconds, waitTime);
metronome.Connect(callback);
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
ss.str("");
char time[10];
if (0 < strftime(time, sizeof(time), "%X", std::localtime(&in_time_t)))
ss << time << " ";
ss << "Start Metronome" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
sleep((iterations * waitTime) + 1);
ss.str("");
ss << "==================================" << std::endl << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
return (g_callbacks == iterations);
}
} // namespace Test
} // namespace Timer
+26
View File
@@ -0,0 +1,26 @@
#include "Test.h"
#include <Logging.h>
int main(int /*argc*/, char** /*argv*/)
{
Logging::OpenLog();
Logging::SetLogMask(Logging::Severity::Info);
if (
Timer::Test::TestMetronome() &&
Timer::Test::TestTimer() &&
Timer::Test::TestTrigger()
)
{
Logging::Log(Logging::Severity::Info, "Pass");
Logging::CloseLog();
return 0;
}
Logging::Log(Logging::Severity::Info, "Error");
Logging::CloseLog();
return -1;
}
+9
View File
@@ -0,0 +1,9 @@
namespace Timer {
namespace Test {
bool TestMetronome();
bool TestTimer();
bool TestTrigger();
} // namespace Test
} // namespace Timer
+140
View File
@@ -0,0 +1,140 @@
#include "Test.h"
#include "Timer.h"
#include <Logging.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <deque>
#include <memory>
#include <sstream>
#include <string>
namespace Timer {
namespace Test {
void print()
{
Logging::Log(Logging::Severity::Info, "Print");
}
void TestTimerAbort()
{
Timer timer;
std::function<void()> f_print = print;
std::stringstream ss;
ss << "---" << std::endl;
auto start = std::chrono::steady_clock::now();
std::clock_t begin = std::clock();
timer.StartContinuous(1000, f_print);
timer.Stop();
timer.Wait();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
ss << "Elapsed: " << elapsed.count() << std::endl;
ss << "---" << std::endl << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
ss << "---" << std::endl;
start = std::chrono::steady_clock::now();
timer.StartContinuous(3000, f_print);
timer.Abort();
timer.Wait();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
ss << "Elapsed: " << elapsed.count() << std::endl;
ss << "---" << std::endl << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
ss << "---" << std::endl;
start = std::chrono::steady_clock::now();
timer.StartContinuous(3000, f_print);
sleep(1);
timer.Abort();
timer.Wait();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
ss << "Elapsed: " << elapsed.count() << std::endl;
ss << "---" << std::endl << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
}
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
void OnTimerEnd(const std::string& type, int id)
{
std::stringstream ss;
ss << "OnTimerEnd() " << type << " " << id << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
}
void StartNewTimer(std::deque<TimerPointer>& timers)
{
int delay(2000);
std::string identifier("Blabla");
int id(10);
// Protect!
auto timer = make_unique<Timer>();
timer->StartSingle(delay, static_cast<std::function<void(const std::string&, int)>>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id);
timers.push_back(std::move(timer));
}
void Cleanup(std::deque<TimerPointer>& timers)
{
timers.erase(std::remove_if(timers.begin(), timers.end(), [](TimerPointer& t){return !t->IsRunning();}), timers.end());
}
void TestTimerDeque()
{
std::stringstream ss;
std::deque<TimerPointer> timers;
StartNewTimer(timers);
ss << "Count: " << timers.size() << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
StartNewTimer(timers);
ss << "Count: " << timers.size() << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
sleep(5);
ss.str("");
ss << "Count: " << timers.size() << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
Cleanup(timers);
ss << "Count: " << timers.size() << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
}
bool TestTimer()
{
std::stringstream ss;
ss << "----------------------------------" << std::endl;
ss << "| TestTimer |" << std::endl;
ss << "----------------------------------" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
TestTimerAbort();
TestTimerDeque();
ss.str("");
ss << "==================================" << std::endl << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
return true;
}
} // namespace Test
} // namespace Timer
/*
*/
+75
View File
@@ -0,0 +1,75 @@
#include "Test.h"
#include "Trigger.h"
#include <Logging.h>
#include <sstream>
#include <string>
namespace Timer {
namespace Test {
std::string print(time_t t)
{
std::tm * ptm = std::localtime(&t);
char buffer[32];
std::strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", ptm);
return buffer;
}
bool TestTrigger()
{
std::stringstream ss;
ss << "----------------------------------" << std::endl;
ss << "| TestTrigger |" << std::endl;
ss << "----------------------------------" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
auto second = std::chrono::system_clock::to_time_t(Trigger::Second());
ss << "Second: " << print(second) << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
auto minute = std::chrono::system_clock::to_time_t(Trigger::Minute());
ss << "Minute: " << print(minute) << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
auto hour = std::chrono::system_clock::to_time_t(Trigger::Hour());
ss << "Hour: " << print(hour) << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
auto day = std::chrono::system_clock::to_time_t(Trigger::Day());
ss << "Day: " << print(day) << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
/*
ss.str("");
auto target = std::chrono::system_clock::now() - std::chrono::hours(9);
auto test = std::chrono::system_clock::to_time_t(Trigger::Day(target));
ss << "Test: " << print(test) << " ( " << print(std::chrono::system_clock::to_time_t(target)) << " )" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
target = std::chrono::system_clock::now() - std::chrono::hours(8);
test = std::chrono::system_clock::to_time_t(Trigger::Day(target));
ss << "Test: " << print(test) << " ( " << print(std::chrono::system_clock::to_time_t(target)) << " )" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
ss.str("");
target = std::chrono::system_clock::now() - std::chrono::hours(10);
test = std::chrono::system_clock::to_time_t(Trigger::Day(target));
ss << "Test: " << print(test) << " ( " << print(std::chrono::system_clock::to_time_t(target)) << " )" << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
*/
ss.str("");
ss << "==================================" << std::endl << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
return true;
}
} // namespace Test
} // namespace Timer