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 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
utest_tc_init(void)19 static rt_err_t utest_tc_init(void)
20 {
21 buf = rt_malloc(TEST_BUF_SIZE * sizeof(char)); /**< Allocate memory for the buffer */
22 uassert_not_null(buf);
23 return RT_EOK;
24 }
25
utest_tc_cleanup(void)26 static rt_err_t utest_tc_cleanup(void)
27 {
28 rt_free(buf);
29 return RT_EOK;
30 }
31
32 /**
33 * Test memory copy with alignment.
34 * @param dalign The alignment offset for the destination buffer.
35 * @param salign The alignment offset for the source buffer.
36 * @param len The length of data to copy.
37 */
test_align(unsigned dalign,unsigned salign,size_t len)38 static void test_align(unsigned dalign, unsigned salign, size_t len)
39 {
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 */
44 unsigned i;
45
46 /** Assert that the source alignment offset plus length does not exceed N */
47 uassert_false(salign + len > N);
48 /** Assert that the destination alignment offset plus length does not exceed N */
49 uassert_false(dalign + len > N);
50
51 /** Initialize all buffers with '#' or ' ' */
52 for(i = 0; i < N; i++)
53 {
54 src[i] = '#';
55 dst[i] = want[i] = ' ';
56 }
57
58 /** Set data in the specified alignment offsets of the source and expected result buffers */
59 for(i = 0; i < len; i++)
60 {
61 src[salign + i] = want[dalign + i] = (char)('0' + i);
62 }
63
64 /** Call rt_memcpy to copy data */
65 p = rt_memcpy(dst + dalign, src + salign, len);
66
67 /** Assert that the return value of rt_memcpy is the pointer to the start of the copied data in the destination buffer */
68 uassert_ptr_equal(p, dst + dalign);
69
70 /** Assert that the content of the destination buffer matches the expected result buffer */
71 for(i = 0; i < N; i++)
72 {
73 uassert_int_equal(dst[i], want[i]);
74 }
75 }
76
77 /**
78 * Test case to iterate over all possible alignment offsets and length combinations.
79 */
TC_rt_memcpy_align(void)80 static void TC_rt_memcpy_align(void)
81 {
82 for(unsigned i = 0; i < 16; i++) /**< Iterate over source alignment offsets from 0 to 15 */
83 {
84 for(unsigned j = 0; j < 16; j++) /**< Iterate over destination alignment offsets from 0 to 15 */
85 {
86 for(size_t k = 0; k < 64; k++) /**< Iterate over data lengths from 0 to 63 */
87 {
88 test_align(i, j, k); /**< Call the test_align function */
89 }
90 }
91 }
92 }
93
TC_rt_memcpy_str(void)94 static void TC_rt_memcpy_str(void)
95 {
96 const char src[] = "Hello, memcpy!";
97 char dest[20] = {0};
98 rt_memcpy(dest, src, sizeof(src));
99 uassert_true(rt_strcmp(src, dest) == 0);
100 }
101
utest_do_tc(void)102 static void utest_do_tc(void)
103 {
104 UTEST_UNIT_RUN(TC_rt_memcpy_str);
105 UTEST_UNIT_RUN(TC_rt_memcpy_align);
106 }
107
108 UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcpy", utest_tc_init, utest_tc_cleanup, 1000);
109