string_view_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <stdlib.h>
18 #include <iomanip>
19 #include <iterator>
20 #include <limits>
21 #include <map>
22 #include <sstream>
23 #include <stdexcept>
24 #include <string>
25 #include <type_traits>
26 #include <utility>
27 
28 #include "gtest/gtest.h"
29 #include "absl/base/config.h"
31 
32 #ifdef __ANDROID__
33 // Android assert messages only go to system log, so death tests cannot inspect
34 // the message for matching.
35 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
36  EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
37 #else
38 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
39  EXPECT_DEATH_IF_SUPPORTED(statement, regex)
40 #endif
41 
42 namespace {
43 
44 // A minimal allocator that uses malloc().
45 template <typename T>
46 struct Mallocator {
47  typedef T value_type;
48  typedef size_t size_type;
49  typedef ptrdiff_t difference_type;
50  typedef T* pointer;
51  typedef const T* const_pointer;
52  typedef T& reference;
53  typedef const T& const_reference;
54 
55  size_type max_size() const {
56  return size_t(std::numeric_limits<size_type>::max()) / sizeof(value_type);
57  }
58  template <typename U>
59  struct rebind {
60  typedef Mallocator<U> other;
61  };
62  Mallocator() = default;
63  template <class U>
64  Mallocator(const Mallocator<U>&) {} // NOLINT(runtime/explicit)
65 
66  T* allocate(size_t n) { return static_cast<T*>(std::malloc(n * sizeof(T))); }
67  void deallocate(T* p, size_t) { std::free(p); }
68 };
69 template <typename T, typename U>
70 bool operator==(const Mallocator<T>&, const Mallocator<U>&) {
71  return true;
72 }
73 template <typename T, typename U>
74 bool operator!=(const Mallocator<T>&, const Mallocator<U>&) {
75  return false;
76 }
77 
78 TEST(StringViewTest, Ctor) {
79  {
80  // Null.
82  EXPECT_TRUE(s10.data() == nullptr);
83  EXPECT_EQ(0, s10.length());
84  }
85 
86  {
87  // const char* without length.
88  const char* hello = "hello";
89  absl::string_view s20(hello);
90  EXPECT_TRUE(s20.data() == hello);
91  EXPECT_EQ(5, s20.length());
92 
93  // const char* with length.
94  absl::string_view s21(hello, 4);
95  EXPECT_TRUE(s21.data() == hello);
96  EXPECT_EQ(4, s21.length());
97 
98  // Not recommended, but valid C++
99  absl::string_view s22(hello, 6);
100  EXPECT_TRUE(s22.data() == hello);
101  EXPECT_EQ(6, s22.length());
102  }
103 
104  {
105  // std::string.
106  std::string hola = "hola";
107  absl::string_view s30(hola);
108  EXPECT_TRUE(s30.data() == hola.data());
109  EXPECT_EQ(4, s30.length());
110 
111  // std::string with embedded '\0'.
112  hola.push_back('\0');
113  hola.append("h2");
114  hola.push_back('\0');
115  absl::string_view s31(hola);
116  EXPECT_TRUE(s31.data() == hola.data());
117  EXPECT_EQ(8, s31.length());
118  }
119 
120  {
121  using mstring =
122  std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
123  mstring str1("BUNGIE-JUMPING!");
124  const mstring str2("SLEEPING!");
125 
126  absl::string_view s1(str1);
127  s1.remove_prefix(strlen("BUNGIE-JUM"));
128 
129  absl::string_view s2(str2);
130  s2.remove_prefix(strlen("SLEE"));
131 
132  EXPECT_EQ(s1, s2);
133  EXPECT_EQ(s1, "PING!");
134  }
135 
136  // TODO(mec): absl::string_view(const absl::string_view&);
137 }
138 
139 TEST(StringViewTest, Swap) {
140  absl::string_view a("a");
141  absl::string_view b("bbb");
142  EXPECT_TRUE(noexcept(a.swap(b)));
143  a.swap(b);
144  EXPECT_EQ(a, "bbb");
145  EXPECT_EQ(b, "a");
146  a.swap(b);
147  EXPECT_EQ(a, "a");
148  EXPECT_EQ(b, "bbb");
149 }
150 
151 TEST(StringViewTest, STLComparator) {
152  std::string s1("foo");
153  std::string s2("bar");
154  std::string s3("baz");
155 
156  absl::string_view p1(s1);
157  absl::string_view p2(s2);
158  absl::string_view p3(s3);
159 
160  typedef std::map<absl::string_view, int> TestMap;
161  TestMap map;
162 
163  map.insert(std::make_pair(p1, 0));
164  map.insert(std::make_pair(p2, 1));
165  map.insert(std::make_pair(p3, 2));
166  EXPECT_EQ(map.size(), 3);
167 
168  TestMap::const_iterator iter = map.begin();
169  EXPECT_EQ(iter->second, 1);
170  ++iter;
171  EXPECT_EQ(iter->second, 2);
172  ++iter;
173  EXPECT_EQ(iter->second, 0);
174  ++iter;
175  EXPECT_TRUE(iter == map.end());
176 
177  TestMap::iterator new_iter = map.find("zot");
178  EXPECT_TRUE(new_iter == map.end());
179 
180  new_iter = map.find("bar");
181  EXPECT_TRUE(new_iter != map.end());
182 
183  map.erase(new_iter);
184  EXPECT_EQ(map.size(), 2);
185 
186  iter = map.begin();
187  EXPECT_EQ(iter->second, 2);
188  ++iter;
189  EXPECT_EQ(iter->second, 0);
190  ++iter;
191  EXPECT_TRUE(iter == map.end());
192 }
193 
194 #define COMPARE(result, op, x, y) \
195  EXPECT_EQ(result, absl::string_view((x)) op absl::string_view((y))); \
196  EXPECT_EQ(result, absl::string_view((x)).compare(absl::string_view((y))) op 0)
197 
198 TEST(StringViewTest, ComparisonOperators) {
199  COMPARE(true, ==, "", "");
200  COMPARE(true, ==, "", absl::string_view());
201  COMPARE(true, ==, absl::string_view(), "");
202  COMPARE(true, ==, "a", "a");
203  COMPARE(true, ==, "aa", "aa");
204  COMPARE(false, ==, "a", "");
205  COMPARE(false, ==, "", "a");
206  COMPARE(false, ==, "a", "b");
207  COMPARE(false, ==, "a", "aa");
208  COMPARE(false, ==, "aa", "a");
209 
210  COMPARE(false, !=, "", "");
211  COMPARE(false, !=, "a", "a");
212  COMPARE(false, !=, "aa", "aa");
213  COMPARE(true, !=, "a", "");
214  COMPARE(true, !=, "", "a");
215  COMPARE(true, !=, "a", "b");
216  COMPARE(true, !=, "a", "aa");
217  COMPARE(true, !=, "aa", "a");
218 
219  COMPARE(true, <, "a", "b");
220  COMPARE(true, <, "a", "aa");
221  COMPARE(true, <, "aa", "b");
222  COMPARE(true, <, "aa", "bb");
223  COMPARE(false, <, "a", "a");
224  COMPARE(false, <, "b", "a");
225  COMPARE(false, <, "aa", "a");
226  COMPARE(false, <, "b", "aa");
227  COMPARE(false, <, "bb", "aa");
228 
229  COMPARE(true, <=, "a", "a");
230  COMPARE(true, <=, "a", "b");
231  COMPARE(true, <=, "a", "aa");
232  COMPARE(true, <=, "aa", "b");
233  COMPARE(true, <=, "aa", "bb");
234  COMPARE(false, <=, "b", "a");
235  COMPARE(false, <=, "aa", "a");
236  COMPARE(false, <=, "b", "aa");
237  COMPARE(false, <=, "bb", "aa");
238 
239  COMPARE(false, >=, "a", "b");
240  COMPARE(false, >=, "a", "aa");
241  COMPARE(false, >=, "aa", "b");
242  COMPARE(false, >=, "aa", "bb");
243  COMPARE(true, >=, "a", "a");
244  COMPARE(true, >=, "b", "a");
245  COMPARE(true, >=, "aa", "a");
246  COMPARE(true, >=, "b", "aa");
247  COMPARE(true, >=, "bb", "aa");
248 
249  COMPARE(false, >, "a", "a");
250  COMPARE(false, >, "a", "b");
251  COMPARE(false, >, "a", "aa");
252  COMPARE(false, >, "aa", "b");
253  COMPARE(false, >, "aa", "bb");
254  COMPARE(true, >, "b", "a");
255  COMPARE(true, >, "aa", "a");
256  COMPARE(true, >, "b", "aa");
257  COMPARE(true, >, "bb", "aa");
258 }
259 
260 TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {
261  std::string x;
262  for (int i = 0; i < 256; i++) {
263  x += 'a';
264  std::string y = x;
265  COMPARE(true, ==, x, y);
266  for (int j = 0; j < i; j++) {
267  std::string z = x;
268  z[j] = 'b'; // Differs in position 'j'
269  COMPARE(false, ==, x, z);
270  COMPARE(true, <, x, z);
271  COMPARE(true, >, z, x);
272  if (j + 1 < i) {
273  z[j + 1] = 'A'; // Differs in position 'j+1' as well
274  COMPARE(false, ==, x, z);
275  COMPARE(true, <, x, z);
276  COMPARE(true, >, z, x);
277  z[j + 1] = 'z'; // Differs in position 'j+1' as well
278  COMPARE(false, ==, x, z);
279  COMPARE(true, <, x, z);
280  COMPARE(true, >, z, x);
281  }
282  }
283  }
284 }
285 #undef COMPARE
286 
287 // Sadly, our users often confuse std::string::npos with
288 // absl::string_view::npos; So much so that we test here that they are the same.
289 // They need to both be unsigned, and both be the maximum-valued integer of
290 // their type.
291 
292 template <typename T>
293 struct is_type {
294  template <typename U>
295  static bool same(U) {
296  return false;
297  }
298  static bool same(T) { return true; }
299 };
300 
301 TEST(StringViewTest, NposMatchesStdStringView) {
302  EXPECT_EQ(absl::string_view::npos, std::string::npos);
303 
304  EXPECT_TRUE(is_type<size_t>::same(absl::string_view::npos));
305  EXPECT_FALSE(is_type<size_t>::same(""));
306 
307  // Make sure absl::string_view::npos continues to be a header constant.
308  char test[absl::string_view::npos & 1] = {0};
309  EXPECT_EQ(0, test[0]);
310 }
311 
312 TEST(StringViewTest, STL1) {
313  const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
314  const absl::string_view b("abc");
315  const absl::string_view c("xyz");
316  const absl::string_view d("foobar");
317  const absl::string_view e;
318  std::string temp("123");
319  temp += '\0';
320  temp += "456";
321  const absl::string_view f(temp);
322 
323  EXPECT_EQ(a[6], 'g');
324  EXPECT_EQ(b[0], 'a');
325  EXPECT_EQ(c[2], 'z');
326  EXPECT_EQ(f[3], '\0');
327  EXPECT_EQ(f[5], '5');
328 
329  EXPECT_EQ(*d.data(), 'f');
330  EXPECT_EQ(d.data()[5], 'r');
331  EXPECT_TRUE(e.data() == nullptr);
332 
333  EXPECT_EQ(*a.begin(), 'a');
334  EXPECT_EQ(*(b.begin() + 2), 'c');
335  EXPECT_EQ(*(c.end() - 1), 'z');
336 
337  EXPECT_EQ(*a.rbegin(), 'z');
338  EXPECT_EQ(*(b.rbegin() + 2), 'a');
339  EXPECT_EQ(*(c.rend() - 1), 'x');
340  EXPECT_TRUE(a.rbegin() + 26 == a.rend());
341 
342  EXPECT_EQ(a.size(), 26);
343  EXPECT_EQ(b.size(), 3);
344  EXPECT_EQ(c.size(), 3);
345  EXPECT_EQ(d.size(), 6);
346  EXPECT_EQ(e.size(), 0);
347  EXPECT_EQ(f.size(), 7);
348 
349  EXPECT_TRUE(!d.empty());
350  EXPECT_TRUE(d.begin() != d.end());
351  EXPECT_TRUE(d.begin() + 6 == d.end());
352 
353  EXPECT_TRUE(e.empty());
354  EXPECT_TRUE(e.begin() == e.end());
355 
356  char buf[4] = { '%', '%', '%', '%' };
357  EXPECT_EQ(a.copy(buf, 4), 4);
358  EXPECT_EQ(buf[0], a[0]);
359  EXPECT_EQ(buf[1], a[1]);
360  EXPECT_EQ(buf[2], a[2]);
361  EXPECT_EQ(buf[3], a[3]);
362  EXPECT_EQ(a.copy(buf, 3, 7), 3);
363  EXPECT_EQ(buf[0], a[7]);
364  EXPECT_EQ(buf[1], a[8]);
365  EXPECT_EQ(buf[2], a[9]);
366  EXPECT_EQ(buf[3], a[3]);
367  EXPECT_EQ(c.copy(buf, 99), 3);
368  EXPECT_EQ(buf[0], c[0]);
369  EXPECT_EQ(buf[1], c[1]);
370  EXPECT_EQ(buf[2], c[2]);
371  EXPECT_EQ(buf[3], a[3]);
372 }
373 
374 // Separated from STL1() because some compilers produce an overly
375 // large stack frame for the combined function.
376 TEST(StringViewTest, STL2) {
377  const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
378  const absl::string_view b("abc");
379  const absl::string_view c("xyz");
380  absl::string_view d("foobar");
381  const absl::string_view e;
382  const absl::string_view f(
383  "123"
384  "\0"
385  "456",
386  7);
387 
388  d = absl::string_view();
389  EXPECT_EQ(d.size(), 0);
390  EXPECT_TRUE(d.empty());
391  EXPECT_TRUE(d.data() == nullptr);
392  EXPECT_TRUE(d.begin() == d.end());
393 
394  EXPECT_EQ(a.find(b), 0);
395  EXPECT_EQ(a.find(b, 1), absl::string_view::npos);
396  EXPECT_EQ(a.find(c), 23);
397  EXPECT_EQ(a.find(c, 9), 23);
399  EXPECT_EQ(b.find(c), absl::string_view::npos);
401  EXPECT_EQ(a.find(d), 0);
402  EXPECT_EQ(a.find(e), 0);
403  EXPECT_EQ(a.find(d, 12), 12);
404  EXPECT_EQ(a.find(e, 17), 17);
405  absl::string_view g("xx not found bb");
406  EXPECT_EQ(a.find(g), absl::string_view::npos);
407  // empty std::string nonsense
408  EXPECT_EQ(d.find(b), absl::string_view::npos);
409  EXPECT_EQ(e.find(b), absl::string_view::npos);
410  EXPECT_EQ(d.find(b, 4), absl::string_view::npos);
411  EXPECT_EQ(e.find(b, 7), absl::string_view::npos);
412 
413  size_t empty_search_pos = std::string().find(std::string());
414  EXPECT_EQ(d.find(d), empty_search_pos);
415  EXPECT_EQ(d.find(e), empty_search_pos);
416  EXPECT_EQ(e.find(d), empty_search_pos);
417  EXPECT_EQ(e.find(e), empty_search_pos);
418  EXPECT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
419  EXPECT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
420  EXPECT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
421  EXPECT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
422 
423  EXPECT_EQ(a.find('a'), 0);
424  EXPECT_EQ(a.find('c'), 2);
425  EXPECT_EQ(a.find('z'), 25);
426  EXPECT_EQ(a.find('$'), absl::string_view::npos);
427  EXPECT_EQ(a.find('\0'), absl::string_view::npos);
428  EXPECT_EQ(f.find('\0'), 3);
429  EXPECT_EQ(f.find('3'), 2);
430  EXPECT_EQ(f.find('5'), 5);
431  EXPECT_EQ(g.find('o'), 4);
432  EXPECT_EQ(g.find('o', 4), 4);
433  EXPECT_EQ(g.find('o', 5), 8);
434  EXPECT_EQ(a.find('b', 5), absl::string_view::npos);
435  // empty std::string nonsense
436  EXPECT_EQ(d.find('\0'), absl::string_view::npos);
437  EXPECT_EQ(e.find('\0'), absl::string_view::npos);
438  EXPECT_EQ(d.find('\0', 4), absl::string_view::npos);
439  EXPECT_EQ(e.find('\0', 7), absl::string_view::npos);
440  EXPECT_EQ(d.find('x'), absl::string_view::npos);
441  EXPECT_EQ(e.find('x'), absl::string_view::npos);
442  EXPECT_EQ(d.find('x', 4), absl::string_view::npos);
443  EXPECT_EQ(e.find('x', 7), absl::string_view::npos);
444 
445  EXPECT_EQ(a.rfind(b), 0);
446  EXPECT_EQ(a.rfind(b, 1), 0);
447  EXPECT_EQ(a.rfind(c), 23);
448  EXPECT_EQ(a.rfind(c, 22), absl::string_view::npos);
449  EXPECT_EQ(a.rfind(c, 1), absl::string_view::npos);
450  EXPECT_EQ(a.rfind(c, 0), absl::string_view::npos);
451  EXPECT_EQ(b.rfind(c), absl::string_view::npos);
452  EXPECT_EQ(b.rfind(c, 0), absl::string_view::npos);
453  EXPECT_EQ(a.rfind(d), std::string(a).rfind(std::string()));
454  EXPECT_EQ(a.rfind(e), std::string(a).rfind(std::string()));
455  EXPECT_EQ(a.rfind(d, 12), 12);
456  EXPECT_EQ(a.rfind(e, 17), 17);
457  EXPECT_EQ(a.rfind(g), absl::string_view::npos);
458  EXPECT_EQ(d.rfind(b), absl::string_view::npos);
459  EXPECT_EQ(e.rfind(b), absl::string_view::npos);
460  EXPECT_EQ(d.rfind(b, 4), absl::string_view::npos);
461  EXPECT_EQ(e.rfind(b, 7), absl::string_view::npos);
462  // empty std::string nonsense
463  EXPECT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
464  EXPECT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
465  EXPECT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
466  EXPECT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
467  EXPECT_EQ(d.rfind(d), std::string().rfind(std::string()));
468  EXPECT_EQ(e.rfind(d), std::string().rfind(std::string()));
469  EXPECT_EQ(d.rfind(e), std::string().rfind(std::string()));
470  EXPECT_EQ(e.rfind(e), std::string().rfind(std::string()));
471 
472  EXPECT_EQ(g.rfind('o'), 8);
473  EXPECT_EQ(g.rfind('q'), absl::string_view::npos);
474  EXPECT_EQ(g.rfind('o', 8), 8);
475  EXPECT_EQ(g.rfind('o', 7), 4);
476  EXPECT_EQ(g.rfind('o', 3), absl::string_view::npos);
477  EXPECT_EQ(f.rfind('\0'), 3);
478  EXPECT_EQ(f.rfind('\0', 12), 3);
479  EXPECT_EQ(f.rfind('3'), 2);
480  EXPECT_EQ(f.rfind('5'), 5);
481  // empty std::string nonsense
482  EXPECT_EQ(d.rfind('o'), absl::string_view::npos);
483  EXPECT_EQ(e.rfind('o'), absl::string_view::npos);
484  EXPECT_EQ(d.rfind('o', 4), absl::string_view::npos);
485  EXPECT_EQ(e.rfind('o', 7), absl::string_view::npos);
486 }
487 
488 // Continued from STL2
489 TEST(StringViewTest, STL2FindFirst) {
490  const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
491  const absl::string_view b("abc");
492  const absl::string_view c("xyz");
493  absl::string_view d("foobar");
494  const absl::string_view e;
495  const absl::string_view f(
496  "123"
497  "\0"
498  "456",
499  7);
500  absl::string_view g("xx not found bb");
501 
502  d = absl::string_view();
503  EXPECT_EQ(a.find_first_of(b), 0);
504  EXPECT_EQ(a.find_first_of(b, 0), 0);
505  EXPECT_EQ(a.find_first_of(b, 1), 1);
506  EXPECT_EQ(a.find_first_of(b, 2), 2);
507  EXPECT_EQ(a.find_first_of(b, 3), absl::string_view::npos);
508  EXPECT_EQ(a.find_first_of(c), 23);
509  EXPECT_EQ(a.find_first_of(c, 23), 23);
510  EXPECT_EQ(a.find_first_of(c, 24), 24);
511  EXPECT_EQ(a.find_first_of(c, 25), 25);
512  EXPECT_EQ(a.find_first_of(c, 26), absl::string_view::npos);
513  EXPECT_EQ(g.find_first_of(b), 13);
514  EXPECT_EQ(g.find_first_of(c), 0);
515  EXPECT_EQ(a.find_first_of(f), absl::string_view::npos);
516  EXPECT_EQ(f.find_first_of(a), absl::string_view::npos);
517  // empty std::string nonsense
518  EXPECT_EQ(a.find_first_of(d), absl::string_view::npos);
519  EXPECT_EQ(a.find_first_of(e), absl::string_view::npos);
520  EXPECT_EQ(d.find_first_of(b), absl::string_view::npos);
521  EXPECT_EQ(e.find_first_of(b), absl::string_view::npos);
522  EXPECT_EQ(d.find_first_of(d), absl::string_view::npos);
523  EXPECT_EQ(e.find_first_of(d), absl::string_view::npos);
524  EXPECT_EQ(d.find_first_of(e), absl::string_view::npos);
525  EXPECT_EQ(e.find_first_of(e), absl::string_view::npos);
526 
527  EXPECT_EQ(a.find_first_not_of(b), 3);
528  EXPECT_EQ(a.find_first_not_of(c), 0);
531  EXPECT_EQ(f.find_first_not_of(a), 0);
532  EXPECT_EQ(a.find_first_not_of(f), 0);
533  EXPECT_EQ(a.find_first_not_of(d), 0);
534  EXPECT_EQ(a.find_first_not_of(e), 0);
535  // empty std::string nonsense
536  EXPECT_EQ(a.find_first_not_of(d), 0);
537  EXPECT_EQ(a.find_first_not_of(e), 0);
538  EXPECT_EQ(a.find_first_not_of(d, 1), 1);
539  EXPECT_EQ(a.find_first_not_of(e, 1), 1);
540  EXPECT_EQ(a.find_first_not_of(d, a.size() - 1), a.size() - 1);
541  EXPECT_EQ(a.find_first_not_of(e, a.size() - 1), a.size() - 1);
542  EXPECT_EQ(a.find_first_not_of(d, a.size()), absl::string_view::npos);
543  EXPECT_EQ(a.find_first_not_of(e, a.size()), absl::string_view::npos);
554 
555  absl::string_view h("====");
556  EXPECT_EQ(h.find_first_not_of('='), absl::string_view::npos);
557  EXPECT_EQ(h.find_first_not_of('=', 3), absl::string_view::npos);
558  EXPECT_EQ(h.find_first_not_of('\0'), 0);
559  EXPECT_EQ(g.find_first_not_of('x'), 2);
560  EXPECT_EQ(f.find_first_not_of('\0'), 0);
561  EXPECT_EQ(f.find_first_not_of('\0', 3), 4);
562  EXPECT_EQ(f.find_first_not_of('\0', 2), 2);
563  // empty std::string nonsense
564  EXPECT_EQ(d.find_first_not_of('x'), absl::string_view::npos);
565  EXPECT_EQ(e.find_first_not_of('x'), absl::string_view::npos);
566  EXPECT_EQ(d.find_first_not_of('\0'), absl::string_view::npos);
567  EXPECT_EQ(e.find_first_not_of('\0'), absl::string_view::npos);
568 }
569 
570 // Continued from STL2
571 TEST(StringViewTest, STL2FindLast) {
572  const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
573  const absl::string_view b("abc");
574  const absl::string_view c("xyz");
575  absl::string_view d("foobar");
576  const absl::string_view e;
577  const absl::string_view f(
578  "123"
579  "\0"
580  "456",
581  7);
582  absl::string_view g("xx not found bb");
583  absl::string_view h("====");
584  absl::string_view i("56");
585 
586  d = absl::string_view();
587  EXPECT_EQ(h.find_last_of(a), absl::string_view::npos);
588  EXPECT_EQ(g.find_last_of(a), g.size()-1);
589  EXPECT_EQ(a.find_last_of(b), 2);
590  EXPECT_EQ(a.find_last_of(c), a.size()-1);
591  EXPECT_EQ(f.find_last_of(i), 6);
592  EXPECT_EQ(a.find_last_of('a'), 0);
593  EXPECT_EQ(a.find_last_of('b'), 1);
594  EXPECT_EQ(a.find_last_of('z'), 25);
595  EXPECT_EQ(a.find_last_of('a', 5), 0);
596  EXPECT_EQ(a.find_last_of('b', 5), 1);
597  EXPECT_EQ(a.find_last_of('b', 0), absl::string_view::npos);
598  EXPECT_EQ(a.find_last_of('z', 25), 25);
599  EXPECT_EQ(a.find_last_of('z', 24), absl::string_view::npos);
600  EXPECT_EQ(f.find_last_of(i, 5), 5);
601  EXPECT_EQ(f.find_last_of(i, 6), 6);
602  EXPECT_EQ(f.find_last_of(a, 4), absl::string_view::npos);
603  // empty std::string nonsense
604  EXPECT_EQ(f.find_last_of(d), absl::string_view::npos);
605  EXPECT_EQ(f.find_last_of(e), absl::string_view::npos);
606  EXPECT_EQ(f.find_last_of(d, 4), absl::string_view::npos);
607  EXPECT_EQ(f.find_last_of(e, 4), absl::string_view::npos);
608  EXPECT_EQ(d.find_last_of(d), absl::string_view::npos);
609  EXPECT_EQ(d.find_last_of(e), absl::string_view::npos);
610  EXPECT_EQ(e.find_last_of(d), absl::string_view::npos);
611  EXPECT_EQ(e.find_last_of(e), absl::string_view::npos);
612  EXPECT_EQ(d.find_last_of(f), absl::string_view::npos);
613  EXPECT_EQ(e.find_last_of(f), absl::string_view::npos);
614  EXPECT_EQ(d.find_last_of(d, 4), absl::string_view::npos);
615  EXPECT_EQ(d.find_last_of(e, 4), absl::string_view::npos);
616  EXPECT_EQ(e.find_last_of(d, 4), absl::string_view::npos);
617  EXPECT_EQ(e.find_last_of(e, 4), absl::string_view::npos);
618  EXPECT_EQ(d.find_last_of(f, 4), absl::string_view::npos);
619  EXPECT_EQ(e.find_last_of(f, 4), absl::string_view::npos);
620 
621  EXPECT_EQ(a.find_last_not_of(b), a.size()-1);
622  EXPECT_EQ(a.find_last_not_of(c), 22);
623  EXPECT_EQ(b.find_last_not_of(a), absl::string_view::npos);
624  EXPECT_EQ(b.find_last_not_of(b), absl::string_view::npos);
625  EXPECT_EQ(f.find_last_not_of(i), 4);
626  EXPECT_EQ(a.find_last_not_of(c, 24), 22);
627  EXPECT_EQ(a.find_last_not_of(b, 3), 3);
628  EXPECT_EQ(a.find_last_not_of(b, 2), absl::string_view::npos);
629  // empty std::string nonsense
630  EXPECT_EQ(f.find_last_not_of(d), f.size()-1);
631  EXPECT_EQ(f.find_last_not_of(e), f.size()-1);
632  EXPECT_EQ(f.find_last_not_of(d, 4), 4);
633  EXPECT_EQ(f.find_last_not_of(e, 4), 4);
634  EXPECT_EQ(d.find_last_not_of(d), absl::string_view::npos);
635  EXPECT_EQ(d.find_last_not_of(e), absl::string_view::npos);
636  EXPECT_EQ(e.find_last_not_of(d), absl::string_view::npos);
637  EXPECT_EQ(e.find_last_not_of(e), absl::string_view::npos);
638  EXPECT_EQ(d.find_last_not_of(f), absl::string_view::npos);
639  EXPECT_EQ(e.find_last_not_of(f), absl::string_view::npos);
640  EXPECT_EQ(d.find_last_not_of(d, 4), absl::string_view::npos);
641  EXPECT_EQ(d.find_last_not_of(e, 4), absl::string_view::npos);
642  EXPECT_EQ(e.find_last_not_of(d, 4), absl::string_view::npos);
643  EXPECT_EQ(e.find_last_not_of(e, 4), absl::string_view::npos);
644  EXPECT_EQ(d.find_last_not_of(f, 4), absl::string_view::npos);
645  EXPECT_EQ(e.find_last_not_of(f, 4), absl::string_view::npos);
646 
647  EXPECT_EQ(h.find_last_not_of('x'), h.size() - 1);
648  EXPECT_EQ(h.find_last_not_of('='), absl::string_view::npos);
649  EXPECT_EQ(b.find_last_not_of('c'), 1);
650  EXPECT_EQ(h.find_last_not_of('x', 2), 2);
651  EXPECT_EQ(h.find_last_not_of('=', 2), absl::string_view::npos);
652  EXPECT_EQ(b.find_last_not_of('b', 1), 0);
653  // empty std::string nonsense
654  EXPECT_EQ(d.find_last_not_of('x'), absl::string_view::npos);
655  EXPECT_EQ(e.find_last_not_of('x'), absl::string_view::npos);
656  EXPECT_EQ(d.find_last_not_of('\0'), absl::string_view::npos);
657  EXPECT_EQ(e.find_last_not_of('\0'), absl::string_view::npos);
658 }
659 
660 // Continued from STL2
661 TEST(StringViewTest, STL2Substr) {
662  const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
663  const absl::string_view b("abc");
664  const absl::string_view c("xyz");
665  absl::string_view d("foobar");
666  const absl::string_view e;
667 
668  d = absl::string_view();
669  EXPECT_EQ(a.substr(0, 3), b);
670  EXPECT_EQ(a.substr(23), c);
671  EXPECT_EQ(a.substr(23, 3), c);
672  EXPECT_EQ(a.substr(23, 99), c);
673  EXPECT_EQ(a.substr(0), a);
674  EXPECT_EQ(a.substr(3, 2), "de");
675  // empty std::string nonsense
676  EXPECT_EQ(d.substr(0, 99), e);
677  // use of npos
678  EXPECT_EQ(a.substr(0, absl::string_view::npos), a);
679  EXPECT_EQ(a.substr(23, absl::string_view::npos), c);
680  // throw exception
681 #ifdef ABSL_HAVE_EXCEPTIONS
682  EXPECT_THROW((void)a.substr(99, 2), std::out_of_range);
683 #else
684  EXPECT_DEATH((void)a.substr(99, 2), "absl::string_view::substr");
685 #endif
686 }
687 
688 TEST(StringViewTest, TruncSubstr) {
689  const absl::string_view hi("hi");
690  EXPECT_EQ("", absl::ClippedSubstr(hi, 0, 0));
691  EXPECT_EQ("h", absl::ClippedSubstr(hi, 0, 1));
692  EXPECT_EQ("hi", absl::ClippedSubstr(hi, 0));
693  EXPECT_EQ("i", absl::ClippedSubstr(hi, 1));
694  EXPECT_EQ("", absl::ClippedSubstr(hi, 2));
695  EXPECT_EQ("", absl::ClippedSubstr(hi, 3)); // truncation
696  EXPECT_EQ("", absl::ClippedSubstr(hi, 3, 2)); // truncation
697 }
698 
699 TEST(StringViewTest, UTF8) {
700  std::string utf8 = "\u00E1";
701  std::string utf8_twice = utf8 + " " + utf8;
702  int utf8_len = strlen(utf8.data());
703  EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" "));
704  EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" \t"));
705 }
706 
707 TEST(StringViewTest, FindConformance) {
708  struct {
709  std::string haystack;
710  std::string needle;
711  } specs[] = {
712  {"", ""},
713  {"", "a"},
714  {"a", ""},
715  {"a", "a"},
716  {"a", "b"},
717  {"aa", ""},
718  {"aa", "a"},
719  {"aa", "b"},
720  {"ab", "a"},
721  {"ab", "b"},
722  {"abcd", ""},
723  {"abcd", "a"},
724  {"abcd", "d"},
725  {"abcd", "ab"},
726  {"abcd", "bc"},
727  {"abcd", "cd"},
728  {"abcd", "abcd"},
729  };
730  for (const auto& s : specs) {
731  SCOPED_TRACE(s.haystack);
732  SCOPED_TRACE(s.needle);
733  std::string st = s.haystack;
734  absl::string_view sp = s.haystack;
735  for (size_t i = 0; i <= sp.size(); ++i) {
736  size_t pos = (i == sp.size()) ? absl::string_view::npos : i;
737  SCOPED_TRACE(pos);
738  EXPECT_EQ(sp.find(s.needle, pos),
739  st.find(s.needle, pos));
740  EXPECT_EQ(sp.rfind(s.needle, pos),
741  st.rfind(s.needle, pos));
742  EXPECT_EQ(sp.find_first_of(s.needle, pos),
743  st.find_first_of(s.needle, pos));
744  EXPECT_EQ(sp.find_first_not_of(s.needle, pos),
745  st.find_first_not_of(s.needle, pos));
746  EXPECT_EQ(sp.find_last_of(s.needle, pos),
747  st.find_last_of(s.needle, pos));
748  EXPECT_EQ(sp.find_last_not_of(s.needle, pos),
749  st.find_last_not_of(s.needle, pos));
750  }
751  }
752 }
753 
754 TEST(StringViewTest, Remove) {
755  absl::string_view a("foobar");
756  std::string s1("123");
757  s1 += '\0';
758  s1 += "456";
760  std::string s2;
761 
762  // remove_prefix
763  absl::string_view c(a);
764  c.remove_prefix(3);
765  EXPECT_EQ(c, "bar");
766  c = a;
767  c.remove_prefix(0);
768  EXPECT_EQ(c, a);
769  c.remove_prefix(c.size());
770  EXPECT_EQ(c, e);
771 
772  // remove_suffix
773  c = a;
774  c.remove_suffix(3);
775  EXPECT_EQ(c, "foo");
776  c = a;
777  c.remove_suffix(0);
778  EXPECT_EQ(c, a);
779  c.remove_suffix(c.size());
780  EXPECT_EQ(c, e);
781 }
782 
783 TEST(StringViewTest, Set) {
784  absl::string_view a("foobar");
785  absl::string_view empty;
787 
788  // set
789  b = absl::string_view("foobar", 6);
790  EXPECT_EQ(b, a);
791  b = absl::string_view("foobar", 0);
792  EXPECT_EQ(b, empty);
793  b = absl::string_view("foobar", 7);
794  EXPECT_NE(b, a);
795 
796  b = absl::string_view("foobar");
797  EXPECT_EQ(b, a);
798 }
799 
800 TEST(StringViewTest, FrontBack) {
801  static const char arr[] = "abcd";
802  const absl::string_view csp(arr, 4);
803  EXPECT_EQ(&arr[0], &csp.front());
804  EXPECT_EQ(&arr[3], &csp.back());
805 }
806 
807 TEST(StringViewTest, FrontBackSingleChar) {
808  static const char c = 'a';
809  const absl::string_view csp(&c, 1);
810  EXPECT_EQ(&c, &csp.front());
811  EXPECT_EQ(&c, &csp.back());
812 }
813 
814 // `std::string_view::string_view(const char*)` calls
815 // `std::char_traits<char>::length(const char*)` to get the string length. In
816 // libc++, it doesn't allow `nullptr` in the constexpr context, with the error
817 // "read of dereferenced null pointer is not allowed in a constant expression".
818 // At run time, the behavior of `std::char_traits::length()` on `nullptr` is
819 // undefined by the standard and usually results in crash with libc++.
820 // In MSVC, creating a constexpr string_view from nullptr also triggers an
821 // "unevaluable pointer value" error. This compiler implementation conforms
822 // to the standard, but `absl::string_view` implements a different
823 // behavior for historical reasons. We work around tests that construct
824 // `string_view` from `nullptr` when using libc++.
825 #if !defined(ABSL_HAVE_STD_STRING_VIEW) || \
826  (!defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
827 #define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
828 #endif // !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
829 
830 TEST(StringViewTest, NULLInput) {
832  EXPECT_EQ(s.data(), nullptr);
833  EXPECT_EQ(s.size(), 0);
834 
835 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
836  s = absl::string_view(nullptr);
837  EXPECT_EQ(s.data(), nullptr);
838  EXPECT_EQ(s.size(), 0);
839 
840  // .ToString() on a absl::string_view with nullptr should produce the empty
841  // std::string.
842  EXPECT_EQ("", std::string(s));
843 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
844 }
845 
846 TEST(StringViewTest, Comparisons2) {
847  // The `compare` member has 6 overloads (v: string_view, s: const char*):
848  // (1) compare(v)
849  // (2) compare(pos1, count1, v)
850  // (3) compare(pos1, count1, v, pos2, count2)
851  // (4) compare(s)
852  // (5) compare(pos1, count1, s)
853  // (6) compare(pos1, count1, s, count2)
854 
855  absl::string_view abc("abcdefghijklmnopqrstuvwxyz");
856 
857  // check comparison operations on strings longer than 4 bytes.
858  EXPECT_EQ(abc, absl::string_view("abcdefghijklmnopqrstuvwxyz"));
859  EXPECT_EQ(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxyz")), 0);
860 
861  EXPECT_LT(abc, absl::string_view("abcdefghijklmnopqrstuvwxzz"));
862  EXPECT_LT(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxzz")), 0);
863 
864  EXPECT_GT(abc, absl::string_view("abcdefghijklmnopqrstuvwxyy"));
865  EXPECT_GT(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxyy")), 0);
866 
867  // The "substr" variants of `compare`.
868  absl::string_view digits("0123456789");
869  auto npos = absl::string_view::npos;
870 
871  // Taking string_view
872  EXPECT_EQ(digits.compare(3, npos, absl::string_view("3456789")), 0); // 2
873  EXPECT_EQ(digits.compare(3, 4, absl::string_view("3456")), 0); // 2
874  EXPECT_EQ(digits.compare(10, 0, absl::string_view()), 0); // 2
875  EXPECT_EQ(digits.compare(3, 4, absl::string_view("0123456789"), 3, 4),
876  0); // 3
877  EXPECT_LT(digits.compare(3, 4, absl::string_view("0123456789"), 3, 5),
878  0); // 3
879  EXPECT_LT(digits.compare(0, npos, absl::string_view("0123456789"), 3, 5),
880  0); // 3
881  // Taking const char*
882  EXPECT_EQ(digits.compare(3, 4, "3456"), 0); // 5
883  EXPECT_EQ(digits.compare(3, npos, "3456789"), 0); // 5
884  EXPECT_EQ(digits.compare(10, 0, ""), 0); // 5
885  EXPECT_EQ(digits.compare(3, 4, "0123456789", 3, 4), 0); // 6
886  EXPECT_LT(digits.compare(3, 4, "0123456789", 3, 5), 0); // 6
887  EXPECT_LT(digits.compare(0, npos, "0123456789", 3, 5), 0); // 6
888 }
889 
890 struct MyCharAlloc : std::allocator<char> {};
891 
892 TEST(StringViewTest, ExplicitConversionOperator) {
893  absl::string_view sp = "hi";
894  EXPECT_EQ(sp, std::string(sp));
895 }
896 
897 TEST(StringViewTest, NullSafeStringView) {
898  {
900  EXPECT_EQ(nullptr, s.data());
901  EXPECT_EQ(0, s.size());
902  EXPECT_EQ(absl::string_view(), s);
903  }
904  {
905  static const char kHi[] = "hi";
907  EXPECT_EQ(kHi, s.data());
908  EXPECT_EQ(strlen(kHi), s.size());
909  EXPECT_EQ(absl::string_view("hi"), s);
910  }
911 }
912 
913 TEST(StringViewTest, ConstexprCompiles) {
914  constexpr absl::string_view sp;
915 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
916  constexpr absl::string_view cstr(nullptr);
917 #endif
918  constexpr absl::string_view cstr_len("cstr", 4);
919 
920 #if defined(ABSL_HAVE_STD_STRING_VIEW)
921  // In libstdc++ (as of 7.2), `std::string_view::string_view(const char*)`
922  // calls `std::char_traits<char>::length(const char*)` to get the std::string
923  // length, but it is not marked constexpr yet. See GCC bug:
924  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78156
925  // Also, there is a LWG issue that adds constexpr to length() which was just
926  // resolved 2017-06-02. See
927  // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2232
928  // TODO(zhangxy): Update the condition when libstdc++ adopts the constexpr
929  // length().
930 #if !defined(__GLIBCXX__)
931 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
932 #endif // !__GLIBCXX__
933 
934 #else // ABSL_HAVE_STD_STRING_VIEW
935 
936 // This duplicates the check for __builtin_strlen in the header.
937 #if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
938  (defined(__GNUC__) && !defined(__clang__))
939 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
940 #elif defined(__GNUC__) // GCC or clang
941 #error GCC/clang should have constexpr string_view.
942 #endif
943 
944 #endif // ABSL_HAVE_STD_STRING_VIEW
945 
946 #ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR
947  constexpr absl::string_view cstr_strlen("foo");
948  EXPECT_EQ(cstr_strlen.length(), 3);
949  constexpr absl::string_view cstr_strlen2 = "bar";
950  EXPECT_EQ(cstr_strlen2, "bar");
951 #endif
952 
953 #if !defined(__clang__) || 3 < __clang_major__ || \
954  (3 == __clang_major__ && 4 < __clang_minor__)
955  // older clang versions (< 3.5) complain that:
956  // "cannot perform pointer arithmetic on null pointer"
957  constexpr absl::string_view::iterator const_begin_empty = sp.begin();
958  constexpr absl::string_view::iterator const_end_empty = sp.end();
959  EXPECT_EQ(const_begin_empty, const_end_empty);
960 
961 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
962  constexpr absl::string_view::iterator const_begin_nullptr = cstr.begin();
963  constexpr absl::string_view::iterator const_end_nullptr = cstr.end();
964  EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
965 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
966 #endif // !defined(__clang__) || ...
967 
968  constexpr absl::string_view::iterator const_begin = cstr_len.begin();
969  constexpr absl::string_view::iterator const_end = cstr_len.end();
970  constexpr absl::string_view::size_type const_size = cstr_len.size();
971  constexpr absl::string_view::size_type const_length = cstr_len.length();
972  EXPECT_EQ(const_begin + const_size, const_end);
973  EXPECT_EQ(const_begin + const_length, const_end);
974 
975  constexpr bool isempty = sp.empty();
976  EXPECT_TRUE(isempty);
977 
978  constexpr const char c = cstr_len[2];
979  EXPECT_EQ(c, 't');
980 
981  constexpr const char cfront = cstr_len.front();
982  constexpr const char cback = cstr_len.back();
983  EXPECT_EQ(cfront, 'c');
984  EXPECT_EQ(cback, 'r');
985 
986  constexpr const char* np = sp.data();
987  constexpr const char* cstr_ptr = cstr_len.data();
988  EXPECT_EQ(np, nullptr);
989  EXPECT_NE(cstr_ptr, nullptr);
990 
991  constexpr size_t sp_npos = sp.npos;
992  EXPECT_EQ(sp_npos, -1);
993 }
994 
995 TEST(StringViewTest, Noexcept) {
996  EXPECT_TRUE((std::is_nothrow_constructible<absl::string_view,
997  const std::string&>::value));
998  EXPECT_TRUE((std::is_nothrow_constructible<absl::string_view,
999  const std::string&>::value));
1001  constexpr absl::string_view sp;
1002  EXPECT_TRUE(noexcept(sp.begin()));
1003  EXPECT_TRUE(noexcept(sp.end()));
1004  EXPECT_TRUE(noexcept(sp.cbegin()));
1005  EXPECT_TRUE(noexcept(sp.cend()));
1006  EXPECT_TRUE(noexcept(sp.rbegin()));
1007  EXPECT_TRUE(noexcept(sp.rend()));
1008  EXPECT_TRUE(noexcept(sp.crbegin()));
1009  EXPECT_TRUE(noexcept(sp.crend()));
1010  EXPECT_TRUE(noexcept(sp.size()));
1011  EXPECT_TRUE(noexcept(sp.length()));
1012  EXPECT_TRUE(noexcept(sp.empty()));
1013  EXPECT_TRUE(noexcept(sp.data()));
1014  EXPECT_TRUE(noexcept(sp.compare(sp)));
1015  EXPECT_TRUE(noexcept(sp.find(sp)));
1016  EXPECT_TRUE(noexcept(sp.find('f')));
1017  EXPECT_TRUE(noexcept(sp.rfind(sp)));
1018  EXPECT_TRUE(noexcept(sp.rfind('f')));
1019  EXPECT_TRUE(noexcept(sp.find_first_of(sp)));
1020  EXPECT_TRUE(noexcept(sp.find_first_of('f')));
1021  EXPECT_TRUE(noexcept(sp.find_last_of(sp)));
1022  EXPECT_TRUE(noexcept(sp.find_last_of('f')));
1023  EXPECT_TRUE(noexcept(sp.find_first_not_of(sp)));
1024  EXPECT_TRUE(noexcept(sp.find_first_not_of('f')));
1025  EXPECT_TRUE(noexcept(sp.find_last_not_of(sp)));
1026  EXPECT_TRUE(noexcept(sp.find_last_not_of('f')));
1027 }
1028 
1029 TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
1030  EXPECT_EQ("hello", std::string("hello"));
1031  EXPECT_LT("hello", std::string("world"));
1032 }
1033 
1034 TEST(ComparisonOpsTest, HeterogenousStringViewEquals) {
1035  EXPECT_EQ(absl::string_view("hello"), std::string("hello"));
1036  EXPECT_EQ("hello", absl::string_view("hello"));
1037 }
1038 
1039 TEST(FindOneCharTest, EdgeCases) {
1040  absl::string_view a("xxyyyxx");
1041 
1042  // Set a = "xyyyx".
1043  a.remove_prefix(1);
1044  a.remove_suffix(1);
1045 
1046  EXPECT_EQ(0, a.find('x'));
1047  EXPECT_EQ(0, a.find('x', 0));
1048  EXPECT_EQ(4, a.find('x', 1));
1049  EXPECT_EQ(4, a.find('x', 4));
1050  EXPECT_EQ(absl::string_view::npos, a.find('x', 5));
1051 
1052  EXPECT_EQ(4, a.rfind('x'));
1053  EXPECT_EQ(4, a.rfind('x', 5));
1054  EXPECT_EQ(4, a.rfind('x', 4));
1055  EXPECT_EQ(0, a.rfind('x', 3));
1056  EXPECT_EQ(0, a.rfind('x', 0));
1057 
1058  // Set a = "yyy".
1059  a.remove_prefix(1);
1060  a.remove_suffix(1);
1061 
1062  EXPECT_EQ(absl::string_view::npos, a.find('x'));
1063  EXPECT_EQ(absl::string_view::npos, a.rfind('x'));
1064 }
1065 
1066 #ifndef THREAD_SANITIZER // Allocates too much memory for tsan.
1067 TEST(HugeStringView, TwoPointTwoGB) {
1068  if (sizeof(size_t) <= 4 || RunningOnValgrind())
1069  return;
1070  // Try a huge std::string piece.
1071  const size_t size = size_t{2200} * 1000 * 1000;
1072  std::string s(size, 'a');
1073  absl::string_view sp(s);
1074  EXPECT_EQ(size, sp.length());
1075  sp.remove_prefix(1);
1076  EXPECT_EQ(size - 1, sp.length());
1077  sp.remove_suffix(2);
1078  EXPECT_EQ(size - 1 - 2, sp.length());
1079 }
1080 #endif // THREAD_SANITIZER
1081 
1082 #if !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
1083 TEST(NonNegativeLenTest, NonNegativeLen) {
1085  "len <= kMaxSize");
1086 }
1087 
1088 TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
1089  auto max_size = absl::string_view().max_size();
1090 
1091  // This should construct ok (although the view itself is obviously invalid).
1092  absl::string_view ok_view("", max_size);
1093 
1094  // Adding one to the max should trigger an assertion.
1096  "len <= kMaxSize");
1097 }
1098 #endif // !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
1099 
1100 class StringViewStreamTest : public ::testing::Test {
1101  public:
1102  // Set negative 'width' for right justification.
1103  template <typename T>
1104  std::string Pad(const T& s, int width, char fill = 0) {
1105  std::ostringstream oss;
1106  if (fill != 0) {
1107  oss << std::setfill(fill);
1108  }
1109  if (width < 0) {
1110  width = -width;
1111  oss << std::right;
1112  }
1113  oss << std::setw(width) << s;
1114  return oss.str();
1115  }
1116 };
1117 
1118 TEST_F(StringViewStreamTest, Padding) {
1119  std::string s("hello");
1120  absl::string_view sp(s);
1121  for (int w = -64; w < 64; ++w) {
1122  SCOPED_TRACE(w);
1123  EXPECT_EQ(Pad(s, w), Pad(sp, w));
1124  }
1125  for (int w = -64; w < 64; ++w) {
1126  SCOPED_TRACE(w);
1127  EXPECT_EQ(Pad(s, w, '#'), Pad(sp, w, '#'));
1128  }
1129 }
1130 
1131 TEST_F(StringViewStreamTest, ResetsWidth) {
1132  // Width should reset after one formatted write.
1133  // If we weren't resetting width after formatting the string_view,
1134  // we'd have width=5 carrying over to the printing of the "]",
1135  // creating "[###hi####]".
1136  std::string s = "hi";
1137  absl::string_view sp = s;
1138  {
1139  std::ostringstream oss;
1140  oss << "[" << std::setfill('#') << std::setw(5) << s << "]";
1141  ASSERT_EQ("[###hi]", oss.str());
1142  }
1143  {
1144  std::ostringstream oss;
1145  oss << "[" << std::setfill('#') << std::setw(5) << sp << "]";
1146  EXPECT_EQ("[###hi]", oss.str());
1147  }
1148 }
1149 
1150 } // namespace
string_view NullSafeStringView(const char *p)
Definition: string_view.h:559
const_reverse_iterator rend() const noexcept
Definition: string_view.h:238
const_reverse_iterator rbegin() const noexcept
Definition: string_view.h:229
string_view substr(size_type pos, size_type n=npos) const
Definition: string_view.h:354
void remove_prefix(size_type n)
Definition: string_view.h:310
size_type find(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:92
static constexpr size_type npos
Definition: string_view.h:159
string_view ClippedSubstr(string_view s, size_t pos, size_t n=string_view::npos)
Definition: string_view.h:548
constexpr const_reference back() const
Definition: string_view.h:293
int fill
void swap(string_view &s) noexcept
Definition: string_view.h:328
char buf[N]
#define COMPARE(result, op, x, y)
void remove_suffix(size_type n)
Definition: string_view.h:320
constexpr size_type size() const noexcept
Definition: string_view.h:260
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
const_iterator iterator
Definition: string_view.h:153
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
Definition: type_traits.h:673
size_type rfind(string_view s, size_type pos=npos) const noexcept
Definition: string_view.cc:112
size_t value
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
TEST_F(GraphCyclesTest, NoCycle)
constexpr const_iterator begin() const noexcept
Definition: string_view.h:203
size_type find_last_of(string_view s, size_type pos=npos) const noexcept
Definition: string_view.cc:179
uintptr_t size
constexpr size_type max_size() const noexcept
Definition: string_view.h:272
constexpr bool empty() const noexcept
Definition: string_view.h:277
size_type find_last_not_of(string_view s, size_type pos=npos) const noexcept
Definition: string_view.cc:194
constexpr size_type length() const noexcept
Definition: string_view.h:267
size_type find_first_not_of(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:152
constexpr const_pointer data() const noexcept
Definition: string_view.h:302
size_type find_first_of(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:135
constexpr const_reference front() const
Definition: string_view.h:288
TEST(Symbolize, Unimplemented)
int RunningOnValgrind(void)
uint64_t b
Definition: layout_test.cc:50
size_type copy(char *buf, size_type n, size_type pos=0) const
Definition: string_view.cc:80
int compare(string_view x) const noexcept
Definition: string_view.h:369
constexpr const_iterator end() const noexcept
Definition: string_view.h:210


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58