16
16
#include < modm-test/mock/clock.hpp>
17
17
18
18
using namespace std ::chrono_literals;
19
- using test_clock = modm_test::chrono::milli_clock;
19
+ using test_clock_ms = modm_test::chrono::milli_clock;
20
+ using test_clock_us = modm_test::chrono::micro_clock;
20
21
21
22
void
22
23
FiberTest::setUp ()
@@ -103,7 +104,7 @@ FiberTest::testYieldFromSubroutine()
103
104
void
104
105
FiberTest::testPollFor ()
105
106
{
106
- test_clock ::setTime (1251 );
107
+ test_clock_ms ::setTime (1251 );
107
108
TEST_ASSERT_TRUE (modm::this_fiber::poll_for (20ms, []{ return true ; }));
108
109
// timeout is tested in the SleepFor() test
109
110
}
@@ -112,7 +113,7 @@ FiberTest::testPollFor()
112
113
void
113
114
FiberTest::testPollUntil ()
114
115
{
115
- test_clock ::setTime (451250 );
116
+ test_clock_ms ::setTime (451250 );
116
117
TEST_ASSERT_TRUE (modm::this_fiber::poll_until (modm::Clock::now () + 20ms, []{ return true ; }));
117
118
TEST_ASSERT_TRUE (modm::this_fiber::poll_until (modm::Clock::now () - 20ms, []{ return true ; }));
118
119
// timeout is tested in the SleepUntil() tests
@@ -123,33 +124,52 @@ static void
123
124
f4 ()
124
125
{
125
126
TEST_ASSERT_EQUALS (state++, 0u );
126
- modm::this_fiber::sleep_for (50ms); // goto 1
127
+ // yields only once
128
+ modm::this_fiber::sleep_for (0s); // goto 1
129
+
130
+ TEST_ASSERT_EQUALS (state++, 2u );
131
+ // rounds up to 1us, not 0us!
132
+ modm::this_fiber::sleep_for (100ns); // goto 3
133
+
127
134
TEST_ASSERT_EQUALS (state++, 5u );
135
+ modm::this_fiber::sleep_for (50ms); // goto 6
136
+ TEST_ASSERT_EQUALS (state++, 10u );
128
137
}
129
138
130
139
static void
131
140
f5 ()
132
141
{
133
142
TEST_ASSERT_EQUALS (state++, 1u );
134
- test_clock::increment (10 );
135
- TEST_ASSERT_EQUALS (state++, 2u );
136
- modm::this_fiber::yield ();
143
+ modm::this_fiber::yield (); // goto 2
137
144
138
- test_clock::increment (20 );
139
145
TEST_ASSERT_EQUALS (state++, 3u );
140
146
modm::this_fiber::yield ();
141
147
142
- test_clock::increment (30 );
143
148
TEST_ASSERT_EQUALS (state++, 4u );
149
+ test_clock_us::increment (1 );
144
150
modm::this_fiber::yield (); // goto 5
145
151
146
152
TEST_ASSERT_EQUALS (state++, 6u );
153
+ test_clock_ms::increment (10 );
154
+ TEST_ASSERT_EQUALS (state++, 7u );
155
+ modm::this_fiber::yield ();
156
+
157
+ test_clock_ms::increment (20 );
158
+ TEST_ASSERT_EQUALS (state++, 8u );
159
+ modm::this_fiber::yield ();
160
+
161
+ test_clock_ms::increment (30 );
162
+ TEST_ASSERT_EQUALS (state++, 9u );
163
+ modm::this_fiber::yield (); // goto 10
164
+
165
+ TEST_ASSERT_EQUALS (state++, 11u );
147
166
}
148
167
149
168
static void
150
169
runSleepFor (uint32_t startTime)
151
170
{
152
- test_clock::setTime (startTime);
171
+ test_clock_ms::setTime (startTime);
172
+ test_clock_us::setTime (startTime);
153
173
modm::fiber::Task fiber1 (stack1, f4), fiber2 (stack2, f5);
154
174
modm::fiber::Scheduler::run ();
155
175
}
@@ -165,6 +185,25 @@ FiberTest::testSleepFor()
165
185
166
186
static void
167
187
f6 ()
188
+ {
189
+ TEST_ASSERT_EQUALS (state++, 1u );
190
+ test_clock_ms::increment (10 );
191
+ TEST_ASSERT_EQUALS (state++, 2u );
192
+ modm::this_fiber::yield ();
193
+
194
+ test_clock_ms::increment (20 );
195
+ TEST_ASSERT_EQUALS (state++, 3u );
196
+ modm::this_fiber::yield ();
197
+
198
+ test_clock_ms::increment (30 );
199
+ TEST_ASSERT_EQUALS (state++, 4u );
200
+ modm::this_fiber::yield (); // goto 5
201
+
202
+ TEST_ASSERT_EQUALS (state++, 6u );
203
+ }
204
+
205
+ static void
206
+ f7 ()
168
207
{
169
208
TEST_ASSERT_EQUALS (state++, 0u ); // goto 1
170
209
modm::this_fiber::sleep_until (modm::Clock::now () + 50ms);
177
216
static void
178
217
runSleepUntil (uint32_t startTime)
179
218
{
180
- test_clock ::setTime (startTime);
181
- modm::fiber::Task fiber1 (stack1, f6 ), fiber2 (stack2, f5 );
219
+ test_clock_ms ::setTime (startTime);
220
+ modm::fiber::Task fiber1 (stack1, f7 ), fiber2 (stack2, f6 );
182
221
modm::fiber::Scheduler::run ();
183
222
}
184
223
@@ -191,7 +230,7 @@ FiberTest::testSleepUntil()
191
230
}
192
231
193
232
static void
194
- f7 (modm::fiber::stop_token stoken)
233
+ f8 (modm::fiber::stop_token stoken)
195
234
{
196
235
TEST_ASSERT_EQUALS (state++, 0u );
197
236
TEST_ASSERT_TRUE (stoken.stop_possible ());
@@ -208,7 +247,7 @@ f7(modm::fiber::stop_token stoken)
208
247
void
209
248
FiberTest::testStopToken ()
210
249
{
211
- modm::fiber::Task fiber1 (stack1, f7 );
250
+ modm::fiber::Task fiber1 (stack1, f8 );
212
251
modm::fiber::Task fiber2 (stack2, [&](modm::fiber::stop_token stoken)
213
252
{
214
253
TEST_ASSERT_EQUALS (state++, 1u );
0 commit comments