utest.c 13 KB

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