Skip to content

Commit ff35791

Browse files
committed
Add Timer copy/move tests
1 parent 6268a95 commit ff35791

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

TESTS/mbed_drivers/timer/main.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,91 @@ void test_timer_time_measurement()
660660
TEST_ASSERT_DURATION_WITHIN(delta(wait_val), wait_val, p_timer->elapsed_time());
661661
}
662662

663+
/* This test verifies if a timer can be successfully copied.
664+
*
665+
* For this test Timer which uses os ticker
666+
* must be used.
667+
*
668+
* Given timer is created
669+
* Delay occurs
670+
* Timer is copied
671+
* Timer is stopped
672+
* Timer is copied again
673+
* Delay occurs
674+
* Then original timer and second copy should have measured first delay.
675+
* First copy should have measured both delays.
676+
*/
677+
template<int wait_val_us>
678+
void test_timer_copying()
679+
{
680+
microseconds wait_val(wait_val_us);
681+
const Timer &original = *static_cast<Timer *>(p_timer);
682+
683+
/* Start the timer. */
684+
p_timer->start();
685+
686+
/* Wait <wait_val_us> us. */
687+
busy_wait(wait_val);
688+
689+
/* Copy the timer */
690+
Timer running_copy{original};
691+
692+
/* Stop the original timer. */
693+
p_timer->stop();
694+
695+
/* Copy the timer */
696+
Timer stopped_copy{original};
697+
698+
/* Wait <wait_val_us> us. */
699+
busy_wait(wait_val);
700+
701+
/* Stop the running copy. */
702+
running_copy.stop();
703+
704+
/* Check results. */
705+
TEST_ASSERT_DURATION_WITHIN(delta(wait_val), wait_val, p_timer->elapsed_time());
706+
TEST_ASSERT_EQUAL_DURATION(p_timer->elapsed_time(), stopped_copy.elapsed_time());
707+
TEST_ASSERT_DURATION_WITHIN(delta(wait_val * 2), wait_val * 2, running_copy.elapsed_time());
708+
}
709+
710+
/* This test verifies if a timer can be successfully moved.
711+
*
712+
* For this test Timer which uses os ticker
713+
* must be used.
714+
*
715+
* Given timer is created
716+
* Delay occurs
717+
* Timer is moved
718+
* Delay occurs
719+
* Then moved timer should have measured both delays.
720+
*/
721+
template<int wait_val_us>
722+
void test_timer_moving()
723+
{
724+
microseconds wait_val(wait_val_us);
725+
Timer &original = *static_cast<Timer *>(p_timer);
726+
727+
/* Start the timer. */
728+
p_timer->start();
729+
730+
/* Wait <wait_val_us> us. */
731+
busy_wait(wait_val);
732+
733+
/* Move the timer */
734+
Timer moved_timer{std::move(original)};
735+
736+
/* No longer valid to do anything with the original, other than destroy it (happens in cleanup) */
737+
738+
/* Wait <wait_val_us> us. */
739+
busy_wait(wait_val);
740+
741+
/* Stop the moved timer . */
742+
moved_timer.stop();
743+
744+
/* Check results. */
745+
TEST_ASSERT_DURATION_WITHIN(delta(wait_val * 2), wait_val * 2, moved_timer.elapsed_time());
746+
}
747+
663748
utest::v1::status_t test_setup(const size_t number_of_cases)
664749
{
665750
GREENTEA_SETUP(15, "default_auto");
@@ -683,6 +768,9 @@ Case cases[] = {
683768
Case("Test: Timer - time measurement 10 ms.", timer_os_ticker_setup_handler, test_timer_time_measurement<10000>, timer_os_ticker_cleanup_handler),
684769
Case("Test: Timer - time measurement 100 ms.", timer_os_ticker_setup_handler, test_timer_time_measurement<100000>, timer_os_ticker_cleanup_handler),
685770
Case("Test: Timer - time measurement 1 s.", timer_os_ticker_setup_handler, test_timer_time_measurement<1000000>, timer_os_ticker_cleanup_handler),
771+
772+
Case("Test: Timer - copying 5 ms.", timer_os_ticker_setup_handler, test_timer_copying<5000>, timer_os_ticker_cleanup_handler),
773+
Case("Test: Timer - moving 5 ms.", timer_os_ticker_setup_handler, test_timer_moving<5000>, timer_os_ticker_cleanup_handler),
686774
};
687775

688776
Specification specification(test_setup, cases);

0 commit comments

Comments
 (0)