1e8a2af28Scgyurgyik //===-- Unittests for strrchr ---------------------------------------------===//
2e8a2af28Scgyurgyik //
3e8a2af28Scgyurgyik // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8a2af28Scgyurgyik // See https://llvm.org/LICENSE.txt for license information.
5e8a2af28Scgyurgyik // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8a2af28Scgyurgyik //
7e8a2af28Scgyurgyik //===----------------------------------------------------------------------===//
8e8a2af28Scgyurgyik
9e8a2af28Scgyurgyik #include "src/string/strrchr.h"
10e8a2af28Scgyurgyik #include "utils/UnitTest/Test.h"
11e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,FindsFirstCharacter)12*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, FindsFirstCharacter) {
13e8a2af28Scgyurgyik const char *src = "abcde";
14e8a2af28Scgyurgyik
15e8a2af28Scgyurgyik // Should return original string since 'a' is the first character.
16e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, 'a'), "abcde");
17e8a2af28Scgyurgyik // Source string should not change.
1859547559Scgyurgyik ASSERT_STREQ(src, "abcde");
19e8a2af28Scgyurgyik }
20e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,FindsMiddleCharacter)21*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, FindsMiddleCharacter) {
22e8a2af28Scgyurgyik const char *src = "abcde";
23e8a2af28Scgyurgyik
24e8a2af28Scgyurgyik // Should return characters after (and including) 'c'.
25e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, 'c'), "cde");
26e8a2af28Scgyurgyik // Source string should not change.
2759547559Scgyurgyik ASSERT_STREQ(src, "abcde");
28e8a2af28Scgyurgyik }
29e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,FindsLastCharacterThatIsNotNullTerminator)30*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, FindsLastCharacterThatIsNotNullTerminator) {
31e8a2af28Scgyurgyik const char *src = "abcde";
32e8a2af28Scgyurgyik
33e8a2af28Scgyurgyik // Should return 'e' and null-terminator.
34e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, 'e'), "e");
35e8a2af28Scgyurgyik // Source string should not change.
3659547559Scgyurgyik ASSERT_STREQ(src, "abcde");
37e8a2af28Scgyurgyik }
38e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,FindsNullTerminator)39*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, FindsNullTerminator) {
40e8a2af28Scgyurgyik const char *src = "abcde";
41e8a2af28Scgyurgyik
42e8a2af28Scgyurgyik // Should return null terminator.
43e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, '\0'), "");
44e8a2af28Scgyurgyik // Source string should not change.
4559547559Scgyurgyik ASSERT_STREQ(src, "abcde");
46e8a2af28Scgyurgyik }
47e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,FindsLastBehindFirstNullTerminator)48*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, FindsLastBehindFirstNullTerminator) {
497212ad06SChris Gyurgyik const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
50e8a2af28Scgyurgyik // 'b' is behind a null terminator, so should not be found.
51e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, 'b'), nullptr);
52e8a2af28Scgyurgyik // Same goes for 'c'.
53e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, 'c'), nullptr);
547212ad06SChris Gyurgyik
557212ad06SChris Gyurgyik // Should find the second of the two a's.
567212ad06SChris Gyurgyik ASSERT_STREQ(__llvm_libc::strrchr(src, 'a'), "a");
57e8a2af28Scgyurgyik }
58e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,CharacterNotWithinStringShouldReturnNullptr)59*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, CharacterNotWithinStringShouldReturnNullptr) {
60e8a2af28Scgyurgyik // Since 'z' is not within the string, should return nullptr.
61e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr("123?", 'z'), nullptr);
62e8a2af28Scgyurgyik }
63e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,ShouldFindLastOfDuplicates)64*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, ShouldFindLastOfDuplicates) {
65e8a2af28Scgyurgyik // '1' is duplicated in the string, but it should find the last copy.
66e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr("abc1def1ghi", '1'), "1ghi");
67e8a2af28Scgyurgyik
68e8a2af28Scgyurgyik const char *dups = "XXXXX";
69e8a2af28Scgyurgyik // Should return the last occurrence of 'X'.
70e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr(dups, 'X'), "X");
71e8a2af28Scgyurgyik }
72e8a2af28Scgyurgyik
TEST(LlvmLibcStrRChrTest,EmptyStringShouldOnlyMatchNullTerminator)73*1df0dbfcSMichael Jones TEST(LlvmLibcStrRChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
74e8a2af28Scgyurgyik // Null terminator should match.
75e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr("", '\0'), "");
76e8a2af28Scgyurgyik // All other characters should not match.
77e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr("", 'A'), nullptr);
78e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr("", '2'), nullptr);
79e8a2af28Scgyurgyik ASSERT_STREQ(__llvm_libc::strrchr("", '*'), nullptr);
80e8a2af28Scgyurgyik }
81