1 | //===-- Unittests for memchr ----------------------------------------------===// |
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/memchr.h" |
11 | #include "test/UnitTest/Test.h" |
12 | #include <stddef.h> |
13 | |
14 | // A helper function that calls memchr and abstracts away the explicit cast for |
15 | // readability purposes. |
16 | const char *call_memchr(const void *src, int c, size_t size) { |
17 | return reinterpret_cast<const char *>(LIBC_NAMESPACE::memchr(src, c, size)); |
18 | } |
19 | |
20 | TEST(LlvmLibcMemChrTest, FindsCharacterAfterNullTerminator) { |
21 | // memchr should continue searching after a null terminator. |
22 | const size_t size = 5; |
23 | const unsigned char src[size] = {'a', '\0', 'b', 'c', '\0'}; |
24 | // Should return 'b', 'c', '\0' even when after null terminator. |
25 | ASSERT_STREQ(call_memchr(src, c: 'b', size), "bc" ); |
26 | } |
27 | |
28 | TEST(LlvmLibcMemChrTest, 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_memchr(src, c: 'b', size); |
33 | ASSERT_EQ(ret[0], 'b'); |
34 | ASSERT_EQ(ret[1], 'c'); |
35 | } |
36 | |
37 | TEST(LlvmLibcMemChrTest, 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_memchr(src, c: 'a', size), "abcde" ); |
42 | } |
43 | |
44 | TEST(LlvmLibcMemChrTest, 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_memchr(src, c: 'c', size), "cde" ); |
49 | } |
50 | |
51 | TEST(LlvmLibcMemChrTest, 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_memchr(src, c: 'e', size), "e" ); |
56 | } |
57 | |
58 | TEST(LlvmLibcMemChrTest, 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_memchr(src, c: '\0', size), "" ); |
63 | } |
64 | |
65 | TEST(LlvmLibcMemChrTest, 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_memchr(src, c: 'z', size), nullptr); |
70 | } |
71 | |
72 | TEST(LlvmLibcMemChrTest, CharacterNotWithinSizeShouldReturnNullptr) { |
73 | const unsigned char src[5] = {'1', '2', '3', '4', '\0'}; |
74 | // Since '4' is not the first or second character, this should return nullptr. |
75 | const size_t size = 2; |
76 | ASSERT_STREQ(call_memchr(src, c: '4', size), nullptr); |
77 | } |
78 | |
79 | TEST(LlvmLibcMemChrTest, TheSourceShouldNotChange) { |
80 | const size_t size = 6; |
81 | const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'}; |
82 | const char *src_copy = reinterpret_cast<const char *>(src); |
83 | // When the character is found, the source string should not change. |
84 | LIBC_NAMESPACE::memchr(src, 'd', size); |
85 | ASSERT_STREQ(reinterpret_cast<const char *>(src), src_copy); |
86 | // Same case for when the character is not found. |
87 | LIBC_NAMESPACE::memchr(src, 'z', size); |
88 | ASSERT_STREQ(reinterpret_cast<const char *>(src), src_copy); |
89 | } |
90 | |
91 | TEST(LlvmLibcMemChrTest, ShouldFindFirstOfDuplicates) { |
92 | const size_t size = 12; // 11 characters + null terminator. |
93 | const char *dups = "abc1def1ghi" ; |
94 | // 1 is duplicated in 'dups', but it should find the first copy. |
95 | ASSERT_STREQ(call_memchr(src: dups, c: '1', size), "1def1ghi" ); |
96 | } |
97 | |
98 | TEST(LlvmLibcMemChrTest, EmptyStringShouldOnlyMatchNullTerminator) { |
99 | const size_t size = 1; // Null terminator. |
100 | const char *empty_string = "" ; |
101 | // Null terminator should match. |
102 | ASSERT_STREQ(call_memchr(src: empty_string, c: '\0', size), "" ); |
103 | // All other characters should not match. |
104 | ASSERT_STREQ(call_memchr(src: empty_string, c: 'A', size), nullptr); |
105 | ASSERT_STREQ(call_memchr(src: empty_string, c: '9', size), nullptr); |
106 | ASSERT_STREQ(call_memchr(src: empty_string, c: '?', size), nullptr); |
107 | } |
108 | |
109 | TEST(LlvmLibcMemChrTest, SingleRepeatedCharacterShouldReturnFirst) { |
110 | const char *dups = "XXXXX" ; |
111 | const size_t size = 6; // 5 characters + null terminator. |
112 | // Should return original string since X is first character. |
113 | ASSERT_STREQ(call_memchr(src: dups, c: 'X', size), dups); |
114 | } |
115 | |
116 | TEST(LlvmLibcMemChrTest, SignedCharacterFound) { |
117 | char c = -1; |
118 | const size_t size = 1; |
119 | char src[size] = {c}; |
120 | const char *actual = call_memchr(src, c, size); |
121 | // Should find the first character 'c'. |
122 | ASSERT_EQ(actual[0], c); |
123 | } |
124 | |
125 | #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
126 | |
127 | TEST(LlvmLibcMemChrTest, CrashOnNullPtr) { |
128 | ASSERT_DEATH([]() { LIBC_NAMESPACE::memchr(nullptr, 1, 1); }, |
129 | WITH_SIGNAL(-1)); |
130 | } |
131 | |
132 | #endif // defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
133 | |