[utest] implement uassert_ptr_equal and uassert_ptr_not_equal

This commit is contained in:
Meco Man
2024-12-23 17:44:14 -05:00
committed by Rbb666
parent a992226863
commit 26828a175e
3 changed files with 50 additions and 25 deletions
+35 -13
View File
@@ -11,12 +11,8 @@
#include <rtthread.h> #include <rtthread.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "utest.h" #include "utest.h"
#include <utest_log.h> #include "utest_log.h"
#undef DBG_TAG
#undef DBG_LVL
#define DBG_TAG "utest" #define DBG_TAG "utest"
#ifdef UTEST_DEBUG #ifdef UTEST_DEBUG
@@ -217,7 +213,7 @@ static void utest_do_run(const char *utest_name)
{ {
if (utest_name) if (utest_name)
{ {
int len = strlen(utest_name); int len = rt_strlen(utest_name);
if (utest_name[len - 1] == '*') if (utest_name[len - 1] == '*')
{ {
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)) if (!(value))
{ {
local_utest.error = UTEST_FAILED; local_utest.error = UTEST_FAILED;
local_utest.failed_num ++; local_utest.failed_num ++;
LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg); LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
rst = RT_FALSE;
} }
else else
{ {
@@ -413,38 +423,50 @@ void utest_assert(int value, const char *file, int line, const char *func, const
} }
local_utest.error = UTEST_PASSED; local_utest.error = UTEST_PASSED;
local_utest.passed_num ++; 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) 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) if (a == RT_NULL || b == RT_NULL)
{ {
utest_assert(0, file, line, func, msg); rst = utest_assert(0, file, line, func, msg);
} }
else
{
if (equal) if (equal)
{ {
if (rt_strcmp(a, b) == 0) if (rt_strcmp(a, b) == 0)
{ {
utest_assert(1, file, line, func, msg); rst = utest_assert(1, file, line, func, msg);
} }
else else
{ {
utest_assert(0, file, line, func, msg); rst = utest_assert(0, file, line, func, msg);
} }
} }
else else
{ {
if (rt_strcmp(a, b) == 0) if (rt_strcmp(a, b) == 0)
{ {
utest_assert(0, file, line, func, msg); rst = utest_assert(0, file, line, func, msg);
} }
else else
{ {
utest_assert(1, file, line, func, msg); rst = utest_assert(1, file, line, func, msg);
} }
} }
}
if (!rst)
{
LOG_E("[ ASSERT ] [ unit ] str-a: (%s); str-b: (%s)", a, b);
}
} }
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) 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)
+4 -1
View File
@@ -19,7 +19,7 @@ extern "C" {
#endif #endif
/* No need for the user to use this function directly */ /* 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 */ /* 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); 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_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_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_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_str_not_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal")
+1 -1
View File
@@ -13,7 +13,7 @@
#include <rtthread.h> #include <rtthread.h>
#define UTEST_DEBUG // #define UTEST_DEBUG
#undef DBG_TAG #undef DBG_TAG
#undef DBG_LVL #undef DBG_LVL