45 TEST(StringPiece, Ctor) {
55 const char* hello =
"hello";
56 StringPiece s20(hello);
61 StringPiece s21(hello, 4);
66 StringPiece s22(hello, 6);
74 StringPiece s30(hola);
82 StringPiece s31(hola);
87 #if defined(HAS_GLOBAL_STRING)
90 string bonjour =
"bonjour";
91 StringPiece s40(bonjour);
102 TEST(StringPiece, STLComparator) {
111 typedef std::map<StringPiece, int> TestMap;
114 map.insert(std::make_pair(p1, 0));
115 map.insert(std::make_pair(p2, 1));
116 map.insert(std::make_pair(p3, 2));
119 TestMap::const_iterator iter =
map.begin();
128 TestMap::iterator new_iter =
map.find(
"zot");
131 new_iter =
map.find(
"bar");
145 TEST(StringPiece, ComparisonOperators) {
146 #define COMPARE(result, op, x, y) \
147 EXPECT_EQ(result, StringPiece((x)) op StringPiece((y))); \
148 EXPECT_EQ(result, StringPiece((x)).compare(StringPiece((y))) op 0)
151 COMPARE(
true, ==,
"",
nullptr);
152 COMPARE(
true, ==,
nullptr,
"");
163 COMPARE(
false, !=,
"aa",
"aa");
188 COMPARE(
false, <=,
"bb",
"aa");
193 COMPARE(
false, >=,
"aa",
"bb");
211 for (
int i = 0;
i < 256;
i++) {
215 for (
int j = 0; j <
i; j++) {
237 TEST(StringPiece, STL1) {
238 const StringPiece
a(
"abcdefghijklmnopqrstuvwxyz");
239 const StringPiece
b(
"abc");
240 const StringPiece c(
"xyz");
241 const StringPiece
d(
"foobar");
246 const StringPiece
f(temp);
284 char buf[4] = {
'%',
'%',
'%',
'%' };
304 TEST(StringPiece, STL2) {
305 const StringPiece
a(
"abcdefghijklmnopqrstuvwxyz");
306 const StringPiece
b(
"abc");
307 const StringPiece c(
"xyz");
308 StringPiece
d(
"foobar");
310 const StringPiece
f(
"123" "\0" "456", 7);
331 StringPiece
g(
"xx not found bb");
339 size_t empty_search_pos =
string().find(
string());
344 EXPECT_EQ(
d.find(d, 4),
string().find(
string(), 4));
345 EXPECT_EQ(
d.find(e, 4),
string().find(
string(), 4));
346 EXPECT_EQ(e.find(d, 4),
string().find(
string(), 4));
347 EXPECT_EQ(e.find(e, 4),
string().find(
string(), 4));
379 EXPECT_EQ(
a.rfind(d),
a.as_string().rfind(
string()));
380 EXPECT_EQ(
a.rfind(e),
a.as_string().rfind(
string()));
389 EXPECT_EQ(
d.rfind(d, 4),
string().rfind(
string()));
390 EXPECT_EQ(e.rfind(d, 7),
string().rfind(
string()));
391 EXPECT_EQ(
d.rfind(e, 4),
string().rfind(
string()));
392 EXPECT_EQ(e.rfind(e, 7),
string().rfind(
string()));
393 EXPECT_EQ(
d.rfind(d),
string().rfind(
string()));
394 EXPECT_EQ(e.rfind(d),
string().rfind(
string()));
395 EXPECT_EQ(
d.rfind(e),
string().rfind(
string()));
396 EXPECT_EQ(e.rfind(e),
string().rfind(
string()));
453 StringPiece
h(
"====");
569 string a2 =
a.as_string();
578 TEST(StringPiece, Custom) {
579 StringPiece
a(
"foobar");
599 a.AppendToString(&s2);
602 a.AppendToString(&s2);
637 c.remove_prefix(c.size());
647 c.remove_suffix(c.size());
661 c.set(
static_cast<const void*
>(
"foobar"), 6);
663 c.set(
static_cast<const void*
>(
"foobar"), 0);
665 c.set(
static_cast<const void*
>(
"foobar"), 7);
669 string s3(
a.as_string().c_str(), 7);
671 string s4(e.as_string());
676 string s5(
a.ToString().c_str(), 7);
678 string s6(e.ToString());
701 StringPiece
a(
"abcdefg");
702 StringPiece
b(
"abcd");
703 StringPiece c(
"efg");
710 TEST(StringPiece, NullInput) {
712 StringPiece
s(
nullptr);
725 TEST(StringPiece, Comparisons2) {
726 StringPiece abc(
"abcdefghijklmnopqrstuvwxyz");
729 EXPECT_EQ(abc, StringPiece(
"abcdefghijklmnopqrstuvwxyz"));
730 EXPECT_EQ(abc.compare(StringPiece(
"abcdefghijklmnopqrstuvwxyz")), 0);
732 EXPECT_LT(abc, StringPiece(
"abcdefghijklmnopqrstuvwxzz"));
733 EXPECT_LT(abc.compare(StringPiece(
"abcdefghijklmnopqrstuvwxzz")), 0);
735 EXPECT_GT(abc, StringPiece(
"abcdefghijklmnopqrstuvwxyy"));
736 EXPECT_GT(abc.compare(StringPiece(
"abcdefghijklmnopqrstuvwxyy")), 0);
749 TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
754 TEST(ComparisonOpsTest, HeterogenousStringPieceEquals) {
755 EXPECT_EQ(StringPiece(
"hello"),
string(
"hello"));
756 EXPECT_EQ(
"hello", StringPiece(
"hello"));
759 TEST(FindOneCharTest, EdgeCases) {
760 StringPiece
a(
"xxyyyxx");
786 #ifdef PROTOBUF_HAS_DEATH_TEST
788 TEST(NonNegativeLenTest, NonNegativeLen) {
789 EXPECT_DEATH(StringPiece(
"xyz", -1),
"len >= 0");
792 #endif // PROTOBUF_HAS_DEATH_TEST