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