1 //===-- Unittests for strrchr ---------------------------------------------===//
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/strrchr.h"
10 #include "utils/UnitTest/Test.h"
11 
TEST(StrRChrTest,FindsFirstCharacter)12 TEST(StrRChrTest, FindsFirstCharacter) {
13   const char *src = "abcde";
14 
15   // Should return original string since 'a' is the first character.
16   ASSERT_STREQ(__llvm_libc::strrchr(src, 'a'), "abcde");
17   // Source string should not change.
18   ASSERT_STREQ(src, "abcde");
19 }
20 
TEST(StrRChrTest,FindsMiddleCharacter)21 TEST(StrRChrTest, FindsMiddleCharacter) {
22   const char *src = "abcde";
23 
24   // Should return characters after (and including) 'c'.
25   ASSERT_STREQ(__llvm_libc::strrchr(src, 'c'), "cde");
26   // Source string should not change.
27   ASSERT_STREQ(src, "abcde");
28 }
29 
TEST(StrRChrTest,FindsLastCharacterThatIsNotNullTerminator)30 TEST(StrRChrTest, FindsLastCharacterThatIsNotNullTerminator) {
31   const char *src = "abcde";
32 
33   // Should return 'e' and null-terminator.
34   ASSERT_STREQ(__llvm_libc::strrchr(src, 'e'), "e");
35   // Source string should not change.
36   ASSERT_STREQ(src, "abcde");
37 }
38 
TEST(StrRChrTest,FindsNullTerminator)39 TEST(StrRChrTest, FindsNullTerminator) {
40   const char *src = "abcde";
41 
42   // Should return null terminator.
43   ASSERT_STREQ(__llvm_libc::strrchr(src, '\0'), "");
44   // Source string should not change.
45   ASSERT_STREQ(src, "abcde");
46 }
47 
TEST(StrRChrTest,FindsLastBehindFirstNullTerminator)48 TEST(StrRChrTest, FindsLastBehindFirstNullTerminator) {
49   const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
50   // 'b' is behind a null terminator, so should not be found.
51   ASSERT_STREQ(__llvm_libc::strrchr(src, 'b'), nullptr);
52   // Same goes for 'c'.
53   ASSERT_STREQ(__llvm_libc::strrchr(src, 'c'), nullptr);
54 
55   // Should find the second of the two a's.
56   ASSERT_STREQ(__llvm_libc::strrchr(src, 'a'), "a");
57 }
58 
TEST(StrRChrTest,CharacterNotWithinStringShouldReturnNullptr)59 TEST(StrRChrTest, CharacterNotWithinStringShouldReturnNullptr) {
60   // Since 'z' is not within the string, should return nullptr.
61   ASSERT_STREQ(__llvm_libc::strrchr("123?", 'z'), nullptr);
62 }
63 
TEST(StrRChrTest,ShouldFindLastOfDuplicates)64 TEST(StrRChrTest, ShouldFindLastOfDuplicates) {
65   // '1' is duplicated in the string, but it should find the last copy.
66   ASSERT_STREQ(__llvm_libc::strrchr("abc1def1ghi", '1'), "1ghi");
67 
68   const char *dups = "XXXXX";
69   // Should return the last occurrence of 'X'.
70   ASSERT_STREQ(__llvm_libc::strrchr(dups, 'X'), "X");
71 }
72 
TEST(StrRChrTest,EmptyStringShouldOnlyMatchNullTerminator)73 TEST(StrRChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
74   // Null terminator should match.
75   ASSERT_STREQ(__llvm_libc::strrchr("", '\0'), "");
76   // All other characters should not match.
77   ASSERT_STREQ(__llvm_libc::strrchr("", 'A'), nullptr);
78   ASSERT_STREQ(__llvm_libc::strrchr("", '2'), nullptr);
79   ASSERT_STREQ(__llvm_libc::strrchr("", '*'), nullptr);
80 }
81