PageRenderTime 65ms CodeModel.GetById 16ms app.highlight 44ms RepoModel.GetById 2ms app.codeStats 0ms

/firmware/tests/T0-Utilities/src/T0.2.TimeoutTest.cc

http://github.com/makerbot/G3Firmware
C++ | 172 lines | 147 code | 19 blank | 6 comment | 10 complexity | c0d458281562233d523d282d6270fc32 MD5 | raw file
  1#define __STDC_LIMIT_MACROS
  2#include <gtest/gtest.h>
  3#include <stdlib.h>
  4#include <stdint.h>
  5
  6using namespace std;
  7
  8/// This doesn't test compiled code atm; instead it's an algorithmic
  9/// testbed to make sure our timer overflow works correctly in all instances.
 10
 11typedef uint32_t micros_t;
 12
 13micros_t current_time;
 14micros_t getCurrentMicros() { return current_time; }
 15
 16class Timeout {
 17private:
 18  bool active;
 19  bool elapsed;
 20  micros_t start_stamp_micros;
 21  micros_t duration_micros;
 22public:
 23  Timeout();
 24  void start(micros_t duration_micros);
 25  bool hasElapsed();
 26  bool isActive() const { return active; }
 27  void abort();
 28};
 29
 30Timeout::Timeout() : active(false), elapsed(false) {}
 31
 32void Timeout::start(micros_t duration_micros_in) {
 33	active = true;
 34	elapsed = false;
 35	start_stamp_micros = getCurrentMicros();
 36	duration_micros = duration_micros_in;
 37}
 38
 39bool Timeout::hasElapsed() {
 40  if (active && !elapsed) {
 41    micros_t delta = getCurrentMicros() - start_stamp_micros;
 42    if (delta >= duration_micros) {
 43      active = false;
 44      elapsed = true;
 45    }
 46  }
 47  return elapsed;
 48}
 49
 50void Timeout::abort() {
 51	active = false;
 52}
 53
 54void runTest(micros_t start_time, 
 55	     uint32_t duration, 
 56	     micros_t simulated_time,
 57	     bool expected) {
 58  current_time = start_time;
 59  Timeout t;
 60  t.start(duration);
 61  current_time = simulated_time;
 62  ASSERT_EQ(expected,t.hasElapsed()) <<
 63    " Start " << start_time << endl <<
 64    " Dur.  " << duration << endl <<
 65    " Time  " << simulated_time;
 66}
 67
 68TEST(TimeoutTest, OrdinaryCase)
 69{
 70  srandom(time(NULL));
 71  for (int i  = 0; i < 100; i++) {
 72    uint32_t duration = random() % (UINT32_MAX/4);
 73    if (duration < 2) duration = 2;
 74    micros_t start = random() % (UINT32_MAX-duration);
 75    runTest(start,duration,start-1,true);
 76    runTest(start,duration,start,false);
 77    runTest(start,duration,start+1,false);
 78    runTest(start,duration,start+duration-1,false);
 79    runTest(start,duration,start+duration,true);
 80    runTest(start,duration,start+duration+1,true);
 81  }
 82}
 83
 84TEST(TimeoutTest, IntervalWrapCase)
 85{
 86  srandom(time(NULL));
 87  for (int i  = 0; i < 100; i++) {
 88    uint32_t duration = random() % (UINT32_MAX/4);
 89    if (duration < 2) duration = 2;
 90    micros_t start = random() % duration + (UINT32_MAX-duration);
 91    runTest(start,duration,start-1,true);
 92    runTest(start,duration,start,false);
 93    runTest(start,duration,start+1,false);
 94    runTest(start,duration,start+duration-1,false);
 95    runTest(start,duration,start+duration,true);
 96    runTest(start,duration,start+duration+1,true);
 97  }
 98}
 99
100
101TEST(TimeoutTest, TimerWrapCase)
102{
103  srandom(time(NULL));
104  for (int i  = 0; i < 100; i++) {
105    uint32_t duration = random() % (UINT32_MAX/4);
106    micros_t start = random() % UINT32_MAX;
107    micros_t when = start - random()%(UINT32_MAX-duration);
108    runTest(start,duration,when,true);
109  }
110}
111
112TEST(TimeoutTest, BoundryCase1)
113{
114  // start time = 0;
115  srandom(time(NULL));
116  uint32_t duration = random() % (UINT32_MAX/4);
117  if (duration < 2) duration = 2;
118  micros_t start = 0;
119  runTest(start,duration,start-1,true);
120  runTest(start,duration,start,false);
121  runTest(start,duration,start+1,false);
122  runTest(start,duration,start+duration-1,false);
123  runTest(start,duration,start+duration,true);
124  runTest(start,duration,start+duration+1,true);
125}
126
127TEST(TimeoutTest, BoundryCase2)
128{
129  // start time = max-1;
130  srandom(time(NULL));
131  uint32_t duration = random() % (UINT32_MAX/4);
132  if (duration < 2) duration = 2;
133  micros_t start = UINT32_MAX-1;
134  runTest(start,duration,start-1,true);
135  runTest(start,duration,start,false);
136  runTest(start,duration,start+1,false);
137  runTest(start,duration,start+duration-1,false);
138  runTest(start,duration,start+duration,true);
139  runTest(start,duration,start+duration+1,true);
140}
141
142TEST(TimeoutTest, BoundryCase3)
143{
144  // end time = max-1;
145  srandom(time(NULL));
146  micros_t start = random() % UINT32_MAX;
147  micros_t duration = (UINT32_MAX-1) - start;
148  runTest(start,duration,start-1,true);
149  runTest(start,duration,start,false);
150  runTest(start,duration,start+1,false);
151  runTest(start,duration,start+duration-1,false);
152  runTest(start,duration,start+duration,true);
153  runTest(start,duration,start+duration+1,true);
154}
155
156TEST(TimeoutTest, BoundryCase4)
157{
158  // end time = 0;
159  srandom(time(NULL));
160  micros_t start = random() % UINT32_MAX;
161  micros_t duration = 0 - start;
162  runTest(start,duration,start-1,true);
163  runTest(start,duration,start,false);
164  runTest(start,duration,start+1,false);
165  runTest(start,duration,start+duration-1,false);
166  runTest(start,duration,start+duration,true);
167  runTest(start,duration,start+duration+1,true);
168}
169
170micros_t call(micros_t in) {
171  return in;
172}