Skip to content

Commit 8b7eabe

Browse files
committed
[utest] add rt_memset test case
1 parent 8c762b4 commit 8b7eabe

File tree

4 files changed

+117
-20
lines changed

4 files changed

+117
-20
lines changed

components/utilities/utest/utest.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static void utest_do_run(const char *utest_name)
231231
{
232232
if (tc_table[i].init() != RT_EOK)
233233
{
234-
LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
234+
LOG_E("[ FAILED ] [ result ] testcase init (%s)", tc_table[i].name);
235235
goto __tc_continue;
236236
}
237237
}
@@ -259,7 +259,7 @@ static void utest_do_run(const char *utest_name)
259259
{
260260
if (tc_table[i].cleanup() != RT_EOK)
261261
{
262-
LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
262+
LOG_E("[ FAILED ] [ result ] testcase cleanup (%s)", tc_table[i].name);
263263
goto __tc_continue;
264264
}
265265
}

components/utilities/utest/utest_assert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa
5050
*/
5151
#define uassert_true(value) __utest_assert(value, "(" #value ") is false")
5252
#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")
53+
5354
#define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null")
5455
#define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null")
5556

src/klibc/utest/TC_rt_memcpy.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,26 @@
99
* 2024-12-24 Meco Man port to utest
1010
*/
1111

12-
#include <rtklibc.h>
12+
#include <rtthread.h>
1313
#include <utest.h>
1414

15+
#define N 80 /**< Define the constant N for buffer size as 80 */
16+
#define TEST_BUF_SIZE 512 /**< Define the constant TEST_BUF_SIZE as 512 */
17+
static char *buf; /**< Define a static buffer of 512 bytes, initialized to 0 */
18+
1519
static rt_err_t utest_tc_init(void)
1620
{
21+
buf = rt_malloc(TEST_BUF_SIZE * sizeof(char)); /**< Allocate memory for the buffer */
22+
uassert_not_null(buf);
1723
return RT_EOK;
1824
}
1925

2026
static rt_err_t utest_tc_cleanup(void)
2127
{
28+
rt_free(buf);
2229
return RT_EOK;
2330
}
2431

25-
#define N 80 /**< Define the constant N for buffer size as 80 */
26-
static char buf[512] = {0}; /**< Define a static buffer of 512 bytes, initialized to 0 */
27-
28-
/**
29-
* Align a given pointer to a 64-byte boundary.
30-
* @param p The pointer to align.
31-
* @return The aligned pointer.
32-
*/
33-
static void* aligned(void* p)
34-
{
35-
return (void*)(((intptr_t)p + 63) & -64);
36-
}
37-
3832
/**
3933
* Test memory copy with alignment.
4034
* @param dalign The alignment offset for the destination buffer.
@@ -43,10 +37,10 @@ static void* aligned(void* p)
4337
*/
4438
static void test_align(unsigned dalign, unsigned salign, size_t len)
4539
{
46-
char* src = aligned(buf); /**< Source buffer starting address, 64-byte aligned */
47-
char* dst = aligned(buf + 128); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
48-
char* want = aligned(buf + 256); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
49-
char* p; /**< Pointer to receive the return value of rt_memcpy */
40+
char *src = (char *)RT_ALIGN((rt_ubase_t)buf, 64); /**< Source buffer starting address, 64-byte aligned */
41+
char *dst = (char *)RT_ALIGN(((rt_ubase_t)buf + 128), 64); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
42+
char *want = (char *)RT_ALIGN(((rt_ubase_t)buf + 256), 64); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
43+
char *p; /**< Pointer to receive the return value of rt_memcpy */
5044
unsigned i;
5145

5246
/** Assert that the source alignment offset plus length does not exceed N */

src/klibc/utest/TC_rt_memset.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2020-05-06 Phillip Johnston the first version
9+
* 2024-12-24 Meco Man port to utest
10+
*/
11+
12+
#include <rtthread.h>
13+
#include <utest.h>
14+
15+
#define TEST_BUF_SIZE 400
16+
17+
static char *buf;
18+
static char *buf2;
19+
20+
static rt_err_t utest_tc_init(void)
21+
{
22+
buf = rt_malloc(TEST_BUF_SIZE * sizeof(char));
23+
uassert_not_null(buf);
24+
buf2 = rt_malloc(TEST_BUF_SIZE * sizeof(char));
25+
uassert_not_null(buf2);
26+
return RT_EOK;
27+
}
28+
29+
static rt_err_t utest_tc_cleanup(void)
30+
{
31+
rt_free(buf);
32+
rt_free(buf2);
33+
return RT_EOK;
34+
}
35+
36+
static void test_align(int align, size_t len)
37+
{
38+
char *s = (char *)RT_ALIGN(((rt_ubase_t)buf + 64), 64) + align;
39+
char *want = (char *)RT_ALIGN(((rt_ubase_t)buf2 + 64), 64) + align;
40+
char *p;
41+
int i;
42+
43+
uassert_false(len + 64 > (size_t)(buf + TEST_BUF_SIZE - s));
44+
uassert_false(len + 64 > (size_t)(buf2 + TEST_BUF_SIZE - want));
45+
46+
for(i = 0; i < TEST_BUF_SIZE; i++)
47+
{
48+
buf[i] = buf2[i] = ' ';
49+
}
50+
51+
for(i = 0; i < (int)len; i++)
52+
{
53+
want[i] = '#';
54+
}
55+
56+
p = rt_memset(s, '#', len);
57+
58+
uassert_ptr_equal(p, s);
59+
60+
for(i = -64; i < (int)len + 64; i++)
61+
{
62+
uassert_int_equal(s[i], want[i]);
63+
}
64+
}
65+
66+
static void TC_rt_memcpy_align(void)
67+
{
68+
for(int i = 0; i < 16; i++)
69+
{
70+
for(size_t j = 0; j < 200; j++)
71+
{
72+
test_align(i, j);
73+
}
74+
}
75+
}
76+
77+
static void test_input(char c)
78+
{
79+
rt_memset(buf, c, 10);
80+
for(int i = 0; i < 10; i++)
81+
{
82+
uassert_int_equal(buf[i], c);
83+
}
84+
}
85+
86+
static void TC_rt_memcpy_input(void)
87+
{
88+
test_input('c');
89+
test_input(0);
90+
test_input(-1);
91+
test_input(0xab);
92+
test_input((char)RT_UINT32_MAX);
93+
test_input((char)-RT_UINT32_MAX);
94+
}
95+
96+
static void utest_do_tc(void)
97+
{
98+
UTEST_UNIT_RUN(TC_rt_memcpy_align);
99+
UTEST_UNIT_RUN(TC_rt_memcpy_input);
100+
}
101+
102+
UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memset", utest_tc_init, utest_tc_cleanup, 1000);

0 commit comments

Comments
 (0)