2 * Copyright 2013 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)
23 #include <boost/range/concepts.hpp>
24 #include <gtest/gtest.h>
25 #include "folly/Range.h"
27 namespace folly { namespace detail {
28 // declaration of functions in Range.cpp
29 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
30 const StringPiece& needles);
32 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
33 const StringPiece& needles);
35 size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
36 const StringPiece& needles);
39 using namespace folly;
42 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
44 TEST(StringPiece, All) {
45 const char* foo = "foo";
46 const char* foo2 = "foo";
50 // we expect the compiler to optimize things so that there's only one copy
51 // of the string literal "foo", even though we've got it in multiple places
52 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
53 // comparison rather than lexical
55 // the string object creates copies though, so the c_str of these should be
57 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
59 // test the basic StringPiece functionality
61 EXPECT_EQ(s.size(), 3);
63 EXPECT_EQ(s.start(), foo); // ptr comparison
64 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
65 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
67 EXPECT_EQ(s.toString(), foo); // lexical comparison
68 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
69 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
71 EXPECT_EQ(s, foo); // lexical comparison
72 EXPECT_EQ(s, fooStr); // lexical comparison
73 EXPECT_EQ(s, foo2Str); // lexical comparison
76 // check using StringPiece to reference substrings
77 const char* foobarbaz = "foobarbaz";
79 // the full "foobarbaz"
80 s.reset(foobarbaz, strlen(foobarbaz));
81 EXPECT_EQ(s.size(), 9);
82 EXPECT_EQ(s.start(), foobarbaz);
83 EXPECT_EQ(s, "foobarbaz");
86 s.assign(foobarbaz, foobarbaz + 3);
87 EXPECT_EQ(s.size(), 3);
88 EXPECT_EQ(s.start(), foobarbaz);
92 s.reset(foobarbaz, strlen(foobarbaz));
93 EXPECT_EQ(s.find("bar"), 3);
94 EXPECT_EQ(s.find("ba", 3), 3);
95 EXPECT_EQ(s.find("ba", 4), 6);
96 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
97 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
98 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
99 // starting pos that is obviously past the end -- This works for std::string
100 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
101 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
102 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
104 EXPECT_EQ(s.find(""), std::string().find(""));
105 EXPECT_EQ(s.find(""), 0);
108 EXPECT_EQ(s.find('b'), 3);
109 EXPECT_EQ(s.find('b', 3), 3);
110 EXPECT_EQ(s.find('b', 4), 6);
111 EXPECT_EQ(s.find('o', 2), 2);
112 EXPECT_EQ(s.find('y'), StringPiece::npos);
113 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
114 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
115 // starting pos that is obviously past the end -- This works for std::string
116 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
117 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
118 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
120 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
121 EXPECT_EQ(s.find('\0'), StringPiece::npos);
124 s.reset(foobarbaz, strlen(foobarbaz));
125 EXPECT_EQ(s.find_first_of("bar"), 3);
126 EXPECT_EQ(s.find_first_of("ba", 3), 3);
127 EXPECT_EQ(s.find_first_of("ba", 4), 4);
128 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
129 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
130 // starting position too far
131 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
132 // starting pos that is obviously past the end -- This works for std::string
133 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
134 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
135 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
136 // empty needle. Note that this returns npos, while find() returns 0!
137 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
138 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
140 // single char find_first_ofs
141 EXPECT_EQ(s.find_first_of('b'), 3);
142 EXPECT_EQ(s.find_first_of('b', 3), 3);
143 EXPECT_EQ(s.find_first_of('b', 4), 6);
144 EXPECT_EQ(s.find_first_of('o', 2), 2);
145 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
146 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
147 // starting position too far
148 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
149 // starting pos that is obviously past the end -- This works for std::string
150 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
151 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
152 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
154 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
155 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
158 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
159 EXPECT_EQ(s.size(), 6);
160 EXPECT_EQ(s.start(), foobarbaz + 3);
161 EXPECT_EQ(s, "barbaz");
164 s.reset(foobarbaz + 3, 3);
165 EXPECT_EQ(s.size(), 3);
170 EXPECT_EQ(s.toString(), "");
172 // test an empty StringPiece
174 EXPECT_EQ(s2.size(), 0);
176 // Test comparison operators
209 TEST(StringPiece, ToByteRange) {
210 StringPiece a("hello");
212 EXPECT_EQ(static_cast<const void*>(a.begin()),
213 static_cast<const void*>(b.begin()));
214 EXPECT_EQ(static_cast<const void*>(a.end()),
215 static_cast<const void*>(b.end()));
217 // and convert back again
219 EXPECT_EQ(a.begin(), c.begin());
220 EXPECT_EQ(a.end(), c.end());
223 template <typename NeedleFinder>
224 class NeedleFinderTest : public ::testing::Test {
226 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
227 return NeedleFinder::find_first_byte_of(haystack, needles);
231 struct SseNeedleFinder {
232 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
233 // This will only use the SSE version if it is supported on this CPU
234 // (selected using ifunc).
235 return detail::qfind_first_byte_of(haystack, needles);
239 struct NoSseNeedleFinder {
240 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
241 return detail::qfind_first_byte_of_nosse(haystack, needles);
245 struct MemchrNeedleFinder {
246 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
247 return detail::qfind_first_byte_of_memchr(haystack, needles);
251 struct ByteSetNeedleFinder {
252 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
253 return detail::qfind_first_byte_of_byteset(haystack, needles);
257 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
258 ByteSetNeedleFinder> NeedleFinders;
259 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
261 TYPED_TEST(NeedleFinderTest, Null) {
262 { // null characters in the string
263 string s(10, char(0));
265 string delims("abc");
266 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
268 { // null characters in delim
270 string delims(10, char(0));
273 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
275 { // range not terminated by null character
276 string buf = "abcdefghijklmnopqrstuvwxyz";
277 StringPiece s(buf.data() + 5, 3);
278 StringPiece delims("z");
279 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
283 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
284 string delims(1000, 'b');
285 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
286 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
289 TYPED_TEST(NeedleFinderTest, Empty) {
292 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
293 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
294 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
297 TYPED_TEST(NeedleFinderTest, Unaligned) {
298 // works correctly even if input buffers are not 16-byte aligned
299 string s = "0123456789ABCDEFGH";
300 for (int i = 0; i < s.size(); ++i) {
301 StringPiece a(s.c_str() + i);
302 for (int j = 0; j < s.size(); ++j) {
303 StringPiece b(s.c_str() + j);
304 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
309 // for some algorithms (specifically those that create a set of needles),
310 // we check for the edge-case of _all_ possible needles being sought.
311 TYPED_TEST(NeedleFinderTest, Needles256) {
313 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
314 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
315 // make the size ~big to avoid any edge-case branches for tiny haystacks
316 const int haystackSize = 50;
317 for (int i = minValue; i <= maxValue; i++) { // <=
318 needles.push_back(i);
320 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
321 for (int i = minValue; i <= maxValue; i++) {
322 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
325 needles.append("these are redundant characters");
326 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
327 for (int i = minValue; i <= maxValue; i++) {
328 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
332 TYPED_TEST(NeedleFinderTest, Base) {
333 for (int i = 0; i < 32; ++i) {
334 for (int j = 0; j < 32; ++j) {
335 string s = string(i, 'X') + "abca" + string(i, 'X');
336 string delims = string(j, 'Y') + "a" + string(j, 'Y');
337 EXPECT_EQ(i, this->find_first_byte_of(s, delims));