event_tc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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-15 liukang the first version
  9. * 2023-09-15 xqyjlj change stack size in cpu64
  10. */
  11. #include <rtthread.h>
  12. #include "utest.h"
  13. #include <stdlib.h>
  14. #define THREAD_STACKSIZE UTEST_THR_STACK_SIZE
  15. #define EVENT_FLAG3 (1 << 3)
  16. #define EVENT_FLAG5 (1 << 5)
  17. static struct rt_event static_event = {0};
  18. #ifdef RT_USING_HEAP
  19. static rt_event_t dynamic_event = RT_NULL;
  20. static rt_uint32_t dynamic_event_recv_thread_finish = 0, dynamic_event_send_thread_finish = 0;
  21. rt_align(RT_ALIGN_SIZE)
  22. static char thread3_stack[UTEST_THR_STACK_SIZE];
  23. static struct rt_thread thread3;
  24. rt_align(RT_ALIGN_SIZE)
  25. static char thread4_stack[UTEST_THR_STACK_SIZE];
  26. static struct rt_thread thread4;
  27. #endif /* RT_USING_HEAP */
  28. static rt_uint32_t recv_event_times1 = 0, recv_event_times2 = 0;
  29. static rt_uint32_t static_event_recv_thread_finish = 0, static_event_send_thread_finish = 0;
  30. rt_align(RT_ALIGN_SIZE)
  31. static char thread1_stack[UTEST_THR_STACK_SIZE];
  32. static struct rt_thread thread1;
  33. rt_align(RT_ALIGN_SIZE)
  34. static char thread2_stack[UTEST_THR_STACK_SIZE];
  35. static struct rt_thread thread2;
  36. #define THREAD_PRIORITY 9
  37. #define THREAD_TIMESLICE 5
  38. static void test_event_init(void)
  39. {
  40. rt_err_t result;
  41. result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO);
  42. if (result != RT_EOK)
  43. {
  44. uassert_false(1);
  45. }
  46. result = rt_event_detach(&static_event);
  47. if (result != RT_EOK)
  48. {
  49. uassert_false(1);
  50. }
  51. result = rt_event_init(&static_event, "event", RT_IPC_FLAG_FIFO);
  52. if (result != RT_EOK)
  53. {
  54. uassert_false(1);
  55. }
  56. result = rt_event_detach(&static_event);
  57. if (result != RT_EOK)
  58. {
  59. uassert_false(1);
  60. }
  61. uassert_true(1);
  62. }
  63. static void test_event_detach(void)
  64. {
  65. rt_err_t result = RT_EOK;
  66. result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO);
  67. if (result != RT_EOK)
  68. {
  69. uassert_false(1);
  70. }
  71. result = rt_event_detach(&static_event);
  72. if (result != RT_EOK)
  73. {
  74. uassert_false(1);
  75. }
  76. uassert_true(1);
  77. }
  78. static void thread1_recv_static_event(void *param)
  79. {
  80. rt_uint32_t e;
  81. if (rt_event_recv(&static_event, (EVENT_FLAG3 | EVENT_FLAG5),
  82. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  83. RT_WAITING_FOREVER, &e) != RT_EOK)
  84. {
  85. return;
  86. }
  87. recv_event_times1 = e;
  88. rt_thread_mdelay(50);
  89. if (rt_event_recv(&static_event, (EVENT_FLAG3 | EVENT_FLAG5),
  90. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  91. RT_WAITING_FOREVER, &e) != RT_EOK)
  92. {
  93. return;
  94. }
  95. recv_event_times2 = e;
  96. static_event_recv_thread_finish = 1;
  97. }
  98. static void thread2_send_static_event(void *param)
  99. {
  100. rt_event_send(&static_event, EVENT_FLAG3);
  101. rt_thread_mdelay(10);
  102. rt_event_send(&static_event, EVENT_FLAG5);
  103. rt_thread_mdelay(10);
  104. rt_event_send(&static_event, EVENT_FLAG3);
  105. static_event_send_thread_finish = 1;
  106. }
  107. static void test_static_event_send_recv(void)
  108. {
  109. rt_err_t result = RT_EOK;
  110. result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO);
  111. if (result != RT_EOK)
  112. {
  113. uassert_false(1);
  114. }
  115. rt_thread_init(&thread1,
  116. "thread1",
  117. thread1_recv_static_event,
  118. RT_NULL,
  119. &thread1_stack[0],
  120. sizeof(thread1_stack),
  121. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  122. rt_thread_startup(&thread1);
  123. rt_thread_init(&thread2,
  124. "thread2",
  125. thread2_send_static_event,
  126. RT_NULL,
  127. &thread2_stack[0],
  128. sizeof(thread2_stack),
  129. THREAD_PRIORITY, THREAD_TIMESLICE);
  130. rt_thread_startup(&thread2);
  131. while (static_event_recv_thread_finish != 1 || static_event_send_thread_finish != 1)
  132. {
  133. rt_thread_delay(1);
  134. }
  135. if (recv_event_times1 == EVENT_FLAG3 && recv_event_times2 == (EVENT_FLAG3 | EVENT_FLAG5))
  136. {
  137. if (rt_event_detach(&static_event) != RT_EOK)
  138. {
  139. uassert_false(1);
  140. }
  141. uassert_true(1);
  142. }
  143. else
  144. {
  145. if (rt_event_detach(&static_event) != RT_EOK)
  146. {
  147. uassert_false(1);
  148. }
  149. uassert_false(1);
  150. }
  151. return;
  152. }
  153. #ifdef RT_USING_HEAP
  154. static void test_event_create(void)
  155. {
  156. rt_err_t result = RT_EOK;
  157. dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_FIFO);
  158. if (dynamic_event == RT_NULL)
  159. {
  160. uassert_false(1);
  161. }
  162. result = rt_event_delete(dynamic_event);
  163. if (result != RT_EOK)
  164. {
  165. uassert_false(1);
  166. }
  167. uassert_true(1);
  168. }
  169. static void test_event_delete(void)
  170. {
  171. rt_err_t result;
  172. dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_FIFO);
  173. if (dynamic_event == RT_NULL)
  174. {
  175. uassert_false(1);
  176. }
  177. result = rt_event_delete(dynamic_event);
  178. if (result != RT_EOK)
  179. {
  180. uassert_false(1);
  181. }
  182. uassert_true(1);
  183. }
  184. static void thread3_recv_dynamic_event(void *param)
  185. {
  186. rt_uint32_t e;
  187. if (rt_event_recv(dynamic_event, (EVENT_FLAG3 | EVENT_FLAG5),
  188. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  189. RT_WAITING_FOREVER, &e) != RT_EOK)
  190. {
  191. return;
  192. }
  193. recv_event_times1 = e;
  194. rt_thread_mdelay(50);
  195. if (rt_event_recv(dynamic_event, (EVENT_FLAG3 | EVENT_FLAG5),
  196. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  197. RT_WAITING_FOREVER, &e) != RT_EOK)
  198. {
  199. return;
  200. }
  201. recv_event_times2 = e;
  202. dynamic_event_recv_thread_finish = 1;
  203. }
  204. static void thread4_send_dynamic_event(void *param)
  205. {
  206. rt_event_send(dynamic_event, EVENT_FLAG3);
  207. rt_thread_mdelay(10);
  208. rt_event_send(dynamic_event, EVENT_FLAG5);
  209. rt_thread_mdelay(10);
  210. rt_event_send(dynamic_event, EVENT_FLAG3);
  211. dynamic_event_send_thread_finish = 1;
  212. }
  213. static void test_dynamic_event_send_recv(void)
  214. {
  215. dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_PRIO);
  216. if (dynamic_event == RT_NULL)
  217. {
  218. uassert_false(1);
  219. }
  220. rt_thread_init(&thread3,
  221. "thread3",
  222. thread3_recv_dynamic_event,
  223. RT_NULL,
  224. &thread3_stack[0],
  225. sizeof(thread3_stack),
  226. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  227. rt_thread_startup(&thread3);
  228. rt_thread_init(&thread4,
  229. "thread4",
  230. thread4_send_dynamic_event,
  231. RT_NULL,
  232. &thread4_stack[0],
  233. sizeof(thread4_stack),
  234. THREAD_PRIORITY, THREAD_TIMESLICE);
  235. rt_thread_startup(&thread4);
  236. while (dynamic_event_recv_thread_finish != 1 || dynamic_event_send_thread_finish != 1)
  237. {
  238. rt_thread_delay(1);
  239. }
  240. if (recv_event_times1 == EVENT_FLAG3 && recv_event_times2 == (EVENT_FLAG3 | EVENT_FLAG5))
  241. {
  242. if (rt_event_delete(dynamic_event) != RT_EOK)
  243. {
  244. uassert_false(1);
  245. }
  246. uassert_true(1);
  247. }
  248. else
  249. {
  250. if (rt_event_delete(dynamic_event) != RT_EOK)
  251. {
  252. uassert_false(1);
  253. }
  254. uassert_false(1);
  255. }
  256. return;
  257. }
  258. #endif
  259. static rt_err_t utest_tc_init(void)
  260. {
  261. static_event_recv_thread_finish = 0;
  262. static_event_send_thread_finish = 0;
  263. #ifdef RT_USING_HEAP
  264. dynamic_event_recv_thread_finish = 0;
  265. dynamic_event_send_thread_finish = 0;
  266. #endif
  267. return RT_EOK;
  268. }
  269. static rt_err_t utest_tc_cleanup(void)
  270. {
  271. return RT_EOK;
  272. }
  273. static void testcase(void)
  274. {
  275. UTEST_UNIT_RUN(test_event_init);
  276. UTEST_UNIT_RUN(test_event_detach);
  277. UTEST_UNIT_RUN(test_static_event_send_recv);
  278. #ifdef RT_USING_HEAP
  279. UTEST_UNIT_RUN(test_event_create);
  280. UTEST_UNIT_RUN(test_event_delete);
  281. UTEST_UNIT_RUN(test_dynamic_event_send_recv);
  282. #endif
  283. }
  284. UTEST_TC_EXPORT(testcase, "src.ipc.event_tc", utest_tc_init, utest_tc_cleanup, 60);