Skip to content

Commit 5601a20

Browse files
committed
Fix and update tests to use Chronos APIs
1 parent b17355f commit 5601a20

File tree

10 files changed

+134
-169
lines changed

10 files changed

+134
-169
lines changed

TESTS/mbedmicro-rtos-mbed/basic/main.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@
2828
#else
2929

3030
using utest::v1::Case;
31+
using std::milli;
32+
using std::micro;
33+
using namespace std::chrono;
3134

3235
#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
3336
#define TEST_STACK_SIZE 512
3437
#else
3538
#define TEST_STACK_SIZE 256
3639
#endif
37-
#define ONE_MILLI_SEC 1000
3840

39-
volatile uint32_t elapsed_time_ms = 0;
41+
static duration<uint32_t, milli> elapsed_time_ms;
4042
static const int test_timeout = 40;
4143

4244

4345
void update_tick_thread(Mutex *mutex)
4446
{
4547
while (true) {
46-
ThisThread::sleep_for(1);
48+
ThisThread::sleep_for(1ms);
4749
mutex->lock();
4850
++elapsed_time_ms;
4951
mutex->unlock();
@@ -69,7 +71,7 @@ void test(void)
6971
char _value[128] = { };
7072
int expected_key = 1;
7173
Mutex mutex;
72-
uint32_t elapsed_time;
74+
duration<uint32_t, micro> elapsed_time;
7375

7476
Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
7577
tick_thread.start(callback(update_tick_thread, &mutex));
@@ -86,7 +88,7 @@ void test(void)
8688
elapsed_time = elapsed_time_ms;
8789
mutex.unlock();
8890
// send base_time
89-
greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
91+
greentea_send_kv(_key, elapsed_time.count());
9092

9193
// wait for 2nd signal from host
9294
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
@@ -95,7 +97,7 @@ void test(void)
9597
elapsed_time = elapsed_time_ms;
9698
mutex.unlock();
9799
// send final_time
98-
greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
100+
greentea_send_kv(_key, elapsed_time.count());
99101

100102
//get the results from host
101103
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));

TESTS/mbedmicro-rtos-mbed/condition_variable/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
#include "rtos.h"
3030

3131
using namespace utest::v1;
32+
using namespace std::chrono_literals;
3233

3334
#define TEST_STACK_SIZE 512
34-
#define TEST_DELAY 10
35+
#define TEST_DELAY 10ms
3536

3637
static int change_counter = 0;
3738
static Mutex mutex;

TESTS/mbedmicro-rtos-mbed/event_flags/main.cpp

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "utest/utest.h"
2222

2323
using utest::v1::Case;
24+
using namespace std::chrono;
2425

2526
#if !DEVICE_USTICKER
2627
#error [NOT_SUPPORTED] UsTicker need to be enabled for this test.
@@ -44,14 +45,14 @@ using utest::v1::Case;
4445
#define PROHIBITED_FLAG 0x80000000 /* 10000000000000000000000000000000 */
4546
#define NO_FLAGS 0x0
4647

47-
void send_thread(EventFlags *ef, uint32_t flags, uint32_t wait_ms)
48+
void send_thread(EventFlags *ef, uint32_t flags, milliseconds wait)
4849
{
4950
for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
5051
const uint32_t flag = flags & (1 << i);
5152
if (flag) {
5253
ef->set(flag);
53-
if (wait_ms != 0) {
54-
ThisThread::sleep_for(wait_ms);
54+
if (wait != 0ms) {
55+
ThisThread::sleep_for(wait);
5556
}
5657
}
5758
}
@@ -60,21 +61,19 @@ void send_thread(EventFlags *ef, uint32_t flags, uint32_t wait_ms)
6061
#if defined(MBED_CONF_RTOS_PRESENT)
6162
Semaphore sync_sem(0, 1);
6263

63-
template<uint32_t flags, uint32_t wait_ms>
64-
void send_thread_sync(EventFlags *ef)
64+
void send_thread_sync(EventFlags *ef, uint32_t flags, milliseconds wait)
6565
{
6666
for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
6767
const uint32_t flag = flags & (1 << i);
6868
if (flag) {
6969
sync_sem.acquire();
7070
ef->set(flag);
71-
ThisThread::sleep_for(wait_ms);
71+
ThisThread::sleep_for(wait);
7272
}
7373
}
7474
}
7575

76-
template<uint32_t flags>
77-
void wait_thread_all(EventFlags *ef)
76+
void wait_thread_all(EventFlags *ef, uint32_t flags)
7877
{
7978
uint32_t ret, flags_after_clear;
8079
ret = ef->wait_all(flags);
@@ -218,9 +217,9 @@ void test_multi_thread_all(void)
218217
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
219218
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
220219
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
221-
thread1.start([&] { send_thread(&ef, FLAG01, 1); });
222-
thread2.start([&] { send_thread(&ef, FLAG02, 2); });
223-
thread3.start([&] { send_thread(&ef, FLAG03, 3); });
220+
thread1.start([&] { send_thread(&ef, FLAG01, 1ms); });
221+
thread2.start([&] { send_thread(&ef, FLAG02, 2ms); });
222+
thread3.start([&] { send_thread(&ef, FLAG03, 3ms); });
224223

225224
uint32_t ret = ef.wait_all(FLAG01 | FLAG02 | FLAG03);
226225
TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, ret);
@@ -239,9 +238,9 @@ void test_multi_thread_any(void)
239238
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
240239
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
241240
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
242-
thread1.start([&] { send_thread(&ef, FLAG01, 1); });
243-
thread2.start([&] { send_thread(&ef, FLAG02, 1); });
244-
thread3.start([&] { send_thread(&ef, FLAG03, 1); });
241+
thread1.start([&] { send_thread(&ef, FLAG01, 1ms); });
242+
thread2.start([&] { send_thread(&ef, FLAG02, 1ms); });
243+
thread3.start([&] { send_thread(&ef, FLAG03, 1ms); });
245244

246245
for (int i = 0; i <= MAX_FLAG_POS; i++) {
247246
uint32_t flag = 1 << i;
@@ -265,16 +264,16 @@ void test_multi_thread_any_timeout(void)
265264
EventFlags ef;
266265
uint32_t ret;
267266
Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
268-
thread.start(callback(send_thread_sync < FLAG01 | FLAG02 | FLAG03, 1 >, &ef));
267+
thread.start([&] { send_thread_sync(&ef, FLAG01 | FLAG02 | FLAG03, 1ms); });
269268

270269
for (int i = 0; i <= MAX_FLAG_POS; i++) {
271270
uint32_t flag = 1 << i;
272271

273-
ret = ef.wait_any(flag, 10);
272+
ret = ef.wait_any_for(flag, 10ms);
274273
TEST_ASSERT_EQUAL(osFlagsErrorTimeout, ret);
275274

276275
sync_sem.release();
277-
ret = ef.wait_any(flag, 10);
276+
ret = ef.wait_any_for(flag, 10ms);
278277
TEST_ASSERT_EQUAL(flag, ret);
279278
}
280279
ret = ef.get();
@@ -294,13 +293,13 @@ void test_multi_thread_any_no_clear(void)
294293
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
295294
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
296295
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
297-
thread1.start([&] { send_thread(&ef, FLAG01, 1); });
298-
thread2.start([&] { send_thread(&ef, FLAG02, 1); });
299-
thread3.start([&] { send_thread(&ef, FLAG03, 1); });
296+
thread1.start([&] { send_thread(&ef, FLAG01, 1ms); });
297+
thread2.start([&] { send_thread(&ef, FLAG02, 1ms); });
298+
thread3.start([&] { send_thread(&ef, FLAG03, 1ms); });
300299

301300
for (int i = 0; i <= MAX_FLAG_POS; i++) {
302301
uint32_t flag = 1 << i;
303-
ret = ef.wait_any(flag, osWaitForever, false);
302+
ret = ef.wait_any_for(flag, Kernel::wait_for_u32_forever, false);
304303
TEST_ASSERT(flag | ret);
305304
ret = ef.clear(flag);
306305
TEST_ASSERT(ret < osFlagsError);
@@ -322,9 +321,9 @@ void test_multi_thread_all_many_wait(void)
322321
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
323322
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
324323
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
325-
thread1.start(callback(wait_thread_all<FLAG01>, &ef));
326-
thread2.start(callback(wait_thread_all<FLAG02>, &ef));
327-
thread3.start(callback(wait_thread_all<FLAG03>, &ef));
324+
thread1.start([&] { wait_thread_all(&ef, FLAG01); });
325+
thread2.start([&] { wait_thread_all(&ef, FLAG02); });
326+
thread3.start([&] { wait_thread_all(&ef, FLAG03); });
328327

329328
ef.set(FLAG01 | FLAG02 | FLAG03);
330329
thread1.join();
@@ -337,9 +336,9 @@ void test_multi_thread_all_many_wait(void)
337336
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
338337
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
339338
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
340-
thread1.start(callback(wait_thread_all<FLAG01>, &ef));
341-
thread2.start(callback(wait_thread_all<FLAG02>, &ef));
342-
thread3.start(callback(wait_thread_all<FLAG03>, &ef));
339+
thread1.start([&] { wait_thread_all(&ef, FLAG01); });
340+
thread2.start([&] { wait_thread_all(&ef, FLAG02); });
341+
thread3.start([&] { wait_thread_all(&ef, FLAG03); });
343342

344343
ef.set(FLAG01);
345344
thread1.join();
@@ -362,10 +361,10 @@ void test_multi_eventflags_all(void)
362361
{
363362
EventFlags ef;
364363
Ticker t1, t2, t3;
365-
t1.attach_us([&] { send_thread(&ef, FLAG01, 0); }, 3000);
366-
t2.attach_us([&] { send_thread(&ef, FLAG02, 0); }, 4000);
367-
t3.attach_us([&] { send_thread(&ef, FLAG03, 0); }, 5000);
368-
uint32_t ret = ef.wait_all(FLAG01 | FLAG02 | FLAG03, 20, false);
364+
t1.attach([&] { send_thread(&ef, FLAG01, 0ms); }, 3ms);
365+
t2.attach([&] { send_thread(&ef, FLAG02, 0ms); }, 4ms);
366+
t3.attach([&] { send_thread(&ef, FLAG03, 0ms); }, 5ms);
367+
uint32_t ret = ef.wait_all_for(FLAG01 | FLAG02 | FLAG03, 20ms, false);
369368
TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, ret);
370369
}
371370

@@ -380,9 +379,9 @@ void test_multi_eventflags_any(void)
380379
EventFlags ef;
381380
uint32_t ret;
382381
Ticker t1, t2, t3;
383-
t1.attach_us([&] { send_thread(&ef, FLAG01, 0); }, 3000);
384-
t2.attach_us([&] { send_thread(&ef, FLAG02, 0); }, 4000);
385-
t3.attach_us([&] { send_thread(&ef, FLAG03, 0); }, 5000);
382+
t1.attach([&] { send_thread(&ef, FLAG01, 0ms); }, 3ms);
383+
t2.attach([&] { send_thread(&ef, FLAG02, 0ms); }, 4ms);
384+
t3.attach([&] { send_thread(&ef, FLAG03, 0ms); }, 5ms);
386385

387386
for (int i = 0; i <= MAX_FLAG_POS; i++) {
388387
uint32_t flag = 1 << i;
@@ -404,9 +403,9 @@ void test_multi_eventflags_any_no_clear(void)
404403
EventFlags ef;
405404
uint32_t ret;
406405
Ticker t1, t2, t3;
407-
t1.attach_us([&] { send_thread(&ef, FLAG01, 0); }, 3000);
408-
t2.attach_us([&] { send_thread(&ef, FLAG02, 0); }, 4000);
409-
t3.attach_us([&] { send_thread(&ef, FLAG03, 0); }, 5000);
406+
t1.attach([&] { send_thread(&ef, FLAG01, 0ms); }, 3ms);
407+
t2.attach([&] { send_thread(&ef, FLAG02, 0ms); }, 4ms);
408+
t3.attach([&] { send_thread(&ef, FLAG03, 0ms); }, 5ms);
410409

411410
for (int i = 0; i <= MAX_FLAG_POS; i++) {
412411
uint32_t flag = 1 << i;

TESTS/mbedmicro-rtos-mbed/kernel_tick_count/main.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121
#include "rtos/Kernel.h"
2222
#include "mbed.h"
2323

24+
using namespace std::chrono_literals;
2425

2526
using utest::v1::Case;
2627

2728
#define TEST_REPEAT_COUNT 1000
28-
#define NUM_WAIT_TICKS 1000
29+
#define NUM_WAIT_TICKS rtos::Kernel::Clock::duration(1s)
2930

30-
// all in [us]
31-
#define ONE_SECOND 1000000
32-
#define SMALL_DELTA 1500 // 0.15%
33-
#define BIG_DELTA 15000 // 1.5%
31+
#define ONE_SECOND Timer::duration(1s)
32+
#define SMALL_DELTA Timer::duration(1500us) // 0.15%
33+
#define BIG_DELTA Timer::duration(15000us) // 1.5%
3434

35-
#if defined(MBED_CONF_RTOS_PRESENT)
3635
/** Test if kernel ticker frequency is 1kHz
3736
3837
Given a RTOS kernel ticker
@@ -41,25 +40,27 @@ using utest::v1::Case;
4140
*/
4241
void test_frequency()
4342
{
43+
#if defined(MBED_CONF_RTOS_PRESENT)
4444
uint32_t freq = osKernelGetTickFreq();
4545
TEST_ASSERT_EQUAL_UINT32_MESSAGE(1000, freq, "Expected SysTick frequency is 1kHz");
46-
}
4746
#endif
47+
TEST_ASSERT_TRUE_MESSAGE((std::ratio_equal<rtos::Kernel::Clock::period, std::milli>::value), "Expected Kernel::Clock frequency is 1kHz");
48+
}
4849

4950
/** Test if kernel ticker increments by one
5051
5152
Given a RTOS kernel ticker
52-
When perform subsequent calls of @a rtos::Kernel::get_ms_count
53+
When perform subsequent calls of @a rtos::Kernel::Clock::now
5354
Then subsequent reads should not differ by more than one
5455
*/
5556
void test_increment(void)
5657
{
5758
for (uint32_t i = 0; i < TEST_REPEAT_COUNT; i++) {
58-
const uint64_t start = rtos::Kernel::get_ms_count();
59+
auto start = rtos::Kernel::Clock::now();
5960
while (true) {
60-
uint64_t diff = rtos::Kernel::get_ms_count() - start;
61-
if (diff != 0) {
62-
TEST_ASSERT_EQUAL_UINT64(1, diff);
61+
rtos::Kernel::Clock::duration diff = rtos::Kernel::Clock::now() - start;
62+
if (diff.count() != 0) {
63+
TEST_ASSERT_EQUAL_INT64(1, diff.count());
6364
break;
6465
}
6566
}
@@ -69,43 +70,41 @@ void test_increment(void)
6970
/** Test if kernel ticker interval is 1ms
7071
7172
Given a RTOS kernel ticker
72-
When perform subsequent calls of @a rtos::Kernel::get_ms_count
73+
When perform subsequent calls of @a rtos::Kernel::Clock::now
7374
Then the ticker interval should be 1ms
7475
*/
7576
void test_interval()
7677
{
77-
uint64_t start, stop;
78+
Kernel::Clock::time_point start, stop;
7879
Timer timer;
7980

80-
start = rtos::Kernel::get_ms_count();
81+
start = rtos::Kernel::Clock::now();
8182
// wait for tick
8283
do {
83-
stop = rtos::Kernel::get_ms_count();
84-
} while ((stop - start) == 0);
84+
stop = rtos::Kernel::Clock::now();
85+
} while (stop == start);
8586
timer.start();
8687
start = stop;
8788

8889
// wait for NUM_WAIT_TICKS ticks
8990
do {
90-
stop = rtos::Kernel::get_ms_count();
91+
stop = rtos::Kernel::Clock::now();
9192
} while ((stop - start) != NUM_WAIT_TICKS);
9293
timer.stop();
93-
TEST_ASSERT_EQUAL_UINT64(NUM_WAIT_TICKS, (stop - start));
94+
TEST_ASSERT_EQUAL_INT64(NUM_WAIT_TICKS.count(), (stop - start).count());
9495

9596
#if defined(NO_SYSTICK) || defined(MBED_TICKLESS)
9697
// On targets with NO_SYSTICK/MBED_TICKLESS enabled, systick is emulated by lp_ticker what makes it less accurate
9798
// for more details https://os.mbed.com/docs/latest/reference/tickless.html
98-
TEST_ASSERT_UINT64_WITHIN(BIG_DELTA, ONE_SECOND, timer.read_high_resolution_us());
99+
TEST_ASSERT_INT64_WITHIN(BIG_DELTA.count(), ONE_SECOND.count(), timer.read_duration().count());
99100
#else
100-
TEST_ASSERT_UINT64_WITHIN(SMALL_DELTA, ONE_SECOND, timer.read_high_resolution_us());
101+
TEST_ASSERT_INT64_WITHIN(SMALL_DELTA.count(), ONE_SECOND.count(), timer.read_duration().count());
101102
#endif
102103
}
103104

104105
// Test cases
105106
Case cases[] = {
106-
#if defined(MBED_CONF_RTOS_PRESENT)
107107
Case("Test kernel ticker frequency", test_frequency),
108-
#endif
109108
Case("Test if kernel ticker increments by one", test_increment),
110109
Case("Test if kernel ticker interval is 1ms", test_interval)
111110
};

TESTS/mbedmicro-rtos-mbed/mutex/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929
#include "rtos.h"
3030

3131
using namespace utest::v1;
32+
using namespace std::chrono;
3233

3334
#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
3435
#define TEST_STACK_SIZE 768
3536
#else
3637
#define TEST_STACK_SIZE 512
3738
#endif
3839

39-
#define TEST_LONG_DELAY 20
40-
#define TEST_DELAY 10
40+
#define TEST_LONG_DELAY 20ms
41+
#define TEST_DELAY 10ms
4142
#define SIGNALS_TO_EMIT 100
4243

4344
Mutex stdio_mutex;
@@ -89,9 +90,9 @@ void test_thread(int const *thread_delay)
8990
*/
9091
void test_multiple_threads(void)
9192
{
92-
const int t1_delay = TEST_DELAY * 1;
93-
const int t2_delay = TEST_DELAY * 2;
94-
const int t3_delay = TEST_DELAY * 3;
93+
const Kernel::Clock::duration t1_delay = TEST_DELAY * 1;
94+
const Kernel::Clock::duration t2_delay = TEST_DELAY * 2;
95+
const Kernel::Clock::duration t3_delay = TEST_DELAY * 3;
9596

9697
Thread t2(osPriorityNormal, TEST_STACK_SIZE);
9798
Thread t3(osPriorityNormal, TEST_STACK_SIZE);

0 commit comments

Comments
 (0)