Timer_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file
  3. * @author Marek Bel
  4. */
  5. #include "catch.hpp"
  6. #include "../Firmware/Timer.h"
  7. #include "../Firmware/TimerRemaining.h"
  8. static unsigned long now = 0;
  9. unsigned long millis()
  10. {
  11. return now;
  12. }
  13. unsigned long millis2()
  14. {
  15. return now;
  16. }
  17. static void basicTimer()
  18. {
  19. LongTimer timer;
  20. CHECK( timer.running() == false);
  21. timer.start();
  22. CHECK( timer.running() == true);
  23. timer.stop();
  24. CHECK( timer.running() == false);
  25. timer.start();
  26. CHECK( timer.expired(0) == true);
  27. CHECK( timer.expired(0) == false);
  28. CHECK( timer.running() == false);
  29. timer.start();
  30. CHECK( timer.expired(1) == false);
  31. CHECK( timer.running() == true);
  32. ++now;
  33. CHECK( timer.expired(1) == true);
  34. CHECK( timer.running() == false);
  35. --now;
  36. timer.start();
  37. CHECK( timer.expired(ULONG_MAX - 1) == false);
  38. now+= ULONG_MAX - 2;
  39. CHECK( timer.expired(ULONG_MAX - 1) == false);
  40. now++;
  41. CHECK( timer.expired(ULONG_MAX - 1) == true);
  42. CHECK( timer.running() == false);
  43. now-= ULONG_MAX - 1;
  44. timer.start();
  45. CHECK( timer.expired(ULONG_MAX) == false);
  46. now+= ULONG_MAX - 1;
  47. CHECK( timer.expired(ULONG_MAX) == false);
  48. now++;
  49. CHECK( timer.expired(ULONG_MAX) == true);
  50. CHECK( timer.running() == false);
  51. now-= ULONG_MAX;
  52. timer.start();
  53. CHECK( timer.running() == true);
  54. CHECK( timer.expired(12*3600000ul + 38*60000ul + 15000) == false);
  55. now+= 12*3600000ul + 38*60000ul + 14999;
  56. CHECK( timer.expired(12*3600000ul + 38*60000ul + 15000) == false);
  57. ++now;
  58. CHECK( timer.expired(12*3600000ul + 38*60000ul + 15000) == true);
  59. CHECK( timer.running() == false);
  60. now-= 12*3600000ul + 38*60000ul + 15000;
  61. }
  62. TEST_CASE( "LongTimer test.", "[timer]" )
  63. {
  64. now = 0;
  65. basicTimer();
  66. now = 1;
  67. basicTimer();
  68. now = ULONG_MAX;
  69. basicTimer();
  70. now = ULONG_MAX - 1;
  71. basicTimer();
  72. now = 12*3600000ul + 38*60000ul + 15000;
  73. basicTimer();
  74. }
  75. static void basicRemaining()
  76. {
  77. TimerRemaining timer;
  78. CHECK( timer.running() == false);
  79. CHECK( timer.remaining() == 0);
  80. timer.start(100);
  81. CHECK( timer.running() == true);
  82. CHECK( timer.remaining() == 100);
  83. now += 99;
  84. CHECK( timer.running() == true);
  85. CHECK( timer.remaining() == 1);
  86. ++now;
  87. CHECK( timer.running() == true);
  88. CHECK( timer.remaining() == 0);
  89. CHECK( timer.running() == false);
  90. now -=100;
  91. timer.start(1);
  92. timer.stop();
  93. CHECK( timer.remaining() == 0);
  94. CHECK( timer.running() == false);
  95. timer.start(0);
  96. CHECK( timer.remaining() == 0);
  97. CHECK( timer.running() == false);
  98. timer.start(1);
  99. CHECK( timer.remaining() == 1);
  100. CHECK( timer.running() == true);
  101. ++now;
  102. CHECK( timer.running() == true);
  103. CHECK( timer.remaining() == 0);
  104. CHECK( timer.running() == false);
  105. --now;
  106. timer.start(ULONG_MAX - 1);
  107. CHECK( timer.running() == true);
  108. CHECK( timer.remaining() == ULONG_MAX - 1);
  109. now+= ULONG_MAX - 2;
  110. CHECK( timer.running() == true);
  111. CHECK( timer.remaining() == 1);
  112. CHECK( timer.running() == true);
  113. ++now;
  114. CHECK( timer.remaining() == 0);
  115. CHECK( timer.running() == false);
  116. now-= ULONG_MAX - 1;
  117. timer.start(ULONG_MAX);
  118. CHECK( timer.running() == true);
  119. CHECK( timer.remaining() == ULONG_MAX);
  120. now+= ULONG_MAX - 1;
  121. CHECK( timer.remaining() == 1);
  122. CHECK( timer.running() == true);
  123. ++now;
  124. CHECK( timer.remaining() == 0);
  125. CHECK( timer.running() == false);
  126. timer.start(12*3600000ul + 38*60000ul + 15000);
  127. CHECK( timer.running() == true);
  128. CHECK( timer.remaining() == 12*3600000ul + 38*60000ul + 15000);
  129. now+= 12*3600000ul + 38*60000ul + 14999;
  130. CHECK( timer.remaining() == 1);
  131. ++now;
  132. CHECK( timer.remaining() == 0);
  133. CHECK( timer.running() == false);
  134. now-= 12*3600000ul + 38*60000ul + 15000;
  135. }
  136. TEST_CASE( "TimerRemaining test.", "[timer]" )
  137. {
  138. now = 0;
  139. basicRemaining();
  140. now = 1;
  141. basicRemaining();
  142. now = ULONG_MAX;
  143. basicRemaining();
  144. now = ULONG_MAX - 1;
  145. basicRemaining();
  146. now = 12*3600000ul + 38*60000ul + 15000;
  147. basicRemaining();
  148. }