2 * Copyright 2014 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"
27 #include <boost/range/concepts.hpp>
28 #include <gtest/gtest.h>
30 namespace folly { namespace detail {
32 // declaration of functions in Range.cpp
33 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
34 const StringPiece& needles);
36 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
37 const StringPiece& needles);
41 using namespace folly;
44 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
46 TEST(StringPiece, All) {
47 const char* foo = "foo";
48 const char* foo2 = "foo";
52 // we expect the compiler to optimize things so that there's only one copy
53 // of the string literal "foo", even though we've got it in multiple places
54 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
55 // comparison rather than lexical
57 // the string object creates copies though, so the c_str of these should be
59 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
61 // test the basic StringPiece functionality
63 EXPECT_EQ(s.size(), 3);
65 EXPECT_EQ(s.start(), foo); // ptr comparison
66 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
67 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
69 EXPECT_EQ(s.toString(), foo); // lexical comparison
70 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
71 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
73 EXPECT_EQ(s, foo); // lexical comparison
74 EXPECT_EQ(s, fooStr); // lexical comparison
75 EXPECT_EQ(s, foo2Str); // lexical comparison
78 // check using StringPiece to reference substrings
79 const char* foobarbaz = "foobarbaz";
81 // the full "foobarbaz"
82 s.reset(foobarbaz, strlen(foobarbaz));
83 EXPECT_EQ(s.size(), 9);
84 EXPECT_EQ(s.start(), foobarbaz);
85 EXPECT_EQ(s, "foobarbaz");
88 s.assign(foobarbaz, foobarbaz + 3);
89 EXPECT_EQ(s.size(), 3);
90 EXPECT_EQ(s.start(), foobarbaz);
94 s.reset(foobarbaz, strlen(foobarbaz));
95 EXPECT_EQ(s.find("bar"), 3);
96 EXPECT_EQ(s.find("ba", 3), 3);
97 EXPECT_EQ(s.find("ba", 4), 6);
98 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
99 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
100 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
101 // starting pos that is obviously past the end -- This works for std::string
102 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
103 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
104 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
106 EXPECT_EQ(s.find(""), std::string().find(""));
107 EXPECT_EQ(s.find(""), 0);
110 EXPECT_EQ(s.find('b'), 3);
111 EXPECT_EQ(s.find('b', 3), 3);
112 EXPECT_EQ(s.find('b', 4), 6);
113 EXPECT_EQ(s.find('o', 2), 2);
114 EXPECT_EQ(s.find('y'), StringPiece::npos);
115 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
116 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
117 // starting pos that is obviously past the end -- This works for std::string
118 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
119 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
120 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
122 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
123 EXPECT_EQ(s.find('\0'), StringPiece::npos);
125 // single char rfinds
126 EXPECT_EQ(s.rfind('b'), 6);
127 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
128 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
129 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
130 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
132 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
133 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
136 s.reset(foobarbaz, strlen(foobarbaz));
137 EXPECT_EQ(s.find_first_of("bar"), 3);
138 EXPECT_EQ(s.find_first_of("ba", 3), 3);
139 EXPECT_EQ(s.find_first_of("ba", 4), 4);
140 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
141 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
142 // starting position too far
143 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
144 // starting pos that is obviously past the end -- This works for std::string
145 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
146 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
147 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
148 // empty needle. Note that this returns npos, while find() returns 0!
149 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
150 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
152 // single char find_first_ofs
153 EXPECT_EQ(s.find_first_of('b'), 3);
154 EXPECT_EQ(s.find_first_of('b', 3), 3);
155 EXPECT_EQ(s.find_first_of('b', 4), 6);
156 EXPECT_EQ(s.find_first_of('o', 2), 2);
157 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
158 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
159 // starting position too far
160 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
161 // starting pos that is obviously past the end -- This works for std::string
162 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
163 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
164 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
166 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
167 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
170 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
171 EXPECT_EQ(s.size(), 6);
172 EXPECT_EQ(s.start(), foobarbaz + 3);
173 EXPECT_EQ(s, "barbaz");
176 s.reset(foobarbaz + 3, 3);
177 EXPECT_EQ(s.size(), 3);
182 EXPECT_EQ(s.toString(), "");
184 // test an empty StringPiece
186 EXPECT_EQ(s2.size(), 0);
188 // Test comparison operators
222 void expectLT(const T& a, const T& b) {
225 EXPECT_FALSE(a == b);
226 EXPECT_FALSE(a >= b);
230 EXPECT_FALSE(b <= a);
236 void expectEQ(const T& a, const T& b) {
244 TEST(StringPiece, EightBitComparisons) {
245 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
246 constexpr size_t count = sizeof(values) / sizeof(values[0]);
247 for (size_t i = 0; i < count; ++i) {
248 std::string a(1, values[i]);
249 // Defeat copy-on-write
250 std::string aCopy(a.data(), a.size());
252 expectEQ(StringPiece(a), StringPiece(aCopy));
254 for (size_t j = i + 1; j < count; ++j) {
255 std::string b(1, values[j]);
257 expectLT(StringPiece(a), StringPiece(b));
262 TEST(StringPiece, ToByteRange) {
263 StringPiece a("hello");
265 EXPECT_EQ(static_cast<const void*>(a.begin()),
266 static_cast<const void*>(b.begin()));
267 EXPECT_EQ(static_cast<const void*>(a.end()),
268 static_cast<const void*>(b.end()));
270 // and convert back again
272 EXPECT_EQ(a.begin(), c.begin());
273 EXPECT_EQ(a.end(), c.end());
276 TEST(StringPiece, InvalidRange) {
277 StringPiece a("hello");
278 EXPECT_EQ(a, a.subpiece(0, 10));
279 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
280 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
281 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
282 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
283 EXPECT_THROW(a.subpiece(6), std::out_of_range);
285 std::string b("hello");
286 EXPECT_EQ(a, StringPiece(b, 0, 10));
287 EXPECT_EQ("ello", a.subpiece(1));
288 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
289 EXPECT_EQ("ell", a.subpiece(1, 3));
290 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
291 EXPECT_THROW(a.subpiece(6), std::out_of_range);
294 #if FOLLY_HAVE_CONSTEXPR_STRLEN
295 constexpr char helloArray[] = "hello";
297 TEST(StringPiece, Constexpr) {
298 constexpr StringPiece hello1("hello");
299 EXPECT_EQ("hello", hello1);
301 constexpr StringPiece hello2(helloArray);
302 EXPECT_EQ("hello", hello2);
306 TEST(StringPiece, Prefix) {
307 StringPiece a("hello");
308 EXPECT_TRUE(a.startsWith(""));
309 EXPECT_TRUE(a.startsWith("h"));
310 EXPECT_TRUE(a.startsWith('h'));
311 EXPECT_TRUE(a.startsWith("hello"));
312 EXPECT_FALSE(a.startsWith("hellox"));
313 EXPECT_FALSE(a.startsWith('x'));
314 EXPECT_FALSE(a.startsWith("x"));
318 EXPECT_TRUE(b.removePrefix(""));
319 EXPECT_EQ("hello", b);
323 EXPECT_TRUE(b.removePrefix("h"));
324 EXPECT_EQ("ello", b);
328 EXPECT_TRUE(b.removePrefix('h'));
329 EXPECT_EQ("ello", b);
333 EXPECT_TRUE(b.removePrefix("hello"));
338 EXPECT_FALSE(b.removePrefix("hellox"));
339 EXPECT_EQ("hello", b);
343 EXPECT_FALSE(b.removePrefix("x"));
344 EXPECT_EQ("hello", b);
348 EXPECT_FALSE(b.removePrefix('x'));
349 EXPECT_EQ("hello", b);
353 TEST(StringPiece, Suffix) {
354 StringPiece a("hello");
355 EXPECT_TRUE(a.endsWith(""));
356 EXPECT_TRUE(a.endsWith("o"));
357 EXPECT_TRUE(a.endsWith('o'));
358 EXPECT_TRUE(a.endsWith("hello"));
359 EXPECT_FALSE(a.endsWith("xhello"));
360 EXPECT_FALSE(a.endsWith("x"));
361 EXPECT_FALSE(a.endsWith('x'));
365 EXPECT_TRUE(b.removeSuffix(""));
366 EXPECT_EQ("hello", b);
370 EXPECT_TRUE(b.removeSuffix("o"));
371 EXPECT_EQ("hell", b);
375 EXPECT_TRUE(b.removeSuffix('o'));
376 EXPECT_EQ("hell", b);
380 EXPECT_TRUE(b.removeSuffix("hello"));
385 EXPECT_FALSE(b.removeSuffix("xhello"));
386 EXPECT_EQ("hello", b);
390 EXPECT_FALSE(b.removeSuffix("x"));
391 EXPECT_EQ("hello", b);
395 EXPECT_FALSE(b.removeSuffix('x'));
396 EXPECT_EQ("hello", b);
400 TEST(StringPiece, PrefixEmpty) {
402 EXPECT_TRUE(a.startsWith(""));
403 EXPECT_FALSE(a.startsWith("a"));
404 EXPECT_FALSE(a.startsWith('a'));
405 EXPECT_TRUE(a.removePrefix(""));
407 EXPECT_FALSE(a.removePrefix("a"));
409 EXPECT_FALSE(a.removePrefix('a'));
413 TEST(StringPiece, SuffixEmpty) {
415 EXPECT_TRUE(a.endsWith(""));
416 EXPECT_FALSE(a.endsWith("a"));
417 EXPECT_FALSE(a.endsWith('a'));
418 EXPECT_TRUE(a.removeSuffix(""));
420 EXPECT_FALSE(a.removeSuffix("a"));
422 EXPECT_FALSE(a.removeSuffix('a'));
426 TEST(StringPiece, split_step_char_delimiter) {
428 // 012345678901234567890123456
429 auto const s = "this is just a test string";
430 auto const e = std::next(s, std::strlen(s));
433 folly::StringPiece p(s);
434 EXPECT_EQ(s, p.begin());
435 EXPECT_EQ(e, p.end());
437 auto x = p.split_step(' ');
438 EXPECT_EQ(std::next(s, 5), p.begin());
439 EXPECT_EQ(e, p.end());
440 EXPECT_EQ("this", x);
442 x = p.split_step(' ');
443 EXPECT_EQ(std::next(s, 8), p.begin());
444 EXPECT_EQ(e, p.end());
447 x = p.split_step('u');
448 EXPECT_EQ(std::next(s, 10), p.begin());
449 EXPECT_EQ(e, p.end());
452 x = p.split_step(' ');
453 EXPECT_EQ(std::next(s, 13), p.begin());
454 EXPECT_EQ(e, p.end());
457 x = p.split_step(' ');
458 EXPECT_EQ(std::next(s, 14), p.begin());
459 EXPECT_EQ(e, p.end());
462 x = p.split_step(' ');
463 EXPECT_EQ(std::next(s, 16), p.begin());
464 EXPECT_EQ(e, p.end());
467 x = p.split_step(' ');
468 EXPECT_EQ(std::next(s, 21), p.begin());
469 EXPECT_EQ(e, p.end());
470 EXPECT_EQ("test", x);
472 x = p.split_step(' ');
473 EXPECT_EQ(e, p.begin());
474 EXPECT_EQ(e, p.end());
475 EXPECT_EQ("string", x);
477 x = p.split_step(' ');
478 EXPECT_EQ(e, p.begin());
479 EXPECT_EQ(e, p.end());
483 TEST(StringPiece, split_step_range_delimiter) {
485 // 0123456789012345678901234567890123
486 auto const s = "this is just a test string";
487 auto const e = std::next(s, std::strlen(s));
490 folly::StringPiece p(s);
491 EXPECT_EQ(s, p.begin());
492 EXPECT_EQ(e, p.end());
494 auto x = p.split_step(" ");
495 EXPECT_EQ(std::next(s, 6), p.begin());
496 EXPECT_EQ(e, p.end());
497 EXPECT_EQ("this", x);
499 x = p.split_step(" ");
500 EXPECT_EQ(std::next(s, 10), p.begin());
501 EXPECT_EQ(e, p.end());
504 x = p.split_step("u");
505 EXPECT_EQ(std::next(s, 12), p.begin());
506 EXPECT_EQ(e, p.end());
509 x = p.split_step(" ");
510 EXPECT_EQ(std::next(s, 16), p.begin());
511 EXPECT_EQ(e, p.end());
514 x = p.split_step(" ");
515 EXPECT_EQ(std::next(s, 18), p.begin());
516 EXPECT_EQ(e, p.end());
519 x = p.split_step(" ");
520 EXPECT_EQ(std::next(s, 21), p.begin());
521 EXPECT_EQ(e, p.end());
524 x = p.split_step(" ");
525 EXPECT_EQ(std::next(s, 28), p.begin());
526 EXPECT_EQ(e, p.end());
527 EXPECT_EQ(" test", x);
529 x = p.split_step(" ");
530 EXPECT_EQ(e, p.begin());
531 EXPECT_EQ(e, p.end());
532 EXPECT_EQ("string", x);
534 x = p.split_step(" ");
535 EXPECT_EQ(e, p.begin());
536 EXPECT_EQ(e, p.end());
539 x = p.split_step(" ");
540 EXPECT_EQ(e, p.begin());
541 EXPECT_EQ(e, p.end());
545 void split_step_with_process_noop(folly::StringPiece) {}
547 TEST(StringPiece, split_step_with_process_char_delimiter) {
549 // 012345678901234567890123456
550 auto const s = "this is just a test string";
551 auto const e = std::next(s, std::strlen(s));
554 folly::StringPiece p(s);
555 EXPECT_EQ(s, p.begin());
556 EXPECT_EQ(e, p.end());
558 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
559 EXPECT_EQ(std::next(s, 5), p.begin());
560 EXPECT_EQ(e, p.end());
561 EXPECT_EQ("this", x);
565 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
566 EXPECT_EQ(std::next(s, 8), p.begin());
567 EXPECT_EQ(e, p.end());
572 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
573 EXPECT_EQ(std::next(s, 10), p.begin());
574 EXPECT_EQ(e, p.end());
579 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
580 EXPECT_EQ(std::next(s, 13), p.begin());
581 EXPECT_EQ(e, p.end());
586 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
587 EXPECT_EQ(std::next(s, 14), p.begin());
588 EXPECT_EQ(e, p.end());
593 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
594 EXPECT_EQ(std::next(s, 16), p.begin());
595 EXPECT_EQ(e, p.end());
600 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
601 EXPECT_EQ(std::next(s, 21), p.begin());
602 EXPECT_EQ(e, p.end());
603 EXPECT_EQ("test", x);
607 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
608 EXPECT_EQ(e, p.begin());
609 EXPECT_EQ(e, p.end());
610 EXPECT_EQ("string", x);
614 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
615 EXPECT_EQ(e, p.begin());
616 EXPECT_EQ(e, p.end());
621 EXPECT_TRUE((std::is_same<
623 decltype(p.split_step(' ', split_step_with_process_noop))
626 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
629 TEST(StringPiece, split_step_with_process_range_delimiter) {
631 // 0123456789012345678901234567890123
632 auto const s = "this is just a test string";
633 auto const e = std::next(s, std::strlen(s));
636 folly::StringPiece p(s);
637 EXPECT_EQ(s, p.begin());
638 EXPECT_EQ(e, p.end());
640 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
641 EXPECT_EQ(std::next(s, 6), p.begin());
642 EXPECT_EQ(e, p.end());
643 EXPECT_EQ("this", x);
647 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
648 EXPECT_EQ(std::next(s, 10), p.begin());
649 EXPECT_EQ(e, p.end());
654 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
655 EXPECT_EQ(std::next(s, 12), p.begin());
656 EXPECT_EQ(e, p.end());
661 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
662 EXPECT_EQ(std::next(s, 16), p.begin());
663 EXPECT_EQ(e, p.end());
668 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
669 EXPECT_EQ(std::next(s, 18), p.begin());
670 EXPECT_EQ(e, p.end());
675 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
676 EXPECT_EQ(std::next(s, 21), p.begin());
677 EXPECT_EQ(e, p.end());
682 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
683 EXPECT_EQ(std::next(s, 28), p.begin());
684 EXPECT_EQ(e, p.end());
685 EXPECT_EQ(" test", x);
689 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
690 EXPECT_EQ(e, p.begin());
691 EXPECT_EQ(e, p.end());
692 EXPECT_EQ("string", x);
696 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
697 EXPECT_EQ(e, p.begin());
698 EXPECT_EQ(e, p.end());
703 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
704 EXPECT_EQ(e, p.begin());
705 EXPECT_EQ(e, p.end());
710 EXPECT_TRUE((std::is_same<
712 decltype(p.split_step(' ', split_step_with_process_noop))
715 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
718 TEST(qfind, UInt32_Ranges) {
719 vector<uint32_t> a({1, 2, 3, 260, 5});
720 vector<uint32_t> b({2, 3, 4});
722 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
723 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
725 EXPECT_EQ(qfind(a_range, b_range), string::npos);
728 EXPECT_EQ(qfind(a_range, b_range), 1);
731 template <typename NeedleFinder>
732 class NeedleFinderTest : public ::testing::Test {
734 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
735 return NeedleFinder::find_first_byte_of(haystack, needles);
739 struct SseNeedleFinder {
740 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
741 // This will only use the SSE version if it is supported on this CPU
742 // (selected using ifunc).
743 return detail::qfind_first_byte_of(haystack, needles);
747 struct NoSseNeedleFinder {
748 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
749 return detail::qfind_first_byte_of_nosse(haystack, needles);
753 struct MemchrNeedleFinder {
754 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
755 return detail::qfind_first_byte_of_memchr(haystack, needles);
759 struct ByteSetNeedleFinder {
760 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
761 return detail::qfind_first_byte_of_byteset(haystack, needles);
765 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
766 ByteSetNeedleFinder> NeedleFinders;
767 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
769 TYPED_TEST(NeedleFinderTest, Null) {
770 { // null characters in the string
771 string s(10, char(0));
773 string delims("abc");
774 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
776 { // null characters in delim
778 string delims(10, char(0));
781 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
783 { // range not terminated by null character
784 string buf = "abcdefghijklmnopqrstuvwxyz";
785 StringPiece s(buf.data() + 5, 3);
786 StringPiece delims("z");
787 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
791 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
792 string delims(1000, 'b');
793 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
794 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
797 TYPED_TEST(NeedleFinderTest, Empty) {
800 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
801 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
802 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
805 TYPED_TEST(NeedleFinderTest, Unaligned) {
806 // works correctly even if input buffers are not 16-byte aligned
807 string s = "0123456789ABCDEFGH";
808 for (int i = 0; i < s.size(); ++i) {
809 StringPiece a(s.c_str() + i);
810 for (int j = 0; j < s.size(); ++j) {
811 StringPiece b(s.c_str() + j);
812 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
817 // for some algorithms (specifically those that create a set of needles),
818 // we check for the edge-case of _all_ possible needles being sought.
819 TYPED_TEST(NeedleFinderTest, Needles256) {
821 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
822 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
823 // make the size ~big to avoid any edge-case branches for tiny haystacks
824 const int haystackSize = 50;
825 for (int i = minValue; i <= maxValue; i++) { // <=
826 needles.push_back(i);
828 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
829 for (int i = minValue; i <= maxValue; i++) {
830 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
833 needles.append("these are redundant characters");
834 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
835 for (int i = minValue; i <= maxValue; i++) {
836 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
840 TYPED_TEST(NeedleFinderTest, Base) {
841 for (int i = 0; i < 32; ++i) {
842 for (int j = 0; j < 32; ++j) {
843 string s = string(i, 'X') + "abca" + string(i, 'X');
844 string delims = string(j, 'Y') + "a" + string(j, 'Y');
845 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
850 const size_t kPageSize = 4096;
851 // Updates contents so that any read accesses past the last byte will
852 // cause a SIGSEGV. It accomplishes this by changing access to the page that
853 // begins immediately after the end of the contents (as allocators and mmap()
854 // all operate on page boundaries, this is a reasonable assumption).
855 // This function will also initialize buf, which caller must free().
856 void createProtectedBuf(StringPiece& contents, char** buf) {
857 ASSERT_LE(contents.size(), kPageSize);
858 const size_t kSuccess = 0;
859 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
862 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
863 size_t newBegin = kPageSize - contents.size();
864 memcpy(*buf + newBegin, contents.data(), contents.size());
865 contents.reset(*buf + newBegin, contents.size());
868 void freeProtectedBuf(char* buf) {
869 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
873 TYPED_TEST(NeedleFinderTest, NoSegFault) {
874 const string base = string(32, 'a') + string("b");
875 const string delims = string(32, 'c') + string("b");
876 for (int i = 0; i <= 32; i++) {
877 for (int j = 0; j <= 33; j++) {
878 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
879 StringPiece s1(base);
881 ASSERT_TRUE(!s1.empty());
885 StringPiece s2(delims);
889 createProtectedBuf(s1, &buf1);
890 createProtectedBuf(s2, &buf2);
891 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
892 // string(s1.data(), s1.size()).c_str(), s1.size(),
893 // string(s2.data(), s2.size()).c_str(), s2.size());
894 auto r1 = this->find_first_byte_of(s1, s2);
895 auto f1 = std::find_first_of(s1.begin(), s1.end(),
896 s2.begin(), s2.end());
897 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
899 auto r2 = this->find_first_byte_of(s2, s1);
900 auto f2 = std::find_first_of(s2.begin(), s2.end(),
901 s1.begin(), s1.end());
902 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
904 freeProtectedBuf(buf1);
905 freeProtectedBuf(buf2);
911 TEST(NonConstTest, StringPiece) {
912 std::string hello("hello");
913 MutableStringPiece sp(&hello.front(), hello.size());
915 EXPECT_EQ("xello", hello);
918 EXPECT_EQ("xello", s);
922 MutableByteRange r2(sp);