Skip to content

Commit 24d39ae

Browse files
committed
Fix and update tests to use Chronos APIs
1 parent 08ed067 commit 24d39ae

File tree

10 files changed

+74
-131
lines changed

10 files changed

+74
-131
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
#include "rtos.h"
2929

3030
using namespace utest::v1;
31+
using namespace std::chrono_literals;
3132

3233
#define TEST_STACK_SIZE 512
33-
#define TEST_DELAY 10
34+
#define TEST_DELAY 10ms
3435

3536
static int change_counter = 0;
3637
static Mutex mutex;
@@ -55,14 +56,14 @@ void test_notify_one()
5556
t1.start(increment_on_signal);
5657
t2.start(increment_on_signal);
5758

58-
wait_ms(TEST_DELAY);
59+
ThisThread::sleep_for(TEST_DELAY);
5960
TEST_ASSERT_EQUAL(0, change_counter);
6061

6162
mutex.lock();
6263
cond.notify_one();
6364
mutex.unlock();
6465

65-
wait_ms(TEST_DELAY);
66+
ThisThread::sleep_for(TEST_DELAY);
6667
TEST_ASSERT_EQUAL(1, change_counter);
6768

6869
mutex.lock();
@@ -82,14 +83,14 @@ void test_notify_all()
8283
t1.start(increment_on_signal);
8384
t2.start(increment_on_signal);
8485

85-
wait_ms(TEST_DELAY);
86+
ThisThread::sleep_for(TEST_DELAY);
8687
TEST_ASSERT_EQUAL(0, change_counter);
8788

8889
mutex.lock();
8990
cond.notify_all();
9091
mutex.unlock();
9192

92-
wait_ms(TEST_DELAY);
93+
ThisThread::sleep_for(TEST_DELAY);
9394
TEST_ASSERT_EQUAL(2, change_counter);
9495

9596
t1.join();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void send_thread(EventFlags *ef)
5454
const uint32_t flag = flags & (1 << i);
5555
if (flag) {
5656
ef->set(flag);
57-
ThisThread::sleep_for(wait_ms);
57+
ThisThread::sleep_for(std::chrono::milliseconds(wait_ms));
5858
}
5959
}
6060
}
@@ -67,7 +67,7 @@ void send_thread_sync(EventFlags *ef)
6767
if (flag) {
6868
sync_sem.acquire();
6969
ef->set(flag);
70-
ThisThread::sleep_for(wait_ms);
70+
ThisThread::sleep_for(std::chrono::milliseconds(wait_ms));
7171
}
7272
}
7373
}
@@ -267,11 +267,11 @@ void test_multi_thread_any_timeout(void)
267267
for (int i = 0; i <= MAX_FLAG_POS; i++) {
268268
uint32_t flag = 1 << i;
269269

270-
ret = ef.wait_any(flag, 10);
270+
ret = ef.wait_any_for(flag, 10ms);
271271
TEST_ASSERT_EQUAL(osFlagsErrorTimeout, ret);
272272

273273
sync_sem.release();
274-
ret = ef.wait_any(flag, 10);
274+
ret = ef.wait_any_for(flag, 10ms);
275275
TEST_ASSERT_EQUAL(flag, ret);
276276
}
277277
ret = ef.get();
@@ -297,7 +297,7 @@ void test_multi_thread_any_no_clear(void)
297297

298298
for (int i = 0; i <= MAX_FLAG_POS; i++) {
299299
uint32_t flag = 1 << i;
300-
ret = ef.wait_any(flag, osWaitForever, false);
300+
ret = ef.wait_any(flag, Kernel::wait_for_u32_forever, false);
301301
TEST_ASSERT(flag | ret);
302302
ret = ef.clear(flag);
303303
TEST_ASSERT(ret < osFlagsError);
@@ -375,4 +375,4 @@ int main()
375375
}
376376

377377
#endif // !DEVICE_USTICKER
378-
#endif // defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
378+
#endif // defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
#include "rtos/Kernel.h"
2525
#include "mbed.h"
2626

27+
using namespace std::chrono_literals;
2728

2829
using utest::v1::Case;
2930

3031
#define TEST_REPEAT_COUNT 1000
31-
#define NUM_WAIT_TICKS 1000
32+
#define NUM_WAIT_TICKS rtos::Kernel::Clock::duration(1s)
3233

33-
// all in [us]
34-
#define ONE_SECOND 1000000
35-
#define SMALL_DELTA 1500 // 0.15%
36-
#define BIG_DELTA 15000 // 1.5%
34+
#define ONE_SECOND Timer::duration(1s)
35+
#define SMALL_DELTA Timer::duration(1500us) // 0.15%
36+
#define BIG_DELTA Timer::duration(15000us) // 1.5%
3737

3838
/** Test if kernel ticker frequency is 1kHz
3939
@@ -45,22 +45,23 @@ void test_frequency()
4545
{
4646
uint32_t freq = osKernelGetTickFreq();
4747
TEST_ASSERT_EQUAL_UINT32_MESSAGE(1000, freq, "Expected SysTick frequency is 1kHz");
48+
TEST_ASSERT_TRUE_MESSAGE((std::ratio_equal<rtos::Kernel::Clock::period, std::milli>::value), "Expected Kernel::Clock frequency is 1kHz");
4849
}
4950

5051
/** Test if kernel ticker increments by one
5152
5253
Given a RTOS kernel ticker
53-
When perform subsequent calls of @a rtos::Kernel::get_ms_count
54+
When perform subsequent calls of @a rtos::Kernel::Clock::now
5455
Then subsequent reads should not differ by more than one
5556
*/
5657
void test_increment(void)
5758
{
5859
for (uint32_t i = 0; i < TEST_REPEAT_COUNT; i++) {
59-
const uint64_t start = rtos::Kernel::get_ms_count();
60+
auto start = rtos::Kernel::Clock::now();
6061
while (true) {
61-
uint64_t diff = rtos::Kernel::get_ms_count() - start;
62-
if (diff != 0) {
63-
TEST_ASSERT_EQUAL_UINT64(1, diff);
62+
rtos::Kernel::Clock::duration diff = rtos::Kernel::Clock::now() - start;
63+
if (diff.count() != 0) {
64+
TEST_ASSERT_EQUAL_INT64(1, diff.count());
6465
break;
6566
}
6667
}
@@ -70,35 +71,35 @@ void test_increment(void)
7071
/** Test if kernel ticker interval is 1ms
7172
7273
Given a RTOS kernel ticker
73-
When perform subsequent calls of @a rtos::Kernel::get_ms_count
74+
When perform subsequent calls of @a rtos::Kernel::Clock::now
7475
Then the ticker interval should be 1ms
7576
*/
7677
void test_interval()
7778
{
78-
uint64_t start, stop;
79+
Kernel::Clock::time_point start, stop;
7980
Timer timer;
8081

81-
start = rtos::Kernel::get_ms_count();
82+
start = rtos::Kernel::Clock::now();
8283
// wait for tick
8384
do {
84-
stop = rtos::Kernel::get_ms_count();
85-
} while ((stop - start) == 0);
85+
stop = rtos::Kernel::Clock::now();
86+
} while (stop == start);
8687
timer.start();
8788
start = stop;
8889

8990
// wait for NUM_WAIT_TICKS ticks
9091
do {
91-
stop = rtos::Kernel::get_ms_count();
92+
stop = rtos::Kernel::Clock::now();
9293
} while ((stop - start) != NUM_WAIT_TICKS);
9394
timer.stop();
94-
TEST_ASSERT_EQUAL_UINT64(NUM_WAIT_TICKS, (stop - start));
95+
TEST_ASSERT_EQUAL_INT64(NUM_WAIT_TICKS.count(), (stop - start).count());
9596

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

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
#include "rtos.h"
2929

3030
using namespace utest::v1;
31+
using namespace std::chrono;
3132

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

38-
#define TEST_LONG_DELAY 20
39-
#define TEST_DELAY 10
39+
#define TEST_LONG_DELAY 20ms
40+
#define TEST_DELAY 10ms
4041
#define SIGNALS_TO_EMIT 100
4142

4243
Mutex stdio_mutex;
@@ -88,9 +89,9 @@ void test_thread(int const *thread_delay)
8889
*/
8990
void test_multiple_threads(void)
9091
{
91-
const int t1_delay = TEST_DELAY * 1;
92-
const int t2_delay = TEST_DELAY * 2;
93-
const int t3_delay = TEST_DELAY * 3;
92+
const Kernel::Clock::duration t1_delay = TEST_DELAY * 1;
93+
const Kernel::Clock::duration t2_delay = TEST_DELAY * 2;
94+
const Kernel::Clock::duration t3_delay = TEST_DELAY * 3;
9495

9596
Thread t2(osPriorityNormal, TEST_STACK_SIZE);
9697
Thread t3(osPriorityNormal, TEST_STACK_SIZE);
@@ -154,7 +155,7 @@ void test_dual_thread_nolock(void)
154155

155156
thread.start(callback(F, &mutex));
156157

157-
wait_ms(TEST_DELAY);
158+
ThisThread::sleep_for(TEST_DELAY);
158159
}
159160

160161
void test_dual_thread_lock_unlock_thread(Mutex *mutex)
@@ -183,7 +184,7 @@ void test_dual_thread_lock_unlock(void)
183184

184185
mutex.unlock();
185186

186-
wait_ms(TEST_DELAY);
187+
ThisThread::sleep_for(TEST_DELAY);
187188
}
188189

189190
void test_dual_thread_lock_trylock_thread(Mutex *mutex)
@@ -227,7 +228,7 @@ void test_dual_thread_lock(void)
227228

228229
thread.start(callback(F, &mutex));
229230

230-
wait_ms(TEST_LONG_DELAY);
231+
ThisThread::sleep_for(TEST_LONG_DELAY);
231232

232233
mutex.unlock();
233234
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
#include "rtos.h"
2929

3030
using namespace utest::v1;
31+
using namespace std::chrono;
3132

32-
#define THREAD_DELAY 30
33+
#define THREAD_DELAY 30ms
3334
#define SEMAPHORE_SLOTS 2
3435
#define SEM_CHANGES 100
35-
#define SHORT_WAIT 5
36+
#define SHORT_WAIT 5ms
3637

3738
#define THREAD_STACK_SIZE 320 /* larger stack cause out of heap memory on some 16kB RAM boards in multi thread test*/
3839

@@ -42,9 +43,9 @@ volatile int change_counter = 0;
4243
volatile int sem_counter = 0;
4344
volatile bool sem_defect = false;
4445

45-
void test_thread(int const *delay)
46+
void test_thread(rtos::Kernel::Clock::duration const *delay)
4647
{
47-
const int thread_delay = *delay;
48+
const auto thread_delay = *delay;
4849
while (true) {
4950
two_slots.acquire();
5051
sem_counter++;
@@ -67,9 +68,9 @@ void test_thread(int const *delay)
6768
*/
6869
void test_multi()
6970
{
70-
const int t1_delay = THREAD_DELAY * 1;
71-
const int t2_delay = THREAD_DELAY * 2;
72-
const int t3_delay = THREAD_DELAY * 3;
71+
const rtos::Kernel::Clock::duration t1_delay = THREAD_DELAY * 1;
72+
const rtos::Kernel::Clock::duration t2_delay = THREAD_DELAY * 2;
73+
const rtos::Kernel::Clock::duration t3_delay = THREAD_DELAY * 3;
7374

7475
Thread t1(osPriorityNormal, THREAD_STACK_SIZE);
7576
Thread t2(osPriorityNormal, THREAD_STACK_SIZE);
@@ -139,7 +140,7 @@ void test_single_thread()
139140

140141
void timeout_thread(Semaphore *sem)
141142
{
142-
bool acquired = sem->try_acquire_for(30);
143+
bool acquired = sem->try_acquire_for(30ms);
143144
TEST_ASSERT_FALSE(acquired);
144145
}
145146

0 commit comments

Comments
 (0)