utest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (c) 2006-2021, 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. #define DBG_TAG "utest"
  16. #ifdef UTEST_DEBUG
  17. #define DBG_LVL DBG_LOG
  18. #else
  19. #define DBG_LVL DBG_INFO
  20. #endif
  21. #include <rtdbg.h>
  22. #if RT_CONSOLEBUF_SIZE < 256
  23. #error "RT_CONSOLEBUF_SIZE is less than 256!"
  24. #endif
  25. #ifdef UTEST_THR_STACK_SIZE
  26. #define UTEST_THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
  27. #else
  28. #define UTEST_THREAD_STACK_SIZE 4096
  29. #endif
  30. #ifdef UTEST_THR_PRIORITY
  31. #define UTEST_THREAD_PRIORITY UTEST_THR_PRIORITY
  32. #else
  33. #define UTEST_THREAD_PRIORITY FINSH_THREAD_PRIORITY
  34. #endif
  35. static rt_uint8_t utest_log_lv = UTEST_LOG_ALL;
  36. static utest_tc_export_t tc_table = RT_NULL;
  37. static rt_size_t tc_num;
  38. static rt_uint32_t tc_loop;
  39. static rt_uint8_t *tc_fail_list;
  40. static struct utest local_utest = {UTEST_PASSED, 0, 0};
  41. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  42. #pragma section="UtestTcTab"
  43. #elif defined(_MSC_VER)
  44. #pragma section("UtestTcTab$a", read)
  45. __declspec(allocate("UtestTcTab$a")) const struct utest_tc_export __tc_export_begin =
  46. {
  47. "__start",
  48. };
  49. #pragma section("UtestTcTab$z", read)
  50. __declspec(allocate("UtestTcTab$z")) const struct utest_tc_export __tc_export_end =
  51. {
  52. "__end",
  53. };
  54. #endif
  55. #define TC_FAIL_LIST_SIZE (RT_ALIGN(tc_num, 8) / 8)
  56. #define TC_FAIL_LIST_MARK_FAILED(index) (tc_fail_list[index / 8] |= (1UL << (index % 8)))
  57. #define TC_FAIL_LIST_IS_FAILED(index) (tc_fail_list[index / 8] & (1UL << (index % 8)))
  58. void utest_log_lv_set(rt_uint8_t lv)
  59. {
  60. if (lv == UTEST_LOG_ALL || lv == UTEST_LOG_ASSERT)
  61. {
  62. utest_log_lv = lv;
  63. }
  64. }
  65. static struct msh_cmd_opt utest_testcase_run_msh_options[RT_UTEST_MAX_OPTIONS];
  66. static void utest_build_options(void);
  67. static void utest_build_options(void)
  68. {
  69. rt_size_t i;
  70. rt_size_t option_index = 0;
  71. if (tc_num >= RT_UTEST_MAX_OPTIONS - 1)
  72. {
  73. LOG_W("The current number of test cases is (%d). Please expand RT_UTEST_MAX_OPTIONS's size to at least (%d).", tc_num, tc_num + 1);
  74. }
  75. rt_memset(utest_testcase_run_msh_options, 0, sizeof(utest_testcase_run_msh_options));
  76. rt_size_t max_cases = (tc_num < RT_UTEST_MAX_OPTIONS - 1) ? tc_num : RT_UTEST_MAX_OPTIONS - 1;
  77. for (i = 0; i < max_cases; i++)
  78. {
  79. utest_testcase_run_msh_options[option_index].id = i + 1;
  80. utest_testcase_run_msh_options[option_index].name = tc_table[i].name;
  81. utest_testcase_run_msh_options[option_index].des = tc_table[i].name;
  82. option_index++;
  83. }
  84. utest_testcase_run_msh_options[option_index].id = 0;
  85. utest_testcase_run_msh_options[option_index].name = RT_NULL;
  86. utest_testcase_run_msh_options[option_index].des = RT_NULL;
  87. }
  88. int utest_init(void)
  89. {
  90. /* initialize the utest commands table.*/
  91. #if defined(__ARMCC_VERSION) /* ARM C Compiler */
  92. extern const int UtestTcTab$$Base;
  93. extern const int UtestTcTab$$Limit;
  94. tc_table = (utest_tc_export_t)&UtestTcTab$$Base;
  95. tc_num = (utest_tc_export_t)&UtestTcTab$$Limit - tc_table;
  96. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  97. tc_table = (utest_tc_export_t)__section_begin("UtestTcTab");
  98. tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table;
  99. #else
  100. unsigned int *ptr_begin, *ptr_end;
  101. #if defined(__GNUC__)
  102. extern const int __rt_utest_tc_tab_start;
  103. extern const int __rt_utest_tc_tab_end;
  104. ptr_begin = (unsigned int *)&__rt_utest_tc_tab_start;
  105. ptr_end = (unsigned int *)&__rt_utest_tc_tab_end;
  106. #elif defined(_MSC_VER)
  107. ptr_begin = (unsigned int *)&__tc_export_begin;
  108. ptr_end = (unsigned int *)&__tc_export_end;
  109. ptr_begin += (sizeof(struct utest_tc_export) / sizeof(unsigned int));
  110. #endif
  111. while (*ptr_begin == 0) ptr_begin++;
  112. ptr_end--;
  113. while (*ptr_end == 0) ptr_end--;
  114. /* copy tc_table from rodata section to ram */
  115. for (unsigned int *ptr = ptr_begin; ptr < ptr_end;)
  116. {
  117. if (!tc_table)
  118. tc_table = (utest_tc_export_t)rt_malloc(sizeof(struct utest_tc_export));
  119. else
  120. tc_table = (utest_tc_export_t)rt_realloc(tc_table, (tc_num + 1)* sizeof(struct utest_tc_export));
  121. RT_ASSERT(tc_table);
  122. tc_table[tc_num++] = *((utest_tc_export_t)ptr);
  123. ptr += (sizeof(struct utest_tc_export) / sizeof(unsigned int));
  124. while (*ptr == 0) ptr++;
  125. }
  126. #endif
  127. LOG_I("utest is initialize success.");
  128. LOG_I("total utest testcase num: (%d)", tc_num);
  129. if (tc_num > 0)
  130. {
  131. tc_fail_list = rt_malloc(TC_FAIL_LIST_SIZE);
  132. if(!tc_fail_list)
  133. {
  134. LOG_E("no memory, tc_fail_list init failed!");
  135. }
  136. }
  137. utest_build_options();
  138. return tc_num;
  139. }
  140. INIT_COMPONENT_EXPORT(utest_init);
  141. static long utest_tc_list(void)
  142. {
  143. rt_size_t i = 0;
  144. LOG_I("Commands list : ");
  145. for (i = 0; i < tc_num; i++)
  146. {
  147. LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout);
  148. }
  149. return 0;
  150. }
  151. MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_list, output all utest testcase);
  152. static const char *file_basename(const char *file)
  153. {
  154. char *end_ptr = RT_NULL;
  155. char *rst = RT_NULL;
  156. if (!((end_ptr = strrchr(file, '\\')) != RT_NULL || \
  157. (end_ptr = strrchr(file, '/')) != RT_NULL) || \
  158. (rt_strlen(file) < 2))
  159. {
  160. rst = (char *)file;
  161. }
  162. else
  163. {
  164. rst = (char *)(end_ptr + 1);
  165. }
  166. return (const char *)rst;
  167. }
  168. static int utest_help(void)
  169. {
  170. rt_kprintf("\n");
  171. rt_kprintf("Command: utest_run\n");
  172. rt_kprintf(" info: Execute test cases.\n");
  173. rt_kprintf(" format: utest_run [-thread or -help] [testcase name] [loop num]\n");
  174. rt_kprintf(" usage:\n");
  175. rt_kprintf(" 1. utest_run\n");
  176. rt_kprintf(" Do not specify a test case name. Run all test cases.\n");
  177. rt_kprintf(" 2. utest_run -thread\n");
  178. rt_kprintf(" Do not specify a test case name. Run all test cases in threaded mode.\n");
  179. rt_kprintf(" 3. utest_run testcaseA\n");
  180. rt_kprintf(" Run 'testcaseA'.\n");
  181. rt_kprintf(" 4. utest_run testcaseA 10\n");
  182. rt_kprintf(" Run 'testcaseA' ten times.\n");
  183. rt_kprintf(" 5. utest_run -thread testcaseA\n");
  184. rt_kprintf(" Run 'testcaseA' in threaded mode.\n");
  185. rt_kprintf(" 6. utest_run -thread testcaseA 10\n");
  186. rt_kprintf(" Run 'testcaseA' ten times in threaded mode.\n");
  187. rt_kprintf(" 7. utest_run test*\n");
  188. rt_kprintf(" support '*' wildcard. Run all test cases starting with 'test'.\n");
  189. rt_kprintf(" 8. utest_run -help\n");
  190. rt_kprintf(" Show utest help information\n");
  191. rt_kprintf("\n");
  192. return 0;
  193. }
  194. static void utest_do_run(const char *utest_name)
  195. {
  196. rt_size_t i;
  197. rt_uint32_t index;
  198. rt_bool_t is_find;
  199. rt_uint32_t tc_fail_num = 0;
  200. rt_uint32_t tc_run_num = 0;
  201. for (index = 0; index < tc_loop; index ++)
  202. {
  203. i = 0;
  204. is_find = RT_FALSE;
  205. tc_fail_num = 0;
  206. tc_run_num = 0;
  207. if (tc_fail_list)
  208. {
  209. rt_memset(tc_fail_list, 0, TC_FAIL_LIST_SIZE);
  210. }
  211. LOG_I("[==========] [ utest ] loop %d/%d", index + 1, tc_loop);
  212. LOG_I("[==========] [ utest ] started");
  213. while(i < tc_num)
  214. {
  215. if (utest_name)
  216. {
  217. int len = rt_strlen(utest_name);
  218. if (utest_name[len - 1] == '*')
  219. {
  220. len -= 1;
  221. if (rt_memcmp(tc_table[i].name, utest_name, len) != 0)
  222. {
  223. i++;
  224. continue;
  225. }
  226. }
  227. else if (rt_strcmp(tc_table[i].name, utest_name) != 0)
  228. {
  229. i++;
  230. continue;
  231. }
  232. }
  233. is_find = RT_TRUE;
  234. LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
  235. if (tc_table[i].init != RT_NULL)
  236. {
  237. if (tc_table[i].init() != RT_EOK)
  238. {
  239. LOG_E("[ FAILED ] [ result ] testcase init (%s)", tc_table[i].name);
  240. goto __tc_continue;
  241. }
  242. }
  243. if (tc_table[i].tc != RT_NULL)
  244. {
  245. tc_table[i].tc();
  246. if (local_utest.failed_num == 0)
  247. {
  248. LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name);
  249. }
  250. else
  251. {
  252. TC_FAIL_LIST_MARK_FAILED(i);
  253. tc_fail_num ++;
  254. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  255. }
  256. }
  257. else
  258. {
  259. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  260. }
  261. if (tc_table[i].cleanup != RT_NULL)
  262. {
  263. if (tc_table[i].cleanup() != RT_EOK)
  264. {
  265. LOG_E("[ FAILED ] [ result ] testcase cleanup (%s)", tc_table[i].name);
  266. goto __tc_continue;
  267. }
  268. }
  269. __tc_continue:
  270. LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name);
  271. tc_run_num ++;
  272. i++;
  273. }
  274. if (i == tc_num && is_find == RT_FALSE && utest_name != RT_NULL)
  275. {
  276. LOG_I("[==========] [ utest ] Not find (%s)", utest_name);
  277. LOG_I("[==========] [ utest ] finished");
  278. break;
  279. }
  280. LOG_I("[==========] [ utest ] %d tests from %d testcase ran.", tc_run_num, tc_num);
  281. LOG_I("[ PASSED ] [ result ] %d tests.", tc_run_num - tc_fail_num);
  282. if(tc_fail_list && (tc_fail_num > 0))
  283. {
  284. LOG_E("[ FAILED ] [ result ] %d tests, listed below:", tc_fail_num);
  285. for(i = 0; i < tc_num; i ++)
  286. {
  287. if (TC_FAIL_LIST_IS_FAILED(i))
  288. {
  289. LOG_E("[ FAILED ] [ result ] %s", tc_table[i].name);
  290. }
  291. }
  292. }
  293. LOG_I("[==========] [ utest ] finished");
  294. }
  295. }
  296. static void utest_thr_entry(void *para)
  297. {
  298. char *utest_name = (char *)para;
  299. rt_thread_mdelay(1000); /* see commit:0dc7b9a for details */
  300. rt_kprintf("\n");
  301. utest_do_run(utest_name);
  302. }
  303. static void utest_thread_create(const char *utest_name)
  304. {
  305. rt_thread_t tid = RT_NULL;
  306. tid = rt_thread_create("utest",
  307. utest_thr_entry, (void *)utest_name,
  308. UTEST_THREAD_STACK_SIZE, UTEST_THREAD_PRIORITY, 10);
  309. if (tid != RT_NULL)
  310. {
  311. rt_thread_startup(tid);
  312. }
  313. }
  314. #ifdef RT_UTEST_USING_AUTO_RUN
  315. static int utest_auto_run(void)
  316. {
  317. tc_loop = 1;
  318. utest_thread_create(RT_NULL);
  319. return RT_EOK;
  320. }
  321. INIT_APP_EXPORT(utest_auto_run);
  322. #endif /* RT_UTEST_USING_AUTO_RUN */
  323. int utest_testcase_run(int argc, char** argv)
  324. {
  325. static char utest_name[UTEST_NAME_MAX_LEN];
  326. rt_memset(utest_name, 0x0, sizeof(utest_name));
  327. tc_loop = 1;
  328. if (argc == 1)
  329. {
  330. utest_thread_create(RT_NULL);
  331. }
  332. else if (argc == 2 || argc == 3 || argc == 4)
  333. {
  334. if (rt_strcmp(argv[1], "-thread") == 0)
  335. {
  336. if (argc == 3 || argc == 4)
  337. {
  338. rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
  339. if (argc == 4)
  340. {
  341. tc_loop = atoi(argv[3]);
  342. }
  343. }
  344. utest_thread_create(utest_name);
  345. }
  346. else if (rt_strcmp(argv[1], "-help") == 0)
  347. {
  348. utest_help();
  349. }
  350. else
  351. {
  352. rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
  353. if (argc == 3)
  354. {
  355. tc_loop = atoi(argv[2]);
  356. }
  357. utest_do_run(utest_name);
  358. }
  359. }
  360. else
  361. {
  362. LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__);
  363. utest_help();
  364. }
  365. return RT_EOK;
  366. }
  367. MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num], optenable);
  368. utest_t utest_handle_get(void)
  369. {
  370. return (utest_t)&local_utest;
  371. }
  372. void utest_unit_run(test_unit_func func, const char *unit_func_name)
  373. {
  374. LOG_I("[==========] utest unit name: (%s)", unit_func_name);
  375. local_utest.error = UTEST_PASSED;
  376. local_utest.passed_num = 0;
  377. local_utest.failed_num = 0;
  378. if (func != RT_NULL)
  379. {
  380. func();
  381. }
  382. }
  383. /*
  384. * utest_assert - assert function
  385. *
  386. * @param value - assert value
  387. * @param file - file name
  388. * @param line - line number
  389. * @param func - function name
  390. * @param msg - assert message
  391. *
  392. * @return - RT_TRUE: assert success; RT_FALSE: assert failed
  393. */
  394. rt_bool_t utest_assert(int value, const char *file, int line, const char *func, const char *msg)
  395. {
  396. rt_bool_t rst = RT_FALSE;
  397. if (!(value))
  398. {
  399. local_utest.error = UTEST_FAILED;
  400. local_utest.failed_num ++;
  401. LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
  402. rst = RT_FALSE;
  403. }
  404. else
  405. {
  406. if (utest_log_lv == UTEST_LOG_ALL)
  407. {
  408. LOG_D("[ OK ] [ unit ] (%s:%d) is passed", func, line);
  409. }
  410. local_utest.error = UTEST_PASSED;
  411. local_utest.passed_num ++;
  412. rst = RT_TRUE;
  413. }
  414. return rst;
  415. }
  416. 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)
  417. {
  418. rt_bool_t rst = RT_FALSE;
  419. if (a == RT_NULL || b == RT_NULL)
  420. {
  421. rst = utest_assert(0, file, line, func, msg);
  422. }
  423. else
  424. {
  425. if (equal)
  426. {
  427. if (rt_strcmp(a, b) == 0)
  428. {
  429. rst = utest_assert(1, file, line, func, msg);
  430. }
  431. else
  432. {
  433. rst = utest_assert(0, file, line, func, msg);
  434. }
  435. }
  436. else
  437. {
  438. if (rt_strcmp(a, b) == 0)
  439. {
  440. rst = utest_assert(0, file, line, func, msg);
  441. }
  442. else
  443. {
  444. rst = utest_assert(1, file, line, func, msg);
  445. }
  446. }
  447. }
  448. if (!rst)
  449. {
  450. LOG_E("[ ASSERT ] [ unit ] str-a: (%s); str-b: (%s)", a, b);
  451. }
  452. }
  453. 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)
  454. {
  455. if (a == RT_NULL || b == RT_NULL)
  456. {
  457. utest_assert(0, file, line, func, msg);
  458. }
  459. if (equal)
  460. {
  461. if (rt_memcmp(a, b, sz) == 0)
  462. {
  463. utest_assert(1, file, line, func, msg);
  464. }
  465. else
  466. {
  467. utest_assert(0, file, line, func, msg);
  468. }
  469. }
  470. else
  471. {
  472. if (rt_memcmp(a, b, sz) == 0)
  473. {
  474. utest_assert(0, file, line, func, msg);
  475. }
  476. else
  477. {
  478. utest_assert(1, file, line, func, msg);
  479. }
  480. }
  481. }