Skip to content

Commit b63d7a3

Browse files
committed
[utest] implement uassert_ptr_equal and uassert_ptr_not_equal
1 parent eabe76a commit b63d7a3

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

components/utilities/utest/utest.c

Lines changed: 42 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,47 @@ 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
}
417428
}
418429

419430
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)
420431
{
432+
rt_bool_t rst = RT_FALSE;
433+
421434
if (a == RT_NULL || b == RT_NULL)
422435
{
423-
utest_assert(0, file, line, func, msg);
436+
rst = utest_assert(0, file, line, func, msg);
424437
}
425-
426-
if (equal)
438+
else
427439
{
428-
if (rt_strcmp(a, b) == 0)
440+
if (equal)
429441
{
430-
utest_assert(1, file, line, func, msg);
442+
if (rt_strcmp(a, b) == 0)
443+
{
444+
rst = utest_assert(1, file, line, func, msg);
445+
}
446+
else
447+
{
448+
rst = utest_assert(0, file, line, func, msg);
449+
}
431450
}
432451
else
433452
{
434-
utest_assert(0, file, line, func, msg);
453+
if (rt_strcmp(a, b) == 0)
454+
{
455+
rst = utest_assert(0, file, line, func, msg);
456+
}
457+
else
458+
{
459+
rst = utest_assert(1, file, line, func, msg);
460+
}
435461
}
436462
}
437-
else
463+
464+
if (!rst)
438465
{
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-
}
466+
LOG_E("[ ASSERT ] [ unit ] str-a: (%s); str-b: (%s)", a, b);
447467
}
448468
}
449469

components/utilities/utest/utest.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#define __UTEST_H__
1313

1414
#include <rtthread.h>
15-
#include "utest_log.h"
1615
#include "utest_assert.h"
1716

1817
#ifdef __cplusplus

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)