25 #include <type_traits> 28 #include "gtest/gtest.h" 35 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 36 EXPECT_DEATH_IF_SUPPORTED(statement, ".*") 38 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 39 EXPECT_DEATH_IF_SUPPORTED(statement, regex) 48 typedef size_t size_type;
49 typedef ptrdiff_t difference_type;
51 typedef const T* const_pointer;
53 typedef const T& const_reference;
55 size_type max_size()
const {
56 return size_t(std::numeric_limits<size_type>::max()) /
sizeof(
value_type);
60 typedef Mallocator<U> other;
62 Mallocator() =
default;
64 Mallocator(
const Mallocator<U>&) {}
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); }
69 template <
typename T,
typename U>
70 bool operator==(
const Mallocator<T>&,
const Mallocator<U>&) {
73 template <
typename T,
typename U>
74 bool operator!=(
const Mallocator<T>&,
const Mallocator<U>&) {
78 TEST(StringViewTest, Ctor) {
82 EXPECT_TRUE(s10.
data() ==
nullptr);
83 EXPECT_EQ(0, s10.
length());
88 const char* hello =
"hello";
90 EXPECT_TRUE(s20.
data() == hello);
91 EXPECT_EQ(5, s20.
length());
95 EXPECT_TRUE(s21.data() == hello);
96 EXPECT_EQ(4, s21.length());
100 EXPECT_TRUE(s22.data() == hello);
101 EXPECT_EQ(6, s22.length());
106 std::string hola =
"hola";
108 EXPECT_TRUE(s30.
data() == hola.data());
109 EXPECT_EQ(4, s30.
length());
112 hola.push_back(
'\0');
114 hola.push_back(
'\0');
116 EXPECT_TRUE(s31.
data() == hola.data());
117 EXPECT_EQ(8, s31.
length());
122 std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
123 mstring str1(
"BUNGIE-JUMPING!");
124 const mstring str2(
"SLEEPING!");
133 EXPECT_EQ(s1,
"PING!");
142 EXPECT_TRUE(noexcept(a.
swap(b)));
151 TEST(StringViewTest, STLComparator) {
152 std::string s1(
"foo");
153 std::string s2(
"bar");
154 std::string s3(
"baz");
160 typedef std::map<absl::string_view, int> TestMap;
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);
168 TestMap::const_iterator iter = map.begin();
169 EXPECT_EQ(iter->second, 1);
171 EXPECT_EQ(iter->second, 2);
173 EXPECT_EQ(iter->second, 0);
175 EXPECT_TRUE(iter == map.end());
177 TestMap::iterator new_iter = map.find(
"zot");
178 EXPECT_TRUE(new_iter == map.end());
180 new_iter = map.find(
"bar");
181 EXPECT_TRUE(new_iter != map.end());
184 EXPECT_EQ(map.size(), 2);
187 EXPECT_EQ(iter->second, 2);
189 EXPECT_EQ(iter->second, 0);
191 EXPECT_TRUE(iter == map.end());
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) 198 TEST(StringViewTest, ComparisonOperators) {
212 COMPARE(
false, !=,
"aa",
"aa");
237 COMPARE(
false, <=,
"bb",
"aa");
242 COMPARE(
false, >=,
"aa",
"bb");
260 TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {
262 for (
int i = 0;
i < 256;
i++) {
266 for (
int j = 0; j <
i; j++) {
292 template <
typename T>
294 template <
typename U>
295 static bool same(U) {
298 static bool same(T) {
return true; }
301 TEST(StringViewTest, NposMatchesStdStringView) {
305 EXPECT_FALSE(is_type<size_t>::same(
""));
309 EXPECT_EQ(0, test[0]);
312 TEST(StringViewTest, STL1) {
318 std::string temp(
"123");
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');
329 EXPECT_EQ(*d.
data(),
'f');
330 EXPECT_EQ(d.
data()[5],
'r');
331 EXPECT_TRUE(e.
data() ==
nullptr);
333 EXPECT_EQ(*a.
begin(),
'a');
334 EXPECT_EQ(*(b.
begin() + 2),
'c');
335 EXPECT_EQ(*(c.
end() - 1),
'z');
337 EXPECT_EQ(*a.
rbegin(),
'z');
338 EXPECT_EQ(*(b.
rbegin() + 2),
'a');
339 EXPECT_EQ(*(c.
rend() - 1),
'x');
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);
349 EXPECT_TRUE(!d.
empty());
351 EXPECT_TRUE(d.
begin() + 6 == d.
end());
353 EXPECT_TRUE(e.
empty());
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]);
376 TEST(StringViewTest, STL2) {
389 EXPECT_EQ(d.
size(), 0);
390 EXPECT_TRUE(d.
empty());
391 EXPECT_TRUE(d.
data() ==
nullptr);
394 EXPECT_EQ(a.
find(b), 0);
396 EXPECT_EQ(a.
find(c), 23);
397 EXPECT_EQ(a.
find(c, 9), 23);
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);
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));
423 EXPECT_EQ(a.
find(
'a'), 0);
424 EXPECT_EQ(a.
find(
'c'), 2);
425 EXPECT_EQ(a.
find(
'z'), 25);
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);
445 EXPECT_EQ(a.
rfind(b), 0);
446 EXPECT_EQ(a.
rfind(b, 1), 0);
447 EXPECT_EQ(a.
rfind(c), 23);
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);
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()));
472 EXPECT_EQ(g.rfind(
'o'), 8);
474 EXPECT_EQ(g.rfind(
'o', 8), 8);
475 EXPECT_EQ(g.rfind(
'o', 7), 4);
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);
489 TEST(StringViewTest, STL2FindFirst) {
558 EXPECT_EQ(h.find_first_not_of(
'\0'), 0);
571 TEST(StringViewTest, STL2FindLast) {
661 TEST(StringViewTest, STL2Substr) {
670 EXPECT_EQ(a.
substr(23), c);
671 EXPECT_EQ(a.
substr(23, 3), c);
672 EXPECT_EQ(a.
substr(23, 99), c);
674 EXPECT_EQ(a.
substr(3, 2),
"de");
676 EXPECT_EQ(d.
substr(0, 99), e);
681 #ifdef ABSL_HAVE_EXCEPTIONS 682 EXPECT_THROW((
void)a.
substr(99, 2), std::out_of_range);
684 EXPECT_DEATH((
void)a.
substr(99, 2),
"absl::string_view::substr");
688 TEST(StringViewTest, TruncSubstr) {
699 TEST(StringViewTest, UTF8) {
700 std::string utf8 =
"\u00E1";
701 std::string utf8_twice = utf8 +
" " + utf8;
702 int utf8_len = strlen(utf8.data());
707 TEST(StringViewTest, FindConformance) {
709 std::string haystack;
730 for (
const auto& s : specs) {
731 SCOPED_TRACE(s.haystack);
732 SCOPED_TRACE(s.needle);
733 std::string st = s.haystack;
735 for (
size_t i = 0;
i <= sp.
size(); ++
i) {
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));
743 st.find_first_of(s.needle, pos));
745 st.find_first_not_of(s.needle, pos));
747 st.find_last_of(s.needle, pos));
749 st.find_last_not_of(s.needle, pos));
754 TEST(StringViewTest, Remove) {
756 std::string s1(
"123");
783 TEST(StringViewTest, Set) {
800 TEST(StringViewTest, FrontBack) {
801 static const char arr[] =
"abcd";
803 EXPECT_EQ(&arr[0], &csp.
front());
804 EXPECT_EQ(&arr[3], &csp.
back());
807 TEST(StringViewTest, FrontBackSingleChar) {
808 static const char c =
'a';
810 EXPECT_EQ(&c, &csp.
front());
811 EXPECT_EQ(&c, &csp.
back());
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) 830 TEST(StringViewTest, NULLInput) {
832 EXPECT_EQ(s.
data(),
nullptr);
833 EXPECT_EQ(s.
size(), 0);
835 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 837 EXPECT_EQ(s.data(),
nullptr);
838 EXPECT_EQ(s.size(), 0);
842 EXPECT_EQ(
"", std::string(s));
843 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 846 TEST(StringViewTest, Comparisons2) {
882 EXPECT_EQ(digits.compare(3, 4,
"3456"), 0);
883 EXPECT_EQ(digits.compare(3, npos,
"3456789"), 0);
884 EXPECT_EQ(digits.compare(10, 0,
""), 0);
885 EXPECT_EQ(digits.compare(3, 4,
"0123456789", 3, 4), 0);
886 EXPECT_LT(digits.compare(3, 4,
"0123456789", 3, 5), 0);
887 EXPECT_LT(digits.compare(0, npos,
"0123456789", 3, 5), 0);
890 struct MyCharAlloc : std::allocator<char> {};
892 TEST(StringViewTest, ExplicitConversionOperator) {
894 EXPECT_EQ(sp, std::string(sp));
900 EXPECT_EQ(
nullptr, s.
data());
901 EXPECT_EQ(0, s.
size());
905 static const char kHi[] =
"hi";
907 EXPECT_EQ(kHi, s.
data());
908 EXPECT_EQ(strlen(kHi), s.
size());
913 TEST(StringViewTest, ConstexprCompiles) {
915 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 920 #if defined(ABSL_HAVE_STD_STRING_VIEW) 930 #if !defined(__GLIBCXX__) 931 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1 932 #endif // !__GLIBCXX__ 934 #else // ABSL_HAVE_STD_STRING_VIEW 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. 944 #endif // ABSL_HAVE_STD_STRING_VIEW 946 #ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 948 EXPECT_EQ(cstr_strlen.length(), 3);
950 EXPECT_EQ(cstr_strlen2,
"bar");
953 #if !defined(__clang__) || 3 < __clang_major__ || \ 954 (3 == __clang_major__ && 4 < __clang_minor__) 959 EXPECT_EQ(const_begin_empty, const_end_empty);
961 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 964 EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
965 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 966 #endif // !defined(__clang__) || ... 972 EXPECT_EQ(const_begin + const_size, const_end);
973 EXPECT_EQ(const_begin + const_length, const_end);
975 constexpr
bool isempty = sp.empty();
976 EXPECT_TRUE(isempty);
978 constexpr
const char c = cstr_len[2];
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');
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);
991 constexpr
size_t sp_npos = sp.npos;
992 EXPECT_EQ(sp_npos, -1);
995 TEST(StringViewTest, Noexcept) {
997 const std::string&>::
value));
999 const std::string&>::
value));
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')));
1029 TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
1030 EXPECT_EQ(
"hello", std::string(
"hello"));
1031 EXPECT_LT(
"hello", std::string(
"world"));
1034 TEST(ComparisonOpsTest, HeterogenousStringViewEquals) {
1039 TEST(FindOneCharTest, EdgeCases) {
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));
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));
1066 #ifndef THREAD_SANITIZER // Allocates too much memory for tsan. 1067 TEST(HugeStringView, TwoPointTwoGB) {
1071 const size_t size =
size_t{2200} * 1000 * 1000;
1072 std::string s(size,
'a');
1074 EXPECT_EQ(size, sp.
length());
1076 EXPECT_EQ(size - 1, sp.
length());
1078 EXPECT_EQ(size - 1 - 2, sp.
length());
1080 #endif // THREAD_SANITIZER 1082 #if !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW) 1083 TEST(NonNegativeLenTest, NonNegativeLen) {
1088 TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
1098 #endif // !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW) 1100 class StringViewStreamTest :
public ::testing::Test {
1103 template <
typename T>
1104 std::string Pad(
const T& s,
int width,
char fill = 0) {
1105 std::ostringstream oss;
1107 oss << std::setfill(
fill);
1113 oss << std::setw(width) << s;
1118 TEST_F(StringViewStreamTest, Padding) {
1119 std::string s(
"hello");
1121 for (
int w = -64; w < 64; ++w) {
1123 EXPECT_EQ(Pad(s, w), Pad(sp, w));
1125 for (
int w = -64; w < 64; ++w) {
1127 EXPECT_EQ(Pad(s, w,
'#'), Pad(sp, w,
'#'));
1131 TEST_F(StringViewStreamTest, ResetsWidth) {
1136 std::string s =
"hi";
1139 std::ostringstream oss;
1140 oss <<
"[" << std::setfill(
'#') << std::setw(5) << s <<
"]";
1141 ASSERT_EQ(
"[###hi]", oss.str());
1144 std::ostringstream oss;
1145 oss <<
"[" << std::setfill(
'#') << std::setw(5) << sp <<
"]";
1146 EXPECT_EQ(
"[###hi]", oss.str());
string_view NullSafeStringView(const char *p)
const_reverse_iterator rend() const noexcept
const_reverse_iterator rbegin() const noexcept
string_view substr(size_type pos, size_type n=npos) const
void remove_prefix(size_type n)
size_type find(string_view s, size_type pos=0) const noexcept
static constexpr size_type npos
string_view ClippedSubstr(string_view s, size_t pos, size_t n=string_view::npos)
constexpr const_reference back() const
void swap(string_view &s) noexcept
#define COMPARE(result, op, x, y)
void remove_suffix(size_type n)
constexpr size_type size() const noexcept
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
size_type rfind(string_view s, size_type pos=npos) const noexcept
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
size_type find_last_of(string_view s, size_type pos=npos) const noexcept
constexpr size_type max_size() const noexcept
constexpr bool empty() const noexcept
size_type find_last_not_of(string_view s, size_type pos=npos) const noexcept
constexpr size_type length() const noexcept
size_type find_first_not_of(string_view s, size_type pos=0) const noexcept
constexpr const_pointer data() const noexcept
size_type find_first_of(string_view s, size_type pos=0) const noexcept
constexpr const_reference front() const
TEST(Symbolize, Unimplemented)
int RunningOnValgrind(void)
size_type copy(char *buf, size_type n, size_type pos=0) const
int compare(string_view x) const noexcept
constexpr const_iterator end() const noexcept