Roll WPR. (Remove old WPR code. Make WPR cert file name unique.)
[chromium-blink-merge.git] / base / strings / string_piece_unittest.cc
blob53366036d6fb0ba1c46d744a2a90457c833705dc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/strings/string16.h"
8 #include "base/strings/string_piece.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
14 template <typename T>
15 class CommonStringPieceTest : public ::testing::Test {
16 public:
17 static const T as_string(const char* input) {
18 return T(input);
20 static const T& as_string(const T& input) {
21 return input;
25 template <>
26 class CommonStringPieceTest<string16> : public ::testing::Test {
27 public:
28 static const string16 as_string(const char* input) {
29 return ASCIIToUTF16(input);
31 static const string16 as_string(const std::string& input) {
32 return ASCIIToUTF16(input);
36 typedef ::testing::Types<std::string, string16> SupportedStringTypes;
38 TYPED_TEST_CASE(CommonStringPieceTest, SupportedStringTypes);
40 TYPED_TEST(CommonStringPieceTest, CheckComparisonOperators) {
41 #define CMP_Y(op, x, y) \
42 { \
43 TypeParam lhs(TestFixture::as_string(x)); \
44 TypeParam rhs(TestFixture::as_string(y)); \
45 ASSERT_TRUE( (BasicStringPiece<TypeParam>((lhs.c_str())) op \
46 BasicStringPiece<TypeParam>((rhs.c_str())))); \
47 ASSERT_TRUE( (BasicStringPiece<TypeParam>((lhs.c_str())).compare( \
48 BasicStringPiece<TypeParam>((rhs.c_str()))) op 0)); \
51 #define CMP_N(op, x, y) \
52 { \
53 TypeParam lhs(TestFixture::as_string(x)); \
54 TypeParam rhs(TestFixture::as_string(y)); \
55 ASSERT_FALSE( (BasicStringPiece<TypeParam>((lhs.c_str())) op \
56 BasicStringPiece<TypeParam>((rhs.c_str())))); \
57 ASSERT_FALSE( (BasicStringPiece<TypeParam>((lhs.c_str())).compare( \
58 BasicStringPiece<TypeParam>((rhs.c_str()))) op 0)); \
61 CMP_Y(==, "", "");
62 CMP_Y(==, "a", "a");
63 CMP_Y(==, "aa", "aa");
64 CMP_N(==, "a", "");
65 CMP_N(==, "", "a");
66 CMP_N(==, "a", "b");
67 CMP_N(==, "a", "aa");
68 CMP_N(==, "aa", "a");
70 CMP_N(!=, "", "");
71 CMP_N(!=, "a", "a");
72 CMP_N(!=, "aa", "aa");
73 CMP_Y(!=, "a", "");
74 CMP_Y(!=, "", "a");
75 CMP_Y(!=, "a", "b");
76 CMP_Y(!=, "a", "aa");
77 CMP_Y(!=, "aa", "a");
79 CMP_Y(<, "a", "b");
80 CMP_Y(<, "a", "aa");
81 CMP_Y(<, "aa", "b");
82 CMP_Y(<, "aa", "bb");
83 CMP_N(<, "a", "a");
84 CMP_N(<, "b", "a");
85 CMP_N(<, "aa", "a");
86 CMP_N(<, "b", "aa");
87 CMP_N(<, "bb", "aa");
89 CMP_Y(<=, "a", "a");
90 CMP_Y(<=, "a", "b");
91 CMP_Y(<=, "a", "aa");
92 CMP_Y(<=, "aa", "b");
93 CMP_Y(<=, "aa", "bb");
94 CMP_N(<=, "b", "a");
95 CMP_N(<=, "aa", "a");
96 CMP_N(<=, "b", "aa");
97 CMP_N(<=, "bb", "aa");
99 CMP_N(>=, "a", "b");
100 CMP_N(>=, "a", "aa");
101 CMP_N(>=, "aa", "b");
102 CMP_N(>=, "aa", "bb");
103 CMP_Y(>=, "a", "a");
104 CMP_Y(>=, "b", "a");
105 CMP_Y(>=, "aa", "a");
106 CMP_Y(>=, "b", "aa");
107 CMP_Y(>=, "bb", "aa");
109 CMP_N(>, "a", "a");
110 CMP_N(>, "a", "b");
111 CMP_N(>, "a", "aa");
112 CMP_N(>, "aa", "b");
113 CMP_N(>, "aa", "bb");
114 CMP_Y(>, "b", "a");
115 CMP_Y(>, "aa", "a");
116 CMP_Y(>, "b", "aa");
117 CMP_Y(>, "bb", "aa");
119 std::string x;
120 for (int i = 0; i < 256; i++) {
121 x += 'a';
122 std::string y = x;
123 CMP_Y(==, x, y);
124 for (int j = 0; j < i; j++) {
125 std::string z = x;
126 z[j] = 'b'; // Differs in position 'j'
127 CMP_N(==, x, z);
131 #undef CMP_Y
132 #undef CMP_N
135 TYPED_TEST(CommonStringPieceTest, CheckSTL) {
136 TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
137 TypeParam abc(TestFixture::as_string("abc"));
138 TypeParam xyz(TestFixture::as_string("xyz"));
139 TypeParam foobar(TestFixture::as_string("foobar"));
141 BasicStringPiece<TypeParam> a(alphabet);
142 BasicStringPiece<TypeParam> b(abc);
143 BasicStringPiece<TypeParam> c(xyz);
144 BasicStringPiece<TypeParam> d(foobar);
145 BasicStringPiece<TypeParam> e;
146 TypeParam temp(TestFixture::as_string("123"));
147 temp += static_cast<typename TypeParam::value_type>(0);
148 temp += TestFixture::as_string("456");
149 BasicStringPiece<TypeParam> f(temp);
151 ASSERT_EQ(a[6], static_cast<typename TypeParam::value_type>('g'));
152 ASSERT_EQ(b[0], static_cast<typename TypeParam::value_type>('a'));
153 ASSERT_EQ(c[2], static_cast<typename TypeParam::value_type>('z'));
154 ASSERT_EQ(f[3], static_cast<typename TypeParam::value_type>('\0'));
155 ASSERT_EQ(f[5], static_cast<typename TypeParam::value_type>('5'));
157 ASSERT_EQ(*d.data(), static_cast<typename TypeParam::value_type>('f'));
158 ASSERT_EQ(d.data()[5], static_cast<typename TypeParam::value_type>('r'));
159 ASSERT_TRUE(e.data() == NULL);
161 ASSERT_EQ(*a.begin(), static_cast<typename TypeParam::value_type>('a'));
162 ASSERT_EQ(*(b.begin() + 2), static_cast<typename TypeParam::value_type>('c'));
163 ASSERT_EQ(*(c.end() - 1), static_cast<typename TypeParam::value_type>('z'));
165 ASSERT_EQ(*a.rbegin(), static_cast<typename TypeParam::value_type>('z'));
166 ASSERT_EQ(*(b.rbegin() + 2),
167 static_cast<typename TypeParam::value_type>('a'));
168 ASSERT_EQ(*(c.rend() - 1), static_cast<typename TypeParam::value_type>('x'));
169 ASSERT_TRUE(a.rbegin() + 26 == a.rend());
171 ASSERT_EQ(a.size(), 26U);
172 ASSERT_EQ(b.size(), 3U);
173 ASSERT_EQ(c.size(), 3U);
174 ASSERT_EQ(d.size(), 6U);
175 ASSERT_EQ(e.size(), 0U);
176 ASSERT_EQ(f.size(), 7U);
178 ASSERT_TRUE(!d.empty());
179 ASSERT_TRUE(d.begin() != d.end());
180 ASSERT_TRUE(d.begin() + 6 == d.end());
182 ASSERT_TRUE(e.empty());
183 ASSERT_TRUE(e.begin() == e.end());
185 d.clear();
186 ASSERT_EQ(d.size(), 0U);
187 ASSERT_TRUE(d.empty());
188 ASSERT_TRUE(d.data() == NULL);
189 ASSERT_TRUE(d.begin() == d.end());
191 ASSERT_GE(a.max_size(), a.capacity());
192 ASSERT_GE(a.capacity(), a.size());
195 TYPED_TEST(CommonStringPieceTest, CheckFind) {
196 typedef BasicStringPiece<TypeParam> Piece;
198 TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
199 TypeParam abc(TestFixture::as_string("abc"));
200 TypeParam xyz(TestFixture::as_string("xyz"));
201 TypeParam foobar(TestFixture::as_string("foobar"));
203 BasicStringPiece<TypeParam> a(alphabet);
204 BasicStringPiece<TypeParam> b(abc);
205 BasicStringPiece<TypeParam> c(xyz);
206 BasicStringPiece<TypeParam> d(foobar);
208 d.clear();
209 Piece e;
210 TypeParam temp(TestFixture::as_string("123"));
211 temp.push_back('\0');
212 temp += TestFixture::as_string("456");
213 Piece f(temp);
215 typename TypeParam::value_type buf[4] = { '%', '%', '%', '%' };
216 ASSERT_EQ(a.copy(buf, 4), 4U);
217 ASSERT_EQ(buf[0], a[0]);
218 ASSERT_EQ(buf[1], a[1]);
219 ASSERT_EQ(buf[2], a[2]);
220 ASSERT_EQ(buf[3], a[3]);
221 ASSERT_EQ(a.copy(buf, 3, 7), 3U);
222 ASSERT_EQ(buf[0], a[7]);
223 ASSERT_EQ(buf[1], a[8]);
224 ASSERT_EQ(buf[2], a[9]);
225 ASSERT_EQ(buf[3], a[3]);
226 ASSERT_EQ(c.copy(buf, 99), 3U);
227 ASSERT_EQ(buf[0], c[0]);
228 ASSERT_EQ(buf[1], c[1]);
229 ASSERT_EQ(buf[2], c[2]);
230 ASSERT_EQ(buf[3], a[3]);
232 ASSERT_EQ(Piece::npos, TypeParam::npos);
234 ASSERT_EQ(a.find(b), 0U);
235 ASSERT_EQ(a.find(b, 1), Piece::npos);
236 ASSERT_EQ(a.find(c), 23U);
237 ASSERT_EQ(a.find(c, 9), 23U);
238 ASSERT_EQ(a.find(c, Piece::npos), Piece::npos);
239 ASSERT_EQ(b.find(c), Piece::npos);
240 ASSERT_EQ(b.find(c, Piece::npos), Piece::npos);
241 ASSERT_EQ(a.find(d), 0U);
242 ASSERT_EQ(a.find(e), 0U);
243 ASSERT_EQ(a.find(d, 12), 12U);
244 ASSERT_EQ(a.find(e, 17), 17U);
245 TypeParam not_found(TestFixture::as_string("xx not found bb"));
246 Piece g(not_found);
247 ASSERT_EQ(a.find(g), Piece::npos);
248 // empty string nonsense
249 ASSERT_EQ(d.find(b), Piece::npos);
250 ASSERT_EQ(e.find(b), Piece::npos);
251 ASSERT_EQ(d.find(b, 4), Piece::npos);
252 ASSERT_EQ(e.find(b, 7), Piece::npos);
254 size_t empty_search_pos = TypeParam().find(TypeParam());
255 ASSERT_EQ(d.find(d), empty_search_pos);
256 ASSERT_EQ(d.find(e), empty_search_pos);
257 ASSERT_EQ(e.find(d), empty_search_pos);
258 ASSERT_EQ(e.find(e), empty_search_pos);
259 ASSERT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
260 ASSERT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
261 ASSERT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
262 ASSERT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
264 ASSERT_EQ(a.find('a'), 0U);
265 ASSERT_EQ(a.find('c'), 2U);
266 ASSERT_EQ(a.find('z'), 25U);
267 ASSERT_EQ(a.find('$'), Piece::npos);
268 ASSERT_EQ(a.find('\0'), Piece::npos);
269 ASSERT_EQ(f.find('\0'), 3U);
270 ASSERT_EQ(f.find('3'), 2U);
271 ASSERT_EQ(f.find('5'), 5U);
272 ASSERT_EQ(g.find('o'), 4U);
273 ASSERT_EQ(g.find('o', 4), 4U);
274 ASSERT_EQ(g.find('o', 5), 8U);
275 ASSERT_EQ(a.find('b', 5), Piece::npos);
276 // empty string nonsense
277 ASSERT_EQ(d.find('\0'), Piece::npos);
278 ASSERT_EQ(e.find('\0'), Piece::npos);
279 ASSERT_EQ(d.find('\0', 4), Piece::npos);
280 ASSERT_EQ(e.find('\0', 7), Piece::npos);
281 ASSERT_EQ(d.find('x'), Piece::npos);
282 ASSERT_EQ(e.find('x'), Piece::npos);
283 ASSERT_EQ(d.find('x', 4), Piece::npos);
284 ASSERT_EQ(e.find('x', 7), Piece::npos);
286 ASSERT_EQ(a.rfind(b), 0U);
287 ASSERT_EQ(a.rfind(b, 1), 0U);
288 ASSERT_EQ(a.rfind(c), 23U);
289 ASSERT_EQ(a.rfind(c, 22U), Piece::npos);
290 ASSERT_EQ(a.rfind(c, 1U), Piece::npos);
291 ASSERT_EQ(a.rfind(c, 0U), Piece::npos);
292 ASSERT_EQ(b.rfind(c), Piece::npos);
293 ASSERT_EQ(b.rfind(c, 0U), Piece::npos);
294 ASSERT_EQ(a.rfind(d), static_cast<size_t>(a.as_string().rfind(TypeParam())));
295 ASSERT_EQ(a.rfind(e), a.as_string().rfind(TypeParam()));
296 ASSERT_EQ(a.rfind(d, 12), 12U);
297 ASSERT_EQ(a.rfind(e, 17), 17U);
298 ASSERT_EQ(a.rfind(g), Piece::npos);
299 ASSERT_EQ(d.rfind(b), Piece::npos);
300 ASSERT_EQ(e.rfind(b), Piece::npos);
301 ASSERT_EQ(d.rfind(b, 4), Piece::npos);
302 ASSERT_EQ(e.rfind(b, 7), Piece::npos);
303 // empty string nonsense
304 ASSERT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
305 ASSERT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
306 ASSERT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
307 ASSERT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
308 ASSERT_EQ(d.rfind(d), std::string().rfind(std::string()));
309 ASSERT_EQ(e.rfind(d), std::string().rfind(std::string()));
310 ASSERT_EQ(d.rfind(e), std::string().rfind(std::string()));
311 ASSERT_EQ(e.rfind(e), std::string().rfind(std::string()));
313 ASSERT_EQ(g.rfind('o'), 8U);
314 ASSERT_EQ(g.rfind('q'), Piece::npos);
315 ASSERT_EQ(g.rfind('o', 8), 8U);
316 ASSERT_EQ(g.rfind('o', 7), 4U);
317 ASSERT_EQ(g.rfind('o', 3), Piece::npos);
318 ASSERT_EQ(f.rfind('\0'), 3U);
319 ASSERT_EQ(f.rfind('\0', 12), 3U);
320 ASSERT_EQ(f.rfind('3'), 2U);
321 ASSERT_EQ(f.rfind('5'), 5U);
322 // empty string nonsense
323 ASSERT_EQ(d.rfind('o'), Piece::npos);
324 ASSERT_EQ(e.rfind('o'), Piece::npos);
325 ASSERT_EQ(d.rfind('o', 4), Piece::npos);
326 ASSERT_EQ(e.rfind('o', 7), Piece::npos);
328 TypeParam one_two_three_four(TestFixture::as_string("one,two:three;four"));
329 TypeParam comma_colon(TestFixture::as_string(",:"));
330 ASSERT_EQ(3U, Piece(one_two_three_four).find_first_of(comma_colon));
331 ASSERT_EQ(a.find_first_of(b), 0U);
332 ASSERT_EQ(a.find_first_of(b, 0), 0U);
333 ASSERT_EQ(a.find_first_of(b, 1), 1U);
334 ASSERT_EQ(a.find_first_of(b, 2), 2U);
335 ASSERT_EQ(a.find_first_of(b, 3), Piece::npos);
336 ASSERT_EQ(a.find_first_of(c), 23U);
337 ASSERT_EQ(a.find_first_of(c, 23), 23U);
338 ASSERT_EQ(a.find_first_of(c, 24), 24U);
339 ASSERT_EQ(a.find_first_of(c, 25), 25U);
340 ASSERT_EQ(a.find_first_of(c, 26), Piece::npos);
341 ASSERT_EQ(g.find_first_of(b), 13U);
342 ASSERT_EQ(g.find_first_of(c), 0U);
343 ASSERT_EQ(a.find_first_of(f), Piece::npos);
344 ASSERT_EQ(f.find_first_of(a), Piece::npos);
345 // empty string nonsense
346 ASSERT_EQ(a.find_first_of(d), Piece::npos);
347 ASSERT_EQ(a.find_first_of(e), Piece::npos);
348 ASSERT_EQ(d.find_first_of(b), Piece::npos);
349 ASSERT_EQ(e.find_first_of(b), Piece::npos);
350 ASSERT_EQ(d.find_first_of(d), Piece::npos);
351 ASSERT_EQ(e.find_first_of(d), Piece::npos);
352 ASSERT_EQ(d.find_first_of(e), Piece::npos);
353 ASSERT_EQ(e.find_first_of(e), Piece::npos);
355 ASSERT_EQ(a.find_first_not_of(b), 3U);
356 ASSERT_EQ(a.find_first_not_of(c), 0U);
357 ASSERT_EQ(b.find_first_not_of(a), Piece::npos);
358 ASSERT_EQ(c.find_first_not_of(a), Piece::npos);
359 ASSERT_EQ(f.find_first_not_of(a), 0U);
360 ASSERT_EQ(a.find_first_not_of(f), 0U);
361 ASSERT_EQ(a.find_first_not_of(d), 0U);
362 ASSERT_EQ(a.find_first_not_of(e), 0U);
363 // empty string nonsense
364 ASSERT_EQ(d.find_first_not_of(a), Piece::npos);
365 ASSERT_EQ(e.find_first_not_of(a), Piece::npos);
366 ASSERT_EQ(d.find_first_not_of(d), Piece::npos);
367 ASSERT_EQ(e.find_first_not_of(d), Piece::npos);
368 ASSERT_EQ(d.find_first_not_of(e), Piece::npos);
369 ASSERT_EQ(e.find_first_not_of(e), Piece::npos);
371 TypeParam equals(TestFixture::as_string("===="));
372 Piece h(equals);
373 ASSERT_EQ(h.find_first_not_of('='), Piece::npos);
374 ASSERT_EQ(h.find_first_not_of('=', 3), Piece::npos);
375 ASSERT_EQ(h.find_first_not_of('\0'), 0U);
376 ASSERT_EQ(g.find_first_not_of('x'), 2U);
377 ASSERT_EQ(f.find_first_not_of('\0'), 0U);
378 ASSERT_EQ(f.find_first_not_of('\0', 3), 4U);
379 ASSERT_EQ(f.find_first_not_of('\0', 2), 2U);
380 // empty string nonsense
381 ASSERT_EQ(d.find_first_not_of('x'), Piece::npos);
382 ASSERT_EQ(e.find_first_not_of('x'), Piece::npos);
383 ASSERT_EQ(d.find_first_not_of('\0'), Piece::npos);
384 ASSERT_EQ(e.find_first_not_of('\0'), Piece::npos);
386 // Piece g("xx not found bb");
387 TypeParam fifty_six(TestFixture::as_string("56"));
388 Piece i(fifty_six);
389 ASSERT_EQ(h.find_last_of(a), Piece::npos);
390 ASSERT_EQ(g.find_last_of(a), g.size()-1);
391 ASSERT_EQ(a.find_last_of(b), 2U);
392 ASSERT_EQ(a.find_last_of(c), a.size()-1);
393 ASSERT_EQ(f.find_last_of(i), 6U);
394 ASSERT_EQ(a.find_last_of('a'), 0U);
395 ASSERT_EQ(a.find_last_of('b'), 1U);
396 ASSERT_EQ(a.find_last_of('z'), 25U);
397 ASSERT_EQ(a.find_last_of('a', 5), 0U);
398 ASSERT_EQ(a.find_last_of('b', 5), 1U);
399 ASSERT_EQ(a.find_last_of('b', 0), Piece::npos);
400 ASSERT_EQ(a.find_last_of('z', 25), 25U);
401 ASSERT_EQ(a.find_last_of('z', 24), Piece::npos);
402 ASSERT_EQ(f.find_last_of(i, 5), 5U);
403 ASSERT_EQ(f.find_last_of(i, 6), 6U);
404 ASSERT_EQ(f.find_last_of(a, 4), Piece::npos);
405 // empty string nonsense
406 ASSERT_EQ(f.find_last_of(d), Piece::npos);
407 ASSERT_EQ(f.find_last_of(e), Piece::npos);
408 ASSERT_EQ(f.find_last_of(d, 4), Piece::npos);
409 ASSERT_EQ(f.find_last_of(e, 4), Piece::npos);
410 ASSERT_EQ(d.find_last_of(d), Piece::npos);
411 ASSERT_EQ(d.find_last_of(e), Piece::npos);
412 ASSERT_EQ(e.find_last_of(d), Piece::npos);
413 ASSERT_EQ(e.find_last_of(e), Piece::npos);
414 ASSERT_EQ(d.find_last_of(f), Piece::npos);
415 ASSERT_EQ(e.find_last_of(f), Piece::npos);
416 ASSERT_EQ(d.find_last_of(d, 4), Piece::npos);
417 ASSERT_EQ(d.find_last_of(e, 4), Piece::npos);
418 ASSERT_EQ(e.find_last_of(d, 4), Piece::npos);
419 ASSERT_EQ(e.find_last_of(e, 4), Piece::npos);
420 ASSERT_EQ(d.find_last_of(f, 4), Piece::npos);
421 ASSERT_EQ(e.find_last_of(f, 4), Piece::npos);
423 ASSERT_EQ(a.find_last_not_of(b), a.size()-1);
424 ASSERT_EQ(a.find_last_not_of(c), 22U);
425 ASSERT_EQ(b.find_last_not_of(a), Piece::npos);
426 ASSERT_EQ(b.find_last_not_of(b), Piece::npos);
427 ASSERT_EQ(f.find_last_not_of(i), 4U);
428 ASSERT_EQ(a.find_last_not_of(c, 24), 22U);
429 ASSERT_EQ(a.find_last_not_of(b, 3), 3U);
430 ASSERT_EQ(a.find_last_not_of(b, 2), Piece::npos);
431 // empty string nonsense
432 ASSERT_EQ(f.find_last_not_of(d), f.size()-1);
433 ASSERT_EQ(f.find_last_not_of(e), f.size()-1);
434 ASSERT_EQ(f.find_last_not_of(d, 4), 4U);
435 ASSERT_EQ(f.find_last_not_of(e, 4), 4U);
436 ASSERT_EQ(d.find_last_not_of(d), Piece::npos);
437 ASSERT_EQ(d.find_last_not_of(e), Piece::npos);
438 ASSERT_EQ(e.find_last_not_of(d), Piece::npos);
439 ASSERT_EQ(e.find_last_not_of(e), Piece::npos);
440 ASSERT_EQ(d.find_last_not_of(f), Piece::npos);
441 ASSERT_EQ(e.find_last_not_of(f), Piece::npos);
442 ASSERT_EQ(d.find_last_not_of(d, 4), Piece::npos);
443 ASSERT_EQ(d.find_last_not_of(e, 4), Piece::npos);
444 ASSERT_EQ(e.find_last_not_of(d, 4), Piece::npos);
445 ASSERT_EQ(e.find_last_not_of(e, 4), Piece::npos);
446 ASSERT_EQ(d.find_last_not_of(f, 4), Piece::npos);
447 ASSERT_EQ(e.find_last_not_of(f, 4), Piece::npos);
449 ASSERT_EQ(h.find_last_not_of('x'), h.size() - 1);
450 ASSERT_EQ(h.find_last_not_of('='), Piece::npos);
451 ASSERT_EQ(b.find_last_not_of('c'), 1U);
452 ASSERT_EQ(h.find_last_not_of('x', 2), 2U);
453 ASSERT_EQ(h.find_last_not_of('=', 2), Piece::npos);
454 ASSERT_EQ(b.find_last_not_of('b', 1), 0U);
455 // empty string nonsense
456 ASSERT_EQ(d.find_last_not_of('x'), Piece::npos);
457 ASSERT_EQ(e.find_last_not_of('x'), Piece::npos);
458 ASSERT_EQ(d.find_last_not_of('\0'), Piece::npos);
459 ASSERT_EQ(e.find_last_not_of('\0'), Piece::npos);
461 ASSERT_EQ(a.substr(0, 3), b);
462 ASSERT_EQ(a.substr(23), c);
463 ASSERT_EQ(a.substr(23, 3), c);
464 ASSERT_EQ(a.substr(23, 99), c);
465 ASSERT_EQ(a.substr(0), a);
466 ASSERT_EQ(a.substr(3, 2), TestFixture::as_string("de"));
467 // empty string nonsense
468 ASSERT_EQ(a.substr(99, 2), e);
469 ASSERT_EQ(d.substr(99), e);
470 ASSERT_EQ(d.substr(0, 99), e);
471 ASSERT_EQ(d.substr(99, 99), e);
474 TYPED_TEST(CommonStringPieceTest, CheckCustom) {
475 TypeParam foobar(TestFixture::as_string("foobar"));
476 BasicStringPiece<TypeParam> a(foobar);
477 TypeParam s1(TestFixture::as_string("123"));
478 s1 += static_cast<typename TypeParam::value_type>('\0');
479 s1 += TestFixture::as_string("456");
480 BasicStringPiece<TypeParam> b(s1);
481 BasicStringPiece<TypeParam> e;
482 TypeParam s2;
484 // remove_prefix
485 BasicStringPiece<TypeParam> c(a);
486 c.remove_prefix(3);
487 ASSERT_EQ(c, TestFixture::as_string("bar"));
488 c = a;
489 c.remove_prefix(0);
490 ASSERT_EQ(c, a);
491 c.remove_prefix(c.size());
492 ASSERT_EQ(c, e);
494 // remove_suffix
495 c = a;
496 c.remove_suffix(3);
497 ASSERT_EQ(c, TestFixture::as_string("foo"));
498 c = a;
499 c.remove_suffix(0);
500 ASSERT_EQ(c, a);
501 c.remove_suffix(c.size());
502 ASSERT_EQ(c, e);
504 // set
505 c.set(foobar.c_str());
506 ASSERT_EQ(c, a);
507 c.set(foobar.c_str(), 6);
508 ASSERT_EQ(c, a);
509 c.set(foobar.c_str(), 0);
510 ASSERT_EQ(c, e);
511 c.set(foobar.c_str(), 7); // Note, has an embedded NULL
512 ASSERT_NE(c, a);
514 // as_string
515 TypeParam s3(a.as_string().c_str(), 7); // Note, has an embedded NULL
516 ASSERT_TRUE(c == s3);
517 TypeParam s4(e.as_string());
518 ASSERT_TRUE(s4.empty());
521 TEST(StringPieceTest, CheckCustom) {
522 StringPiece a("foobar");
523 std::string s1("123");
524 s1 += '\0';
525 s1 += "456";
526 StringPiece b(s1);
527 StringPiece e;
528 std::string s2;
530 // CopyToString
531 a.CopyToString(&s2);
532 ASSERT_EQ(s2.size(), 6U);
533 ASSERT_EQ(s2, "foobar");
534 b.CopyToString(&s2);
535 ASSERT_EQ(s2.size(), 7U);
536 ASSERT_EQ(s1, s2);
537 e.CopyToString(&s2);
538 ASSERT_TRUE(s2.empty());
540 // AppendToString
541 s2.erase();
542 a.AppendToString(&s2);
543 ASSERT_EQ(s2.size(), 6U);
544 ASSERT_EQ(s2, "foobar");
545 a.AppendToString(&s2);
546 ASSERT_EQ(s2.size(), 12U);
547 ASSERT_EQ(s2, "foobarfoobar");
549 // starts_with
550 ASSERT_TRUE(a.starts_with(a));
551 ASSERT_TRUE(a.starts_with("foo"));
552 ASSERT_TRUE(a.starts_with(e));
553 ASSERT_TRUE(b.starts_with(s1));
554 ASSERT_TRUE(b.starts_with(b));
555 ASSERT_TRUE(b.starts_with(e));
556 ASSERT_TRUE(e.starts_with(""));
557 ASSERT_TRUE(!a.starts_with(b));
558 ASSERT_TRUE(!b.starts_with(a));
559 ASSERT_TRUE(!e.starts_with(a));
561 // ends with
562 ASSERT_TRUE(a.ends_with(a));
563 ASSERT_TRUE(a.ends_with("bar"));
564 ASSERT_TRUE(a.ends_with(e));
565 ASSERT_TRUE(b.ends_with(s1));
566 ASSERT_TRUE(b.ends_with(b));
567 ASSERT_TRUE(b.ends_with(e));
568 ASSERT_TRUE(e.ends_with(""));
569 ASSERT_TRUE(!a.ends_with(b));
570 ASSERT_TRUE(!b.ends_with(a));
571 ASSERT_TRUE(!e.ends_with(a));
573 StringPiece c;
574 c.set("foobar", 6);
575 ASSERT_EQ(c, a);
576 c.set("foobar", 0);
577 ASSERT_EQ(c, e);
578 c.set("foobar", 7);
579 ASSERT_NE(c, a);
582 TYPED_TEST(CommonStringPieceTest, CheckNULL) {
583 // we used to crash here, but now we don't.
584 BasicStringPiece<TypeParam> s(NULL);
585 ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)NULL);
586 ASSERT_EQ(s.size(), 0U);
588 s.set(NULL);
589 ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)NULL);
590 ASSERT_EQ(s.size(), 0U);
592 TypeParam str = s.as_string();
593 ASSERT_EQ(str.length(), 0U);
594 ASSERT_EQ(str, TypeParam());
597 TYPED_TEST(CommonStringPieceTest, CheckComparisons2) {
598 TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
599 TypeParam alphabet_z(TestFixture::as_string("abcdefghijklmnopqrstuvwxyzz"));
600 TypeParam alphabet_y(TestFixture::as_string("abcdefghijklmnopqrstuvwxyy"));
601 BasicStringPiece<TypeParam> abc(alphabet);
603 // check comparison operations on strings longer than 4 bytes.
604 ASSERT_TRUE(abc == BasicStringPiece<TypeParam>(alphabet));
605 ASSERT_EQ(abc.compare(BasicStringPiece<TypeParam>(alphabet)), 0);
607 ASSERT_TRUE(abc < BasicStringPiece<TypeParam>(alphabet_z));
608 ASSERT_LT(abc.compare(BasicStringPiece<TypeParam>(alphabet_z)), 0);
610 ASSERT_TRUE(abc > BasicStringPiece<TypeParam>(alphabet_y));
611 ASSERT_GT(abc.compare(BasicStringPiece<TypeParam>(alphabet_y)), 0);
614 // Test operations only supported by std::string version.
615 TEST(StringPieceTest, CheckComparisons2) {
616 StringPiece abc("abcdefghijklmnopqrstuvwxyz");
618 // starts_with
619 ASSERT_TRUE(abc.starts_with(abc));
620 ASSERT_TRUE(abc.starts_with("abcdefghijklm"));
621 ASSERT_TRUE(!abc.starts_with("abcdefguvwxyz"));
623 // ends_with
624 ASSERT_TRUE(abc.ends_with(abc));
625 ASSERT_TRUE(!abc.ends_with("abcdefguvwxyz"));
626 ASSERT_TRUE(abc.ends_with("nopqrstuvwxyz"));
629 TYPED_TEST(CommonStringPieceTest, StringCompareNotAmbiguous) {
630 ASSERT_TRUE(TestFixture::as_string("hello").c_str() ==
631 TestFixture::as_string("hello"));
632 ASSERT_TRUE(TestFixture::as_string("hello").c_str() <
633 TestFixture::as_string("world"));
636 TYPED_TEST(CommonStringPieceTest, HeterogenousStringPieceEquals) {
637 TypeParam hello(TestFixture::as_string("hello"));
639 ASSERT_TRUE(BasicStringPiece<TypeParam>(hello) == hello);
640 ASSERT_TRUE(hello.c_str() == BasicStringPiece<TypeParam>(hello));
643 // string16-specific stuff
644 TEST(StringPiece16Test, CheckSTL) {
645 // Check some non-ascii characters.
646 string16 fifth(ASCIIToUTF16("123"));
647 fifth.push_back(0x0000);
648 fifth.push_back(0xd8c5);
649 fifth.push_back(0xdffe);
650 StringPiece16 f(fifth);
652 ASSERT_EQ(f[3], '\0');
653 ASSERT_EQ(f[5], static_cast<char16>(0xdffe));
655 ASSERT_EQ(f.size(), 6U);
660 TEST(StringPiece16Test, CheckConversion) {
661 // Make sure that we can convert from UTF8 to UTF16 and back. We use a two
662 // byte character (G clef) to test this.
663 ASSERT_EQ(
664 UTF16ToUTF8(
665 StringPiece16(UTF8ToUTF16("\xf0\x9d\x84\x9e")).as_string()),
666 "\xf0\x9d\x84\x9e");
669 TYPED_TEST(CommonStringPieceTest, CheckConstructors) {
670 TypeParam str(TestFixture::as_string("hello world"));
671 TypeParam empty;
673 ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str));
674 ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str.c_str()));
675 ASSERT_TRUE(TestFixture::as_string("hello") ==
676 BasicStringPiece<TypeParam>(str.c_str(), 5));
677 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(str.c_str(),
678 static_cast<typename BasicStringPiece<TypeParam>::size_type>(0)));
679 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(NULL));
680 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(NULL,
681 static_cast<typename BasicStringPiece<TypeParam>::size_type>(0)));
682 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>());
683 ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str.begin(), str.end()));
684 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(str.begin(), str.begin()));
685 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(empty));
686 ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(empty.begin(), empty.end()));
689 } // namespace base