1 | //===-- Unittests for string_view -----------------------------------------===// |
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/__support/CPP/string_view.h" |
10 | #include "test/UnitTest/Test.h" |
11 | |
12 | using LIBC_NAMESPACE::cpp::string_view; |
13 | |
14 | TEST(LlvmLibcStringViewTest, InitializeCheck) { |
15 | string_view v; |
16 | ASSERT_EQ(v.size(), size_t(0)); |
17 | ASSERT_TRUE(v.data() == nullptr); |
18 | |
19 | v = string_view("" ); |
20 | ASSERT_EQ(v.size(), size_t(0)); |
21 | ASSERT_TRUE(v.data() != nullptr); |
22 | |
23 | v = string_view("abc" , 0); |
24 | ASSERT_EQ(v.size(), size_t(0)); |
25 | ASSERT_TRUE(v.data() != nullptr); |
26 | |
27 | v = string_view("123456789" ); |
28 | ASSERT_EQ(v.size(), size_t(9)); |
29 | } |
30 | |
31 | TEST(LlvmLibcStringViewTest, Equals) { |
32 | string_view v("abc" ); |
33 | ASSERT_EQ(v, string_view("abc" )); |
34 | ASSERT_NE(v, string_view()); |
35 | ASSERT_NE(v, string_view("" )); |
36 | ASSERT_NE(v, string_view("123" )); |
37 | ASSERT_NE(v, string_view("abd" )); |
38 | ASSERT_NE(v, string_view("aaa" )); |
39 | ASSERT_NE(v, string_view("abcde" )); |
40 | } |
41 | |
42 | TEST(LlvmLibcStringViewTest, startsWith) { |
43 | string_view v("abc" ); |
44 | ASSERT_TRUE(v.starts_with('a')); |
45 | ASSERT_TRUE(v.starts_with(string_view("a" ))); |
46 | ASSERT_TRUE(v.starts_with(string_view("ab" ))); |
47 | ASSERT_TRUE(v.starts_with(string_view("abc" ))); |
48 | ASSERT_TRUE(v.starts_with(string_view())); |
49 | ASSERT_TRUE(v.starts_with(string_view("" ))); |
50 | ASSERT_FALSE(v.starts_with('1')); |
51 | ASSERT_FALSE(v.starts_with(string_view("123" ))); |
52 | ASSERT_FALSE(v.starts_with(string_view("abd" ))); |
53 | ASSERT_FALSE(v.starts_with(string_view("aaa" ))); |
54 | ASSERT_FALSE(v.starts_with(string_view("abcde" ))); |
55 | } |
56 | |
57 | TEST(LlvmLibcStringViewTest, endsWith) { |
58 | string_view v("abc" ); |
59 | ASSERT_TRUE(v.ends_with('c')); |
60 | ASSERT_TRUE(v.ends_with(string_view("c" ))); |
61 | ASSERT_TRUE(v.ends_with(string_view("bc" ))); |
62 | ASSERT_TRUE(v.ends_with(string_view("abc" ))); |
63 | ASSERT_TRUE(v.ends_with(string_view())); |
64 | ASSERT_TRUE(v.ends_with(string_view("" ))); |
65 | ASSERT_FALSE(v.ends_with('1')); |
66 | ASSERT_FALSE(v.ends_with(string_view("123" ))); |
67 | ASSERT_FALSE(v.ends_with(string_view("abd" ))); |
68 | ASSERT_FALSE(v.ends_with(string_view("aaa" ))); |
69 | ASSERT_FALSE(v.ends_with(string_view("abcde" ))); |
70 | } |
71 | |
72 | TEST(LlvmLibcStringViewTest, RemovePrefix) { |
73 | string_view a("123456789" ); |
74 | a.remove_prefix(N: 0); |
75 | ASSERT_EQ(a.size(), size_t(9)); |
76 | ASSERT_TRUE(a == "123456789" ); |
77 | |
78 | string_view b("123456789" ); |
79 | b.remove_prefix(N: 4); |
80 | ASSERT_EQ(b.size(), size_t(5)); |
81 | ASSERT_TRUE(b == "56789" ); |
82 | |
83 | string_view c("123456789" ); |
84 | c.remove_prefix(N: 9); |
85 | ASSERT_EQ(c.size(), size_t(0)); |
86 | } |
87 | |
88 | TEST(LlvmLibcStringViewTest, RemoveSuffix) { |
89 | string_view a("123456789" ); |
90 | a.remove_suffix(N: 0); |
91 | ASSERT_EQ(a.size(), size_t(9)); |
92 | ASSERT_TRUE(a == "123456789" ); |
93 | |
94 | string_view b("123456789" ); |
95 | b.remove_suffix(N: 4); |
96 | ASSERT_EQ(b.size(), size_t(5)); |
97 | ASSERT_TRUE(b == "12345" ); |
98 | |
99 | string_view c("123456789" ); |
100 | c.remove_suffix(N: 9); |
101 | ASSERT_EQ(c.size(), size_t(0)); |
102 | } |
103 | |
104 | TEST(LlvmLibcStringViewTest, Observer) { |
105 | string_view ABC("abc" ); |
106 | ASSERT_EQ(ABC.size(), size_t(3)); |
107 | ASSERT_FALSE(ABC.empty()); |
108 | ASSERT_EQ(ABC.front(), 'a'); |
109 | ASSERT_EQ(ABC.back(), 'c'); |
110 | } |
111 | |
112 | bool isDigit(char c) { return c >= '0' && c <= '9'; } |
113 | |
114 | TEST(LlvmLibcStringViewTest, FindFirstOf) { |
115 | string_view Tmp("abca" ); |
116 | ASSERT_TRUE(Tmp.find_first_of('a') == 0); |
117 | ASSERT_TRUE(Tmp.find_first_of('d') == string_view::npos); |
118 | ASSERT_TRUE(Tmp.find_first_of('b') == 1); |
119 | ASSERT_TRUE(Tmp.find_first_of('a', 0) == 0); |
120 | ASSERT_TRUE(Tmp.find_first_of('b', 1) == 1); |
121 | ASSERT_TRUE(Tmp.find_first_of('a', 1) == 3); |
122 | ASSERT_TRUE(Tmp.find_first_of('a', 42) == string_view::npos); |
123 | ASSERT_FALSE(Tmp.find_first_of('c') == 1); |
124 | ASSERT_FALSE(Tmp.find_first_of('c', 0) == 1); |
125 | ASSERT_FALSE(Tmp.find_first_of('c', 1) == 1); |
126 | } |
127 | |
128 | TEST(LlvmLibcStringViewTest, FindLastOf) { |
129 | string_view Tmp("abada" ); |
130 | |
131 | ASSERT_EQ(Tmp.find_last_of('a'), size_t(4)); |
132 | ASSERT_EQ(Tmp.find_last_of('a', 123), size_t(4)); |
133 | ASSERT_EQ(Tmp.find_last_of('a', 5), size_t(4)); |
134 | ASSERT_EQ(Tmp.find_last_of('a', 4), size_t(4)); |
135 | ASSERT_EQ(Tmp.find_last_of('a', 3), size_t(2)); |
136 | ASSERT_EQ(Tmp.find_last_of('a', 2), size_t(2)); |
137 | ASSERT_EQ(Tmp.find_last_of('a', 1), size_t(0)); |
138 | ASSERT_EQ(Tmp.find_last_of('a', 0), size_t(0)); |
139 | |
140 | ASSERT_EQ(Tmp.find_last_of('b'), size_t(1)); |
141 | ASSERT_EQ(Tmp.find_last_of('b', 123), size_t(1)); |
142 | ASSERT_EQ(Tmp.find_last_of('b', 5), size_t(1)); |
143 | ASSERT_EQ(Tmp.find_last_of('b', 4), size_t(1)); |
144 | ASSERT_EQ(Tmp.find_last_of('b', 3), size_t(1)); |
145 | ASSERT_EQ(Tmp.find_last_of('b', 2), size_t(1)); |
146 | ASSERT_EQ(Tmp.find_last_of('b', 1), size_t(1)); |
147 | ASSERT_EQ(Tmp.find_last_of('b', 0), string_view::npos); |
148 | |
149 | ASSERT_EQ(Tmp.find_last_of('d'), size_t(3)); |
150 | ASSERT_EQ(Tmp.find_last_of('d', 123), size_t(3)); |
151 | ASSERT_EQ(Tmp.find_last_of('d', 5), size_t(3)); |
152 | ASSERT_EQ(Tmp.find_last_of('d', 4), size_t(3)); |
153 | ASSERT_EQ(Tmp.find_last_of('d', 3), size_t(3)); |
154 | ASSERT_EQ(Tmp.find_last_of('d', 2), string_view::npos); |
155 | ASSERT_EQ(Tmp.find_last_of('d', 1), string_view::npos); |
156 | ASSERT_EQ(Tmp.find_last_of('d', 0), string_view::npos); |
157 | |
158 | ASSERT_EQ(Tmp.find_last_of('e'), string_view::npos); |
159 | ASSERT_EQ(Tmp.find_last_of('e', 123), string_view::npos); |
160 | ASSERT_EQ(Tmp.find_last_of('e', 5), string_view::npos); |
161 | ASSERT_EQ(Tmp.find_last_of('e', 4), string_view::npos); |
162 | ASSERT_EQ(Tmp.find_last_of('e', 3), string_view::npos); |
163 | ASSERT_EQ(Tmp.find_last_of('e', 2), string_view::npos); |
164 | ASSERT_EQ(Tmp.find_last_of('e', 1), string_view::npos); |
165 | ASSERT_EQ(Tmp.find_last_of('e', 0), string_view::npos); |
166 | |
167 | string_view Empty; |
168 | ASSERT_EQ(Empty.find_last_of('a'), string_view::npos); |
169 | ASSERT_EQ(Empty.find_last_of('a', 0), string_view::npos); |
170 | ASSERT_EQ(Empty.find_last_of('a', 123), string_view::npos); |
171 | |
172 | string_view Empty1("" ); |
173 | ASSERT_EQ(Empty1.find_last_of('a'), string_view::npos); |
174 | ASSERT_EQ(Empty1.find_last_of('a', 0), string_view::npos); |
175 | ASSERT_EQ(Empty1.find_last_of('a', 123), string_view::npos); |
176 | } |
177 | |
178 | TEST(LlvmLibcStringViewTest, FindFirstNotOf) { |
179 | string_view Tmp("abada" ); |
180 | |
181 | EXPECT_EQ(Tmp.find_first_not_of('a'), size_t(1)); |
182 | EXPECT_EQ(Tmp.find_first_not_of('a', 123), string_view::npos); |
183 | EXPECT_EQ(Tmp.find_first_not_of('a', 5), string_view::npos); |
184 | EXPECT_EQ(Tmp.find_first_not_of('a', 4), string_view::npos); |
185 | EXPECT_EQ(Tmp.find_first_not_of('a', 3), size_t(3)); |
186 | EXPECT_EQ(Tmp.find_first_not_of('a', 2), size_t(3)); |
187 | EXPECT_EQ(Tmp.find_first_not_of('a', 1), size_t(1)); |
188 | EXPECT_EQ(Tmp.find_first_not_of('a', 0), size_t(1)); |
189 | |
190 | EXPECT_EQ(Tmp.find_first_not_of('b'), size_t(0)); |
191 | EXPECT_EQ(Tmp.find_first_not_of('b', 123), string_view::npos); |
192 | EXPECT_EQ(Tmp.find_first_not_of('b', 5), string_view::npos); |
193 | EXPECT_EQ(Tmp.find_first_not_of('b', 4), size_t(4)); |
194 | EXPECT_EQ(Tmp.find_first_not_of('b', 3), size_t(3)); |
195 | EXPECT_EQ(Tmp.find_first_not_of('b', 2), size_t(2)); |
196 | EXPECT_EQ(Tmp.find_first_not_of('b', 1), size_t(2)); |
197 | EXPECT_EQ(Tmp.find_first_not_of('b', 0), size_t(0)); |
198 | |
199 | EXPECT_EQ(Tmp.find_first_not_of('d'), size_t(0)); |
200 | EXPECT_EQ(Tmp.find_first_not_of('d', 123), string_view::npos); |
201 | EXPECT_EQ(Tmp.find_first_not_of('d', 5), string_view::npos); |
202 | EXPECT_EQ(Tmp.find_first_not_of('d', 4), size_t(4)); |
203 | EXPECT_EQ(Tmp.find_first_not_of('d', 3), size_t(4)); |
204 | EXPECT_EQ(Tmp.find_first_not_of('d', 2), size_t(2)); |
205 | EXPECT_EQ(Tmp.find_first_not_of('d', 1), size_t(1)); |
206 | EXPECT_EQ(Tmp.find_first_not_of('d', 0), size_t(0)); |
207 | |
208 | EXPECT_EQ(Tmp.find_first_not_of('e'), size_t(0)); |
209 | EXPECT_EQ(Tmp.find_first_not_of('e', 123), string_view::npos); |
210 | EXPECT_EQ(Tmp.find_first_not_of('e', 5), string_view::npos); |
211 | EXPECT_EQ(Tmp.find_first_not_of('e', 4), size_t(4)); |
212 | EXPECT_EQ(Tmp.find_first_not_of('e', 3), size_t(3)); |
213 | EXPECT_EQ(Tmp.find_first_not_of('e', 2), size_t(2)); |
214 | EXPECT_EQ(Tmp.find_first_not_of('e', 1), size_t(1)); |
215 | EXPECT_EQ(Tmp.find_first_not_of('e', 0), size_t(0)); |
216 | |
217 | string_view Empty; |
218 | EXPECT_EQ(Empty.find_first_not_of('a'), string_view::npos); |
219 | EXPECT_EQ(Empty.find_first_not_of('a', 0), string_view::npos); |
220 | EXPECT_EQ(Empty.find_first_not_of('a', 123), string_view::npos); |
221 | |
222 | string_view Empty1("" ); |
223 | EXPECT_EQ(Empty1.find_first_not_of('a'), string_view::npos); |
224 | EXPECT_EQ(Empty1.find_first_not_of('a', 0), string_view::npos); |
225 | EXPECT_EQ(Empty1.find_first_not_of('a', 123), string_view::npos); |
226 | |
227 | string_view Full("aaaaaaa" ); |
228 | EXPECT_EQ(Full.find_first_not_of('a'), string_view::npos); |
229 | EXPECT_EQ(Full.find_first_not_of('a', 0), string_view::npos); |
230 | EXPECT_EQ(Full.find_first_not_of('a', 123), string_view::npos); |
231 | |
232 | EXPECT_EQ(Full.find_first_not_of('b'), size_t(0)); |
233 | EXPECT_EQ(Full.find_first_not_of('b', 0), size_t(0)); |
234 | EXPECT_EQ(Full.find_first_not_of('b', 123), string_view::npos); |
235 | } |
236 | |
237 | TEST(LlvmLibcStringViewTest, Contains) { |
238 | string_view Empty; |
239 | for (char c = 'a'; c < 'z'; ++c) |
240 | EXPECT_FALSE(Empty.contains(c)); |
241 | |
242 | string_view Tmp("abada" ); |
243 | EXPECT_TRUE(Tmp.contains('a')); |
244 | EXPECT_TRUE(Tmp.contains('b')); |
245 | EXPECT_FALSE(Tmp.contains('c')); |
246 | EXPECT_TRUE(Tmp.contains('d')); |
247 | EXPECT_FALSE(Tmp.contains('e')); |
248 | |
249 | EXPECT_TRUE(Tmp.substr(1).contains('a')); |
250 | EXPECT_TRUE(Tmp.substr(1).contains('b')); |
251 | EXPECT_FALSE(Tmp.substr(1).contains('c')); |
252 | EXPECT_TRUE(Tmp.substr(1).contains('d')); |
253 | EXPECT_FALSE(Tmp.substr(1).contains('e')); |
254 | |
255 | EXPECT_TRUE(Tmp.substr(2).contains('a')); |
256 | EXPECT_FALSE(Tmp.substr(2).contains('b')); |
257 | EXPECT_FALSE(Tmp.substr(2).contains('c')); |
258 | EXPECT_TRUE(Tmp.substr(2).contains('d')); |
259 | EXPECT_FALSE(Tmp.substr(2).contains('e')); |
260 | |
261 | EXPECT_TRUE(Tmp.substr(3).contains('a')); |
262 | EXPECT_FALSE(Tmp.substr(3).contains('b')); |
263 | EXPECT_FALSE(Tmp.substr(3).contains('c')); |
264 | EXPECT_TRUE(Tmp.substr(3).contains('d')); |
265 | EXPECT_FALSE(Tmp.substr(3).contains('e')); |
266 | |
267 | EXPECT_TRUE(Tmp.substr(4).contains('a')); |
268 | EXPECT_FALSE(Tmp.substr(4).contains('b')); |
269 | EXPECT_FALSE(Tmp.substr(4).contains('c')); |
270 | EXPECT_FALSE(Tmp.substr(4).contains('d')); |
271 | EXPECT_FALSE(Tmp.substr(4).contains('e')); |
272 | |
273 | EXPECT_FALSE(Tmp.substr(5).contains('a')); |
274 | EXPECT_FALSE(Tmp.substr(5).contains('b')); |
275 | EXPECT_FALSE(Tmp.substr(5).contains('c')); |
276 | EXPECT_FALSE(Tmp.substr(5).contains('d')); |
277 | EXPECT_FALSE(Tmp.substr(5).contains('e')); |
278 | |
279 | EXPECT_FALSE(Tmp.substr(6).contains('a')); |
280 | EXPECT_FALSE(Tmp.substr(6).contains('b')); |
281 | EXPECT_FALSE(Tmp.substr(6).contains('c')); |
282 | EXPECT_FALSE(Tmp.substr(6).contains('d')); |
283 | EXPECT_FALSE(Tmp.substr(6).contains('e')); |
284 | } |
285 | |