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.
17 // @author Kristina Holst (kholst@fb.com)
18 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
20 #include "folly/Range.h"
26 #include <boost/range/concepts.hpp>
27 #include <gtest/gtest.h>
29 namespace folly { namespace detail {
31 // declaration of functions in Range.cpp
32 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
33 const StringPiece& needles);
35 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
36 const StringPiece& needles);
40 using namespace folly;
43 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
45 TEST(StringPiece, All) {
46 const char* foo = "foo";
47 const char* foo2 = "foo";
51 // we expect the compiler to optimize things so that there's only one copy
52 // of the string literal "foo", even though we've got it in multiple places
53 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
54 // comparison rather than lexical
56 // the string object creates copies though, so the c_str of these should be
58 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
60 // test the basic StringPiece functionality
62 EXPECT_EQ(s.size(), 3);
64 EXPECT_EQ(s.start(), foo); // ptr comparison
65 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
66 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
68 EXPECT_EQ(s.toString(), foo); // lexical comparison
69 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
70 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
72 EXPECT_EQ(s, foo); // lexical comparison
73 EXPECT_EQ(s, fooStr); // lexical comparison
74 EXPECT_EQ(s, foo2Str); // lexical comparison
77 // check using StringPiece to reference substrings
78 const char* foobarbaz = "foobarbaz";
80 // the full "foobarbaz"
81 s.reset(foobarbaz, strlen(foobarbaz));
82 EXPECT_EQ(s.size(), 9);
83 EXPECT_EQ(s.start(), foobarbaz);
84 EXPECT_EQ(s, "foobarbaz");
87 s.assign(foobarbaz, foobarbaz + 3);
88 EXPECT_EQ(s.size(), 3);
89 EXPECT_EQ(s.start(), foobarbaz);
93 s.reset(foobarbaz, strlen(foobarbaz));
94 EXPECT_EQ(s.find("bar"), 3);
95 EXPECT_EQ(s.find("ba", 3), 3);
96 EXPECT_EQ(s.find("ba", 4), 6);
97 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
98 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
99 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
100 // starting pos that is obviously past the end -- This works for std::string
101 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
102 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
103 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
105 EXPECT_EQ(s.find(""), std::string().find(""));
106 EXPECT_EQ(s.find(""), 0);
109 EXPECT_EQ(s.find('b'), 3);
110 EXPECT_EQ(s.find('b', 3), 3);
111 EXPECT_EQ(s.find('b', 4), 6);
112 EXPECT_EQ(s.find('o', 2), 2);
113 EXPECT_EQ(s.find('y'), StringPiece::npos);
114 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
115 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
116 // starting pos that is obviously past the end -- This works for std::string
117 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
118 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
119 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
121 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
122 EXPECT_EQ(s.find('\0'), StringPiece::npos);
125 s.reset(foobarbaz, strlen(foobarbaz));
126 EXPECT_EQ(s.find_first_of("bar"), 3);
127 EXPECT_EQ(s.find_first_of("ba", 3), 3);
128 EXPECT_EQ(s.find_first_of("ba", 4), 4);
129 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
130 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
131 // starting position too far
132 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
133 // starting pos that is obviously past the end -- This works for std::string
134 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
135 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
136 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
137 // empty needle. Note that this returns npos, while find() returns 0!
138 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
139 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
141 // single char find_first_ofs
142 EXPECT_EQ(s.find_first_of('b'), 3);
143 EXPECT_EQ(s.find_first_of('b', 3), 3);
144 EXPECT_EQ(s.find_first_of('b', 4), 6);
145 EXPECT_EQ(s.find_first_of('o', 2), 2);
146 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
147 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
148 // starting position too far
149 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
150 // starting pos that is obviously past the end -- This works for std::string
151 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
152 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
153 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
155 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
156 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
159 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
160 EXPECT_EQ(s.size(), 6);
161 EXPECT_EQ(s.start(), foobarbaz + 3);
162 EXPECT_EQ(s, "barbaz");
165 s.reset(foobarbaz + 3, 3);
166 EXPECT_EQ(s.size(), 3);
171 EXPECT_EQ(s.toString(), "");
173 // test an empty StringPiece
175 EXPECT_EQ(s2.size(), 0);
177 // Test comparison operators
210 TEST(StringPiece, ToByteRange) {
211 StringPiece a("hello");
213 EXPECT_EQ(static_cast<const void*>(a.begin()),
214 static_cast<const void*>(b.begin()));
215 EXPECT_EQ(static_cast<const void*>(a.end()),
216 static_cast<const void*>(b.end()));
218 // and convert back again
220 EXPECT_EQ(a.begin(), c.begin());
221 EXPECT_EQ(a.end(), c.end());
224 template <typename NeedleFinder>
225 class NeedleFinderTest : public ::testing::Test {
227 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
228 return NeedleFinder::find_first_byte_of(haystack, needles);
232 struct SseNeedleFinder {
233 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
234 // This will only use the SSE version if it is supported on this CPU
235 // (selected using ifunc).
236 return detail::qfind_first_byte_of(haystack, needles);
240 struct NoSseNeedleFinder {
241 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
242 return detail::qfind_first_byte_of_nosse(haystack, needles);
246 struct MemchrNeedleFinder {
247 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
248 return detail::qfind_first_byte_of_memchr(haystack, needles);
252 struct ByteSetNeedleFinder {
253 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
254 return detail::qfind_first_byte_of_byteset(haystack, needles);
258 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
259 ByteSetNeedleFinder> NeedleFinders;
260 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
262 TYPED_TEST(NeedleFinderTest, Null) {
263 { // null characters in the string
264 string s(10, char(0));
266 string delims("abc");
267 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
269 { // null characters in delim
271 string delims(10, char(0));
274 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
276 { // range not terminated by null character
277 string buf = "abcdefghijklmnopqrstuvwxyz";
278 StringPiece s(buf.data() + 5, 3);
279 StringPiece delims("z");
280 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
284 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
285 string delims(1000, 'b');
286 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
287 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
290 TYPED_TEST(NeedleFinderTest, Empty) {
293 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
294 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
295 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
298 TYPED_TEST(NeedleFinderTest, Unaligned) {
299 // works correctly even if input buffers are not 16-byte aligned
300 string s = "0123456789ABCDEFGH";
301 for (int i = 0; i < s.size(); ++i) {
302 StringPiece a(s.c_str() + i);
303 for (int j = 0; j < s.size(); ++j) {
304 StringPiece b(s.c_str() + j);
305 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
310 // for some algorithms (specifically those that create a set of needles),
311 // we check for the edge-case of _all_ possible needles being sought.
312 TYPED_TEST(NeedleFinderTest, Needles256) {
314 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
315 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
316 // make the size ~big to avoid any edge-case branches for tiny haystacks
317 const int haystackSize = 50;
318 for (int i = minValue; i <= maxValue; i++) { // <=
319 needles.push_back(i);
321 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
322 for (int i = minValue; i <= maxValue; i++) {
323 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
326 needles.append("these are redundant characters");
327 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
328 for (int i = minValue; i <= maxValue; i++) {
329 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
333 TYPED_TEST(NeedleFinderTest, Base) {
334 for (int i = 0; i < 32; ++i) {
335 for (int j = 0; j < 32; ++j) {
336 string s = string(i, 'X') + "abca" + string(i, 'X');
337 string delims = string(j, 'Y') + "a" + string(j, 'Y');
338 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
343 const size_t kPageSize = 4096;
344 // Updates contents so that any read accesses past the last byte will
345 // cause a SIGSEGV. It accomplishes this by changing access to the page that
346 // begins immediately after the end of the contents (as allocators and mmap()
347 // all operate on page boundaries, this is a reasonable assumption).
348 // This function will also initialize buf, which caller must free().
349 void createProtectedBuf(StringPiece& contents, char** buf) {
350 ASSERT_LE(contents.size(), kPageSize);
351 const size_t kSuccess = 0;
353 if (kSuccess != posix_memalign((void**)buf, kPageSize, 2 * kPageSize)) {
356 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
357 size_t newBegin = kPageSize - contents.size();
358 memcpy(*buf + newBegin, contents.data(), contents.size());
359 contents.reset(*buf + newBegin, contents.size());
362 TYPED_TEST(NeedleFinderTest, NoSegFault) {
363 const string base = string(32, 'a') + string("b");
364 const string delims = string(32, 'c') + string("b");
365 for (int i = 0; i <= 32; i++) {
366 for (int j = 0; j <= 33; j++) {
367 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
368 StringPiece s1(base);
370 ASSERT_TRUE(!s1.empty());
374 StringPiece s2(delims);
378 createProtectedBuf(s1, &buf1);
379 createProtectedBuf(s2, &buf2);
380 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
381 // string(s1.data(), s1.size()).c_str(), s1.size(),
382 // string(s2.data(), s2.size()).c_str(), s2.size());
383 auto r1 = this->find_first_byte_of(s1, s2);
384 auto f1 = std::find_first_of(s1.begin(), s1.end(),
385 s2.begin(), s2.end());
386 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
388 auto r2 = this->find_first_byte_of(s2, s1);
389 auto f2 = std::find_first_of(s2.begin(), s2.end(),
390 s1.begin(), s1.end());
391 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();