timer_tc.c 13 KB

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