30 #include <google/protobuf/stubs/stringpiece.h>
38 #include <google/protobuf/testing/googletest.h>
39 #include <google/protobuf/stubs/hash.h>
40 #include <gtest/gtest.h>
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)
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();
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");
309 const StringPiece
f(
"123" "\0" "456", 7);
322 StringPiece
g(
"xx not found bb");
410 StringPiece
h(
"====");
493 TEST(StringPiece, Custom) {
494 StringPiece
a(
"foobar");
514 a.AppendToString(&s2);
517 a.AppendToString(&s2);
552 c.remove_prefix(
c.size());
562 c.remove_suffix(
c.size());
565 c = StringPiece(
"foobar", 7);
583 StringPiece
str(
"foobar");
593 StringPiece
str(
"foobar");
604 StringPiece
a(
"abcdefg");
605 StringPiece
b(
"abcd");
606 StringPiece
c(
"efg");
613 TEST(StringPiece, NullInput) {
615 StringPiece
s(
nullptr);
624 TEST(StringPiece, Comparisons2) {
625 StringPiece abc(
"abcdefghijklmnopqrstuvwxyz");
628 EXPECT_EQ(abc, StringPiece(
"abcdefghijklmnopqrstuvwxyz"));
629 EXPECT_EQ(abc.compare(StringPiece(
"abcdefghijklmnopqrstuvwxyz")), 0);
631 EXPECT_LT(abc, StringPiece(
"abcdefghijklmnopqrstuvwxzz"));
632 EXPECT_LT(abc.compare(StringPiece(
"abcdefghijklmnopqrstuvwxzz")), 0);
634 EXPECT_GT(abc, StringPiece(
"abcdefghijklmnopqrstuvwxyy"));
635 EXPECT_GT(abc.compare(StringPiece(
"abcdefghijklmnopqrstuvwxyy")), 0);
648 TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
653 TEST(ComparisonOpsTest, HeterogenousStringPieceEquals) {
655 EXPECT_EQ(
"hello", StringPiece(
"hello"));
658 TEST(FindOneCharTest, EdgeCases) {
659 StringPiece
a(
"xxyyyxx");
685 #ifdef PROTOBUF_HAS_DEATH_TEST
687 TEST(NonNegativeLenTest, NonNegativeLen) {
688 EXPECT_DEATH(StringPiece(
"xyz", -1),
"string length exceeds max size");
691 #endif // PROTOBUF_HAS_DEATH_TEST