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