123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /*
- * Copyright (c) 2006-2019, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2021-08-12 luckyzjq the first version
- */
- #include <rtthread.h>
- #include <stdlib.h>
- #include "utest.h"
- static rt_uint8_t timer_flag_oneshot[] = {
- RT_TIMER_FLAG_ONE_SHOT,
- RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
- RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER,
- };
- static rt_uint8_t timer_flag_periodic[] = {
- RT_TIMER_FLAG_PERIODIC,
- RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER,
- RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER,
- };
- typedef struct test_timer_struct
- {
- struct rt_timer static_timer; /* static timer handler */
- rt_timer_t dynamic_timer; /* dynamic timer pointer */
- rt_tick_t expect_tick; /* expect tick */
- rt_uint8_t test_flag; /* timer callback done flag */
- } timer_struct;
- static timer_struct timer;
- #define test_static_timer_start test_static_timer_init
- #define test_static_timer_stop test_static_timer_init
- #define test_static_timer_detach test_static_timer_init
- static void static_timer_oneshot(void *param)
- {
- timer_struct *timer_call;
- timer_call = (timer_struct *)param;
- timer_call->test_flag = RT_TRUE;
- /* check expect tick */
- if (rt_tick_get() - timer_call->expect_tick > 1)
- {
- uassert_true(RT_FALSE);
- }
- return;
- }
- static void static_timer_periodic(void *param)
- {
- rt_err_t result;
- timer_struct *timer_call;
- timer_call = (timer_struct *)param;
- timer_call->test_flag = RT_TRUE;
- /* check expect tick */
- if (rt_tick_get() - timer_call->expect_tick > 1)
- {
- uassert_true(RT_FALSE);
- }
- /* periodic timer can stop */
- result = rt_timer_stop(&timer_call->static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- return;
- }
- static void test_static_timer_init(void)
- {
- rt_err_t result;
- int rand_num = rand() % 10;
- /* one shot timer test */
- for (int time_out = 0; time_out < rand_num; time_out++)
- {
- for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
- {
- rt_timer_init(&timer.static_timer,
- "static_timer",
- static_timer_oneshot,
- &timer,
- time_out,
- timer_flag_oneshot[i]);
- /* calc expect tick */
- timer.expect_tick = rt_tick_get() + time_out;
- /* start timer */
- result = rt_timer_start(&timer.static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- /* wait for timerout */
- rt_thread_delay(time_out + 1);
- /* detach timer */
- result = rt_timer_detach(&timer.static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- if (timer.test_flag != RT_TRUE)
- {
- uassert_true(RT_FALSE);
- return;
- }
- }
- }
- /* periodic timer test */
- for (int time_out = 0; time_out < rand_num; time_out++)
- {
- for (int i = 0; i < sizeof(timer_flag_periodic); i++)
- {
- rt_timer_init(&timer.static_timer,
- "static_timer",
- static_timer_periodic,
- &timer,
- time_out,
- timer_flag_periodic[i]);
- /* calc expect tick */
- timer.expect_tick = rt_tick_get() + time_out;
- /* start timer */
- result = rt_timer_start(&timer.static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- /* wait for timerout */
- rt_thread_delay(time_out + 1);
- /* detach timer */
- result = rt_timer_detach(&timer.static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- if (timer.test_flag != RT_TRUE)
- {
- uassert_true(RT_FALSE);
- return;
- }
- }
- }
- timer.test_flag = RT_FALSE;
- uassert_true(RT_TRUE);
- return;
- }
- static void static_timer_control(void *param)
- {
- rt_err_t result;
- timer_struct *timer_call;
- timer_call = (timer_struct *)param;
- timer_call->test_flag = RT_TRUE;
- /* check expect tick */
- if (rt_tick_get() - timer_call->expect_tick > 1)
- {
- uassert_true(RT_FALSE);
- }
- /* periodic timer can stop */
- result = rt_timer_stop(&timer_call->static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- return;
- }
- static void test_static_timer_control(void)
- {
- rt_err_t result;
- int rand_num = rand() % 10;
- int set_data;
- int get_data;
- rt_timer_init(&timer.static_timer,
- "static_timer",
- static_timer_control,
- &timer,
- 5,
- RT_TIMER_FLAG_PERIODIC);
- /* test set data */
- set_data = rand_num;
- result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_SET_TIME, &set_data);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- /* test get data */
- result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_GET_TIME, &get_data);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- /* a set of test */
- if (set_data != get_data)
- {
- uassert_true(RT_FALSE);
- }
- /* calc expect tick */
- timer.expect_tick = rt_tick_get() + set_data;
- /* start timer */
- result = rt_timer_start(&timer.static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- rt_thread_delay(set_data + 1);
- /* detach timer */
- result = rt_timer_detach(&timer.static_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- if (timer.test_flag != RT_TRUE)
- {
- uassert_true(RT_FALSE);
- return;
- }
- timer.test_flag = RT_FALSE;
- uassert_true(RT_TRUE);
- }
- #ifdef RT_USING_HEAP
- #define test_dynamic_timer_start test_dynamic_timer_create
- #define test_dynamic_timer_stop test_dynamic_timer_create
- #define test_dynamic_timer_delete test_dynamic_timer_create
- static void dynamic_timer_oneshot(void *param)
- {
- timer_struct *timer_call;
- timer_call = (timer_struct *)param;
- timer_call->test_flag = RT_TRUE;
- /* check expect tick */
- if (rt_tick_get() - timer_call->expect_tick > 1)
- {
- uassert_true(RT_FALSE);
- }
- return;
- }
- static void dynamic_timer_periodic(void *param)
- {
- rt_err_t result;
- timer_struct *timer_call;
- timer_call = (timer_struct *)param;
- timer_call->test_flag = RT_TRUE;
- /* check expect tick */
- if (rt_tick_get() - timer_call->expect_tick > 1)
- {
- uassert_true(RT_FALSE);
- }
- /* periodic timer can stop */
- result = rt_timer_stop(timer_call->dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- return;
- }
- static void test_dynamic_timer_create(void)
- {
- rt_err_t result;
- int rand_num = rand() % 10;
- /* one shot timer test */
- for (int time_out = 0; time_out < rand_num; time_out++)
- {
- for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
- {
- timer.dynamic_timer = rt_timer_create("dynamic_timer",
- dynamic_timer_oneshot,
- &timer,
- time_out,
- timer_flag_oneshot[i]);
- /* calc expect tick */
- timer.expect_tick = rt_tick_get() + time_out;
- /* start timer */
- result = rt_timer_start(timer.dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- /* wait for timerout */
- rt_thread_delay(time_out + 1);
- /* detach timer */
- result = rt_timer_delete(timer.dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- if (timer.test_flag != RT_TRUE)
- {
- uassert_true(RT_FALSE);
- return;
- }
- }
- }
- /* periodic timer test */
- for (int time_out = 0; time_out < rand_num; time_out++)
- {
- for (int i = 0; i < sizeof(timer_flag_periodic); i++)
- {
- timer.dynamic_timer = rt_timer_create("dynamic_timer",
- dynamic_timer_periodic,
- &timer,
- time_out,
- timer_flag_periodic[i]);
- /* calc expect tick */
- timer.expect_tick = rt_tick_get() + time_out;
- /* start timer */
- result = rt_timer_start(timer.dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- /* wait for timerout */
- rt_thread_delay(time_out + 1);
- /* detach timer */
- result = rt_timer_delete(timer.dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- if (timer.test_flag != RT_TRUE)
- {
- uassert_true(RT_FALSE);
- return;
- }
- }
- }
- timer.test_flag = RT_FALSE;
- uassert_true(RT_TRUE);
- return;
- }
- static void dynamic_timer_control(void *param)
- {
- rt_err_t result;
- timer_struct *timer_call;
- timer_call = (timer_struct *)param;
- timer_call->test_flag = RT_TRUE;
- /* check expect tick */
- if (rt_tick_get() - timer_call->expect_tick > 1)
- {
- uassert_true(RT_FALSE);
- }
- /* periodic timer can stop */
- result = rt_timer_stop(timer_call->dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- return;
- }
- static void test_dynamic_timer_control(void)
- {
- rt_err_t result;
- int rand_num = rand() % 10;
- int set_data;
- int get_data;
- timer.dynamic_timer = rt_timer_create("dynamic_timer",
- dynamic_timer_control,
- &timer,
- 5,
- RT_TIMER_FLAG_PERIODIC);
- /* test set data */
- set_data = rand_num;
- result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_SET_TIME, &set_data);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- /* test get data */
- result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_GET_TIME, &get_data);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- }
- /* a set of test */
- if (set_data != get_data)
- {
- uassert_true(RT_FALSE);
- }
- /* calc expect tick */
- timer.expect_tick = rt_tick_get() + set_data;
- /* start timer */
- result = rt_timer_start(timer.dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- rt_thread_delay(set_data + 1);
- /* detach timer */
- result = rt_timer_delete(timer.dynamic_timer);
- if (RT_EOK != result)
- {
- uassert_true(RT_FALSE);
- return;
- }
- if (timer.test_flag != RT_TRUE)
- {
- uassert_true(RT_FALSE);
- return;
- }
- timer.test_flag = RT_FALSE;
- uassert_true(RT_TRUE);
- }
- #endif /* RT_USING_HEAP */
- static rt_err_t utest_tc_init(void)
- {
- timer.dynamic_timer = RT_NULL;
- timer.test_flag = RT_FALSE;
- return RT_EOK;
- }
- static rt_err_t utest_tc_cleanup(void)
- {
- timer.dynamic_timer = RT_NULL;
- timer.test_flag = RT_FALSE;
- return RT_EOK;
- }
- static void testcase(void)
- {
- UTEST_UNIT_RUN(test_static_timer_init);
- UTEST_UNIT_RUN(test_static_timer_start);
- UTEST_UNIT_RUN(test_static_timer_stop);
- UTEST_UNIT_RUN(test_static_timer_detach);
- UTEST_UNIT_RUN(test_static_timer_control);
- #ifdef RT_USING_HEAP
- UTEST_UNIT_RUN(test_dynamic_timer_create);
- UTEST_UNIT_RUN(test_dynamic_timer_start);
- UTEST_UNIT_RUN(test_dynamic_timer_stop);
- UTEST_UNIT_RUN(test_dynamic_timer_delete);
- UTEST_UNIT_RUN(test_dynamic_timer_control);
- #endif /* RT_USING_HEAP */
- }
- UTEST_TC_EXPORT(testcase, "testcases.kernel.timer_tc", utest_tc_init, utest_tc_cleanup, 1000);
- /*********************** end of file ****************************/
|