utest.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-19 MurphyZhao the first version
  9. */
  10. #ifndef __UTEST_H__
  11. #define __UTEST_H__
  12. #include <rtthread.h>
  13. #include <stdint.h>
  14. #include "utest_log.h"
  15. #include "utest_assert.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * utest_error
  21. *
  22. * @brief Test result.
  23. *
  24. * @member UTEST_PASSED Test success.
  25. * @member UTEST_FAILED Test failed.
  26. * @member UTEST_PASSED Test skipped.
  27. *
  28. */
  29. enum utest_error
  30. {
  31. UTEST_PASSED = 0,
  32. UTEST_FAILED = 1,
  33. UTEST_SKIPPED = 2
  34. };
  35. typedef enum utest_error utest_err_e;
  36. /**
  37. * utest
  38. *
  39. * @brief utest data structure.
  40. *
  41. * @member error Error number from enum `utest_error`.
  42. * @member passed_num Total number of tests passed.
  43. * @member failed_num Total number of tests failed.
  44. *
  45. */
  46. struct utest
  47. {
  48. utest_err_e error;
  49. uint32_t passed_num;
  50. uint32_t failed_num;
  51. };
  52. typedef struct utest *utest_t;
  53. /**
  54. * utest_tc_export
  55. *
  56. * @brief utest testcase data structure.
  57. * Will export the data to `UtestTcTab` section in flash.
  58. *
  59. * @member name Testcase name.
  60. * @member run_timeout Testcase maximum test time (Time unit: seconds).
  61. * @member init Necessary initialization before executing the test case function.
  62. * @member tc Total number of tests failed.
  63. * @member cleanup Total number of tests failed.
  64. *
  65. */
  66. struct utest_tc_export {
  67. const char *name;
  68. uint32_t run_timeout;
  69. rt_err_t (*init)(void);
  70. void (*tc)(void);
  71. rt_err_t (*cleanup)(void);
  72. };
  73. typedef struct utest_tc_export *utest_tc_export_t;
  74. /**
  75. * test_unit_func
  76. *
  77. * @brief Unit test handler function pointer.
  78. *
  79. */
  80. typedef void (*test_unit_func)(void);
  81. /**
  82. * utest_unit_run
  83. *
  84. * @brief Unit test function executor.
  85. * No need for the user to call this function directly
  86. *
  87. * @param func Unit test function.
  88. * @param unit_func_name Unit test function name.
  89. *
  90. * @return void
  91. *
  92. */
  93. void utest_unit_run(test_unit_func func, const char *unit_func_name);
  94. /**
  95. * utest_handle_get
  96. *
  97. * @brief Get the utest data structure handle.
  98. * No need for the user to call this function directly
  99. *
  100. * @param void
  101. *
  102. * @return utest_t type. (struct utest *)
  103. *
  104. */
  105. utest_t utest_handle_get(void);
  106. /**
  107. * UTEST_NAME_MAX_LEN
  108. *
  109. * @brief Testcase name maximum length.
  110. *
  111. */
  112. #define UTEST_NAME_MAX_LEN (128u)
  113. /**
  114. * UTEST_TC_EXPORT
  115. *
  116. * @brief Export testcase function to `UtestTcTab` section in flash.
  117. * Used in application layer.
  118. *
  119. * @param testcase The testcase function.
  120. * @param name The testcase name.
  121. * @param init The initialization function of the test case.
  122. * @param cleanup The cleanup function of the test case.
  123. * @param timeout Testcase maximum test time (Time unit: seconds).
  124. *
  125. * @return None
  126. *
  127. */
  128. #ifdef _MSC_VER
  129. #pragma section("UtestTcTab$f",read)
  130. #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \
  131. __declspec(allocate("UtestTcTab$f")) \
  132. static const struct utest_tc_export _utest_testcase = \
  133. { \
  134. name, \
  135. timeout, \
  136. init, \
  137. testcase, \
  138. cleanup \
  139. }
  140. #pragma comment(linker, "/merge:UtestTcTab=tctext")
  141. #else
  142. #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \
  143. rt_used static const struct utest_tc_export _utest_testcase \
  144. rt_section("UtestTcTab") = \
  145. { \
  146. name, \
  147. timeout, \
  148. init, \
  149. testcase, \
  150. cleanup \
  151. }
  152. #endif /* _MSC_VER */
  153. /**
  154. * UTEST_UNIT_RUN
  155. *
  156. * @brief Unit test function executor.
  157. * Used in `testcase` function in application.
  158. *
  159. * @param test_unit_func Unit test function
  160. *
  161. * @return None
  162. *
  163. */
  164. #define UTEST_UNIT_RUN(test_unit_func) \
  165. utest_unit_run(test_unit_func, #test_unit_func); \
  166. if(utest_handle_get()->failed_num != 0) return;
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif /* __UTEST_H__ */