1 | //===-- Unittests for strxfrm ---------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/string/strxfrm.h" |
10 | #include "test/UnitTest/Test.h" |
11 | |
12 | #include "src/string/string_utils.h" |
13 | |
14 | // TODO: Add more comprehensive tests once locale support is added. |
15 | |
16 | TEST(LlvmLibcStrxfrmTest, SimpleTestSufficientlySizedN) { |
17 | const char *src = "abc" ; |
18 | const size_t n = 5; |
19 | |
20 | char dest[n]; |
21 | size_t result = LIBC_NAMESPACE::strxfrm(dest, src, n); |
22 | ASSERT_EQ(result, LIBC_NAMESPACE::internal::string_length(src)); |
23 | ASSERT_STREQ(dest, src); |
24 | } |
25 | |
26 | TEST(LlvmLibcStrxfrmTest, SimpleTestExactSizedN) { |
27 | const char *src = "abc" ; |
28 | const size_t n = 4; |
29 | |
30 | char dest[n]; |
31 | size_t result = LIBC_NAMESPACE::strxfrm(dest, src, n); |
32 | ASSERT_EQ(result, LIBC_NAMESPACE::internal::string_length(src)); |
33 | ASSERT_STREQ(dest, src); |
34 | } |
35 | |
36 | TEST(LlvmLibcStrxfrmTest, SimpleTestInsufficientlySizedN) { |
37 | const char *src = "abc" ; |
38 | const size_t n = 3; |
39 | |
40 | // Verify strxfrm does not modify dest if src len >= n |
41 | char dest[n] = {'x', 'x', '\0'}; |
42 | size_t result = LIBC_NAMESPACE::strxfrm(dest, src, n); |
43 | ASSERT_GE(result, n); |
44 | ASSERT_STREQ(dest, "xx" ); |
45 | } |
46 | |