forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_event_queue.cpp
More file actions
219 lines (172 loc) · 5.55 KB
/
Copy pathtest_event_queue.cpp
File metadata and controls
219 lines (172 loc) · 5.55 KB
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
/**
* @file test_signal_executor.cpp
*
* @date Nov 28, 2013
* @author Andrey Belomutskiy, (c) 2012-2020
*/
#include "pch.h"
#include "event_queue.h"
static int callbackCounter = 0;
static void callback() {
callbackCounter++;
}
namespace {
auto const callbackAction{ action_s::make<callback>() };
}
static int complexTestNow;
struct TestPwm {
TestPwm(EventQueue *eventQueue) {
this->eventQueue = eventQueue;
}
scheduling_s s;
int period;
EventQueue *eventQueue;
};
static void complexCallback(TestPwm *testPwm) {
callbackCounter++;
testPwm->eventQueue->insertTask(&testPwm->s, complexTestNow + testPwm->period,
action_s::make<complexCallback>(testPwm));
}
TEST(EventQueue, testSignalExecutor2) {
EventQueue eq;
TestPwm p1(&eq);
TestPwm p2(&eq);
p1.period = 2;
p2.period = 3;
complexTestNow = 0;
callbackCounter = 0;
eq.insertTask(&p1.s, 0, action_s::make<complexCallback>(&p1));
eq.insertTask(&p2.s, 0, action_s::make<complexCallback>(&p2));
eq.executeAll(complexTestNow);
ASSERT_EQ( 2, callbackCounter) << "callbackCounter #1";
ASSERT_EQ(2, eq.size());
eq.executeAll(complexTestNow = 2);
ASSERT_EQ( 3, callbackCounter) << "callbackCounter #2";
ASSERT_EQ(2, eq.size());
eq.executeAll(complexTestNow = 3);
ASSERT_EQ( 4, callbackCounter) << "callbackCounter #3";
ASSERT_EQ(2, eq.size());
}
static uintptr_t prevValue = 0;
static void orderCallback(uintptr_t a) {
uintptr_t value = a;
printf("value=%d prevValue=%d\r\n", value, prevValue);
ASSERT_TRUE(value > prevValue) << "orderCallback";
prevValue = value;
}
TEST(EventQueue, simple) {
EventQueue eq;
scheduling_s s1;
scheduling_s s2;
scheduling_s s3;
eq.insertTask(&s1, 10, action_s::make<orderCallback>(uintptr_t{1}));
eq.insertTask(&s2, 11, action_s::make<orderCallback>(uintptr_t{2}));
eq.insertTask(&s3, 12, action_s::make<orderCallback>(uintptr_t{3}));
eq.executeAll(100);
}
TEST(EventQueue, complex) {
EventQueue eq;
ASSERT_EQ(eq.getNextEventTime(0), unexpected);
scheduling_s s1;
scheduling_s s2;
scheduling_s s3;
scheduling_s s4;
eq.insertTask(&s1, 10, callbackAction);
eq.insertTask(&s4, 10, callbackAction);
eq.insertTask(&s3, 12, callbackAction);
eq.insertTask(&s2, 11, callbackAction);
ASSERT_EQ(4, eq.size());
ASSERT_EQ(10, eq.getHead()->getMomentNt());
ASSERT_EQ(10, eq.getHead()->next->getMomentNt());
ASSERT_EQ(11, eq.getHead()->next->next->getMomentNt());
ASSERT_EQ(12, eq.getHead()->next->next->next->getMomentNt());
callbackCounter = 0;
eq.executeAll(10);
ASSERT_EQ( 2, callbackCounter) << "callbackCounter/2";
callbackCounter = 0;
eq.executeAll(11);
ASSERT_EQ( 1, callbackCounter) << "callbackCounter/1#1";
eq.executeAll(100);
ASSERT_EQ(0, eq.size());
eq.insertTask(&s1, 12, callbackAction);
eq.insertTask(&s2, 11, callbackAction);
eq.insertTask(&s3, 10, callbackAction);
callbackCounter = 0;
eq.executeAll(10);
ASSERT_EQ( 1, callbackCounter) << "callbackCounter/1#2";
callbackCounter = 0;
eq.executeAll(11);
ASSERT_EQ(1, callbackCounter);
eq.executeAll(100);
ASSERT_EQ(0, eq.size());
callbackCounter = 0;
eq.insertTask(&s1, 10, callbackAction);
ASSERT_EQ(10, eq.getNextEventTime(0).value_or(-1));
eq.executeAll(1);
ASSERT_EQ( 0, callbackCounter) << "callbacks not expected";
eq.executeAll(11);
ASSERT_EQ(1, callbackCounter);
ASSERT_EQ(eq.getNextEventTime(0), unexpected);
eq.insertTask(&s1, 10, callbackAction);
eq.insertTask(&s2, 13, callbackAction);
ASSERT_EQ(10, eq.getNextEventTime(0).value_or(-1));
eq.executeAll(1);
ASSERT_EQ(10, eq.getNextEventTime(0).value_or(-1));
eq.executeAll(100);
ASSERT_EQ(0, eq.size());
callbackCounter = 0;
// both events are scheduled for the same time
eq.insertTask(&s1, 10, callbackAction);
eq.insertTask(&s2, 10, callbackAction);
eq.executeAll(11);
ASSERT_EQ(2, callbackCounter);
}
class EventQueueRemoveTest : public ::testing::Test {
protected:
EventQueue dut;
scheduling_s s1, s2, s3;
void SetUp() override {
dut.insertTask(&s1, 100, callbackAction);
dut.insertTask(&s2, 200, callbackAction);
dut.insertTask(&s3, 300, callbackAction);
// Check that things are assembled as we think
ASSERT_EQ(&s1, dut.getElementAtIndexForUnitText(0));
ASSERT_EQ(&s2, dut.getElementAtIndexForUnitText(1));
ASSERT_EQ(&s3, dut.getElementAtIndexForUnitText(2));
ASSERT_EQ(nullptr, dut.getElementAtIndexForUnitText(3));
}
};
TEST_F(EventQueueRemoveTest, removeHead) {
// Remove the element at the head
dut.remove(&s1);
// Check that it's gone
ASSERT_EQ(&s2, dut.getElementAtIndexForUnitText(0));
ASSERT_EQ(&s3, dut.getElementAtIndexForUnitText(1));
ASSERT_EQ(nullptr, dut.getElementAtIndexForUnitText(2));
}
TEST_F(EventQueueRemoveTest, removeMiddle) {
// Remove the element in the middle
dut.remove(&s2);
// Check that it's gone
ASSERT_EQ(&s1, dut.getElementAtIndexForUnitText(0));
ASSERT_EQ(&s3, dut.getElementAtIndexForUnitText(1));
ASSERT_EQ(nullptr, dut.getElementAtIndexForUnitText(2));
}
TEST_F(EventQueueRemoveTest, removeEnd) {
// Remove the element at the end
dut.remove(&s3);
// Check that it's gone
ASSERT_EQ(&s1, dut.getElementAtIndexForUnitText(0));
ASSERT_EQ(&s2, dut.getElementAtIndexForUnitText(1));
ASSERT_EQ(nullptr, dut.getElementAtIndexForUnitText(2));
}
TEST_F(EventQueueRemoveTest, removeNotPresent) {
scheduling_s s4;
// Remove an element not already in the list - shouldn't fail
EXPECT_NO_THROW(dut.remove(&s4));
// Check that the list didn't change
ASSERT_EQ(&s1, dut.getElementAtIndexForUnitText(0));
ASSERT_EQ(&s2, dut.getElementAtIndexForUnitText(1));
ASSERT_EQ(&s3, dut.getElementAtIndexForUnitText(2));
ASSERT_EQ(nullptr, dut.getElementAtIndexForUnitText(3));
}