oslib_test_sequence_002.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. ChibiOS - Copyright (C) 2006..2017 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 "oslib_test_root.h"
  15. /**
  16. * @file oslib_test_sequence_002.c
  17. * @brief Test Sequence 002 code.
  18. *
  19. * @page oslib_test_sequence_002 [2] Pipes
  20. *
  21. * File: @ref oslib_test_sequence_002.c
  22. *
  23. * <h2>Description</h2>
  24. * This sequence tests the ChibiOS library functionalities related to
  25. * pipes.
  26. *
  27. * <h2>Conditions</h2>
  28. * This sequence is only executed if the following preprocessor condition
  29. * evaluates to true:
  30. * - CH_CFG_USE_PIPES
  31. * .
  32. *
  33. * <h2>Test Cases</h2>
  34. * - @subpage oslib_test_002_001
  35. * - @subpage oslib_test_002_002
  36. * .
  37. */
  38. #if (CH_CFG_USE_PIPES) || defined(__DOXYGEN__)
  39. /****************************************************************************
  40. * Shared code.
  41. ****************************************************************************/
  42. #include <string.h>
  43. #define PIPE_SIZE 16
  44. static uint8_t buffer[PIPE_SIZE];
  45. static PIPE_DECL(pipe1, buffer, PIPE_SIZE);
  46. static const uint8_t pipe_pattern[] = "0123456789ABCDEF";
  47. /****************************************************************************
  48. * Test cases.
  49. ****************************************************************************/
  50. /**
  51. * @page oslib_test_002_001 [2.1] Pipes normal API, non-blocking tests
  52. *
  53. * <h2>Description</h2>
  54. * The pipe functionality is tested by loading and emptying it, all
  55. * conditions are tested.
  56. *
  57. * <h2>Test Steps</h2>
  58. * - [2.1.1] Resetting pipe.
  59. * - [2.1.2] Writing data, must fail.
  60. * - [2.1.3] Reading data, must fail.
  61. * - [2.1.4] Reactivating pipe.
  62. * - [2.1.5] Filling whole pipe.
  63. * - [2.1.6] Emptying pipe.
  64. * - [2.1.7] Small write.
  65. * - [2.1.8] Filling remaining space.
  66. * - [2.1.9] Small Read.
  67. * - [2.1.10] Reading remaining data.
  68. * - [2.1.11] Small Write.
  69. * - [2.1.12] Small Read.
  70. * - [2.1.13] Write wrapping buffer boundary.
  71. * - [2.1.14] Read wrapping buffer boundary.
  72. * .
  73. */
  74. static void oslib_test_002_001_setup(void) {
  75. chPipeObjectInit(&pipe1, buffer, PIPE_SIZE);
  76. }
  77. static void oslib_test_002_001_execute(void) {
  78. /* [2.1.1] Resetting pipe.*/
  79. test_set_step(1);
  80. {
  81. chPipeReset(&pipe1);
  82. test_assert((pipe1.rdptr == pipe1.buffer) &&
  83. (pipe1.wrptr == pipe1.buffer) &&
  84. (pipe1.cnt == 0),
  85. "invalid pipe state");
  86. }
  87. /* [2.1.2] Writing data, must fail.*/
  88. test_set_step(2);
  89. {
  90. size_t n;
  91. n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
  92. test_assert(n == 0, "not reset");
  93. test_assert((pipe1.rdptr == pipe1.buffer) &&
  94. (pipe1.wrptr == pipe1.buffer) &&
  95. (pipe1.cnt == 0),
  96. "invalid pipe state");
  97. }
  98. /* [2.1.3] Reading data, must fail.*/
  99. test_set_step(3);
  100. {
  101. size_t n;
  102. uint8_t buf[PIPE_SIZE];
  103. n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
  104. test_assert(n == 0, "not reset");
  105. test_assert((pipe1.rdptr == pipe1.buffer) &&
  106. (pipe1.wrptr == pipe1.buffer) &&
  107. (pipe1.cnt == 0),
  108. "invalid pipe state");
  109. }
  110. /* [2.1.4] Reactivating pipe.*/
  111. test_set_step(4);
  112. {
  113. chPipeResume(&pipe1);
  114. test_assert((pipe1.rdptr == pipe1.buffer) &&
  115. (pipe1.wrptr == pipe1.buffer) &&
  116. (pipe1.cnt == 0),
  117. "invalid pipe state");
  118. }
  119. /* [2.1.5] Filling whole pipe.*/
  120. test_set_step(5);
  121. {
  122. size_t n;
  123. n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
  124. test_assert(n == PIPE_SIZE, "wrong size");
  125. test_assert((pipe1.rdptr == pipe1.buffer) &&
  126. (pipe1.wrptr == pipe1.buffer) &&
  127. (pipe1.cnt == PIPE_SIZE),
  128. "invalid pipe state");
  129. }
  130. /* [2.1.6] Emptying pipe.*/
  131. test_set_step(6);
  132. {
  133. size_t n;
  134. uint8_t buf[PIPE_SIZE];
  135. n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
  136. test_assert(n == PIPE_SIZE, "wrong size");
  137. test_assert((pipe1.rdptr == pipe1.buffer) &&
  138. (pipe1.wrptr == pipe1.buffer) &&
  139. (pipe1.cnt == 0),
  140. "invalid pipe state");
  141. test_assert(memcmp(pipe_pattern, buf, PIPE_SIZE) == 0, "content mismatch");
  142. }
  143. /* [2.1.7] Small write.*/
  144. test_set_step(7);
  145. {
  146. size_t n;
  147. n = chPipeWriteTimeout(&pipe1, pipe_pattern, 4, TIME_IMMEDIATE);
  148. test_assert(n == 4, "wrong size");
  149. test_assert((pipe1.rdptr != pipe1.wrptr) &&
  150. (pipe1.rdptr == pipe1.buffer) &&
  151. (pipe1.cnt == 4),
  152. "invalid pipe state");
  153. }
  154. /* [2.1.8] Filling remaining space.*/
  155. test_set_step(8);
  156. {
  157. size_t n;
  158. n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE - 4, TIME_IMMEDIATE);
  159. test_assert(n == PIPE_SIZE - 4, "wrong size");
  160. test_assert((pipe1.rdptr == pipe1.buffer) &&
  161. (pipe1.wrptr == pipe1.buffer) &&
  162. (pipe1.cnt == PIPE_SIZE),
  163. "invalid pipe state");
  164. }
  165. /* [2.1.9] Small Read.*/
  166. test_set_step(9);
  167. {
  168. size_t n;
  169. uint8_t buf[PIPE_SIZE];
  170. n = chPipeReadTimeout(&pipe1, buf, 4, TIME_IMMEDIATE);
  171. test_assert(n == 4, "wrong size");
  172. test_assert((pipe1.rdptr != pipe1.buffer) &&
  173. (pipe1.wrptr == pipe1.buffer) &&
  174. (pipe1.cnt == PIPE_SIZE - 4),
  175. "invalid pipe state");
  176. test_assert(memcmp(pipe_pattern, buf, 4) == 0, "content mismatch");
  177. }
  178. /* [2.1.10] Reading remaining data.*/
  179. test_set_step(10);
  180. {
  181. size_t n;
  182. uint8_t buf[PIPE_SIZE];
  183. n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE - 4, TIME_IMMEDIATE);
  184. test_assert(n == PIPE_SIZE - 4, "wrong size");
  185. test_assert((pipe1.rdptr == pipe1.buffer) &&
  186. (pipe1.wrptr == pipe1.buffer) &&
  187. (pipe1.cnt == 0),
  188. "invalid pipe state");
  189. test_assert(memcmp(pipe_pattern, buf, PIPE_SIZE - 4) == 0, "content mismatch");
  190. }
  191. /* [2.1.11] Small Write.*/
  192. test_set_step(11);
  193. {
  194. size_t n;
  195. n = chPipeWriteTimeout(&pipe1, pipe_pattern, 5, TIME_IMMEDIATE);
  196. test_assert(n == 5, "wrong size");
  197. test_assert((pipe1.rdptr != pipe1.wrptr) &&
  198. (pipe1.rdptr == pipe1.buffer) &&
  199. (pipe1.cnt == 5),
  200. "invalid pipe state");
  201. }
  202. /* [2.1.12] Small Read.*/
  203. test_set_step(12);
  204. {
  205. size_t n;
  206. uint8_t buf[PIPE_SIZE];
  207. n = chPipeReadTimeout(&pipe1, buf, 5, TIME_IMMEDIATE);
  208. test_assert(n == 5, "wrong size");
  209. test_assert((pipe1.rdptr == pipe1.wrptr) &&
  210. (pipe1.wrptr != pipe1.buffer) &&
  211. (pipe1.cnt == 0),
  212. "invalid pipe state");
  213. test_assert(memcmp(pipe_pattern, buf, 5) == 0, "content mismatch");
  214. }
  215. /* [2.1.13] Write wrapping buffer boundary.*/
  216. test_set_step(13);
  217. {
  218. size_t n;
  219. n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
  220. test_assert(n == PIPE_SIZE, "wrong size");
  221. test_assert((pipe1.rdptr == pipe1.wrptr) &&
  222. (pipe1.wrptr != pipe1.buffer) &&
  223. (pipe1.cnt == PIPE_SIZE),
  224. "invalid pipe state");
  225. }
  226. /* [2.1.14] Read wrapping buffer boundary.*/
  227. test_set_step(14);
  228. {
  229. size_t n;
  230. uint8_t buf[PIPE_SIZE];
  231. n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
  232. test_assert(n == PIPE_SIZE, "wrong size");
  233. test_assert((pipe1.rdptr == pipe1.wrptr) &&
  234. (pipe1.wrptr != pipe1.buffer) &&
  235. (pipe1.cnt == 0),
  236. "invalid pipe state");
  237. test_assert(memcmp(pipe_pattern, buf, PIPE_SIZE) == 0, "content mismatch");
  238. }
  239. }
  240. static const testcase_t oslib_test_002_001 = {
  241. "Pipes normal API, non-blocking tests",
  242. oslib_test_002_001_setup,
  243. NULL,
  244. oslib_test_002_001_execute
  245. };
  246. /**
  247. * @page oslib_test_002_002 [2.2] Pipe timeouts
  248. *
  249. * <h2>Description</h2>
  250. * The pipe API is tested for timeouts.
  251. *
  252. * <h2>Test Steps</h2>
  253. * - [2.2.1] Reading while pipe is empty.
  254. * - [2.2.2] Writing a string larger than pipe buffer.
  255. * .
  256. */
  257. static void oslib_test_002_002_setup(void) {
  258. chPipeObjectInit(&pipe1, buffer, PIPE_SIZE / 2);
  259. }
  260. static void oslib_test_002_002_execute(void) {
  261. /* [2.2.1] Reading while pipe is empty.*/
  262. test_set_step(1);
  263. {
  264. size_t n;
  265. uint8_t buf[PIPE_SIZE];
  266. n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
  267. test_assert(n == 0, "wrong size");
  268. test_assert((pipe1.rdptr == pipe1.buffer) &&
  269. (pipe1.wrptr == pipe1.buffer) &&
  270. (pipe1.cnt == 0),
  271. "invalid pipe state");
  272. }
  273. /* [2.2.2] Writing a string larger than pipe buffer.*/
  274. test_set_step(2);
  275. {
  276. size_t n;
  277. n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
  278. test_assert(n == PIPE_SIZE / 2, "wrong size");
  279. test_assert((pipe1.rdptr == pipe1.wrptr) &&
  280. (pipe1.wrptr == pipe1.buffer) &&
  281. (pipe1.cnt == PIPE_SIZE / 2),
  282. "invalid pipe state");
  283. }
  284. }
  285. static const testcase_t oslib_test_002_002 = {
  286. "Pipe timeouts",
  287. oslib_test_002_002_setup,
  288. NULL,
  289. oslib_test_002_002_execute
  290. };
  291. /****************************************************************************
  292. * Exported data.
  293. ****************************************************************************/
  294. /**
  295. * @brief Array of test cases.
  296. */
  297. const testcase_t * const oslib_test_sequence_002_array[] = {
  298. &oslib_test_002_001,
  299. &oslib_test_002_002,
  300. NULL
  301. };
  302. /**
  303. * @brief Pipes.
  304. */
  305. const testsequence_t oslib_test_sequence_002 = {
  306. "Pipes",
  307. oslib_test_sequence_002_array
  308. };
  309. #endif /* CH_CFG_USE_PIPES */