rt_test_sequence_003.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_003.c
  17. * @brief Test Sequence 003 code.
  18. *
  19. * @page rt_test_sequence_003 [3] Threads Functionality
  20. *
  21. * File: @ref rt_test_sequence_003.c
  22. *
  23. * <h2>Description</h2>
  24. * This sequence tests the ChibiOS/RT functionalities related to
  25. * threading.
  26. *
  27. * <h2>Test Cases</h2>
  28. * - @subpage rt_test_003_001
  29. * - @subpage rt_test_003_002
  30. * - @subpage rt_test_003_003
  31. * - @subpage rt_test_003_004
  32. * .
  33. */
  34. /****************************************************************************
  35. * Shared code.
  36. ****************************************************************************/
  37. static THD_FUNCTION(thread, p) {
  38. test_emit_token(*(char *)p);
  39. }
  40. /****************************************************************************
  41. * Test cases.
  42. ****************************************************************************/
  43. /**
  44. * @page rt_test_003_001 [3.1] Thread Sleep functionality
  45. *
  46. * <h2>Description</h2>
  47. * The functionality of @p chThdSleep() and derivatives is tested.
  48. *
  49. * <h2>Test Steps</h2>
  50. * - [3.1.1] The current system time is read then a sleep is performed
  51. * for 100 system ticks and on exit the system time is verified
  52. * again.
  53. * - [3.1.2] The current system time is read then a sleep is performed
  54. * for 100000 microseconds and on exit the system time is verified
  55. * again.
  56. * - [3.1.3] The current system time is read then a sleep is performed
  57. * for 100 milliseconds and on exit the system time is verified
  58. * again.
  59. * - [3.1.4] The current system time is read then a sleep is performed
  60. * for 1 second and on exit the system time is verified again.
  61. * - [3.1.5] Function chThdSleepUntil() is tested with a timeline of
  62. * "now" + 100 ticks.
  63. * .
  64. */
  65. static void rt_test_003_001_execute(void) {
  66. systime_t time;
  67. /* [3.1.1] The current system time is read then a sleep is performed
  68. for 100 system ticks and on exit the system time is verified
  69. again.*/
  70. test_set_step(1);
  71. {
  72. time = chVTGetSystemTimeX();
  73. chThdSleep(100);
  74. test_assert_time_window(chTimeAddX(time, 100),
  75. chTimeAddX(time, 100 + CH_CFG_ST_TIMEDELTA + 1),
  76. "out of time window");
  77. }
  78. /* [3.1.2] The current system time is read then a sleep is performed
  79. for 100000 microseconds and on exit the system time is verified
  80. again.*/
  81. test_set_step(2);
  82. {
  83. time = chVTGetSystemTimeX();
  84. chThdSleepMicroseconds(100000);
  85. test_assert_time_window(chTimeAddX(time, TIME_US2I(100000)),
  86. chTimeAddX(time, TIME_US2I(100000) + CH_CFG_ST_TIMEDELTA + 1),
  87. "out of time window");
  88. }
  89. /* [3.1.3] The current system time is read then a sleep is performed
  90. for 100 milliseconds and on exit the system time is verified
  91. again.*/
  92. test_set_step(3);
  93. {
  94. time = chVTGetSystemTimeX();
  95. chThdSleepMilliseconds(100);
  96. test_assert_time_window(chTimeAddX(time, TIME_MS2I(100)),
  97. chTimeAddX(time, TIME_MS2I(100) + CH_CFG_ST_TIMEDELTA + 1),
  98. "out of time window");
  99. }
  100. /* [3.1.4] The current system time is read then a sleep is performed
  101. for 1 second and on exit the system time is verified again.*/
  102. test_set_step(4);
  103. {
  104. time = chVTGetSystemTimeX();
  105. chThdSleepSeconds(1);
  106. test_assert_time_window(chTimeAddX(time, TIME_S2I(1)),
  107. chTimeAddX(time, TIME_S2I(1) + CH_CFG_ST_TIMEDELTA + 1),
  108. "out of time window");
  109. }
  110. /* [3.1.5] Function chThdSleepUntil() is tested with a timeline of
  111. "now" + 100 ticks.*/
  112. test_set_step(5);
  113. {
  114. time = chVTGetSystemTimeX();
  115. chThdSleepUntil(chTimeAddX(time, 100));
  116. test_assert_time_window(chTimeAddX(time, 100),
  117. chTimeAddX(time, 100 + CH_CFG_ST_TIMEDELTA + 1),
  118. "out of time window");
  119. }
  120. }
  121. static const testcase_t rt_test_003_001 = {
  122. "Thread Sleep functionality",
  123. NULL,
  124. NULL,
  125. rt_test_003_001_execute
  126. };
  127. /**
  128. * @page rt_test_003_002 [3.2] Ready List functionality, threads priority order
  129. *
  130. * <h2>Description</h2>
  131. * Five threads, are enqueued in the ready list and atomically
  132. * executed. The test expects the threads to perform their operations
  133. * in correct priority order regardless of the initial order.
  134. *
  135. * <h2>Test Steps</h2>
  136. * - [3.2.1] Creating 5 threads with increasing priority, execution
  137. * sequence is tested.
  138. * - [3.2.2] Creating 5 threads with decreasing priority, execution
  139. * sequence is tested.
  140. * - [3.2.3] Creating 5 threads with pseudo-random priority, execution
  141. * sequence is tested.
  142. * .
  143. */
  144. static void rt_test_003_002_execute(void) {
  145. /* [3.2.1] Creating 5 threads with increasing priority, execution
  146. sequence is tested.*/
  147. test_set_step(1);
  148. {
  149. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-5, thread, "E");
  150. threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-4, thread, "D");
  151. threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread, "C");
  152. threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriorityX()-2, thread, "B");
  153. threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriorityX()-1, thread, "A");
  154. test_wait_threads();
  155. test_assert_sequence("ABCDE", "invalid sequence");
  156. }
  157. /* [3.2.2] Creating 5 threads with decreasing priority, execution
  158. sequence is tested.*/
  159. test_set_step(2);
  160. {
  161. threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriorityX()-1, thread, "A");
  162. threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriorityX()-2, thread, "B");
  163. threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread, "C");
  164. threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-4, thread, "D");
  165. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-5, thread, "E");
  166. test_wait_threads();
  167. test_assert_sequence("ABCDE", "invalid sequence");
  168. }
  169. /* [3.2.3] Creating 5 threads with pseudo-random priority, execution
  170. sequence is tested.*/
  171. test_set_step(3);
  172. {
  173. threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-4, thread, "D");
  174. threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-5, thread, "E");
  175. threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriorityX()-1, thread, "A");
  176. threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriorityX()-2, thread, "B");
  177. threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread, "C");
  178. test_wait_threads();
  179. test_assert_sequence("ABCDE", "invalid sequence");
  180. }
  181. }
  182. static const testcase_t rt_test_003_002 = {
  183. "Ready List functionality, threads priority order",
  184. NULL,
  185. NULL,
  186. rt_test_003_002_execute
  187. };
  188. /**
  189. * @page rt_test_003_003 [3.3] Priority change test
  190. *
  191. * <h2>Description</h2>
  192. * A series of priority changes are performed on the current thread in
  193. * order to verify that the priority change happens as expected.
  194. *
  195. * <h2>Test Steps</h2>
  196. * - [3.3.1] Thread priority is increased by one then a check is
  197. * performed.
  198. * - [3.3.2] Thread priority is returned to the previous value then a
  199. * check is performed.
  200. * .
  201. */
  202. static void rt_test_003_003_execute(void) {
  203. tprio_t prio, p1;
  204. /* [3.3.1] Thread priority is increased by one then a check is
  205. performed.*/
  206. test_set_step(1);
  207. {
  208. prio = chThdGetPriorityX();
  209. p1 = chThdSetPriority(prio + 1);
  210. test_assert(p1 == prio, "unexpected returned priority level");
  211. test_assert(chThdGetPriorityX() == prio + 1, "unexpected priority level");
  212. }
  213. /* [3.3.2] Thread priority is returned to the previous value then a
  214. check is performed.*/
  215. test_set_step(2);
  216. {
  217. p1 = chThdSetPriority(p1);
  218. test_assert(p1 == prio + 1, "unexpected returned priority level");
  219. test_assert(chThdGetPriorityX() == prio, "unexpected priority level");
  220. }
  221. }
  222. static const testcase_t rt_test_003_003 = {
  223. "Priority change test",
  224. NULL,
  225. NULL,
  226. rt_test_003_003_execute
  227. };
  228. #if (CH_CFG_USE_MUTEXES) || defined(__DOXYGEN__)
  229. /**
  230. * @page rt_test_003_004 [3.4] Priority change test with Priority Inheritance
  231. *
  232. * <h2>Description</h2>
  233. * A series of priority changes are performed on the current thread in
  234. * order to verify that the priority change happens as expected.
  235. *
  236. * <h2>Conditions</h2>
  237. * This test is only executed if the following preprocessor condition
  238. * evaluates to true:
  239. * - CH_CFG_USE_MUTEXES
  240. * .
  241. *
  242. * <h2>Test Steps</h2>
  243. * - [3.4.1] Simulating a priority boost situation (prio > realprio).
  244. * - [3.4.2] Raising thread priority above original priority but below
  245. * the boosted level.
  246. * - [3.4.3] Raising thread priority above the boosted level.
  247. * - [3.4.4] Restoring original conditions.
  248. * .
  249. */
  250. static void rt_test_003_004_execute(void) {
  251. tprio_t prio, p1;
  252. /* [3.4.1] Simulating a priority boost situation (prio > realprio).*/
  253. test_set_step(1);
  254. {
  255. prio = chThdGetPriorityX();
  256. chThdGetSelfX()->prio += 2;
  257. test_assert(chThdGetPriorityX() == prio + 2, "unexpected priority level");
  258. }
  259. /* [3.4.2] Raising thread priority above original priority but below
  260. the boosted level.*/
  261. test_set_step(2);
  262. {
  263. p1 = chThdSetPriority(prio + 1);
  264. test_assert(p1 == prio, "unexpected returned priority level");
  265. test_assert(chThdGetSelfX()->prio == prio + 2, "unexpected priority level");
  266. test_assert(chThdGetSelfX()->realprio == prio + 1, "unexpected returned real priority level");
  267. }
  268. /* [3.4.3] Raising thread priority above the boosted level.*/
  269. test_set_step(3);
  270. {
  271. p1 = chThdSetPriority(prio + 3);
  272. test_assert(p1 == prio + 1, "unexpected returned priority level");
  273. test_assert(chThdGetSelfX()->prio == prio + 3, "unexpected priority level");
  274. test_assert(chThdGetSelfX()->realprio == prio + 3, "unexpected real priority level");
  275. }
  276. /* [3.4.4] Restoring original conditions.*/
  277. test_set_step(4);
  278. {
  279. chSysLock();
  280. chThdGetSelfX()->prio = prio;
  281. chThdGetSelfX()->realprio = prio;
  282. chSysUnlock();
  283. }
  284. }
  285. static const testcase_t rt_test_003_004 = {
  286. "Priority change test with Priority Inheritance",
  287. NULL,
  288. NULL,
  289. rt_test_003_004_execute
  290. };
  291. #endif /* CH_CFG_USE_MUTEXES */
  292. /****************************************************************************
  293. * Exported data.
  294. ****************************************************************************/
  295. /**
  296. * @brief Array of test cases.
  297. */
  298. const testcase_t * const rt_test_sequence_003_array[] = {
  299. &rt_test_003_001,
  300. &rt_test_003_002,
  301. &rt_test_003_003,
  302. #if (CH_CFG_USE_MUTEXES) || defined(__DOXYGEN__)
  303. &rt_test_003_004,
  304. #endif
  305. NULL
  306. };
  307. /**
  308. * @brief Threads Functionality.
  309. */
  310. const testsequence_t rt_test_sequence_003 = {
  311. "Threads Functionality",
  312. rt_test_sequence_003_array
  313. };