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);
124 // single char rfinds
125 EXPECT_EQ(s.rfind('b'), 6);
126 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
127 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
128 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
129 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
131 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
132 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
135 s.reset(foobarbaz, strlen(foobarbaz));
136 EXPECT_EQ(s.find_first_of("bar"), 3);
137 EXPECT_EQ(s.find_first_of("ba", 3), 3);
138 EXPECT_EQ(s.find_first_of("ba", 4), 4);
139 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
140 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
141 // starting position too far
142 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
143 // starting pos that is obviously past the end -- This works for std::string
144 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
145 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
146 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
147 // empty needle. Note that this returns npos, while find() returns 0!
148 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
149 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
151 // single char find_first_ofs
152 EXPECT_EQ(s.find_first_of('b'), 3);
153 EXPECT_EQ(s.find_first_of('b', 3), 3);
154 EXPECT_EQ(s.find_first_of('b', 4), 6);
155 EXPECT_EQ(s.find_first_of('o', 2), 2);
156 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
157 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
158 // starting position too far
159 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
160 // starting pos that is obviously past the end -- This works for std::string
161 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
162 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
163 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
165 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
166 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
169 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
170 EXPECT_EQ(s.size(), 6);
171 EXPECT_EQ(s.start(), foobarbaz + 3);
172 EXPECT_EQ(s, "barbaz");
175 s.reset(foobarbaz + 3, 3);
176 EXPECT_EQ(s.size(), 3);
181 EXPECT_EQ(s.toString(), "");
183 // test an empty StringPiece
185 EXPECT_EQ(s2.size(), 0);
187 // Test comparison operators
221 void expectLT(const T& a, const T& b) {
224 EXPECT_FALSE(a == b);
225 EXPECT_FALSE(a >= b);
229 EXPECT_FALSE(b <= a);
235 void expectEQ(const T& a, const T& b) {
243 TEST(StringPiece, EightBitComparisons) {
244 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
245 constexpr size_t count = sizeof(values) / sizeof(values[0]);
246 for (size_t i = 0; i < count; ++i) {
247 std::string a(1, values[i]);
248 // Defeat copy-on-write
249 std::string aCopy(a.data(), a.size());
251 expectEQ(StringPiece(a), StringPiece(aCopy));
253 for (size_t j = i + 1; j < count; ++j) {
254 std::string b(1, values[j]);
256 expectLT(StringPiece(a), StringPiece(b));
261 TEST(StringPiece, ToByteRange) {
262 StringPiece a("hello");
264 EXPECT_EQ(static_cast<const void*>(a.begin()),
265 static_cast<const void*>(b.begin()));
266 EXPECT_EQ(static_cast<const void*>(a.end()),
267 static_cast<const void*>(b.end()));
269 // and convert back again
271 EXPECT_EQ(a.begin(), c.begin());
272 EXPECT_EQ(a.end(), c.end());
275 TEST(StringPiece, InvalidRange) {
276 StringPiece a("hello");
277 EXPECT_EQ(a, a.subpiece(0, 10));
278 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
279 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
280 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
281 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
282 EXPECT_THROW(a.subpiece(6), std::out_of_range);
284 std::string b("hello");
285 EXPECT_EQ(a, StringPiece(b, 0, 10));
286 EXPECT_EQ("ello", a.subpiece(1));
287 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
288 EXPECT_EQ("ell", a.subpiece(1, 3));
289 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
290 EXPECT_THROW(a.subpiece(6), std::out_of_range);
293 constexpr char helloArray[] = "hello";
295 TEST(StringPiece, Constexpr) {
296 constexpr StringPiece hello1("hello");
297 EXPECT_EQ("hello", hello1);
299 constexpr StringPiece hello2(helloArray);
300 EXPECT_EQ("hello", hello2);
303 TEST(qfind, UInt32_Ranges) {
304 vector<uint32_t> a({1, 2, 3, 260, 5});
305 vector<uint32_t> b({2, 3, 4});
307 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
308 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
310 EXPECT_EQ(qfind(a_range, b_range), string::npos);
313 EXPECT_EQ(qfind(a_range, b_range), 1);
316 template <typename NeedleFinder>
317 class NeedleFinderTest : public ::testing::Test {
319 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
320 return NeedleFinder::find_first_byte_of(haystack, needles);
324 struct SseNeedleFinder {
325 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
326 // This will only use the SSE version if it is supported on this CPU
327 // (selected using ifunc).
328 return detail::qfind_first_byte_of(haystack, needles);
332 struct NoSseNeedleFinder {
333 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
334 return detail::qfind_first_byte_of_nosse(haystack, needles);
338 struct MemchrNeedleFinder {
339 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
340 return detail::qfind_first_byte_of_memchr(haystack, needles);
344 struct ByteSetNeedleFinder {
345 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
346 return detail::qfind_first_byte_of_byteset(haystack, needles);
350 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
351 ByteSetNeedleFinder> NeedleFinders;
352 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
354 TYPED_TEST(NeedleFinderTest, Null) {
355 { // null characters in the string
356 string s(10, char(0));
358 string delims("abc");
359 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
361 { // null characters in delim
363 string delims(10, char(0));
366 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
368 { // range not terminated by null character
369 string buf = "abcdefghijklmnopqrstuvwxyz";
370 StringPiece s(buf.data() + 5, 3);
371 StringPiece delims("z");
372 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
376 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
377 string delims(1000, 'b');
378 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
379 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
382 TYPED_TEST(NeedleFinderTest, Empty) {
385 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
386 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
387 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
390 TYPED_TEST(NeedleFinderTest, Unaligned) {
391 // works correctly even if input buffers are not 16-byte aligned
392 string s = "0123456789ABCDEFGH";
393 for (int i = 0; i < s.size(); ++i) {
394 StringPiece a(s.c_str() + i);
395 for (int j = 0; j < s.size(); ++j) {
396 StringPiece b(s.c_str() + j);
397 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
402 // for some algorithms (specifically those that create a set of needles),
403 // we check for the edge-case of _all_ possible needles being sought.
404 TYPED_TEST(NeedleFinderTest, Needles256) {
406 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
407 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
408 // make the size ~big to avoid any edge-case branches for tiny haystacks
409 const int haystackSize = 50;
410 for (int i = minValue; i <= maxValue; i++) { // <=
411 needles.push_back(i);
413 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
414 for (int i = minValue; i <= maxValue; i++) {
415 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
418 needles.append("these are redundant characters");
419 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
420 for (int i = minValue; i <= maxValue; i++) {
421 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
425 TYPED_TEST(NeedleFinderTest, Base) {
426 for (int i = 0; i < 32; ++i) {
427 for (int j = 0; j < 32; ++j) {
428 string s = string(i, 'X') + "abca" + string(i, 'X');
429 string delims = string(j, 'Y') + "a" + string(j, 'Y');
430 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
435 const size_t kPageSize = 4096;
436 // Updates contents so that any read accesses past the last byte will
437 // cause a SIGSEGV. It accomplishes this by changing access to the page that
438 // begins immediately after the end of the contents (as allocators and mmap()
439 // all operate on page boundaries, this is a reasonable assumption).
440 // This function will also initialize buf, which caller must free().
441 void createProtectedBuf(StringPiece& contents, char** buf) {
442 ASSERT_LE(contents.size(), kPageSize);
443 const size_t kSuccess = 0;
444 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
447 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
448 size_t newBegin = kPageSize - contents.size();
449 memcpy(*buf + newBegin, contents.data(), contents.size());
450 contents.reset(*buf + newBegin, contents.size());
453 void freeProtectedBuf(char* buf) {
454 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
458 TYPED_TEST(NeedleFinderTest, NoSegFault) {
459 const string base = string(32, 'a') + string("b");
460 const string delims = string(32, 'c') + string("b");
461 for (int i = 0; i <= 32; i++) {
462 for (int j = 0; j <= 33; j++) {
463 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
464 StringPiece s1(base);
466 ASSERT_TRUE(!s1.empty());
470 StringPiece s2(delims);
474 createProtectedBuf(s1, &buf1);
475 createProtectedBuf(s2, &buf2);
476 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
477 // string(s1.data(), s1.size()).c_str(), s1.size(),
478 // string(s2.data(), s2.size()).c_str(), s2.size());
479 auto r1 = this->find_first_byte_of(s1, s2);
480 auto f1 = std::find_first_of(s1.begin(), s1.end(),
481 s2.begin(), s2.end());
482 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
484 auto r2 = this->find_first_byte_of(s2, s1);
485 auto f2 = std::find_first_of(s2.begin(), s2.end(),
486 s1.begin(), s1.end());
487 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
489 freeProtectedBuf(buf1);
490 freeProtectedBuf(buf2);