|
| 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 | +static void TC_rt_memcmp_str(void) |
| 26 | +{ |
| 27 | + const char* s = "abc 123"; |
| 28 | + |
| 29 | + uassert_int_equal(rt_memcmp("abc", "abc", 4), 0); |
| 30 | + uassert_int_equal(rt_memcmp(s, "abc", 3), 0); |
| 31 | + uassert_int_equal(rt_memcmp("abc", s, 3), 0); |
| 32 | + |
| 33 | + /* The following tests intentionally use a length > 3 */ |
| 34 | + /* To test what rt_memcmp does in such a situation */ |
| 35 | + uassert_int_equal(!!(rt_memcmp(s, "abc", 6) > 0), 1); |
| 36 | + uassert_int_equal(!!(rt_memcmp("abc", s, 6) < 0), 1); |
| 37 | + |
| 38 | + /* Check RT_NULL input handling */ |
| 39 | + uassert_int_not_equal(rt_memcmp("abc", RT_NULL, 3), 0); |
| 40 | + uassert_int_not_equal(rt_memcmp(RT_NULL, "abc", 3), 0); |
| 41 | + |
| 42 | + /* Check that two RT_NULL strings will match */ |
| 43 | + uassert_int_equal(rt_memcmp(RT_NULL, RT_NULL, 0), 0); |
| 44 | +} |
| 45 | + |
| 46 | +static void utest_do_tc(void) |
| 47 | +{ |
| 48 | + UTEST_UNIT_RUN(TC_rt_memcmp_str); |
| 49 | +} |
| 50 | + |
| 51 | +UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcmp", utest_tc_init, utest_tc_cleanup, 1000); |
0 commit comments