diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index 8e43cfa0a0..1175006a58 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -234,4 +234,13 @@ config RT_USING_UTEST bool "Enable utest (RT-Thread test framework)" default n + if RT_USING_UTEST + config UTEST_THR_STACK_SIZE + int "The utest thread stack size" + default 4096 + config UTEST_THR_PRIORITY + int "The utest thread priority" + default 20 + endif + endmenu diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index a0a42d7438..f7348b3be4 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -10,6 +10,8 @@ #include #include +#include + #include "utest.h" #include @@ -32,9 +34,22 @@ #error "RT_CONSOLEBUF_SIZE is less than 256!" #endif +#ifdef UTEST_THR_STACK_SIZE +#define UTEST_THREAD_STACK_SIZE UTEST_THR_STACK_SIZE +#else +#define UTEST_THREAD_STACK_SIZE (4096) +#endif + +#ifdef UTEST_THR_PRIORITY +#define UTEST_THREAD_PRIORITY UTEST_THR_PRIORITY +#else +#define UTEST_THREAD_PRIORITY FINSH_THREAD_PRIORITY +#endif + static rt_uint8_t utest_log_lv = UTEST_LOG_ALL; static utest_tc_export_t tc_table = RT_NULL; static rt_size_t tc_num; +static rt_uint32_t tc_loop; static struct utest local_utest = {UTEST_PASSED, 0, 0}; #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */ @@ -104,83 +119,168 @@ static const char *file_basename(const char *file) return (const char *)rst; } +static int utest_help(void) +{ + rt_kprintf("\n"); + rt_kprintf("Command: utest_run\n"); + rt_kprintf(" info: Execute test cases.\n"); + rt_kprintf(" format: utest_run [-thread or -help] [testcase name] [loop num]\n"); + rt_kprintf(" usage:\n"); + rt_kprintf(" 1. utest_run\n"); + rt_kprintf(" Do not specify a test case name. Run all test cases.\n"); + rt_kprintf(" 2. utest_run -thread\n"); + rt_kprintf(" Do not specify a test case name. Run all test cases in threaded mode.\n"); + rt_kprintf(" 3. utest_run testcaseA\n"); + rt_kprintf(" Run 'testcaseA'.\n"); + rt_kprintf(" 4. utest_run testcaseA 10\n"); + rt_kprintf(" Run 'testcaseA' ten times.\n"); + rt_kprintf(" 5. utest_run -thread testcaseA\n"); + rt_kprintf(" Run 'testcaseA' in threaded mode.\n"); + rt_kprintf(" 6. utest_run -thread testcaseA 10\n"); + rt_kprintf(" Run 'testcaseA' ten times in threaded mode.\n"); + rt_kprintf(" 7. utest_run test*\n"); + rt_kprintf(" support '*' wildcard. Run all test cases starting with 'test'.\n"); + rt_kprintf(" 8. utest_run -help\n"); + rt_kprintf(" Show utest help information\n"); + rt_kprintf("\n"); + return 0; +} + static void utest_run(const char *utest_name) { - rt_size_t i = 0; + rt_size_t i; + rt_uint32_t index; + rt_bool_t is_find; - LOG_I("[==========] [ utest ] started"); - while(i < tc_num) + rt_thread_mdelay(1000); + + for (index = 0; index < tc_loop; index ++) { - if (utest_name && rt_strcmp(utest_name, tc_table[i].name)) + i = 0; + is_find = RT_FALSE; + LOG_I("[==========] [ utest ] started"); + while(i < tc_num) { - i++; - continue; - } - - LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name); - if (tc_table[i].init != RT_NULL) - { - if (tc_table[i].init() != RT_EOK) + if (utest_name) { - LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); - goto __tc_continue; + int len = strlen(utest_name); + if (utest_name[len - 1] == '*') + { + len -= 1; + } + if (rt_memcmp(tc_table[i].name, utest_name, len) != 0) + { + i++; + continue; + } } - } + is_find = RT_TRUE; - if (tc_table[i].tc != RT_NULL) - { - tc_table[i].tc(); - if (local_utest.failed_num == 0) + LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name); + if (tc_table[i].init != RT_NULL) { - LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name); + if (tc_table[i].init() != RT_EOK) + { + LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + goto __tc_continue; + } + } + + if (tc_table[i].tc != RT_NULL) + { + tc_table[i].tc(); + if (local_utest.failed_num == 0) + { + LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name); + } + else + { + LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + } } else { LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); } - } - else - { - LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); - } - if (tc_table[i].cleanup != RT_NULL) - { - if (tc_table[i].cleanup() != RT_EOK) + if (tc_table[i].cleanup != RT_NULL) { - LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); - goto __tc_continue; + if (tc_table[i].cleanup() != RT_EOK) + { + LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + goto __tc_continue; + } } + + __tc_continue: + LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name); + + i++; } -__tc_continue: - LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name); + if (i == tc_num && is_find == RT_FALSE) + { + LOG_I("[==========] [ utest ] Not find (%s)", utest_name); + LOG_I("[==========] [ utest ] finished"); + break; + } - i++; + LOG_I("[==========] [ utest ] finished"); } - LOG_I("[==========] [ utest ] finished"); } static void utest_testcase_run(int argc, char** argv) { - char utest_name[UTEST_NAME_MAX_LEN]; + void *thr_param = RT_NULL; + + static char utest_name[UTEST_NAME_MAX_LEN]; + rt_memset(utest_name, 0x0, sizeof(utest_name)); + + tc_loop = 1; if (argc == 1) { utest_run(RT_NULL); + return; } - else if (argc == 2) + else if (argc == 2 || argc == 3 || argc == 4) { - rt_memset(utest_name, 0x0, sizeof(utest_name)); - rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1); - utest_run(utest_name); + if (rt_strcmp(argv[1], "-thread") == 0) + { + rt_thread_t tid = RT_NULL; + if (argc == 3 || argc == 4) + { + rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1); + thr_param = (void*)utest_name; + + if (argc == 4) tc_loop = atoi(argv[3]); + } + tid = rt_thread_create("utest", + (void (*)(void *))utest_run, thr_param, + UTEST_THREAD_STACK_SIZE, UTEST_THREAD_PRIORITY, 10); + if (tid != NULL) + { + rt_thread_startup(tid); + } + } + else if (rt_strcmp(argv[1], "-help") == 0) + { + utest_help(); + } + else + { + rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1); + if (argc == 3) tc_loop = atoi(argv[2]); + utest_run(utest_name); + } } else { LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__); + utest_help(); } } -MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [testcase name]); +MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num]); utest_t utest_handle_get(void) {