ch_test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /**
  14. * @file ch_test.c
  15. * @brief Unit Tests Engine module code.
  16. *
  17. * @addtogroup CH_TEST
  18. * @{
  19. */
  20. #include "hal.h"
  21. #include "ch_test.h"
  22. /*===========================================================================*/
  23. /* Module local definitions. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Module exported variables. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Test step being executed.
  30. */
  31. unsigned test_step;
  32. /**
  33. * @brief Test result flag.
  34. */
  35. bool test_global_fail;
  36. /*===========================================================================*/
  37. /* Module local types. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Module local variables. */
  41. /*===========================================================================*/
  42. static bool test_local_fail;
  43. static const char *test_failure_message;
  44. static char test_tokens_buffer[TEST_MAX_TOKENS];
  45. static char *test_tokp;
  46. static BaseSequentialStream *test_chp;
  47. /*===========================================================================*/
  48. /* Module local functions. */
  49. /*===========================================================================*/
  50. static void clear_tokens(void) {
  51. test_tokp = test_tokens_buffer;
  52. }
  53. static void print_tokens(void) {
  54. char *cp = test_tokens_buffer;
  55. while (cp < test_tokp)
  56. streamPut(test_chp, *cp++);
  57. }
  58. static void execute_test(const testcase_t *tcp) {
  59. /* Initialization */
  60. clear_tokens();
  61. test_local_fail = false;
  62. if (tcp->setup != NULL)
  63. tcp->setup();
  64. tcp->execute();
  65. if (tcp->teardown != NULL)
  66. tcp->teardown();
  67. }
  68. static void print_line(void) {
  69. unsigned i;
  70. for (i = 0; i < 76; i++)
  71. streamPut(test_chp, '-');
  72. streamWrite(test_chp, (const uint8_t *)"\r\n", 2);
  73. }
  74. static void print_fat_line(void) {
  75. unsigned i;
  76. for (i = 0; i < 76; i++)
  77. streamPut(test_chp, '=');
  78. streamWrite(test_chp, (const uint8_t *)"\r\n", 2);
  79. }
  80. /*===========================================================================*/
  81. /* Module exported functions. */
  82. /*===========================================================================*/
  83. bool _test_fail(const char *msg) {
  84. test_local_fail = true;
  85. test_global_fail = true;
  86. test_failure_message = msg;
  87. return true;
  88. }
  89. bool _test_assert(bool condition, const char *msg) {
  90. if (!condition)
  91. return _test_fail(msg);
  92. return false;
  93. }
  94. bool _test_assert_sequence(char *expected, const char *msg) {
  95. char *cp = test_tokens_buffer;
  96. while (cp < test_tokp) {
  97. if (*cp++ != *expected++)
  98. return _test_fail(msg);
  99. }
  100. if (*expected)
  101. return _test_fail(msg);
  102. clear_tokens();
  103. return false;
  104. }
  105. bool _test_assert_time_window(systime_t start,
  106. systime_t end,
  107. const char *msg) {
  108. return _test_assert(osalTimeIsInRangeX(osalOsGetSystemTimeX(), start, end),
  109. msg);
  110. }
  111. /**
  112. * @brief Prints a decimal unsigned number.
  113. *
  114. * @param[in] n the number to be printed
  115. *
  116. * @api
  117. */
  118. void test_printn(uint32_t n) {
  119. char buf[16], *p;
  120. if (!n)
  121. streamPut(test_chp, '0');
  122. else {
  123. p = buf;
  124. while (n)
  125. *p++ = (n % 10) + '0', n /= 10;
  126. while (p > buf)
  127. streamPut(test_chp, *--p);
  128. }
  129. }
  130. /**
  131. * @brief Prints a line without final end-of-line.
  132. *
  133. * @param[in] msgp the message
  134. *
  135. * @api
  136. */
  137. void test_print(const char *msgp) {
  138. while (*msgp)
  139. streamPut(test_chp, *msgp++);
  140. }
  141. /**
  142. * @brief Prints a line.
  143. *
  144. * @param[in] msgp the message
  145. *
  146. * @api
  147. */
  148. void test_println(const char *msgp) {
  149. test_print(msgp);
  150. streamWrite(test_chp, (const uint8_t *)"\r\n", 2);
  151. }
  152. /**
  153. * @brief Emits a token into the tokens buffer.
  154. *
  155. * @param[in] token the token as a char
  156. *
  157. * @api
  158. */
  159. void test_emit_token(char token) {
  160. osalSysLock();
  161. if (test_tokp < &test_tokens_buffer[TEST_MAX_TOKENS])
  162. *test_tokp++ = token;
  163. osalSysUnlock();
  164. }
  165. /**
  166. * @brief Emits a token into the tokens buffer from a critical zone.
  167. *
  168. * @param[in] token the token as a char
  169. *
  170. * @iclass
  171. */
  172. void test_emit_token_i(char token) {
  173. if (test_tokp < &test_tokens_buffer[TEST_MAX_TOKENS])
  174. *test_tokp++ = token;
  175. }
  176. /**
  177. * @brief Test execution thread function.
  178. *
  179. * @param[in] stream pointer to a @p BaseSequentialStream object for test
  180. * output
  181. * @param[in] tsp test suite to execute
  182. * @return A failure boolean value casted to @p msg_t.
  183. * @retval false if no errors occurred.
  184. * @retval true if one or more tests failed.
  185. *
  186. * @api
  187. */
  188. msg_t test_execute(BaseSequentialStream *stream, const testsuite_t *tsp) {
  189. int tseq, tcase;
  190. test_chp = stream;
  191. test_println("");
  192. if (tsp->name != NULL) {
  193. test_print("*** ");
  194. test_println(tsp->name);
  195. }
  196. else {
  197. test_println("*** Test Suite");
  198. }
  199. test_println("***");
  200. test_print("*** Compiled: ");
  201. test_println(__DATE__ " - " __TIME__);
  202. #if defined(PLATFORM_NAME)
  203. test_print("*** Platform: ");
  204. test_println(PLATFORM_NAME);
  205. #endif
  206. #if defined(BOARD_NAME)
  207. test_print("*** Test Board: ");
  208. test_println(BOARD_NAME);
  209. #endif
  210. #if defined(TEST_REPORT_HOOK_HEADER)
  211. TEST_REPORT_HOOK_HEADER
  212. #endif
  213. test_println("");
  214. test_global_fail = false;
  215. tseq = 0;
  216. while (tsp->sequences[tseq] != NULL) {
  217. #if TEST_SHOW_SEQUENCES == TRUE
  218. print_fat_line();
  219. test_print("=== Test Sequence ");
  220. test_printn(tseq + 1);
  221. test_print(" (");
  222. test_print(tsp->sequences[tseq]->name);
  223. test_println(")");
  224. #endif
  225. tcase = 0;
  226. while (tsp->sequences[tseq]->cases[tcase] != NULL) {
  227. print_line();
  228. test_print("--- Test Case ");
  229. test_printn(tseq + 1);
  230. test_print(".");
  231. test_printn(tcase + 1);
  232. test_print(" (");
  233. test_print(tsp->sequences[tseq]->cases[tcase]->name);
  234. test_println(")");
  235. #if TEST_DELAY_BETWEEN_TESTS > 0
  236. osalThreadSleepMilliseconds(TEST_DELAY_BETWEEN_TESTS);
  237. #endif
  238. execute_test(tsp->sequences[tseq]->cases[tcase]);
  239. if (test_local_fail) {
  240. test_print("--- Result: FAILURE (#");
  241. test_printn(test_step);
  242. test_print(" [");
  243. print_tokens();
  244. test_print("] \"");
  245. test_print(test_failure_message);
  246. test_println("\")");
  247. }
  248. else {
  249. test_println("--- Result: SUCCESS");
  250. }
  251. tcase++;
  252. }
  253. tseq++;
  254. }
  255. print_line();
  256. test_println("");
  257. test_print("Final result: ");
  258. if (test_global_fail)
  259. test_println("FAILURE");
  260. else
  261. test_println("SUCCESS");
  262. #if defined(TEST_REPORT_HOOK_END)
  263. TEST_REPORT_HOOK_END
  264. #endif
  265. return (msg_t)test_global_fail;
  266. }
  267. /** @} */