1 | //===-- Unittests for wcsrchr ---------------------------------------------===// |
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 "hdr/types/wchar_t.h" |
10 | #include "src/wchar/wcsrchr.h" |
11 | #include "test/UnitTest/Test.h" |
12 | |
13 | TEST(LlvmLibcWCSRChrTest, FindsFirstCharacter) { |
14 | // Should return pointer to original string since 'a' is the first character. |
15 | const wchar_t *src = L"abcde" ; |
16 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'a'), src); |
17 | } |
18 | |
19 | TEST(LlvmLibcWCSRChrTest, FindsMiddleCharacter) { |
20 | // Should return pointer to 'c'. |
21 | const wchar_t *src = L"abcde" ; |
22 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'c'), (src + 2)); |
23 | } |
24 | |
25 | TEST(LlvmLibcWCSRChrTest, FindsLastCharacterThatIsNotNullTerminator) { |
26 | // Should return pointer to 'e'. |
27 | const wchar_t *src = L"abcde" ; |
28 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'e'), (src + 4)); |
29 | } |
30 | |
31 | TEST(LlvmLibcWCSRChrTest, FindsNullTerminator) { |
32 | // Should return pointer to null terminator. |
33 | const wchar_t *src = L"abcde" ; |
34 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'\0'), (src + 5)); |
35 | } |
36 | |
37 | TEST(LlvmLibcWCSRChrTest, CharacterNotWithinStringShouldReturnNullptr) { |
38 | // Since 'z' is not within the string, should return nullptr. |
39 | const wchar_t *src = L"abcde" ; |
40 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'z'), nullptr); |
41 | } |
42 | |
43 | TEST(LlvmLibcWCSRChrTest, ShouldFindLastOfDuplicates) { |
44 | // Should return pointer to the last '1'. |
45 | const wchar_t *src = L"abc1def1ghi" ; |
46 | ASSERT_EQ((int)(LIBC_NAMESPACE::wcsrchr(src, L'1') - src), 7); |
47 | |
48 | // Should return pointer to the last 'X' |
49 | const wchar_t *dups = L"XXXXX" ; |
50 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(dups, L'X'), dups + 4); |
51 | } |
52 | |
53 | TEST(LlvmLibcWCSRChrTest, EmptyStringShouldOnlyMatchNullTerminator) { |
54 | // Null terminator should match |
55 | const wchar_t *src = L"" ; |
56 | ASSERT_EQ(src, LIBC_NAMESPACE::wcsrchr(src, L'\0')); |
57 | // All other characters should not match |
58 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'Z'), nullptr); |
59 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'3'), nullptr); |
60 | ASSERT_EQ(LIBC_NAMESPACE::wcsrchr(src, L'*'), nullptr); |
61 | } |
62 | |
63 | #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
64 | TEST(LlvmLibcWCSRChrTest, NullptrCrash) { |
65 | // Passing in a nullptr should crash the program. |
66 | EXPECT_DEATH([] { LIBC_NAMESPACE::wcsrchr(nullptr, L'a'); }, WITH_SIGNAL(-1)); |
67 | } |
68 | #endif // LIBC_HAS_ADDRESS_SANITIZER |
69 | |