Skip to content

Commit 8acbf58

Browse files
committed
Fix and update tests to use Chronos APIs
1 parent afc8868 commit 8acbf58

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
@@ -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;
@@ -56,14 +57,14 @@ void test_notify_one()
5657
t1.start(increment_on_signal);
5758
t2.start(increment_on_signal);
5859

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

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

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

6970
mutex.lock();
@@ -83,14 +84,14 @@ void test_notify_all()
8384
t1.start(increment_on_signal);
8485
t2.start(increment_on_signal);
8586

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

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

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

9697
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
@@ -25,16 +25,16 @@
2525
#include "rtos/Kernel.h"
2626
#include "mbed.h"
2727

28+
using namespace std::chrono_literals;
2829

2930
using utest::v1::Case;
3031

3132
#define TEST_REPEAT_COUNT 1000
32-
#define NUM_WAIT_TICKS 1000
33+
#define NUM_WAIT_TICKS rtos::Kernel::Clock::duration(1s)
3334

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

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

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

82-
start = rtos::Kernel::get_ms_count();
83+
start = rtos::Kernel::Clock::now();
8384
// wait for tick
8485
do {
85-
stop = rtos::Kernel::get_ms_count();
86-
} while ((stop - start) == 0);
86+
stop = rtos::Kernel::Clock::now();
87+
} while (stop == start);
8788
timer.start();
8889
start = stop;
8990

9091
// wait for NUM_WAIT_TICKS ticks
9192
do {
92-
stop = rtos::Kernel::get_ms_count();
93+
stop = rtos::Kernel::Clock::now();
9394
} while ((stop - start) != NUM_WAIT_TICKS);
9495
timer.stop();
95-
TEST_ASSERT_EQUAL_UINT64(NUM_WAIT_TICKS, (stop - start));
96+
TEST_ASSERT_EQUAL_INT64(NUM_WAIT_TICKS.count(), (stop - start).count());
9697

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

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

Lines changed: 9 additions & 8 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);
@@ -155,7 +156,7 @@ void test_dual_thread_nolock(void)
155156

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

158-
wait_ms(TEST_DELAY);
159+
ThisThread::sleep_for(TEST_DELAY);
159160
}
160161

161162
void test_dual_thread_lock_unlock_thread(Mutex *mutex)
@@ -184,7 +185,7 @@ void test_dual_thread_lock_unlock(void)
184185

185186
mutex.unlock();
186187

187-
wait_ms(TEST_DELAY);
188+
ThisThread::sleep_for(TEST_DELAY);
188189
}
189190

190191
void test_dual_thread_lock_trylock_thread(Mutex *mutex)
@@ -228,7 +229,7 @@ void test_dual_thread_lock(void)
228229

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

231-
wait_ms(TEST_LONG_DELAY);
232+
ThisThread::sleep_for(TEST_LONG_DELAY);
232233

233234
mutex.unlock();
234235
}

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

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

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

33-
#define THREAD_DELAY 30
34+
#define THREAD_DELAY 30ms
3435
#define SEMAPHORE_SLOTS 2
3536
#define SEM_CHANGES 100
36-
#define SHORT_WAIT 5
37+
#define SHORT_WAIT 5ms
3738

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

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

46-
void test_thread(int const *delay)
47+
void test_thread(rtos::Kernel::Clock::duration const *delay)
4748
{
48-
const int thread_delay = *delay;
49+
const auto thread_delay = *delay;
4950
while (true) {
5051
two_slots.acquire();
5152
sem_counter++;
@@ -68,9 +69,9 @@ void test_thread(int const *delay)
6869
*/
6970
void test_multi()
7071
{
71-
const int t1_delay = THREAD_DELAY * 1;
72-
const int t2_delay = THREAD_DELAY * 2;
73-
const int t3_delay = THREAD_DELAY * 3;
72+
const rtos::Kernel::Clock::duration t1_delay = THREAD_DELAY * 1;
73+
const rtos::Kernel::Clock::duration t2_delay = THREAD_DELAY * 2;
74+
const rtos::Kernel::Clock::duration t3_delay = THREAD_DELAY * 3;
7475

7576
Thread t1(osPriorityNormal, THREAD_STACK_SIZE);
7677
Thread t2(osPriorityNormal, THREAD_STACK_SIZE);
@@ -140,7 +141,7 @@ void test_single_thread()
140141

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

0 commit comments

Comments
 (0)