utest.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2018, 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 "utest_log.h"
  14. /**
  15. * utest_error
  16. *
  17. * @brief Test result.
  18. *
  19. * @member UTEST_PASSED Test success.
  20. * @member UTEST_FAILED Test failed.
  21. * @member UTEST_PASSED Test skipped.
  22. *
  23. */
  24. enum utest_error
  25. {
  26. UTEST_PASSED = 0,
  27. UTEST_FAILED = 1,
  28. UTEST_SKIPPED = 2
  29. };
  30. typedef enum utest_error utest_err_e;
  31. /**
  32. * utest
  33. *
  34. * @brief utest data structure.
  35. *
  36. * @member error Error number from enum `utest_error`.
  37. * @member passed_num Total number of tests passed.
  38. * @member failed_num Total number of tests failed.
  39. *
  40. */
  41. struct utest
  42. {
  43. utest_err_e error;
  44. uint32_t passed_num;
  45. uint32_t failed_num;
  46. };
  47. typedef struct utest *utest_t;
  48. /**
  49. * utest_tc_export
  50. *
  51. * @brief utest testcase data structure.
  52. * Will export the data to `UtestTcTab` section in flash.
  53. *
  54. * @member name Testcase name.
  55. * @member run_timeout Testcase maximum test time.
  56. * @member init Necessary initialization before executing the test case function.
  57. * @member tc Total number of tests failed.
  58. * @member cleanup Total number of tests failed.
  59. *
  60. */
  61. struct utest_tc_export {
  62. const char *name;
  63. uint32_t run_timeout;
  64. rt_err_t (*init)(void);
  65. void (*tc)(void);
  66. rt_err_t (*cleanup)(void);
  67. };
  68. typedef struct utest_tc_export *utest_tc_export_t;
  69. /**
  70. * test_unit_func
  71. *
  72. * @brief Unit test handler function pointer.
  73. *
  74. */
  75. typedef void (*test_unit_func)(void);
  76. /**
  77. * utest_unit_run
  78. *
  79. * @brief Unit test function executor.
  80. * No need for the user to call this function directly
  81. *
  82. * @param func Unit test function.
  83. * @param unit_func_name Unit test function name.
  84. *
  85. * @return void
  86. *
  87. */
  88. void utest_unit_run(test_unit_func func, const char *unit_func_name);
  89. /**
  90. * utest_handle_get
  91. *
  92. * @brief Get the utest data structure handle.
  93. * No need for the user to call this function directly
  94. *
  95. * @param void
  96. *
  97. * @return utest_t type. (struct utest *)
  98. *
  99. */
  100. utest_t utest_handle_get(void);
  101. /**
  102. * UTEST_NAME_MAX_LEN
  103. *
  104. * @brief Testcase name maximum length.
  105. *
  106. */
  107. #define UTEST_NAME_MAX_LEN (128u)
  108. /**
  109. * UTEST_TC_EXPORT
  110. *
  111. * @brief Export testcase function to `UtestTcTab` section in flash.
  112. * Used in application layer.
  113. *
  114. * @param testcase The testcase function.
  115. * @param name The testcase name.
  116. * @param init The initialization function of the test case.
  117. * @param cleanup The cleanup function of the test case.
  118. * @param timeout Testcase maximum test time.
  119. *
  120. * @return None
  121. *
  122. */
  123. #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \
  124. RT_USED static const struct utest_tc_export _utest_testcase \
  125. SECTION("UtestTcTab") = \
  126. { \
  127. name, \
  128. timeout, \
  129. init, \
  130. testcase, \
  131. cleanup \
  132. }
  133. /**
  134. * UTEST_UNIT_RUN
  135. *
  136. * @brief Unit test function executor.
  137. * Used in `testcase` function in application.
  138. *
  139. * @param test_unit_func Unit test function
  140. *
  141. * @return None
  142. *
  143. */
  144. #define UTEST_UNIT_RUN(test_unit_func) \
  145. utest_unit_run(test_unit_func, #test_unit_func); \
  146. if(utest_handle_get()->failed_num != 0) return;
  147. #endif /* __UTEST_H__ */