| 1 | //===-- Unittests for strchrnul -------------------------------------------===// |
| 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/strchrnul.h" |
| 10 | #include "test/UnitTest/Test.h" |
| 11 | |
| 12 | TEST(LlvmLibcStrChrNulTest, FindsFirstCharacter) { |
| 13 | const char *src = "abcde" ; |
| 14 | |
| 15 | // Should return original string since 'a' is the first character. |
| 16 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul(src, 'a'), "abcde" ); |
| 17 | // Source string should not change. |
| 18 | ASSERT_STREQ(src, "abcde" ); |
| 19 | } |
| 20 | |
| 21 | TEST(LlvmLibcStrChrNulTest, FindsMiddleCharacter) { |
| 22 | const char *src = "abcde" ; |
| 23 | |
| 24 | // Should return characters after (and including) 'c'. |
| 25 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul(src, 'c'), "cde" ); |
| 26 | // Source string should not change. |
| 27 | ASSERT_STREQ(src, "abcde" ); |
| 28 | } |
| 29 | |
| 30 | TEST(LlvmLibcStrChrNulTest, FindsLastCharacterThatIsNotNullTerminator) { |
| 31 | const char *src = "abcde" ; |
| 32 | |
| 33 | // Should return 'e' and null-terminator. |
| 34 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul(src, 'e'), "e" ); |
| 35 | // Source string should not change. |
| 36 | ASSERT_STREQ(src, "abcde" ); |
| 37 | } |
| 38 | |
| 39 | TEST(LlvmLibcStrChrNulTest, FindsNullTerminator) { |
| 40 | const char *src = "abcde" ; |
| 41 | |
| 42 | // Should return null terminator. |
| 43 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul(src, '\0'), "" ); |
| 44 | // Source string should not change. |
| 45 | ASSERT_STREQ(src, "abcde" ); |
| 46 | } |
| 47 | |
| 48 | TEST(LlvmLibcStrChrNulTest, |
| 49 | CharacterNotWithinStringShouldReturnNullTerminator) { |
| 50 | const char *src = "123?" ; |
| 51 | |
| 52 | // Since 'z' is not within the string, should return a pointer to the source |
| 53 | // string's null terminator. |
| 54 | char *result = LIBC_NAMESPACE::strchrnul(src, 'z'); |
| 55 | ASSERT_EQ(*result, '\0'); |
| 56 | |
| 57 | char *term = const_cast<char *>(src) + 4; |
| 58 | ASSERT_EQ(result, term); |
| 59 | } |
| 60 | |
| 61 | TEST(LlvmLibcStrChrNulTest, TheSourceShouldNotChange) { |
| 62 | const char *src = "abcde" ; |
| 63 | // When the character is found, the source string should not change. |
| 64 | LIBC_NAMESPACE::strchrnul(src, 'd'); |
| 65 | ASSERT_STREQ(src, "abcde" ); |
| 66 | // Same case for when the character is not found. |
| 67 | LIBC_NAMESPACE::strchrnul(src, 'z'); |
| 68 | ASSERT_STREQ(src, "abcde" ); |
| 69 | // Same case for when looking for null terminator. |
| 70 | LIBC_NAMESPACE::strchrnul(src, '\0'); |
| 71 | ASSERT_STREQ(src, "abcde" ); |
| 72 | } |
| 73 | |
| 74 | TEST(LlvmLibcStrChrNulTest, ShouldFindFirstOfDuplicates) { |
| 75 | // '1' is duplicated in the string, but it should find the first copy. |
| 76 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul("abc1def1ghi" , '1'), "1def1ghi" ); |
| 77 | |
| 78 | const char *dups = "XXXXX" ; |
| 79 | // Should return original string since 'X' is the first character. |
| 80 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul(dups, 'X'), dups); |
| 81 | } |
| 82 | |
| 83 | TEST(LlvmLibcStrChrNulTest, EmptyStringShouldOnlyMatchNullTerminator) { |
| 84 | // Null terminator should match. |
| 85 | ASSERT_STREQ(LIBC_NAMESPACE::strchrnul("" , '\0'), "" ); |
| 86 | |
| 87 | // All other characters should not match. |
| 88 | char *result = LIBC_NAMESPACE::strchrnul("" , 'Z'); |
| 89 | ASSERT_EQ(*result, '\0'); |
| 90 | |
| 91 | result = LIBC_NAMESPACE::strchrnul("" , '3'); |
| 92 | ASSERT_EQ(*result, '\0'); |
| 93 | |
| 94 | result = LIBC_NAMESPACE::strchrnul("" , '*'); |
| 95 | ASSERT_EQ(*result, '\0'); |
| 96 | } |
| 97 | |