Skip to content

Commit e5e45d8

Browse files
committed
Chrono test fixes
* Remove ambiguity in single-parameter Queue::put and get * Fix type problems in RTC test - add missing include * Don't attempt to use TimerEvent default constructor * Remove references to Timer::read_duration
1 parent a10dafd commit e5e45d8

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

TESTS/mbed_drivers/timerevent/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TestTimerEvent: public TimerEvent {
4141

4242
public:
4343
TestTimerEvent() :
44-
TimerEvent(), sem(0, 1)
44+
TimerEvent(get_us_ticker_data()), sem(0, 1)
4545
{
4646

4747
sleep_manager_lock_deep_sleep();

TESTS/mbed_hal/rtc/main.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@
2727
#include "mbed.h"
2828
#include "drivers/RealTimeClock.h"
2929
#include "rtc_api.h"
30+
#include <type_traits>
31+
#include <mstd_atomic>
3032

3133
using namespace utest::v1;
3234
using namespace std::chrono;
3335

3436
static constexpr auto WAIT_TIME = 4s;
3537
static constexpr auto WAIT_TOLERANCE = 1s;
3638

39+
#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \
40+
do { \
41+
using ct = std::common_type_t<decltype(delta), decltype(expected), decltype(actual)>; \
42+
TEST_ASSERT_INT_WITHIN(ct(delta).count(), ct(expected).count(), ct(actual).count()); \
43+
} while (0)
44+
3745
#if DEVICE_LPTICKER
3846
mstd::atomic_bool expired;
3947

40-
#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \
41-
TEST_ASSERT_INT64_WITHIN(microseconds(delta).count(), microseconds(expected).count(), microseconds(actual).count())
42-
4348
void set_flag_true(void)
4449
{
4550
expired = true;
@@ -154,11 +159,11 @@ void rtc_glitch_test()
154159
/* Test that the RTC correctly handles different time values. */
155160
void rtc_range_test()
156161
{
157-
static const RealTimeClock::time_point starts[] {
158-
0x00000000s,
159-
0xEFFFFFFFs,
160-
0x00001000s,
161-
0x00010000s,
162+
static const RealTimeClock::time_point starts[] = {
163+
RealTimeClock::time_point{0x00000000s},
164+
RealTimeClock::time_point{0xEFFFFFFFs},
165+
RealTimeClock::time_point{0x00001000s},
166+
RealTimeClock::time_point{0x00010000s},
162167
};
163168

164169
RealTimeClock::init();
@@ -187,7 +192,7 @@ void rtc_accuracy_test()
187192
timer1.stop();
188193

189194
/* RTC accuracy is at least 10%. */
190-
TEST_ASSERT_DURATION_WITHIN(1s, 10s, timer1.read_duration());
195+
TEST_ASSERT_DURATION_WITHIN(1s, 10s, timer1.elapsed_time());
191196
}
192197

193198
/* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */
@@ -221,7 +226,7 @@ void rtc_enabled_test()
221226
*/
222227

223228
RealTimeClock::init();
224-
RealTimeClock::write(RealTimeClock::time_point(0));
229+
RealTimeClock::write(RealTimeClock::time_point(0s));
225230
TEST_ASSERT_TRUE(RealTimeClock::isenabled());
226231
RealTimeClock::free();
227232
}

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

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

24-
using namespace std::chrono_literals;
24+
#include <type_traits>
25+
26+
using namespace std::chrono;
2527

2628
using utest::v1::Case;
2729

2830
#define TEST_REPEAT_COUNT 1000
29-
#define NUM_WAIT_TICKS rtos::Kernel::Clock::duration(1s)
3031

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%
32+
#define ACCURACY_DURATION 1s
33+
#if defined(NO_SYSTICK) || defined(MBED_TICKLESS)
34+
// On targets with NO_SYSTICK/MBED_TICKLESS enabled, systick is emulated by lp_ticker what makes it less accurate
35+
// for more details https://os.mbed.com/docs/latest/reference/tickless.html
36+
#define ACCURACY_DELTA 15000us // 1.5%
37+
#else
38+
#define ACCURACY_DELTA 1500us // 0.15%
39+
#endif
40+
3441

35-
/** Test if kernel ticker frequency is 1kHz
42+
#define TEST_ASSERT_EQUAL_DURATION(expected, actual) \
43+
do { \
44+
using ct = std::common_type_t<decltype(expected), decltype(actual)>; \
45+
TEST_ASSERT_EQUAL(ct(expected).count(), ct(actual).count()); \
46+
} while (0)
47+
48+
#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \
49+
do { \
50+
using ct = std::common_type_t<decltype(delta), decltype(expected), decltype(actual)>; \
51+
TEST_ASSERT_WITHIN(ct(delta).count(), ct(expected).count(), ct(actual).count()); \
52+
} while (0)
53+
54+
/** Test if declared kernel ticker frequency is 1kHz
3655
3756
Given a RTOS kernel ticker
3857
When check it frequency
@@ -67,13 +86,13 @@ void test_increment(void)
6786
}
6887
}
6988

70-
/** Test if kernel ticker interval is 1ms
89+
/** Test if kernel ticker rate is correct
7190
7291
Given a RTOS kernel ticker
7392
When perform subsequent calls of @a rtos::Kernel::Clock::now
74-
Then the ticker interval should be 1ms
93+
Then when it reports 1 second elapsed, the time measured using a high-res Timer corresponds.
7594
*/
76-
void test_interval()
95+
void test_accuracy()
7796
{
7897
Kernel::Clock::time_point start, stop;
7998
Timer timer;
@@ -86,27 +105,20 @@ void test_interval()
86105
timer.start();
87106
start = stop;
88107

89-
// wait for NUM_WAIT_TICKS ticks
108+
// wait for 1 second to elapse according to kernel
90109
do {
91110
stop = rtos::Kernel::Clock::now();
92-
} while ((stop - start) != NUM_WAIT_TICKS);
111+
} while ((stop - start) < ACCURACY_DURATION);
93112
timer.stop();
94-
TEST_ASSERT_EQUAL_INT64(NUM_WAIT_TICKS.count(), (stop - start).count());
95113

96-
#if defined(NO_SYSTICK) || defined(MBED_TICKLESS)
97-
// On targets with NO_SYSTICK/MBED_TICKLESS enabled, systick is emulated by lp_ticker what makes it less accurate
98-
// for more details https://os.mbed.com/docs/latest/reference/tickless.html
99-
TEST_ASSERT_INT64_WITHIN(BIG_DELTA.count(), ONE_SECOND.count(), timer.read_duration().count());
100-
#else
101-
TEST_ASSERT_INT64_WITHIN(SMALL_DELTA.count(), ONE_SECOND.count(), timer.read_duration().count());
102-
#endif
114+
TEST_ASSERT_DURATION_WITHIN(ACCURACY_DELTA, ACCURACY_DURATION, timer.elapsed_time());
103115
}
104116

105117
// Test cases
106118
Case cases[] = {
107-
Case("Test kernel ticker frequency", test_frequency),
119+
Case("Test kernel ticker declared frequency", test_frequency),
108120
Case("Test if kernel ticker increments by one", test_increment),
109-
Case("Test if kernel ticker interval is 1ms", test_interval)
121+
Case("Test kernel ticker accuracy", test_accuracy)
110122
};
111123

112124
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)

rtos/Queue.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
174174
* queue.
175175
*
176176
* The parameter `millisec` can have the following values:
177-
* - When the timeout is 0 (the default), the function returns instantly.
177+
* - When the timeout is 0, the function returns instantly.
178178
* - When the timeout is osWaitForever, the function waits for an
179179
* infinite time.
180180
* - For all other values, the function waits for the given number of
181181
* milliseconds.
182182
*
183183
* @param data Pointer to the element to insert into the queue.
184184
* @param millisec Timeout for the operation to be executed, or 0 in case
185-
* of no timeout. (default: 0)
185+
* of no timeout.
186186
* @param prio Priority of the operation or 0 in case of default.
187187
* (default: 0)
188188
*
@@ -201,7 +201,7 @@ class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
201201
* @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
202202
*/
203203
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
204-
osStatus put(T *data, uint32_t millisec = 0, uint8_t prio = 0)
204+
osStatus put(T *data, uint32_t millisec, uint8_t prio = 0)
205205
{
206206
return put(data, std::chrono::duration<uint32_t, std::milli>(millisec), prio);
207207
}
@@ -288,7 +288,6 @@ class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
288288
* (FIFO) order.
289289
*
290290
* @param millisec Timeout value.
291-
* (default: osWaitForever).
292291
*
293292
* @return Event information that includes the message in event. Message
294293
* value and the status code in event.status:
@@ -305,7 +304,7 @@ class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
305304
* @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
306305
*/
307306
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
308-
osEvent get(uint32_t millisec = osWaitForever)
307+
osEvent get(uint32_t millisec)
309308
{
310309
return get(std::chrono::duration<uint32_t, std::milli>(millisec));
311310
}

0 commit comments

Comments
 (0)