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