rt_test_sequence_008.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "hal.h"
  14. #include "rt_test_root.h"
  15. /**
  16. * @file rt_test_sequence_008.c
  17. * @brief Test Sequence 008 code.
  18. *
  19. * @page rt_test_sequence_008 [8] Event Sources and Event Flags
  20. *
  21. * File: @ref rt_test_sequence_008.c
  22. *
  23. * <h2>Description</h2>
  24. * This module implements the test sequence for the Events subsystem.
  25. *
  26. * <h2>Conditions</h2>
  27. * This sequence is only executed if the following preprocessor condition
  28. * evaluates to true:
  29. * - CH_CFG_USE_EVENTS
  30. * .
  31. *
  32. * <h2>Test Cases</h2>
  33. * - @subpage rt_test_008_001
  34. * - @subpage rt_test_008_002
  35. * - @subpage rt_test_008_003
  36. * - @subpage rt_test_008_004
  37. * - @subpage rt_test_008_005
  38. * - @subpage rt_test_008_006
  39. * - @subpage rt_test_008_007
  40. * .
  41. */
  42. #if (CH_CFG_USE_EVENTS) || defined(__DOXYGEN__)
  43. /****************************************************************************
  44. * Shared code.
  45. ****************************************************************************/
  46. static EVENTSOURCE_DECL(es1);
  47. static EVENTSOURCE_DECL(es2);
  48. static void h1(eventid_t id) {(void)id;test_emit_token('A');}
  49. static void h2(eventid_t id) {(void)id;test_emit_token('B');}
  50. static void h3(eventid_t id) {(void)id;test_emit_token('C');}
  51. static ROMCONST evhandler_t evhndl[] = {h1, h2, h3};
  52. static THD_FUNCTION(evt_thread3, p) {
  53. chThdSleepMilliseconds(50);
  54. chEvtSignal((thread_t *)p, 1);
  55. }
  56. static THD_FUNCTION(evt_thread7, p) {
  57. (void)p;
  58. chEvtBroadcast(&es1);
  59. chThdSleepMilliseconds(50);
  60. chEvtBroadcast(&es2);
  61. }
  62. /****************************************************************************
  63. * Test cases.
  64. ****************************************************************************/
  65. /**
  66. * @page rt_test_008_001 [8.1] Events registration
  67. *
  68. * <h2>Description</h2>
  69. * Two event listeners are registered on an event source and then
  70. * unregistered in the same order.<br> The test expects that the even
  71. * source has listeners after the registrations and after the first
  72. * unregistration, then, after the second unegistration, the test
  73. * expects no more listeners.
  74. *
  75. * <h2>Test Steps</h2>
  76. * - [8.1.1] An Event Source is initialized.
  77. * - [8.1.2] Two Event Listeners are registered on the Event Source,
  78. * the Event Source is tested to have listeners.
  79. * - [8.1.3] An Event Listener is unregistered, the Event Source must
  80. * still have listeners.
  81. * - [8.1.4] An Event Listener is unregistered, the Event Source must
  82. * not have listeners.
  83. * .
  84. */
  85. static void rt_test_008_001_execute(void) {
  86. event_listener_t el1, el2;
  87. /* [8.1.1] An Event Source is initialized.*/
  88. test_set_step(1);
  89. {
  90. chEvtObjectInit(&es1);
  91. }
  92. /* [8.1.2] Two Event Listeners are registered on the Event Source,
  93. the Event Source is tested to have listeners.*/
  94. test_set_step(2);
  95. {
  96. chEvtRegisterMask(&es1, &el1, 1);
  97. chEvtRegisterMask(&es1, &el2, 2);
  98. test_assert_lock(chEvtIsListeningI(&es1), "no listener");
  99. }
  100. /* [8.1.3] An Event Listener is unregistered, the Event Source must
  101. still have listeners.*/
  102. test_set_step(3);
  103. {
  104. chEvtUnregister(&es1, &el1);
  105. test_assert_lock(chEvtIsListeningI(&es1), "no listener");
  106. }
  107. /* [8.1.4] An Event Listener is unregistered, the Event Source must
  108. not have listeners.*/
  109. test_set_step(4);
  110. {
  111. chEvtUnregister(&es1, &el2);
  112. test_assert_lock(!chEvtIsListeningI(&es1), "stuck listener");
  113. }
  114. }
  115. static const testcase_t rt_test_008_001 = {
  116. "Events registration",
  117. NULL,
  118. NULL,
  119. rt_test_008_001_execute
  120. };
  121. /**
  122. * @page rt_test_008_002 [8.2] Event Flags dispatching
  123. *
  124. * <h2>Description</h2>
  125. * The test dispatches three event flags and verifies that the
  126. * associated event handlers are invoked in LSb-first order.
  127. *
  128. * <h2>Test Steps</h2>
  129. * - [8.2.1] Three evenf flag bits are raised then chEvtDispatch() is
  130. * invoked, the sequence of handlers calls is tested.
  131. * .
  132. */
  133. static void rt_test_008_002_setup(void) {
  134. chEvtGetAndClearEvents(ALL_EVENTS);
  135. }
  136. static void rt_test_008_002_execute(void) {
  137. /* [8.2.1] Three evenf flag bits are raised then chEvtDispatch() is
  138. invoked, the sequence of handlers calls is tested.*/
  139. test_set_step(1);
  140. {
  141. chEvtDispatch(evhndl, 7);
  142. test_assert_sequence("ABC", "invalid sequence");
  143. }
  144. }
  145. static const testcase_t rt_test_008_002 = {
  146. "Event Flags dispatching",
  147. rt_test_008_002_setup,
  148. NULL,
  149. rt_test_008_002_execute
  150. };
  151. /**
  152. * @page rt_test_008_003 [8.3] Events Flags wait using chEvtWaitOne()
  153. *
  154. * <h2>Description</h2>
  155. * Functionality of chEvtWaitOne() is tested under various scenarios.
  156. *
  157. * <h2>Test Steps</h2>
  158. * - [8.3.1] Setting three event flags.
  159. * - [8.3.2] Calling chEvtWaitOne() three times, each time a single
  160. * flag must be returned in order of priority.
  161. * - [8.3.3] Getting current time and starting a signaler thread, the
  162. * thread will set an event flag after 50mS.
  163. * - [8.3.4] Calling chEvtWaitOne() then verifying that the event has
  164. * been received after 50mS and that the event flags mask has been
  165. * emptied.
  166. * .
  167. */
  168. static void rt_test_008_003_setup(void) {
  169. chEvtGetAndClearEvents(ALL_EVENTS);
  170. }
  171. static void rt_test_008_003_execute(void) {
  172. eventmask_t m;
  173. systime_t target_time;
  174. /* [8.3.1] Setting three event flags.*/
  175. test_set_step(1);
  176. {
  177. chEvtAddEvents(7);
  178. }
  179. /* [8.3.2] Calling chEvtWaitOne() three times, each time a single
  180. flag must be returned in order of priority.*/
  181. test_set_step(2);
  182. {
  183. m = chEvtWaitOne(ALL_EVENTS);
  184. test_assert(m == 1, "single event error");
  185. m = chEvtWaitOne(ALL_EVENTS);
  186. test_assert(m == 2, "single event error");
  187. m = chEvtWaitOne(ALL_EVENTS);
  188. test_assert(m == 4, "single event error");
  189. m = chEvtGetAndClearEvents(ALL_EVENTS);
  190. test_assert(m == 0, "stuck event");
  191. }
  192. /* [8.3.3] Getting current time and starting a signaler thread, the
  193. thread will set an event flag after 50mS.*/
  194. test_set_step(3);
  195. {
  196. target_time = chTimeAddX(test_wait_tick(), TIME_MS2I(50));
  197. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1,
  198. evt_thread3, chThdGetSelfX());
  199. }
  200. /* [8.3.4] Calling chEvtWaitOne() then verifying that the event has
  201. been received after 50mS and that the event flags mask has been
  202. emptied.*/
  203. test_set_step(4);
  204. {
  205. m = chEvtWaitOne(ALL_EVENTS);
  206. test_assert_time_window(target_time,
  207. chTimeAddX(target_time, ALLOWED_DELAY),
  208. "out of time window");
  209. test_assert(m == 1, "event flag error");
  210. m = chEvtGetAndClearEvents(ALL_EVENTS);
  211. test_assert(m == 0, "stuck event");
  212. test_wait_threads();
  213. }
  214. }
  215. static const testcase_t rt_test_008_003 = {
  216. "Events Flags wait using chEvtWaitOne()",
  217. rt_test_008_003_setup,
  218. NULL,
  219. rt_test_008_003_execute
  220. };
  221. /**
  222. * @page rt_test_008_004 [8.4] Events Flags wait using chEvtWaitAny()
  223. *
  224. * <h2>Description</h2>
  225. * Functionality of chEvtWaitAny() is tested under various scenarios.
  226. *
  227. * <h2>Test Steps</h2>
  228. * - [8.4.1] Setting two, non contiguous, event flags.
  229. * - [8.4.2] Calling chEvtWaitAny() one time, the two flags must be
  230. * returned.
  231. * - [8.4.3] Getting current time and starting a signaler thread, the
  232. * thread will set an event flag after 50mS.
  233. * - [8.4.4] Calling chEvtWaitAny() then verifying that the event has
  234. * been received after 50mS and that the event flags mask has been
  235. * emptied.
  236. * .
  237. */
  238. static void rt_test_008_004_setup(void) {
  239. chEvtGetAndClearEvents(ALL_EVENTS);
  240. }
  241. static void rt_test_008_004_execute(void) {
  242. eventmask_t m;
  243. systime_t target_time;
  244. /* [8.4.1] Setting two, non contiguous, event flags.*/
  245. test_set_step(1);
  246. {
  247. chEvtAddEvents(5);
  248. }
  249. /* [8.4.2] Calling chEvtWaitAny() one time, the two flags must be
  250. returned.*/
  251. test_set_step(2);
  252. {
  253. m = chEvtWaitAny(ALL_EVENTS);
  254. test_assert(m == 5, "unexpected pending bit");
  255. m = chEvtGetAndClearEvents(ALL_EVENTS);
  256. test_assert(m == 0, "stuck event");
  257. }
  258. /* [8.4.3] Getting current time and starting a signaler thread, the
  259. thread will set an event flag after 50mS.*/
  260. test_set_step(3);
  261. {
  262. target_time = chTimeAddX(test_wait_tick(), TIME_MS2I(50));
  263. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1,
  264. evt_thread3, chThdGetSelfX());
  265. }
  266. /* [8.4.4] Calling chEvtWaitAny() then verifying that the event has
  267. been received after 50mS and that the event flags mask has been
  268. emptied.*/
  269. test_set_step(4);
  270. {
  271. m = chEvtWaitAny(ALL_EVENTS);
  272. test_assert_time_window(target_time,
  273. chTimeAddX(target_time, ALLOWED_DELAY),
  274. "out of time window");
  275. test_assert(m == 1, "event flag error");
  276. m = chEvtGetAndClearEvents(ALL_EVENTS);
  277. test_assert(m == 0, "stuck event");
  278. test_wait_threads();
  279. }
  280. }
  281. static const testcase_t rt_test_008_004 = {
  282. "Events Flags wait using chEvtWaitAny()",
  283. rt_test_008_004_setup,
  284. NULL,
  285. rt_test_008_004_execute
  286. };
  287. /**
  288. * @page rt_test_008_005 [8.5] Events Flags wait using chEvtWaitAll()
  289. *
  290. * <h2>Description</h2>
  291. * Functionality of chEvtWaitAll() is tested under various scenarios.
  292. *
  293. * <h2>Test Steps</h2>
  294. * - [8.5.1] Setting two, non contiguous, event flags.
  295. * - [8.5.2] Calling chEvtWaitAll() one time, the two flags must be
  296. * returned.
  297. * - [8.5.3] Setting one event flag.
  298. * - [8.5.4] Getting current time and starting a signaler thread, the
  299. * thread will set another event flag after 50mS.
  300. * - [8.5.5] Calling chEvtWaitAll() then verifying that both event
  301. * flags have been received after 50mS and that the event flags mask
  302. * has been emptied.
  303. * .
  304. */
  305. static void rt_test_008_005_setup(void) {
  306. chEvtGetAndClearEvents(ALL_EVENTS);
  307. }
  308. static void rt_test_008_005_execute(void) {
  309. eventmask_t m;
  310. systime_t target_time;
  311. /* [8.5.1] Setting two, non contiguous, event flags.*/
  312. test_set_step(1);
  313. {
  314. chEvtAddEvents(5);
  315. }
  316. /* [8.5.2] Calling chEvtWaitAll() one time, the two flags must be
  317. returned.*/
  318. test_set_step(2);
  319. {
  320. m = chEvtWaitAll(5);
  321. test_assert(m == 5, "unexpected pending bit");
  322. m = chEvtGetAndClearEvents(ALL_EVENTS);
  323. test_assert(m == 0, "stuck event");
  324. }
  325. /* [8.5.3] Setting one event flag.*/
  326. test_set_step(3);
  327. {
  328. chEvtAddEvents(4);
  329. }
  330. /* [8.5.4] Getting current time and starting a signaler thread, the
  331. thread will set another event flag after 50mS.*/
  332. test_set_step(4);
  333. {
  334. target_time = chTimeAddX(test_wait_tick(), TIME_MS2I(50));
  335. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1,
  336. evt_thread3, chThdGetSelfX());
  337. }
  338. /* [8.5.5] Calling chEvtWaitAll() then verifying that both event
  339. flags have been received after 50mS and that the event flags mask
  340. has been emptied.*/
  341. test_set_step(5);
  342. {
  343. m = chEvtWaitAll(5);
  344. test_assert_time_window(target_time,
  345. chTimeAddX(target_time, ALLOWED_DELAY),
  346. "out of time window");
  347. test_assert(m == 5, "event flags error");
  348. m = chEvtGetAndClearEvents(ALL_EVENTS);
  349. test_assert(m == 0, "stuck event");
  350. test_wait_threads();
  351. }
  352. }
  353. static const testcase_t rt_test_008_005 = {
  354. "Events Flags wait using chEvtWaitAll()",
  355. rt_test_008_005_setup,
  356. NULL,
  357. rt_test_008_005_execute
  358. };
  359. #if (CH_CFG_USE_EVENTS_TIMEOUT) || defined(__DOXYGEN__)
  360. /**
  361. * @page rt_test_008_006 [8.6] Events Flags wait timeouts
  362. *
  363. * <h2>Description</h2>
  364. * Timeout functionality is tested for chEvtWaitOneTimeout(),
  365. * chEvtWaitAnyTimeout() and chEvtWaitAllTimeout().
  366. *
  367. * <h2>Conditions</h2>
  368. * This test is only executed if the following preprocessor condition
  369. * evaluates to true:
  370. * - CH_CFG_USE_EVENTS_TIMEOUT
  371. * .
  372. *
  373. * <h2>Test Steps</h2>
  374. * - [8.6.1] The functions are invoked first with TIME_IMMEDIATE
  375. * timeout, the timeout condition is tested.
  376. * - [8.6.2] The functions are invoked first with a 50mS timeout, the
  377. * timeout condition is tested.
  378. * .
  379. */
  380. static void rt_test_008_006_setup(void) {
  381. chEvtGetAndClearEvents(ALL_EVENTS);
  382. }
  383. static void rt_test_008_006_execute(void) {
  384. eventmask_t m;
  385. /* [8.6.1] The functions are invoked first with TIME_IMMEDIATE
  386. timeout, the timeout condition is tested.*/
  387. test_set_step(1);
  388. {
  389. m = chEvtWaitOneTimeout(ALL_EVENTS, TIME_IMMEDIATE);
  390. test_assert(m == 0, "spurious event");
  391. m = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_IMMEDIATE);
  392. test_assert(m == 0, "spurious event");
  393. m = chEvtWaitAllTimeout(ALL_EVENTS, TIME_IMMEDIATE);
  394. test_assert(m == 0, "spurious event");
  395. }
  396. /* [8.6.2] The functions are invoked first with a 50mS timeout, the
  397. timeout condition is tested.*/
  398. test_set_step(2);
  399. {
  400. m = chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(50));
  401. test_assert(m == 0, "spurious event");
  402. m = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(50));
  403. test_assert(m == 0, "spurious event");
  404. m = chEvtWaitAllTimeout(ALL_EVENTS, TIME_MS2I(50));
  405. test_assert(m == 0, "spurious event");
  406. }
  407. }
  408. static const testcase_t rt_test_008_006 = {
  409. "Events Flags wait timeouts",
  410. rt_test_008_006_setup,
  411. NULL,
  412. rt_test_008_006_execute
  413. };
  414. #endif /* CH_CFG_USE_EVENTS_TIMEOUT */
  415. /**
  416. * @page rt_test_008_007 [8.7] Broadcasting using chEvtBroadcast()
  417. *
  418. * <h2>Description</h2>
  419. * Functionality of chEvtBroadcast() is tested.
  420. *
  421. * <h2>Test Steps</h2>
  422. * - [8.7.1] Registering on two event sources associating them with
  423. * flags 1 and 4.
  424. * - [8.7.2] Getting current time and starting a broadcaster thread,
  425. * the thread broadcast the first Event Source immediately and the
  426. * other after 50mS.
  427. * - [8.7.3] Calling chEvtWaitAll() then verifying that both event
  428. * flags have been received after 50mS and that the event flags mask
  429. * has been emptied.
  430. * - [8.7.4] Unregistering from the Event Sources.
  431. * .
  432. */
  433. static void rt_test_008_007_setup(void) {
  434. chEvtGetAndClearEvents(ALL_EVENTS);
  435. chEvtObjectInit(&es1);
  436. chEvtObjectInit(&es2);
  437. }
  438. static void rt_test_008_007_execute(void) {
  439. eventmask_t m;
  440. event_listener_t el1, el2;
  441. systime_t target_time;
  442. /* [8.7.1] Registering on two event sources associating them with
  443. flags 1 and 4.*/
  444. test_set_step(1);
  445. {
  446. chEvtRegisterMask(&es1, &el1, 1);
  447. chEvtRegisterMask(&es2, &el2, 4);
  448. }
  449. /* [8.7.2] Getting current time and starting a broadcaster thread,
  450. the thread broadcast the first Event Source immediately and the
  451. other after 50mS.*/
  452. test_set_step(2);
  453. {
  454. target_time = chTimeAddX(test_wait_tick(), TIME_MS2I(50));
  455. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1,
  456. evt_thread7, "A");
  457. }
  458. /* [8.7.3] Calling chEvtWaitAll() then verifying that both event
  459. flags have been received after 50mS and that the event flags mask
  460. has been emptied.*/
  461. test_set_step(3);
  462. {
  463. m = chEvtWaitAll(5);
  464. test_assert_time_window(target_time,
  465. chTimeAddX(target_time, ALLOWED_DELAY),
  466. "out of time window");
  467. m = chEvtGetAndClearEvents(ALL_EVENTS);
  468. test_assert(m == 0, "stuck event");
  469. test_wait_threads();
  470. }
  471. /* [8.7.4] Unregistering from the Event Sources.*/
  472. test_set_step(4);
  473. {
  474. chEvtUnregister(&es1, &el1);
  475. chEvtUnregister(&es2, &el2);
  476. test_assert(!chEvtIsListeningI(&es1), "stuck listener");
  477. test_assert(!chEvtIsListeningI(&es2), "stuck listener");
  478. }
  479. }
  480. static const testcase_t rt_test_008_007 = {
  481. "Broadcasting using chEvtBroadcast()",
  482. rt_test_008_007_setup,
  483. NULL,
  484. rt_test_008_007_execute
  485. };
  486. /****************************************************************************
  487. * Exported data.
  488. ****************************************************************************/
  489. /**
  490. * @brief Array of test cases.
  491. */
  492. const testcase_t * const rt_test_sequence_008_array[] = {
  493. &rt_test_008_001,
  494. &rt_test_008_002,
  495. &rt_test_008_003,
  496. &rt_test_008_004,
  497. &rt_test_008_005,
  498. #if (CH_CFG_USE_EVENTS_TIMEOUT) || defined(__DOXYGEN__)
  499. &rt_test_008_006,
  500. #endif
  501. &rt_test_008_007,
  502. NULL
  503. };
  504. /**
  505. * @brief Event Sources and Event Flags.
  506. */
  507. const testsequence_t rt_test_sequence_008 = {
  508. "Event Sources and Event Flags",
  509. rt_test_sequence_008_array
  510. };
  511. #endif /* CH_CFG_USE_EVENTS */