timer_tc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-12 luckyzjq the first version
  9. */
  10. #include <rtthread.h>
  11. #include <stdlib.h>
  12. #include "utest.h"
  13. static rt_uint8_t timer_flag_oneshot[] =
  14. {
  15. RT_TIMER_FLAG_ONE_SHOT,
  16. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
  17. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER,
  18. };
  19. static rt_uint8_t timer_flag_periodic[] =
  20. {
  21. RT_TIMER_FLAG_PERIODIC,
  22. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER,
  23. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER,
  24. };
  25. typedef struct test_timer_struct
  26. {
  27. struct rt_timer static_timer; /* static timer handler */
  28. rt_timer_t dynamic_timer; /* dynamic timer pointer */
  29. rt_tick_t expect_tick; /* expect tick */
  30. rt_uint8_t test_flag; /* timer callback done flag */
  31. } timer_struct;
  32. static timer_struct timer;
  33. #define test_static_timer_start test_static_timer_init
  34. #define test_static_timer_stop test_static_timer_init
  35. #define test_static_timer_detach test_static_timer_init
  36. static void static_timer_oneshot(void *param)
  37. {
  38. timer_struct *timer_call;
  39. timer_call = (timer_struct *)param;
  40. timer_call->test_flag = RT_TRUE;
  41. /* check expect tick */
  42. if (rt_tick_get() - timer_call->expect_tick > 1)
  43. {
  44. uassert_true(RT_FALSE);
  45. }
  46. return;
  47. }
  48. static void static_timer_periodic(void *param)
  49. {
  50. rt_err_t result;
  51. timer_struct *timer_call;
  52. timer_call = (timer_struct *)param;
  53. timer_call->test_flag = RT_TRUE;
  54. /* check expect tick */
  55. if (rt_tick_get() - timer_call->expect_tick > 1)
  56. {
  57. uassert_true(RT_FALSE);
  58. }
  59. /* periodic timer can stop */
  60. result = rt_timer_stop(&timer_call->static_timer);
  61. if (RT_EOK != result)
  62. {
  63. uassert_true(RT_FALSE);
  64. }
  65. return;
  66. }
  67. static void test_static_timer_init(void)
  68. {
  69. rt_err_t result;
  70. int rand_num = rand() % 10;
  71. /* one shot timer test */
  72. for (int time_out = 0; time_out < rand_num; time_out++)
  73. {
  74. for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
  75. {
  76. rt_timer_init(&timer.static_timer,
  77. "static_timer",
  78. static_timer_oneshot,
  79. &timer,
  80. time_out,
  81. timer_flag_oneshot[i]);
  82. /* calc expect tick */
  83. timer.expect_tick = rt_tick_get() + time_out;
  84. /* start timer */
  85. result = rt_timer_start(&timer.static_timer);
  86. if (RT_EOK != result)
  87. {
  88. uassert_true(RT_FALSE);
  89. return;
  90. }
  91. /* wait for timerout */
  92. rt_thread_delay(time_out + 1);
  93. /* detach timer */
  94. result = rt_timer_detach(&timer.static_timer);
  95. if (RT_EOK != result)
  96. {
  97. uassert_true(RT_FALSE);
  98. return;
  99. }
  100. if (timer.test_flag != RT_TRUE)
  101. {
  102. uassert_true(RT_FALSE);
  103. return;
  104. }
  105. }
  106. }
  107. /* periodic timer test */
  108. for (int time_out = 0; time_out < rand_num; time_out++)
  109. {
  110. for (int i = 0; i < sizeof(timer_flag_periodic); i++)
  111. {
  112. rt_timer_init(&timer.static_timer,
  113. "static_timer",
  114. static_timer_periodic,
  115. &timer,
  116. time_out,
  117. timer_flag_periodic[i]);
  118. /* calc expect tick */
  119. timer.expect_tick = rt_tick_get() + time_out;
  120. /* start timer */
  121. result = rt_timer_start(&timer.static_timer);
  122. if (RT_EOK != result)
  123. {
  124. uassert_true(RT_FALSE);
  125. return;
  126. }
  127. /* wait for timerout */
  128. rt_thread_delay(time_out + 1);
  129. /* detach timer */
  130. result = rt_timer_detach(&timer.static_timer);
  131. if (RT_EOK != result)
  132. {
  133. uassert_true(RT_FALSE);
  134. return;
  135. }
  136. if (timer.test_flag != RT_TRUE)
  137. {
  138. uassert_true(RT_FALSE);
  139. return;
  140. }
  141. }
  142. }
  143. timer.test_flag = RT_FALSE;
  144. uassert_true(RT_TRUE);
  145. return;
  146. }
  147. static void static_timer_control(void *param)
  148. {
  149. rt_err_t result;
  150. timer_struct *timer_call;
  151. timer_call = (timer_struct *)param;
  152. timer_call->test_flag = RT_TRUE;
  153. /* check expect tick */
  154. if (rt_tick_get() - timer_call->expect_tick > 1)
  155. {
  156. uassert_true(RT_FALSE);
  157. }
  158. /* periodic timer can stop */
  159. result = rt_timer_stop(&timer_call->static_timer);
  160. if (RT_EOK != result)
  161. {
  162. uassert_true(RT_FALSE);
  163. }
  164. return;
  165. }
  166. static void test_static_timer_control(void)
  167. {
  168. rt_err_t result;
  169. int rand_num = rand() % 10;
  170. int set_data;
  171. int get_data;
  172. rt_timer_init(&timer.static_timer,
  173. "static_timer",
  174. static_timer_control,
  175. &timer,
  176. 5,
  177. RT_TIMER_FLAG_PERIODIC);
  178. /* test set data */
  179. set_data = rand_num;
  180. result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_SET_TIME, &set_data);
  181. if (RT_EOK != result)
  182. {
  183. uassert_true(RT_FALSE);
  184. }
  185. /* test get data */
  186. result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_GET_TIME, &get_data);
  187. if (RT_EOK != result)
  188. {
  189. uassert_true(RT_FALSE);
  190. }
  191. /* a set of test */
  192. if (set_data != get_data)
  193. {
  194. uassert_true(RT_FALSE);
  195. }
  196. /* calc expect tick */
  197. timer.expect_tick = rt_tick_get() + set_data;
  198. /* start timer */
  199. result = rt_timer_start(&timer.static_timer);
  200. if (RT_EOK != result)
  201. {
  202. uassert_true(RT_FALSE);
  203. return;
  204. }
  205. rt_thread_delay(set_data + 1);
  206. /* detach timer */
  207. result = rt_timer_detach(&timer.static_timer);
  208. if (RT_EOK != result)
  209. {
  210. uassert_true(RT_FALSE);
  211. return;
  212. }
  213. if (timer.test_flag != RT_TRUE)
  214. {
  215. uassert_true(RT_FALSE);
  216. return;
  217. }
  218. timer.test_flag = RT_FALSE;
  219. uassert_true(RT_TRUE);
  220. }
  221. #ifdef RT_USING_HEAP
  222. #define test_dynamic_timer_start test_dynamic_timer_create
  223. #define test_dynamic_timer_stop test_dynamic_timer_create
  224. #define test_dynamic_timer_delete test_dynamic_timer_create
  225. static void dynamic_timer_oneshot(void *param)
  226. {
  227. timer_struct *timer_call;
  228. timer_call = (timer_struct *)param;
  229. timer_call->test_flag = RT_TRUE;
  230. /* check expect tick */
  231. if (rt_tick_get() - timer_call->expect_tick > 1)
  232. {
  233. uassert_true(RT_FALSE);
  234. }
  235. return;
  236. }
  237. static void dynamic_timer_periodic(void *param)
  238. {
  239. rt_err_t result;
  240. timer_struct *timer_call;
  241. timer_call = (timer_struct *)param;
  242. timer_call->test_flag = RT_TRUE;
  243. /* check expect tick */
  244. if (rt_tick_get() - timer_call->expect_tick > 1)
  245. {
  246. uassert_true(RT_FALSE);
  247. }
  248. /* periodic timer can stop */
  249. result = rt_timer_stop(timer_call->dynamic_timer);
  250. if (RT_EOK != result)
  251. {
  252. uassert_true(RT_FALSE);
  253. }
  254. return;
  255. }
  256. static void test_dynamic_timer_create(void)
  257. {
  258. rt_err_t result;
  259. int rand_num = rand() % 10;
  260. /* one shot timer test */
  261. for (int time_out = 0; time_out < rand_num; time_out++)
  262. {
  263. for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
  264. {
  265. timer.dynamic_timer = rt_timer_create("dynamic_timer",
  266. dynamic_timer_oneshot,
  267. &timer,
  268. time_out,
  269. timer_flag_oneshot[i]);
  270. /* calc expect tick */
  271. timer.expect_tick = rt_tick_get() + time_out;
  272. /* start timer */
  273. result = rt_timer_start(timer.dynamic_timer);
  274. if (RT_EOK != result)
  275. {
  276. uassert_true(RT_FALSE);
  277. return;
  278. }
  279. /* wait for timerout */
  280. rt_thread_delay(time_out + 1);
  281. /* detach timer */
  282. result = rt_timer_delete(timer.dynamic_timer);
  283. if (RT_EOK != result)
  284. {
  285. uassert_true(RT_FALSE);
  286. return;
  287. }
  288. if (timer.test_flag != RT_TRUE)
  289. {
  290. uassert_true(RT_FALSE);
  291. return;
  292. }
  293. }
  294. }
  295. /* periodic timer test */
  296. for (int time_out = 0; time_out < rand_num; time_out++)
  297. {
  298. for (int i = 0; i < sizeof(timer_flag_periodic); i++)
  299. {
  300. timer.dynamic_timer = rt_timer_create("dynamic_timer",
  301. dynamic_timer_periodic,
  302. &timer,
  303. time_out,
  304. timer_flag_periodic[i]);
  305. /* calc expect tick */
  306. timer.expect_tick = rt_tick_get() + time_out;
  307. /* start timer */
  308. result = rt_timer_start(timer.dynamic_timer);
  309. if (RT_EOK != result)
  310. {
  311. uassert_true(RT_FALSE);
  312. return;
  313. }
  314. /* wait for timerout */
  315. rt_thread_delay(time_out + 1);
  316. /* detach timer */
  317. result = rt_timer_delete(timer.dynamic_timer);
  318. if (RT_EOK != result)
  319. {
  320. uassert_true(RT_FALSE);
  321. return;
  322. }
  323. if (timer.test_flag != RT_TRUE)
  324. {
  325. uassert_true(RT_FALSE);
  326. return;
  327. }
  328. }
  329. }
  330. timer.test_flag = RT_FALSE;
  331. uassert_true(RT_TRUE);
  332. return;
  333. }
  334. static void dynamic_timer_control(void *param)
  335. {
  336. rt_err_t result;
  337. timer_struct *timer_call;
  338. timer_call = (timer_struct *)param;
  339. timer_call->test_flag = RT_TRUE;
  340. /* check expect tick */
  341. if (rt_tick_get() - timer_call->expect_tick > 1)
  342. {
  343. uassert_true(RT_FALSE);
  344. }
  345. /* periodic timer can stop */
  346. result = rt_timer_stop(timer_call->dynamic_timer);
  347. if (RT_EOK != result)
  348. {
  349. uassert_true(RT_FALSE);
  350. }
  351. return;
  352. }
  353. static void test_dynamic_timer_control(void)
  354. {
  355. rt_err_t result;
  356. int rand_num = rand() % 10;
  357. int set_data;
  358. int get_data;
  359. timer.dynamic_timer = rt_timer_create("dynamic_timer",
  360. dynamic_timer_control,
  361. &timer,
  362. 5,
  363. RT_TIMER_FLAG_PERIODIC);
  364. /* test set data */
  365. set_data = rand_num;
  366. result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_SET_TIME, &set_data);
  367. if (RT_EOK != result)
  368. {
  369. uassert_true(RT_FALSE);
  370. }
  371. /* test get data */
  372. result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_GET_TIME, &get_data);
  373. if (RT_EOK != result)
  374. {
  375. uassert_true(RT_FALSE);
  376. }
  377. /* a set of test */
  378. if (set_data != get_data)
  379. {
  380. uassert_true(RT_FALSE);
  381. }
  382. /* calc expect tick */
  383. timer.expect_tick = rt_tick_get() + set_data;
  384. /* start timer */
  385. result = rt_timer_start(timer.dynamic_timer);
  386. if (RT_EOK != result)
  387. {
  388. uassert_true(RT_FALSE);
  389. return;
  390. }
  391. rt_thread_delay(set_data + 1);
  392. /* detach timer */
  393. result = rt_timer_delete(timer.dynamic_timer);
  394. if (RT_EOK != result)
  395. {
  396. uassert_true(RT_FALSE);
  397. return;
  398. }
  399. if (timer.test_flag != RT_TRUE)
  400. {
  401. uassert_true(RT_FALSE);
  402. return;
  403. }
  404. timer.test_flag = RT_FALSE;
  405. uassert_true(RT_TRUE);
  406. }
  407. #endif /* RT_USING_HEAP */
  408. static rt_err_t utest_tc_init(void)
  409. {
  410. timer.dynamic_timer = RT_NULL;
  411. timer.test_flag = RT_FALSE;
  412. return RT_EOK;
  413. }
  414. static rt_err_t utest_tc_cleanup(void)
  415. {
  416. timer.dynamic_timer = RT_NULL;
  417. timer.test_flag = RT_FALSE;
  418. return RT_EOK;
  419. }
  420. static void testcase(void)
  421. {
  422. UTEST_UNIT_RUN(test_static_timer_init);
  423. UTEST_UNIT_RUN(test_static_timer_start);
  424. UTEST_UNIT_RUN(test_static_timer_stop);
  425. UTEST_UNIT_RUN(test_static_timer_detach);
  426. UTEST_UNIT_RUN(test_static_timer_control);
  427. #ifdef RT_USING_HEAP
  428. UTEST_UNIT_RUN(test_dynamic_timer_create);
  429. UTEST_UNIT_RUN(test_dynamic_timer_start);
  430. UTEST_UNIT_RUN(test_dynamic_timer_stop);
  431. UTEST_UNIT_RUN(test_dynamic_timer_delete);
  432. UTEST_UNIT_RUN(test_dynamic_timer_control);
  433. #endif /* RT_USING_HEAP */
  434. }
  435. UTEST_TC_EXPORT(testcase, "testcases.kernel.timer_tc", utest_tc_init, utest_tc_cleanup, 1000);
  436. /*********************** end of file ****************************/