forked from contrem/arduino-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimerTest.ino
339 lines (268 loc) · 9.05 KB
/
timerTest.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// timerTest.ino
//
// Confirm arduino-timer behaves as expected.
// UnixHostDuino emulation needs this include
// (it's not picked up "for free" by Arduino IDE)
//
#include <Arduino.h>
// fake it for UnixHostDuino emulation
#if defined(UNIX_HOST_DUINO)
# ifndef ARDUINO
# define ARDUINO 100
# endif
#endif
//
// also, you need to provide your own forward references
// These tests depend on the Arduino "AUnit" library
#include <AUnit.h>
#include <arduino-timer.h>
////////////////////////////////////////////////////////
// You really want to be simulating time, rather than
// forcing tests to slow down.
// This simulation works across different processor families.
//
// BE CAREFUL to use delay() and millis() ONLY WHEN YOU MEAN IT!
//
// this namespace collision should help you make it more clear what you get
//
namespace simulateTime {
////////////////////////////////////////////////////////
// Simulate delays and elapsed time
//
unsigned long simTime = 0;
unsigned long simMillis(void) {
return simTime;
}
void simDelay(unsigned long delayTime) {
simTime += delayTime;
}
// TRAP ambiguous calls to delay() and millis() that are NOT simulated
void delay(unsigned long delayTime) {
simDelay(delayTime);
}
unsigned long millis(void) {
return simMillis();
}
}; // namespace simulateTime
using namespace simulateTime; // and detect ambiguous function calls
////////////////////////////////////////////////////////
// instrumented dummy tasks
class DummyTask {
public:
DummyTask(unsigned long runTime, bool repeats = true) :
busyTime(runTime), repeat(repeats)
{
reset();
};
unsigned long busyTime;
unsigned long numRuns;
unsigned long timeOfLastRun;
bool repeat;
bool run(void) {
timeOfLastRun = simMillis();
simDelay(busyTime);
numRuns++;
return repeat;
};
// run a task as an object method
static bool runATask(void * aDummy)
{
DummyTask * myDummy = static_cast<DummyTask *>(aDummy);
return myDummy->run();
};
void reset(void) {
timeOfLastRun = 0;
numRuns = 0;
};
};
// trivial dummy task to perform
bool no_op(void *) {
return false;
}
// timer doesn't work as a local var; too big for the stack?
// Since it's not on the stack it's harder to guarantee it starts empty
// after the first test()
//
#define MAXTASKS 5
Timer<MAXTASKS, simMillis> timer;
void prepForTests(void) {
timer.cancel();
simTime = 0ul;
}
//////////////////////////////////////////////////////////////////////////////
// confirm tasks can be cancelled.
test(timer_cancelTasks) {
prepForTests();
unsigned long aWait = timer.ticks(); // time to next active task
assertEqual(aWait, 0ul); // no tasks!
DummyTask dt_3millisec(3ul);
DummyTask dt_5millisec(5ul);
DummyTask dt_7millisec(7ul);
auto inTask = timer.in(13ul, DummyTask::runATask, &dt_3millisec);
auto atTask = timer.at(17ul, DummyTask::runATask, &dt_5millisec);
auto everyTask = timer.every(19ul, DummyTask::runATask, &dt_7millisec);
aWait = timer.ticks();
assertEqual(aWait, 13ul); // inTask delay
timer.cancel(inTask);
aWait = timer.ticks();
assertEqual(aWait, 17ul); // atTask delay
timer.cancel(atTask);
aWait = timer.ticks();
assertEqual(aWait, 19ul); // everyTask delay
timer.cancel(everyTask);
aWait = timer.ticks();
assertEqual(aWait, 0ul); // no tasks! all canceled
};
//////////////////////////////////////////////////////////////////////////////
// confirm timer.at() behaviors
//
test(timer_at) {
prepForTests();
unsigned long aWait = timer.ticks(); // time to next active task
assertEqual(aWait, 0ul); // no tasks!
assertEqual(simMillis(), 0ul);
DummyTask waste_3ms(3ul);
const unsigned long atTime = 17ul;
const unsigned long lateStart = 4ul;
simDelay(lateStart);
// Note timer.at() returns the task ID.
// Keep it to modify the task later.
//
timer.at(atTime, DummyTask::runATask, &waste_3ms);
aWait = timer.tick();
assertEqual(aWait, atTime - lateStart);
assertEqual(waste_3ms.numRuns, 0ul);
for (unsigned long i = lateStart + 1ul; i < atTime; i++ ) {
simDelay(1ul);
aWait = timer.tick();
assertEqual(waste_3ms.numRuns, 0ul); // still waiting
}
simDelay(1ul);
aWait = timer.tick();
assertEqual(waste_3ms.numRuns, 1ul); // triggered
assertEqual(aWait, 0ul);
simDelay(1ul);
aWait = timer.tick();
assertEqual(waste_3ms.numRuns, 1ul); // not repeating
aWait = timer.tick();
assertEqual(aWait, 0ul); // no tasks! all canceled
};
//////////////////////////////////////////////////////////////////////////////
// confirm timer.in() behaviors
//
test(timer_in) {
prepForTests();
unsigned long aWait = timer.ticks(); // time to next active task
assertEqual(aWait, 0ul); // no tasks!
assertEqual(simMillis(), 0ul);
DummyTask waste_3ms(3);
const unsigned long lateStart = 7;
simDelay(lateStart);
const unsigned long delayTime = 17;
timer.in(delayTime, DummyTask::runATask, &waste_3ms);
aWait = timer.tick();
assertEqual(aWait, delayTime);
assertEqual(waste_3ms.numRuns, 0ul);
for (unsigned long i = 1ul; i < delayTime; i++ ) {
simDelay(1ul);
aWait = timer.tick();
assertEqual(waste_3ms.numRuns, 0ul); // still waiting
}
simDelay(1ul);
aWait = timer.tick();
assertEqual(waste_3ms.numRuns, 1ul); // triggered
assertEqual(aWait, 0ul);
simDelay(1ul);
aWait = timer.tick();
assertEqual(waste_3ms.numRuns, 1ul); // not repeating
aWait = timer.tick();
assertEqual(aWait, 0ul); // no tasks! all canceled
};
//////////////////////////////////////////////////////////////////////////////
// confirm timer.every() behaviors
//
test(timer_every) {
prepForTests();
unsigned long aWait = timer.ticks(); // time to next active task
assertEqual(aWait, 0ul); // no tasks!
assertEqual(simMillis(), 0ul);
DummyTask waste_3ms(3ul);
DummyTask waste_100ms_once(100ul, false);
const unsigned long lateStart = 7ul;
simDelay(lateStart);
//const unsigned long delayTime = 17;
timer.every(50ul, DummyTask::runATask, &waste_3ms);
timer.every(200ul, DummyTask::runATask, &waste_100ms_once);
aWait = timer.tick();
assertEqual(aWait, 50ul);
assertEqual(waste_3ms.numRuns, 0ul);
for (unsigned long i = 1ul; i < 1000ul; i++ ) {
simDelay(1ul);
aWait = timer.tick();
}
assertEqual(waste_3ms.numRuns, 22ul); // triggered
assertEqual(waste_100ms_once.numRuns, 1ul); // triggered
aWait = timer.tick();
assertEqual(aWait, 39ul); // still a repeating task
};
//////////////////////////////////////////////////////////////////////////////
// confirm calculated delays to next event in the timer are "reasonable"
// reported by timer.tick() and timer.ticks().
//
test(timer_delayToNextEvent) {
prepForTests();
unsigned long aWait = timer.ticks(); // time to next active task
assertEqual(aWait, 0ul); // no tasks!
DummyTask dt_3millisec(3ul);
DummyTask dt_5millisec(5ul);
timer.every( 7ul, DummyTask::runATask, &dt_3millisec);
timer.every(11ul, DummyTask::runATask, &dt_5millisec);
assertEqual(dt_3millisec.numRuns, 0ul);
unsigned long start = simMillis();
assertEqual(start, 0ul); // earliest task
aWait = timer.ticks(); // time to next active task
assertEqual(aWait, 7ul); // earliest task
aWait = timer.tick(); // no tasks ran?
unsigned long firstRunTime = simMillis() - start;
assertEqual(firstRunTime, 0ul);
simDelay(aWait);
unsigned long firstActiveRunStart = simMillis();
aWait = timer.tick();
unsigned long firstTaskRunTime = simMillis() - firstActiveRunStart;
assertEqual(firstTaskRunTime, 3ul);
assertEqual(aWait, (unsigned long) (11 - 7 - 3)); // other pending task
// run some tasks; count them.
while (simMillis() < start + 1000ul) {
aWait = timer.tick();
if(aWait == 0ul) {
aWait = 1ul; // at least one millisec
}
simDelay(aWait);
}
// expect the other task causes some missed deadlines
assertNear(dt_3millisec.numRuns, 100ul, 9ul); // 7+ millisecs apart
// (ideally 142 runs)
assertNear(dt_5millisec.numRuns, 90ul, 4ul); // 11+ millisecs apart
// (ideally 90 runs)
};
////////////// ... so which sketch is this?
void showID(void)
{
Serial.println();
Serial.println(F( "Running " __FILE__ ", Built " __DATE__));
};
//////////////////////////////////////////////////////////////////////////////
void setup() {
::delay(1000ul); // wait for stability on some boards to prevent garbage Serial
Serial.begin(115200ul); // ESP8266 default of 74880 not supported on Linux
while (!Serial); // for the Arduino Leonardo/Micro only
showID();
}
//////////////////////////////////////////////////////////////////////////////
void loop() {
// Should get:
// TestRunner summary:
// <n> passed, <n> failed, <n> skipped, <n> timed out, out of <n> test(s).
aunit::TestRunner::run();
}