utest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #define DBG_SECTION_NAME "utest"
  18. #ifdef UTEST_DEBUG
  19. #define DBG_LEVEL DBG_LOG
  20. #else
  21. #define DBG_LEVEL 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 struct utest local_utest = {UTEST_PASSED, 0, 0};
  42. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  43. #pragma section="UtestTcTab"
  44. #endif
  45. void utest_log_lv_set(rt_uint8_t lv)
  46. {
  47. if (lv == UTEST_LOG_ALL || lv == UTEST_LOG_ASSERT)
  48. {
  49. utest_log_lv = lv;
  50. }
  51. }
  52. int utest_init(void)
  53. {
  54. /* initialize the utest commands table.*/
  55. #if defined(__CC_ARM) /* ARM C Compiler */
  56. extern const int UtestTcTab$$Base;
  57. extern const int UtestTcTab$$Limit;
  58. tc_table = (utest_tc_export_t)&UtestTcTab$$Base;
  59. tc_num = (utest_tc_export_t)&UtestTcTab$$Limit - tc_table;
  60. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  61. tc_table = (utest_tc_export_t)__section_begin("UtestTcTab");
  62. tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table;
  63. #elif defined (__GNUC__) /* for GCC Compiler */
  64. extern const int __rt_utest_tc_tab_start;
  65. extern const int __rt_utest_tc_tab_end;
  66. tc_table = (utest_tc_export_t)&__rt_utest_tc_tab_start;
  67. tc_num = (utest_tc_export_t) &__rt_utest_tc_tab_end - tc_table;
  68. #endif /* defined(__CC_ARM) */
  69. LOG_I("utest is initialize success.");
  70. LOG_I("total utest testcase num: (%d)", tc_num);
  71. return tc_num;
  72. }
  73. INIT_COMPONENT_EXPORT(utest_init);
  74. static void utest_tc_list(void)
  75. {
  76. rt_size_t i = 0;
  77. LOG_I("Commands list : ");
  78. for (i = 0; i < tc_num; i++)
  79. {
  80. LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout);
  81. }
  82. }
  83. MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_list, output all utest testcase);
  84. static const char *file_basename(const char *file)
  85. {
  86. char *end_ptr = RT_NULL;
  87. char *rst = RT_NULL;
  88. if (!((end_ptr = strrchr(file, '\\')) != RT_NULL || \
  89. (end_ptr = strrchr(file, '/')) != RT_NULL) || \
  90. (rt_strlen(file) < 2))
  91. {
  92. rst = (char *)file;
  93. }
  94. else
  95. {
  96. rst = (char *)(end_ptr + 1);
  97. }
  98. return (const char *)rst;
  99. }
  100. static int utest_help(void)
  101. {
  102. rt_kprintf("\n");
  103. rt_kprintf("Command: utest_run\n");
  104. rt_kprintf(" info: Execute test cases.\n");
  105. rt_kprintf(" format: utest_run [-thread or -help] [testcase name] [loop num]\n");
  106. rt_kprintf(" usage:\n");
  107. rt_kprintf(" 1. utest_run\n");
  108. rt_kprintf(" Do not specify a test case name. Run all test cases.\n");
  109. rt_kprintf(" 2. utest_run -thread\n");
  110. rt_kprintf(" Do not specify a test case name. Run all test cases in threaded mode.\n");
  111. rt_kprintf(" 3. utest_run testcaseA\n");
  112. rt_kprintf(" Run 'testcaseA'.\n");
  113. rt_kprintf(" 4. utest_run testcaseA 10\n");
  114. rt_kprintf(" Run 'testcaseA' ten times.\n");
  115. rt_kprintf(" 5. utest_run -thread testcaseA\n");
  116. rt_kprintf(" Run 'testcaseA' in threaded mode.\n");
  117. rt_kprintf(" 6. utest_run -thread testcaseA 10\n");
  118. rt_kprintf(" Run 'testcaseA' ten times in threaded mode.\n");
  119. rt_kprintf(" 7. utest_run test*\n");
  120. rt_kprintf(" support '*' wildcard. Run all test cases starting with 'test'.\n");
  121. rt_kprintf(" 8. utest_run -help\n");
  122. rt_kprintf(" Show utest help information\n");
  123. rt_kprintf("\n");
  124. return 0;
  125. }
  126. static void utest_run(const char *utest_name)
  127. {
  128. rt_size_t i;
  129. rt_uint32_t index;
  130. rt_bool_t is_find;
  131. rt_thread_mdelay(1000);
  132. for (index = 0; index < tc_loop; index ++)
  133. {
  134. i = 0;
  135. is_find = RT_FALSE;
  136. LOG_I("[==========] [ utest ] started");
  137. while(i < tc_num)
  138. {
  139. if (utest_name)
  140. {
  141. int len = strlen(utest_name);
  142. if (utest_name[len - 1] == '*')
  143. {
  144. len -= 1;
  145. }
  146. if (rt_memcmp(tc_table[i].name, utest_name, len) != 0)
  147. {
  148. i++;
  149. continue;
  150. }
  151. }
  152. is_find = RT_TRUE;
  153. LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
  154. if (tc_table[i].init != RT_NULL)
  155. {
  156. if (tc_table[i].init() != RT_EOK)
  157. {
  158. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  159. goto __tc_continue;
  160. }
  161. }
  162. if (tc_table[i].tc != RT_NULL)
  163. {
  164. tc_table[i].tc();
  165. if (local_utest.failed_num == 0)
  166. {
  167. LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name);
  168. }
  169. else
  170. {
  171. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  172. }
  173. }
  174. else
  175. {
  176. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  177. }
  178. if (tc_table[i].cleanup != RT_NULL)
  179. {
  180. if (tc_table[i].cleanup() != RT_EOK)
  181. {
  182. LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
  183. goto __tc_continue;
  184. }
  185. }
  186. __tc_continue:
  187. LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name);
  188. i++;
  189. }
  190. if (i == tc_num && is_find == RT_FALSE)
  191. {
  192. LOG_I("[==========] [ utest ] Not find (%s)", utest_name);
  193. LOG_I("[==========] [ utest ] finished");
  194. break;
  195. }
  196. LOG_I("[==========] [ utest ] finished");
  197. }
  198. }
  199. static void utest_testcase_run(int argc, char** argv)
  200. {
  201. void *thr_param = RT_NULL;
  202. static char utest_name[UTEST_NAME_MAX_LEN];
  203. rt_memset(utest_name, 0x0, sizeof(utest_name));
  204. tc_loop = 1;
  205. if (argc == 1)
  206. {
  207. utest_run(RT_NULL);
  208. return;
  209. }
  210. else if (argc == 2 || argc == 3 || argc == 4)
  211. {
  212. if (rt_strcmp(argv[1], "-thread") == 0)
  213. {
  214. rt_thread_t tid = RT_NULL;
  215. if (argc == 3 || argc == 4)
  216. {
  217. rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
  218. thr_param = (void*)utest_name;
  219. if (argc == 4) tc_loop = atoi(argv[3]);
  220. }
  221. tid = rt_thread_create("utest",
  222. (void (*)(void *))utest_run, thr_param,
  223. UTEST_THREAD_STACK_SIZE, UTEST_THREAD_PRIORITY, 10);
  224. if (tid != NULL)
  225. {
  226. rt_thread_startup(tid);
  227. }
  228. }
  229. else if (rt_strcmp(argv[1], "-help") == 0)
  230. {
  231. utest_help();
  232. }
  233. else
  234. {
  235. rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
  236. if (argc == 3) tc_loop = atoi(argv[2]);
  237. utest_run(utest_name);
  238. }
  239. }
  240. else
  241. {
  242. LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__);
  243. utest_help();
  244. }
  245. }
  246. MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num]);
  247. utest_t utest_handle_get(void)
  248. {
  249. return (utest_t)&local_utest;
  250. }
  251. void utest_unit_run(test_unit_func func, const char *unit_func_name)
  252. {
  253. // LOG_I("[==========] utest unit name: (%s)", unit_func_name);
  254. local_utest.error = UTEST_PASSED;
  255. local_utest.passed_num = 0;
  256. local_utest.failed_num = 0;
  257. if (func != RT_NULL)
  258. {
  259. func();
  260. }
  261. }
  262. void utest_assert(int value, const char *file, int line, const char *func, const char *msg)
  263. {
  264. if (!(value))
  265. {
  266. local_utest.error = UTEST_FAILED;
  267. local_utest.failed_num ++;
  268. LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
  269. }
  270. else
  271. {
  272. if (utest_log_lv == UTEST_LOG_ALL)
  273. {
  274. LOG_D("[ OK ] [ unit ] (%s:%d) is passed", func, line);
  275. }
  276. local_utest.error = UTEST_PASSED;
  277. local_utest.passed_num ++;
  278. }
  279. }
  280. 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)
  281. {
  282. if (a == RT_NULL || b == RT_NULL)
  283. {
  284. utest_assert(0, file, line, func, msg);
  285. }
  286. if (equal)
  287. {
  288. if (rt_strcmp(a, b) == 0)
  289. {
  290. utest_assert(1, file, line, func, msg);
  291. }
  292. else
  293. {
  294. utest_assert(0, file, line, func, msg);
  295. }
  296. }
  297. else
  298. {
  299. if (rt_strcmp(a, b) == 0)
  300. {
  301. utest_assert(0, file, line, func, msg);
  302. }
  303. else
  304. {
  305. utest_assert(1, file, line, func, msg);
  306. }
  307. }
  308. }
  309. 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)
  310. {
  311. if (a == RT_NULL || b == RT_NULL)
  312. {
  313. utest_assert(0, file, line, func, msg);
  314. }
  315. if (equal)
  316. {
  317. if (rt_memcmp(a, b, sz) == 0)
  318. {
  319. utest_assert(1, file, line, func, msg);
  320. }
  321. else
  322. {
  323. utest_assert(0, file, line, func, msg);
  324. }
  325. }
  326. else
  327. {
  328. if (rt_memcmp(a, b, sz) == 0)
  329. {
  330. utest_assert(0, file, line, func, msg);
  331. }
  332. else
  333. {
  334. utest_assert(1, file, line, func, msg);
  335. }
  336. }
  337. }