1
0

utest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "utest.h"
  11. #include <rtthread.h>
  12. #include <finsh.h>
  13. #undef DBG_SECTION_NAME
  14. #undef DBG_LEVEL
  15. #undef DBG_COLOR
  16. #undef DBG_ENABLE
  17. #define DBG_ENABLE
  18. #define DBG_SECTION_NAME "utest"
  19. #ifdef UTEST_DEBUG
  20. #define DBG_LEVEL DBG_LOG
  21. #else
  22. #define DBG_LEVEL DBG_INFO
  23. #endif
  24. #define DBG_COLOR
  25. #include <rtdbg.h>
  26. #if RT_CONSOLEBUF_SIZE < 256
  27. #error "RT_CONSOLEBUF_SIZE is less than 256!"
  28. #endif
  29. static utest_tc_export_t tc_table = RT_NULL;
  30. static rt_size_t tc_num;
  31. static struct utest local_utest = {UTEST_PASSED, 0, 0};
  32. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  33. #pragma section="UtestTcTab"
  34. #endif
  35. int utest_init(void)
  36. {
  37. /* initialize the utest commands table.*/
  38. #if defined(__CC_ARM) /* ARM C Compiler */
  39. extern const int UtestTcTab$$Base;
  40. extern const int UtestTcTab$$Limit;
  41. tc_table = (utest_tc_export_t)&UtestTcTab$$Base;
  42. tc_num = (utest_tc_export_t)&UtestTcTab$$Limit - tc_table;
  43. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  44. tc_table = (utest_tc_export_t)__section_begin("UtestTcTab");
  45. tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table;
  46. #elif defined (__GNUC__) /* for GCC Compiler */
  47. extern const int __rt_utest_tc_tab_start;
  48. extern const int __rt_utest_tc_tab_end;
  49. tc_table = (utest_tc_export_t)&__rt_utest_tc_tab_start;
  50. tc_num = (utest_tc_export_t) &__rt_utest_tc_tab_end - tc_table;
  51. #endif /* defined(__CC_ARM) */
  52. LOG_I("utest is initialize success.");
  53. LOG_I("total utest testcase num: (%d)", tc_num);
  54. return tc_num;
  55. }
  56. INIT_COMPONENT_EXPORT(utest_init);
  57. static void utest_tc_list(void)
  58. {
  59. rt_size_t i = 0;
  60. LOG_I("Commands list : ");
  61. for (i = 0; i < tc_num; i++)
  62. {
  63. LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout);
  64. }
  65. }
  66. MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_list, output all utest testcase);
  67. static const char *file_basename(const char *file)
  68. {
  69. char *end_ptr = RT_NULL;
  70. char *rst = RT_NULL;
  71. if (!((end_ptr = strrchr(file, '\\')) != RT_NULL || \
  72. (end_ptr = strrchr(file, '/')) != RT_NULL) || \
  73. (rt_strlen(file) < 2))
  74. {
  75. rst = (char *)file;
  76. }
  77. else
  78. {
  79. rst = (char *)(end_ptr + 1);
  80. }
  81. return (const char *)rst;
  82. }
  83. static void utest_run(const char *utest_name)
  84. {
  85. rt_size_t i = 0;
  86. LOG_I("[==========] [ utest ] started");
  87. while(i < tc_num)
  88. {
  89. if (utest_name && rt_strcmp(utest_name, tc_table[i].name))
  90. {
  91. i++;
  92. continue;
  93. }
  94. LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
  95. if (tc_table[i].init != RT_NULL)
  96. {
  97. if (tc_table[i].init() != RT_EOK)
  98. {
  99. LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  100. goto __tc_continue;
  101. }
  102. }
  103. if (tc_table[i].tc != RT_NULL)
  104. {
  105. tc_table[i].tc();
  106. if (local_utest.failed_num == 0)
  107. {
  108. LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name);
  109. }
  110. else
  111. {
  112. LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  113. }
  114. }
  115. else
  116. {
  117. LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  118. }
  119. if (tc_table[i].cleanup != RT_NULL)
  120. {
  121. if (tc_table[i].cleanup() != RT_EOK)
  122. {
  123. LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  124. goto __tc_continue;
  125. }
  126. }
  127. __tc_continue:
  128. LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name);
  129. i++;
  130. }
  131. LOG_I("[==========] [ utest ] finished");
  132. }
  133. static void utest_testcase_run(int argc, char** argv)
  134. {
  135. char utest_name[UTEST_NAME_MAX_LEN];
  136. if (argc == 1)
  137. {
  138. utest_run(RT_NULL);
  139. }
  140. else if (argc == 2)
  141. {
  142. rt_memset(utest_name, 0x0, sizeof(utest_name));
  143. rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
  144. utest_run(utest_name);
  145. }
  146. else
  147. {
  148. LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__);
  149. }
  150. }
  151. MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [testcase name]);
  152. utest_t utest_handle_get(void)
  153. {
  154. return (utest_t)&local_utest;
  155. }
  156. void utest_unit_run(test_unit_func func, const char *unit_func_name)
  157. {
  158. // LOG_I("[==========] utest unit name: (%s)", unit_func_name);
  159. local_utest.error = UTEST_PASSED;
  160. local_utest.passed_num = 0;
  161. local_utest.failed_num = 0;
  162. if (func != RT_NULL)
  163. {
  164. func();
  165. }
  166. }
  167. void utest_assert(int value, const char *file, int line, const char *func, const char *msg)
  168. {
  169. if (!(value))
  170. {
  171. local_utest.error = UTEST_FAILED;
  172. local_utest.failed_num ++;
  173. LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
  174. }
  175. else
  176. {
  177. LOG_D("[ OK ] [ unit ] (%s:%d) is passed", func, line);
  178. local_utest.error = UTEST_PASSED;
  179. local_utest.passed_num ++;
  180. }
  181. }
  182. void utest_assert_string(const char *a, const char *b, rt_bool_t equal, const char *file, int line, const char *func, const char *msg)
  183. {
  184. if (a == RT_NULL || b == RT_NULL)
  185. {
  186. utest_assert(0, file, line, func, msg);
  187. }
  188. if (equal)
  189. {
  190. if (rt_strcmp(a, b) == 0)
  191. {
  192. utest_assert(1, file, line, func, msg);
  193. }
  194. else
  195. {
  196. utest_assert(0, file, line, func, msg);
  197. }
  198. }
  199. else
  200. {
  201. if (rt_strcmp(a, b) == 0)
  202. {
  203. utest_assert(0, file, line, func, msg);
  204. }
  205. else
  206. {
  207. utest_assert(1, file, line, func, msg);
  208. }
  209. }
  210. }