1 //===-- Unittests for memrchr ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/string/memrchr.h"
10 #include "test/UnitTest/Test.h"
13 // A helper function that calls memrchr and abstracts away the explicit cast for
14 // readability purposes.
15 const char *call_memrchr(const void *src
, int c
, size_t size
) {
16 return reinterpret_cast<const char *>(LIBC_NAMESPACE::memrchr(src
, c
, size
));
19 TEST(LlvmLibcMemRChrTest
, FindsCharacterAfterNullTerminator
) {
20 // memrchr should continue searching after a null terminator.
21 const size_t size
= 6;
22 const unsigned char src
[size
] = {'a', '\0', 'b', 'c', 'd', '\0'};
23 // Should return 'b', 'c', 'd', '\0' even when after null terminator.
24 ASSERT_STREQ(call_memrchr(src
, 'b', size
), "bcd");
27 TEST(LlvmLibcMemRChrTest
, FindsCharacterInNonNullTerminatedCollection
) {
28 const size_t size
= 3;
29 const unsigned char src
[size
] = {'a', 'b', 'c'};
30 // Should return 'b', 'c'.
31 const char *ret
= call_memrchr(src
, 'b', size
);
32 ASSERT_EQ(ret
[0], 'b');
33 ASSERT_EQ(ret
[1], 'c');
36 TEST(LlvmLibcMemRChrTest
, FindsFirstCharacter
) {
37 const size_t size
= 6;
38 const unsigned char src
[size
] = {'a', 'b', 'c', 'd', 'e', '\0'};
39 // Should return original array since 'a' is the first character.
40 ASSERT_STREQ(call_memrchr(src
, 'a', size
), "abcde");
43 TEST(LlvmLibcMemRChrTest
, FindsMiddleCharacter
) {
44 const size_t size
= 6;
45 const unsigned char src
[size
] = {'a', 'b', 'c', 'd', 'e', '\0'};
46 // Should return characters after (and including) 'c'.
47 ASSERT_STREQ(call_memrchr(src
, 'c', size
), "cde");
50 TEST(LlvmLibcMemRChrTest
, FindsLastCharacterThatIsNotNullTerminator
) {
51 const size_t size
= 6;
52 const unsigned char src
[size
] = {'a', 'b', 'c', 'd', 'e', '\0'};
53 // Should return 'e' and null-terminator.
54 ASSERT_STREQ(call_memrchr(src
, 'e', size
), "e");
57 TEST(LlvmLibcMemRChrTest
, FindsNullTerminator
) {
58 const size_t size
= 6;
59 const unsigned char src
[size
] = {'a', 'b', 'c', 'd', 'e', '\0'};
60 // Should return null terminator.
61 ASSERT_STREQ(call_memrchr(src
, '\0', size
), "");
64 TEST(LlvmLibcMemRChrTest
, CharacterNotWithinStringShouldReturnNullptr
) {
65 const size_t size
= 4;
66 const unsigned char src
[size
] = {'1', '2', '3', '?'};
67 // Since 'z' is not within 'characters', should return nullptr.
68 ASSERT_STREQ(call_memrchr(src
, 'z', size
), nullptr);
71 TEST(LlvmLibcMemRChrTest
, CharacterNotWithinSizeShouldReturnNullptr
) {
72 const unsigned char src
[5] = {'1', '2', '3', '4', '\0'};
73 // Since '4' is not within the first 2 characters, this should return nullptr.
74 const size_t size
= 2;
75 ASSERT_STREQ(call_memrchr(src
, '4', size
), nullptr);
78 TEST(LlvmLibcMemRChrTest
, ShouldFindLastOfDuplicates
) {
79 size_t size
= 12; // 11 characters + null terminator.
80 const char *dups
= "abc1def1ghi";
81 // 1 is duplicated in 'dups', but it should find the last copy.
82 ASSERT_STREQ(call_memrchr(dups
, '1', size
), "1ghi");
84 const char *repeated
= "XXXXX";
85 size
= 6; // 5 characters + null terminator.
86 // Should return the last X with the null terminator.
87 ASSERT_STREQ(call_memrchr(repeated
, 'X', size
), "X");
90 TEST(LlvmLibcMemRChrTest
, EmptyStringShouldOnlyMatchNullTerminator
) {
91 const size_t size
= 1; // Null terminator.
92 const char *empty_string
= "";
93 // Null terminator should match.
94 ASSERT_STREQ(call_memrchr(empty_string
, '\0', size
), "");
95 // All other characters should not match.
96 ASSERT_STREQ(call_memrchr(empty_string
, 'A', size
), nullptr);
97 ASSERT_STREQ(call_memrchr(empty_string
, '9', size
), nullptr);
98 ASSERT_STREQ(call_memrchr(empty_string
, '?', size
), nullptr);
101 TEST(LlvmLibcMemRChrTest
, SignedCharacterFound
) {
103 const size_t size
= 1;
104 char src
[size
] = {c
};
105 const char *actual
= call_memrchr(src
, c
, size
);
106 // Should find the last character 'c'.
107 ASSERT_EQ(actual
[0], c
);
110 TEST(LlvmLibcMemRChrTest
, ZeroLengthShouldReturnNullptr
) {
111 const unsigned char src
[4] = {'a', 'b', 'c', '\0'};
112 // This will iterate over exactly zero characters, so should return nullptr.
113 ASSERT_STREQ(call_memrchr(src
, 'd', 0), nullptr);