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 "src/string/memchr.h"
10 #include "utils/UnitTest/Test.h"
11 #include <stddef.h>
12 
13 // A helper function that calls memchr and abstracts away the explicit cast for
14 // readability purposes.
call_memchr(const void * src,int c,size_t size)15 const char *call_memchr(const void *src, int c, size_t size) {
16   return reinterpret_cast<const char *>(__llvm_libc::memchr(src, c, size));
17 }
18 
TEST(MemChrTest,FindsCharacterAfterNullTerminator)19 TEST(MemChrTest, 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");
25 }
26 
TEST(MemChrTest,FindsCharacterInNonNullTerminatedCollection)27 TEST(MemChrTest, 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');
34 }
35 
TEST(MemChrTest,FindsFirstCharacter)36 TEST(MemChrTest, 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");
41 }
42 
TEST(MemChrTest,FindsMiddleCharacter)43 TEST(MemChrTest, 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");
48 }
49 
TEST(MemChrTest,FindsLastCharacterThatIsNotNullTerminator)50 TEST(MemChrTest, 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");
55 }
56 
TEST(MemChrTest,FindsNullTerminator)57 TEST(MemChrTest, 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), "");
62 }
63 
TEST(MemChrTest,CharacterNotWithinStringShouldReturnNullptr)64 TEST(MemChrTest, 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);
69 }
70 
TEST(MemChrTest,CharacterNotWithinSizeShouldReturnNullptr)71 TEST(MemChrTest, 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);
76 }
77 
TEST(MemChrTest,TheSourceShouldNotChange)78 TEST(MemChrTest, 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   __llvm_libc::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   __llvm_libc::memchr(src, 'z', size);
87   ASSERT_STREQ(reinterpret_cast<const char *>(src), src_copy);
88 }
89 
TEST(MemChrTest,ShouldFindFirstOfDuplicates)90 TEST(MemChrTest, 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");
95 }
96 
TEST(MemChrTest,EmptyStringShouldOnlyMatchNullTerminator)97 TEST(MemChrTest, 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);
106 }
107 
TEST(MemChrTest,SingleRepeatedCharacterShouldReturnFirst)108 TEST(MemChrTest, 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);
113 }
114 
TEST(MemChrTest,SignedCharacterFound)115 TEST(MemChrTest, SignedCharacterFound) {
116   char c = -1;
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);
122 }
123