utest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 int utest_help(void)
  105. {
  106. rt_kprintf("\n");
  107. rt_kprintf("Command: utest_run\n");
  108. rt_kprintf(" info: Execute test cases.\n");
  109. rt_kprintf(" format: utest_run [-thread or -help] [testcase name] [loop num]\n");
  110. rt_kprintf(" usage:\n");
  111. rt_kprintf(" 1. utest_run\n");
  112. rt_kprintf(" Do not specify a test case name. Run all test cases.\n");
  113. rt_kprintf(" 2. utest_run -thread\n");
  114. rt_kprintf(" Do not specify a test case name. Run all test cases in threaded mode.\n");
  115. rt_kprintf(" 3. utest_run testcaseA\n");
  116. rt_kprintf(" Run 'testcaseA'.\n");
  117. rt_kprintf(" 4. utest_run testcaseA 10\n");
  118. rt_kprintf(" Run 'testcaseA' ten times.\n");
  119. rt_kprintf(" 5. utest_run -thread testcaseA\n");
  120. rt_kprintf(" Run 'testcaseA' in threaded mode.\n");
  121. rt_kprintf(" 6. utest_run -thread testcaseA 10\n");
  122. rt_kprintf(" Run 'testcaseA' ten times in threaded mode.\n");
  123. rt_kprintf(" 7. utest_run test*\n");
  124. rt_kprintf(" support '*' wildcard. Run all test cases starting with 'test'.\n");
  125. rt_kprintf(" 8. utest_run -help\n");
  126. rt_kprintf(" Show utest help information\n");
  127. rt_kprintf("\n");
  128. return 0;
  129. }
  130. static void utest_run(const char *utest_name)
  131. {
  132. rt_size_t i;
  133. rt_uint32_t index;
  134. rt_bool_t is_find;
  135. rt_thread_mdelay(1000);
  136. for (index = 0; index < tc_loop; index ++)
  137. {
  138. i = 0;
  139. is_find = RT_FALSE;
  140. LOG_I("[==========] [ utest ] started");
  141. while(i < tc_num)
  142. {
  143. if (utest_name)
  144. {
  145. int len = strlen(utest_name);
  146. if (utest_name[len - 1] == '*')
  147. {
  148. len -= 1;
  149. }
  150. if (rt_memcmp(tc_table[i].name, utest_name, len) != 0)
  151. {
  152. i++;
  153. continue;
  154. }
  155. }
  156. is_find = RT_TRUE;
  157. LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
  158. if (tc_table[i].init != RT_NULL)
  159. {
  160. if (tc_table[i].init() != RT_EOK)
  161. {
  162. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  163. goto __tc_continue;
  164. }
  165. }
  166. if (tc_table[i].tc != RT_NULL)
  167. {
  168. tc_table[i].tc();
  169. if (local_utest.failed_num == 0)
  170. {
  171. LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name);
  172. }
  173. else
  174. {
  175. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  176. }
  177. }
  178. else
  179. {
  180. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  181. }
  182. if (tc_table[i].cleanup != RT_NULL)
  183. {
  184. if (tc_table[i].cleanup() != RT_EOK)
  185. {
  186. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  187. goto __tc_continue;
  188. }
  189. }
  190. __tc_continue:
  191. LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name);
  192. i++;
  193. }
  194. if (i == tc_num && is_find == RT_FALSE)
  195. {
  196. LOG_I("[==========] [ utest ] Not find (%s)", utest_name);
  197. LOG_I("[==========] [ utest ] finished");
  198. break;
  199. }
  200. LOG_I("[==========] [ utest ] finished");
  201. }
  202. }
  203. static void utest_testcase_run(int argc, char** argv)
  204. {
  205. void *thr_param = RT_NULL;
  206. static char utest_name[UTEST_NAME_MAX_LEN];
  207. rt_memset(utest_name, 0x0, sizeof(utest_name));
  208. tc_loop = 1;
  209. if (argc == 1)
  210. {
  211. utest_run(RT_NULL);
  212. return;
  213. }
  214. else if (argc == 2 || argc == 3 || argc == 4)
  215. {
  216. if (rt_strcmp(argv[1], "-thread") == 0)
  217. {
  218. rt_thread_t tid = RT_NULL;
  219. if (argc == 3 || argc == 4)
  220. {
  221. rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
  222. thr_param = (void*)utest_name;
  223. if (argc == 4) tc_loop = atoi(argv[3]);
  224. }
  225. tid = rt_thread_create("utest",
  226. (void (*)(void *))utest_run, thr_param,
  227. UTEST_THREAD_STACK_SIZE, UTEST_THREAD_PRIORITY, 10);
  228. if (tid != NULL)
  229. {
  230. rt_thread_startup(tid);
  231. }
  232. }
  233. else if (rt_strcmp(argv[1], "-help") == 0)
  234. {
  235. utest_help();
  236. }
  237. else
  238. {
  239. rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
  240. if (argc == 3) tc_loop = atoi(argv[2]);
  241. utest_run(utest_name);
  242. }
  243. }
  244. else
  245. {
  246. LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__);
  247. utest_help();
  248. }
  249. }
  250. MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num]);
  251. utest_t utest_handle_get(void)
  252. {
  253. return (utest_t)&local_utest;
  254. }
  255. void utest_unit_run(test_unit_func func, const char *unit_func_name)
  256. {
  257. // LOG_I("[==========] utest unit name: (%s)", unit_func_name);
  258. local_utest.error = UTEST_PASSED;
  259. local_utest.passed_num = 0;
  260. local_utest.failed_num = 0;
  261. if (func != RT_NULL)
  262. {
  263. func();
  264. }
  265. }
  266. void utest_assert(int value, const char *file, int line, const char *func, const char *msg)
  267. {
  268. if (!(value))
  269. {
  270. local_utest.error = UTEST_FAILED;
  271. local_utest.failed_num ++;
  272. LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
  273. }
  274. else
  275. {
  276. if (utest_log_lv == UTEST_LOG_ALL)
  277. {
  278. LOG_D("[ OK ] [ unit ] (%s:%d) is passed", func, line);
  279. }
  280. local_utest.error = UTEST_PASSED;
  281. local_utest.passed_num ++;
  282. }
  283. }
  284. 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)
  285. {
  286. if (a == RT_NULL || b == RT_NULL)
  287. {
  288. utest_assert(0, file, line, func, msg);
  289. }
  290. if (equal)
  291. {
  292. if (rt_strcmp(a, b) == 0)
  293. {
  294. utest_assert(1, file, line, func, msg);
  295. }
  296. else
  297. {
  298. utest_assert(0, file, line, func, msg);
  299. }
  300. }
  301. else
  302. {
  303. if (rt_strcmp(a, b) == 0)
  304. {
  305. utest_assert(0, file, line, func, msg);
  306. }
  307. else
  308. {
  309. utest_assert(1, file, line, func, msg);
  310. }
  311. }
  312. }
  313. 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)
  314. {
  315. if (a == RT_NULL || b == RT_NULL)
  316. {
  317. utest_assert(0, file, line, func, msg);
  318. }
  319. if (equal)
  320. {
  321. if (rt_memcmp(a, b, sz) == 0)
  322. {
  323. utest_assert(1, file, line, func, msg);
  324. }
  325. else
  326. {
  327. utest_assert(0, file, line, func, msg);
  328. }
  329. }
  330. else
  331. {
  332. if (rt_memcmp(a, b, sz) == 0)
  333. {
  334. utest_assert(0, file, line, func, msg);
  335. }
  336. else
  337. {
  338. utest_assert(1, file, line, func, msg);
  339. }
  340. }
  341. }