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