15 #include "absl/strings/string_view.h"
25 #include <type_traits>
28 #include "gtest/gtest.h"
29 #include "absl/base/config.h"
30 #include "absl/base/dynamic_annotations.h"
31 #include "absl/base/options.h"
33 #if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__)
37 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
38 EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
40 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
41 EXPECT_DEATH_IF_SUPPORTED(statement, regex)
50 typedef size_t size_type;
51 typedef ptrdiff_t difference_type;
57 size_type max_size()
const {
62 typedef Mallocator<U> other;
64 Mallocator() =
default;
66 Mallocator(
const Mallocator<U>&) {}
68 T* allocate(
size_t n) {
return static_cast<T*
>(std::malloc(
n *
sizeof(
T))); }
69 void deallocate(
T*
p,
size_t) { std::free(
p); }
71 template <
typename T,
typename U>
72 bool operator==(
const Mallocator<T>&,
const Mallocator<U>&) {
75 template <
typename T,
typename U>
76 bool operator!=(
const Mallocator<T>&,
const Mallocator<U>&) {
80 TEST(StringViewTest, Ctor) {
90 const char*
hello =
"hello";
114 hola.push_back(
'\0');
116 hola.push_back(
'\0');
124 std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
125 mstring str1(
"BUNGIE-JUMPING!");
126 const mstring str2(
"SLEEPING!");
153 TEST(StringViewTest, STLComparator) {
162 typedef std::map<absl::string_view, int> TestMap;
165 map.insert(std::make_pair(p1, 0));
166 map.insert(std::make_pair(p2, 1));
167 map.insert(std::make_pair(p3, 2));
170 TestMap::const_iterator
iter =
map.begin();
182 new_iter =
map.find(
"bar");
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)
200 TEST(StringViewTest, ComparisonOperators) {
214 COMPARE(
false, !=,
"aa",
"aa");
239 COMPARE(
false, <=,
"bb",
"aa");
244 COMPARE(
false, >=,
"aa",
"bb");
262 TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {
264 for (
int i = 0;
i < 256;
i++) {
268 for (
int j = 0;
j <
i;
j++) {
294 template <
typename T>
296 template <
typename U>
297 static bool same(U) {
300 static bool same(
T) {
return true; }
303 TEST(StringViewTest, NposMatchesStdStringView) {
314 TEST(StringViewTest, STL1) {
358 char buf[4] = {
'%',
'%',
'%',
'%' };
374 #ifdef ABSL_HAVE_EXCEPTIONS
383 TEST(StringViewTest, STL2) {
522 TEST(StringViewTest, STL2FindFirst) {
573 EXPECT_EQ(
a.find_first_not_of(
d,
a.size() - 1),
a.size() - 1);
574 EXPECT_EQ(
a.find_first_not_of(e,
a.size() - 1),
a.size() - 1);
604 TEST(StringViewTest, STL2FindLast) {
694 TEST(StringViewTest, STL2Substr) {
715 #ifdef ABSL_HAVE_EXCEPTIONS
719 "absl::string_view::substr");
723 TEST(StringViewTest, TruncSubstr) {
737 int utf8_len = strlen(utf8.data());
742 TEST(StringViewTest, FindConformance) {
765 for (
const auto& s : specs) {
770 for (
size_t i = 0;
i <= sp.
size(); ++
i) {
774 st.find(
s.needle,
pos));
776 st.rfind(
s.needle,
pos));
778 st.find_first_of(
s.needle,
pos));
780 st.find_first_not_of(
s.needle,
pos));
782 st.find_last_of(
s.needle,
pos));
784 st.find_last_not_of(
s.needle,
pos));
804 c.remove_prefix(
c.size());
814 c.remove_suffix(
c.size());
818 TEST(StringViewTest, Set) {
835 TEST(StringViewTest, FrontBack) {
836 static const char arr[] =
"abcd";
842 TEST(StringViewTest, FrontBackSingleChar) {
843 static const char c =
'a';
849 TEST(StringViewTest, FrontBackEmpty) {
850 #ifndef ABSL_USES_STD_STRING_VIEW
851 #if !defined(NDEBUG) || ABSL_OPTION_HARDENED
873 #if !defined(ABSL_USES_STD_STRING_VIEW) || \
874 (!(defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 9) && \
875 !defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
876 #define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
879 TEST(StringViewTest, NULLInput) {
884 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
892 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
895 TEST(StringViewTest, Comparisons2) {
931 EXPECT_EQ(digits.compare(3, 4,
"3456"), 0);
932 EXPECT_EQ(digits.compare(3, npos,
"3456789"), 0);
934 EXPECT_EQ(digits.compare(3, 4,
"0123456789", 3, 4), 0);
935 EXPECT_LT(digits.compare(3, 4,
"0123456789", 3, 5), 0);
936 EXPECT_LT(digits.compare(0, npos,
"0123456789", 3, 5), 0);
939 TEST(StringViewTest, At) {
944 #ifdef ABSL_HAVE_EXCEPTIONS
951 struct MyCharAlloc : std::allocator<char> {};
953 TEST(StringViewTest, ExplicitConversionOperator) {
966 static const char kHi[] =
"hi";
974 TEST(StringViewTest, ConstexprNullSafeStringView) {
981 #if !defined(_MSC_VER) || _MSC_VER >= 1910
985 static constexpr
char kHi[] =
"hi";
999 TEST(StringViewTest, ConstexprCompiles) {
1001 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
1006 #if defined(ABSL_USES_STD_STRING_VIEW)
1016 #if !defined(__GLIBCXX__)
1017 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
1018 #endif // !__GLIBCXX__
1020 #else // ABSL_USES_STD_STRING_VIEW
1023 #if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
1024 (defined(__GNUC__) && !defined(__clang__))
1025 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
1026 #elif defined(__GNUC__) // GCC or clang
1027 #error GCC/clang should have constexpr string_view.
1031 #if defined(_MSC_VER) && _MSC_VER >= 1910
1032 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
1035 #endif // ABSL_USES_STD_STRING_VIEW
1037 #ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR
1043 #if ABSL_HAVE_BUILTIN(__builtin_memcmp) || \
1044 (defined(__GNUC__) && !defined(__clang__))
1045 #define ABSL_HAVE_CONSTEXPR_STRING_VIEW_COMPARISON 1
1047 #ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_COMPARISON
1050 constexpr
bool foo_eq_bar =
foo ==
bar;
1051 constexpr
bool foo_ne_bar =
foo !=
bar;
1052 constexpr
bool foo_lt_bar =
foo <
bar;
1053 constexpr
bool foo_le_bar =
foo <=
bar;
1054 constexpr
bool foo_gt_bar =
foo >
bar;
1055 constexpr
bool foo_ge_bar =
foo >=
bar;
1056 constexpr
int foo_compare_bar =
foo.compare(
bar);
1067 #if !defined(__clang__) || 3 < __clang_major__ || \
1068 (3 == __clang_major__ && 4 < __clang_minor__)
1073 EXPECT_EQ(const_begin_empty, const_end_empty);
1075 #ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
1078 EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
1079 #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
1080 #endif // !defined(__clang__) || ...
1086 static_assert(const_begin + const_size == const_end,
1087 "pointer arithmetic check");
1088 static_assert(const_begin + const_length == const_end,
1089 "pointer arithmetic check");
1093 EXPECT_EQ(const_begin + const_size, const_end);
1094 EXPECT_EQ(const_begin + const_length, const_end);
1097 constexpr
bool isempty = sp.empty();
1100 constexpr
const char c = cstr_len[2];
1103 constexpr
const char cfront = cstr_len.front();
1104 constexpr
const char cback = cstr_len.back();
1108 constexpr
const char* np = sp.data();
1109 constexpr
const char* cstr_ptr = cstr_len.data();
1113 constexpr
size_t sp_npos = sp.npos;
1117 constexpr
char ConstexprMethodsHelper() {
1118 #if defined(__cplusplus) && __cplusplus >= 201402L
1120 str.remove_prefix(1);
1121 str.remove_suffix(1);
1130 TEST(StringViewTest, ConstexprMethods) {
1132 static_assert(ConstexprMethodsHelper() ==
'2',
"");
1142 TEST(StringViewTest, Noexcept) {
1176 TEST(StringViewTest, BoundsCheck) {
1177 #ifndef ABSL_USES_STD_STRING_VIEW
1178 #if !defined(NDEBUG) || ABSL_OPTION_HARDENED
1187 TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
1192 TEST(ComparisonOpsTest, HeterogeneousStringViewEquals) {
1197 TEST(FindOneCharTest, EdgeCases) {
1224 #ifndef ABSL_HAVE_THREAD_SANITIZER // Allocates too much memory for tsan.
1225 TEST(HugeStringView, TwoPointTwoGB) {
1226 if (
sizeof(
size_t) <= 4)
1229 const size_t size =
size_t{2200} * 1000 * 1000;
1233 sp.remove_prefix(1);
1235 sp.remove_suffix(2);
1238 #endif // ABSL_HAVE_THREAD_SANITIZER
1240 #if !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)
1241 TEST(NonNegativeLenTest, NonNegativeLen) {
1246 TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
1256 #endif // !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)
1261 template <
typename T>
1263 std::ostringstream oss;
1265 oss << std::setfill(
fill);
1271 oss << std::setw(
width) <<
s;
1276 TEST_F(StringViewStreamTest, Padding) {
1279 for (
int w = -64; w < 64; ++w) {
1283 for (
int w = -64; w < 64; ++w) {
1285 EXPECT_EQ(Pad(s, w,
'#'), Pad(sp, w,
'#'));
1289 TEST_F(StringViewStreamTest, ResetsWidth) {
1297 std::ostringstream oss;
1298 oss <<
"[" << std::setfill(
'#') << std::setw(5) <<
s <<
"]";
1302 std::ostringstream oss;
1303 oss <<
"[" << std::setfill(
'#') << std::setw(5) << sp <<
"]";