aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/string/memrchr_test.cpp
blob: 140395b3046f680e669a3369ec0f7f188e0cf8eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//===-- Unittests for memrchr ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/signal_macros.h"
#include "src/string/memrchr.h"
#include "test/UnitTest/Test.h"
#include <stddef.h>

// A helper function that calls memrchr and abstracts away the explicit cast for
// readability purposes.
const char *call_memrchr(const void *src, int c, size_t size) {
  return reinterpret_cast<const char *>(LIBC_NAMESPACE::memrchr(src, c, size));
}

TEST(LlvmLibcMemRChrTest, FindsCharacterAfterNullTerminator) {
  // memrchr should continue searching after a null terminator.
  const size_t size = 6;
  const unsigned char src[size] = {'a', '\0', 'b', 'c', 'd', '\0'};
  // Should return 'b', 'c', 'd', '\0' even when after null terminator.
  ASSERT_STREQ(call_memrchr(src, 'b', size), "bcd");
}

TEST(LlvmLibcMemRChrTest, FindsCharacterInNonNullTerminatedCollection) {
  const size_t size = 3;
  const unsigned char src[size] = {'a', 'b', 'c'};
  // Should return 'b', 'c'.
  const char *ret = call_memrchr(src, 'b', size);
  ASSERT_EQ(ret[0], 'b');
  ASSERT_EQ(ret[1], 'c');
}

TEST(LlvmLibcMemRChrTest, FindsFirstCharacter) {
  const size_t size = 6;
  const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};
  // Should return original array since 'a' is the first character.
  ASSERT_STREQ(call_memrchr(src, 'a', size), "abcde");
}

TEST(LlvmLibcMemRChrTest, FindsMiddleCharacter) {
  const size_t size = 6;
  const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};
  // Should return characters after (and including) 'c'.
  ASSERT_STREQ(call_memrchr(src, 'c', size), "cde");
}

TEST(LlvmLibcMemRChrTest, FindsLastCharacterThatIsNotNullTerminator) {
  const size_t size = 6;
  const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};
  // Should return 'e' and null-terminator.
  ASSERT_STREQ(call_memrchr(src, 'e', size), "e");
}

TEST(LlvmLibcMemRChrTest, FindsNullTerminator) {
  const size_t size = 6;
  const unsigned char src[size] = {'a', 'b', 'c', 'd', 'e', '\0'};
  // Should return null terminator.
  ASSERT_STREQ(call_memrchr(src, '\0', size), "");
}

TEST(LlvmLibcMemRChrTest, CharacterNotWithinStringShouldReturnNullptr) {
  const size_t size = 4;
  const unsigned char src[size] = {'1', '2', '3', '?'};
  // Since 'z' is not within 'characters', should return nullptr.
  ASSERT_STREQ(call_memrchr(src, 'z', size), nullptr);
}

TEST(LlvmLibcMemRChrTest, CharacterNotWithinSizeShouldReturnNullptr) {
  const unsigned char src[5] = {'1', '2', '3', '4', '\0'};
  // Since '4' is not within the first 2 characters, this should return nullptr.
  const size_t size = 2;
  ASSERT_STREQ(call_memrchr(src, '4', size), nullptr);
}

TEST(LlvmLibcMemRChrTest, ShouldFindLastOfDuplicates) {
  size_t size = 12; // 11 characters + null terminator.
  const char *dups = "abc1def1ghi";
  // 1 is duplicated in 'dups', but it should find the last copy.
  ASSERT_STREQ(call_memrchr(dups, '1', size), "1ghi");

  const char *repeated = "XXXXX";
  size = 6; // 5 characters + null terminator.
  // Should return the last X with the null terminator.
  ASSERT_STREQ(call_memrchr(repeated, 'X', size), "X");
}

TEST(LlvmLibcMemRChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
  const size_t size = 1; // Null terminator.
  const char *empty_string = "";
  // Null terminator should match.
  ASSERT_STREQ(call_memrchr(empty_string, '\0', size), "");
  // All other characters should not match.
  ASSERT_STREQ(call_memrchr(empty_string, 'A', size), nullptr);
  ASSERT_STREQ(call_memrchr(empty_string, '9', size), nullptr);
  ASSERT_STREQ(call_memrchr(empty_string, '?', size), nullptr);
}

TEST(LlvmLibcMemRChrTest, SignedCharacterFound) {
  char c = -1;
  const size_t size = 1;
  char src[size] = {c};
  const char *actual = call_memrchr(src, c, size);
  // Should find the last character 'c'.
  ASSERT_EQ(actual[0], c);
}

TEST(LlvmLibcMemRChrTest, ZeroLengthShouldReturnNullptr) {
  const unsigned char src[4] = {'a', 'b', 'c', '\0'};
  // This will iterate over exactly zero characters, so should return nullptr.
  ASSERT_STREQ(call_memrchr(src, 'd', 0), nullptr);
}

#if defined(LIBC_ADD_NULL_CHECKS)

TEST(LlvmLibcMemRChrTest, CrashOnNullPtr) {
  ASSERT_DEATH([]() { LIBC_NAMESPACE::memrchr(nullptr, 'd', 1); },
               WITH_SIGNAL(-1));
}

#endif // defined(LIBC_ADD_NULL_CHECKS)