utest.c 7.0 KB

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