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