21
21
#include " rtos/Kernel.h"
22
22
#include " mbed.h"
23
23
24
- using namespace std ::chrono_literals;
24
+ #include < type_traits>
25
+
26
+ using namespace std ::chrono;
25
27
26
28
using utest::v1::Case;
27
29
28
30
#define TEST_REPEAT_COUNT 1000
29
- #define NUM_WAIT_TICKS rtos::Kernel::Clock::duration (1s)
30
31
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
+
34
41
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
36
55
37
56
Given a RTOS kernel ticker
38
57
When check it frequency
@@ -67,13 +86,13 @@ void test_increment(void)
67
86
}
68
87
}
69
88
70
- /* * Test if kernel ticker interval is 1ms
89
+ /* * Test if kernel ticker rate is correct
71
90
72
91
Given a RTOS kernel ticker
73
92
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.
75
94
*/
76
- void test_interval ()
95
+ void test_accuracy ()
77
96
{
78
97
Kernel::Clock::time_point start, stop;
79
98
Timer timer;
@@ -86,27 +105,20 @@ void test_interval()
86
105
timer.start ();
87
106
start = stop;
88
107
89
- // wait for NUM_WAIT_TICKS ticks
108
+ // wait for 1 second to elapse according to kernel
90
109
do {
91
110
stop = rtos::Kernel::Clock::now ();
92
- } while ((stop - start) != NUM_WAIT_TICKS );
111
+ } while ((stop - start) < ACCURACY_DURATION );
93
112
timer.stop ();
94
- TEST_ASSERT_EQUAL_INT64 (NUM_WAIT_TICKS.count (), (stop - start).count ());
95
113
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 ());
103
115
}
104
116
105
117
// Test cases
106
118
Case cases[] = {
107
- Case (" Test kernel ticker frequency" , test_frequency),
119
+ Case (" Test kernel ticker declared frequency" , test_frequency),
108
120
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 )
110
122
};
111
123
112
124
utest::v1::status_t greentea_test_setup (const size_t number_of_cases)
0 commit comments