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 "src/string/memrchr.h"
10 #include "utils/UnitTest/Test.h"
11 #include <stddef.h>
12 
13 // A helper function that calls memrchr and abstracts away the explicit cast for
14 // readability purposes.
call_memrchr(const void * src,int c,size_t size)15 const char *call_memrchr(const void *src, int c, size_t size) {
16   return reinterpret_cast<const char *>(__llvm_libc::memrchr(src, c, size));
17 }
18 
TEST(MemRChrTest,FindsCharacterAfterNullTerminator)19 TEST(MemRChrTest, 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");
25 }
26 
TEST(MemRChrTest,FindsCharacterInNonNullTerminatedCollection)27 TEST(MemRChrTest, 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');
34 }
35 
TEST(MemRChrTest,FindsFirstCharacter)36 TEST(MemRChrTest, 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");
41 }
42 
TEST(MemRChrTest,FindsMiddleCharacter)43 TEST(MemRChrTest, 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");
48 }
49 
TEST(MemRChrTest,FindsLastCharacterThatIsNotNullTerminator)50 TEST(MemRChrTest, 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");
55 }
56 
TEST(MemRChrTest,FindsNullTerminator)57 TEST(MemRChrTest, 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), "");
62 }
63 
TEST(MemRChrTest,CharacterNotWithinStringShouldReturnNullptr)64 TEST(MemRChrTest, 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);
69 }
70 
TEST(MemRChrTest,CharacterNotWithinSizeShouldReturnNullptr)71 TEST(MemRChrTest, 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);
76 }
77 
TEST(MemRChrTest,ShouldFindLastOfDuplicates)78 TEST(MemRChrTest, 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");
83 
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");
88 }
89 
TEST(MemRChrTest,EmptyStringShouldOnlyMatchNullTerminator)90 TEST(MemRChrTest, 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);
99 }
100 
TEST(MemRChrTest,SignedCharacterFound)101 TEST(MemRChrTest, SignedCharacterFound) {
102   char c = -1;
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);
108 }
109 
TEST(MemRChrTest,ZeroLengthShouldReturnNullptr)110 TEST(MemRChrTest, 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);
114 }
115