| 1 | //===-- Unittests for wcschr ----------------------------------------------===// |
| 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/wcschr.h" |
| 11 | #include "test/UnitTest/Test.h" |
| 12 | |
| 13 | TEST(LlvmLibcWCSChrTest, 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::wcschr(src, L'a'), src); |
| 17 | } |
| 18 | |
| 19 | TEST(LlvmLibcWCSChrTest, FindsMiddleCharacter) { |
| 20 | // Should return pointer to 'c'. |
| 21 | const wchar_t *src = L"abcde" ; |
| 22 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'c'), (src + 2)); |
| 23 | } |
| 24 | |
| 25 | TEST(LlvmLibcWCSChrTest, FindsLastCharacterThatIsNotNullTerminator) { |
| 26 | // Should return pointer to 'e'. |
| 27 | const wchar_t *src = L"abcde" ; |
| 28 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'e'), (src + 4)); |
| 29 | } |
| 30 | |
| 31 | TEST(LlvmLibcWCSChrTest, FindsNullTerminator) { |
| 32 | // Should return pointer to null terminator. |
| 33 | const wchar_t *src = L"abcde" ; |
| 34 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'\0'), (src + 5)); |
| 35 | } |
| 36 | |
| 37 | TEST(LlvmLibcWCSChrTest, CharacterNotWithinStringShouldReturnNullptr) { |
| 38 | // Since 'z' is not within the string, should return nullptr. |
| 39 | const wchar_t *src = L"abcde" ; |
| 40 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'z'), nullptr); |
| 41 | } |
| 42 | |
| 43 | TEST(LlvmLibcWCSChrTest, ShouldFindFirstOfDuplicates) { |
| 44 | // Should return pointer to the first '1'. |
| 45 | const wchar_t *src = L"abc1def1ghi" ; |
| 46 | ASSERT_EQ((int)(LIBC_NAMESPACE::wcschr(src, L'1') - src), 3); |
| 47 | |
| 48 | // Should return original string since 'X' is the first character. |
| 49 | const wchar_t *dups = L"XXXXX" ; |
| 50 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(dups, L'X'), dups); |
| 51 | } |
| 52 | |
| 53 | TEST(LlvmLibcWCSChrTest, EmptyStringShouldOnlyMatchNullTerminator) { |
| 54 | // Null terminator should match |
| 55 | const wchar_t *src = L"" ; |
| 56 | ASSERT_EQ(src, LIBC_NAMESPACE::wcschr(src, L'\0')); |
| 57 | // All other characters should not match |
| 58 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'Z'), nullptr); |
| 59 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'3'), nullptr); |
| 60 | ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'*'), nullptr); |
| 61 | } |
| 62 | |