Fix platform conditionalization of NaCl IRT file name
[chromium-blink-merge.git] / base / string_piece_unittest.cc
blob694c43b61f5dddf1ca69406657c462f26c4028a5
1 // Copyright (c) 2011 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/string16.h"
8 #include "base/string_piece.h"
9 #include "base/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
14 TEST(StringPieceTest, CheckComparisonOperators) {
15 #define CMP_Y(op, x, y) \
16 ASSERT_TRUE( (StringPiece((x)) op StringPiece((y)))); \
17 ASSERT_TRUE( (StringPiece((x)).compare(StringPiece((y))) op 0))
19 #define CMP_N(op, x, y) \
20 ASSERT_FALSE(StringPiece((x)) op StringPiece((y))); \
21 ASSERT_FALSE(StringPiece((x)).compare(StringPiece((y))) op 0)
23 CMP_Y(==, "", "");
24 CMP_Y(==, "a", "a");
25 CMP_Y(==, "aa", "aa");
26 CMP_N(==, "a", "");
27 CMP_N(==, "", "a");
28 CMP_N(==, "a", "b");
29 CMP_N(==, "a", "aa");
30 CMP_N(==, "aa", "a");
32 CMP_N(!=, "", "");
33 CMP_N(!=, "a", "a");
34 CMP_N(!=, "aa", "aa");
35 CMP_Y(!=, "a", "");
36 CMP_Y(!=, "", "a");
37 CMP_Y(!=, "a", "b");
38 CMP_Y(!=, "a", "aa");
39 CMP_Y(!=, "aa", "a");
41 CMP_Y(<, "a", "b");
42 CMP_Y(<, "a", "aa");
43 CMP_Y(<, "aa", "b");
44 CMP_Y(<, "aa", "bb");
45 CMP_N(<, "a", "a");
46 CMP_N(<, "b", "a");
47 CMP_N(<, "aa", "a");
48 CMP_N(<, "b", "aa");
49 CMP_N(<, "bb", "aa");
51 CMP_Y(<=, "a", "a");
52 CMP_Y(<=, "a", "b");
53 CMP_Y(<=, "a", "aa");
54 CMP_Y(<=, "aa", "b");
55 CMP_Y(<=, "aa", "bb");
56 CMP_N(<=, "b", "a");
57 CMP_N(<=, "aa", "a");
58 CMP_N(<=, "b", "aa");
59 CMP_N(<=, "bb", "aa");
61 CMP_N(>=, "a", "b");
62 CMP_N(>=, "a", "aa");
63 CMP_N(>=, "aa", "b");
64 CMP_N(>=, "aa", "bb");
65 CMP_Y(>=, "a", "a");
66 CMP_Y(>=, "b", "a");
67 CMP_Y(>=, "aa", "a");
68 CMP_Y(>=, "b", "aa");
69 CMP_Y(>=, "bb", "aa");
71 CMP_N(>, "a", "a");
72 CMP_N(>, "a", "b");
73 CMP_N(>, "a", "aa");
74 CMP_N(>, "aa", "b");
75 CMP_N(>, "aa", "bb");
76 CMP_Y(>, "b", "a");
77 CMP_Y(>, "aa", "a");
78 CMP_Y(>, "b", "aa");
79 CMP_Y(>, "bb", "aa");
81 std::string x;
82 for (int i = 0; i < 256; i++) {
83 x += 'a';
84 std::string y = x;
85 CMP_Y(==, x, y);
86 for (int j = 0; j < i; j++) {
87 std::string z = x;
88 z[j] = 'b'; // Differs in position 'j'
89 CMP_N(==, x, z);
93 #undef CMP_Y
94 #undef CMP_N
97 TEST(StringPieceTest, CheckSTL) {
98 StringPiece a("abcdefghijklmnopqrstuvwxyz");
99 StringPiece b("abc");
100 StringPiece c("xyz");
101 StringPiece d("foobar");
102 StringPiece e;
103 std::string temp("123");
104 temp += '\0';
105 temp += "456";
106 StringPiece f(temp);
108 ASSERT_EQ(a[6], 'g');
109 ASSERT_EQ(b[0], 'a');
110 ASSERT_EQ(c[2], 'z');
111 ASSERT_EQ(f[3], '\0');
112 ASSERT_EQ(f[5], '5');
114 ASSERT_EQ(*d.data(), 'f');
115 ASSERT_EQ(d.data()[5], 'r');
116 ASSERT_TRUE(e.data() == NULL);
118 ASSERT_EQ(*a.begin(), 'a');
119 ASSERT_EQ(*(b.begin() + 2), 'c');
120 ASSERT_EQ(*(c.end() - 1), 'z');
122 ASSERT_EQ(*a.rbegin(), 'z');
123 ASSERT_EQ(*(b.rbegin() + 2), 'a');
124 ASSERT_EQ(*(c.rend() - 1), 'x');
125 ASSERT_TRUE(a.rbegin() + 26 == a.rend());
127 ASSERT_EQ(a.size(), 26U);
128 ASSERT_EQ(b.size(), 3U);
129 ASSERT_EQ(c.size(), 3U);
130 ASSERT_EQ(d.size(), 6U);
131 ASSERT_EQ(e.size(), 0U);
132 ASSERT_EQ(f.size(), 7U);
134 ASSERT_TRUE(!d.empty());
135 ASSERT_TRUE(d.begin() != d.end());
136 ASSERT_TRUE(d.begin() + 6 == d.end());
138 ASSERT_TRUE(e.empty());
139 ASSERT_TRUE(e.begin() == e.end());
141 d.clear();
142 ASSERT_EQ(d.size(), 0U);
143 ASSERT_TRUE(d.empty());
144 ASSERT_TRUE(d.data() == NULL);
145 ASSERT_TRUE(d.begin() == d.end());
147 ASSERT_GE(a.max_size(), a.capacity());
148 ASSERT_GE(a.capacity(), a.size());
150 char buf[4] = { '%', '%', '%', '%' };
151 ASSERT_EQ(a.copy(buf, 4), 4U);
152 ASSERT_EQ(buf[0], a[0]);
153 ASSERT_EQ(buf[1], a[1]);
154 ASSERT_EQ(buf[2], a[2]);
155 ASSERT_EQ(buf[3], a[3]);
156 ASSERT_EQ(a.copy(buf, 3, 7), 3U);
157 ASSERT_EQ(buf[0], a[7]);
158 ASSERT_EQ(buf[1], a[8]);
159 ASSERT_EQ(buf[2], a[9]);
160 ASSERT_EQ(buf[3], a[3]);
161 ASSERT_EQ(c.copy(buf, 99), 3U);
162 ASSERT_EQ(buf[0], c[0]);
163 ASSERT_EQ(buf[1], c[1]);
164 ASSERT_EQ(buf[2], c[2]);
165 ASSERT_EQ(buf[3], a[3]);
167 ASSERT_EQ(StringPiece::npos, std::string::npos);
169 ASSERT_EQ(a.find(b), 0U);
170 ASSERT_EQ(a.find(b, 1), StringPiece::npos);
171 ASSERT_EQ(a.find(c), 23U);
172 ASSERT_EQ(a.find(c, 9), 23U);
173 ASSERT_EQ(a.find(c, StringPiece::npos), StringPiece::npos);
174 ASSERT_EQ(b.find(c), StringPiece::npos);
175 ASSERT_EQ(b.find(c, StringPiece::npos), StringPiece::npos);
176 ASSERT_EQ(a.find(d), 0U);
177 ASSERT_EQ(a.find(e), 0U);
178 ASSERT_EQ(a.find(d, 12), 12U);
179 ASSERT_EQ(a.find(e, 17), 17U);
180 StringPiece g("xx not found bb");
181 ASSERT_EQ(a.find(g), StringPiece::npos);
182 // empty string nonsense
183 ASSERT_EQ(d.find(b), StringPiece::npos);
184 ASSERT_EQ(e.find(b), StringPiece::npos);
185 ASSERT_EQ(d.find(b, 4), StringPiece::npos);
186 ASSERT_EQ(e.find(b, 7), StringPiece::npos);
188 size_t empty_search_pos = std::string().find(std::string());
189 ASSERT_EQ(d.find(d), empty_search_pos);
190 ASSERT_EQ(d.find(e), empty_search_pos);
191 ASSERT_EQ(e.find(d), empty_search_pos);
192 ASSERT_EQ(e.find(e), empty_search_pos);
193 ASSERT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
194 ASSERT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
195 ASSERT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
196 ASSERT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
198 ASSERT_EQ(a.find('a'), 0U);
199 ASSERT_EQ(a.find('c'), 2U);
200 ASSERT_EQ(a.find('z'), 25U);
201 ASSERT_EQ(a.find('$'), StringPiece::npos);
202 ASSERT_EQ(a.find('\0'), StringPiece::npos);
203 ASSERT_EQ(f.find('\0'), 3U);
204 ASSERT_EQ(f.find('3'), 2U);
205 ASSERT_EQ(f.find('5'), 5U);
206 ASSERT_EQ(g.find('o'), 4U);
207 ASSERT_EQ(g.find('o', 4), 4U);
208 ASSERT_EQ(g.find('o', 5), 8U);
209 ASSERT_EQ(a.find('b', 5), StringPiece::npos);
210 // empty string nonsense
211 ASSERT_EQ(d.find('\0'), StringPiece::npos);
212 ASSERT_EQ(e.find('\0'), StringPiece::npos);
213 ASSERT_EQ(d.find('\0', 4), StringPiece::npos);
214 ASSERT_EQ(e.find('\0', 7), StringPiece::npos);
215 ASSERT_EQ(d.find('x'), StringPiece::npos);
216 ASSERT_EQ(e.find('x'), StringPiece::npos);
217 ASSERT_EQ(d.find('x', 4), StringPiece::npos);
218 ASSERT_EQ(e.find('x', 7), StringPiece::npos);
220 ASSERT_EQ(a.rfind(b), 0U);
221 ASSERT_EQ(a.rfind(b, 1), 0U);
222 ASSERT_EQ(a.rfind(c), 23U);
223 ASSERT_EQ(a.rfind(c, 22U), StringPiece::npos);
224 ASSERT_EQ(a.rfind(c, 1U), StringPiece::npos);
225 ASSERT_EQ(a.rfind(c, 0U), StringPiece::npos);
226 ASSERT_EQ(b.rfind(c), StringPiece::npos);
227 ASSERT_EQ(b.rfind(c, 0U), StringPiece::npos);
228 ASSERT_EQ(a.rfind(d), (size_t) a.as_string().rfind(std::string()));
229 ASSERT_EQ(a.rfind(e), a.as_string().rfind(std::string()));
230 ASSERT_EQ(a.rfind(d, 12), 12U);
231 ASSERT_EQ(a.rfind(e, 17), 17U);
232 ASSERT_EQ(a.rfind(g), StringPiece::npos);
233 ASSERT_EQ(d.rfind(b), StringPiece::npos);
234 ASSERT_EQ(e.rfind(b), StringPiece::npos);
235 ASSERT_EQ(d.rfind(b, 4), StringPiece::npos);
236 ASSERT_EQ(e.rfind(b, 7), StringPiece::npos);
237 // empty string nonsense
238 ASSERT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
239 ASSERT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
240 ASSERT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
241 ASSERT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
242 ASSERT_EQ(d.rfind(d), std::string().rfind(std::string()));
243 ASSERT_EQ(e.rfind(d), std::string().rfind(std::string()));
244 ASSERT_EQ(d.rfind(e), std::string().rfind(std::string()));
245 ASSERT_EQ(e.rfind(e), std::string().rfind(std::string()));
247 ASSERT_EQ(g.rfind('o'), 8U);
248 ASSERT_EQ(g.rfind('q'), StringPiece::npos);
249 ASSERT_EQ(g.rfind('o', 8), 8U);
250 ASSERT_EQ(g.rfind('o', 7), 4U);
251 ASSERT_EQ(g.rfind('o', 3), StringPiece::npos);
252 ASSERT_EQ(f.rfind('\0'), 3U);
253 ASSERT_EQ(f.rfind('\0', 12), 3U);
254 ASSERT_EQ(f.rfind('3'), 2U);
255 ASSERT_EQ(f.rfind('5'), 5U);
256 // empty string nonsense
257 ASSERT_EQ(d.rfind('o'), StringPiece::npos);
258 ASSERT_EQ(e.rfind('o'), StringPiece::npos);
259 ASSERT_EQ(d.rfind('o', 4), StringPiece::npos);
260 ASSERT_EQ(e.rfind('o', 7), StringPiece::npos);
262 ASSERT_EQ(a.find_first_of(b), 0U);
263 ASSERT_EQ(a.find_first_of(b, 0), 0U);
264 ASSERT_EQ(a.find_first_of(b, 1), 1U);
265 ASSERT_EQ(a.find_first_of(b, 2), 2U);
266 ASSERT_EQ(a.find_first_of(b, 3), StringPiece::npos);
267 ASSERT_EQ(a.find_first_of(c), 23U);
268 ASSERT_EQ(a.find_first_of(c, 23), 23U);
269 ASSERT_EQ(a.find_first_of(c, 24), 24U);
270 ASSERT_EQ(a.find_first_of(c, 25), 25U);
271 ASSERT_EQ(a.find_first_of(c, 26), StringPiece::npos);
272 ASSERT_EQ(g.find_first_of(b), 13U);
273 ASSERT_EQ(g.find_first_of(c), 0U);
274 ASSERT_EQ(a.find_first_of(f), StringPiece::npos);
275 ASSERT_EQ(f.find_first_of(a), StringPiece::npos);
276 // empty string nonsense
277 ASSERT_EQ(a.find_first_of(d), StringPiece::npos);
278 ASSERT_EQ(a.find_first_of(e), StringPiece::npos);
279 ASSERT_EQ(d.find_first_of(b), StringPiece::npos);
280 ASSERT_EQ(e.find_first_of(b), StringPiece::npos);
281 ASSERT_EQ(d.find_first_of(d), StringPiece::npos);
282 ASSERT_EQ(e.find_first_of(d), StringPiece::npos);
283 ASSERT_EQ(d.find_first_of(e), StringPiece::npos);
284 ASSERT_EQ(e.find_first_of(e), StringPiece::npos);
286 ASSERT_EQ(a.find_first_not_of(b), 3U);
287 ASSERT_EQ(a.find_first_not_of(c), 0U);
288 ASSERT_EQ(b.find_first_not_of(a), StringPiece::npos);
289 ASSERT_EQ(c.find_first_not_of(a), StringPiece::npos);
290 ASSERT_EQ(f.find_first_not_of(a), 0U);
291 ASSERT_EQ(a.find_first_not_of(f), 0U);
292 ASSERT_EQ(a.find_first_not_of(d), 0U);
293 ASSERT_EQ(a.find_first_not_of(e), 0U);
294 // empty string nonsense
295 ASSERT_EQ(d.find_first_not_of(a), StringPiece::npos);
296 ASSERT_EQ(e.find_first_not_of(a), StringPiece::npos);
297 ASSERT_EQ(d.find_first_not_of(d), StringPiece::npos);
298 ASSERT_EQ(e.find_first_not_of(d), StringPiece::npos);
299 ASSERT_EQ(d.find_first_not_of(e), StringPiece::npos);
300 ASSERT_EQ(e.find_first_not_of(e), StringPiece::npos);
302 StringPiece h("====");
303 ASSERT_EQ(h.find_first_not_of('='), StringPiece::npos);
304 ASSERT_EQ(h.find_first_not_of('=', 3), StringPiece::npos);
305 ASSERT_EQ(h.find_first_not_of('\0'), 0U);
306 ASSERT_EQ(g.find_first_not_of('x'), 2U);
307 ASSERT_EQ(f.find_first_not_of('\0'), 0U);
308 ASSERT_EQ(f.find_first_not_of('\0', 3), 4U);
309 ASSERT_EQ(f.find_first_not_of('\0', 2), 2U);
310 // empty string nonsense
311 ASSERT_EQ(d.find_first_not_of('x'), StringPiece::npos);
312 ASSERT_EQ(e.find_first_not_of('x'), StringPiece::npos);
313 ASSERT_EQ(d.find_first_not_of('\0'), StringPiece::npos);
314 ASSERT_EQ(e.find_first_not_of('\0'), StringPiece::npos);
316 // StringPiece g("xx not found bb");
317 StringPiece i("56");
318 ASSERT_EQ(h.find_last_of(a), StringPiece::npos);
319 ASSERT_EQ(g.find_last_of(a), g.size()-1);
320 ASSERT_EQ(a.find_last_of(b), 2U);
321 ASSERT_EQ(a.find_last_of(c), a.size()-1);
322 ASSERT_EQ(f.find_last_of(i), 6U);
323 ASSERT_EQ(a.find_last_of('a'), 0U);
324 ASSERT_EQ(a.find_last_of('b'), 1U);
325 ASSERT_EQ(a.find_last_of('z'), 25U);
326 ASSERT_EQ(a.find_last_of('a', 5), 0U);
327 ASSERT_EQ(a.find_last_of('b', 5), 1U);
328 ASSERT_EQ(a.find_last_of('b', 0), StringPiece::npos);
329 ASSERT_EQ(a.find_last_of('z', 25), 25U);
330 ASSERT_EQ(a.find_last_of('z', 24), StringPiece::npos);
331 ASSERT_EQ(f.find_last_of(i, 5), 5U);
332 ASSERT_EQ(f.find_last_of(i, 6), 6U);
333 ASSERT_EQ(f.find_last_of(a, 4), StringPiece::npos);
334 // empty string nonsense
335 ASSERT_EQ(f.find_last_of(d), StringPiece::npos);
336 ASSERT_EQ(f.find_last_of(e), StringPiece::npos);
337 ASSERT_EQ(f.find_last_of(d, 4), StringPiece::npos);
338 ASSERT_EQ(f.find_last_of(e, 4), StringPiece::npos);
339 ASSERT_EQ(d.find_last_of(d), StringPiece::npos);
340 ASSERT_EQ(d.find_last_of(e), StringPiece::npos);
341 ASSERT_EQ(e.find_last_of(d), StringPiece::npos);
342 ASSERT_EQ(e.find_last_of(e), StringPiece::npos);
343 ASSERT_EQ(d.find_last_of(f), StringPiece::npos);
344 ASSERT_EQ(e.find_last_of(f), StringPiece::npos);
345 ASSERT_EQ(d.find_last_of(d, 4), StringPiece::npos);
346 ASSERT_EQ(d.find_last_of(e, 4), StringPiece::npos);
347 ASSERT_EQ(e.find_last_of(d, 4), StringPiece::npos);
348 ASSERT_EQ(e.find_last_of(e, 4), StringPiece::npos);
349 ASSERT_EQ(d.find_last_of(f, 4), StringPiece::npos);
350 ASSERT_EQ(e.find_last_of(f, 4), StringPiece::npos);
352 ASSERT_EQ(a.find_last_not_of(b), a.size()-1);
353 ASSERT_EQ(a.find_last_not_of(c), 22U);
354 ASSERT_EQ(b.find_last_not_of(a), StringPiece::npos);
355 ASSERT_EQ(b.find_last_not_of(b), StringPiece::npos);
356 ASSERT_EQ(f.find_last_not_of(i), 4U);
357 ASSERT_EQ(a.find_last_not_of(c, 24), 22U);
358 ASSERT_EQ(a.find_last_not_of(b, 3), 3U);
359 ASSERT_EQ(a.find_last_not_of(b, 2), StringPiece::npos);
360 // empty string nonsense
361 ASSERT_EQ(f.find_last_not_of(d), f.size()-1);
362 ASSERT_EQ(f.find_last_not_of(e), f.size()-1);
363 ASSERT_EQ(f.find_last_not_of(d, 4), 4U);
364 ASSERT_EQ(f.find_last_not_of(e, 4), 4U);
365 ASSERT_EQ(d.find_last_not_of(d), StringPiece::npos);
366 ASSERT_EQ(d.find_last_not_of(e), StringPiece::npos);
367 ASSERT_EQ(e.find_last_not_of(d), StringPiece::npos);
368 ASSERT_EQ(e.find_last_not_of(e), StringPiece::npos);
369 ASSERT_EQ(d.find_last_not_of(f), StringPiece::npos);
370 ASSERT_EQ(e.find_last_not_of(f), StringPiece::npos);
371 ASSERT_EQ(d.find_last_not_of(d, 4), StringPiece::npos);
372 ASSERT_EQ(d.find_last_not_of(e, 4), StringPiece::npos);
373 ASSERT_EQ(e.find_last_not_of(d, 4), StringPiece::npos);
374 ASSERT_EQ(e.find_last_not_of(e, 4), StringPiece::npos);
375 ASSERT_EQ(d.find_last_not_of(f, 4), StringPiece::npos);
376 ASSERT_EQ(e.find_last_not_of(f, 4), StringPiece::npos);
378 ASSERT_EQ(h.find_last_not_of('x'), h.size() - 1);
379 ASSERT_EQ(h.find_last_not_of('='), StringPiece::npos);
380 ASSERT_EQ(b.find_last_not_of('c'), 1U);
381 ASSERT_EQ(h.find_last_not_of('x', 2), 2U);
382 ASSERT_EQ(h.find_last_not_of('=', 2), StringPiece::npos);
383 ASSERT_EQ(b.find_last_not_of('b', 1), 0U);
384 // empty string nonsense
385 ASSERT_EQ(d.find_last_not_of('x'), StringPiece::npos);
386 ASSERT_EQ(e.find_last_not_of('x'), StringPiece::npos);
387 ASSERT_EQ(d.find_last_not_of('\0'), StringPiece::npos);
388 ASSERT_EQ(e.find_last_not_of('\0'), StringPiece::npos);
390 ASSERT_EQ(a.substr(0, 3), b);
391 ASSERT_EQ(a.substr(23), c);
392 ASSERT_EQ(a.substr(23, 3), c);
393 ASSERT_EQ(a.substr(23, 99), c);
394 ASSERT_EQ(a.substr(0), a);
395 ASSERT_EQ(a.substr(3, 2), "de");
396 // empty string nonsense
397 ASSERT_EQ(a.substr(99, 2), e);
398 ASSERT_EQ(d.substr(99), e);
399 ASSERT_EQ(d.substr(0, 99), e);
400 ASSERT_EQ(d.substr(99, 99), e);
403 TEST(StringPieceTest, CheckCustom) {
404 StringPiece a("foobar");
405 std::string s1("123");
406 s1 += '\0';
407 s1 += "456";
408 StringPiece b(s1);
409 StringPiece e;
410 std::string s2;
412 // CopyToString
413 a.CopyToString(&s2);
414 ASSERT_EQ(s2.size(), 6U);
415 ASSERT_EQ(s2, "foobar");
416 b.CopyToString(&s2);
417 ASSERT_EQ(s2.size(), 7U);
418 ASSERT_EQ(s1, s2);
419 e.CopyToString(&s2);
420 ASSERT_TRUE(s2.empty());
422 // AppendToString
423 s2.erase();
424 a.AppendToString(&s2);
425 ASSERT_EQ(s2.size(), 6U);
426 ASSERT_EQ(s2, "foobar");
427 a.AppendToString(&s2);
428 ASSERT_EQ(s2.size(), 12U);
429 ASSERT_EQ(s2, "foobarfoobar");
431 // starts_with
432 ASSERT_TRUE(a.starts_with(a));
433 ASSERT_TRUE(a.starts_with("foo"));
434 ASSERT_TRUE(a.starts_with(e));
435 ASSERT_TRUE(b.starts_with(s1));
436 ASSERT_TRUE(b.starts_with(b));
437 ASSERT_TRUE(b.starts_with(e));
438 ASSERT_TRUE(e.starts_with(""));
439 ASSERT_TRUE(!a.starts_with(b));
440 ASSERT_TRUE(!b.starts_with(a));
441 ASSERT_TRUE(!e.starts_with(a));
443 // ends with
444 ASSERT_TRUE(a.ends_with(a));
445 ASSERT_TRUE(a.ends_with("bar"));
446 ASSERT_TRUE(a.ends_with(e));
447 ASSERT_TRUE(b.ends_with(s1));
448 ASSERT_TRUE(b.ends_with(b));
449 ASSERT_TRUE(b.ends_with(e));
450 ASSERT_TRUE(e.ends_with(""));
451 ASSERT_TRUE(!a.ends_with(b));
452 ASSERT_TRUE(!b.ends_with(a));
453 ASSERT_TRUE(!e.ends_with(a));
455 // remove_prefix
456 StringPiece c(a);
457 c.remove_prefix(3);
458 ASSERT_EQ(c, "bar");
459 c = a;
460 c.remove_prefix(0);
461 ASSERT_EQ(c, a);
462 c.remove_prefix(c.size());
463 ASSERT_EQ(c, e);
465 // remove_suffix
466 c = a;
467 c.remove_suffix(3);
468 ASSERT_EQ(c, "foo");
469 c = a;
470 c.remove_suffix(0);
471 ASSERT_EQ(c, a);
472 c.remove_suffix(c.size());
473 ASSERT_EQ(c, e);
475 // set
476 c.set("foobar", 6);
477 ASSERT_EQ(c, a);
478 c.set("foobar", 0);
479 ASSERT_EQ(c, e);
480 c.set("foobar", 7);
481 ASSERT_NE(c, a);
483 c.set("foobar");
484 ASSERT_EQ(c, a);
486 c.set(static_cast<const void*>("foobar"), 6);
487 ASSERT_EQ(c, a);
488 c.set(static_cast<const void*>("foobar"), 0);
489 ASSERT_EQ(c, e);
490 c.set(static_cast<const void*>("foobar"), 7);
491 ASSERT_NE(c, a);
493 // as_string
494 std::string s3(a.as_string().c_str(), 7);
495 ASSERT_EQ(c, s3);
496 std::string s4(e.as_string());
497 ASSERT_TRUE(s4.empty());
500 TEST(StringPieceTest, CheckNULL) {
501 // we used to crash here, but now we don't.
502 StringPiece s(NULL);
503 ASSERT_EQ(s.data(), (const char*)NULL);
504 ASSERT_EQ(s.size(), 0U);
506 s.set(NULL);
507 ASSERT_EQ(s.data(), (const char*)NULL);
508 ASSERT_EQ(s.size(), 0U);
511 TEST(StringPieceTest, CheckComparisons2) {
512 StringPiece abc("abcdefghijklmnopqrstuvwxyz");
514 // check comparison operations on strings longer than 4 bytes.
515 ASSERT_TRUE(abc == StringPiece("abcdefghijklmnopqrstuvwxyz"));
516 ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyz")) == 0);
518 ASSERT_TRUE(abc < StringPiece("abcdefghijklmnopqrstuvwxzz"));
519 ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxzz")) < 0);
521 ASSERT_TRUE(abc > StringPiece("abcdefghijklmnopqrstuvwxyy"));
522 ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyy")) > 0);
524 // starts_with
525 ASSERT_TRUE(abc.starts_with(abc));
526 ASSERT_TRUE(abc.starts_with("abcdefghijklm"));
527 ASSERT_TRUE(!abc.starts_with("abcdefguvwxyz"));
529 // ends_with
530 ASSERT_TRUE(abc.ends_with(abc));
531 ASSERT_TRUE(!abc.ends_with("abcdefguvwxyz"));
532 ASSERT_TRUE(abc.ends_with("nopqrstuvwxyz"));
535 TEST(StringPieceTest, StringCompareNotAmbiguous) {
536 ASSERT_TRUE("hello" == std::string("hello"));
537 ASSERT_TRUE("hello" < std::string("world"));
540 TEST(StringPieceTest, HeterogenousStringPieceEquals) {
541 ASSERT_TRUE(StringPiece("hello") == std::string("hello"));
542 ASSERT_TRUE("hello" == StringPiece("hello"));
545 TEST(StringPiece16Test, CheckComparisonOperators) {
546 ASSERT_TRUE(StringPiece16(string16()) ==
547 StringPiece16(string16()));
548 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) ==
549 StringPiece16(ASCIIToUTF16("a")));
550 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) ==
551 StringPiece16(ASCIIToUTF16("aa")));
552 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) ==
553 StringPiece16(string16()));
554 ASSERT_FALSE(StringPiece16(string16()) ==
555 StringPiece16(ASCIIToUTF16("a")));
556 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) ==
557 StringPiece16(ASCIIToUTF16("b")));
558 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) ==
559 StringPiece16(ASCIIToUTF16("aa")));
560 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) ==
561 StringPiece16(ASCIIToUTF16("a")));
563 ASSERT_FALSE(StringPiece16(string16()) !=
564 StringPiece16(string16()));
565 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) !=
566 StringPiece16(ASCIIToUTF16("a")));
567 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) !=
568 StringPiece16(ASCIIToUTF16("aa")));
569 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) !=
570 StringPiece16(string16()));
571 ASSERT_TRUE(StringPiece16(string16()) !=
572 StringPiece16(ASCIIToUTF16("a")));
573 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) !=
574 StringPiece16(ASCIIToUTF16("b")));
575 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) !=
576 StringPiece16(ASCIIToUTF16("aa")));
577 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) !=
578 StringPiece16(ASCIIToUTF16("a")));
580 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <
581 StringPiece16(ASCIIToUTF16("b")));
582 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <
583 StringPiece16(ASCIIToUTF16("aa")));
584 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <
585 StringPiece16(ASCIIToUTF16("b")));
586 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <
587 StringPiece16(ASCIIToUTF16("bb")));
588 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) <
589 StringPiece16(ASCIIToUTF16("a")));
590 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <
591 StringPiece16(ASCIIToUTF16("a")));
592 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <
593 StringPiece16(ASCIIToUTF16("a")));
594 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <
595 StringPiece16(ASCIIToUTF16("aa")));
596 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <
597 StringPiece16(ASCIIToUTF16("aa")));
599 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
600 StringPiece16(ASCIIToUTF16("a")));
601 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
602 StringPiece16(ASCIIToUTF16("b")));
603 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
604 StringPiece16(ASCIIToUTF16("aa")));
605 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
606 StringPiece16(ASCIIToUTF16("b")));
607 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
608 StringPiece16(ASCIIToUTF16("bb")));
609 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
610 StringPiece16(ASCIIToUTF16("a")));
611 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <=
612 StringPiece16(ASCIIToUTF16("a")));
613 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
614 StringPiece16(ASCIIToUTF16("aa")));
615 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <=
616 StringPiece16(ASCIIToUTF16("aa")));
618 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
619 StringPiece16(ASCIIToUTF16("a")));
620 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
621 StringPiece16(ASCIIToUTF16("b")));
622 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
623 StringPiece16(ASCIIToUTF16("aa")));
624 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
625 StringPiece16(ASCIIToUTF16("b")));
626 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
627 StringPiece16(ASCIIToUTF16("bb")));
628 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
629 StringPiece16(ASCIIToUTF16("a")));
630 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <=
631 StringPiece16(ASCIIToUTF16("a")));
632 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
633 StringPiece16(ASCIIToUTF16("aa")));
634 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <=
635 StringPiece16(ASCIIToUTF16("aa")));
637 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >=
638 StringPiece16(ASCIIToUTF16("b")));
639 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >=
640 StringPiece16(ASCIIToUTF16("aa")));
641 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >=
642 StringPiece16(ASCIIToUTF16("b")));
643 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >=
644 StringPiece16(ASCIIToUTF16("bb")));
645 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) >=
646 StringPiece16(ASCIIToUTF16("a")));
647 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >=
648 StringPiece16(ASCIIToUTF16("a")));
649 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) >=
650 StringPiece16(ASCIIToUTF16("a")));
651 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >=
652 StringPiece16(ASCIIToUTF16("aa")));
653 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("bb")) >=
654 StringPiece16(ASCIIToUTF16("aa")));
656 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >
657 StringPiece16(ASCIIToUTF16("a")));
658 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >
659 StringPiece16(ASCIIToUTF16("b")));
660 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >
661 StringPiece16(ASCIIToUTF16("aa")));
662 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >
663 StringPiece16(ASCIIToUTF16("b")));
664 ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >
665 StringPiece16(ASCIIToUTF16("bb")));
666 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >
667 StringPiece16(ASCIIToUTF16("a")));
668 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) >
669 StringPiece16(ASCIIToUTF16("a")));
670 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >
671 StringPiece16(ASCIIToUTF16("aa")));
672 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("bb")) >
673 StringPiece16(ASCIIToUTF16("aa")));
675 string16 x;
676 for (int i = 0; i < 256; i++) {
677 x += 'a';
678 string16 y = x;
679 ASSERT_EQ(StringPiece16(x), StringPiece16(y));
680 for (int j = 0; j < i; j++) {
681 string16 z = x;
682 z[j] = 'b'; // Differs in position 'j'
683 ASSERT_NE(StringPiece16(x), StringPiece16(z));
688 TEST(StringPiece16Test, CheckSTL) {
689 string16 first = ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz");
690 StringPiece16 a(first);
691 string16 second = ASCIIToUTF16("abc");
692 StringPiece16 b(second.c_str());
693 string16 third = ASCIIToUTF16("xyz");
694 StringPiece16 c(third.c_str(), third.size());
695 string16 fourth = ASCIIToUTF16("foobarrandomstuff");
696 StringPiece16 d(fourth.c_str(), 6);
697 StringPiece16 e;
698 // Check some non-ascii characters.
699 string16 fifth(ASCIIToUTF16("123"));
700 fifth.push_back(0x0000);
701 fifth.push_back(0xd8c5);
702 fifth.push_back(0xdffe);
703 StringPiece16 f(fifth);
705 ASSERT_EQ(a[6], 'g');
706 ASSERT_EQ(b[0], 'a');
707 ASSERT_EQ(c[2], 'z');
708 ASSERT_EQ(f[3], '\0');
709 ASSERT_EQ(f[5], static_cast<char16>(0xdffe));
711 ASSERT_EQ(*d.data(), 'f');
712 ASSERT_EQ(d.data()[5], 'r');
713 ASSERT_TRUE(e.data() == NULL);
715 ASSERT_EQ(*a.begin(), 'a');
716 ASSERT_EQ(*(b.begin() + 2), 'c');
717 ASSERT_EQ(*(c.end() - 1), 'z');
719 ASSERT_EQ(*a.rbegin(), 'z');
720 ASSERT_EQ(*(b.rbegin() + 2), 'a');
721 ASSERT_EQ(*(c.rend() - 1), 'x');
722 ASSERT_TRUE(a.rbegin() + 26 == a.rend());
724 ASSERT_EQ(a.size(), 26U);
725 ASSERT_EQ(b.size(), 3U);
726 ASSERT_EQ(c.size(), 3U);
727 ASSERT_EQ(d.size(), 6U);
728 ASSERT_EQ(e.size(), 0U);
729 ASSERT_EQ(f.size(), 6U);
731 ASSERT_TRUE(!d.empty());
732 ASSERT_TRUE(d.begin() != d.end());
733 ASSERT_TRUE(d.begin() + 6 == d.end());
735 ASSERT_TRUE(e.empty());
736 ASSERT_TRUE(e.begin() == e.end());
738 d.clear();
739 ASSERT_EQ(d.size(), 0U);
740 ASSERT_TRUE(d.empty());
741 ASSERT_TRUE(d.data() == NULL);
742 ASSERT_TRUE(d.begin() == d.end());
744 ASSERT_GE(a.max_size(), a.capacity());
745 ASSERT_GE(a.capacity(), a.size());
748 TEST(StringPiece16Test, CheckNULL) {
749 StringPiece16 s(NULL);
750 ASSERT_EQ(s.data(), (const char16*)NULL);
751 ASSERT_EQ(s.size(), 0U);
753 s.set(NULL);
754 ASSERT_EQ(s.data(), (const char16*)NULL);
755 ASSERT_EQ(s.size(), 0U);
757 string16 str = s.as_string16();
758 ASSERT_EQ(s.data(), (const char16*)NULL);
759 ASSERT_EQ(s.size(), 0U);
762 TEST(StringPiece16Test, HeterogenousStringPieceEquals) {
763 ASSERT_TRUE(StringPiece16(ASCIIToUTF16("hello")) == ASCIIToUTF16("hello"));
766 TEST(StringPiece16Test, CheckConversion) {
767 // Make sure that we can convert from UTF8 to UTF16 and back. We use a two
768 // byte character (G clef) to test this.
769 ASSERT_EQ(
770 UTF16ToUTF8(
771 StringPiece16(UTF8ToUTF16("\xf0\x9d\x84\x9e")).as_string16()),
772 "\xf0\x9d\x84\x9e");
775 } // namespace base