unit-CallEvery.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <catch2/catch.hpp>
  2. #include <iostream>
  3. #include <gul/CallEvery.h>
  4. #include <thread>
  5. using namespace gul;
  6. SCENARIO("Execute without sleeping")
  7. {
  8. CallEvery C1( std::chrono::milliseconds(20));
  9. size_t count = 0;
  10. size_t iterations = 0;
  11. while(count < 10)
  12. {
  13. auto execTime = C1([&](auto dt)
  14. {
  15. ++count;
  16. auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dt);
  17. REQUIRE(ms.count() == 20); // called at regular intervals
  18. });
  19. (void)execTime;
  20. iterations++;
  21. }
  22. // should have looped more than count times
  23. REQUIRE(iterations > count);
  24. // total number of loops should be quite large
  25. // since we are not sleeping at all
  26. REQUIRE(iterations > 1000);
  27. }
  28. SCENARIO("Execute without sleeping, no-lambda")
  29. {
  30. CallEvery C1( std::chrono::milliseconds(20));
  31. size_t count = 0;
  32. size_t iterations = 0;
  33. auto t0 = CallEvery::_clock::now();
  34. while(count < 10)
  35. {
  36. if(C1())
  37. {
  38. auto t1 = CallEvery::_clock::now();
  39. auto dt = t1-t0;
  40. t0 = t1;
  41. ++count;
  42. auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dt);
  43. REQUIRE(ms.count() >= 19); // called at regular intervals
  44. };
  45. iterations++;
  46. }
  47. // should have looped more than count times
  48. REQUIRE(iterations > count);
  49. // total number of loops should be quite large
  50. // since we are not sleeping at all
  51. REQUIRE(iterations > 1000);
  52. }
  53. SCENARIO("Execute with sleep")
  54. {
  55. CallEvery C1( std::chrono::milliseconds(20));
  56. size_t count = 0;
  57. size_t iterations = 0;
  58. while(count < 10)
  59. {
  60. auto execTime = C1([&](auto dt)
  61. {
  62. ++count;
  63. auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dt);
  64. REQUIRE(ms.count() == 20); // called at regular intervals
  65. });
  66. auto timeToSleep = C1.getInterval()-execTime;
  67. std::this_thread::sleep_for(timeToSleep);
  68. iterations++;
  69. }
  70. // should iterate at least count times
  71. REQUIRE(iterations >= count);
  72. // but shouldn't have 1000s of iterations
  73. // since we are sleeping
  74. REQUIRE(iterations <= 20);
  75. }
  76. SCENARIO("Execute with computation time longer than interval")
  77. {
  78. CallEvery C1;
  79. size_t count = 0;
  80. bool isRun=false;
  81. while(count < 10)
  82. {
  83. auto execTime = C1([&](auto dt)
  84. {
  85. isRun = true;
  86. auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dt);
  87. // make the exec time at least 2x as long as the interval
  88. std::this_thread::sleep_for(C1.getInterval()*2);
  89. if(count == 0)
  90. {
  91. CHECK( ms.count() == 20);
  92. }
  93. else
  94. {
  95. CHECK( ms.count() == 40 );
  96. }
  97. ++count;
  98. });
  99. auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(execTime);
  100. if(isRun) // during iterations when the funciton is run
  101. {
  102. REQUIRE( execTime > C1.getInterval());
  103. REQUIRE( ms.count() == 40 );
  104. }
  105. else // when the funciton isn't run, the exec time should be 0
  106. {
  107. REQUIRE( ms.count() == 0 );
  108. }
  109. isRun = false;
  110. }
  111. }