forked from frc971/971-Robot-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphased_loop_test.cc
307 lines (268 loc) · 11.3 KB
/
phased_loop_test.cc
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
#include "aos/util/phased_loop.h"
#include <memory>
#include <ratio>
#include "gtest/gtest.h"
#include "aos/time/time.h"
namespace aos::time::testing {
using ::std::chrono::milliseconds;
using ::std::chrono::nanoseconds;
typedef ::testing::Test PhasedLoopTest;
typedef PhasedLoopTest PhasedLoopDeathTest;
monotonic_clock::time_point InMs(int ms) {
return monotonic_clock::time_point(::std::chrono::milliseconds(ms));
}
TEST_F(PhasedLoopTest, Reset) {
{
PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
milliseconds(0));
loop.Reset(monotonic_clock::epoch());
EXPECT_EQ(InMs(0), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
EXPECT_EQ(InMs(100), loop.sleep_time());
loop.Reset(InMs(99));
EXPECT_EQ(InMs(0), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(99)));
EXPECT_EQ(InMs(100), loop.sleep_time());
loop.Reset(InMs(100));
EXPECT_EQ(InMs(100), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(199)));
EXPECT_EQ(InMs(200), loop.sleep_time());
loop.Reset(InMs(101));
EXPECT_EQ(InMs(100), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(101)));
EXPECT_EQ(InMs(200), loop.sleep_time());
}
{
PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
milliseconds(1));
loop.Reset(monotonic_clock::epoch());
EXPECT_EQ(InMs(-99), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
EXPECT_EQ(InMs(1), loop.sleep_time());
}
{
PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
milliseconds(99));
loop.Reset(monotonic_clock::epoch());
EXPECT_EQ(InMs(-1), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
EXPECT_EQ(InMs(99), loop.sleep_time());
loop.Reset(InMs(98));
EXPECT_EQ(InMs(-1), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(98)));
EXPECT_EQ(InMs(99), loop.sleep_time());
loop.Reset(InMs(99));
EXPECT_EQ(InMs(99), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(99)));
EXPECT_EQ(InMs(199), loop.sleep_time());
loop.Reset(InMs(100));
EXPECT_EQ(InMs(99), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(100)));
EXPECT_EQ(InMs(199), loop.sleep_time());
}
}
TEST_F(PhasedLoopTest, Iterate) {
{
PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
milliseconds(99));
loop.Reset(monotonic_clock::epoch());
EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
EXPECT_EQ(InMs(99), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(100)));
EXPECT_EQ(InMs(199), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(100)));
EXPECT_EQ(InMs(199), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(101)));
EXPECT_EQ(InMs(199), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(198)));
EXPECT_EQ(InMs(199), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(199)));
EXPECT_EQ(InMs(299), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(300)));
EXPECT_EQ(InMs(399), loop.sleep_time());
EXPECT_EQ(3, loop.Iterate(InMs(600)));
EXPECT_EQ(InMs(699), loop.sleep_time());
}
{
PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
milliseconds(1));
loop.Reset(monotonic_clock::epoch());
EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
EXPECT_EQ(InMs(1), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(100)));
EXPECT_EQ(InMs(101), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(100)));
EXPECT_EQ(InMs(101), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(103)));
EXPECT_EQ(InMs(201), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(198)));
EXPECT_EQ(InMs(201), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(200)));
EXPECT_EQ(InMs(201), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(201)));
EXPECT_EQ(InMs(301), loop.sleep_time());
EXPECT_EQ(3, loop.Iterate(InMs(600)));
EXPECT_EQ(InMs(601), loop.sleep_time());
}
}
// Makes sure that everything works correctly when crossing zero.
// This seems like a rare case at first, but starting from zero needs to
// work, which means negatives should too.
TEST_F(PhasedLoopTest, CrossingZero) {
PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(), milliseconds(1));
loop.Reset(InMs(-1000));
EXPECT_EQ(InMs(-1099), loop.sleep_time());
EXPECT_EQ(9, loop.Iterate(InMs(-250)));
EXPECT_EQ(InMs(-199), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(-199)));
EXPECT_EQ(InMs(-99), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(-90)));
EXPECT_EQ(InMs(1), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(0)));
EXPECT_EQ(InMs(1), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(1)));
EXPECT_EQ(InMs(101), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(2)));
EXPECT_EQ(InMs(101), loop.sleep_time());
EXPECT_EQ(-2, loop.Iterate(InMs(-101)));
EXPECT_EQ(InMs(-99), loop.sleep_time());
EXPECT_EQ(1, loop.Iterate(InMs(-99)));
EXPECT_EQ(InMs(1), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(InMs(-99)));
EXPECT_EQ(InMs(1), loop.sleep_time());
}
// Tests OffsetFromIntervalAndTime for various edge conditions.
TEST_F(PhasedLoopTest, OffsetFromIntervalAndTimeTest) {
PhasedLoop loop(milliseconds(1000), monotonic_clock::epoch(),
milliseconds(300));
EXPECT_EQ(milliseconds(1),
loop.OffsetFromIntervalAndTime(milliseconds(1000), InMs(1001)));
EXPECT_EQ(milliseconds(0),
loop.OffsetFromIntervalAndTime(milliseconds(1000), InMs(1000)));
EXPECT_EQ(milliseconds(0),
loop.OffsetFromIntervalAndTime(milliseconds(1000), InMs(0)));
EXPECT_EQ(milliseconds(999),
loop.OffsetFromIntervalAndTime(milliseconds(1000), InMs(-1)));
EXPECT_EQ(milliseconds(7),
loop.OffsetFromIntervalAndTime(milliseconds(1000), InMs(19115007)));
EXPECT_EQ(milliseconds(7), loop.OffsetFromIntervalAndTime(milliseconds(1000),
InMs(-19115993)));
}
// Tests that passing invalid values to the constructor dies correctly.
TEST_F(PhasedLoopDeathTest, InvalidValues) {
EXPECT_DEATH(
PhasedLoop(milliseconds(1), monotonic_clock::epoch(), milliseconds(2)),
".*offset < interval.*");
EXPECT_DEATH(
PhasedLoop(milliseconds(1), monotonic_clock::epoch(), milliseconds(1)),
".*offset < interval.*");
EXPECT_DEATH(
PhasedLoop(milliseconds(1), monotonic_clock::epoch(), milliseconds(-1)),
".*offset >= monotonic_clock::duration\\(0\\).*");
EXPECT_DEATH(
PhasedLoop(milliseconds(0), monotonic_clock::epoch(), milliseconds(0)),
".*interval > monotonic_clock::duration\\(0\\).*");
}
// Tests that every single value within two intervals of 0 works.
// This is good at finding edge cases in the rounding.
TEST_F(PhasedLoopTest, SweepingZero) {
for (int i = -30; i < -20; ++i) {
PhasedLoop loop(nanoseconds(20),
monotonic_clock::epoch() - nanoseconds(30));
EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
}
for (int i = -20; i < 0; ++i) {
PhasedLoop loop(nanoseconds(20),
monotonic_clock::epoch() - nanoseconds(30));
EXPECT_EQ(2, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
}
for (int i = 0; i < 20; ++i) {
PhasedLoop loop(nanoseconds(20),
monotonic_clock::epoch() - nanoseconds(30));
EXPECT_EQ(3, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
}
for (int i = 20; i < 30; ++i) {
PhasedLoop loop(nanoseconds(20),
monotonic_clock::epoch() - nanoseconds(30));
EXPECT_EQ(4, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
}
}
// Tests that the phased loop is correctly adjusting when the offset is
// decremented multiple times.
TEST_F(PhasedLoopTest, DecrementingOffset) {
constexpr int kCount = 5;
constexpr int kIterations = 10;
const auto kOffset = milliseconds(400);
const auto kInterval = milliseconds(1000);
const auto kAllIterationsInterval = kInterval * kIterations;
PhasedLoop loop(kInterval, monotonic_clock::epoch(), kOffset);
auto last_time = monotonic_clock::epoch() + kOffset + (kInterval * 3);
ASSERT_EQ(5, loop.Iterate(last_time));
for (int i = 1; i < kCount; i++) {
const auto offset = kOffset - milliseconds(i);
// First, set the interval/offset without specifying a "now". If we then
// attempt to Iterate() to the same time as the last iteration, this should
// always result in zero cycles elapsed.
{
const monotonic_clock::time_point original_time = loop.sleep_time();
loop.set_interval_and_offset(kInterval, offset);
EXPECT_EQ(original_time - milliseconds(1), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(last_time));
}
// Now, explicitly update/clear things to last_time. This should have the
// same behavior as not specifying a monotonic_now.
{
loop.set_interval_and_offset(kInterval, offset, last_time);
EXPECT_EQ(0, loop.Iterate(last_time));
}
const auto next_time = last_time - milliseconds(1) + kAllIterationsInterval;
EXPECT_EQ(kIterations, loop.Iterate(next_time));
last_time = next_time;
}
}
// Tests that the phased loop is correctly adjusting when the offset is
// incremented multiple times.
TEST_F(PhasedLoopTest, IncrementingOffset) {
constexpr int kCount = 5;
constexpr int kIterations = 10;
const auto kOffset = milliseconds(0);
const auto kInterval = milliseconds(1000);
const auto kAllIterationsInterval = kInterval * kIterations;
PhasedLoop loop(kInterval, monotonic_clock::epoch(), kOffset);
auto last_time = monotonic_clock::epoch() + kOffset + (kInterval * 3);
ASSERT_EQ(4, loop.Iterate(last_time));
for (int i = 1; i < kCount; i++) {
const auto offset = kOffset + milliseconds(i);
{
const monotonic_clock::time_point original_time = loop.sleep_time();
loop.set_interval_and_offset(kInterval, offset);
EXPECT_EQ(original_time - kInterval + milliseconds(1), loop.sleep_time());
EXPECT_EQ(0, loop.Iterate(last_time));
}
// Now, explicitly update/clear things to a set time. We add a milliseconds
// so that when we call Iterate() next we actually get the expected number
// of iterations (otherwise, there is an iteration that would happen at
// last_time + 1 that gets counted, which is correct behavior, and so just
// needs to be accounted for somehow).
{
loop.set_interval_and_offset(kInterval, offset,
last_time + milliseconds(1));
EXPECT_EQ(0, loop.Iterate(last_time + milliseconds(1)));
}
const auto next_time = last_time + milliseconds(1) + kAllIterationsInterval;
EXPECT_EQ(kIterations, loop.Iterate(next_time));
last_time = next_time;
}
}
// Tests that the phased loop is correctly adjusting when the offset is
// changed to 0.
TEST_F(PhasedLoopTest, ChangingOffset) {
const auto kOffset = milliseconds(900);
const auto kInterval = milliseconds(1000);
PhasedLoop loop(kInterval, monotonic_clock::epoch(), kOffset);
const auto last_time = monotonic_clock::epoch() + kOffset + (kInterval * 3);
ASSERT_EQ(5, loop.Iterate(last_time));
loop.set_interval_and_offset(kInterval, milliseconds(0));
EXPECT_EQ(4, loop.Iterate((last_time - kOffset) + (kInterval * 4)));
}
} // namespace aos::time::testing