Skip to content

Commit 0a701cc

Browse files
Greenta: Remove deprecated APIs and use chrono
References to time should do so using std::chrono. We reworked tests in connectivity and drivers to use std::chrono and new APIs in order to remove deprecation warnings resulting from deprecated API calls. This required addition of a macro for test assertions using std::chrono values. As host test "timing_drift_auto" expects time values represented as an integral number of microseconds, we explicitly provide this in place using "microseconds{TICKER_TIME}.count()" in the relevant ticker tests. We recognise this is ugly, but thought it best to descriptively convert from std::chrono to the host test's required representation. Co-authored-by: Hari Limaye <hari.limaye@arm.com>
1 parent bfe24ce commit 0a701cc

File tree

3 files changed

+86
-109
lines changed
  • connectivity/FEATURE_BLE/source/cordio/TESTS/cordio_hci/driver
  • drivers/tests/TESTS/mbed_drivers

3 files changed

+86
-109
lines changed

connectivity/FEATURE_BLE/source/cordio/TESTS/cordio_hci/driver/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737

3838
using namespace utest::v1;
3939
using mbed::callback;
40+
using namespace std::chrono_literals;
4041

41-
#define INITIALIZATION_TIMEOUT (10 * 1000)
42+
#define INITIALIZATION_TIMEOUT 10000ms
4243

4344
static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
4445

@@ -72,7 +73,7 @@ static void test_stack_initialization()
7273
BLE &ble = BLE::Instance();
7374
ble.onEventsToProcess(process_ble_events);
7475
ble.init(on_initialization_complete);
75-
event_queue.dispatch(INITIALIZATION_TIMEOUT);
76+
event_queue.dispatch_for(INITIALIZATION_TIMEOUT);
7677

7778
// At this point ble is suppose to be initialized; inspect the various state
7879
// of the stack.

drivers/tests/TESTS/mbed_drivers/lp_ticker/main.cpp

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@
2525
#else
2626

2727
using utest::v1::Case;
28+
using namespace std::chrono;
2829

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)
3041

3142
#define TICKER_COUNT 16
32-
#define MULTI_TICKER_TIME_MS 100
43+
#define MULTI_TICKER_TIME 100ms
3344

3445
/* Due to poor accuracy of LowPowerTicker on many platforms
3546
there is no sense to tune tolerance value as it was in Ticker tests.
3647
3748
Tolerance value is set to 600us for measurement inaccuracy + 5% tolerance
3849
for LowPowerTicker. */
39-
#define TOLERANCE_US(DELAY) (600 + DELAY / 20)
40-
50+
#define TOLERANCE_US(DELAY) (600us + DELAY / 20)
4151

52+
static const int test_timeout = 10;
4253
volatile uint32_t ticker_callback_flag;
4354
volatile uint32_t multi_counter;
4455
Timer gtimer;
@@ -73,14 +84,14 @@ void increment_multi_counter(void)
7384
void test_multi_ticker(void)
7485
{
7586
LowPowerTicker ticker[TICKER_COUNT];
76-
const uint32_t extra_wait = 10; // extra 10ms wait time
87+
milliseconds extra_wait = 10ms; // extra 10ms wait time
7788

7889
multi_counter = 0;
7990
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);
8192
}
8293

83-
ThisThread::sleep_for(MULTI_TICKER_TIME_MS + extra_wait);
94+
ThisThread::sleep_for(MULTI_TICKER_TIME + extra_wait);
8495
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
8596

8697
for (int i = 0; i < TICKER_COUNT; i++) {
@@ -93,10 +104,10 @@ void test_multi_ticker(void)
93104

94105
multi_counter = 0;
95106
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});
97108
}
98109

99-
ThisThread::sleep_for(MULTI_TICKER_TIME_MS + TICKER_COUNT + extra_wait);
110+
ThisThread::sleep_for(MULTI_TICKER_TIME + milliseconds{TICKER_COUNT} + extra_wait);
100111
TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
101112

102113
for (int i = 0; i < TICKER_COUNT; i++) {
@@ -117,19 +128,18 @@ void test_multi_ticker(void)
117128
void test_multi_call_time(void)
118129
{
119130
LowPowerTicker ticker;
120-
int time_diff;
121131
const int attach_count = 10;
122132

123133
for (int i = 0; i < attach_count; i++) {
124134
ticker_callback_flag = 0;
125135
gtimer.reset();
126136

127137
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);
129139
while (!ticker_callback_flag);
130-
time_diff = gtimer.read_us();
140+
auto time_diff = gtimer.elapsed_time();
131141

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_US(MULTI_TICKER_TIME), MULTI_TICKER_TIME, time_diff);
133143
}
134144
}
135145

@@ -143,18 +153,18 @@ void test_detach(void)
143153
{
144154
LowPowerTicker ticker;
145155
bool ret;
146-
const float ticker_time_s = 0.1f;
147-
const uint32_t wait_time_ms = 500;
156+
const milliseconds ticker_time = 100ms;
157+
const milliseconds wait_time = 500ms;
148158
Semaphore sem(0, 1);
149159

150-
ticker.attach(callback(sem_release, &sem), ticker_time_s);
160+
ticker.attach(callback(sem_release, &sem), ticker_time);
151161

152162
sem.acquire();
153163

154164
sem.acquire();
155165
ticker.detach(); /* cancel */
156166

157-
ret = sem.try_acquire_for(wait_time_ms);
167+
ret = sem.try_acquire_for(wait_time);
158168
TEST_ASSERT_FALSE(ret);
159169
}
160170

@@ -164,54 +174,29 @@ void test_detach(void)
164174
When callback attached with time interval specified
165175
Then ticker properly executes callback within a specified time interval
166176
*/
167-
template<us_timestamp_t DELAY_US>
177+
template<typename Duration, uint64_t DELAY>
168178
void test_attach_time(void)
169179
{
170180
LowPowerTicker ticker;
171181
ticker_callback_flag = 0;
182+
auto delay = Duration{DELAY};
172183

173184
gtimer.reset();
174185
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);
186+
ticker.attach(callback(stop_gtimer_set_flag), delay);
198187
while (!ticker_callback_flag);
199188
ticker.detach();
200-
const int time_diff = gtimer.read_us();
189+
auto time_diff = gtimer.elapsed_time();
201190

202-
TEST_ASSERT_UINT64_WITHIN(TOLERANCE_US(DELAY_US), DELAY_US, time_diff);
191+
TEST_ASSERT_DURATION_WITHIN(TOLERANCE_US(delay), delay, time_diff);
203192
}
204193

205194
// Test cases
206195
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>),
196+
Case("Test attach for 1ms and time measure", test_attach_time<milliseconds, 1>),
197+
Case("Test attach for 10ms and time measure", test_attach_time<milliseconds, 10>),
198+
Case("Test attach for 100ms and time measure", test_attach_time<milliseconds, 100>),
199+
Case("Test attach for 500ms and time measure", test_attach_time<milliseconds, 500>),
215200
Case("Test detach", test_detach),
216201
Case("Test multi call and time measure", test_multi_call_time),
217202
Case("Test multi ticker", test_multi_ticker),

0 commit comments

Comments
 (0)