event_tc.c 7.8 KB

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