Skip to content

[utest] implement rt_memcmp test cases #9823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions components/libc/compilers/common/cwchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ int wcwidth(wchar_t ucs)
(ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
(ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
(ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
(ucs >= 0xffe0 && ucs <= 0xffe6) // ||
//#ifndef _WIN32
// (ucs >= 0x20000 && ucs <= 0x2ffff)
//#else
// 0
//#endif
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
#ifndef _WIN32
(ucs >= 0x20000 && ucs <= 0x2ffff)
#else
0
#endif
));
}

Expand Down
66 changes: 44 additions & 22 deletions components/utilities/utest/utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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);
}
}

Expand Down
5 changes: 4 additions & 1 deletion components/utilities/utest/utest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion components/utilities/utest/utest_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <rtthread.h>

#define UTEST_DEBUG
// #define UTEST_DEBUG

#undef DBG_TAG
#undef DBG_LVL
Expand Down
37 changes: 0 additions & 37 deletions src/klibc/utest/TC_kstdlib.c

This file was deleted.

51 changes: 51 additions & 0 deletions src/klibc/utest/TC_rt_memcmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-05-06 Phillip Johnston the first version
* 2024-12-24 Meco Man port to utest
*/

#include <rtklibc.h>
#include <utest.h>

static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}

static void TC_rt_memcmp_str(void)
{
const char* s = "abc 123";

uassert_int_equal(rt_memcmp("abc", "abc", 4), 0);
uassert_int_equal(rt_memcmp(s, "abc", 3), 0);
uassert_int_equal(rt_memcmp("abc", s, 3), 0);

/* The following tests intentionally use a length > 3 */
/* To test what rt_memcmp does in such a situation */
uassert_int_equal(!!(rt_memcmp(s, "abc", 6) > 0), 1);
uassert_int_equal(!!(rt_memcmp("abc", s, 6) < 0), 1);

/* Check RT_NULL input handling */
uassert_int_not_equal(rt_memcmp("abc", RT_NULL, 3), 0);
uassert_int_not_equal(rt_memcmp(RT_NULL, "abc", 3), 0);

/* Check that two RT_NULL strings will match */
uassert_int_equal(rt_memcmp(RT_NULL, RT_NULL, 0), 0);
}

static void utest_do_tc(void)
{
UTEST_UNIT_RUN(TC_rt_memcmp_str);
}

UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcmp", utest_tc_init, utest_tc_cleanup, 1000);
114 changes: 114 additions & 0 deletions src/klibc/utest/TC_rt_memcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-05-06 Phillip Johnston the first version
* 2024-12-24 Meco Man port to utest
*/

#include <rtklibc.h>
#include <utest.h>

static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}

#define N 80 /**< Define the constant N for buffer size as 80 */
static char buf[512] = {0}; /**< Define a static buffer of 512 bytes, initialized to 0 */

/**
* Align a given pointer to a 64-byte boundary.
* @param p The pointer to align.
* @return The aligned pointer.
*/
static void* aligned(void* p)
{
return (void*)(((intptr_t)p + 63) & -64);
}

/**
* Test memory copy with alignment.
* @param dalign The alignment offset for the destination buffer.
* @param salign The alignment offset for the source buffer.
* @param len The length of data to copy.
*/
static void test_align(unsigned dalign, unsigned salign, size_t len)
{
char* src = aligned(buf); /**< Source buffer starting address, 64-byte aligned */
char* dst = aligned(buf + 128); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
char* want = aligned(buf + 256); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
char* p; /**< Pointer to receive the return value of rt_memcpy */
unsigned i;

/** Assert that the source alignment offset plus length does not exceed N */
uassert_false(salign + len > N);
/** Assert that the destination alignment offset plus length does not exceed N */
uassert_false(dalign + len > N);

/** Initialize all buffers with '#' or ' ' */
for(i = 0; i < N; i++)
{
src[i] = '#';
dst[i] = want[i] = ' ';
}

/** Set data in the specified alignment offsets of the source and expected result buffers */
for(i = 0; i < len; i++)
{
src[salign + i] = want[dalign + i] = (char)('0' + i);
}

/** Call rt_memcpy to copy data */
p = rt_memcpy(dst + dalign, src + salign, len);

/** Assert that the return value of rt_memcpy is the pointer to the start of the copied data in the destination buffer */
uassert_ptr_equal(p, dst + dalign);

/** Assert that the content of the destination buffer matches the expected result buffer */
for(i = 0; i < N; i++)
{
uassert_int_equal(dst[i], want[i]);
}
}

/**
* Test case to iterate over all possible alignment offsets and length combinations.
*/
static void TC_rt_memcpy_align(void)
{
for(unsigned i = 0; i < 16; i++) /**< Iterate over source alignment offsets from 0 to 15 */
{
for(unsigned j = 0; j < 16; j++) /**< Iterate over destination alignment offsets from 0 to 15 */
{
for(size_t k = 0; k < 64; k++) /**< Iterate over data lengths from 0 to 63 */
{
test_align(i, j, k); /**< Call the test_align function */
}
}
}
}

static void TC_rt_memcpy_str(void)
{
const char src[] = "Hello, memcpy!";
char dest[20] = {0};
rt_memcpy(dest, src, sizeof(src));
uassert_true(rt_strcmp(src, dest) == 0);
}

static void utest_do_tc(void)
{
UTEST_UNIT_RUN(TC_rt_memcpy_str);
UTEST_UNIT_RUN(TC_rt_memcpy_align);
}

UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcpy", utest_tc_init, utest_tc_cleanup, 1000);
6 changes: 1 addition & 5 deletions src/klibc/utest/TC_rt_sprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ extremal_unsigned_integer_values

#define base_buffer_size 100

#define SPRINTF_CHECK(expected, buffer, format, ...) \
#define SPRINTF_CHECK(expected, buffer, format, ...) \
do { \
rt_memset(buffer, 0xCC, base_buffer_size); \
rt_sprintf(buffer, format, ##__VA_ARGS__); \
if (rt_strcmp(buffer, expected) != 0) { \
rt_kprintf("Expected: %s\n", expected); \
rt_kprintf("Actual : %s\n", buffer); \
} \
uassert_str_equal(expected, buffer); \
} while (0)

Expand Down
Loading