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());
443 auto x = p.split_step(' ');
444 EXPECT_EQ(std::next(s, 5), p.begin());
445 EXPECT_EQ(e, p.end());
446 EXPECT_EQ("this", x);
448 x = p.split_step(' ');
449 EXPECT_EQ(std::next(s, 8), p.begin());
450 EXPECT_EQ(e, p.end());
453 x = p.split_step('u');
454 EXPECT_EQ(std::next(s, 10), p.begin());
455 EXPECT_EQ(e, p.end());
458 x = p.split_step(' ');
459 EXPECT_EQ(std::next(s, 13), p.begin());
460 EXPECT_EQ(e, p.end());
463 x = p.split_step(' ');
464 EXPECT_EQ(std::next(s, 14), p.begin());
465 EXPECT_EQ(e, p.end());
468 x = p.split_step(' ');
469 EXPECT_EQ(std::next(s, 16), p.begin());
470 EXPECT_EQ(e, p.end());
473 x = p.split_step(' ');
474 EXPECT_EQ(std::next(s, 21), p.begin());
475 EXPECT_EQ(e, p.end());
476 EXPECT_EQ("test", x);
478 x = p.split_step(' ');
479 EXPECT_EQ(e, p.begin());
480 EXPECT_EQ(e, p.end());
481 EXPECT_EQ("string", x);
483 x = p.split_step(' ');
484 EXPECT_EQ(e, p.begin());
485 EXPECT_EQ(e, p.end());
489 TEST(StringPiece, split_step_range_delimiter) {
491 // 0123456789012345678901234567890123
492 auto const s = "this is just a test string";
493 auto const e = std::next(s, std::strlen(s));
496 folly::StringPiece p(s);
497 EXPECT_EQ(s, p.begin());
498 EXPECT_EQ(e, p.end());
500 auto x = p.split_step(" ");
501 EXPECT_EQ(std::next(s, 6), p.begin());
502 EXPECT_EQ(e, p.end());
503 EXPECT_EQ("this", x);
505 x = p.split_step(" ");
506 EXPECT_EQ(std::next(s, 10), p.begin());
507 EXPECT_EQ(e, p.end());
510 x = p.split_step("u");
511 EXPECT_EQ(std::next(s, 12), p.begin());
512 EXPECT_EQ(e, p.end());
515 x = p.split_step(" ");
516 EXPECT_EQ(std::next(s, 16), p.begin());
517 EXPECT_EQ(e, p.end());
520 x = p.split_step(" ");
521 EXPECT_EQ(std::next(s, 18), p.begin());
522 EXPECT_EQ(e, p.end());
525 x = p.split_step(" ");
526 EXPECT_EQ(std::next(s, 21), p.begin());
527 EXPECT_EQ(e, p.end());
530 x = p.split_step(" ");
531 EXPECT_EQ(std::next(s, 28), p.begin());
532 EXPECT_EQ(e, p.end());
533 EXPECT_EQ(" test", x);
535 x = p.split_step(" ");
536 EXPECT_EQ(e, p.begin());
537 EXPECT_EQ(e, p.end());
538 EXPECT_EQ("string", x);
540 x = p.split_step(" ");
541 EXPECT_EQ(e, p.begin());
542 EXPECT_EQ(e, p.end());
545 x = p.split_step(" ");
546 EXPECT_EQ(e, p.begin());
547 EXPECT_EQ(e, p.end());
551 void split_step_with_process_noop(folly::StringPiece) {}
553 TEST(StringPiece, split_step_with_process_char_delimiter) {
555 // 012345678901234567890123456
556 auto const s = "this is just a test string";
557 auto const e = std::next(s, std::strlen(s));
560 folly::StringPiece p(s);
561 EXPECT_EQ(s, p.begin());
562 EXPECT_EQ(e, p.end());
564 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
565 EXPECT_EQ(std::next(s, 5), p.begin());
566 EXPECT_EQ(e, p.end());
567 EXPECT_EQ("this", x);
571 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
572 EXPECT_EQ(std::next(s, 8), p.begin());
573 EXPECT_EQ(e, p.end());
578 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
579 EXPECT_EQ(std::next(s, 10), p.begin());
580 EXPECT_EQ(e, p.end());
585 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
586 EXPECT_EQ(std::next(s, 13), p.begin());
587 EXPECT_EQ(e, p.end());
592 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
593 EXPECT_EQ(std::next(s, 14), p.begin());
594 EXPECT_EQ(e, p.end());
599 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
600 EXPECT_EQ(std::next(s, 16), p.begin());
601 EXPECT_EQ(e, p.end());
606 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
607 EXPECT_EQ(std::next(s, 21), p.begin());
608 EXPECT_EQ(e, p.end());
609 EXPECT_EQ("test", x);
613 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
614 EXPECT_EQ(e, p.begin());
615 EXPECT_EQ(e, p.end());
616 EXPECT_EQ("string", x);
620 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
621 EXPECT_EQ(e, p.begin());
622 EXPECT_EQ(e, p.end());
627 EXPECT_TRUE((std::is_same<
629 decltype(p.split_step(' ', split_step_with_process_noop))
632 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
635 TEST(StringPiece, split_step_with_process_range_delimiter) {
637 // 0123456789012345678901234567890123
638 auto const s = "this is just a test string";
639 auto const e = std::next(s, std::strlen(s));
642 folly::StringPiece p(s);
643 EXPECT_EQ(s, p.begin());
644 EXPECT_EQ(e, p.end());
646 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
647 EXPECT_EQ(std::next(s, 6), p.begin());
648 EXPECT_EQ(e, p.end());
649 EXPECT_EQ("this", x);
653 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
654 EXPECT_EQ(std::next(s, 10), p.begin());
655 EXPECT_EQ(e, p.end());
660 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
661 EXPECT_EQ(std::next(s, 12), p.begin());
662 EXPECT_EQ(e, p.end());
667 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
668 EXPECT_EQ(std::next(s, 16), p.begin());
669 EXPECT_EQ(e, p.end());
674 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
675 EXPECT_EQ(std::next(s, 18), p.begin());
676 EXPECT_EQ(e, p.end());
681 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
682 EXPECT_EQ(std::next(s, 21), p.begin());
683 EXPECT_EQ(e, p.end());
688 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
689 EXPECT_EQ(std::next(s, 28), p.begin());
690 EXPECT_EQ(e, p.end());
691 EXPECT_EQ(" test", x);
695 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
696 EXPECT_EQ(e, p.begin());
697 EXPECT_EQ(e, p.end());
698 EXPECT_EQ("string", x);
702 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
703 EXPECT_EQ(e, p.begin());
704 EXPECT_EQ(e, p.end());
709 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
710 EXPECT_EQ(e, p.begin());
711 EXPECT_EQ(e, p.end());
716 EXPECT_TRUE((std::is_same<
718 decltype(p.split_step(' ', split_step_with_process_noop))
721 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
724 TEST(qfind, UInt32_Ranges) {
725 vector<uint32_t> a({1, 2, 3, 260, 5});
726 vector<uint32_t> b({2, 3, 4});
728 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
729 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
731 EXPECT_EQ(qfind(a_range, b_range), string::npos);
734 EXPECT_EQ(qfind(a_range, b_range), 1);
737 template <typename NeedleFinder>
738 class NeedleFinderTest : public ::testing::Test {
740 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
741 return NeedleFinder::find_first_byte_of(haystack, needles);
745 struct SseNeedleFinder {
746 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
747 // This will only use the SSE version if it is supported on this CPU
748 // (selected using ifunc).
749 return detail::qfind_first_byte_of(haystack, needles);
753 struct NoSseNeedleFinder {
754 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
755 return detail::qfind_first_byte_of_nosse(haystack, needles);
759 struct MemchrNeedleFinder {
760 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
761 return detail::qfind_first_byte_of_memchr(haystack, needles);
765 struct ByteSetNeedleFinder {
766 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
767 return detail::qfind_first_byte_of_byteset(haystack, needles);
771 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
772 ByteSetNeedleFinder> NeedleFinders;
773 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
775 TYPED_TEST(NeedleFinderTest, Null) {
776 { // null characters in the string
777 string s(10, char(0));
779 string delims("abc");
780 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
782 { // null characters in delim
784 string delims(10, char(0));
787 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
789 { // range not terminated by null character
790 string buf = "abcdefghijklmnopqrstuvwxyz";
791 StringPiece s(buf.data() + 5, 3);
792 StringPiece delims("z");
793 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
797 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
798 string delims(1000, 'b');
799 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
800 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
803 TYPED_TEST(NeedleFinderTest, Empty) {
806 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
807 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
808 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
811 TYPED_TEST(NeedleFinderTest, Unaligned) {
812 // works correctly even if input buffers are not 16-byte aligned
813 string s = "0123456789ABCDEFGH";
814 for (int i = 0; i < s.size(); ++i) {
815 StringPiece a(s.c_str() + i);
816 for (int j = 0; j < s.size(); ++j) {
817 StringPiece b(s.c_str() + j);
818 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
823 // for some algorithms (specifically those that create a set of needles),
824 // we check for the edge-case of _all_ possible needles being sought.
825 TYPED_TEST(NeedleFinderTest, Needles256) {
827 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
828 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
829 // make the size ~big to avoid any edge-case branches for tiny haystacks
830 const int haystackSize = 50;
831 for (int i = minValue; i <= maxValue; i++) { // <=
832 needles.push_back(i);
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));
839 needles.append("these are redundant characters");
840 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
841 for (int i = minValue; i <= maxValue; i++) {
842 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
846 TYPED_TEST(NeedleFinderTest, Base) {
847 for (int i = 0; i < 32; ++i) {
848 for (int j = 0; j < 32; ++j) {
849 string s = string(i, 'X') + "abca" + string(i, 'X');
850 string delims = string(j, 'Y') + "a" + string(j, 'Y');
851 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
856 const size_t kPageSize = 4096;
857 // Updates contents so that any read accesses past the last byte will
858 // cause a SIGSEGV. It accomplishes this by changing access to the page that
859 // begins immediately after the end of the contents (as allocators and mmap()
860 // all operate on page boundaries, this is a reasonable assumption).
861 // This function will also initialize buf, which caller must free().
862 void createProtectedBuf(StringPiece& contents, char** buf) {
863 ASSERT_LE(contents.size(), kPageSize);
864 const size_t kSuccess = 0;
865 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
868 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
869 size_t newBegin = kPageSize - contents.size();
870 memcpy(*buf + newBegin, contents.data(), contents.size());
871 contents.reset(*buf + newBegin, contents.size());
874 void freeProtectedBuf(char* buf) {
875 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
879 TYPED_TEST(NeedleFinderTest, NoSegFault) {
880 const string base = string(32, 'a') + string("b");
881 const string delims = string(32, 'c') + string("b");
882 for (int i = 0; i <= 32; i++) {
883 for (int j = 0; j <= 33; j++) {
884 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
885 StringPiece s1(base);
887 ASSERT_TRUE(!s1.empty());
891 StringPiece s2(delims);
895 createProtectedBuf(s1, &buf1);
896 createProtectedBuf(s2, &buf2);
897 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
898 // string(s1.data(), s1.size()).c_str(), s1.size(),
899 // string(s2.data(), s2.size()).c_str(), s2.size());
900 auto r1 = this->find_first_byte_of(s1, s2);
901 auto f1 = std::find_first_of(s1.begin(), s1.end(),
902 s2.begin(), s2.end());
903 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
905 auto r2 = this->find_first_byte_of(s2, s1);
906 auto f2 = std::find_first_of(s2.begin(), s2.end(),
907 s1.begin(), s1.end());
908 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
910 freeProtectedBuf(buf1);
911 freeProtectedBuf(buf2);
917 TEST(NonConstTest, StringPiece) {
918 std::string hello("hello");
919 MutableStringPiece sp(&hello.front(), hello.size());
921 EXPECT_EQ("xello", hello);
924 EXPECT_EQ("xello", s);
928 MutableByteRange r2(sp);
933 void testRangeFunc(C&& x, size_t n) {
935 // type, conversion checks
936 Range<int*> r1 = range(std::forward<C>(x));
937 Range<const int*> r2 = range(std::forward<C>(x));
938 Range<const int*> r3 = range(cx);
939 Range<const int*> r5 = range(std::move(cx));
940 EXPECT_EQ(r1.begin(), &x[0]);
941 EXPECT_EQ(r1.end(), &x[n]);
942 EXPECT_EQ(n, r1.size());
943 EXPECT_EQ(n, r2.size());
944 EXPECT_EQ(n, r3.size());
945 EXPECT_EQ(n, r5.size());
948 TEST(RangeFunc, Vector) {
953 testRangeFunc(std::vector<int>{1, 2}, 2);
956 TEST(RangeFunc, Array) {
957 std::array<int, 3> x;
961 TEST(RangeFunc, CArray) {
962 int x[] {1, 2, 3, 4};