Test.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. #include <algorithm>
  5. #include <unistd.h>
  6. #include <chrono>
  7. #include <deque>
  8. #include "Util/Timer.h"
  9. #include "Util/INIh.h"
  10. void print()
  11. {
  12. std::cout << "Print" << std::endl;
  13. }
  14. void TestTimerAbort()
  15. {
  16. PresenceDetection::Util::Timer timer;
  17. std::function<void()> f_print = print;
  18. std::cout << "---" << std::endl;
  19. auto start = std::chrono::steady_clock::now();
  20. std::clock_t begin = std::clock();
  21. timer.StartContinuous(1000, f_print);
  22. timer.Stop();
  23. timer.Wait();
  24. auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
  25. std::cout << "Elapsed: " << elapsed.count() << std::endl;
  26. std::cout << "---" << std::endl << std::endl;
  27. std::cout << "---" << std::endl;
  28. start = std::chrono::steady_clock::now();
  29. timer.StartContinuous(3000, f_print);
  30. timer.Abort();
  31. timer.Wait();
  32. elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
  33. std::cout << "Elapsed: " << elapsed.count() << std::endl;
  34. std::cout << "---" << std::endl << std::endl;
  35. std::cout << "---" << std::endl;
  36. start = std::chrono::steady_clock::now();
  37. timer.StartContinuous(3000, f_print);
  38. sleep(1);
  39. timer.Abort();
  40. timer.Wait();
  41. elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
  42. std::cout << "Elapsed: " << elapsed.count() << std::endl;
  43. std::cout << "---" << std::endl << std::endl;
  44. }
  45. typedef std::unique_ptr<PresenceDetection::Util::Timer> Pointer;
  46. template<typename T, typename... Args>
  47. std::unique_ptr<T> make_unique(Args&&... args) {
  48. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  49. }
  50. void OnTimerEnd(const std::string& type, int id)
  51. {
  52. std::cout << "OnTimerEnd() " << type << " " << id <<std::endl;
  53. }
  54. void StartNewTimer(std::deque<Pointer>& timers)
  55. {
  56. int delay(2000);
  57. std::string identifier("Blabla");
  58. int id(10);
  59. // Protect!
  60. auto timer = make_unique<PresenceDetection::Util::Timer>();
  61. timer->StartSingle(delay, static_cast<std::function<void(const std::string&, int)>>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id);
  62. timers.push_back(std::move(timer));
  63. }
  64. void Cleanup(std::deque<Pointer>& timers)
  65. {
  66. timers.erase(std::remove_if(timers.begin(), timers.end(), [](Pointer& t){return !t->IsRunning();}), timers.end());
  67. }
  68. void TestTimerDeque()
  69. {
  70. std::deque<Pointer> timers;
  71. StartNewTimer(timers);
  72. std::cout << "Count: " << timers.size() << std::endl;
  73. StartNewTimer(timers);
  74. std::cout << "Count: " << timers.size() << std::endl;
  75. sleep(5);
  76. std::cout << "Count: " << timers.size() << std::endl;
  77. Cleanup(timers);
  78. std::cout << "Count: " << timers.size() << std::endl;
  79. }
  80. void TestINIReader()
  81. {
  82. PresenceDetection::Util::INIReader reader("PresenceDetection.ini");
  83. std::string target = reader.Get("PresenceDetection", "Target", "");
  84. if (!target.empty())
  85. std::cout << "Error" << std::endl;
  86. std::cout << target.size() << " " << target << std::endl;
  87. bool unifi = reader.GetBoolean("PresenceDetection", "UniFi", false);
  88. if (unifi)
  89. std::cout << "UniFi On" << std::endl;
  90. else
  91. std::cout << "UniFi Off" << std::endl;
  92. bool test = reader.GetBoolean("PresenceDetection", "Test", false);
  93. if (test)
  94. std::cout << "Test On" << std::endl;
  95. else
  96. std::cout << "Test Off" << std::endl;
  97. }
  98. int main(int /*argc*/, char** /*argv[]*/)
  99. {
  100. TestINIReader();
  101. return 0;
  102. }