00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/string_view.h"
00016
00017 #include <stdlib.h>
00018 #include <iomanip>
00019 #include <iterator>
00020 #include <limits>
00021 #include <map>
00022 #include <sstream>
00023 #include <stdexcept>
00024 #include <string>
00025 #include <type_traits>
00026 #include <utility>
00027
00028 #include "gtest/gtest.h"
00029 #include "absl/base/config.h"
00030 #include "absl/base/dynamic_annotations.h"
00031
00032 #ifdef __ANDROID__
00033
00034
00035 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
00036 EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
00037 #else
00038 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
00039 EXPECT_DEATH_IF_SUPPORTED(statement, regex)
00040 #endif
00041
00042 namespace {
00043
00044
00045 template <typename T>
00046 struct Mallocator {
00047 typedef T value_type;
00048 typedef size_t size_type;
00049 typedef ptrdiff_t difference_type;
00050 typedef T* pointer;
00051 typedef const T* const_pointer;
00052 typedef T& reference;
00053 typedef const T& const_reference;
00054
00055 size_type max_size() const {
00056 return size_t(std::numeric_limits<size_type>::max()) / sizeof(value_type);
00057 }
00058 template <typename U>
00059 struct rebind {
00060 typedef Mallocator<U> other;
00061 };
00062 Mallocator() = default;
00063 template <class U>
00064 Mallocator(const Mallocator<U>&) {}
00065
00066 T* allocate(size_t n) { return static_cast<T*>(std::malloc(n * sizeof(T))); }
00067 void deallocate(T* p, size_t) { std::free(p); }
00068 };
00069 template <typename T, typename U>
00070 bool operator==(const Mallocator<T>&, const Mallocator<U>&) {
00071 return true;
00072 }
00073 template <typename T, typename U>
00074 bool operator!=(const Mallocator<T>&, const Mallocator<U>&) {
00075 return false;
00076 }
00077
00078 TEST(StringViewTest, Ctor) {
00079 {
00080
00081 absl::string_view s10;
00082 EXPECT_TRUE(s10.data() == nullptr);
00083 EXPECT_EQ(0, s10.length());
00084 }
00085
00086 {
00087
00088 const char* hello = "hello";
00089 absl::string_view s20(hello);
00090 EXPECT_TRUE(s20.data() == hello);
00091 EXPECT_EQ(5, s20.length());
00092
00093
00094 absl::string_view s21(hello, 4);
00095 EXPECT_TRUE(s21.data() == hello);
00096 EXPECT_EQ(4, s21.length());
00097
00098
00099 absl::string_view s22(hello, 6);
00100 EXPECT_TRUE(s22.data() == hello);
00101 EXPECT_EQ(6, s22.length());
00102 }
00103
00104 {
00105
00106 std::string hola = "hola";
00107 absl::string_view s30(hola);
00108 EXPECT_TRUE(s30.data() == hola.data());
00109 EXPECT_EQ(4, s30.length());
00110
00111
00112 hola.push_back('\0');
00113 hola.append("h2");
00114 hola.push_back('\0');
00115 absl::string_view s31(hola);
00116 EXPECT_TRUE(s31.data() == hola.data());
00117 EXPECT_EQ(8, s31.length());
00118 }
00119
00120 {
00121 using mstring =
00122 std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
00123 mstring str1("BUNGIE-JUMPING!");
00124 const mstring str2("SLEEPING!");
00125
00126 absl::string_view s1(str1);
00127 s1.remove_prefix(strlen("BUNGIE-JUM"));
00128
00129 absl::string_view s2(str2);
00130 s2.remove_prefix(strlen("SLEE"));
00131
00132 EXPECT_EQ(s1, s2);
00133 EXPECT_EQ(s1, "PING!");
00134 }
00135
00136
00137 }
00138
00139 TEST(StringViewTest, Swap) {
00140 absl::string_view a("a");
00141 absl::string_view b("bbb");
00142 EXPECT_TRUE(noexcept(a.swap(b)));
00143 a.swap(b);
00144 EXPECT_EQ(a, "bbb");
00145 EXPECT_EQ(b, "a");
00146 a.swap(b);
00147 EXPECT_EQ(a, "a");
00148 EXPECT_EQ(b, "bbb");
00149 }
00150
00151 TEST(StringViewTest, STLComparator) {
00152 std::string s1("foo");
00153 std::string s2("bar");
00154 std::string s3("baz");
00155
00156 absl::string_view p1(s1);
00157 absl::string_view p2(s2);
00158 absl::string_view p3(s3);
00159
00160 typedef std::map<absl::string_view, int> TestMap;
00161 TestMap map;
00162
00163 map.insert(std::make_pair(p1, 0));
00164 map.insert(std::make_pair(p2, 1));
00165 map.insert(std::make_pair(p3, 2));
00166 EXPECT_EQ(map.size(), 3);
00167
00168 TestMap::const_iterator iter = map.begin();
00169 EXPECT_EQ(iter->second, 1);
00170 ++iter;
00171 EXPECT_EQ(iter->second, 2);
00172 ++iter;
00173 EXPECT_EQ(iter->second, 0);
00174 ++iter;
00175 EXPECT_TRUE(iter == map.end());
00176
00177 TestMap::iterator new_iter = map.find("zot");
00178 EXPECT_TRUE(new_iter == map.end());
00179
00180 new_iter = map.find("bar");
00181 EXPECT_TRUE(new_iter != map.end());
00182
00183 map.erase(new_iter);
00184 EXPECT_EQ(map.size(), 2);
00185
00186 iter = map.begin();
00187 EXPECT_EQ(iter->second, 2);
00188 ++iter;
00189 EXPECT_EQ(iter->second, 0);
00190 ++iter;
00191 EXPECT_TRUE(iter == map.end());
00192 }
00193
00194 #define COMPARE(result, op, x, y) \
00195 EXPECT_EQ(result, absl::string_view((x)) op absl::string_view((y))); \
00196 EXPECT_EQ(result, absl::string_view((x)).compare(absl::string_view((y))) op 0)
00197
00198 TEST(StringViewTest, ComparisonOperators) {
00199 COMPARE(true, ==, "", "");
00200 COMPARE(true, ==, "", absl::string_view());
00201 COMPARE(true, ==, absl::string_view(), "");
00202 COMPARE(true, ==, "a", "a");
00203 COMPARE(true, ==, "aa", "aa");
00204 COMPARE(false, ==, "a", "");
00205 COMPARE(false, ==, "", "a");
00206 COMPARE(false, ==, "a", "b");
00207 COMPARE(false, ==, "a", "aa");
00208 COMPARE(false, ==, "aa", "a");
00209
00210 COMPARE(false, !=, "", "");
00211 COMPARE(false, !=, "a", "a");
00212 COMPARE(false, !=, "aa", "aa");
00213 COMPARE(true, !=, "a", "");
00214 COMPARE(true, !=, "", "a");
00215 COMPARE(true, !=, "a", "b");
00216 COMPARE(true, !=, "a", "aa");
00217 COMPARE(true, !=, "aa", "a");
00218
00219 COMPARE(true, <, "a", "b");
00220 COMPARE(true, <, "a", "aa");
00221 COMPARE(true, <, "aa", "b");
00222 COMPARE(true, <, "aa", "bb");
00223 COMPARE(false, <, "a", "a");
00224 COMPARE(false, <, "b", "a");
00225 COMPARE(false, <, "aa", "a");
00226 COMPARE(false, <, "b", "aa");
00227 COMPARE(false, <, "bb", "aa");
00228
00229 COMPARE(true, <=, "a", "a");
00230 COMPARE(true, <=, "a", "b");
00231 COMPARE(true, <=, "a", "aa");
00232 COMPARE(true, <=, "aa", "b");
00233 COMPARE(true, <=, "aa", "bb");
00234 COMPARE(false, <=, "b", "a");
00235 COMPARE(false, <=, "aa", "a");
00236 COMPARE(false, <=, "b", "aa");
00237 COMPARE(false, <=, "bb", "aa");
00238
00239 COMPARE(false, >=, "a", "b");
00240 COMPARE(false, >=, "a", "aa");
00241 COMPARE(false, >=, "aa", "b");
00242 COMPARE(false, >=, "aa", "bb");
00243 COMPARE(true, >=, "a", "a");
00244 COMPARE(true, >=, "b", "a");
00245 COMPARE(true, >=, "aa", "a");
00246 COMPARE(true, >=, "b", "aa");
00247 COMPARE(true, >=, "bb", "aa");
00248
00249 COMPARE(false, >, "a", "a");
00250 COMPARE(false, >, "a", "b");
00251 COMPARE(false, >, "a", "aa");
00252 COMPARE(false, >, "aa", "b");
00253 COMPARE(false, >, "aa", "bb");
00254 COMPARE(true, >, "b", "a");
00255 COMPARE(true, >, "aa", "a");
00256 COMPARE(true, >, "b", "aa");
00257 COMPARE(true, >, "bb", "aa");
00258 }
00259
00260 TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {
00261 std::string x;
00262 for (int i = 0; i < 256; i++) {
00263 x += 'a';
00264 std::string y = x;
00265 COMPARE(true, ==, x, y);
00266 for (int j = 0; j < i; j++) {
00267 std::string z = x;
00268 z[j] = 'b';
00269 COMPARE(false, ==, x, z);
00270 COMPARE(true, <, x, z);
00271 COMPARE(true, >, z, x);
00272 if (j + 1 < i) {
00273 z[j + 1] = 'A';
00274 COMPARE(false, ==, x, z);
00275 COMPARE(true, <, x, z);
00276 COMPARE(true, >, z, x);
00277 z[j + 1] = 'z';
00278 COMPARE(false, ==, x, z);
00279 COMPARE(true, <, x, z);
00280 COMPARE(true, >, z, x);
00281 }
00282 }
00283 }
00284 }
00285 #undef COMPARE
00286
00287
00288
00289
00290
00291
00292 template <typename T>
00293 struct is_type {
00294 template <typename U>
00295 static bool same(U) {
00296 return false;
00297 }
00298 static bool same(T) { return true; }
00299 };
00300
00301 TEST(StringViewTest, NposMatchesStdStringView) {
00302 EXPECT_EQ(absl::string_view::npos, std::string::npos);
00303
00304 EXPECT_TRUE(is_type<size_t>::same(absl::string_view::npos));
00305 EXPECT_FALSE(is_type<size_t>::same(""));
00306
00307
00308 char test[absl::string_view::npos & 1] = {0};
00309 EXPECT_EQ(0, test[0]);
00310 }
00311
00312 TEST(StringViewTest, STL1) {
00313 const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
00314 const absl::string_view b("abc");
00315 const absl::string_view c("xyz");
00316 const absl::string_view d("foobar");
00317 const absl::string_view e;
00318 std::string temp("123");
00319 temp += '\0';
00320 temp += "456";
00321 const absl::string_view f(temp);
00322
00323 EXPECT_EQ(a[6], 'g');
00324 EXPECT_EQ(b[0], 'a');
00325 EXPECT_EQ(c[2], 'z');
00326 EXPECT_EQ(f[3], '\0');
00327 EXPECT_EQ(f[5], '5');
00328
00329 EXPECT_EQ(*d.data(), 'f');
00330 EXPECT_EQ(d.data()[5], 'r');
00331 EXPECT_TRUE(e.data() == nullptr);
00332
00333 EXPECT_EQ(*a.begin(), 'a');
00334 EXPECT_EQ(*(b.begin() + 2), 'c');
00335 EXPECT_EQ(*(c.end() - 1), 'z');
00336
00337 EXPECT_EQ(*a.rbegin(), 'z');
00338 EXPECT_EQ(*(b.rbegin() + 2), 'a');
00339 EXPECT_EQ(*(c.rend() - 1), 'x');
00340 EXPECT_TRUE(a.rbegin() + 26 == a.rend());
00341
00342 EXPECT_EQ(a.size(), 26);
00343 EXPECT_EQ(b.size(), 3);
00344 EXPECT_EQ(c.size(), 3);
00345 EXPECT_EQ(d.size(), 6);
00346 EXPECT_EQ(e.size(), 0);
00347 EXPECT_EQ(f.size(), 7);
00348
00349 EXPECT_TRUE(!d.empty());
00350 EXPECT_TRUE(d.begin() != d.end());
00351 EXPECT_TRUE(d.begin() + 6 == d.end());
00352
00353 EXPECT_TRUE(e.empty());
00354 EXPECT_TRUE(e.begin() == e.end());
00355
00356 char buf[4] = { '%', '%', '%', '%' };
00357 EXPECT_EQ(a.copy(buf, 4), 4);
00358 EXPECT_EQ(buf[0], a[0]);
00359 EXPECT_EQ(buf[1], a[1]);
00360 EXPECT_EQ(buf[2], a[2]);
00361 EXPECT_EQ(buf[3], a[3]);
00362 EXPECT_EQ(a.copy(buf, 3, 7), 3);
00363 EXPECT_EQ(buf[0], a[7]);
00364 EXPECT_EQ(buf[1], a[8]);
00365 EXPECT_EQ(buf[2], a[9]);
00366 EXPECT_EQ(buf[3], a[3]);
00367 EXPECT_EQ(c.copy(buf, 99), 3);
00368 EXPECT_EQ(buf[0], c[0]);
00369 EXPECT_EQ(buf[1], c[1]);
00370 EXPECT_EQ(buf[2], c[2]);
00371 EXPECT_EQ(buf[3], a[3]);
00372 }
00373
00374
00375
00376 TEST(StringViewTest, STL2) {
00377 const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
00378 const absl::string_view b("abc");
00379 const absl::string_view c("xyz");
00380 absl::string_view d("foobar");
00381 const absl::string_view e;
00382 const absl::string_view f(
00383 "123"
00384 "\0"
00385 "456",
00386 7);
00387
00388 d = absl::string_view();
00389 EXPECT_EQ(d.size(), 0);
00390 EXPECT_TRUE(d.empty());
00391 EXPECT_TRUE(d.data() == nullptr);
00392 EXPECT_TRUE(d.begin() == d.end());
00393
00394 EXPECT_EQ(a.find(b), 0);
00395 EXPECT_EQ(a.find(b, 1), absl::string_view::npos);
00396 EXPECT_EQ(a.find(c), 23);
00397 EXPECT_EQ(a.find(c, 9), 23);
00398 EXPECT_EQ(a.find(c, absl::string_view::npos), absl::string_view::npos);
00399 EXPECT_EQ(b.find(c), absl::string_view::npos);
00400 EXPECT_EQ(b.find(c, absl::string_view::npos), absl::string_view::npos);
00401 EXPECT_EQ(a.find(d), 0);
00402 EXPECT_EQ(a.find(e), 0);
00403 EXPECT_EQ(a.find(d, 12), 12);
00404 EXPECT_EQ(a.find(e, 17), 17);
00405 absl::string_view g("xx not found bb");
00406 EXPECT_EQ(a.find(g), absl::string_view::npos);
00407
00408 EXPECT_EQ(d.find(b), absl::string_view::npos);
00409 EXPECT_EQ(e.find(b), absl::string_view::npos);
00410 EXPECT_EQ(d.find(b, 4), absl::string_view::npos);
00411 EXPECT_EQ(e.find(b, 7), absl::string_view::npos);
00412
00413 size_t empty_search_pos = std::string().find(std::string());
00414 EXPECT_EQ(d.find(d), empty_search_pos);
00415 EXPECT_EQ(d.find(e), empty_search_pos);
00416 EXPECT_EQ(e.find(d), empty_search_pos);
00417 EXPECT_EQ(e.find(e), empty_search_pos);
00418 EXPECT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
00419 EXPECT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
00420 EXPECT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
00421 EXPECT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
00422
00423 EXPECT_EQ(a.find('a'), 0);
00424 EXPECT_EQ(a.find('c'), 2);
00425 EXPECT_EQ(a.find('z'), 25);
00426 EXPECT_EQ(a.find('$'), absl::string_view::npos);
00427 EXPECT_EQ(a.find('\0'), absl::string_view::npos);
00428 EXPECT_EQ(f.find('\0'), 3);
00429 EXPECT_EQ(f.find('3'), 2);
00430 EXPECT_EQ(f.find('5'), 5);
00431 EXPECT_EQ(g.find('o'), 4);
00432 EXPECT_EQ(g.find('o', 4), 4);
00433 EXPECT_EQ(g.find('o', 5), 8);
00434 EXPECT_EQ(a.find('b', 5), absl::string_view::npos);
00435
00436 EXPECT_EQ(d.find('\0'), absl::string_view::npos);
00437 EXPECT_EQ(e.find('\0'), absl::string_view::npos);
00438 EXPECT_EQ(d.find('\0', 4), absl::string_view::npos);
00439 EXPECT_EQ(e.find('\0', 7), absl::string_view::npos);
00440 EXPECT_EQ(d.find('x'), absl::string_view::npos);
00441 EXPECT_EQ(e.find('x'), absl::string_view::npos);
00442 EXPECT_EQ(d.find('x', 4), absl::string_view::npos);
00443 EXPECT_EQ(e.find('x', 7), absl::string_view::npos);
00444
00445 EXPECT_EQ(a.rfind(b), 0);
00446 EXPECT_EQ(a.rfind(b, 1), 0);
00447 EXPECT_EQ(a.rfind(c), 23);
00448 EXPECT_EQ(a.rfind(c, 22), absl::string_view::npos);
00449 EXPECT_EQ(a.rfind(c, 1), absl::string_view::npos);
00450 EXPECT_EQ(a.rfind(c, 0), absl::string_view::npos);
00451 EXPECT_EQ(b.rfind(c), absl::string_view::npos);
00452 EXPECT_EQ(b.rfind(c, 0), absl::string_view::npos);
00453 EXPECT_EQ(a.rfind(d), std::string(a).rfind(std::string()));
00454 EXPECT_EQ(a.rfind(e), std::string(a).rfind(std::string()));
00455 EXPECT_EQ(a.rfind(d, 12), 12);
00456 EXPECT_EQ(a.rfind(e, 17), 17);
00457 EXPECT_EQ(a.rfind(g), absl::string_view::npos);
00458 EXPECT_EQ(d.rfind(b), absl::string_view::npos);
00459 EXPECT_EQ(e.rfind(b), absl::string_view::npos);
00460 EXPECT_EQ(d.rfind(b, 4), absl::string_view::npos);
00461 EXPECT_EQ(e.rfind(b, 7), absl::string_view::npos);
00462
00463 EXPECT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
00464 EXPECT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
00465 EXPECT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
00466 EXPECT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
00467 EXPECT_EQ(d.rfind(d), std::string().rfind(std::string()));
00468 EXPECT_EQ(e.rfind(d), std::string().rfind(std::string()));
00469 EXPECT_EQ(d.rfind(e), std::string().rfind(std::string()));
00470 EXPECT_EQ(e.rfind(e), std::string().rfind(std::string()));
00471
00472 EXPECT_EQ(g.rfind('o'), 8);
00473 EXPECT_EQ(g.rfind('q'), absl::string_view::npos);
00474 EXPECT_EQ(g.rfind('o', 8), 8);
00475 EXPECT_EQ(g.rfind('o', 7), 4);
00476 EXPECT_EQ(g.rfind('o', 3), absl::string_view::npos);
00477 EXPECT_EQ(f.rfind('\0'), 3);
00478 EXPECT_EQ(f.rfind('\0', 12), 3);
00479 EXPECT_EQ(f.rfind('3'), 2);
00480 EXPECT_EQ(f.rfind('5'), 5);
00481
00482 EXPECT_EQ(d.rfind('o'), absl::string_view::npos);
00483 EXPECT_EQ(e.rfind('o'), absl::string_view::npos);
00484 EXPECT_EQ(d.rfind('o', 4), absl::string_view::npos);
00485 EXPECT_EQ(e.rfind('o', 7), absl::string_view::npos);
00486 }
00487
00488
00489 TEST(StringViewTest, STL2FindFirst) {
00490 const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
00491 const absl::string_view b("abc");
00492 const absl::string_view c("xyz");
00493 absl::string_view d("foobar");
00494 const absl::string_view e;
00495 const absl::string_view f(
00496 "123"
00497 "\0"
00498 "456",
00499 7);
00500 absl::string_view g("xx not found bb");
00501
00502 d = absl::string_view();
00503 EXPECT_EQ(a.find_first_of(b), 0);
00504 EXPECT_EQ(a.find_first_of(b, 0), 0);
00505 EXPECT_EQ(a.find_first_of(b, 1), 1);
00506 EXPECT_EQ(a.find_first_of(b, 2), 2);
00507 EXPECT_EQ(a.find_first_of(b, 3), absl::string_view::npos);
00508 EXPECT_EQ(a.find_first_of(c), 23);
00509 EXPECT_EQ(a.find_first_of(c, 23), 23);
00510 EXPECT_EQ(a.find_first_of(c, 24), 24);
00511 EXPECT_EQ(a.find_first_of(c, 25), 25);
00512 EXPECT_EQ(a.find_first_of(c, 26), absl::string_view::npos);
00513 EXPECT_EQ(g.find_first_of(b), 13);
00514 EXPECT_EQ(g.find_first_of(c), 0);
00515 EXPECT_EQ(a.find_first_of(f), absl::string_view::npos);
00516 EXPECT_EQ(f.find_first_of(a), absl::string_view::npos);
00517
00518 EXPECT_EQ(a.find_first_of(d), absl::string_view::npos);
00519 EXPECT_EQ(a.find_first_of(e), absl::string_view::npos);
00520 EXPECT_EQ(d.find_first_of(b), absl::string_view::npos);
00521 EXPECT_EQ(e.find_first_of(b), absl::string_view::npos);
00522 EXPECT_EQ(d.find_first_of(d), absl::string_view::npos);
00523 EXPECT_EQ(e.find_first_of(d), absl::string_view::npos);
00524 EXPECT_EQ(d.find_first_of(e), absl::string_view::npos);
00525 EXPECT_EQ(e.find_first_of(e), absl::string_view::npos);
00526
00527 EXPECT_EQ(a.find_first_not_of(b), 3);
00528 EXPECT_EQ(a.find_first_not_of(c), 0);
00529 EXPECT_EQ(b.find_first_not_of(a), absl::string_view::npos);
00530 EXPECT_EQ(c.find_first_not_of(a), absl::string_view::npos);
00531 EXPECT_EQ(f.find_first_not_of(a), 0);
00532 EXPECT_EQ(a.find_first_not_of(f), 0);
00533 EXPECT_EQ(a.find_first_not_of(d), 0);
00534 EXPECT_EQ(a.find_first_not_of(e), 0);
00535
00536 EXPECT_EQ(a.find_first_not_of(d), 0);
00537 EXPECT_EQ(a.find_first_not_of(e), 0);
00538 EXPECT_EQ(a.find_first_not_of(d, 1), 1);
00539 EXPECT_EQ(a.find_first_not_of(e, 1), 1);
00540 EXPECT_EQ(a.find_first_not_of(d, a.size() - 1), a.size() - 1);
00541 EXPECT_EQ(a.find_first_not_of(e, a.size() - 1), a.size() - 1);
00542 EXPECT_EQ(a.find_first_not_of(d, a.size()), absl::string_view::npos);
00543 EXPECT_EQ(a.find_first_not_of(e, a.size()), absl::string_view::npos);
00544 EXPECT_EQ(a.find_first_not_of(d, absl::string_view::npos),
00545 absl::string_view::npos);
00546 EXPECT_EQ(a.find_first_not_of(e, absl::string_view::npos),
00547 absl::string_view::npos);
00548 EXPECT_EQ(d.find_first_not_of(a), absl::string_view::npos);
00549 EXPECT_EQ(e.find_first_not_of(a), absl::string_view::npos);
00550 EXPECT_EQ(d.find_first_not_of(d), absl::string_view::npos);
00551 EXPECT_EQ(e.find_first_not_of(d), absl::string_view::npos);
00552 EXPECT_EQ(d.find_first_not_of(e), absl::string_view::npos);
00553 EXPECT_EQ(e.find_first_not_of(e), absl::string_view::npos);
00554
00555 absl::string_view h("====");
00556 EXPECT_EQ(h.find_first_not_of('='), absl::string_view::npos);
00557 EXPECT_EQ(h.find_first_not_of('=', 3), absl::string_view::npos);
00558 EXPECT_EQ(h.find_first_not_of('\0'), 0);
00559 EXPECT_EQ(g.find_first_not_of('x'), 2);
00560 EXPECT_EQ(f.find_first_not_of('\0'), 0);
00561 EXPECT_EQ(f.find_first_not_of('\0', 3), 4);
00562 EXPECT_EQ(f.find_first_not_of('\0', 2), 2);
00563
00564 EXPECT_EQ(d.find_first_not_of('x'), absl::string_view::npos);
00565 EXPECT_EQ(e.find_first_not_of('x'), absl::string_view::npos);
00566 EXPECT_EQ(d.find_first_not_of('\0'), absl::string_view::npos);
00567 EXPECT_EQ(e.find_first_not_of('\0'), absl::string_view::npos);
00568 }
00569
00570
00571 TEST(StringViewTest, STL2FindLast) {
00572 const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
00573 const absl::string_view b("abc");
00574 const absl::string_view c("xyz");
00575 absl::string_view d("foobar");
00576 const absl::string_view e;
00577 const absl::string_view f(
00578 "123"
00579 "\0"
00580 "456",
00581 7);
00582 absl::string_view g("xx not found bb");
00583 absl::string_view h("====");
00584 absl::string_view i("56");
00585
00586 d = absl::string_view();
00587 EXPECT_EQ(h.find_last_of(a), absl::string_view::npos);
00588 EXPECT_EQ(g.find_last_of(a), g.size()-1);
00589 EXPECT_EQ(a.find_last_of(b), 2);
00590 EXPECT_EQ(a.find_last_of(c), a.size()-1);
00591 EXPECT_EQ(f.find_last_of(i), 6);
00592 EXPECT_EQ(a.find_last_of('a'), 0);
00593 EXPECT_EQ(a.find_last_of('b'), 1);
00594 EXPECT_EQ(a.find_last_of('z'), 25);
00595 EXPECT_EQ(a.find_last_of('a', 5), 0);
00596 EXPECT_EQ(a.find_last_of('b', 5), 1);
00597 EXPECT_EQ(a.find_last_of('b', 0), absl::string_view::npos);
00598 EXPECT_EQ(a.find_last_of('z', 25), 25);
00599 EXPECT_EQ(a.find_last_of('z', 24), absl::string_view::npos);
00600 EXPECT_EQ(f.find_last_of(i, 5), 5);
00601 EXPECT_EQ(f.find_last_of(i, 6), 6);
00602 EXPECT_EQ(f.find_last_of(a, 4), absl::string_view::npos);
00603
00604 EXPECT_EQ(f.find_last_of(d), absl::string_view::npos);
00605 EXPECT_EQ(f.find_last_of(e), absl::string_view::npos);
00606 EXPECT_EQ(f.find_last_of(d, 4), absl::string_view::npos);
00607 EXPECT_EQ(f.find_last_of(e, 4), absl::string_view::npos);
00608 EXPECT_EQ(d.find_last_of(d), absl::string_view::npos);
00609 EXPECT_EQ(d.find_last_of(e), absl::string_view::npos);
00610 EXPECT_EQ(e.find_last_of(d), absl::string_view::npos);
00611 EXPECT_EQ(e.find_last_of(e), absl::string_view::npos);
00612 EXPECT_EQ(d.find_last_of(f), absl::string_view::npos);
00613 EXPECT_EQ(e.find_last_of(f), absl::string_view::npos);
00614 EXPECT_EQ(d.find_last_of(d, 4), absl::string_view::npos);
00615 EXPECT_EQ(d.find_last_of(e, 4), absl::string_view::npos);
00616 EXPECT_EQ(e.find_last_of(d, 4), absl::string_view::npos);
00617 EXPECT_EQ(e.find_last_of(e, 4), absl::string_view::npos);
00618 EXPECT_EQ(d.find_last_of(f, 4), absl::string_view::npos);
00619 EXPECT_EQ(e.find_last_of(f, 4), absl::string_view::npos);
00620
00621 EXPECT_EQ(a.find_last_not_of(b), a.size()-1);
00622 EXPECT_EQ(a.find_last_not_of(c), 22);
00623 EXPECT_EQ(b.find_last_not_of(a), absl::string_view::npos);
00624 EXPECT_EQ(b.find_last_not_of(b), absl::string_view::npos);
00625 EXPECT_EQ(f.find_last_not_of(i), 4);
00626 EXPECT_EQ(a.find_last_not_of(c, 24), 22);
00627 EXPECT_EQ(a.find_last_not_of(b, 3), 3);
00628 EXPECT_EQ(a.find_last_not_of(b, 2), absl::string_view::npos);
00629
00630 EXPECT_EQ(f.find_last_not_of(d), f.size()-1);
00631 EXPECT_EQ(f.find_last_not_of(e), f.size()-1);
00632 EXPECT_EQ(f.find_last_not_of(d, 4), 4);
00633 EXPECT_EQ(f.find_last_not_of(e, 4), 4);
00634 EXPECT_EQ(d.find_last_not_of(d), absl::string_view::npos);
00635 EXPECT_EQ(d.find_last_not_of(e), absl::string_view::npos);
00636 EXPECT_EQ(e.find_last_not_of(d), absl::string_view::npos);
00637 EXPECT_EQ(e.find_last_not_of(e), absl::string_view::npos);
00638 EXPECT_EQ(d.find_last_not_of(f), absl::string_view::npos);
00639 EXPECT_EQ(e.find_last_not_of(f), absl::string_view::npos);
00640 EXPECT_EQ(d.find_last_not_of(d, 4), absl::string_view::npos);
00641 EXPECT_EQ(d.find_last_not_of(e, 4), absl::string_view::npos);
00642 EXPECT_EQ(e.find_last_not_of(d, 4), absl::string_view::npos);
00643 EXPECT_EQ(e.find_last_not_of(e, 4), absl::string_view::npos);
00644 EXPECT_EQ(d.find_last_not_of(f, 4), absl::string_view::npos);
00645 EXPECT_EQ(e.find_last_not_of(f, 4), absl::string_view::npos);
00646
00647 EXPECT_EQ(h.find_last_not_of('x'), h.size() - 1);
00648 EXPECT_EQ(h.find_last_not_of('='), absl::string_view::npos);
00649 EXPECT_EQ(b.find_last_not_of('c'), 1);
00650 EXPECT_EQ(h.find_last_not_of('x', 2), 2);
00651 EXPECT_EQ(h.find_last_not_of('=', 2), absl::string_view::npos);
00652 EXPECT_EQ(b.find_last_not_of('b', 1), 0);
00653
00654 EXPECT_EQ(d.find_last_not_of('x'), absl::string_view::npos);
00655 EXPECT_EQ(e.find_last_not_of('x'), absl::string_view::npos);
00656 EXPECT_EQ(d.find_last_not_of('\0'), absl::string_view::npos);
00657 EXPECT_EQ(e.find_last_not_of('\0'), absl::string_view::npos);
00658 }
00659
00660
00661 TEST(StringViewTest, STL2Substr) {
00662 const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
00663 const absl::string_view b("abc");
00664 const absl::string_view c("xyz");
00665 absl::string_view d("foobar");
00666 const absl::string_view e;
00667
00668 d = absl::string_view();
00669 EXPECT_EQ(a.substr(0, 3), b);
00670 EXPECT_EQ(a.substr(23), c);
00671 EXPECT_EQ(a.substr(23, 3), c);
00672 EXPECT_EQ(a.substr(23, 99), c);
00673 EXPECT_EQ(a.substr(0), a);
00674 EXPECT_EQ(a.substr(3, 2), "de");
00675
00676 EXPECT_EQ(d.substr(0, 99), e);
00677
00678 EXPECT_EQ(a.substr(0, absl::string_view::npos), a);
00679 EXPECT_EQ(a.substr(23, absl::string_view::npos), c);
00680
00681 #ifdef ABSL_HAVE_EXCEPTIONS
00682 EXPECT_THROW((void)a.substr(99, 2), std::out_of_range);
00683 #else
00684 EXPECT_DEATH((void)a.substr(99, 2), "absl::string_view::substr");
00685 #endif
00686 }
00687
00688 TEST(StringViewTest, TruncSubstr) {
00689 const absl::string_view hi("hi");
00690 EXPECT_EQ("", absl::ClippedSubstr(hi, 0, 0));
00691 EXPECT_EQ("h", absl::ClippedSubstr(hi, 0, 1));
00692 EXPECT_EQ("hi", absl::ClippedSubstr(hi, 0));
00693 EXPECT_EQ("i", absl::ClippedSubstr(hi, 1));
00694 EXPECT_EQ("", absl::ClippedSubstr(hi, 2));
00695 EXPECT_EQ("", absl::ClippedSubstr(hi, 3));
00696 EXPECT_EQ("", absl::ClippedSubstr(hi, 3, 2));
00697 }
00698
00699 TEST(StringViewTest, UTF8) {
00700 std::string utf8 = "\u00E1";
00701 std::string utf8_twice = utf8 + " " + utf8;
00702 int utf8_len = strlen(utf8.data());
00703 EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" "));
00704 EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" \t"));
00705 }
00706
00707 TEST(StringViewTest, FindConformance) {
00708 struct {
00709 std::string haystack;
00710 std::string needle;
00711 } specs[] = {
00712 {"", ""},
00713 {"", "a"},
00714 {"a", ""},
00715 {"a", "a"},
00716 {"a", "b"},
00717 {"aa", ""},
00718 {"aa", "a"},
00719 {"aa", "b"},
00720 {"ab", "a"},
00721 {"ab", "b"},
00722 {"abcd", ""},
00723 {"abcd", "a"},
00724 {"abcd", "d"},
00725 {"abcd", "ab"},
00726 {"abcd", "bc"},
00727 {"abcd", "cd"},
00728 {"abcd", "abcd"},
00729 };
00730 for (const auto& s : specs) {
00731 SCOPED_TRACE(s.haystack);
00732 SCOPED_TRACE(s.needle);
00733 std::string st = s.haystack;
00734 absl::string_view sp = s.haystack;
00735 for (size_t i = 0; i <= sp.size(); ++i) {
00736 size_t pos = (i == sp.size()) ? absl::string_view::npos : i;
00737 SCOPED_TRACE(pos);
00738 EXPECT_EQ(sp.find(s.needle, pos),
00739 st.find(s.needle, pos));
00740 EXPECT_EQ(sp.rfind(s.needle, pos),
00741 st.rfind(s.needle, pos));
00742 EXPECT_EQ(sp.find_first_of(s.needle, pos),
00743 st.find_first_of(s.needle, pos));
00744 EXPECT_EQ(sp.find_first_not_of(s.needle, pos),
00745 st.find_first_not_of(s.needle, pos));
00746 EXPECT_EQ(sp.find_last_of(s.needle, pos),
00747 st.find_last_of(s.needle, pos));
00748 EXPECT_EQ(sp.find_last_not_of(s.needle, pos),
00749 st.find_last_not_of(s.needle, pos));
00750 }
00751 }
00752 }
00753
00754 TEST(StringViewTest, Remove) {
00755 absl::string_view a("foobar");
00756 std::string s1("123");
00757 s1 += '\0';
00758 s1 += "456";
00759 absl::string_view e;
00760 std::string s2;
00761
00762
00763 absl::string_view c(a);
00764 c.remove_prefix(3);
00765 EXPECT_EQ(c, "bar");
00766 c = a;
00767 c.remove_prefix(0);
00768 EXPECT_EQ(c, a);
00769 c.remove_prefix(c.size());
00770 EXPECT_EQ(c, e);
00771
00772
00773 c = a;
00774 c.remove_suffix(3);
00775 EXPECT_EQ(c, "foo");
00776 c = a;
00777 c.remove_suffix(0);
00778 EXPECT_EQ(c, a);
00779 c.remove_suffix(c.size());
00780 EXPECT_EQ(c, e);
00781 }
00782
00783 TEST(StringViewTest, Set) {
00784 absl::string_view a("foobar");
00785 absl::string_view empty;
00786 absl::string_view b;
00787
00788
00789 b = absl::string_view("foobar", 6);
00790 EXPECT_EQ(b, a);
00791 b = absl::string_view("foobar", 0);
00792 EXPECT_EQ(b, empty);
00793 b = absl::string_view("foobar", 7);
00794 EXPECT_NE(b, a);
00795
00796 b = absl::string_view("foobar");
00797 EXPECT_EQ(b, a);
00798 }
00799
00800 TEST(StringViewTest, FrontBack) {
00801 static const char arr[] = "abcd";
00802 const absl::string_view csp(arr, 4);
00803 EXPECT_EQ(&arr[0], &csp.front());
00804 EXPECT_EQ(&arr[3], &csp.back());
00805 }
00806
00807 TEST(StringViewTest, FrontBackSingleChar) {
00808 static const char c = 'a';
00809 const absl::string_view csp(&c, 1);
00810 EXPECT_EQ(&c, &csp.front());
00811 EXPECT_EQ(&c, &csp.back());
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825 #if !defined(ABSL_HAVE_STD_STRING_VIEW) || \
00826 (!defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
00827 #define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
00828 #endif // !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
00829
00830 TEST(StringViewTest, NULLInput) {
00831 absl::string_view s;
00832 EXPECT_EQ(s.data(), nullptr);
00833 EXPECT_EQ(s.size(), 0);
00834
00835 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
00836 s = absl::string_view(nullptr);
00837 EXPECT_EQ(s.data(), nullptr);
00838 EXPECT_EQ(s.size(), 0);
00839
00840
00841
00842 EXPECT_EQ("", std::string(s));
00843 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
00844 }
00845
00846 TEST(StringViewTest, Comparisons2) {
00847
00848
00849
00850
00851
00852
00853
00854
00855 absl::string_view abc("abcdefghijklmnopqrstuvwxyz");
00856
00857
00858 EXPECT_EQ(abc, absl::string_view("abcdefghijklmnopqrstuvwxyz"));
00859 EXPECT_EQ(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxyz")), 0);
00860
00861 EXPECT_LT(abc, absl::string_view("abcdefghijklmnopqrstuvwxzz"));
00862 EXPECT_LT(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxzz")), 0);
00863
00864 EXPECT_GT(abc, absl::string_view("abcdefghijklmnopqrstuvwxyy"));
00865 EXPECT_GT(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxyy")), 0);
00866
00867
00868 absl::string_view digits("0123456789");
00869 auto npos = absl::string_view::npos;
00870
00871
00872 EXPECT_EQ(digits.compare(3, npos, absl::string_view("3456789")), 0);
00873 EXPECT_EQ(digits.compare(3, 4, absl::string_view("3456")), 0);
00874 EXPECT_EQ(digits.compare(10, 0, absl::string_view()), 0);
00875 EXPECT_EQ(digits.compare(3, 4, absl::string_view("0123456789"), 3, 4),
00876 0);
00877 EXPECT_LT(digits.compare(3, 4, absl::string_view("0123456789"), 3, 5),
00878 0);
00879 EXPECT_LT(digits.compare(0, npos, absl::string_view("0123456789"), 3, 5),
00880 0);
00881
00882 EXPECT_EQ(digits.compare(3, 4, "3456"), 0);
00883 EXPECT_EQ(digits.compare(3, npos, "3456789"), 0);
00884 EXPECT_EQ(digits.compare(10, 0, ""), 0);
00885 EXPECT_EQ(digits.compare(3, 4, "0123456789", 3, 4), 0);
00886 EXPECT_LT(digits.compare(3, 4, "0123456789", 3, 5), 0);
00887 EXPECT_LT(digits.compare(0, npos, "0123456789", 3, 5), 0);
00888 }
00889
00890 struct MyCharAlloc : std::allocator<char> {};
00891
00892 TEST(StringViewTest, ExplicitConversionOperator) {
00893 absl::string_view sp = "hi";
00894 EXPECT_EQ(sp, std::string(sp));
00895 }
00896
00897 TEST(StringViewTest, NullSafeStringView) {
00898 {
00899 absl::string_view s = absl::NullSafeStringView(nullptr);
00900 EXPECT_EQ(nullptr, s.data());
00901 EXPECT_EQ(0, s.size());
00902 EXPECT_EQ(absl::string_view(), s);
00903 }
00904 {
00905 static const char kHi[] = "hi";
00906 absl::string_view s = absl::NullSafeStringView(kHi);
00907 EXPECT_EQ(kHi, s.data());
00908 EXPECT_EQ(strlen(kHi), s.size());
00909 EXPECT_EQ(absl::string_view("hi"), s);
00910 }
00911 }
00912
00913 TEST(StringViewTest, ConstexprCompiles) {
00914 constexpr absl::string_view sp;
00915 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
00916 constexpr absl::string_view cstr(nullptr);
00917 #endif
00918 constexpr absl::string_view cstr_len("cstr", 4);
00919
00920 #if defined(ABSL_HAVE_STD_STRING_VIEW)
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930 #if !defined(__GLIBCXX__)
00931 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
00932 #endif // !__GLIBCXX__
00933
00934 #else // ABSL_HAVE_STD_STRING_VIEW
00935
00936
00937 #if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
00938 (defined(__GNUC__) && !defined(__clang__))
00939 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
00940 #elif defined(__GNUC__) // GCC or clang
00941 #error GCC/clang should have constexpr string_view.
00942 #endif
00943
00944 #endif // ABSL_HAVE_STD_STRING_VIEW
00945
00946 #ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR
00947 constexpr absl::string_view cstr_strlen("foo");
00948 EXPECT_EQ(cstr_strlen.length(), 3);
00949 constexpr absl::string_view cstr_strlen2 = "bar";
00950 EXPECT_EQ(cstr_strlen2, "bar");
00951 #endif
00952
00953 #if !defined(__clang__) || 3 < __clang_major__ || \
00954 (3 == __clang_major__ && 4 < __clang_minor__)
00955
00956
00957 constexpr absl::string_view::iterator const_begin_empty = sp.begin();
00958 constexpr absl::string_view::iterator const_end_empty = sp.end();
00959 EXPECT_EQ(const_begin_empty, const_end_empty);
00960
00961 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
00962 constexpr absl::string_view::iterator const_begin_nullptr = cstr.begin();
00963 constexpr absl::string_view::iterator const_end_nullptr = cstr.end();
00964 EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
00965 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
00966 #endif // !defined(__clang__) || ...
00967
00968 constexpr absl::string_view::iterator const_begin = cstr_len.begin();
00969 constexpr absl::string_view::iterator const_end = cstr_len.end();
00970 constexpr absl::string_view::size_type const_size = cstr_len.size();
00971 constexpr absl::string_view::size_type const_length = cstr_len.length();
00972 EXPECT_EQ(const_begin + const_size, const_end);
00973 EXPECT_EQ(const_begin + const_length, const_end);
00974
00975 constexpr bool isempty = sp.empty();
00976 EXPECT_TRUE(isempty);
00977
00978 constexpr const char c = cstr_len[2];
00979 EXPECT_EQ(c, 't');
00980
00981 constexpr const char cfront = cstr_len.front();
00982 constexpr const char cback = cstr_len.back();
00983 EXPECT_EQ(cfront, 'c');
00984 EXPECT_EQ(cback, 'r');
00985
00986 constexpr const char* np = sp.data();
00987 constexpr const char* cstr_ptr = cstr_len.data();
00988 EXPECT_EQ(np, nullptr);
00989 EXPECT_NE(cstr_ptr, nullptr);
00990
00991 constexpr size_t sp_npos = sp.npos;
00992 EXPECT_EQ(sp_npos, -1);
00993 }
00994
00995 TEST(StringViewTest, Noexcept) {
00996 EXPECT_TRUE((std::is_nothrow_constructible<absl::string_view,
00997 const std::string&>::value));
00998 EXPECT_TRUE((std::is_nothrow_constructible<absl::string_view,
00999 const std::string&>::value));
01000 EXPECT_TRUE(std::is_nothrow_constructible<absl::string_view>::value);
01001 constexpr absl::string_view sp;
01002 EXPECT_TRUE(noexcept(sp.begin()));
01003 EXPECT_TRUE(noexcept(sp.end()));
01004 EXPECT_TRUE(noexcept(sp.cbegin()));
01005 EXPECT_TRUE(noexcept(sp.cend()));
01006 EXPECT_TRUE(noexcept(sp.rbegin()));
01007 EXPECT_TRUE(noexcept(sp.rend()));
01008 EXPECT_TRUE(noexcept(sp.crbegin()));
01009 EXPECT_TRUE(noexcept(sp.crend()));
01010 EXPECT_TRUE(noexcept(sp.size()));
01011 EXPECT_TRUE(noexcept(sp.length()));
01012 EXPECT_TRUE(noexcept(sp.empty()));
01013 EXPECT_TRUE(noexcept(sp.data()));
01014 EXPECT_TRUE(noexcept(sp.compare(sp)));
01015 EXPECT_TRUE(noexcept(sp.find(sp)));
01016 EXPECT_TRUE(noexcept(sp.find('f')));
01017 EXPECT_TRUE(noexcept(sp.rfind(sp)));
01018 EXPECT_TRUE(noexcept(sp.rfind('f')));
01019 EXPECT_TRUE(noexcept(sp.find_first_of(sp)));
01020 EXPECT_TRUE(noexcept(sp.find_first_of('f')));
01021 EXPECT_TRUE(noexcept(sp.find_last_of(sp)));
01022 EXPECT_TRUE(noexcept(sp.find_last_of('f')));
01023 EXPECT_TRUE(noexcept(sp.find_first_not_of(sp)));
01024 EXPECT_TRUE(noexcept(sp.find_first_not_of('f')));
01025 EXPECT_TRUE(noexcept(sp.find_last_not_of(sp)));
01026 EXPECT_TRUE(noexcept(sp.find_last_not_of('f')));
01027 }
01028
01029 TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
01030 EXPECT_EQ("hello", std::string("hello"));
01031 EXPECT_LT("hello", std::string("world"));
01032 }
01033
01034 TEST(ComparisonOpsTest, HeterogenousStringViewEquals) {
01035 EXPECT_EQ(absl::string_view("hello"), std::string("hello"));
01036 EXPECT_EQ("hello", absl::string_view("hello"));
01037 }
01038
01039 TEST(FindOneCharTest, EdgeCases) {
01040 absl::string_view a("xxyyyxx");
01041
01042
01043 a.remove_prefix(1);
01044 a.remove_suffix(1);
01045
01046 EXPECT_EQ(0, a.find('x'));
01047 EXPECT_EQ(0, a.find('x', 0));
01048 EXPECT_EQ(4, a.find('x', 1));
01049 EXPECT_EQ(4, a.find('x', 4));
01050 EXPECT_EQ(absl::string_view::npos, a.find('x', 5));
01051
01052 EXPECT_EQ(4, a.rfind('x'));
01053 EXPECT_EQ(4, a.rfind('x', 5));
01054 EXPECT_EQ(4, a.rfind('x', 4));
01055 EXPECT_EQ(0, a.rfind('x', 3));
01056 EXPECT_EQ(0, a.rfind('x', 0));
01057
01058
01059 a.remove_prefix(1);
01060 a.remove_suffix(1);
01061
01062 EXPECT_EQ(absl::string_view::npos, a.find('x'));
01063 EXPECT_EQ(absl::string_view::npos, a.rfind('x'));
01064 }
01065
01066 #ifndef THREAD_SANITIZER // Allocates too much memory for tsan.
01067 TEST(HugeStringView, TwoPointTwoGB) {
01068 if (sizeof(size_t) <= 4 || RunningOnValgrind())
01069 return;
01070
01071 const size_t size = size_t{2200} * 1000 * 1000;
01072 std::string s(size, 'a');
01073 absl::string_view sp(s);
01074 EXPECT_EQ(size, sp.length());
01075 sp.remove_prefix(1);
01076 EXPECT_EQ(size - 1, sp.length());
01077 sp.remove_suffix(2);
01078 EXPECT_EQ(size - 1 - 2, sp.length());
01079 }
01080 #endif // THREAD_SANITIZER
01081
01082 #if !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
01083 TEST(NonNegativeLenTest, NonNegativeLen) {
01084 ABSL_EXPECT_DEATH_IF_SUPPORTED(absl::string_view("xyz", -1),
01085 "len <= kMaxSize");
01086 }
01087
01088 TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
01089 auto max_size = absl::string_view().max_size();
01090
01091
01092 absl::string_view ok_view("", max_size);
01093
01094
01095 ABSL_EXPECT_DEATH_IF_SUPPORTED(absl::string_view("", max_size + 1),
01096 "len <= kMaxSize");
01097 }
01098 #endif // !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
01099
01100 class StringViewStreamTest : public ::testing::Test {
01101 public:
01102
01103 template <typename T>
01104 std::string Pad(const T& s, int width, char fill = 0) {
01105 std::ostringstream oss;
01106 if (fill != 0) {
01107 oss << std::setfill(fill);
01108 }
01109 if (width < 0) {
01110 width = -width;
01111 oss << std::right;
01112 }
01113 oss << std::setw(width) << s;
01114 return oss.str();
01115 }
01116 };
01117
01118 TEST_F(StringViewStreamTest, Padding) {
01119 std::string s("hello");
01120 absl::string_view sp(s);
01121 for (int w = -64; w < 64; ++w) {
01122 SCOPED_TRACE(w);
01123 EXPECT_EQ(Pad(s, w), Pad(sp, w));
01124 }
01125 for (int w = -64; w < 64; ++w) {
01126 SCOPED_TRACE(w);
01127 EXPECT_EQ(Pad(s, w, '#'), Pad(sp, w, '#'));
01128 }
01129 }
01130
01131 TEST_F(StringViewStreamTest, ResetsWidth) {
01132
01133
01134
01135
01136 std::string s = "hi";
01137 absl::string_view sp = s;
01138 {
01139 std::ostringstream oss;
01140 oss << "[" << std::setfill('#') << std::setw(5) << s << "]";
01141 ASSERT_EQ("[###hi]", oss.str());
01142 }
01143 {
01144 std::ostringstream oss;
01145 oss << "[" << std::setfill('#') << std::setw(5) << sp << "]";
01146 EXPECT_EQ("[###hi]", oss.str());
01147 }
01148 }
01149
01150 }