tests: kernel: Add a new test for providing time duration in milliseconds

Add a new test case to verify whether kernel allow proving time duration
in milliseconds.

Signed-off-by: Jian Kang <jianx.kang@intel.com>
This commit is contained in:
Jian Kang 2020-05-28 15:52:14 +08:00 committed by Anas Nashif
commit 445576c8a2
2 changed files with 81 additions and 0 deletions

View file

@ -22,6 +22,23 @@
} while (0)
#endif
#ifndef CONFIG_BOARD_QEMU_CORTEX_M0
struct timer_data {
int duration_count;
int stop_count;
};
static void duration_expire(struct k_timer *timer);
static void stop_expire(struct k_timer *timer);
/** TESTPOINT: init timer via K_TIMER_DEFINE */
K_TIMER_DEFINE(ktimer, duration_expire, stop_expire);
static ZTEST_BMEM struct timer_data tdata;
#define DURATION 100
#define LESS_DURATION 80
#endif
/**
* @addtogroup kernel_common_tests
* @{
@ -110,6 +127,68 @@ void test_clock_cycle(void)
}
}
#ifdef CONFIG_BOARD_QEMU_CORTEX_M0
void test_ms_time_duration(void)
{
ztest_test_skip();
}
#else
/*
*help function
*/
static void duration_expire(struct k_timer *timer)
{
tdata.duration_count++;
}
static void stop_expire(struct k_timer *timer)
{
tdata.stop_count++;
}
static void init_data_count(void)
{
tdata.duration_count = 0;
tdata.stop_count = 0;
}
/**
* @brief Test millisecond time duration
*
* @details initialize a timer, then providing time duration in
* millisecond, and check the duration time whether correct.
*
* @see k_timer_init(), k_timer_start(), k_timer_stop(),
* k_busy_wait()
*
*
*/
void test_ms_time_duration(void)
{
init_data_count();
k_timer_start(&ktimer, K_MSEC(DURATION), K_NO_WAIT);
/** TESTPOINT: waiting time less than duration and check the count*/
k_busy_wait(LESS_DURATION * 1000);
zassert_true(tdata.duration_count == 0, NULL);
zassert_true(tdata.stop_count == 0, NULL);
/** TESTPOINT: proving duration in millisecond */
init_data_count();
k_timer_start(&ktimer, K_MSEC(100), K_MSEC(50));
/** TESTPOINT: waiting time more than duration and check the count */
k_busy_wait((DURATION + 1) * 1000);
zassert_true(tdata.duration_count == 1, NULL);
zassert_true(tdata.stop_count == 0, NULL);
/** cleanup environemtn */
k_timer_stop(&ktimer);
}
#endif
/**
* @}
*/

View file

@ -29,6 +29,7 @@ extern void test_printk(void);
extern void test_timeout_order(void);
extern void test_clock_cycle(void);
extern void test_clock_uptime(void);
extern void test_ms_time_duration(void);
extern void test_multilib(void);
extern void test_thread_context(void);
extern void test_bootdelay(void);
@ -124,6 +125,7 @@ void test_main(void)
ztest_unit_test(test_version),
ztest_unit_test(test_multilib),
ztest_unit_test(test_thread_context),
ztest_unit_test(test_ms_time_duration),
ztest_unit_test(test_bounds_check_mitigation)
);