1 //===-- Unittests for memchr ----------------------------------------------===//
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/memchr.h"
10 #include "test/UnitTest/Test.h"
13 // A helper function that calls memchr and abstracts away the explicit cast for
14 // readability purposes.
15 const char *call_memchr(const void *src
, int c
, size_t size
) {
16 return reinterpret_cast<const char *>(LIBC_NAMESPACE::memchr(src
, c
, size
));
19 TEST(LlvmLibcMemChrTest
, FindsCharacterAfterNullTerminator
) {
20 // memchr should continue searching after a null terminator.
21 const size_t size
= 5;
22 const unsigned char src
[size
] = {'a', '\0', 'b', 'c', '\0'};
23 // Should return 'b', 'c', '\0' even when after null terminator.
24 ASSERT_STREQ(call_memchr(src
, 'b', size
), "bc");
27 TEST(LlvmLibcMemChrTest
, 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_memchr(src
, 'b', size
);
32 ASSERT_EQ(ret
[0], 'b');
33 ASSERT_EQ(ret
[1], 'c');
36 TEST(LlvmLibcMemChrTest
, 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_memchr(src
, 'a', size
), "abcde");
43 TEST(LlvmLibcMemChrTest
, 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_memchr(src
, 'c', size
), "cde");
50 TEST(LlvmLibcMemChrTest
, 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_memchr(src
, 'e', size
), "e");
57 TEST(LlvmLibcMemChrTest
, 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_memchr(src
, '\0', size
), "");
64 TEST(LlvmLibcMemChrTest
, 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_memchr(src
, 'z', size
), nullptr);
71 TEST(LlvmLibcMemChrTest
, CharacterNotWithinSizeShouldReturnNullptr
) {
72 const unsigned char src
[5] = {'1', '2', '3', '4', '\0'};
73 // Since '4' is not the first or second character, this should return nullptr.
74 const size_t size
= 2;
75 ASSERT_STREQ(call_memchr(src
, '4', size
), nullptr);
78 TEST(LlvmLibcMemChrTest
, TheSourceShouldNotChange
) {
79 const size_t size
= 6;
80 const unsigned char src
[size
] = {'a', 'b', 'c', 'd', 'e', '\0'};
81 const char *src_copy
= reinterpret_cast<const char *>(src
);
82 // When the character is found, the source string should not change.
83 LIBC_NAMESPACE::memchr(src
, 'd', size
);
84 ASSERT_STREQ(reinterpret_cast<const char *>(src
), src_copy
);
85 // Same case for when the character is not found.
86 LIBC_NAMESPACE::memchr(src
, 'z', size
);
87 ASSERT_STREQ(reinterpret_cast<const char *>(src
), src_copy
);
90 TEST(LlvmLibcMemChrTest
, ShouldFindFirstOfDuplicates
) {
91 const size_t size
= 12; // 11 characters + null terminator.
92 const char *dups
= "abc1def1ghi";
93 // 1 is duplicated in 'dups', but it should find the first copy.
94 ASSERT_STREQ(call_memchr(dups
, '1', size
), "1def1ghi");
97 TEST(LlvmLibcMemChrTest
, EmptyStringShouldOnlyMatchNullTerminator
) {
98 const size_t size
= 1; // Null terminator.
99 const char *empty_string
= "";
100 // Null terminator should match.
101 ASSERT_STREQ(call_memchr(empty_string
, '\0', size
), "");
102 // All other characters should not match.
103 ASSERT_STREQ(call_memchr(empty_string
, 'A', size
), nullptr);
104 ASSERT_STREQ(call_memchr(empty_string
, '9', size
), nullptr);
105 ASSERT_STREQ(call_memchr(empty_string
, '?', size
), nullptr);
108 TEST(LlvmLibcMemChrTest
, SingleRepeatedCharacterShouldReturnFirst
) {
109 const char *dups
= "XXXXX";
110 const size_t size
= 6; // 5 characters + null terminator.
111 // Should return original string since X is first character.
112 ASSERT_STREQ(call_memchr(dups
, 'X', size
), dups
);
115 TEST(LlvmLibcMemChrTest
, SignedCharacterFound
) {
117 const size_t size
= 1;
118 char src
[size
] = {c
};
119 const char *actual
= call_memchr(src
, c
, size
);
120 // Should find the first character 'c'.
121 ASSERT_EQ(actual
[0], c
);