From e5e45d83e77522c8e15fc3755f2b7aebeec98c59 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Tue, 31 Mar 2020 13:18:08 +0300 Subject: [PATCH] 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 --- TESTS/mbed_drivers/timerevent/main.cpp | 2 +- TESTS/mbed_hal/rtc/main.cpp | 25 +++++---- .../kernel_tick_count/main.cpp | 54 +++++++++++-------- rtos/Queue.h | 9 ++-- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/TESTS/mbed_drivers/timerevent/main.cpp b/TESTS/mbed_drivers/timerevent/main.cpp index 56142a051c..99c429b2ef 100644 --- a/TESTS/mbed_drivers/timerevent/main.cpp +++ b/TESTS/mbed_drivers/timerevent/main.cpp @@ -41,7 +41,7 @@ private: public: TestTimerEvent() : - TimerEvent(), sem(0, 1) + TimerEvent(get_us_ticker_data()), sem(0, 1) { sleep_manager_lock_deep_sleep(); diff --git a/TESTS/mbed_hal/rtc/main.cpp b/TESTS/mbed_hal/rtc/main.cpp index 04b4ea7b0e..947bae34e8 100644 --- a/TESTS/mbed_hal/rtc/main.cpp +++ b/TESTS/mbed_hal/rtc/main.cpp @@ -27,6 +27,8 @@ #include "mbed.h" #include "drivers/RealTimeClock.h" #include "rtc_api.h" +#include +#include using namespace utest::v1; using namespace std::chrono; @@ -34,12 +36,15 @@ using namespace std::chrono; static constexpr auto WAIT_TIME = 4s; static constexpr auto WAIT_TOLERANCE = 1s; +#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \ + do { \ + using ct = std::common_type_t; \ + TEST_ASSERT_INT_WITHIN(ct(delta).count(), ct(expected).count(), ct(actual).count()); \ + } while (0) + #if DEVICE_LPTICKER mstd::atomic_bool expired; -#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \ - TEST_ASSERT_INT64_WITHIN(microseconds(delta).count(), microseconds(expected).count(), microseconds(actual).count()) - void set_flag_true(void) { expired = true; @@ -154,11 +159,11 @@ void rtc_glitch_test() /* Test that the RTC correctly handles different time values. */ void rtc_range_test() { - static const RealTimeClock::time_point starts[] { - 0x00000000s, - 0xEFFFFFFFs, - 0x00001000s, - 0x00010000s, + static const RealTimeClock::time_point starts[] = { + RealTimeClock::time_point{0x00000000s}, + RealTimeClock::time_point{0xEFFFFFFFs}, + RealTimeClock::time_point{0x00001000s}, + RealTimeClock::time_point{0x00010000s}, }; RealTimeClock::init(); @@ -187,7 +192,7 @@ void rtc_accuracy_test() timer1.stop(); /* RTC accuracy is at least 10%. */ - TEST_ASSERT_DURATION_WITHIN(1s, 10s, timer1.read_duration()); + TEST_ASSERT_DURATION_WITHIN(1s, 10s, timer1.elapsed_time()); } /* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */ @@ -221,7 +226,7 @@ void rtc_enabled_test() */ RealTimeClock::init(); - RealTimeClock::write(RealTimeClock::time_point(0)); + RealTimeClock::write(RealTimeClock::time_point(0s)); TEST_ASSERT_TRUE(RealTimeClock::isenabled()); RealTimeClock::free(); } diff --git a/TESTS/mbedmicro-rtos-mbed/kernel_tick_count/main.cpp b/TESTS/mbedmicro-rtos-mbed/kernel_tick_count/main.cpp index 24369e9457..59b4954959 100644 --- a/TESTS/mbedmicro-rtos-mbed/kernel_tick_count/main.cpp +++ b/TESTS/mbedmicro-rtos-mbed/kernel_tick_count/main.cpp @@ -21,18 +21,37 @@ #include "rtos/Kernel.h" #include "mbed.h" -using namespace std::chrono_literals; +#include + +using namespace std::chrono; using utest::v1::Case; #define TEST_REPEAT_COUNT 1000 -#define NUM_WAIT_TICKS rtos::Kernel::Clock::duration(1s) -#define ONE_SECOND Timer::duration(1s) -#define SMALL_DELTA Timer::duration(1500us) // 0.15% -#define BIG_DELTA Timer::duration(15000us) // 1.5% +#define ACCURACY_DURATION 1s +#if defined(NO_SYSTICK) || defined(MBED_TICKLESS) +// On targets with NO_SYSTICK/MBED_TICKLESS enabled, systick is emulated by lp_ticker what makes it less accurate +// for more details https://os.mbed.com/docs/latest/reference/tickless.html +#define ACCURACY_DELTA 15000us // 1.5% +#else +#define ACCURACY_DELTA 1500us // 0.15% +#endif -/** Test if kernel ticker frequency is 1kHz + +#define TEST_ASSERT_EQUAL_DURATION(expected, actual) \ + do { \ + using ct = std::common_type_t; \ + TEST_ASSERT_EQUAL(ct(expected).count(), ct(actual).count()); \ + } while (0) + +#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \ + do { \ + using ct = std::common_type_t; \ + TEST_ASSERT_WITHIN(ct(delta).count(), ct(expected).count(), ct(actual).count()); \ + } while (0) + +/** Test if declared kernel ticker frequency is 1kHz Given a RTOS kernel ticker When check it frequency @@ -67,13 +86,13 @@ void test_increment(void) } } -/** Test if kernel ticker interval is 1ms +/** Test if kernel ticker rate is correct Given a RTOS kernel ticker When perform subsequent calls of @a rtos::Kernel::Clock::now - Then the ticker interval should be 1ms + Then when it reports 1 second elapsed, the time measured using a high-res Timer corresponds. */ -void test_interval() +void test_accuracy() { Kernel::Clock::time_point start, stop; Timer timer; @@ -86,27 +105,20 @@ void test_interval() timer.start(); start = stop; - // wait for NUM_WAIT_TICKS ticks + // wait for 1 second to elapse according to kernel do { stop = rtos::Kernel::Clock::now(); - } while ((stop - start) != NUM_WAIT_TICKS); + } while ((stop - start) < ACCURACY_DURATION); timer.stop(); - TEST_ASSERT_EQUAL_INT64(NUM_WAIT_TICKS.count(), (stop - start).count()); -#if defined(NO_SYSTICK) || defined(MBED_TICKLESS) - // On targets with NO_SYSTICK/MBED_TICKLESS enabled, systick is emulated by lp_ticker what makes it less accurate - // for more details https://os.mbed.com/docs/latest/reference/tickless.html - TEST_ASSERT_INT64_WITHIN(BIG_DELTA.count(), ONE_SECOND.count(), timer.read_duration().count()); -#else - TEST_ASSERT_INT64_WITHIN(SMALL_DELTA.count(), ONE_SECOND.count(), timer.read_duration().count()); -#endif + TEST_ASSERT_DURATION_WITHIN(ACCURACY_DELTA, ACCURACY_DURATION, timer.elapsed_time()); } // Test cases Case cases[] = { - Case("Test kernel ticker frequency", test_frequency), + Case("Test kernel ticker declared frequency", test_frequency), Case("Test if kernel ticker increments by one", test_increment), - Case("Test if kernel ticker interval is 1ms", test_interval) + Case("Test kernel ticker accuracy", test_accuracy) }; utest::v1::status_t greentea_test_setup(const size_t number_of_cases) diff --git a/rtos/Queue.h b/rtos/Queue.h index 14081ecd0b..a3a5198320 100644 --- a/rtos/Queue.h +++ b/rtos/Queue.h @@ -174,7 +174,7 @@ public: * queue. * * The parameter `millisec` can have the following values: - * - When the timeout is 0 (the default), the function returns instantly. + * - When the timeout is 0, the function returns instantly. * - When the timeout is osWaitForever, the function waits for an * infinite time. * - For all other values, the function waits for the given number of @@ -182,7 +182,7 @@ public: * * @param data Pointer to the element to insert into the queue. * @param millisec Timeout for the operation to be executed, or 0 in case - * of no timeout. (default: 0) + * of no timeout. * @param prio Priority of the operation or 0 in case of default. * (default: 0) * @@ -201,7 +201,7 @@ public: * @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`. */ MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.") - osStatus put(T *data, uint32_t millisec = 0, uint8_t prio = 0) + osStatus put(T *data, uint32_t millisec, uint8_t prio = 0) { return put(data, std::chrono::duration(millisec), prio); } @@ -288,7 +288,6 @@ public: * (FIFO) order. * * @param millisec Timeout value. - * (default: osWaitForever). * * @return Event information that includes the message in event. Message * value and the status code in event.status: @@ -305,7 +304,7 @@ public: * @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`. */ MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.") - osEvent get(uint32_t millisec = osWaitForever) + osEvent get(uint32_t millisec) { return get(std::chrono::duration(millisec)); }