Timer_test.cpp 4.0 KB

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