ch_test.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.h
  15. * @brief Unit Tests Engine Module macros and structures.
  16. *
  17. * @addtogroup CH_TEST
  18. * @{
  19. */
  20. #ifndef CH_TEST_H
  21. #define CH_TEST_H
  22. /*===========================================================================*/
  23. /* Module constants. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Module pre-compile time settings. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Maximum number of entries in the tokens buffer.
  30. */
  31. #if !defined(TEST_MAX_TOKENS) || defined(__DOXYGEN__)
  32. #define TEST_MAX_TOKENS 16
  33. #endif
  34. /**
  35. * @brief Delay inserted between test cases.
  36. */
  37. #if !defined(TEST_DELAY_BETWEEN_TESTS) || defined(__DOXYGEN__)
  38. #define TEST_DELAY_BETWEEN_TESTS 200
  39. #endif
  40. /**
  41. * @brief Delay inserted between test cases.
  42. */
  43. #if !defined(TEST_SHOW_SEQUENCES) || defined(__DOXYGEN__)
  44. #define TEST_SHOW_SEQUENCES TRUE
  45. #endif
  46. /*===========================================================================*/
  47. /* Derived constants and error checks. */
  48. /*===========================================================================*/
  49. /*===========================================================================*/
  50. /* Module data structures and types. */
  51. /*===========================================================================*/
  52. /**
  53. * @brief Structure representing a test case.
  54. */
  55. typedef struct {
  56. const char *name; /**< @brief Test case name. */
  57. void (*setup)(void); /**< @brief Test case preparation function. */
  58. void (*teardown)(void); /**< @brief Test case clean up function. */
  59. void (*execute)(void); /**< @brief Test case execution function. */
  60. } testcase_t;
  61. /**
  62. * @brief Structure representing a test sequence.
  63. */
  64. typedef struct {
  65. const char *name; /**< @brief Name of the test sequence. */
  66. const testcase_t * const * cases; /**< @brief Test cases array. */
  67. } testsequence_t;
  68. /**
  69. * @brief Type of a test suite.
  70. */
  71. typedef struct {
  72. const char *name; /**< @brief Name of the test suite. */
  73. const testsequence_t * const * sequences; /**< @brief Test sequences array. */
  74. } testsuite_t;
  75. /**
  76. * @brief Type of a test suite.
  77. */
  78. //typedef const testcase_t * const *testsuite_t[];
  79. /*===========================================================================*/
  80. /* Module macros. */
  81. /*===========================================================================*/
  82. /**
  83. * @brief Sets the step identifier.
  84. *
  85. * @param[in] step the step number
  86. */
  87. #define test_set_step(step) test_step = (step)
  88. /**
  89. * @brief Test failure enforcement.
  90. * @note This function can only be called from test_case execute context.
  91. *
  92. * @param[in] msg failure message as string
  93. *
  94. * @api
  95. */
  96. #define test_fail(msg) { \
  97. _test_fail(msg); \
  98. return; \
  99. }
  100. /**
  101. * @brief Test assertion.
  102. * @note This function can only be called from test_case execute context.
  103. *
  104. * @param[in] condition a boolean expression that must be verified to be true
  105. * @param[in] msg failure message as string
  106. *
  107. * @api
  108. */
  109. #define test_assert(condition, msg) { \
  110. if (_test_assert(condition, msg)) \
  111. return; \
  112. }
  113. /**
  114. * @brief Test assertion with lock.
  115. * @note This function can only be called from test_case execute context.
  116. *
  117. * @param[in] condition a boolean expression that must be verified to be true
  118. * @param[in] msg failure message as string
  119. *
  120. * @api
  121. */
  122. #define test_assert_lock(condition, msg) { \
  123. osalSysLock(); \
  124. if (_test_assert(condition, msg)) { \
  125. osalSysUnlock(); \
  126. return; \
  127. } \
  128. osalSysUnlock(); \
  129. }
  130. /**
  131. * @brief Test sequence assertion.
  132. * @note This function can only be called from test_case execute context.
  133. *
  134. * @param[in] expected string to be matched with the tokens buffer
  135. * @param[in] msg failure message as string
  136. *
  137. * @api
  138. */
  139. #define test_assert_sequence(expected, msg) { \
  140. if (_test_assert_sequence(expected, msg)) \
  141. return; \
  142. }
  143. /**
  144. * @brief Test time window assertion.
  145. * @note This function can only be called from test_case execute context.
  146. *
  147. * @param[in] start initial time in the window (included)
  148. * @param[in] end final time in the window (not included)
  149. * @param[in] msg failure message as string
  150. *
  151. * @api
  152. */
  153. #define test_assert_time_window(start, end, msg) { \
  154. if (_test_assert_time_window(start, end, msg)) \
  155. return; \
  156. }
  157. /*===========================================================================*/
  158. /* External declarations. */
  159. /*===========================================================================*/
  160. #if !defined(__DOXYGEN__)
  161. extern unsigned test_step;
  162. extern bool test_global_fail;
  163. #endif
  164. #ifdef __cplusplus
  165. extern "C" {
  166. #endif
  167. bool _test_fail(const char *message);
  168. bool _test_assert(bool condition, const char *msg);
  169. bool _test_assert_sequence(char *expected, const char *msg);
  170. bool _test_assert_time_window(systime_t start,
  171. systime_t end,
  172. const char *msg);
  173. void test_printn(uint32_t n);
  174. void test_print(const char *msgp);
  175. void test_println(const char *msgp);
  176. void test_emit_token(char token);
  177. void test_emit_token_i(char token);
  178. msg_t test_execute(BaseSequentialStream *stream, const testsuite_t *tsp);
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. /*===========================================================================*/
  183. /* Module inline functions. */
  184. /*===========================================================================*/
  185. #endif /* CH_TEST_H */
  186. /** @} */