From 82f022f6f0e975243e25888c5ba8b5dfb37fd4f6 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Wed, 13 Feb 2019 09:37:26 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[components][utest]=20=E5=A2=9E=E5=8A=A0=20?= =?UTF-8?q?utest=20=E7=BA=BF=E7=A8=8B=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/Kconfig | 9 ++++++ components/utilities/utest/utest.c | 46 ++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 6 deletions(-) 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..ee9710c0df 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -32,6 +32,18 @@ #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; @@ -163,24 +175,46 @@ __tc_continue: 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)); if (argc == 1) { utest_run(RT_NULL); + return; } - else if (argc == 2) + else if (argc == 2 || argc == 3) { - 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) + { + rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1); + thr_param = (void*)utest_name; + } + 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 + { + rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1); + utest_run(utest_name); + } } else { LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__); } } -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] [testcase name]); utest_t utest_handle_get(void) { From 0dc7b9a5a26030a945723569c64f5be46bd70ae1 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Thu, 14 Feb 2019 12:00:58 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[components][utest]=20utest=20=E5=BB=B6?= =?UTF-8?q?=E6=97=B6=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在使用 utest 的线程模式的时候,utest_run 命令执行完成后,finsh 会输出 `msh >`,干扰了 utest 内部的日志结构,因此默认在 utest 启动前增加延时。 Signed-off-by: MurphyZhao --- components/utilities/utest/utest.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index ee9710c0df..8d89e3a2a0 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -120,6 +120,8 @@ static void utest_run(const char *utest_name) { rt_size_t i = 0; + rt_thread_mdelay(1000); + LOG_I("[==========] [ utest ] started"); while(i < tc_num) { From a305c6cca93cb7c33cd522098163ae52d6964e90 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Thu, 14 Feb 2019 15:23:07 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[components][utest]=20utest=5Frun=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20loop=20=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E6=8C=81=E7=BB=AD=E8=BF=90=E8=A1=8C=E5=8D=95?= =?UTF-8?q?=E4=B8=80=E4=B8=80=E4=B8=AA=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=EF=BC=9B=20[components][utest]=20utest=5Frun=20=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E5=90=8D=E5=AD=97?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E9=85=8D=E7=AC=A6=20`*`=20=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E5=8F=8A=E6=94=AF=E6=8C=81=E4=BB=85=E6=8C=87=E5=AE=9A=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=E5=90=8D=E5=AD=97=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=AD=97=E8=8A=82=E6=9D=A5=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E3=80=82=E8=AF=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BC=9A=E6=89=A7=E8=A1=8C=E5=8C=B9=E9=85=8D=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E7=9A=84=E6=89=80=E6=9C=89=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/utest.c | 97 ++++++++++++++++++------------ 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 8d89e3a2a0..13cb405550 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -10,6 +10,8 @@ #include #include +#include + #include "utest.h" #include @@ -47,6 +49,7 @@ 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 */ @@ -118,61 +121,74 @@ static const char *file_basename(const char *file) static void utest_run(const char *utest_name) { - rt_size_t i = 0; + rt_size_t i; + rt_uint32_t index; rt_thread_mdelay(1000); - LOG_I("[==========] [ utest ] started"); - while(i < tc_num) + for (index = 0; index < tc_loop; index ++) { - if (utest_name && rt_strcmp(utest_name, tc_table[i].name)) + i = 0; + 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; + } } - } - 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); - - i++; + LOG_I("[==========] [ utest ] finished"); } - LOG_I("[==========] [ utest ] finished"); } static void utest_testcase_run(int argc, char** argv) @@ -182,20 +198,24 @@ static void utest_testcase_run(int argc, char** argv) 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 || argc == 3) + else if (argc == 2 || argc == 3 || argc == 4) { if (rt_strcmp(argv[1], "-thread") == 0) { rt_thread_t tid = RT_NULL; - if (argc == 3) + 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, @@ -208,6 +228,7 @@ static void utest_testcase_run(int argc, char** argv) else { rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1); + if (argc == 3) tc_loop = atoi(argv[2]); utest_run(utest_name); } } From e3546a50437404c8d95f63d1f830f98d35c09bce Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Fri, 15 Feb 2019 11:47:21 +0800 Subject: [PATCH 4/4] =?UTF-8?q?[components][utest]=20=E5=A2=9E=E5=8A=A0=20?= =?UTF-8?q?utest=5Fhelp=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=B8=AE=E5=8A=A9=E4=BF=A1=E6=81=AF=20[components][utest]=20?= =?UTF-8?q?=E5=AF=B9=E4=BA=8E=E4=B8=8D=E6=94=AF=E6=8C=81=E7=9A=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/utest.c | 45 +++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 13cb405550..f7348b3be4 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -119,16 +119,45 @@ 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; rt_uint32_t index; + rt_bool_t is_find; rt_thread_mdelay(1000); for (index = 0; index < tc_loop; index ++) { i = 0; + is_find = RT_FALSE; LOG_I("[==========] [ utest ] started"); while(i < tc_num) { @@ -145,6 +174,7 @@ static void utest_run(const char *utest_name) continue; } } + is_find = RT_TRUE; LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name); if (tc_table[i].init != RT_NULL) @@ -187,6 +217,14 @@ static void utest_run(const char *utest_name) i++; } + + if (i == tc_num && is_find == RT_FALSE) + { + LOG_I("[==========] [ utest ] Not find (%s)", utest_name); + LOG_I("[==========] [ utest ] finished"); + break; + } + LOG_I("[==========] [ utest ] finished"); } } @@ -225,6 +263,10 @@ static void utest_testcase_run(int argc, char** argv) 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); @@ -235,9 +277,10 @@ static void utest_testcase_run(int argc, char** argv) 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 [-thread] [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) {