2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 // @author Kristina Holst (kholst@fb.com)
19 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
21 #include <boost/range/concepts.hpp>
22 #include <gtest/gtest.h>
23 #include "folly/Range.h"
25 using namespace folly;
28 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
30 TEST(StringPiece, All) {
31 const char* foo = "foo";
32 const char* foo2 = "foo";
36 // we expect the compiler to optimize things so that there's only one copy
37 // of the string literal "foo", even though we've got it in multiple places
38 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
39 // comparison rather than lexical
41 // the string object creates copies though, so the c_str of these should be
43 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
45 // test the basic StringPiece functionality
47 EXPECT_EQ(s.size(), 3);
49 EXPECT_EQ(s.start(), foo); // ptr comparison
50 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
51 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
53 EXPECT_EQ(s.toString(), foo); // lexical comparison
54 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
55 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
57 EXPECT_EQ(s, foo); // lexical comparison
58 EXPECT_EQ(s, fooStr); // lexical comparison
59 EXPECT_EQ(s, foo2Str); // lexical comparison
62 // check using StringPiece to reference substrings
63 const char* foobarbaz = "foobarbaz";
65 // the full "foobarbaz"
66 s.reset(foobarbaz, strlen(foobarbaz));
67 EXPECT_EQ(s.size(), 9);
68 EXPECT_EQ(s.start(), foobarbaz);
69 EXPECT_EQ(s, "foobarbaz");
72 s.assign(foobarbaz, foobarbaz + 3);
73 EXPECT_EQ(s.size(), 3);
74 EXPECT_EQ(s.start(), foobarbaz);
78 s.reset(foobarbaz, strlen(foobarbaz));
79 EXPECT_EQ(s.find("bar"), 3);
80 EXPECT_EQ(s.find("ba", 3), 3);
81 EXPECT_EQ(s.find("ba", 4), 6);
82 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
83 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
84 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
85 // starting pos that is obviously past the end -- This works for std::string
86 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
87 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
88 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
91 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
92 EXPECT_EQ(s.size(), 6);
93 EXPECT_EQ(s.start(), foobarbaz + 3);
94 EXPECT_EQ(s, "barbaz");
97 s.reset(foobarbaz + 3, 3);
98 EXPECT_EQ(s.size(), 3);
103 EXPECT_EQ(s.toString(), "");
105 // test an empty StringPiece
107 EXPECT_EQ(s2.size(), 0);
109 // Test comparison operators
142 TEST(StringPiece, ToByteRange) {
143 StringPiece a("hello");
145 EXPECT_EQ(static_cast<const void*>(a.begin()),
146 static_cast<const void*>(b.begin()));
147 EXPECT_EQ(static_cast<const void*>(a.end()),
148 static_cast<const void*>(b.end()));