diff --git a/events/tests/TESTS/events/equeue/main.cpp b/events/tests/TESTS/events/equeue/main.cpp index 6853bfc7570e..7ee47c727c93 100644 --- a/events/tests/TESTS/events/equeue/main.cpp +++ b/events/tests/TESTS/events/equeue/main.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +using namespace std::chrono_literals; + #define TEST_EQUEUE_SIZE (4*EVENTS_EVENT_SIZE) #define TEST_THREAD_STACK_SIZE 512 #define DISPATCH_INFINITE -1 @@ -43,7 +45,7 @@ static void simple_func(void *p) static void sloth_func(void *p) { - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); (*(reinterpret_cast(p)))++; } @@ -118,7 +120,7 @@ static void nest_func(void *p) struct nest *nst = reinterpret_cast(p); equeue_call(nst->q, nst->cb, nst->data); - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); } static void multithread_thread(equeue_t *p) @@ -151,7 +153,7 @@ static void simple_breaker(void *p) { struct count_and_queue *caq = reinterpret_cast(p); equeue_break(caq->q); - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); caq->p++; } @@ -646,7 +648,7 @@ static void test_equeue_multithread() Thread t1(osPriorityNormal, TEST_THREAD_STACK_SIZE); t1.start(callback(multithread_thread, &q)); - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); equeue_break(&q); err = t1.join(); TEST_ASSERT_EQUAL_INT(0, err); diff --git a/events/tests/TESTS/events/queue/main.cpp b/events/tests/TESTS/events/queue/main.cpp index d19ecd821e8a..7bb8d2d3e824 100644 --- a/events/tests/TESTS/events/queue/main.cpp +++ b/events/tests/TESTS/events/queue/main.cpp @@ -26,6 +26,15 @@ using namespace utest::v1; +using namespace std::chrono; + +// Macro for test assertions between std::chrono values +#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) + // Assume that tolerance is 5% of measured time. #define DELTA(ms) (ms / 20) @@ -111,9 +120,9 @@ SIMPLE_POSTS_TEST(1, 0x01) SIMPLE_POSTS_TEST(0) -void time_func(Timer *t, int ms) +void time_func(Timer *t, milliseconds time) { - TEST_ASSERT_INT_WITHIN(DELTA(ms), ms, t->read_ms()); + TEST_ASSERT_DURATION_WITHIN(DELTA(time), time, t->elapsed_time()); t->reset(); } @@ -126,7 +135,7 @@ void call_in_test() for (int i = 0; i < N; i++) { tickers[i].start(); - queue.call_in((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100); + queue.call_in((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100ms); } queue.dispatch_for(N * 100ms); @@ -141,7 +150,7 @@ void call_every_test() for (int i = 0; i < N; i++) { tickers[i].start(); - queue.call_every((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100); + queue.call_every((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100ms); } queue.dispatch_for(N * 100ms); @@ -528,7 +537,7 @@ void event_period_tests() period_tests_queue.dispatch_for(80ms); // Wait 100ms and check the event execution status - wait_us(100 * 1000); + ThisThread::sleep_for(100ms); // Event should only have been dispatched once and thus counter // should be 1 @@ -546,7 +555,7 @@ void event_period_tests() period_tests_queue.dispatch_for(80ms); // Wait 100ms and check the event execution status - wait_us(100 * 1000); + ThisThread::sleep_for(100ms); // Event should default to non_periodic and thus only have been // dispatched once. Counter should be 1. @@ -564,7 +573,7 @@ void event_period_tests() period_tests_queue.dispatch_for(80ms); // Wait 100ms and check the event execution status - wait_us(100 * 1000); + ThisThread::sleep_for(100ms); // Event should default to non_periodic and thus only have been // dispatched once. Counter should be 1. @@ -581,7 +590,7 @@ void event_period_tests() period_tests_queue.dispatch_for(80ms); // Wait 100ms and check the event execution status - wait_us(100 * 1000); + ThisThread::sleep_for(100ms); // The event should be first dispatched after 10ms and then // every subsequent 20ms until the dispatcher has completed. diff --git a/events/tests/TESTS/events/timing/main.cpp b/events/tests/TESTS/events/timing/main.cpp index 2087e2895eb6..25fac2e62151 100644 --- a/events/tests/TESTS/events/timing/main.cpp +++ b/events/tests/TESTS/events/timing/main.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +using namespace std::chrono; + #if !DEVICE_USTICKER #error [NOT_SUPPORTED] test not supported #else @@ -37,6 +39,9 @@ using namespace utest::v1; #define TEST_EVENTS_TIMING_MEAN 25 #endif +#define TEST_EVENTS_TIMING_TIME_DURATION milliseconds{TEST_EVENTS_TIMING_TIME} +#define TEST_EVENTS_TIMING_MEAN_DURATION milliseconds{TEST_EVENTS_TIMING_MEAN} + #ifndef M_PI #define M_PI 3.14159265358979323846264338327950288 #endif @@ -57,6 +62,17 @@ float chisq(float sigma) return pow(gauss(0, sqrt(sigma)), 2); } +milliseconds random_duration(milliseconds sigma) +{ + milliseconds{static_cast(chisq(sigma.count()))}; +} + +// Macro for test assertions between std::chrono values +#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) Timer timer; @@ -67,13 +83,16 @@ void timer_timing_test() { timer.reset(); timer.start(); - int prev = timer.read_us(); + auto prev = timer.elapsed_time(); - while (prev < TEST_EVENTS_TIMING_TIME * 1000) { - int next = timer.read_us(); + while (prev < TEST_EVENTS_TIMING_TIME_DURATION) { + const auto next = timer.elapsed_time(); if (next < prev) { - printf("backwards drift %d -> %d (%08x -> %08x)\r\n", - prev, next, prev, next); + printf("backwards drift %lldus -> %lldus (%08llx -> %08llx)\r\n", + duration_cast(prev).count(), + duration_cast(next).count(), + static_cast(duration_cast(prev).count()), + static_cast(duration_cast(next).count())); } TEST_ASSERT(next >= prev); prev = next; @@ -83,14 +102,18 @@ void timer_timing_test() // equeue tick timing test void tick_timing_test() { - unsigned start = equeue_tick(); - int prev = 0; + // equeue_tick() returns an integral millisecond value + const auto start = milliseconds{equeue_tick()}; + auto prev = 0us; - while (prev < TEST_EVENTS_TIMING_TIME) { - int next = equeue_tick() - start; + while (prev < TEST_EVENTS_TIMING_TIME_DURATION) { + const auto next = milliseconds{equeue_tick()} - start; if (next < prev) { - printf("backwards drift %d -> %d (%08x -> %08x)\r\n", - prev, next, prev, next); + printf("backwards drift %lldus -> %lldus (%08llx -> %08llx)\r\n", + duration_cast(prev).count(), + duration_cast(next).count(), + static_cast(duration_cast(prev).count()), + static_cast(duration_cast(next).count())); } TEST_ASSERT(next >= prev); prev = next; @@ -104,21 +127,23 @@ void semaphore_timing_test() timer.reset(); timer.start(); - int err = equeue_sema_create(&sema); + const int err = equeue_sema_create(&sema); TEST_ASSERT_EQUAL(0, err); - while (timer.read_ms() < TEST_EVENTS_TIMING_TIME) { - int delay = chisq(TEST_EVENTS_TIMING_MEAN); + while (timer.elapsed_time() < TEST_EVENTS_TIMING_TIME_DURATION) { + const auto delay = random_duration(TEST_EVENTS_TIMING_MEAN_DURATION); - int start = timer.read_us(); - equeue_sema_wait(&sema, delay); - int taken = timer.read_us() - start; + const auto start = timer.elapsed_time(); + equeue_sema_wait(&sema, duration_cast(delay).count()); + const auto taken = timer.elapsed_time() - start; - if (taken < (delay * 1000 - 5000) || taken > (delay * 1000 + 5000)) { - printf("delay %dms => error %dus\r\n", delay, abs(1000 * delay - taken)); + if (taken < (delay - 5000us) || taken > (delay + 5000us)) { + printf("delay %lldms => error %lldus\r\n", + duration_cast(delay).count(), + abs(duration_cast(delay - taken).count())); } - TEST_ASSERT_INT_WITHIN(5000, taken, delay * 1000); + TEST_ASSERT_DURATION_WITHIN(5000us, taken, delay); } equeue_sema_destroy(&sema); @@ -128,7 +153,8 @@ void semaphore_timing_test() // Test setup utest::v1::status_t test_setup(const size_t number_of_cases) { - GREENTEA_SETUP((number_of_cases + 1)*TEST_EVENTS_TIMING_TIME / 1000, "default_auto"); + GREENTEA_SETUP((number_of_cases + 1) * duration_cast(TEST_EVENTS_TIMING_TIME_DURATION).count(), + "default_auto"); return verbose_test_setup_handler(number_of_cases); }