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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
//===-- Tests for str{,r}chr and {,r}index functions ------------*- C++ -*-===//
//
// 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 "test/UnitTest/Test.h"
template <auto Func> struct StrchrTest : public LIBC_NAMESPACE::testing::Test {
void findsFirstCharacter() {
const char *src = "abcde";
// Should return original string since 'a' is the first character.
ASSERT_STREQ(Func(src, 'a'), "abcde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsMiddleCharacter() {
const char *src = "abcde";
// Should return characters after (and including) 'c'.
ASSERT_STREQ(Func(src, 'c'), "cde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsLastCharacterThatIsNotNullTerminator() {
const char *src = "abcde";
// Should return 'e' and null-terminator.
ASSERT_STREQ(Func(src, 'e'), "e");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsNullTerminator() {
const char *src = "abcde";
// Should return null terminator.
const char *nul_terminator = Func(src, '\0');
ASSERT_NE(nul_terminator, nullptr);
ASSERT_STREQ(nul_terminator, "");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void characterNotWithinStringShouldReturnNullptr() {
// Since 'z' is not within the string, should return nullptr.
ASSERT_EQ(Func("123?", 'z'), nullptr);
}
void theSourceShouldNotChange() {
const char *src = "abcde";
// When the character is found, the source string should not change.
Func(src, 'd');
ASSERT_STREQ(src, "abcde");
// Same case for when the character is not found.
Func(src, 'z');
ASSERT_STREQ(src, "abcde");
// Same case for when looking for nullptr.
Func(src, '\0');
ASSERT_STREQ(src, "abcde");
}
void shouldFindFirstOfDuplicates() {
// '1' is duplicated in the string, but it should find the first copy.
ASSERT_STREQ(Func("abc1def1ghi", '1'), "1def1ghi");
const char *dups = "XXXXX";
// Should return original string since 'X' is the first character.
ASSERT_STREQ(Func(dups, 'X'), dups);
}
void emptyStringShouldOnlyMatchNullTerminator() {
// Null terminator should match.
const char empty_string[] = "";
ASSERT_EQ(static_cast<const char *>(Func(empty_string, '\0')),
empty_string);
// All other characters should not match.
ASSERT_EQ(Func("", 'Z'), nullptr);
ASSERT_EQ(Func("", '3'), nullptr);
ASSERT_EQ(Func("", '*'), nullptr);
}
};
template <auto Func> struct StrrchrTest : public LIBC_NAMESPACE::testing::Test {
void findsFirstCharacter() {
const char *src = "abcde";
// Should return original string since 'a' is the first character.
ASSERT_STREQ(Func(src, 'a'), "abcde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsMiddleCharacter() {
const char *src = "abcde";
// Should return characters after (and including) 'c'.
ASSERT_STREQ(Func(src, 'c'), "cde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsLastCharacterThatIsNotNullTerminator() {
const char *src = "abcde";
// Should return 'e' and null-terminator.
ASSERT_STREQ(Func(src, 'e'), "e");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsNullTerminator() {
const char *src = "abcde";
// Should return null terminator.
const char *nul_terminator = Func(src, '\0');
ASSERT_NE(nul_terminator, nullptr);
ASSERT_STREQ(nul_terminator, "");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}
void findsLastBehindFirstNullTerminator() {
static const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
// 'b' is behind a null terminator, so should not be found.
ASSERT_EQ(Func(src, 'b'), nullptr);
// Same goes for 'c'.
ASSERT_EQ(Func(src, 'c'), nullptr);
// Should find the second of the two a's.
ASSERT_STREQ(Func(src, 'a'), "a");
}
void characterNotWithinStringShouldReturnNullptr() {
// Since 'z' is not within the string, should return nullptr.
ASSERT_EQ(Func("123?", 'z'), nullptr);
}
void shouldFindLastOfDuplicates() {
// '1' is duplicated in the string, but it should find the last copy.
ASSERT_STREQ(Func("abc1def1ghi", '1'), "1ghi");
const char *dups = "XXXXX";
// Should return the last occurrence of 'X'.
ASSERT_STREQ(Func(dups, 'X'), "X");
}
void emptyStringShouldOnlyMatchNullTerminator() {
// Null terminator should match.
const char empty_string[] = "";
ASSERT_EQ(static_cast<const char *>(Func(empty_string, '\0')),
empty_string);
// All other characters should not match.
ASSERT_EQ(Func("", 'A'), nullptr);
ASSERT_EQ(Func("", '2'), nullptr);
ASSERT_EQ(Func("", '*'), nullptr);
}
};
#define STRCHR_TEST(name, func) \
using LlvmLibc##name##Test = StrchrTest<func>; \
TEST_F(LlvmLibc##name##Test, FindsFirstCharacter) { findsFirstCharacter(); } \
TEST_F(LlvmLibc##name##Test, FindsMiddleCharacter) { \
findsMiddleCharacter(); \
} \
TEST_F(LlvmLibc##name##Test, FindsLastCharacterThatIsNotNullTerminator) { \
findsLastCharacterThatIsNotNullTerminator(); \
} \
TEST_F(LlvmLibc##name##Test, FindsNullTerminator) { findsNullTerminator(); } \
TEST_F(LlvmLibc##name##Test, CharacterNotWithinStringShouldReturnNullptr) { \
characterNotWithinStringShouldReturnNullptr(); \
} \
TEST_F(LlvmLibc##name##Test, TheSourceShouldNotChange) { \
theSourceShouldNotChange(); \
} \
TEST_F(LlvmLibc##name##Test, ShouldFindFirstOfDuplicates) { \
shouldFindFirstOfDuplicates(); \
} \
TEST_F(LlvmLibc##name##Test, EmptyStringShouldOnlyMatchNullTerminator) { \
emptyStringShouldOnlyMatchNullTerminator(); \
}
#define STRRCHR_TEST(name, func) \
using LlvmLibc##name##Test = StrrchrTest<func>; \
TEST_F(LlvmLibc##name##Test, FindsFirstCharacter) { findsFirstCharacter(); } \
TEST_F(LlvmLibc##name##Test, FindsMiddleCharacter) { \
findsMiddleCharacter(); \
} \
TEST_F(LlvmLibc##name##Test, FindsLastCharacterThatIsNotNullTerminator) { \
findsLastCharacterThatIsNotNullTerminator(); \
} \
TEST_F(LlvmLibc##name##Test, FindsNullTerminator) { findsNullTerminator(); } \
TEST_F(LlvmLibc##name##Test, FindsLastBehindFirstNullTerminator) { \
findsLastBehindFirstNullTerminator(); \
} \
TEST_F(LlvmLibc##name##Test, CharacterNotWithinStringShouldReturnNullptr) { \
characterNotWithinStringShouldReturnNullptr(); \
} \
TEST_F(LlvmLibc##name##Test, ShouldFindLastOfDuplicates) { \
shouldFindLastOfDuplicates(); \
} \
TEST_F(LlvmLibc##name##Test, EmptyStringShouldOnlyMatchNullTerminator) { \
emptyStringShouldOnlyMatchNullTerminator(); \
}
|