Skip to content

Commit 26828a1

Browse files
mysterywolfRbb666
authored andcommitted
[utest] implement uassert_ptr_equal and uassert_ptr_not_equal
1 parent a992226 commit 26828a1

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

components/utilities/utest/utest.c

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111
#include <rtthread.h>
1212
#include <string.h>
1313
#include <stdlib.h>
14-
1514
#include "utest.h"
16-
#include <utest_log.h>
17-
18-
#undef DBG_TAG
19-
#undef DBG_LVL
15+
#include "utest_log.h"
2016

2117
#define DBG_TAG "utest"
2218
#ifdef UTEST_DEBUG
@@ -217,7 +213,7 @@ static void utest_do_run(const char *utest_name)
217213
{
218214
if (utest_name)
219215
{
220-
int len = strlen(utest_name);
216+
int len = rt_strlen(utest_name);
221217
if (utest_name[len - 1] == '*')
222218
{
223219
len -= 1;
@@ -397,13 +393,27 @@ void utest_unit_run(test_unit_func func, const char *unit_func_name)
397393
}
398394
}
399395

400-
void utest_assert(int value, const char *file, int line, const char *func, const char *msg)
396+
/*
397+
* utest_assert - assert function
398+
*
399+
* @param value - assert value
400+
* @param file - file name
401+
* @param line - line number
402+
* @param func - function name
403+
* @param msg - assert message
404+
*
405+
* @return - RT_TRUE: assert success; RT_FALSE: assert failed
406+
*/
407+
rt_bool_t utest_assert(int value, const char *file, int line, const char *func, const char *msg)
401408
{
409+
rt_bool_t rst = RT_FALSE;
410+
402411
if (!(value))
403412
{
404413
local_utest.error = UTEST_FAILED;
405414
local_utest.failed_num ++;
406415
LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
416+
rst = RT_FALSE;
407417
}
408418
else
409419
{
@@ -413,37 +423,49 @@ void utest_assert(int value, const char *file, int line, const char *func, const
413423
}
414424
local_utest.error = UTEST_PASSED;
415425
local_utest.passed_num ++;
426+
rst = RT_TRUE;
416427
}
428+
429+
return rst;
417430
}
418431

419432
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)
420433
{
434+
rt_bool_t rst = RT_FALSE;
435+
421436
if (a == RT_NULL || b == RT_NULL)
422437
{
423-
utest_assert(0, file, line, func, msg);
438+
rst = utest_assert(0, file, line, func, msg);
424439
}
425-
426-
if (equal)
440+
else
427441
{
428-
if (rt_strcmp(a, b) == 0)
442+
if (equal)
429443
{
430-
utest_assert(1, file, line, func, msg);
444+
if (rt_strcmp(a, b) == 0)
445+
{
446+
rst = utest_assert(1, file, line, func, msg);
447+
}
448+
else
449+
{
450+
rst = utest_assert(0, file, line, func, msg);
451+
}
431452
}
432453
else
433454
{
434-
utest_assert(0, file, line, func, msg);
455+
if (rt_strcmp(a, b) == 0)
456+
{
457+
rst = utest_assert(0, file, line, func, msg);
458+
}
459+
else
460+
{
461+
rst = utest_assert(1, file, line, func, msg);
462+
}
435463
}
436464
}
437-
else
465+
466+
if (!rst)
438467
{
439-
if (rt_strcmp(a, b) == 0)
440-
{
441-
utest_assert(0, file, line, func, msg);
442-
}
443-
else
444-
{
445-
utest_assert(1, file, line, func, msg);
446-
}
468+
LOG_E("[ ASSERT ] [ unit ] str-a: (%s); str-b: (%s)", a, b);
447469
}
448470
}
449471

components/utilities/utest/utest_assert.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern "C" {
1919
#endif
2020

2121
/* No need for the user to use this function directly */
22-
void utest_assert(int value, const char *file, int line, const char *func, const char *msg);
22+
rt_bool_t utest_assert(int value, const char *file, int line, const char *func, const char *msg);
2323

2424
/* No need for the user to use this function directly */
2525
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
5656
#define uassert_int_equal(a, b) __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")")
5757
#define uassert_int_not_equal(a, b) __utest_assert((a) != (b), "(" #a ") equal to (" #b ")")
5858

59+
#define uassert_ptr_equal(a, b) __utest_assert((const void*)(a) == (const void*)(b), "(" #a ") not equal to (" #b ")")
60+
#define uassert_ptr_not_equal(a, b) __utest_assert((const void*)(a) != (const void*)(b), "(" #a ") equal to (" #b ")")
61+
5962
#define uassert_str_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_TRUE, __FILE__, __LINE__, __func__, "string not equal")
6063
#define uassert_str_not_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal")
6164

components/utilities/utest/utest_log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include <rtthread.h>
1515

16-
#define UTEST_DEBUG
16+
// #define UTEST_DEBUG
1717

1818
#undef DBG_TAG
1919
#undef DBG_LVL

0 commit comments

Comments
 (0)