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