From 85d275b399b052e529134d2ca632b0a5602c30f3 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Fri, 30 Nov 2018 16:12:35 +0800 Subject: [components/utilities][add] 增加测试框架 utest 代码 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/SConscript | 8 + components/utilities/utest/utest.c | 236 ++++++++++++++++++++++++++++++ components/utilities/utest/utest.h | 69 +++++++++ components/utilities/utest/utest_assert.h | 36 +++++ components/utilities/utest/utest_log.h | 31 ++++ 5 files changed, 380 insertions(+) create mode 100644 components/utilities/utest/SConscript create mode 100644 components/utilities/utest/utest.c create mode 100644 components/utilities/utest/utest.h create mode 100644 components/utilities/utest/utest_assert.h create mode 100644 components/utilities/utest/utest_log.h (limited to 'components') diff --git a/components/utilities/utest/SConscript b/components/utilities/utest/SConscript new file mode 100644 index 0000000000..91ff8a553e --- /dev/null +++ b/components/utilities/utest/SConscript @@ -0,0 +1,8 @@ +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] +group = DefineGroup('utest', src, depend = [], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c new file mode 100644 index 0000000000..f8881de2d0 --- /dev/null +++ b/components/utilities/utest/utest.c @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-19 MurphyZhao the first version + */ + +#include "utest.h" +#include +#include + +#undef DBG_SECTION_NAME +#undef DBG_LEVEL +#undef DBG_COLOR +#undef DBG_ENABLE + +#define DBG_ENABLE +#define DBG_SECTION_NAME "utest" +#ifdef UTEST_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif +#define DBG_COLOR +#include + +#if RT_CONSOLEBUF_SIZE < 256 +#error "RT_CONSOLEBUF_SIZE is less than 256!" +#endif + +static utest_tc_export_t tc_table = RT_NULL; +static rt_size_t tc_num; +static struct utest local_utest = {UTEST_PASSED, 0, 0}; + +#if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */ +#pragma section="UtestTcTab" +#endif + +int utest_init(void) +{ + /* initialize the utest commands table.*/ +#if defined(__CC_ARM) /* ARM C Compiler */ + extern const int UtestTcTab$$Base; + extern const int UtestTcTab$$Limit; + tc_table = (utest_tc_export_t)&UtestTcTab$$Base; + tc_num = (utest_tc_export_t)&UtestTcTab$$Limit - tc_table; +#elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */ + tc_table = (utest_tc_export_t)__section_begin("UtestTcTab"); + tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table; +#elif defined (__GNUC__) /* for GCC Compiler */ + extern const int __rtatcmdtab_start; + extern const int __rtatcmdtab_end; + tc_table = (utest_tc_export_t)&__rtatcmdtab_start; + tc_num = (utest_tc_export_t) &__rtatcmdtab_end - tc_table; +#endif /* defined(__CC_ARM) */ + + LOG_D("[ ] total utest testcase num: (%d)", tc_num); + return tc_num; +} +INIT_COMPONENT_EXPORT(utest_init); + +static void utest_tc_list(void) +{ + rt_size_t i = 0; + + LOG_D("Commands list : "); + + for (i = 0; i < tc_num; i++) + { + LOG_D("%s", tc_table[i].name); + } +} +MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_tc_list, output all utest testcase); + +static char *file_basename(const char *file) +{ + char *ptr = RT_NULL; + char *rst = RT_NULL; + char *file_bak = rt_strdup(file); + uint8_t len = 0; + if ((ptr = strrchr(file_bak, '\\')) != RT_NULL || (ptr = strrchr(file_bak, '/')) != RT_NULL) + { + rst = ptr; + } + else + { + rst = file_bak; + } + + len = rst - file_bak; + if (rst != file) + { + file_bak[len] = '\0'; + + if ((ptr = strrchr(file_bak, '\\')) != RT_NULL || (ptr = strrchr(file_bak, '/')) != RT_NULL) + { + rst = ptr; + } + else + { + rst = file_bak; + } + len = rst - file_bak; + } + + rt_free(file_bak); + len = len != 0? len + 1 : len; + return (char *)(file + len); +} + +static void utest_run(const char *utest_name) +{ + rt_size_t i = 0; + + LOG_D("[ ] total utest testcase num: (%d)", tc_num); + + LOG_I("[==========] [ utest ] started"); + while(i < tc_num) + { + if (utest_name && rt_strcmp(utest_name, tc_table[i].name)) + { + i++; + continue; + } + + if (tc_table[i].init != RT_NULL) + { + tc_table[i].init(); + } + + LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name); + tc_table[i].tc(); + if (local_utest.failed_num == 0) + { + LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name); + } + else + { + LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + } + LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name); + + if (tc_table[i].cleanup != RT_NULL) + { + tc_table[i].cleanup(); + } + + i++; + } + LOG_I("[==========] [ utest ] finished"); +} + +static void utest_testcase_run(int argc, char** argv) +{ + char utest_name[UTEST_NAME_MAX_LEN]; + + if (argc == 1) + { + utest_run(RT_NULL); + } + else if (argc == 2) + { + rt_memset(utest_name, 0x0, sizeof(utest_name)); + 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]); + +utest_t utest_handle_get(void) +{ + return (utest_t)&local_utest; +} + +void utest_unit_run(test_unit_func func, const char *unit_func_name) +{ + // LOG_I("[==========] utest unit name: (%s)", unit_func_name); + local_utest.error = UTEST_PASSED; + local_utest.passed_num = 0; + local_utest.failed_num = 0; + func(); +} + +void utest_assert(int value, const char *file, int line, const char *func, const char *msg) +{ + if (!(value)) + { + local_utest.error = UTEST_FAILED; + local_utest.failed_num ++; + LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg); + } + else + { + LOG_D("[ OK ] [ unit ] (%s:%d) is passed", func, line); + local_utest.error = UTEST_PASSED; + local_utest.passed_num ++; + } +} + +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) +{ + if (a == RT_NULL || b == RT_NULL) + { + utest_assert(0, file, line, func, msg); + } + + if (equal) + { + if (rt_strcmp(a, b) == 0) + { + utest_assert(1, file, line, func, msg); + } + else + { + utest_assert(0, file, line, func, msg); + } + } + else + { + if (rt_strcmp(a, b) == 0) + { + utest_assert(0, file, line, func, msg); + } + else + { + utest_assert(1, file, line, func, msg); + } + } +} diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h new file mode 100644 index 0000000000..3db5895462 --- /dev/null +++ b/components/utilities/utest/utest.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-19 MurphyZhao the first version + */ + +#ifndef __UTEST_H__ +#define __UTEST_H__ + +#include +#include "utest_log.h" + +#define UTEST_SW_VERSION "0.0.1" + +enum utest_error +{ + UTEST_PASSED = 0, + UTEST_FAILED = 1, + UTEST_SKIPPED = 2 +}; +typedef enum utest_error utest_err_e; + +struct utest +{ + utest_err_e error; + uint32_t passed_num; + uint32_t failed_num; +}; +typedef struct utest *utest_t; + +struct utest_tc_export { + const char *name; + rt_err_t (*init)(void); + void (*tc)(void); + rt_err_t (*cleanup)(void); +}; +typedef struct utest_tc_export *utest_tc_export_t; + +typedef void (*test_unit_func)(void); + +#define TC_LOG_I(...) LOG_I(__VA_ARGS__) +#define TC_LOG_D(...) LOG_D(__VA_ARGS__) +#define TC_LOG_E(...) LOG_E(__VA_ARGS__) +#define TC_LOG_W(...) LOG_W(__VA_ARGS__) + +void utest_unit_run(test_unit_func func, const char *unit_func_name); +utest_t utest_handle_get(void); + +#define UTEST_NAME_MAX_LEN (128u) + +#define UTEST_TC_EXPORT(testcase, name, init, cleanup) \ + RT_USED static const struct utest_tc_export _utest_testcase \ + SECTION("UtestTcTab") = \ + { \ + name, \ + init, \ + testcase, \ + cleanup \ + } + +#define UTEST_UNIT_RUN(test_unit_func) \ + utest_unit_run(test_unit_func, #test_unit_func); \ + if(utest_handle_get()->failed_num != 0) return; + +#endif diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h new file mode 100644 index 0000000000..57346b701f --- /dev/null +++ b/components/utilities/utest/utest_assert.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-19 MurphyZhao the first version + */ + +#ifndef __UTEST_ASSERT_H__ +#define __UTEST_ASSERT_H__ + +#include "utest.h" +#include + +void utest_assert(int value, const char *file, int line, const char *func, const char *msg); +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); + +#define __utest_assert(value, msg) utest_assert(value, __FILE__, __LINE__, __func__, msg) + +#define uassert_true(value) __utest_assert(value, "(" #value ") is false") +#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true") +#define uassert_null(value) __utest_assert((value) == NULL, "(" #value ") is not null") +#define uassert_not_null(value) __utest_assert((value) != NULL, "(" #value ") is null") + +#define uassert_int_equal(a, b) __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")") +#define uassert_int_not_equal(a, b) __utest_assert((a) != (b), "(" #a ") equal to (" #b ")") + +#define uassert_str_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_TRUE, __FILE__, __LINE__, __func__, "string not equal") +#define uassert_str_not_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal") + +#define uassert_in_range(value, min, max) __utest_assert(((value >= min) && (value <= max)), "(" #value ") not in range("#min","#max")") +#define uassert_not_in_range(value, min, max) __utest_assert(!((value >= min) && (value <= max)), "(" #value ") in range("#min","#max")") + +#endif /* __UTEST_ASSERT_H__ */ diff --git a/components/utilities/utest/utest_log.h b/components/utilities/utest/utest_log.h new file mode 100644 index 0000000000..c954046b76 --- /dev/null +++ b/components/utilities/utest/utest_log.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-19 MurphyZhao the first version + */ + +#ifndef __UTEST_LOG_H__ +#define __UTEST_LOG_H__ + +#define UTEST_DEBUG + +#undef DBG_SECTION_NAME +#undef DBG_LEVEL +#undef DBG_COLOR +#undef DBG_ENABLE + +#define DBG_ENABLE +#define DBG_SECTION_NAME "testcase" +#ifdef UTEST_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif +#define DBG_COLOR +#include + +#endif /* __UTEST_LOG_H__ */ -- cgit v1.3.1 From 1ecf6cbc7c7dfd32bb77d551ccd391be6705900a Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Wed, 5 Dec 2018 14:53:13 +0800 Subject: 【更新】utest gcc 链接符 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/utest.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'components') diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index f8881de2d0..4226738ce7 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -51,10 +51,10 @@ int utest_init(void) tc_table = (utest_tc_export_t)__section_begin("UtestTcTab"); tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table; #elif defined (__GNUC__) /* for GCC Compiler */ - extern const int __rtatcmdtab_start; - extern const int __rtatcmdtab_end; - tc_table = (utest_tc_export_t)&__rtatcmdtab_start; - tc_num = (utest_tc_export_t) &__rtatcmdtab_end - tc_table; + extern const int __rt_utest_tc_tab_start; + extern const int __rt_utest_tc_tab_end; + tc_table = (utest_tc_export_t)&__rt_utest_tc_tab_start; + tc_num = (utest_tc_export_t) &__rt_utest_tc_tab_end - tc_table; #endif /* defined(__CC_ARM) */ LOG_D("[ ] total utest testcase num: (%d)", tc_num); -- cgit v1.3.1 From 0759b66eb971f0baad1148586ba0db8ab98a1c82 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Wed, 5 Dec 2018 18:06:33 +0800 Subject: 【更新】components/utilities/utest 增加 SConscript depend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/SConscript | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components') diff --git a/components/utilities/utest/SConscript b/components/utilities/utest/SConscript index 91ff8a553e..dc77bc2591 100644 --- a/components/utilities/utest/SConscript +++ b/components/utilities/utest/SConscript @@ -3,6 +3,6 @@ from building import * cwd = GetCurrentDir() src = Glob('*.c') CPPPATH = [cwd] -group = DefineGroup('utest', src, depend = [], CPPPATH = CPPPATH) +group = DefineGroup('utest', src, depend = ['RT_USING_UTEST'], CPPPATH = CPPPATH) Return('group') -- cgit v1.3.1 From 8824b2ca7780c3874de7703a6c4e17b97fe63595 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Thu, 6 Dec 2018 17:11:27 +0800 Subject: [components/utilities] [Kconfig] 增加 kconfig RT_USING_UTEST 配置 [components/utilities] [utest] 增加 testcase 运行超时时间参数 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/Kconfig | 4 ++++ components/utilities/utest/utest.c | 9 ++++----- components/utilities/utest/utest.h | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) (limited to 'components') diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index e830d94578..8e43cfa0a0 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -230,4 +230,8 @@ config RT_USING_ULOG sfotware module version number endif +config RT_USING_UTEST + bool "Enable utest (RT-Thread test framework)" + default n + endmenu diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 4226738ce7..18d2c88d0a 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -57,7 +57,8 @@ int utest_init(void) tc_num = (utest_tc_export_t) &__rt_utest_tc_tab_end - tc_table; #endif /* defined(__CC_ARM) */ - LOG_D("[ ] total utest testcase num: (%d)", tc_num); + LOG_I("utest is initialize success."); + LOG_I("total utest testcase num: (%d)", tc_num); return tc_num; } INIT_COMPONENT_EXPORT(utest_init); @@ -66,11 +67,11 @@ static void utest_tc_list(void) { rt_size_t i = 0; - LOG_D("Commands list : "); + LOG_I("Commands list : "); for (i = 0; i < tc_num; i++) { - LOG_D("%s", tc_table[i].name); + LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout); } } MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_tc_list, output all utest testcase); @@ -115,8 +116,6 @@ static void utest_run(const char *utest_name) { rt_size_t i = 0; - LOG_D("[ ] total utest testcase num: (%d)", tc_num); - LOG_I("[==========] [ utest ] started"); while(i < tc_num) { diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index 3db5895462..9532b115e6 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -34,6 +34,7 @@ typedef struct utest *utest_t; struct utest_tc_export { const char *name; + uint32_t run_timeout; rt_err_t (*init)(void); void (*tc)(void); rt_err_t (*cleanup)(void); @@ -52,18 +53,19 @@ utest_t utest_handle_get(void); #define UTEST_NAME_MAX_LEN (128u) -#define UTEST_TC_EXPORT(testcase, name, init, cleanup) \ +#define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ RT_USED static const struct utest_tc_export _utest_testcase \ - SECTION("UtestTcTab") = \ + SECTION("UtestTcTab") = \ { \ - name, \ - init, \ - testcase, \ - cleanup \ + name, \ + timeout, \ + init, \ + testcase, \ + cleanup \ } -#define UTEST_UNIT_RUN(test_unit_func) \ - utest_unit_run(test_unit_func, #test_unit_func); \ +#define UTEST_UNIT_RUN(test_unit_func) \ + utest_unit_run(test_unit_func, #test_unit_func); \ if(utest_handle_get()->failed_num != 0) return; #endif -- cgit v1.3.1 From 51b17a15e4393495d72ec350fbfc392dc129753e Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Thu, 6 Dec 2018 18:58:23 +0800 Subject: [components/utilities/utest] 完善代码,增加注释 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/utest.c | 37 +++++++--- components/utilities/utest/utest.h | 108 +++++++++++++++++++++++++++++- components/utilities/utest/utest_assert.h | 22 ++++++ 3 files changed, 158 insertions(+), 9 deletions(-) (limited to 'components') diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 18d2c88d0a..5ba36ff397 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -125,28 +125,45 @@ static void utest_run(const char *utest_name) continue; } + LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name); if (tc_table[i].init != RT_NULL) { - tc_table[i].init(); + if (tc_table[i].init() != RT_EOK) + { + LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + goto __tc_continue; + } } - LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name); - tc_table[i].tc(); - if (local_utest.failed_num == 0) + if (tc_table[i].tc != RT_NULL) { - LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name); + tc_table[i].tc(); + if (local_utest.failed_num == 0) + { + LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name); + } + else + { + LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + } } else { LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); } - LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name); if (tc_table[i].cleanup != RT_NULL) { - tc_table[i].cleanup(); + if (tc_table[i].cleanup() != RT_EOK) + { + LOG_I("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); + goto __tc_continue; + } } +__tc_continue: + LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name); + i++; } LOG_I("[==========] [ utest ] finished"); @@ -184,7 +201,11 @@ void utest_unit_run(test_unit_func func, const char *unit_func_name) local_utest.error = UTEST_PASSED; local_utest.passed_num = 0; local_utest.failed_num = 0; - func(); + + if (func != RT_NULL) + { + func(); + } } void utest_assert(int value, const char *file, int line, const char *func, const char *msg) diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index 9532b115e6..9a5e58fc57 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -16,6 +16,16 @@ #define UTEST_SW_VERSION "0.0.1" +/** + * utest_error + * + * @brief Test result. + * + * @member UTEST_PASSED Test success. + * @member UTEST_FAILED Test failed. + * @member UTEST_PASSED Test skipped. + * +*/ enum utest_error { UTEST_PASSED = 0, @@ -24,6 +34,16 @@ enum utest_error }; typedef enum utest_error utest_err_e; +/** + * utest + * + * @brief utest data structure. + * + * @member error Error number from enum `utest_error`. + * @member passed_num Total number of tests passed. + * @member failed_num Total number of tests failed. + * +*/ struct utest { utest_err_e error; @@ -32,6 +52,19 @@ struct utest }; typedef struct utest *utest_t; +/** + * utest_tc_export + * + * @brief utest testcase data structure. + * Will export the data to `UtestTcTab` section in flash. + * + * @member name Testcase name. + * @member run_timeout Testcase maximum test time. + * @member init Necessary initialization before executing the test case function. + * @member tc Total number of tests failed. + * @member cleanup Total number of tests failed. + * +*/ struct utest_tc_export { const char *name; uint32_t run_timeout; @@ -41,18 +74,80 @@ struct utest_tc_export { }; typedef struct utest_tc_export *utest_tc_export_t; +/** + * test_unit_func + * + * @brief Unit test handler function pointer. + * +*/ typedef void (*test_unit_func)(void); +/** + * TC_LOG_x + * + * @brief Log output interface used in test cases. + * + * @type TC_LOG_I Output info level log. + * @type TC_LOG_D Output debug level log. + * @type TC_LOG_E Output error level log. + * @type TC_LOG_W Output warning level log. + * +*/ #define TC_LOG_I(...) LOG_I(__VA_ARGS__) #define TC_LOG_D(...) LOG_D(__VA_ARGS__) #define TC_LOG_E(...) LOG_E(__VA_ARGS__) #define TC_LOG_W(...) LOG_W(__VA_ARGS__) +/** + * utest_unit_run + * + * @brief Unit test function executor. + * No need for the user to call this function directly + * + * @param func Unit test function. + * @param unit_func_name Unit test function name. + * + * @return void + * +*/ void utest_unit_run(test_unit_func func, const char *unit_func_name); + +/** + * utest_handle_get + * + * @brief Get the utest data structure handle. + * No need for the user to call this function directly + * + * @param void + * + * @return utest_t type. (struct utest *) + * +*/ utest_t utest_handle_get(void); +/** + * UTEST_NAME_MAX_LEN + * + * @brief Testcase name maximum length. + * +*/ #define UTEST_NAME_MAX_LEN (128u) +/** + * UTEST_TC_EXPORT + * + * @brief Export testcase function to `UtestTcTab` section in flash. + * Used in application layer. + * + * @param testcase The testcase function. + * @param name The testcase name. + * @param init The initialization function of the test case. + * @param cleanup The cleanup function of the test case. + * @param timeout Testcase maximum test time. + * + * @return None + * +*/ #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ RT_USED static const struct utest_tc_export _utest_testcase \ SECTION("UtestTcTab") = \ @@ -64,8 +159,19 @@ utest_t utest_handle_get(void); cleanup \ } +/** + * UTEST_UNIT_RUN + * + * @brief Unit test function executor. + * Used in `testcase` function in application. + * + * @param test_unit_func Unit test function + * + * @return None + * +*/ #define UTEST_UNIT_RUN(test_unit_func) \ utest_unit_run(test_unit_func, #test_unit_func); \ if(utest_handle_get()->failed_num != 0) return; -#endif +#endif /* __UTEST_H__ */ diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h index 57346b701f..e36fdc8af4 100644 --- a/components/utilities/utest/utest_assert.h +++ b/components/utilities/utest/utest_assert.h @@ -14,11 +14,33 @@ #include "utest.h" #include +/* No need for the user to use this function directly */ void utest_assert(int value, const char *file, int line, const char *func, const char *msg); + +/* No need for the user to use this function directly */ 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); +/* No need for the user to use this macro directly */ #define __utest_assert(value, msg) utest_assert(value, __FILE__, __LINE__, __func__, msg) +/** + * uassert_x macros + * + * @brief Get the utest data structure handle. + * No need for the user to call this function directly. + * + * @macro uassert_true if @value is true, not assert, means passing. + * @macro uassert_false if @value is false, not assert, means passing. + * @macro uassert_null if @value is null, not assert, means passing. + * @macro uassert_not_null if @value is not null, not assert, means passing. + * @macro uassert_int_equal if @a equal to @b, not assert, means passing. Integer type test. + * @macro uassert_int_not_equal if @a not equal to @b, not assert, means passing. Integer type test. + * @macro uassert_str_equal if @a equal to @b, not assert, means passing. String type test. + * @macro uassert_str_not_equal if @a not equal to @b, not assert, means passing. String type test. + * @macro uassert_in_range if @value is in range of min and max, not assert, means passing. + * @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing. + * +*/ #define uassert_true(value) __utest_assert(value, "(" #value ") is false") #define uassert_false(value) __utest_assert(!(value), "(" #value ") is true") #define uassert_null(value) __utest_assert((value) == NULL, "(" #value ") is not null") -- cgit v1.3.1 From 98b10877e7e885da961b571bdb8cc3460021ba4d Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Sat, 8 Dec 2018 14:46:38 +0800 Subject: [components/utilities/utest] 修复 gcc 编译警告 [components/utilities/utest] 移除组件的版本号 [components/utilities/utest] 优化 basename 函数的处理 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/utest.c | 38 ++++++++----------------------- components/utilities/utest/utest.h | 2 -- components/utilities/utest/utest_assert.h | 4 ++-- 3 files changed, 12 insertions(+), 32 deletions(-) (limited to 'components') diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 5ba36ff397..0f5a206d3c 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -74,42 +74,24 @@ static void utest_tc_list(void) LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout); } } -MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_tc_list, output all utest testcase); +MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_list, output all utest testcase); -static char *file_basename(const char *file) +static const char *file_basename(const char *file) { - char *ptr = RT_NULL; + char *end_ptr = RT_NULL; char *rst = RT_NULL; - char *file_bak = rt_strdup(file); - uint8_t len = 0; - if ((ptr = strrchr(file_bak, '\\')) != RT_NULL || (ptr = strrchr(file_bak, '/')) != RT_NULL) + + if (!((end_ptr = strrchr(file, '\\')) != RT_NULL || \ + (end_ptr = strrchr(file, '/')) != RT_NULL) || \ + (rt_strlen(file) < 2)) { - rst = ptr; + rst = (char *)file; } else { - rst = file_bak; + rst = (char *)(end_ptr + 1); } - - len = rst - file_bak; - if (rst != file) - { - file_bak[len] = '\0'; - - if ((ptr = strrchr(file_bak, '\\')) != RT_NULL || (ptr = strrchr(file_bak, '/')) != RT_NULL) - { - rst = ptr; - } - else - { - rst = file_bak; - } - len = rst - file_bak; - } - - rt_free(file_bak); - len = len != 0? len + 1 : len; - return (char *)(file + len); + return (const char *)rst; } static void utest_run(const char *utest_name) diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index 9a5e58fc57..fc01852b6a 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -14,8 +14,6 @@ #include #include "utest_log.h" -#define UTEST_SW_VERSION "0.0.1" - /** * utest_error * diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h index e36fdc8af4..997e8ae7f3 100644 --- a/components/utilities/utest/utest_assert.h +++ b/components/utilities/utest/utest_assert.h @@ -43,8 +43,8 @@ void utest_assert_string(const char *a, const char *b, rt_bool_t equal, const ch */ #define uassert_true(value) __utest_assert(value, "(" #value ") is false") #define uassert_false(value) __utest_assert(!(value), "(" #value ") is true") -#define uassert_null(value) __utest_assert((value) == NULL, "(" #value ") is not null") -#define uassert_not_null(value) __utest_assert((value) != NULL, "(" #value ") is null") +#define uassert_null(value) __utest_assert((const char *)(value) == NULL, "(" #value ") is not null") +#define uassert_not_null(value) __utest_assert((const char *)(value) != NULL, "(" #value ") is null") #define uassert_int_equal(a, b) __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")") #define uassert_int_not_equal(a, b) __utest_assert((a) != (b), "(" #a ") equal to (" #b ")") -- cgit v1.3.1 From bce7f85907a3ef233111ca6799228b1598017bdc Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Sat, 8 Dec 2018 15:00:06 +0800 Subject: [components/utilities/utest] 移除不那么必要的日志接口 redefine MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MurphyZhao --- components/utilities/utest/utest.h | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'components') diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index fc01852b6a..5a7e8af721 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -80,22 +80,6 @@ typedef struct utest_tc_export *utest_tc_export_t; */ typedef void (*test_unit_func)(void); -/** - * TC_LOG_x - * - * @brief Log output interface used in test cases. - * - * @type TC_LOG_I Output info level log. - * @type TC_LOG_D Output debug level log. - * @type TC_LOG_E Output error level log. - * @type TC_LOG_W Output warning level log. - * -*/ -#define TC_LOG_I(...) LOG_I(__VA_ARGS__) -#define TC_LOG_D(...) LOG_D(__VA_ARGS__) -#define TC_LOG_E(...) LOG_E(__VA_ARGS__) -#define TC_LOG_W(...) LOG_W(__VA_ARGS__) - /** * utest_unit_run * -- cgit v1.3.1