25
25
#else
26
26
27
27
using utest::v1::Case;
28
+ using namespace std ::chrono;
28
29
29
- static const int test_timeout = 10 ;
30
+ #define TEST_ASSERT_EQUAL_DURATION (expected, actual ) \
31
+ do { \
32
+ using ct = std::common_type_t <decltype (expected), decltype (actual)>; \
33
+ TEST_ASSERT_EQUAL (ct (expected).count (), ct (actual).count ()); \
34
+ } while (0 )
35
+
36
+ #define TEST_ASSERT_DURATION_WITHIN (delta, expected, actual ) \
37
+ do { \
38
+ using ct = std::common_type_t <decltype (delta), decltype (expected), decltype (actual)>; \
39
+ TEST_ASSERT_INT_WITHIN (ct (delta).count (), ct (expected).count (), ct (actual).count ()); \
40
+ } while (0 )
30
41
31
42
#define TICKER_COUNT 16
32
- #define MULTI_TICKER_TIME_MS 100
43
+ #define MULTI_TICKER_TIME 100ms
33
44
34
45
/* Due to poor accuracy of LowPowerTicker on many platforms
35
46
there is no sense to tune tolerance value as it was in Ticker tests.
36
47
37
48
Tolerance value is set to 600us for measurement inaccuracy + 5% tolerance
38
49
for LowPowerTicker. */
39
- #define TOLERANCE_US (DELAY ) (600 + DELAY / 20 )
40
-
50
+ #define TOLERANCE (DELAY ) (600us + DELAY / 20 )
41
51
52
+ static const int test_timeout = 10 ;
42
53
volatile uint32_t ticker_callback_flag;
43
54
volatile uint32_t multi_counter;
44
55
Timer gtimer;
@@ -73,14 +84,14 @@ void increment_multi_counter(void)
73
84
void test_multi_ticker (void )
74
85
{
75
86
LowPowerTicker ticker[TICKER_COUNT];
76
- const uint32_t extra_wait = 10 ; // extra 10ms wait time
87
+ const milliseconds extra_wait = 10ms ; // extra 10ms wait time
77
88
78
89
multi_counter = 0 ;
79
90
for (int i = 0 ; i < TICKER_COUNT; i++) {
80
- ticker[i].attach_us (callback (increment_multi_counter), MULTI_TICKER_TIME_MS * 1000 );
91
+ ticker[i].attach (callback (increment_multi_counter), MULTI_TICKER_TIME );
81
92
}
82
93
83
- ThisThread::sleep_for (MULTI_TICKER_TIME_MS + extra_wait);
94
+ ThisThread::sleep_for (MULTI_TICKER_TIME + extra_wait);
84
95
TEST_ASSERT_EQUAL (TICKER_COUNT, multi_counter);
85
96
86
97
for (int i = 0 ; i < TICKER_COUNT; i++) {
@@ -93,10 +104,10 @@ void test_multi_ticker(void)
93
104
94
105
multi_counter = 0 ;
95
106
for (int i = 0 ; i < TICKER_COUNT; i++) {
96
- ticker[i].attach_us (callback (increment_multi_counter), (MULTI_TICKER_TIME_MS + i) * 1000 );
107
+ ticker[i].attach (callback (increment_multi_counter), MULTI_TICKER_TIME + milliseconds{i} );
97
108
}
98
109
99
- ThisThread::sleep_for (MULTI_TICKER_TIME_MS + TICKER_COUNT + extra_wait);
110
+ ThisThread::sleep_for (MULTI_TICKER_TIME + milliseconds{ TICKER_COUNT} + extra_wait);
100
111
TEST_ASSERT_EQUAL (TICKER_COUNT, multi_counter);
101
112
102
113
for (int i = 0 ; i < TICKER_COUNT; i++) {
@@ -117,19 +128,18 @@ void test_multi_ticker(void)
117
128
void test_multi_call_time (void )
118
129
{
119
130
LowPowerTicker ticker;
120
- int time_diff;
121
131
const int attach_count = 10 ;
122
132
123
133
for (int i = 0 ; i < attach_count; i++) {
124
134
ticker_callback_flag = 0 ;
125
135
gtimer.reset ();
126
136
127
137
gtimer.start ();
128
- ticker.attach_us (callback (stop_gtimer_set_flag), MULTI_TICKER_TIME_MS * 1000 );
138
+ ticker.attach (callback (stop_gtimer_set_flag), MULTI_TICKER_TIME );
129
139
while (!ticker_callback_flag);
130
- time_diff = gtimer.read_us ();
140
+ const auto time_diff = gtimer.elapsed_time ();
131
141
132
- TEST_ASSERT_UINT32_WITHIN ( TOLERANCE_US (MULTI_TICKER_TIME_MS * 1000 ), MULTI_TICKER_TIME_MS * 1000 , time_diff);
142
+ TEST_ASSERT_DURATION_WITHIN ( TOLERANCE (MULTI_TICKER_TIME ), MULTI_TICKER_TIME , time_diff);
133
143
}
134
144
}
135
145
@@ -142,20 +152,16 @@ void test_multi_call_time(void)
142
152
void test_detach (void )
143
153
{
144
154
LowPowerTicker ticker;
145
- bool ret;
146
- const float ticker_time_s = 0 .1f ;
147
- const uint32_t wait_time_ms = 500 ;
148
155
Semaphore sem (0 , 1 );
149
156
150
- ticker.attach (callback (sem_release, &sem), ticker_time_s );
157
+ ticker.attach (callback (sem_release, &sem), 100ms );
151
158
152
159
sem.acquire ();
153
160
154
161
sem.acquire ();
155
162
ticker.detach (); /* cancel */
156
163
157
- ret = sem.try_acquire_for (wait_time_ms);
158
- TEST_ASSERT_FALSE (ret);
164
+ TEST_ASSERT_FALSE (sem.try_acquire_for (500ms));
159
165
}
160
166
161
167
/* * Test single callback time via attach
@@ -164,54 +170,29 @@ void test_detach(void)
164
170
When callback attached with time interval specified
165
171
Then ticker properly executes callback within a specified time interval
166
172
*/
167
- template <us_timestamp_t DELAY_US >
173
+ template <typename Duration, uint64_t DELAY >
168
174
void test_attach_time (void )
169
175
{
170
176
LowPowerTicker ticker;
171
177
ticker_callback_flag = 0 ;
178
+ const auto delay = Duration{DELAY};
172
179
173
180
gtimer.reset ();
174
181
gtimer.start ();
175
- ticker.attach (callback (stop_gtimer_set_flag), ((float )DELAY_US) / 1000000 .0f );
176
- while (!ticker_callback_flag);
177
- ticker.detach ();
178
- const int time_diff = gtimer.read_us ();
179
-
180
- TEST_ASSERT_UINT64_WITHIN (TOLERANCE_US (DELAY_US), DELAY_US, time_diff);
181
- }
182
-
183
- /* * Test single callback time via attach_us
184
-
185
- Given a Ticker
186
- When callback attached with time interval specified
187
- Then ticker properly executes callback within a specified time interval
188
- */
189
- template <us_timestamp_t DELAY_US>
190
- void test_attach_us_time (void )
191
- {
192
- LowPowerTicker ticker;
193
- ticker_callback_flag = 0 ;
194
-
195
- gtimer.reset ();
196
- gtimer.start ();
197
- ticker.attach_us (callback (stop_gtimer_set_flag), DELAY_US);
182
+ ticker.attach (callback (stop_gtimer_set_flag), delay);
198
183
while (!ticker_callback_flag);
199
184
ticker.detach ();
200
- const int time_diff = gtimer.read_us ();
185
+ const auto time_diff = gtimer.elapsed_time ();
201
186
202
- TEST_ASSERT_UINT64_WITHIN ( TOLERANCE_US (DELAY_US ), DELAY_US , time_diff);
187
+ TEST_ASSERT_DURATION_WITHIN ( TOLERANCE (delay ), delay , time_diff);
203
188
}
204
189
205
190
// Test cases
206
191
Case cases[] = {
207
- Case (" Test attach for 0.001s and time measure" , test_attach_time<1000 >),
208
- Case (" Test attach_us for 1ms and time measure" , test_attach_us_time<1000 >),
209
- Case (" Test attach for 0.01s and time measure" , test_attach_time<10000 >),
210
- Case (" Test attach_us for 10ms and time measure" , test_attach_us_time<10000 >),
211
- Case (" Test attach for 0.1s and time measure" , test_attach_time<100000 >),
212
- Case (" Test attach_us for 100ms and time measure" , test_attach_us_time<100000 >),
213
- Case (" Test attach for 0.5s and time measure" , test_attach_time<500000 >),
214
- Case (" Test attach_us for 500ms and time measure" , test_attach_us_time<500000 >),
192
+ Case (" Test attach for 1ms and time measure" , test_attach_time<milliseconds, 1 >),
193
+ Case (" Test attach for 10ms and time measure" , test_attach_time<milliseconds, 10 >),
194
+ Case (" Test attach for 100ms and time measure" , test_attach_time<milliseconds, 100 >),
195
+ Case (" Test attach for 500ms and time measure" , test_attach_time<milliseconds, 500 >),
215
196
Case (" Test detach" , test_detach),
216
197
Case (" Test multi call and time measure" , test_multi_call_time),
217
198
Case (" Test multi ticker" , test_multi_ticker),
0 commit comments