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>
23 #include <boost/range/concepts.hpp>
25 #include <gtest/gtest.h>
32 namespace folly { namespace detail {
34 // declaration of functions in Range.cpp
35 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
36 const StringPiece& needles);
38 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
39 const StringPiece& needles);
43 using namespace folly;
46 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
48 TEST(StringPiece, All) {
49 const char* foo = "foo";
50 const char* foo2 = "foo";
54 // we expect the compiler to optimize things so that there's only one copy
55 // of the string literal "foo", even though we've got it in multiple places
56 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
57 // comparison rather than lexical
59 // the string object creates copies though, so the c_str of these should be
61 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
63 // test the basic StringPiece functionality
65 EXPECT_EQ(s.size(), 3);
67 EXPECT_EQ(s.start(), foo); // ptr comparison
68 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
69 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
71 EXPECT_EQ(s.toString(), foo); // lexical comparison
72 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
73 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
75 EXPECT_EQ(s, foo); // lexical comparison
76 EXPECT_EQ(s, fooStr); // lexical comparison
77 EXPECT_EQ(s, foo2Str); // lexical comparison
80 // check using StringPiece to reference substrings
81 const char* foobarbaz = "foobarbaz";
83 // the full "foobarbaz"
84 s.reset(foobarbaz, strlen(foobarbaz));
85 EXPECT_EQ(s.size(), 9);
86 EXPECT_EQ(s.start(), foobarbaz);
87 EXPECT_EQ(s, "foobarbaz");
90 s.assign(foobarbaz, foobarbaz + 3);
91 EXPECT_EQ(s.size(), 3);
92 EXPECT_EQ(s.start(), foobarbaz);
96 s.reset(foobarbaz, strlen(foobarbaz));
97 EXPECT_EQ(s.find("bar"), 3);
98 EXPECT_EQ(s.find("ba", 3), 3);
99 EXPECT_EQ(s.find("ba", 4), 6);
100 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
101 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
102 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
103 // starting pos that is obviously past the end -- This works for std::string
104 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
105 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
106 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
108 EXPECT_EQ(s.find(""), std::string().find(""));
109 EXPECT_EQ(s.find(""), 0);
112 EXPECT_EQ(s.find('b'), 3);
113 EXPECT_EQ(s.find('b', 3), 3);
114 EXPECT_EQ(s.find('b', 4), 6);
115 EXPECT_EQ(s.find('o', 2), 2);
116 EXPECT_EQ(s.find('y'), StringPiece::npos);
117 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
118 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
119 EXPECT_TRUE(s.contains('z'));
120 // starting pos that is obviously past the end -- This works for std::string
121 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
122 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
123 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
125 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
126 EXPECT_EQ(s.find('\0'), StringPiece::npos);
127 EXPECT_FALSE(s.contains('\0'));
129 // single char rfinds
130 EXPECT_EQ(s.rfind('b'), 6);
131 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
132 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
133 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
134 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
136 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
137 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
140 s.reset(foobarbaz, strlen(foobarbaz));
141 EXPECT_EQ(s.find_first_of("bar"), 3);
142 EXPECT_EQ(s.find_first_of("ba", 3), 3);
143 EXPECT_EQ(s.find_first_of("ba", 4), 4);
144 EXPECT_TRUE(s.contains("bar"));
145 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
146 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
147 EXPECT_FALSE(s.contains("xyxy"));
148 // starting position too far
149 EXPECT_EQ(s.find_first_of("foo", 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("xyxy", 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);
154 // empty needle. Note that this returns npos, while find() returns 0!
155 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
156 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
158 // single char find_first_ofs
159 EXPECT_EQ(s.find_first_of('b'), 3);
160 EXPECT_EQ(s.find_first_of('b', 3), 3);
161 EXPECT_EQ(s.find_first_of('b', 4), 6);
162 EXPECT_EQ(s.find_first_of('o', 2), 2);
163 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
164 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
165 // starting position too far
166 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
167 // starting pos that is obviously past the end -- This works for std::string
168 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
169 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
170 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
172 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
173 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
176 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
177 EXPECT_EQ(s.size(), 6);
178 EXPECT_EQ(s.start(), foobarbaz + 3);
179 EXPECT_EQ(s, "barbaz");
182 s.reset(foobarbaz + 3, 3);
183 EXPECT_EQ(s.size(), 3);
188 EXPECT_EQ(s.toString(), "");
190 // test an empty StringPiece
192 EXPECT_EQ(s2.size(), 0);
194 // Test comparison operators
228 void expectLT(const T& a, const T& b) {
231 EXPECT_FALSE(a == b);
232 EXPECT_FALSE(a >= b);
236 EXPECT_FALSE(b <= a);
242 void expectEQ(const T& a, const T& b) {
250 TEST(StringPiece, EightBitComparisons) {
251 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
252 constexpr size_t count = sizeof(values) / sizeof(values[0]);
253 for (size_t i = 0; i < count; ++i) {
254 std::string a(1, values[i]);
255 // Defeat copy-on-write
256 std::string aCopy(a.data(), a.size());
258 expectEQ(StringPiece(a), StringPiece(aCopy));
260 for (size_t j = i + 1; j < count; ++j) {
261 std::string b(1, values[j]);
263 expectLT(StringPiece(a), StringPiece(b));
268 TEST(StringPiece, ToByteRange) {
269 StringPiece a("hello");
271 EXPECT_EQ(static_cast<const void*>(a.begin()),
272 static_cast<const void*>(b.begin()));
273 EXPECT_EQ(static_cast<const void*>(a.end()),
274 static_cast<const void*>(b.end()));
276 // and convert back again
278 EXPECT_EQ(a.begin(), c.begin());
279 EXPECT_EQ(a.end(), c.end());
282 TEST(StringPiece, InvalidRange) {
283 StringPiece a("hello");
284 EXPECT_EQ(a, a.subpiece(0, 10));
285 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
286 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
287 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
288 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
289 EXPECT_THROW(a.subpiece(6), std::out_of_range);
291 std::string b("hello");
292 EXPECT_EQ(a, StringPiece(b, 0, 10));
293 EXPECT_EQ("ello", a.subpiece(1));
294 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
295 EXPECT_EQ("ell", a.subpiece(1, 3));
296 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
297 EXPECT_THROW(a.subpiece(6), std::out_of_range);
300 #if FOLLY_HAVE_CONSTEXPR_STRLEN
301 constexpr char helloArray[] = "hello";
303 TEST(StringPiece, Constexpr) {
304 constexpr StringPiece hello1("hello");
305 EXPECT_EQ("hello", hello1);
307 constexpr StringPiece hello2(helloArray);
308 EXPECT_EQ("hello", hello2);
312 TEST(StringPiece, Prefix) {
313 StringPiece a("hello");
314 EXPECT_TRUE(a.startsWith(""));
315 EXPECT_TRUE(a.startsWith("h"));
316 EXPECT_TRUE(a.startsWith('h'));
317 EXPECT_TRUE(a.startsWith("hello"));
318 EXPECT_FALSE(a.startsWith("hellox"));
319 EXPECT_FALSE(a.startsWith('x'));
320 EXPECT_FALSE(a.startsWith("x"));
324 EXPECT_TRUE(b.removePrefix(""));
325 EXPECT_EQ("hello", b);
329 EXPECT_TRUE(b.removePrefix("h"));
330 EXPECT_EQ("ello", b);
334 EXPECT_TRUE(b.removePrefix('h'));
335 EXPECT_EQ("ello", b);
339 EXPECT_TRUE(b.removePrefix("hello"));
344 EXPECT_FALSE(b.removePrefix("hellox"));
345 EXPECT_EQ("hello", b);
349 EXPECT_FALSE(b.removePrefix("x"));
350 EXPECT_EQ("hello", b);
354 EXPECT_FALSE(b.removePrefix('x'));
355 EXPECT_EQ("hello", b);
359 TEST(StringPiece, Suffix) {
360 StringPiece a("hello");
361 EXPECT_TRUE(a.endsWith(""));
362 EXPECT_TRUE(a.endsWith("o"));
363 EXPECT_TRUE(a.endsWith('o'));
364 EXPECT_TRUE(a.endsWith("hello"));
365 EXPECT_FALSE(a.endsWith("xhello"));
366 EXPECT_FALSE(a.endsWith("x"));
367 EXPECT_FALSE(a.endsWith('x'));
371 EXPECT_TRUE(b.removeSuffix(""));
372 EXPECT_EQ("hello", b);
376 EXPECT_TRUE(b.removeSuffix("o"));
377 EXPECT_EQ("hell", b);
381 EXPECT_TRUE(b.removeSuffix('o'));
382 EXPECT_EQ("hell", b);
386 EXPECT_TRUE(b.removeSuffix("hello"));
391 EXPECT_FALSE(b.removeSuffix("xhello"));
392 EXPECT_EQ("hello", b);
396 EXPECT_FALSE(b.removeSuffix("x"));
397 EXPECT_EQ("hello", b);
401 EXPECT_FALSE(b.removeSuffix('x'));
402 EXPECT_EQ("hello", b);
406 TEST(StringPiece, PrefixEmpty) {
408 EXPECT_TRUE(a.startsWith(""));
409 EXPECT_FALSE(a.startsWith("a"));
410 EXPECT_FALSE(a.startsWith('a'));
411 EXPECT_TRUE(a.removePrefix(""));
413 EXPECT_FALSE(a.removePrefix("a"));
415 EXPECT_FALSE(a.removePrefix('a'));
419 TEST(StringPiece, SuffixEmpty) {
421 EXPECT_TRUE(a.endsWith(""));
422 EXPECT_FALSE(a.endsWith("a"));
423 EXPECT_FALSE(a.endsWith('a'));
424 EXPECT_TRUE(a.removeSuffix(""));
426 EXPECT_FALSE(a.removeSuffix("a"));
428 EXPECT_FALSE(a.removeSuffix('a'));
432 TEST(StringPiece, split_step_char_delimiter) {
434 // 012345678901234567890123456
435 auto const s = "this is just a test string";
436 auto const e = std::next(s, std::strlen(s));
439 folly::StringPiece p(s);
440 EXPECT_EQ(s, p.begin());
441 EXPECT_EQ(e, p.end());
444 auto x = p.split_step(' ');
445 EXPECT_EQ(std::next(s, 5), p.begin());
446 EXPECT_EQ(e, p.end());
447 EXPECT_EQ("this", x);
449 x = p.split_step(' ');
450 EXPECT_EQ(std::next(s, 8), p.begin());
451 EXPECT_EQ(e, p.end());
454 x = p.split_step('u');
455 EXPECT_EQ(std::next(s, 10), p.begin());
456 EXPECT_EQ(e, p.end());
459 x = p.split_step(' ');
460 EXPECT_EQ(std::next(s, 13), p.begin());
461 EXPECT_EQ(e, p.end());
464 x = p.split_step(' ');
465 EXPECT_EQ(std::next(s, 14), p.begin());
466 EXPECT_EQ(e, p.end());
469 x = p.split_step(' ');
470 EXPECT_EQ(std::next(s, 16), p.begin());
471 EXPECT_EQ(e, p.end());
474 x = p.split_step(' ');
475 EXPECT_EQ(std::next(s, 21), p.begin());
476 EXPECT_EQ(e, p.end());
477 EXPECT_EQ("test", x);
479 x = p.split_step(' ');
480 EXPECT_EQ(e, p.begin());
481 EXPECT_EQ(e, p.end());
482 EXPECT_EQ("string", x);
484 x = p.split_step(' ');
485 EXPECT_EQ(e, p.begin());
486 EXPECT_EQ(e, p.end());
490 TEST(StringPiece, split_step_range_delimiter) {
492 // 0123456789012345678901234567890123
493 auto const s = "this is just a test string";
494 auto const e = std::next(s, std::strlen(s));
497 folly::StringPiece p(s);
498 EXPECT_EQ(s, p.begin());
499 EXPECT_EQ(e, p.end());
502 auto x = p.split_step(" ");
503 EXPECT_EQ(std::next(s, 6), p.begin());
504 EXPECT_EQ(e, p.end());
505 EXPECT_EQ("this", x);
507 x = p.split_step(" ");
508 EXPECT_EQ(std::next(s, 10), p.begin());
509 EXPECT_EQ(e, p.end());
512 x = p.split_step("u");
513 EXPECT_EQ(std::next(s, 12), p.begin());
514 EXPECT_EQ(e, p.end());
517 x = p.split_step(" ");
518 EXPECT_EQ(std::next(s, 16), p.begin());
519 EXPECT_EQ(e, p.end());
522 x = p.split_step(" ");
523 EXPECT_EQ(std::next(s, 18), p.begin());
524 EXPECT_EQ(e, p.end());
527 x = p.split_step(" ");
528 EXPECT_EQ(std::next(s, 21), p.begin());
529 EXPECT_EQ(e, p.end());
532 x = p.split_step(" ");
533 EXPECT_EQ(std::next(s, 28), p.begin());
534 EXPECT_EQ(e, p.end());
535 EXPECT_EQ(" test", x);
537 x = p.split_step(" ");
538 EXPECT_EQ(e, p.begin());
539 EXPECT_EQ(e, p.end());
540 EXPECT_EQ("string", x);
542 x = p.split_step(" ");
543 EXPECT_EQ(e, p.begin());
544 EXPECT_EQ(e, p.end());
547 x = p.split_step(" ");
548 EXPECT_EQ(e, p.begin());
549 EXPECT_EQ(e, p.end());
553 void split_step_with_process_noop(folly::StringPiece) {}
555 TEST(StringPiece, split_step_with_process_char_delimiter) {
557 // 012345678901234567890123456
558 auto const s = "this is just a test string";
559 auto const e = std::next(s, std::strlen(s));
562 folly::StringPiece p(s);
563 EXPECT_EQ(s, p.begin());
564 EXPECT_EQ(e, p.end());
567 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
568 EXPECT_EQ(std::next(s, 5), p.begin());
569 EXPECT_EQ(e, p.end());
570 EXPECT_EQ("this", x);
574 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
575 EXPECT_EQ(std::next(s, 8), p.begin());
576 EXPECT_EQ(e, p.end());
581 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
582 EXPECT_EQ(std::next(s, 10), p.begin());
583 EXPECT_EQ(e, p.end());
588 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
589 EXPECT_EQ(std::next(s, 13), p.begin());
590 EXPECT_EQ(e, p.end());
595 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
596 EXPECT_EQ(std::next(s, 14), p.begin());
597 EXPECT_EQ(e, p.end());
602 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
603 EXPECT_EQ(std::next(s, 16), p.begin());
604 EXPECT_EQ(e, p.end());
609 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
610 EXPECT_EQ(std::next(s, 21), p.begin());
611 EXPECT_EQ(e, p.end());
612 EXPECT_EQ("test", x);
616 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
617 EXPECT_EQ(e, p.begin());
618 EXPECT_EQ(e, p.end());
619 EXPECT_EQ("string", x);
623 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
624 EXPECT_EQ(e, p.begin());
625 EXPECT_EQ(e, p.end());
630 EXPECT_TRUE((std::is_same<
632 decltype(p.split_step(' ', split_step_with_process_noop))
635 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
638 TEST(StringPiece, split_step_with_process_range_delimiter) {
640 // 0123456789012345678901234567890123
641 auto const s = "this is just a test string";
642 auto const e = std::next(s, std::strlen(s));
645 folly::StringPiece p(s);
646 EXPECT_EQ(s, p.begin());
647 EXPECT_EQ(e, p.end());
650 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
651 EXPECT_EQ(std::next(s, 6), p.begin());
652 EXPECT_EQ(e, p.end());
653 EXPECT_EQ("this", x);
657 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
658 EXPECT_EQ(std::next(s, 10), p.begin());
659 EXPECT_EQ(e, p.end());
664 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
665 EXPECT_EQ(std::next(s, 12), p.begin());
666 EXPECT_EQ(e, p.end());
671 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
672 EXPECT_EQ(std::next(s, 16), p.begin());
673 EXPECT_EQ(e, p.end());
678 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
679 EXPECT_EQ(std::next(s, 18), p.begin());
680 EXPECT_EQ(e, p.end());
685 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
686 EXPECT_EQ(std::next(s, 21), p.begin());
687 EXPECT_EQ(e, p.end());
692 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
693 EXPECT_EQ(std::next(s, 28), p.begin());
694 EXPECT_EQ(e, p.end());
695 EXPECT_EQ(" test", x);
699 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
700 EXPECT_EQ(e, p.begin());
701 EXPECT_EQ(e, p.end());
702 EXPECT_EQ("string", x);
706 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
707 EXPECT_EQ(e, p.begin());
708 EXPECT_EQ(e, p.end());
713 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
714 EXPECT_EQ(e, p.begin());
715 EXPECT_EQ(e, p.end());
720 EXPECT_TRUE((std::is_same<
722 decltype(p.split_step(' ', split_step_with_process_noop))
725 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
728 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
730 // 012345678901234567890123456
731 auto const s = "this is just a test string";
732 auto const e = std::next(s, std::strlen(s));
733 auto const delimiter = ' ';
736 folly::StringPiece p(s);
737 EXPECT_EQ(s, p.begin());
738 EXPECT_EQ(e, p.end());
741 auto const functor = [](
742 folly::StringPiece s,
743 folly::StringPiece expected
745 EXPECT_EQ(expected, s);
749 auto const checker = [&](folly::StringPiece expected) {
750 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
763 EXPECT_TRUE(p.empty());
766 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
768 // 0123456789012345678901234567890123
769 auto const s = "this is just a test string";
770 auto const e = std::next(s, std::strlen(s));
771 auto const delimiter = " ";
774 folly::StringPiece p(s);
775 EXPECT_EQ(s, p.begin());
776 EXPECT_EQ(e, p.end());
779 auto const functor = [](
780 folly::StringPiece s,
781 folly::StringPiece expected
783 EXPECT_EQ(expected, s);
787 auto const checker = [&](folly::StringPiece expected) {
788 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
801 EXPECT_TRUE(p.empty());
804 TEST(qfind, UInt32_Ranges) {
805 vector<uint32_t> a({1, 2, 3, 260, 5});
806 vector<uint32_t> b({2, 3, 4});
808 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
809 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
811 EXPECT_EQ(qfind(a_range, b_range), string::npos);
814 EXPECT_EQ(qfind(a_range, b_range), 1);
817 template <typename NeedleFinder>
818 class NeedleFinderTest : public ::testing::Test {
820 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
821 return NeedleFinder::find_first_byte_of(haystack, needles);
825 struct SseNeedleFinder {
826 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
827 // This will only use the SSE version if it is supported on this CPU
828 // (selected using ifunc).
829 return detail::qfind_first_byte_of(haystack, needles);
833 struct NoSseNeedleFinder {
834 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
835 return detail::qfind_first_byte_of_nosse(haystack, needles);
839 struct MemchrNeedleFinder {
840 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
841 return detail::qfind_first_byte_of_memchr(haystack, needles);
845 struct ByteSetNeedleFinder {
846 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
847 return detail::qfind_first_byte_of_byteset(haystack, needles);
851 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
852 ByteSetNeedleFinder> NeedleFinders;
853 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
855 TYPED_TEST(NeedleFinderTest, Null) {
856 { // null characters in the string
857 string s(10, char(0));
859 string delims("abc");
860 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
862 { // null characters in delim
864 string delims(10, char(0));
867 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
869 { // range not terminated by null character
870 string buf = "abcdefghijklmnopqrstuvwxyz";
871 StringPiece s(buf.data() + 5, 3);
872 StringPiece delims("z");
873 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
877 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
878 string delims(1000, 'b');
879 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
880 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
883 TYPED_TEST(NeedleFinderTest, Empty) {
886 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
887 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
888 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
891 TYPED_TEST(NeedleFinderTest, Unaligned) {
892 // works correctly even if input buffers are not 16-byte aligned
893 string s = "0123456789ABCDEFGH";
894 for (int i = 0; i < s.size(); ++i) {
895 StringPiece a(s.c_str() + i);
896 for (int j = 0; j < s.size(); ++j) {
897 StringPiece b(s.c_str() + j);
898 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
903 // for some algorithms (specifically those that create a set of needles),
904 // we check for the edge-case of _all_ possible needles being sought.
905 TYPED_TEST(NeedleFinderTest, Needles256) {
907 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
908 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
909 // make the size ~big to avoid any edge-case branches for tiny haystacks
910 const int haystackSize = 50;
911 for (int i = minValue; i <= maxValue; i++) { // <=
912 needles.push_back(i);
914 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
915 for (int i = minValue; i <= maxValue; i++) {
916 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
919 needles.append("these are redundant characters");
920 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
921 for (int i = minValue; i <= maxValue; i++) {
922 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
926 TYPED_TEST(NeedleFinderTest, Base) {
927 for (int i = 0; i < 32; ++i) {
928 for (int j = 0; j < 32; ++j) {
929 string s = string(i, 'X') + "abca" + string(i, 'X');
930 string delims = string(j, 'Y') + "a" + string(j, 'Y');
931 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
936 const size_t kPageSize = 4096;
937 // Updates contents so that any read accesses past the last byte will
938 // cause a SIGSEGV. It accomplishes this by changing access to the page that
939 // begins immediately after the end of the contents (as allocators and mmap()
940 // all operate on page boundaries, this is a reasonable assumption).
941 // This function will also initialize buf, which caller must free().
942 void createProtectedBuf(StringPiece& contents, char** buf) {
943 ASSERT_LE(contents.size(), kPageSize);
944 const size_t kSuccess = 0;
945 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
948 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
949 size_t newBegin = kPageSize - contents.size();
950 memcpy(*buf + newBegin, contents.data(), contents.size());
951 contents.reset(*buf + newBegin, contents.size());
954 void freeProtectedBuf(char* buf) {
955 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
959 TYPED_TEST(NeedleFinderTest, NoSegFault) {
960 const string base = string(32, 'a') + string("b");
961 const string delims = string(32, 'c') + string("b");
962 for (int i = 0; i <= 32; i++) {
963 for (int j = 0; j <= 33; j++) {
964 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
965 StringPiece s1(base);
967 ASSERT_TRUE(!s1.empty());
971 StringPiece s2(delims);
975 createProtectedBuf(s1, &buf1);
976 createProtectedBuf(s2, &buf2);
977 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
978 // string(s1.data(), s1.size()).c_str(), s1.size(),
979 // string(s2.data(), s2.size()).c_str(), s2.size());
980 auto r1 = this->find_first_byte_of(s1, s2);
981 auto f1 = std::find_first_of(s1.begin(), s1.end(),
982 s2.begin(), s2.end());
983 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
985 auto r2 = this->find_first_byte_of(s2, s1);
986 auto f2 = std::find_first_of(s2.begin(), s2.end(),
987 s1.begin(), s1.end());
988 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
990 freeProtectedBuf(buf1);
991 freeProtectedBuf(buf2);
997 TEST(NonConstTest, StringPiece) {
998 std::string hello("hello");
999 MutableStringPiece sp(&hello.front(), hello.size());
1001 EXPECT_EQ("xello", hello);
1004 EXPECT_EQ("xello", s);
1008 MutableByteRange r2(sp);
1013 void testRangeFunc(C&& x, size_t n) {
1015 // type, conversion checks
1016 Range<int*> r1 = range(std::forward<C>(x));
1017 Range<const int*> r2 = range(std::forward<C>(x));
1018 Range<const int*> r3 = range(cx);
1019 Range<const int*> r5 = range(std::move(cx));
1020 EXPECT_EQ(r1.begin(), &x[0]);
1021 EXPECT_EQ(r1.end(), &x[n]);
1022 EXPECT_EQ(n, r1.size());
1023 EXPECT_EQ(n, r2.size());
1024 EXPECT_EQ(n, r3.size());
1025 EXPECT_EQ(n, r5.size());
1028 TEST(RangeFunc, Vector) {
1030 testRangeFunc(x, 0);
1032 testRangeFunc(x, 1);
1033 testRangeFunc(std::vector<int>{1, 2}, 2);
1036 TEST(RangeFunc, Array) {
1037 std::array<int, 3> x;
1038 testRangeFunc(x, 3);
1041 TEST(RangeFunc, CArray) {
1042 int x[] {1, 2, 3, 4};
1043 testRangeFunc(x, 4);