[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / string / strchrnul_test.cpp
blob273cf40925d74529cb3f36afd3f78bd503999aea
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 //===----------------------------------------------------------------------===//
9 #include "src/string/strchrnul.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcStrChrNulTest, FindsFirstCharacter) {
13 const char *src = "abcde";
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");
21 TEST(LlvmLibcStrChrNulTest, FindsMiddleCharacter) {
22 const char *src = "abcde";
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");
30 TEST(LlvmLibcStrChrNulTest, FindsLastCharacterThatIsNotNullTerminator) {
31 const char *src = "abcde";
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");
39 TEST(LlvmLibcStrChrNulTest, FindsNullTerminator) {
40 const char *src = "abcde";
42 // Should return null terminator.
43 ASSERT_STREQ(LIBC_NAMESPACE::strchrnul(src, '\0'), "");
44 // Source string should not change.
45 ASSERT_STREQ(src, "abcde");
48 TEST(LlvmLibcStrChrNulTest,
49 CharacterNotWithinStringShouldReturnNullTerminator) {
50 const char *src = "123?";
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');
57 char *term = const_cast<char *>(src) + 4;
58 ASSERT_EQ(result, term);
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");
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");
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);
83 TEST(LlvmLibcStrChrNulTest, EmptyStringShouldOnlyMatchNullTerminator) {
84 // Null terminator should match.
85 ASSERT_STREQ(LIBC_NAMESPACE::strchrnul("", '\0'), "");
87 // All other characters should not match.
88 char *result = LIBC_NAMESPACE::strchrnul("", 'Z');
89 ASSERT_EQ(*result, '\0');
91 result = LIBC_NAMESPACE::strchrnul("", '3');
92 ASSERT_EQ(*result, '\0');
94 result = LIBC_NAMESPACE::strchrnul("", '*');
95 ASSERT_EQ(*result, '\0');