diff options
| author | Meco Man <[email protected]> | 2024-12-23 17:44:14 -0500 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2024-12-24 13:42:04 +0800 |
| commit | 26828a175ed5ba4ecd383002497df212d3e2f5b1 (patch) | |
| tree | eca7c2092e3a47bf36687496d297e00d5efff083 | |
| parent | a992226863d5319c04f2e972a7cc28cdf3fa07db (diff) | |
[utest] implement uassert_ptr_equal and uassert_ptr_not_equal
| -rw-r--r-- | components/utilities/utest/utest.c | 66 | ||||
| -rw-r--r-- | components/utilities/utest/utest_assert.h | 5 | ||||
| -rw-r--r-- | components/utilities/utest/utest_log.h | 2 |
3 files changed, 49 insertions, 24 deletions
diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index d49eee1633..af51508ce5 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -11,12 +11,8 @@ #include <rtthread.h> #include <string.h> #include <stdlib.h> - #include "utest.h" -#include <utest_log.h> - -#undef DBG_TAG -#undef DBG_LVL +#include "utest_log.h" #define DBG_TAG "utest" #ifdef UTEST_DEBUG @@ -217,7 +213,7 @@ static void utest_do_run(const char *utest_name) { if (utest_name) { - int len = strlen(utest_name); + int len = rt_strlen(utest_name); if (utest_name[len - 1] == '*') { len -= 1; @@ -397,13 +393,27 @@ void utest_unit_run(test_unit_func func, const char *unit_func_name) } } -void utest_assert(int value, const char *file, int line, const char *func, const char *msg) +/* +* utest_assert - assert function +* +* @param value - assert value +* @param file - file name +* @param line - line number +* @param func - function name +* @param msg - assert message +* +* @return - RT_TRUE: assert success; RT_FALSE: assert failed +*/ +rt_bool_t utest_assert(int value, const char *file, int line, const char *func, const char *msg) { + rt_bool_t rst = RT_FALSE; + 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); + rst = RT_FALSE; } else { @@ -413,37 +423,49 @@ void utest_assert(int value, const char *file, int line, const char *func, const } local_utest.error = UTEST_PASSED; local_utest.passed_num ++; + rst = RT_TRUE; } + + return rst; } 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) { + rt_bool_t rst = RT_FALSE; + if (a == RT_NULL || b == RT_NULL) { - utest_assert(0, file, line, func, msg); + rst = utest_assert(0, file, line, func, msg); } - - if (equal) + else { - if (rt_strcmp(a, b) == 0) + if (equal) { - utest_assert(1, file, line, func, msg); + if (rt_strcmp(a, b) == 0) + { + rst = utest_assert(1, file, line, func, msg); + } + else + { + rst = utest_assert(0, file, line, func, msg); + } } else { - utest_assert(0, file, line, func, msg); + if (rt_strcmp(a, b) == 0) + { + rst = utest_assert(0, file, line, func, msg); + } + else + { + rst = utest_assert(1, file, line, func, msg); + } } } - else + + if (!rst) { - if (rt_strcmp(a, b) == 0) - { - utest_assert(0, file, line, func, msg); - } - else - { - utest_assert(1, file, line, func, msg); - } + LOG_E("[ ASSERT ] [ unit ] str-a: (%s); str-b: (%s)", a, b); } } diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h index ba76fc39ee..8edfd1b9ad 100644 --- a/components/utilities/utest/utest_assert.h +++ b/components/utilities/utest/utest_assert.h @@ -19,7 +19,7 @@ extern "C" { #endif /* 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); +rt_bool_t 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); @@ -56,6 +56,9 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa #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_ptr_equal(a, b) __utest_assert((const void*)(a) == (const void*)(b), "(" #a ") not equal to (" #b ")") +#define uassert_ptr_not_equal(a, b) __utest_assert((const void*)(a) != (const void*)(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") diff --git a/components/utilities/utest/utest_log.h b/components/utilities/utest/utest_log.h index c25815dc63..e39d0d9344 100644 --- a/components/utilities/utest/utest_log.h +++ b/components/utilities/utest/utest_log.h @@ -13,7 +13,7 @@ #include <rtthread.h> -#define UTEST_DEBUG +// #define UTEST_DEBUG #undef DBG_TAG #undef DBG_LVL |
