utest.c 8.7 KB

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