xref: /aosp_15_r20/external/cronet/base/strings/string_piece_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 
7 #include <string>
8 #include <string_view>
9 
10 #include "base/strings/string_piece.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace base {
15 
16 template <typename CharT>
17 class CommonStringPieceTest : public ::testing::Test {
18  public:
as_string(const char * input)19   static std::string as_string(const char* input) { return input; }
as_string(const std::string & input)20   static const std::string& as_string(const std::string& input) {
21     return input;
22   }
23 };
24 
25 template <>
26 class CommonStringPieceTest<char16_t> : public ::testing::Test {
27  public:
as_string(const char * input)28   static std::u16string as_string(const char* input) {
29     return UTF8ToUTF16(input);
30   }
as_string(const std::string & input)31   static std::u16string as_string(const std::string& input) {
32     return UTF8ToUTF16(input);
33   }
34 };
35 
36 typedef ::testing::Types<char, char16_t> SupportedCharTypes;
37 
38 TYPED_TEST_SUITE(CommonStringPieceTest, SupportedCharTypes);
39 
TYPED_TEST(CommonStringPieceTest,CheckComparisonOperators)40 TYPED_TEST(CommonStringPieceTest, CheckComparisonOperators) {
41 #define CMP_Y(op, x, y)                                                        \
42   {                                                                            \
43     std::basic_string<TypeParam> lhs(TestFixture::as_string(x));               \
44     std::basic_string<TypeParam> rhs(TestFixture::as_string(y));               \
45     ASSERT_TRUE((std::basic_string_view<TypeParam>((lhs.c_str()))              \
46                      op std::basic_string_view<TypeParam>((rhs.c_str()))));    \
47     ASSERT_TRUE(std::basic_string_view<TypeParam>(lhs) op rhs);                \
48     ASSERT_TRUE(lhs op std::basic_string_view<TypeParam>(rhs));                \
49     ASSERT_TRUE((                                                              \
50         std::basic_string_view<TypeParam>((lhs.c_str()))                       \
51             .compare(std::basic_string_view<TypeParam>((rhs.c_str()))) op 0)); \
52   }
53 
54 #define CMP_N(op, x, y)                                                        \
55   {                                                                            \
56     std::basic_string<TypeParam> lhs(TestFixture::as_string(x));               \
57     std::basic_string<TypeParam> rhs(TestFixture::as_string(y));               \
58     ASSERT_FALSE((std::basic_string_view<TypeParam>((lhs.c_str()))             \
59                       op std::basic_string_view<TypeParam>((rhs.c_str()))));   \
60     ASSERT_FALSE(std::basic_string_view<TypeParam>(lhs) op rhs);               \
61     ASSERT_FALSE(lhs op std::basic_string_view<TypeParam>(rhs));               \
62     ASSERT_FALSE((                                                             \
63         std::basic_string_view<TypeParam>((lhs.c_str()))                       \
64             .compare(std::basic_string_view<TypeParam>((rhs.c_str()))) op 0)); \
65   }
66 
67   CMP_Y(==, "", "")
68   CMP_Y(==, "a", "a")
69   CMP_Y(==, "aa", "aa")
70   CMP_N(==, "a", "")
71   CMP_N(==, "", "a")
72   CMP_N(==, "a", "b")
73   CMP_N(==, "a", "aa")
74   CMP_N(==, "aa", "a")
75 
76   CMP_N(!=, "", "")
77   CMP_N(!=, "a", "a")
78   CMP_N(!=, "aa", "aa")
79   CMP_Y(!=, "a", "")
80   CMP_Y(!=, "", "a")
81   CMP_Y(!=, "a", "b")
82   CMP_Y(!=, "a", "aa")
83   CMP_Y(!=, "aa", "a")
84 
85   CMP_Y(<, "a", "b")
86   CMP_Y(<, "a", "aa")
87   CMP_Y(<, "aa", "b")
88   CMP_Y(<, "aa", "bb")
89   CMP_N(<, "a", "a")
90   CMP_N(<, "b", "a")
91   CMP_N(<, "aa", "a")
92   CMP_N(<, "b", "aa")
93   CMP_N(<, "bb", "aa")
94 
95   CMP_Y(<=, "a", "a")
96   CMP_Y(<=, "a", "b")
97   CMP_Y(<=, "a", "aa")
98   CMP_Y(<=, "aa", "b")
99   CMP_Y(<=, "aa", "bb")
100   CMP_N(<=, "b", "a")
101   CMP_N(<=, "aa", "a")
102   CMP_N(<=, "b", "aa")
103   CMP_N(<=, "bb", "aa")
104 
105   CMP_N(>=, "a", "b")
106   CMP_N(>=, "a", "aa")
107   CMP_N(>=, "aa", "b")
108   CMP_N(>=, "aa", "bb")
109   CMP_Y(>=, "a", "a")
110   CMP_Y(>=, "b", "a")
111   CMP_Y(>=, "aa", "a")
112   CMP_Y(>=, "b", "aa")
113   CMP_Y(>=, "bb", "aa")
114 
115   CMP_N(>, "a", "a")
116   CMP_N(>, "a", "b")
117   CMP_N(>, "a", "aa")
118   CMP_N(>, "aa", "b")
119   CMP_N(>, "aa", "bb")
120   CMP_Y(>, "b", "a")
121   CMP_Y(>, "aa", "a")
122   CMP_Y(>, "b", "aa")
123   CMP_Y(>, "bb", "aa")
124 
125   std::string x;
126   for (int i = 0; i < 256; i++) {
127     x += 'a';
128     std::string y = x;
129     CMP_Y(==, x, y);
130     for (int j = 0; j < i; j++) {
131       std::string z = x;
132       z[j] = 'b';       // Differs in position 'j'
133       CMP_N(==, x, z);
134     }
135   }
136 
137 #undef CMP_Y
138 #undef CMP_N
139 }
140 
TYPED_TEST(CommonStringPieceTest,CheckSTL)141 TYPED_TEST(CommonStringPieceTest, CheckSTL) {
142   std::basic_string<TypeParam> alphabet(
143       TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
144   std::basic_string<TypeParam> abc(TestFixture::as_string("abc"));
145   std::basic_string<TypeParam> xyz(TestFixture::as_string("xyz"));
146   std::basic_string<TypeParam> foobar(TestFixture::as_string("foobar"));
147 
148   std::basic_string_view<TypeParam> a(alphabet);
149   std::basic_string_view<TypeParam> b(abc);
150   std::basic_string_view<TypeParam> c(xyz);
151   std::basic_string_view<TypeParam> d(foobar);
152   std::basic_string_view<TypeParam> e;
153   std::basic_string<TypeParam> temp(TestFixture::as_string("123"));
154   temp += static_cast<TypeParam>(0);
155   temp += TestFixture::as_string("456");
156   std::basic_string_view<TypeParam> f(temp);
157 
158   ASSERT_EQ(a[6], static_cast<TypeParam>('g'));
159   ASSERT_EQ(b[0], static_cast<TypeParam>('a'));
160   ASSERT_EQ(c[2], static_cast<TypeParam>('z'));
161   ASSERT_EQ(f[3], static_cast<TypeParam>('\0'));
162   ASSERT_EQ(f[5], static_cast<TypeParam>('5'));
163 
164   ASSERT_EQ(*d.data(), static_cast<TypeParam>('f'));
165   ASSERT_EQ(d.data()[5], static_cast<TypeParam>('r'));
166   ASSERT_EQ(e.data(), nullptr);
167 
168   ASSERT_EQ(*a.begin(), static_cast<TypeParam>('a'));
169   ASSERT_EQ(*(b.begin() + 2), static_cast<TypeParam>('c'));
170   ASSERT_EQ(*(c.end() - 1), static_cast<TypeParam>('z'));
171 
172   ASSERT_EQ(*a.rbegin(), static_cast<TypeParam>('z'));
173   ASSERT_EQ(*(b.rbegin() + 2), static_cast<TypeParam>('a'));
174   ASSERT_EQ(*(c.rend() - 1), static_cast<TypeParam>('x'));
175   ASSERT_EQ(a.rbegin() + 26, a.rend());
176 
177   ASSERT_EQ(a.size(), 26U);
178   ASSERT_EQ(b.size(), 3U);
179   ASSERT_EQ(c.size(), 3U);
180   ASSERT_EQ(d.size(), 6U);
181   ASSERT_EQ(e.size(), 0U);
182   ASSERT_EQ(f.size(), 7U);
183 
184   ASSERT_TRUE(!d.empty());
185   ASSERT_TRUE(d.begin() != d.end());
186   ASSERT_EQ(d.begin() + 6, d.end());
187 
188   ASSERT_TRUE(e.empty());
189   ASSERT_EQ(e.begin(), e.end());
190 
191   d = std::basic_string_view<TypeParam>();
192   ASSERT_EQ(d.size(), 0U);
193   ASSERT_TRUE(d.empty());
194   ASSERT_EQ(d.data(), nullptr);
195   ASSERT_EQ(d.begin(), d.end());
196 
197   ASSERT_GE(a.max_size(), a.size());
198 }
199 
TYPED_TEST(CommonStringPieceTest,CheckFind)200 TYPED_TEST(CommonStringPieceTest, CheckFind) {
201   typedef std::basic_string_view<TypeParam> Piece;
202 
203   std::basic_string<TypeParam> alphabet(
204       TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
205   std::basic_string<TypeParam> abc(TestFixture::as_string("abc"));
206   std::basic_string<TypeParam> xyz(TestFixture::as_string("xyz"));
207   std::basic_string<TypeParam> foobar(TestFixture::as_string("foobar"));
208 
209   std::basic_string_view<TypeParam> a(alphabet);
210   std::basic_string_view<TypeParam> b(abc);
211   std::basic_string_view<TypeParam> c(xyz);
212   std::basic_string_view<TypeParam> d(foobar);
213 
214   d = Piece();
215   Piece e;
216   std::basic_string<TypeParam> temp(TestFixture::as_string("123"));
217   temp.push_back('\0');
218   temp += TestFixture::as_string("456");
219   Piece f(temp);
220 
221   TypeParam buf[4] = {'%', '%', '%', '%'};
222   ASSERT_EQ(a.copy(buf, 4), 4U);
223   ASSERT_EQ(buf[0], a[0]);
224   ASSERT_EQ(buf[1], a[1]);
225   ASSERT_EQ(buf[2], a[2]);
226   ASSERT_EQ(buf[3], a[3]);
227   ASSERT_EQ(a.copy(buf, 3, 7), 3U);
228   ASSERT_EQ(buf[0], a[7]);
229   ASSERT_EQ(buf[1], a[8]);
230   ASSERT_EQ(buf[2], a[9]);
231   ASSERT_EQ(buf[3], a[3]);
232   ASSERT_EQ(c.copy(buf, 99), 3U);
233   ASSERT_EQ(buf[0], c[0]);
234   ASSERT_EQ(buf[1], c[1]);
235   ASSERT_EQ(buf[2], c[2]);
236   ASSERT_EQ(buf[3], a[3]);
237 
238   ASSERT_EQ(Piece::npos, std::basic_string<TypeParam>::npos);
239 
240   ASSERT_EQ(a.find(b), 0U);
241   ASSERT_EQ(a.find(b, 1), Piece::npos);
242   ASSERT_EQ(a.find(c), 23U);
243   ASSERT_EQ(a.find(c, 9), 23U);
244   ASSERT_EQ(a.find(c, Piece::npos), Piece::npos);
245   ASSERT_EQ(b.find(c), Piece::npos);
246   ASSERT_EQ(b.find(c, Piece::npos), Piece::npos);
247   ASSERT_EQ(a.find(d), 0U);
248   ASSERT_EQ(a.find(e), 0U);
249   ASSERT_EQ(a.find(d, 12), 12U);
250   ASSERT_EQ(a.find(e, 17), 17U);
251   std::basic_string<TypeParam> not_found(
252       TestFixture::as_string("xx not found bb"));
253   Piece g(not_found);
254   ASSERT_EQ(a.find(g), Piece::npos);
255   // empty string nonsense
256   ASSERT_EQ(d.find(b), Piece::npos);
257   ASSERT_EQ(e.find(b), Piece::npos);
258   ASSERT_EQ(d.find(b, 4), Piece::npos);
259   ASSERT_EQ(e.find(b, 7), Piece::npos);
260 
261   size_t empty_search_pos =
262       std::basic_string<TypeParam>().find(std::basic_string<TypeParam>());
263   ASSERT_EQ(d.find(d), empty_search_pos);
264   ASSERT_EQ(d.find(e), empty_search_pos);
265   ASSERT_EQ(e.find(d), empty_search_pos);
266   ASSERT_EQ(e.find(e), empty_search_pos);
267   ASSERT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
268   ASSERT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
269   ASSERT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
270   ASSERT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
271 
272   constexpr TypeParam kNul = '\0';
273   ASSERT_EQ(a.find('a'), 0U);
274   ASSERT_EQ(a.find('c'), 2U);
275   ASSERT_EQ(a.find('z'), 25U);
276   ASSERT_EQ(a.find('$'), Piece::npos);
277   ASSERT_EQ(a.find(kNul), Piece::npos);
278   ASSERT_EQ(f.find(kNul), 3U);
279   ASSERT_EQ(f.find('3'), 2U);
280   ASSERT_EQ(f.find('5'), 5U);
281   ASSERT_EQ(g.find('o'), 4U);
282   ASSERT_EQ(g.find('o', 4), 4U);
283   ASSERT_EQ(g.find('o', 5), 8U);
284   ASSERT_EQ(a.find('b', 5), Piece::npos);
285   // empty string nonsense
286   ASSERT_EQ(d.find(kNul), Piece::npos);
287   ASSERT_EQ(e.find(kNul), Piece::npos);
288   ASSERT_EQ(d.find(kNul, 4), Piece::npos);
289   ASSERT_EQ(e.find(kNul, 7), Piece::npos);
290   ASSERT_EQ(d.find('x'), Piece::npos);
291   ASSERT_EQ(e.find('x'), Piece::npos);
292   ASSERT_EQ(d.find('x', 4), Piece::npos);
293   ASSERT_EQ(e.find('x', 7), Piece::npos);
294 
295   ASSERT_EQ(a.find(b.data(), 1, 0), 1U);
296   ASSERT_EQ(a.find(c.data(), 9, 0), 9U);
297   ASSERT_EQ(a.find(c.data(), Piece::npos, 0), Piece::npos);
298   ASSERT_EQ(b.find(c.data(), Piece::npos, 0), Piece::npos);
299   // empty string nonsense
300   ASSERT_EQ(d.find(b.data(), 4, 0), Piece::npos);
301   ASSERT_EQ(e.find(b.data(), 7, 0), Piece::npos);
302 
303   ASSERT_EQ(a.find(b.data(), 1), Piece::npos);
304   ASSERT_EQ(a.find(c.data(), 9), 23U);
305   ASSERT_EQ(a.find(c.data(), Piece::npos), Piece::npos);
306   ASSERT_EQ(b.find(c.data(), Piece::npos), Piece::npos);
307   // empty string nonsense
308   ASSERT_EQ(d.find(b.data(), 4), Piece::npos);
309   ASSERT_EQ(e.find(b.data(), 7), Piece::npos);
310 
311   ASSERT_EQ(a.rfind(b), 0U);
312   ASSERT_EQ(a.rfind(b, 1), 0U);
313   ASSERT_EQ(a.rfind(c), 23U);
314   ASSERT_EQ(a.rfind(c, 22U), Piece::npos);
315   ASSERT_EQ(a.rfind(c, 1U), Piece::npos);
316   ASSERT_EQ(a.rfind(c, 0U), Piece::npos);
317   ASSERT_EQ(b.rfind(c), Piece::npos);
318   ASSERT_EQ(b.rfind(c, 0U), Piece::npos);
319   ASSERT_EQ(a.rfind(d),
320             static_cast<size_t>(a.rfind(std::basic_string<TypeParam>())));
321   ASSERT_EQ(a.rfind(e), a.rfind(std::basic_string<TypeParam>()));
322   ASSERT_EQ(a.rfind(d),
323             static_cast<size_t>(std::basic_string<TypeParam>(a).rfind(
324                 std::basic_string<TypeParam>())));
325   ASSERT_EQ(a.rfind(e), std::basic_string<TypeParam>(a).rfind(
326                             std::basic_string<TypeParam>()));
327   ASSERT_EQ(a.rfind(d, 12), 12U);
328   ASSERT_EQ(a.rfind(e, 17), 17U);
329   ASSERT_EQ(a.rfind(g), Piece::npos);
330   ASSERT_EQ(d.rfind(b), Piece::npos);
331   ASSERT_EQ(e.rfind(b), Piece::npos);
332   ASSERT_EQ(d.rfind(b, 4), Piece::npos);
333   ASSERT_EQ(e.rfind(b, 7), Piece::npos);
334   // empty string nonsense
335   ASSERT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
336   ASSERT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
337   ASSERT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
338   ASSERT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
339   ASSERT_EQ(d.rfind(d), std::string().rfind(std::string()));
340   ASSERT_EQ(e.rfind(d), std::string().rfind(std::string()));
341   ASSERT_EQ(d.rfind(e), std::string().rfind(std::string()));
342   ASSERT_EQ(e.rfind(e), std::string().rfind(std::string()));
343 
344   ASSERT_EQ(g.rfind('o'), 8U);
345   ASSERT_EQ(g.rfind('q'), Piece::npos);
346   ASSERT_EQ(g.rfind('o', 8), 8U);
347   ASSERT_EQ(g.rfind('o', 7), 4U);
348   ASSERT_EQ(g.rfind('o', 3), Piece::npos);
349   ASSERT_EQ(f.rfind(kNul), 3U);
350   ASSERT_EQ(f.rfind(kNul, 12), 3U);
351   ASSERT_EQ(f.rfind('3'), 2U);
352   ASSERT_EQ(f.rfind('5'), 5U);
353   // empty string nonsense
354   ASSERT_EQ(d.rfind('o'), Piece::npos);
355   ASSERT_EQ(e.rfind('o'), Piece::npos);
356   ASSERT_EQ(d.rfind('o', 4), Piece::npos);
357   ASSERT_EQ(e.rfind('o', 7), Piece::npos);
358 
359   ASSERT_EQ(a.rfind(b.data(), 1, 0), 1U);
360   ASSERT_EQ(a.rfind(c.data(), 22U, 0), 22U);
361   ASSERT_EQ(a.rfind(c.data(), 1U, 0), 1U);
362   ASSERT_EQ(a.rfind(c.data(), 0U, 0), 0U);
363   ASSERT_EQ(b.rfind(c.data(), 0U, 0), 0U);
364   ASSERT_EQ(d.rfind(b.data(), 4, 0), 0U);
365   ASSERT_EQ(e.rfind(b.data(), 7, 0), 0U);
366 
367   std::basic_string<TypeParam> one_two_three_four(
368       TestFixture::as_string("one,two:three;four"));
369   std::basic_string<TypeParam> comma_colon(TestFixture::as_string(",:"));
370   ASSERT_EQ(3U, Piece(one_two_three_four).find_first_of(comma_colon));
371   ASSERT_EQ(a.find_first_of(b), 0U);
372   ASSERT_EQ(a.find_first_of(b, 0), 0U);
373   ASSERT_EQ(a.find_first_of(b, 1), 1U);
374   ASSERT_EQ(a.find_first_of(b, 2), 2U);
375   ASSERT_EQ(a.find_first_of(b, 3), Piece::npos);
376   ASSERT_EQ(a.find_first_of(c), 23U);
377   ASSERT_EQ(a.find_first_of(c, 23), 23U);
378   ASSERT_EQ(a.find_first_of(c, 24), 24U);
379   ASSERT_EQ(a.find_first_of(c, 25), 25U);
380   ASSERT_EQ(a.find_first_of(c, 26), Piece::npos);
381   ASSERT_EQ(g.find_first_of(b), 13U);
382   ASSERT_EQ(g.find_first_of(c), 0U);
383   ASSERT_EQ(a.find_first_of(f), Piece::npos);
384   ASSERT_EQ(f.find_first_of(a), Piece::npos);
385   // empty string nonsense
386   ASSERT_EQ(a.find_first_of(d), Piece::npos);
387   ASSERT_EQ(a.find_first_of(e), Piece::npos);
388   ASSERT_EQ(d.find_first_of(b), Piece::npos);
389   ASSERT_EQ(e.find_first_of(b), Piece::npos);
390   ASSERT_EQ(d.find_first_of(d), Piece::npos);
391   ASSERT_EQ(e.find_first_of(d), Piece::npos);
392   ASSERT_EQ(d.find_first_of(e), Piece::npos);
393   ASSERT_EQ(e.find_first_of(e), Piece::npos);
394 
395   ASSERT_EQ(a.find_first_not_of(b), 3U);
396   ASSERT_EQ(a.find_first_not_of(c), 0U);
397   ASSERT_EQ(b.find_first_not_of(a), Piece::npos);
398   ASSERT_EQ(c.find_first_not_of(a), Piece::npos);
399   ASSERT_EQ(f.find_first_not_of(a), 0U);
400   ASSERT_EQ(a.find_first_not_of(f), 0U);
401   ASSERT_EQ(a.find_first_not_of(d), 0U);
402   ASSERT_EQ(a.find_first_not_of(e), 0U);
403   ASSERT_EQ(a.find_first_not_of(d, 1), 1U);
404   ASSERT_EQ(a.find_first_not_of(e, 1), 1U);
405   ASSERT_EQ(a.find_first_not_of(d, a.size()), Piece::npos);
406   ASSERT_EQ(a.find_first_not_of(e, a.size()), Piece::npos);
407   // empty string nonsense
408   ASSERT_EQ(d.find_first_not_of(a), Piece::npos);
409   ASSERT_EQ(e.find_first_not_of(a), Piece::npos);
410   ASSERT_EQ(d.find_first_not_of(d), Piece::npos);
411   ASSERT_EQ(e.find_first_not_of(d), Piece::npos);
412   ASSERT_EQ(d.find_first_not_of(e), Piece::npos);
413   ASSERT_EQ(e.find_first_not_of(e), Piece::npos);
414 
415   std::basic_string<TypeParam> equals(TestFixture::as_string("===="));
416   Piece h(equals);
417   ASSERT_EQ(h.find_first_not_of('='), Piece::npos);
418   ASSERT_EQ(h.find_first_not_of('=', 3), Piece::npos);
419   ASSERT_EQ(h.find_first_not_of(kNul), 0U);
420   ASSERT_EQ(g.find_first_not_of('x'), 2U);
421   ASSERT_EQ(f.find_first_not_of(kNul), 0U);
422   ASSERT_EQ(f.find_first_not_of(kNul, 3), 4U);
423   ASSERT_EQ(f.find_first_not_of(kNul, 2), 2U);
424   // empty string nonsense
425   ASSERT_EQ(d.find_first_not_of('x'), Piece::npos);
426   ASSERT_EQ(e.find_first_not_of('x'), Piece::npos);
427   ASSERT_EQ(d.find_first_not_of(kNul), Piece::npos);
428   ASSERT_EQ(e.find_first_not_of(kNul), Piece::npos);
429 
430   //  Piece g("xx not found bb");
431   std::basic_string<TypeParam> fifty_six(TestFixture::as_string("56"));
432   Piece i(fifty_six);
433   ASSERT_EQ(h.find_last_of(a), Piece::npos);
434   ASSERT_EQ(g.find_last_of(a), g.size()-1);
435   ASSERT_EQ(a.find_last_of(b), 2U);
436   ASSERT_EQ(a.find_last_of(c), a.size()-1);
437   ASSERT_EQ(f.find_last_of(i), 6U);
438   ASSERT_EQ(a.find_last_of('a'), 0U);
439   ASSERT_EQ(a.find_last_of('b'), 1U);
440   ASSERT_EQ(a.find_last_of('z'), 25U);
441   ASSERT_EQ(a.find_last_of('a', 5), 0U);
442   ASSERT_EQ(a.find_last_of('b', 5), 1U);
443   ASSERT_EQ(a.find_last_of('b', 0), Piece::npos);
444   ASSERT_EQ(a.find_last_of('z', 25), 25U);
445   ASSERT_EQ(a.find_last_of('z', 24), Piece::npos);
446   ASSERT_EQ(f.find_last_of(i, 5), 5U);
447   ASSERT_EQ(f.find_last_of(i, 6), 6U);
448   ASSERT_EQ(f.find_last_of(a, 4), Piece::npos);
449   // empty string nonsense
450   ASSERT_EQ(f.find_last_of(d), Piece::npos);
451   ASSERT_EQ(f.find_last_of(e), Piece::npos);
452   ASSERT_EQ(f.find_last_of(d, 4), Piece::npos);
453   ASSERT_EQ(f.find_last_of(e, 4), Piece::npos);
454   ASSERT_EQ(d.find_last_of(d), Piece::npos);
455   ASSERT_EQ(d.find_last_of(e), Piece::npos);
456   ASSERT_EQ(e.find_last_of(d), Piece::npos);
457   ASSERT_EQ(e.find_last_of(e), Piece::npos);
458   ASSERT_EQ(d.find_last_of(f), Piece::npos);
459   ASSERT_EQ(e.find_last_of(f), Piece::npos);
460   ASSERT_EQ(d.find_last_of(d, 4), Piece::npos);
461   ASSERT_EQ(d.find_last_of(e, 4), Piece::npos);
462   ASSERT_EQ(e.find_last_of(d, 4), Piece::npos);
463   ASSERT_EQ(e.find_last_of(e, 4), Piece::npos);
464   ASSERT_EQ(d.find_last_of(f, 4), Piece::npos);
465   ASSERT_EQ(e.find_last_of(f, 4), Piece::npos);
466 
467   ASSERT_EQ(a.find_last_not_of(b), a.size()-1);
468   ASSERT_EQ(a.find_last_not_of(c), 22U);
469   ASSERT_EQ(b.find_last_not_of(a), Piece::npos);
470   ASSERT_EQ(b.find_last_not_of(b), Piece::npos);
471   ASSERT_EQ(f.find_last_not_of(i), 4U);
472   ASSERT_EQ(a.find_last_not_of(c, 24), 22U);
473   ASSERT_EQ(a.find_last_not_of(b, 3), 3U);
474   ASSERT_EQ(a.find_last_not_of(b, 2), Piece::npos);
475   // empty string nonsense
476   ASSERT_EQ(f.find_last_not_of(d), f.size()-1);
477   ASSERT_EQ(f.find_last_not_of(e), f.size()-1);
478   ASSERT_EQ(f.find_last_not_of(d, 4), 4U);
479   ASSERT_EQ(f.find_last_not_of(e, 4), 4U);
480   ASSERT_EQ(d.find_last_not_of(d), Piece::npos);
481   ASSERT_EQ(d.find_last_not_of(e), Piece::npos);
482   ASSERT_EQ(e.find_last_not_of(d), Piece::npos);
483   ASSERT_EQ(e.find_last_not_of(e), Piece::npos);
484   ASSERT_EQ(d.find_last_not_of(f), Piece::npos);
485   ASSERT_EQ(e.find_last_not_of(f), Piece::npos);
486   ASSERT_EQ(d.find_last_not_of(d, 4), Piece::npos);
487   ASSERT_EQ(d.find_last_not_of(e, 4), Piece::npos);
488   ASSERT_EQ(e.find_last_not_of(d, 4), Piece::npos);
489   ASSERT_EQ(e.find_last_not_of(e, 4), Piece::npos);
490   ASSERT_EQ(d.find_last_not_of(f, 4), Piece::npos);
491   ASSERT_EQ(e.find_last_not_of(f, 4), Piece::npos);
492 
493   ASSERT_EQ(h.find_last_not_of('x'), h.size() - 1);
494   ASSERT_EQ(h.find_last_not_of('='), Piece::npos);
495   ASSERT_EQ(b.find_last_not_of('c'), 1U);
496   ASSERT_EQ(h.find_last_not_of('x', 2), 2U);
497   ASSERT_EQ(h.find_last_not_of('=', 2), Piece::npos);
498   ASSERT_EQ(b.find_last_not_of('b', 1), 0U);
499   // empty string nonsense
500   ASSERT_EQ(d.find_last_not_of('x'), Piece::npos);
501   ASSERT_EQ(e.find_last_not_of('x'), Piece::npos);
502   ASSERT_EQ(d.find_last_not_of(kNul), Piece::npos);
503   ASSERT_EQ(e.find_last_not_of(kNul), Piece::npos);
504 
505   ASSERT_EQ(a.substr(0, 3), b);
506   ASSERT_EQ(a.substr(23), c);
507   ASSERT_EQ(a.substr(23, 3), c);
508   ASSERT_EQ(a.substr(23, 99), c);
509   ASSERT_EQ(a.substr(), a);
510   ASSERT_EQ(a.substr(0), a);
511   ASSERT_EQ(a.substr(3, 2), TestFixture::as_string("de"));
512   ASSERT_EQ(d.substr(0, 99), e);
513 }
514 
TYPED_TEST(CommonStringPieceTest,CheckCustom)515 TYPED_TEST(CommonStringPieceTest, CheckCustom) {
516   std::basic_string<TypeParam> foobar(TestFixture::as_string("foobar"));
517   std::basic_string_view<TypeParam> a(foobar);
518   std::basic_string<TypeParam> s1(TestFixture::as_string("123"));
519   s1 += static_cast<TypeParam>('\0');
520   s1 += TestFixture::as_string("456");
521   [[maybe_unused]] std::basic_string_view<TypeParam> b(s1);
522   std::basic_string_view<TypeParam> e;
523   std::basic_string<TypeParam> s2;
524 
525   // remove_prefix
526   std::basic_string_view<TypeParam> c(a);
527   c.remove_prefix(3);
528   ASSERT_EQ(c, TestFixture::as_string("bar"));
529   c = a;
530   c.remove_prefix(0);
531   ASSERT_EQ(c, a);
532   c.remove_prefix(c.size());
533   ASSERT_EQ(c, e);
534 
535   // remove_suffix
536   c = a;
537   c.remove_suffix(3);
538   ASSERT_EQ(c, TestFixture::as_string("foo"));
539   c = a;
540   c.remove_suffix(0);
541   ASSERT_EQ(c, a);
542   c.remove_suffix(c.size());
543   ASSERT_EQ(c, e);
544 
545   // assignment
546   c = foobar.c_str();
547   ASSERT_EQ(c, a);
548   c = {foobar.c_str(), 6};
549   ASSERT_EQ(c, a);
550   c = {foobar.c_str(), 0};
551   ASSERT_EQ(c, e);
552   c = {foobar.c_str(), 7};  // Note, has an embedded NULL
553   ASSERT_NE(c, a);
554 
555   // operator STRING_TYPE()
556   std::basic_string<TypeParam> s5(std::basic_string<TypeParam>(a).c_str(),
557                                   7);  // Note, has an embedded NULL
558   ASSERT_EQ(c, s5);
559   std::basic_string<TypeParam> s6(e);
560   ASSERT_TRUE(s6.empty());
561 }
562 
TEST(StringPieceTest,CheckCustom)563 TEST(StringPieceTest, CheckCustom) {
564   StringPiece a("foobar");
565   std::string s1("123");
566   s1 += '\0';
567   s1 += "456";
568   [[maybe_unused]] StringPiece b(s1);
569   StringPiece e;
570   std::string s2;
571 
572   StringPiece c;
573   c = {"foobar", 6};
574   ASSERT_EQ(c, a);
575   c = {"foobar", 0};
576   ASSERT_EQ(c, e);
577   c = {"foobar", 7};
578   ASSERT_NE(c, a);
579 }
580 
TYPED_TEST(CommonStringPieceTest,CheckNULL)581 TYPED_TEST(CommonStringPieceTest, CheckNULL) {
582   std::basic_string_view<TypeParam> s;
583   ASSERT_EQ(s.data(), nullptr);
584   ASSERT_EQ(s.size(), 0U);
585 
586   std::basic_string<TypeParam> str(s);
587   ASSERT_EQ(str.length(), 0U);
588   ASSERT_EQ(str, std::basic_string<TypeParam>());
589 }
590 
TYPED_TEST(CommonStringPieceTest,CheckComparisons2)591 TYPED_TEST(CommonStringPieceTest, CheckComparisons2) {
592   std::basic_string<TypeParam> alphabet(
593       TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
594   std::basic_string<TypeParam> alphabet_z(
595       TestFixture::as_string("abcdefghijklmnopqrstuvwxyzz"));
596   std::basic_string<TypeParam> alphabet_y(
597       TestFixture::as_string("abcdefghijklmnopqrstuvwxyy"));
598   std::basic_string_view<TypeParam> abc(alphabet);
599 
600   // check comparison operations on strings longer than 4 bytes.
601   ASSERT_EQ(abc, std::basic_string_view<TypeParam>(alphabet));
602   ASSERT_EQ(abc.compare(std::basic_string_view<TypeParam>(alphabet)), 0);
603 
604   ASSERT_TRUE(abc < std::basic_string_view<TypeParam>(alphabet_z));
605   ASSERT_LT(abc.compare(std::basic_string_view<TypeParam>(alphabet_z)), 0);
606 
607   ASSERT_TRUE(abc > std::basic_string_view<TypeParam>(alphabet_y));
608   ASSERT_GT(abc.compare(std::basic_string_view<TypeParam>(alphabet_y)), 0);
609 }
610 
TYPED_TEST(CommonStringPieceTest,StringCompareNotAmbiguous)611 TYPED_TEST(CommonStringPieceTest, StringCompareNotAmbiguous) {
612   ASSERT_TRUE(TestFixture::as_string("hello").c_str() ==
613               TestFixture::as_string("hello"));
614   ASSERT_TRUE(TestFixture::as_string("hello").c_str() <
615               TestFixture::as_string("world"));
616 }
617 
TYPED_TEST(CommonStringPieceTest,HeterogenousStringPieceEquals)618 TYPED_TEST(CommonStringPieceTest, HeterogenousStringPieceEquals) {
619   std::basic_string<TypeParam> hello(TestFixture::as_string("hello"));
620 
621   ASSERT_EQ(std::basic_string_view<TypeParam>(hello), hello);
622   ASSERT_EQ(hello.c_str(), std::basic_string_view<TypeParam>(hello));
623 }
624 
625 // std::u16string-specific stuff
TEST(StringPiece16Test,CheckSTL)626 TEST(StringPiece16Test, CheckSTL) {
627   // Check some non-ascii characters.
628   std::u16string fifth(u"123");
629   fifth.push_back(0x0000);
630   fifth.push_back(0xd8c5);
631   fifth.push_back(0xdffe);
632   StringPiece16 f(fifth);
633 
634   ASSERT_EQ(f[3], '\0');
635   ASSERT_EQ(f[5], 0xdffe);
636 
637   ASSERT_EQ(f.size(), 6U);
638 }
639 
TEST(StringPiece16Test,CheckConversion)640 TEST(StringPiece16Test, CheckConversion) {
641   // Make sure that we can convert from UTF8 to UTF16 and back. We use a
642   // character (G clef) outside the BMP to test this.
643   const char* kTest = "\U0001D11E";
644   ASSERT_EQ(UTF16ToUTF8(UTF8ToUTF16(kTest)), kTest);
645 }
646 
TYPED_TEST(CommonStringPieceTest,CheckConstructors)647 TYPED_TEST(CommonStringPieceTest, CheckConstructors) {
648   std::basic_string<TypeParam> str(TestFixture::as_string("hello world"));
649   std::basic_string<TypeParam> empty;
650 
651   ASSERT_EQ(str, std::basic_string_view<TypeParam>(str));
652   ASSERT_EQ(str, std::basic_string_view<TypeParam>(str.c_str()));
653   ASSERT_TRUE(TestFixture::as_string("hello") ==
654               std::basic_string_view<TypeParam>(str.c_str(), 5));
655   ASSERT_EQ(
656       empty,
657       std::basic_string_view<TypeParam>(
658           str.c_str(),
659           static_cast<typename std::basic_string_view<TypeParam>::size_type>(
660               0)));
661   ASSERT_EQ(empty, std::basic_string_view<TypeParam>());
662   ASSERT_TRUE(
663       empty ==
664       std::basic_string_view<TypeParam>(
665           nullptr,
666           static_cast<typename std::basic_string_view<TypeParam>::size_type>(
667               0)));
668   ASSERT_EQ(empty, std::basic_string_view<TypeParam>());
669   ASSERT_EQ(empty, std::basic_string_view<TypeParam>(empty));
670 }
671 
TEST(StringPieceTest,ConstexprCtor)672 TEST(StringPieceTest, ConstexprCtor) {
673   {
674     constexpr StringPiece piece;
675     std::ignore = piece;
676   }
677 
678   {
679     constexpr StringPiece piece("abc");
680     std::ignore = piece;
681   }
682 
683   {
684     constexpr StringPiece piece("abc", 2);
685     std::ignore = piece;
686   }
687 }
688 
689 // Chromium development assumes StringPiece (which is std::string_view) is
690 // implemented with an STL that enables hardening checks. We treat bugs that
691 // trigger one of these conditions as functional rather than security bugs. If
692 // this test fails on some embedder, it should not be disabled. Instead, the
693 // embedder should fix their STL or build configuration to enable corresponding
694 // hardening checks.
695 //
696 // See https://chromium.googlesource.com/chromium/src/+/main/docs/security/faq.md#indexing-a-container-out-of-bounds-hits-a-libcpp_verbose_abort_is-this-a-security-bug
TEST(StringPieceTest,OutOfBoundsDeath)697 TEST(StringPieceTest, OutOfBoundsDeath) {
698   {
699     constexpr StringPiece piece;
700     ASSERT_DEATH_IF_SUPPORTED(piece[0], "");
701   }
702 
703   {
704     constexpr StringPiece piece;
705     ASSERT_DEATH_IF_SUPPORTED(piece.front(), "");
706   }
707 
708   {
709     constexpr StringPiece piece;
710     ASSERT_DEATH_IF_SUPPORTED(piece.back(), "");
711   }
712 
713   {
714     StringPiece piece;
715     ASSERT_DEATH_IF_SUPPORTED(piece.remove_suffix(1), "");
716   }
717 
718   {
719     StringPiece piece;
720     ASSERT_DEATH_IF_SUPPORTED(piece.remove_prefix(1), "");
721   }
722 
723   {
724     StringPiece piece;
725     ASSERT_DEATH_IF_SUPPORTED(piece.copy(nullptr, 0, 1), "");
726   }
727 
728   {
729     StringPiece piece;
730     ASSERT_DEATH_IF_SUPPORTED(piece.substr(1), "");
731   }
732 }
733 
TEST(StringPieceTest,InvalidLengthDeath)734 TEST(StringPieceTest, InvalidLengthDeath) {
735   int length = -1;
736   ASSERT_DEATH_IF_SUPPORTED({ StringPiece piece("hello", length); }, "");
737 }
738 
TEST(StringPieceTest,ConstexprData)739 TEST(StringPieceTest, ConstexprData) {
740   {
741     constexpr StringPiece piece;
742     static_assert(piece.data() == nullptr, "");
743   }
744 
745   {
746     constexpr StringPiece piece("abc");
747     static_assert(piece.data()[0] == 'a', "");
748     static_assert(piece.data()[1] == 'b', "");
749     static_assert(piece.data()[2] == 'c', "");
750   }
751 
752   {
753     constexpr StringPiece piece("def", 2);
754     static_assert(piece.data()[0] == 'd', "");
755     static_assert(piece.data()[1] == 'e', "");
756   }
757 }
758 
TEST(StringPieceTest,ConstexprSize)759 TEST(StringPieceTest, ConstexprSize) {
760   {
761     constexpr StringPiece piece;
762     static_assert(piece.size() == 0, "");
763   }
764 
765   {
766     constexpr StringPiece piece("abc");
767     static_assert(piece.size() == 3, "");
768   }
769 
770   {
771     constexpr StringPiece piece("def", 2);
772     static_assert(piece.size() == 2, "");
773   }
774 }
775 
TEST(StringPieceTest,ConstexprFront)776 TEST(StringPieceTest, ConstexprFront) {
777   static_assert(StringPiece("abc").front() == 'a', "");
778 }
779 
TEST(StringPieceTest,ConstexprBack)780 TEST(StringPieceTest, ConstexprBack) {
781   static_assert(StringPiece("abc").back() == 'c', "");
782 }
783 
TEST(StringPieceTest,Compare)784 TEST(StringPieceTest, Compare) {
785   constexpr StringPiece piece = "def";
786 
787   static_assert(piece.compare("ab") == 1, "");
788   static_assert(piece.compare("abc") == 1, "");
789   static_assert(piece.compare("abcd") == 1, "");
790   static_assert(piece.compare("de") == 1, "");
791   static_assert(piece.compare("def") == 0, "");
792   static_assert(piece.compare("defg") == -1, "");
793   static_assert(piece.compare("gh") == -1, "");
794   static_assert(piece.compare("ghi") == -1, "");
795   static_assert(piece.compare("ghij") == -1, "");
796 
797   static_assert(piece.compare(0, 0, "") == 0, "");
798   static_assert(piece.compare(0, 1, "d") == 0, "");
799   static_assert(piece.compare(0, 2, "de") == 0, "");
800   static_assert(piece.compare(0, 3, "def") == 0, "");
801   static_assert(piece.compare(1, 0, "") == 0, "");
802   static_assert(piece.compare(1, 1, "e") == 0, "");
803   static_assert(piece.compare(1, 2, "ef") == 0, "");
804   static_assert(piece.compare(1, 3, "ef") == 0, "");
805   static_assert(piece.compare(2, 0, "") == 0, "");
806   static_assert(piece.compare(2, 1, "f") == 0, "");
807   static_assert(piece.compare(2, 2, "f") == 0, "");
808   static_assert(piece.compare(2, 3, "f") == 0, "");
809   static_assert(piece.compare(3, 0, "") == 0, "");
810   static_assert(piece.compare(3, 1, "") == 0, "");
811   static_assert(piece.compare(3, 2, "") == 0, "");
812   static_assert(piece.compare(3, 3, "") == 0, "");
813 
814   static_assert(piece.compare(0, 0, "def", 0) == 0, "");
815   static_assert(piece.compare(0, 1, "def", 1) == 0, "");
816   static_assert(piece.compare(0, 2, "def", 2) == 0, "");
817   static_assert(piece.compare(0, 3, "def", 3) == 0, "");
818   static_assert(piece.compare(1, 0, "ef", 0) == 0, "");
819   static_assert(piece.compare(1, 1, "ef", 1) == 0, "");
820   static_assert(piece.compare(1, 2, "ef", 2) == 0, "");
821   static_assert(piece.compare(1, 3, "ef", 2) == 0, "");
822   static_assert(piece.compare(2, 0, "f", 0) == 0, "");
823   static_assert(piece.compare(2, 1, "f", 1) == 0, "");
824   static_assert(piece.compare(2, 2, "f", 1) == 0, "");
825   static_assert(piece.compare(2, 3, "f", 1) == 0, "");
826   static_assert(piece.compare(3, 0, "", 0) == 0, "");
827   static_assert(piece.compare(3, 1, "", 0) == 0, "");
828   static_assert(piece.compare(3, 2, "", 0) == 0, "");
829   static_assert(piece.compare(3, 3, "", 0) == 0, "");
830 
831   static_assert(piece.compare(0, 0, "def", 0, 0) == 0, "");
832   static_assert(piece.compare(0, 1, "def", 0, 1) == 0, "");
833   static_assert(piece.compare(0, 2, "def", 0, 2) == 0, "");
834   static_assert(piece.compare(0, 3, "def", 0, 3) == 0, "");
835   static_assert(piece.compare(1, 0, "def", 1, 0) == 0, "");
836   static_assert(piece.compare(1, 1, "def", 1, 1) == 0, "");
837   static_assert(piece.compare(1, 2, "def", 1, 2) == 0, "");
838   static_assert(piece.compare(1, 3, "def", 1, 3) == 0, "");
839   static_assert(piece.compare(2, 0, "def", 2, 0) == 0, "");
840   static_assert(piece.compare(2, 1, "def", 2, 1) == 0, "");
841   static_assert(piece.compare(2, 2, "def", 2, 2) == 0, "");
842   static_assert(piece.compare(2, 3, "def", 2, 3) == 0, "");
843   static_assert(piece.compare(3, 0, "def", 3, 0) == 0, "");
844   static_assert(piece.compare(3, 1, "def", 3, 1) == 0, "");
845   static_assert(piece.compare(3, 2, "def", 3, 2) == 0, "");
846   static_assert(piece.compare(3, 3, "def", 3, 3) == 0, "");
847 }
848 
TEST(StringPieceTest,Substr)849 TEST(StringPieceTest, Substr) {
850   constexpr StringPiece piece = "abcdefghijklmnopqrstuvwxyz";
851 
852   static_assert(piece.substr(0, 2) == "ab", "");
853   static_assert(piece.substr(0, 3) == "abc", "");
854   static_assert(piece.substr(0, 4) == "abcd", "");
855   static_assert(piece.substr(3, 2) == "de", "");
856   static_assert(piece.substr(3, 3) == "def", "");
857   static_assert(piece.substr(23) == "xyz", "");
858   static_assert(piece.substr(23, 3) == "xyz", "");
859   static_assert(piece.substr(23, 99) == "xyz", "");
860   static_assert(piece.substr() == piece, "");
861   static_assert(piece.substr(0) == piece, "");
862   static_assert(piece.substr(0, 99) == piece, "");
863 }
864 
TEST(StringPieceTest,Find)865 TEST(StringPieceTest, Find) {
866   constexpr StringPiece foobar("foobar", 6);
867   constexpr StringPiece foo = foobar.substr(0, 3);
868   constexpr StringPiece bar = foobar.substr(3);
869 
870   // find
871   static_assert(foobar.find(bar, 0) == 3, "");
872   static_assert(foobar.find('o', 0) == 1, "");
873   static_assert(foobar.find("ox", 0, 1) == 1, "");
874   static_assert(foobar.find("ox", 0) == StringPiece::npos, "");
875 
876   // rfind
877   static_assert(foobar.rfind(bar, 5) == 3, "");
878   static_assert(foobar.rfind('o', 5) == 2, "");
879   static_assert(foobar.rfind("ox", 5, 1) == 2, "");
880   static_assert(foobar.rfind("ox", 5) == StringPiece::npos, "");
881 
882   // find_first_of
883   static_assert(foobar.find_first_of(foo, 2) == 2, "");
884   static_assert(foobar.find_first_of('o', 2) == 2, "");
885   static_assert(foobar.find_first_of("ox", 2, 2) == 2, "");
886   static_assert(foobar.find_first_of("ox", 2) == 2, "");
887 
888   // find_last_of
889   static_assert(foobar.find_last_of(foo, 5) == 2, "");
890   static_assert(foobar.find_last_of('o', 5) == 2, "");
891   static_assert(foobar.find_last_of("ox", 5, 2) == 2, "");
892   static_assert(foobar.find_last_of("ox", 5) == 2, "");
893 
894   // find_first_not_of
895   static_assert(foobar.find_first_not_of(foo, 2) == 3, "");
896   static_assert(foobar.find_first_not_of('o', 2) == 3, "");
897   static_assert(foobar.find_first_not_of("ox", 2, 2) == 3, "");
898   static_assert(foobar.find_first_not_of("ox", 2) == 3, "");
899 
900   // find_last_not_of
901   static_assert(foobar.find_last_not_of(bar, 5) == 2, "");
902   static_assert(foobar.find_last_not_of('a', 4) == 3, "");
903   static_assert(foobar.find_last_not_of("ox", 2, 2) == 0, "");
904   static_assert(foobar.find_last_not_of("ox", 2) == 0, "");
905 }
906 
907 // Test that `base::StringPiece` and `std::string_view` are interoperable.
TEST(StringPieceTest,StringPieceToStringView)908 TEST(StringPieceTest, StringPieceToStringView) {
909   constexpr StringPiece kPiece = "foo";
910   constexpr std::string_view kView = kPiece;
911   static_assert(kPiece.data() == kView.data());
912   static_assert(kPiece.size() == kView.size());
913 }
914 
TEST(StringPieceTest,StringViewToStringPiece)915 TEST(StringPieceTest, StringViewToStringPiece) {
916   constexpr std::string_view kView = "bar";
917   constexpr StringPiece kPiece = kView;
918   static_assert(kView.data() == kPiece.data());
919   static_assert(kView.size() == kPiece.size());
920 }
921 
922 }  // namespace base
923