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