diff --git a/.gitignore b/.gitignore index 851ef09..0b9ff8b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .*.swp .AppleDouble lib +test* +!test.cc diff --git a/Makefile.conf b/Makefile.conf index 7dcd865..bee10f6 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -2,6 +2,10 @@ # Makefile.conf # +LFLAGS += -lLogging +LFLAGS += -L$(ROOTPATH)/Libraries/Logging/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Logging/include + LFLAGS += -lUtilities LFLAGS += -L$(ROOTPATH)/Libraries/Utilities/lib/$(ARCH) CFLAGS += -I$(ROOTPATH)/Libraries/Utilities/include @@ -17,4 +21,7 @@ LFLAGS += -lTimer LFLAGS += -L$(ROOTPATH)/lib/$(ARCH) CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include +LFLAGS += -lcurl +LFLAGS += -pthread + DEBUGDIR := .debug diff --git a/Makefile.target b/Makefile.target index d7e8967..57b08cb 100644 --- a/Makefile.target +++ b/Makefile.target @@ -8,6 +8,11 @@ Timer.a.$(ARCH) : $(OBJECTS) Timer.a: $(call build_target,$@) +test.$(ARCH): $(OBJECTS) Test/Test.o.$(ARCH) | Timer.a.$(ARCH) + $(call build_target_arch,$@,$^) +test: + $(call build_target,$@) + .DEFAULT_GOAL := Timer.a -TARGETS += Timer.a +TARGETS += Timer.a test diff --git a/Test/Makefile b/Test/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/Test/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/Test/Metronome.cpp b/Test/Metronome.cpp new file mode 100644 index 0000000..92bf88e --- /dev/null +++ b/Test/Metronome.cpp @@ -0,0 +1,69 @@ +#include "Test.h" +#include "Metronome.h" +#include +#include +#include +#include +#include +#include + + +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 diff --git a/Test/Test.cc b/Test/Test.cc new file mode 100644 index 0000000..ad11346 --- /dev/null +++ b/Test/Test.cc @@ -0,0 +1,26 @@ +#include "Test.h" +#include + + +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; +} diff --git a/Test/Test.h b/Test/Test.h new file mode 100644 index 0000000..ef10987 --- /dev/null +++ b/Test/Test.h @@ -0,0 +1,9 @@ +namespace Timer { +namespace Test { + +bool TestMetronome(); +bool TestTimer(); +bool TestTrigger(); + +} // namespace Test +} // namespace Timer diff --git a/Test/Timer.cpp b/Test/Timer.cpp new file mode 100644 index 0000000..94aec4b --- /dev/null +++ b/Test/Timer.cpp @@ -0,0 +1,140 @@ +#include "Test.h" +#include "Timer.h" +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace Timer { +namespace Test { + +void print() +{ + Logging::Log(Logging::Severity::Info, "Print"); +} + +void TestTimerAbort() +{ + Timer timer; + std::function 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::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::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::steady_clock::now() - start); + ss << "Elapsed: " << elapsed.count() << std::endl; + ss << "---" << std::endl << std::endl; + Logging::Log(Logging::Severity::Info, ss.str()); +} + +template +std::unique_ptr make_unique(Args&&... args) { + return std::unique_ptr(new T(std::forward(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& timers) +{ + int delay(2000); + std::string identifier("Blabla"); + int id(10); + + // Protect! + auto timer = make_unique(); + timer->StartSingle(delay, static_cast>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id); + timers.push_back(std::move(timer)); +} + +void Cleanup(std::deque& timers) +{ + timers.erase(std::remove_if(timers.begin(), timers.end(), [](TimerPointer& t){return !t->IsRunning();}), timers.end()); +} + +void TestTimerDeque() +{ + std::stringstream ss; + std::deque 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 + +/* + + +*/ diff --git a/Test/Trigger.cpp b/Test/Trigger.cpp new file mode 100644 index 0000000..8bd435d --- /dev/null +++ b/Test/Trigger.cpp @@ -0,0 +1,75 @@ +#include "Test.h" +#include "Trigger.h" +#include +#include +#include + + +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