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
2628using 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
106118Case 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
112124utest::v1::status_t greentea_test_setup (const size_t number_of_cases)
0 commit comments