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>
29 #include <type_traits>
31 #include <boost/range/concepts.hpp>
32 #include <gtest/gtest.h>
34 namespace folly { namespace detail {
36 // declaration of functions in Range.cpp
37 size_t qfind_first_byte_of_memchr(const StringPiece haystack,
38 const StringPiece needles);
40 size_t qfind_first_byte_of_byteset(const StringPiece haystack,
41 const StringPiece needles);
45 using namespace folly;
48 static_assert(std::is_literal_type<StringPiece>::value, "");
50 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
52 TEST(StringPiece, All) {
53 const char* foo = "foo";
54 const char* foo2 = "foo";
58 // we expect the compiler to optimize things so that there's only one copy
59 // of the string literal "foo", even though we've got it in multiple places
60 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
61 // comparison rather than lexical
63 // the string object creates copies though, so the c_str of these should be
65 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
67 // test the basic StringPiece functionality
69 EXPECT_EQ(s.size(), 3);
71 EXPECT_EQ(s.start(), foo); // ptr comparison
72 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
73 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
75 EXPECT_EQ(s.toString(), foo); // lexical comparison
76 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
77 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
79 EXPECT_EQ(s, foo); // lexical comparison
80 EXPECT_EQ(s, fooStr); // lexical comparison
81 EXPECT_EQ(s, foo2Str); // lexical comparison
84 // check using StringPiece to reference substrings
85 const char* foobarbaz = "foobarbaz";
87 // the full "foobarbaz"
88 s.reset(foobarbaz, strlen(foobarbaz));
89 EXPECT_EQ(s.size(), 9);
90 EXPECT_EQ(s.start(), foobarbaz);
91 EXPECT_EQ(s, "foobarbaz");
94 s.assign(foobarbaz, foobarbaz + 3);
95 EXPECT_EQ(s.size(), 3);
96 EXPECT_EQ(s.start(), foobarbaz);
100 s.reset(foobarbaz, strlen(foobarbaz));
101 EXPECT_EQ(s.find("bar"), 3);
102 EXPECT_EQ(s.find("ba", 3), 3);
103 EXPECT_EQ(s.find("ba", 4), 6);
104 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
105 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
106 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
107 // starting pos that is obviously past the end -- This works for std::string
108 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
109 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
110 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
112 EXPECT_EQ(s.find(""), std::string().find(""));
113 EXPECT_EQ(s.find(""), 0);
116 EXPECT_EQ(s.find('b'), 3);
117 EXPECT_EQ(s.find('b', 3), 3);
118 EXPECT_EQ(s.find('b', 4), 6);
119 EXPECT_EQ(s.find('o', 2), 2);
120 EXPECT_EQ(s.find('y'), StringPiece::npos);
121 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
122 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
123 EXPECT_TRUE(s.contains('z'));
124 // starting pos that is obviously past the end -- This works for std::string
125 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
126 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
127 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
129 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
130 EXPECT_EQ(s.find('\0'), StringPiece::npos);
131 EXPECT_FALSE(s.contains('\0'));
133 // single char rfinds
134 EXPECT_EQ(s.rfind('b'), 6);
135 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
136 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
137 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
138 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
140 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
141 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
144 s.reset(foobarbaz, strlen(foobarbaz));
145 EXPECT_EQ(s.find_first_of("bar"), 3);
146 EXPECT_EQ(s.find_first_of("ba", 3), 3);
147 EXPECT_EQ(s.find_first_of("ba", 4), 4);
148 EXPECT_TRUE(s.contains("bar"));
149 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
150 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
151 EXPECT_FALSE(s.contains("xyxy"));
152 // starting position too far
153 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
154 // starting pos that is obviously past the end -- This works for std::string
155 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
156 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
157 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
158 // empty needle. Note that this returns npos, while find() returns 0!
159 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
160 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
162 // single char find_first_ofs
163 EXPECT_EQ(s.find_first_of('b'), 3);
164 EXPECT_EQ(s.find_first_of('b', 3), 3);
165 EXPECT_EQ(s.find_first_of('b', 4), 6);
166 EXPECT_EQ(s.find_first_of('o', 2), 2);
167 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
168 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
169 // starting position too far
170 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
171 // starting pos that is obviously past the end -- This works for std::string
172 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
173 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
174 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
176 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
177 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
180 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
181 EXPECT_EQ(s.size(), 6);
182 EXPECT_EQ(s.start(), foobarbaz + 3);
183 EXPECT_EQ(s, "barbaz");
186 s.reset(foobarbaz + 3, 3);
187 EXPECT_EQ(s.size(), 3);
192 EXPECT_EQ(s.toString(), "");
194 // test an empty StringPiece
196 EXPECT_EQ(s2.size(), 0);
198 // Test comparison operators
232 void expectLT(const T& a, const T& b) {
235 EXPECT_FALSE(a == b);
236 EXPECT_FALSE(a >= b);
240 EXPECT_FALSE(b <= a);
246 void expectEQ(const T& a, const T& b) {
254 TEST(StringPiece, EightBitComparisons) {
255 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
256 constexpr size_t count = sizeof(values) / sizeof(values[0]);
257 for (size_t i = 0; i < count; ++i) {
258 std::string a(1, values[i]);
259 // Defeat copy-on-write
260 std::string aCopy(a.data(), a.size());
262 expectEQ(StringPiece(a), StringPiece(aCopy));
264 for (size_t j = i + 1; j < count; ++j) {
265 std::string b(1, values[j]);
267 expectLT(StringPiece(a), StringPiece(b));
272 TEST(StringPiece, ToByteRange) {
273 StringPiece a("hello");
275 EXPECT_EQ(static_cast<const void*>(a.begin()),
276 static_cast<const void*>(b.begin()));
277 EXPECT_EQ(static_cast<const void*>(a.end()),
278 static_cast<const void*>(b.end()));
280 // and convert back again
282 EXPECT_EQ(a.begin(), c.begin());
283 EXPECT_EQ(a.end(), c.end());
286 TEST(StringPiece, InvalidRange) {
287 StringPiece a("hello");
288 EXPECT_EQ(a, a.subpiece(0, 10));
289 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
290 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
291 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
292 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
293 EXPECT_THROW(a.subpiece(6), std::out_of_range);
295 std::string b("hello");
296 EXPECT_EQ(a, StringPiece(b, 0, 10));
297 EXPECT_EQ("ello", a.subpiece(1));
298 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
299 EXPECT_EQ("ell", a.subpiece(1, 3));
300 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
301 EXPECT_THROW(a.subpiece(6), std::out_of_range);
304 #if FOLLY_HAVE_CONSTEXPR_STRLEN
305 constexpr char helloArray[] = "hello";
307 TEST(StringPiece, Constexpr) {
308 constexpr StringPiece hello1("hello");
309 EXPECT_EQ("hello", hello1);
311 constexpr StringPiece hello2(helloArray);
312 EXPECT_EQ("hello", hello2);
316 TEST(StringPiece, Prefix) {
317 StringPiece a("hello");
318 EXPECT_TRUE(a.startsWith(""));
319 EXPECT_TRUE(a.startsWith("h"));
320 EXPECT_TRUE(a.startsWith('h'));
321 EXPECT_TRUE(a.startsWith("hello"));
322 EXPECT_FALSE(a.startsWith("hellox"));
323 EXPECT_FALSE(a.startsWith('x'));
324 EXPECT_FALSE(a.startsWith("x"));
328 EXPECT_TRUE(b.removePrefix(""));
329 EXPECT_EQ("hello", b);
333 EXPECT_TRUE(b.removePrefix("h"));
334 EXPECT_EQ("ello", b);
338 EXPECT_TRUE(b.removePrefix('h'));
339 EXPECT_EQ("ello", b);
343 EXPECT_TRUE(b.removePrefix("hello"));
348 EXPECT_FALSE(b.removePrefix("hellox"));
349 EXPECT_EQ("hello", b);
353 EXPECT_FALSE(b.removePrefix("x"));
354 EXPECT_EQ("hello", b);
358 EXPECT_FALSE(b.removePrefix('x'));
359 EXPECT_EQ("hello", b);
363 TEST(StringPiece, Suffix) {
364 StringPiece a("hello");
365 EXPECT_TRUE(a.endsWith(""));
366 EXPECT_TRUE(a.endsWith("o"));
367 EXPECT_TRUE(a.endsWith('o'));
368 EXPECT_TRUE(a.endsWith("hello"));
369 EXPECT_FALSE(a.endsWith("xhello"));
370 EXPECT_FALSE(a.endsWith("x"));
371 EXPECT_FALSE(a.endsWith('x'));
375 EXPECT_TRUE(b.removeSuffix(""));
376 EXPECT_EQ("hello", b);
380 EXPECT_TRUE(b.removeSuffix("o"));
381 EXPECT_EQ("hell", b);
385 EXPECT_TRUE(b.removeSuffix('o'));
386 EXPECT_EQ("hell", b);
390 EXPECT_TRUE(b.removeSuffix("hello"));
395 EXPECT_FALSE(b.removeSuffix("xhello"));
396 EXPECT_EQ("hello", b);
400 EXPECT_FALSE(b.removeSuffix("x"));
401 EXPECT_EQ("hello", b);
405 EXPECT_FALSE(b.removeSuffix('x'));
406 EXPECT_EQ("hello", b);
410 TEST(StringPiece, PrefixEmpty) {
412 EXPECT_TRUE(a.startsWith(""));
413 EXPECT_FALSE(a.startsWith("a"));
414 EXPECT_FALSE(a.startsWith('a'));
415 EXPECT_TRUE(a.removePrefix(""));
417 EXPECT_FALSE(a.removePrefix("a"));
419 EXPECT_FALSE(a.removePrefix('a'));
423 TEST(StringPiece, SuffixEmpty) {
425 EXPECT_TRUE(a.endsWith(""));
426 EXPECT_FALSE(a.endsWith("a"));
427 EXPECT_FALSE(a.endsWith('a'));
428 EXPECT_TRUE(a.removeSuffix(""));
430 EXPECT_FALSE(a.removeSuffix("a"));
432 EXPECT_FALSE(a.removeSuffix('a'));
436 TEST(StringPiece, split_step_char_delimiter) {
438 // 012345678901234567890123456
439 auto const s = "this is just a test string";
440 auto const e = std::next(s, std::strlen(s));
443 folly::StringPiece p(s);
444 EXPECT_EQ(s, p.begin());
445 EXPECT_EQ(e, p.end());
448 auto x = p.split_step(' ');
449 EXPECT_EQ(std::next(s, 5), p.begin());
450 EXPECT_EQ(e, p.end());
451 EXPECT_EQ("this", x);
453 x = p.split_step(' ');
454 EXPECT_EQ(std::next(s, 8), p.begin());
455 EXPECT_EQ(e, p.end());
458 x = p.split_step('u');
459 EXPECT_EQ(std::next(s, 10), p.begin());
460 EXPECT_EQ(e, p.end());
463 x = p.split_step(' ');
464 EXPECT_EQ(std::next(s, 13), p.begin());
465 EXPECT_EQ(e, p.end());
468 x = p.split_step(' ');
469 EXPECT_EQ(std::next(s, 14), p.begin());
470 EXPECT_EQ(e, p.end());
473 x = p.split_step(' ');
474 EXPECT_EQ(std::next(s, 16), p.begin());
475 EXPECT_EQ(e, p.end());
478 x = p.split_step(' ');
479 EXPECT_EQ(std::next(s, 21), p.begin());
480 EXPECT_EQ(e, p.end());
481 EXPECT_EQ("test", x);
483 x = p.split_step(' ');
484 EXPECT_EQ(e, p.begin());
485 EXPECT_EQ(e, p.end());
486 EXPECT_EQ("string", x);
488 x = p.split_step(' ');
489 EXPECT_EQ(e, p.begin());
490 EXPECT_EQ(e, p.end());
494 TEST(StringPiece, split_step_range_delimiter) {
496 // 0123456789012345678901234567890123
497 auto const s = "this is just a test string";
498 auto const e = std::next(s, std::strlen(s));
501 folly::StringPiece p(s);
502 EXPECT_EQ(s, p.begin());
503 EXPECT_EQ(e, p.end());
506 auto x = p.split_step(" ");
507 EXPECT_EQ(std::next(s, 6), p.begin());
508 EXPECT_EQ(e, p.end());
509 EXPECT_EQ("this", x);
511 x = p.split_step(" ");
512 EXPECT_EQ(std::next(s, 10), p.begin());
513 EXPECT_EQ(e, p.end());
516 x = p.split_step("u");
517 EXPECT_EQ(std::next(s, 12), p.begin());
518 EXPECT_EQ(e, p.end());
521 x = p.split_step(" ");
522 EXPECT_EQ(std::next(s, 16), p.begin());
523 EXPECT_EQ(e, p.end());
526 x = p.split_step(" ");
527 EXPECT_EQ(std::next(s, 18), p.begin());
528 EXPECT_EQ(e, p.end());
531 x = p.split_step(" ");
532 EXPECT_EQ(std::next(s, 21), p.begin());
533 EXPECT_EQ(e, p.end());
536 x = p.split_step(" ");
537 EXPECT_EQ(std::next(s, 28), p.begin());
538 EXPECT_EQ(e, p.end());
539 EXPECT_EQ(" test", x);
541 x = p.split_step(" ");
542 EXPECT_EQ(e, p.begin());
543 EXPECT_EQ(e, p.end());
544 EXPECT_EQ("string", x);
546 x = p.split_step(" ");
547 EXPECT_EQ(e, p.begin());
548 EXPECT_EQ(e, p.end());
551 x = p.split_step(" ");
552 EXPECT_EQ(e, p.begin());
553 EXPECT_EQ(e, p.end());
557 void split_step_with_process_noop(folly::StringPiece) {}
559 TEST(StringPiece, split_step_with_process_char_delimiter) {
561 // 012345678901234567890123456
562 auto const s = "this is just a test string";
563 auto const e = std::next(s, std::strlen(s));
566 folly::StringPiece p(s);
567 EXPECT_EQ(s, p.begin());
568 EXPECT_EQ(e, p.end());
571 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
572 EXPECT_EQ(std::next(s, 5), p.begin());
573 EXPECT_EQ(e, p.end());
574 EXPECT_EQ("this", x);
578 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
579 EXPECT_EQ(std::next(s, 8), p.begin());
580 EXPECT_EQ(e, p.end());
585 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
586 EXPECT_EQ(std::next(s, 10), p.begin());
587 EXPECT_EQ(e, p.end());
592 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
593 EXPECT_EQ(std::next(s, 13), p.begin());
594 EXPECT_EQ(e, p.end());
599 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
600 EXPECT_EQ(std::next(s, 14), p.begin());
601 EXPECT_EQ(e, p.end());
606 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
607 EXPECT_EQ(std::next(s, 16), p.begin());
608 EXPECT_EQ(e, p.end());
613 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
614 EXPECT_EQ(std::next(s, 21), p.begin());
615 EXPECT_EQ(e, p.end());
616 EXPECT_EQ("test", x);
620 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
621 EXPECT_EQ(e, p.begin());
622 EXPECT_EQ(e, p.end());
623 EXPECT_EQ("string", x);
627 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
628 EXPECT_EQ(e, p.begin());
629 EXPECT_EQ(e, p.end());
634 EXPECT_TRUE((std::is_same<
636 decltype(p.split_step(' ', split_step_with_process_noop))
639 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
642 TEST(StringPiece, split_step_with_process_range_delimiter) {
644 // 0123456789012345678901234567890123
645 auto const s = "this is just a test string";
646 auto const e = std::next(s, std::strlen(s));
649 folly::StringPiece p(s);
650 EXPECT_EQ(s, p.begin());
651 EXPECT_EQ(e, p.end());
654 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
655 EXPECT_EQ(std::next(s, 6), p.begin());
656 EXPECT_EQ(e, p.end());
657 EXPECT_EQ("this", x);
661 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
662 EXPECT_EQ(std::next(s, 10), p.begin());
663 EXPECT_EQ(e, p.end());
668 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
669 EXPECT_EQ(std::next(s, 12), p.begin());
670 EXPECT_EQ(e, p.end());
675 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
676 EXPECT_EQ(std::next(s, 16), p.begin());
677 EXPECT_EQ(e, p.end());
682 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
683 EXPECT_EQ(std::next(s, 18), p.begin());
684 EXPECT_EQ(e, p.end());
689 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
690 EXPECT_EQ(std::next(s, 21), p.begin());
691 EXPECT_EQ(e, p.end());
696 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
697 EXPECT_EQ(std::next(s, 28), p.begin());
698 EXPECT_EQ(e, p.end());
699 EXPECT_EQ(" test", x);
703 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
704 EXPECT_EQ(e, p.begin());
705 EXPECT_EQ(e, p.end());
706 EXPECT_EQ("string", x);
710 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
711 EXPECT_EQ(e, p.begin());
712 EXPECT_EQ(e, p.end());
717 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
718 EXPECT_EQ(e, p.begin());
719 EXPECT_EQ(e, p.end());
724 EXPECT_TRUE((std::is_same<
726 decltype(p.split_step(' ', split_step_with_process_noop))
729 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
732 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
734 // 012345678901234567890123456
735 auto const s = "this is just a test string";
736 auto const e = std::next(s, std::strlen(s));
737 auto const delimiter = ' ';
740 folly::StringPiece p(s);
741 EXPECT_EQ(s, p.begin());
742 EXPECT_EQ(e, p.end());
745 auto const functor = [](
746 folly::StringPiece s,
747 folly::StringPiece expected
749 EXPECT_EQ(expected, s);
753 auto const checker = [&](folly::StringPiece expected) {
754 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
767 EXPECT_TRUE(p.empty());
770 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
772 // 0123456789012345678901234567890123
773 auto const s = "this is just a test string";
774 auto const e = std::next(s, std::strlen(s));
775 auto const delimiter = " ";
778 folly::StringPiece p(s);
779 EXPECT_EQ(s, p.begin());
780 EXPECT_EQ(e, p.end());
783 auto const functor = [](
784 folly::StringPiece s,
785 folly::StringPiece expected
787 EXPECT_EQ(expected, s);
791 auto const checker = [&](folly::StringPiece expected) {
792 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
805 EXPECT_TRUE(p.empty());
808 TEST(StringPiece, NoInvalidImplicitConversions) {
810 bool operator()(folly::Range<int*>) { return false; }
811 bool operator()(folly::StringPiece) { return true; }
814 std::string s = "hello";
815 EXPECT_TRUE(IsString()(s));
818 TEST(qfind, UInt32_Ranges) {
819 vector<uint32_t> a({1, 2, 3, 260, 5});
820 vector<uint32_t> b({2, 3, 4});
822 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
823 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
825 EXPECT_EQ(qfind(a_range, b_range), string::npos);
828 EXPECT_EQ(qfind(a_range, b_range), 1);
831 template <typename NeedleFinder>
832 class NeedleFinderTest : public ::testing::Test {
834 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
835 return NeedleFinder::find_first_byte_of(haystack, needles);
839 struct SseNeedleFinder {
840 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
841 // This will only use the SSE version if it is supported on this CPU
842 // (selected using ifunc).
843 return detail::qfind_first_byte_of(haystack, needles);
847 struct NoSseNeedleFinder {
848 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
849 return detail::qfind_first_byte_of_nosse(haystack, needles);
853 struct MemchrNeedleFinder {
854 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
855 return detail::qfind_first_byte_of_memchr(haystack, needles);
859 struct ByteSetNeedleFinder {
860 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
861 return detail::qfind_first_byte_of_byteset(haystack, needles);
865 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
866 ByteSetNeedleFinder> NeedleFinders;
867 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
869 TYPED_TEST(NeedleFinderTest, Null) {
870 { // null characters in the string
871 string s(10, char(0));
873 string delims("abc");
874 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
876 { // null characters in delim
878 string delims(10, char(0));
881 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
883 { // range not terminated by null character
884 string buf = "abcdefghijklmnopqrstuvwxyz";
885 StringPiece s(buf.data() + 5, 3);
886 StringPiece delims("z");
887 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
891 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
892 string delims(1000, 'b');
893 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
894 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
897 TYPED_TEST(NeedleFinderTest, Empty) {
900 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
901 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
902 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
905 TYPED_TEST(NeedleFinderTest, Unaligned) {
906 // works correctly even if input buffers are not 16-byte aligned
907 string s = "0123456789ABCDEFGH";
908 for (size_t i = 0; i < s.size(); ++i) {
909 StringPiece a(s.c_str() + i);
910 for (int j = 0; j < s.size(); ++j) {
911 StringPiece b(s.c_str() + j);
912 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
917 // for some algorithms (specifically those that create a set of needles),
918 // we check for the edge-case of _all_ possible needles being sought.
919 TYPED_TEST(NeedleFinderTest, Needles256) {
921 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
922 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
923 // make the size ~big to avoid any edge-case branches for tiny haystacks
924 const int haystackSize = 50;
925 for (size_t i = minValue; i <= maxValue; i++) { // <=
926 needles.push_back(i);
928 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
929 for (size_t i = minValue; i <= maxValue; i++) {
930 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
933 needles.append("these are redundant characters");
934 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
935 for (size_t i = minValue; i <= maxValue; i++) {
936 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
940 TYPED_TEST(NeedleFinderTest, Base) {
941 for (size_t i = 0; i < 32; ++i) {
942 for (int j = 0; j < 32; ++j) {
943 string s = string(i, 'X') + "abca" + string(i, 'X');
944 string delims = string(j, 'Y') + "a" + string(j, 'Y');
945 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
950 const size_t kPageSize = 4096;
951 // Updates contents so that any read accesses past the last byte will
952 // cause a SIGSEGV. It accomplishes this by changing access to the page that
953 // begins immediately after the end of the contents (as allocators and mmap()
954 // all operate on page boundaries, this is a reasonable assumption).
955 // This function will also initialize buf, which caller must free().
956 void createProtectedBuf(StringPiece& contents, char** buf) {
957 ASSERT_LE(contents.size(), kPageSize);
958 const size_t kSuccess = 0;
959 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
962 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
963 size_t newBegin = kPageSize - contents.size();
964 memcpy(*buf + newBegin, contents.data(), contents.size());
965 contents.reset(*buf + newBegin, contents.size());
968 void freeProtectedBuf(char* buf) {
969 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
973 TYPED_TEST(NeedleFinderTest, NoSegFault) {
974 const string base = string(32, 'a') + string("b");
975 const string delims = string(32, 'c') + string("b");
976 for (int i = 0; i <= 32; i++) {
977 for (int j = 0; j <= 33; j++) {
978 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
979 StringPiece s1(base);
981 ASSERT_TRUE(!s1.empty());
985 StringPiece s2(delims);
989 createProtectedBuf(s1, &buf1);
990 createProtectedBuf(s2, &buf2);
991 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
992 // string(s1.data(), s1.size()).c_str(), s1.size(),
993 // string(s2.data(), s2.size()).c_str(), s2.size());
994 auto r1 = this->find_first_byte_of(s1, s2);
995 auto f1 = std::find_first_of(s1.begin(), s1.end(),
996 s2.begin(), s2.end());
997 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
999 auto r2 = this->find_first_byte_of(s2, s1);
1000 auto f2 = std::find_first_of(s2.begin(), s2.end(),
1001 s1.begin(), s1.end());
1002 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1004 freeProtectedBuf(buf1);
1005 freeProtectedBuf(buf2);
1011 TEST(NonConstTest, StringPiece) {
1012 std::string hello("hello");
1013 MutableStringPiece sp(&hello.front(), hello.size());
1015 EXPECT_EQ("xello", hello);
1018 EXPECT_EQ("xello", s);
1022 MutableByteRange r2(sp);
1027 void testRangeFunc(C&& x, size_t n) {
1029 // type, conversion checks
1030 Range<int*> r1 = range(std::forward<C>(x));
1031 Range<const int*> r2 = range(std::forward<C>(x));
1032 Range<const int*> r3 = range(cx);
1033 Range<const int*> r5 = range(std::move(cx));
1034 EXPECT_EQ(r1.begin(), &x[0]);
1035 EXPECT_EQ(r1.end(), &x[n]);
1036 EXPECT_EQ(n, r1.size());
1037 EXPECT_EQ(n, r2.size());
1038 EXPECT_EQ(n, r3.size());
1039 EXPECT_EQ(n, r5.size());
1042 TEST(RangeFunc, Vector) {
1044 testRangeFunc(x, 0);
1046 testRangeFunc(x, 1);
1047 testRangeFunc(std::vector<int>{1, 2}, 2);
1050 TEST(RangeFunc, Array) {
1051 std::array<int, 3> x;
1052 testRangeFunc(x, 3);
1055 TEST(RangeFunc, CArray) {
1056 int x[] {1, 2, 3, 4};
1057 testRangeFunc(x, 4);
1060 std::string get_rand_str(size_t size,
1061 std::uniform_int_distribution<>& dist,
1062 std::mt19937& gen) {
1063 std::string ret(size, '\0');
1064 for (size_t i = 0; i < size; ++i) {
1065 ret[i] = static_cast<char>(dist(gen));
1072 bool operator==(MutableStringPiece mp, StringPiece sp) {
1073 return mp.compare(sp) == 0;
1076 bool operator==(StringPiece sp, MutableStringPiece mp) {
1077 return mp.compare(sp) == 0;
1081 TEST(ReplaceAt, exhaustiveTest) {
1082 char input[] = "this is nice and long input";
1083 auto msp = MutableStringPiece(input);
1084 auto str = std::string(input);
1085 std::random_device rd;
1086 std::mt19937 gen(rd());
1087 std::uniform_int_distribution<> dist('a', 'z');
1089 for (int i=0; i < 100; ++i) {
1090 for (size_t j = 1; j <= msp.size(); ++j) {
1091 auto replacement = get_rand_str(j, dist, gen);
1092 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1093 msp.replaceAt(pos, replacement);
1094 str.replace(pos, replacement.size(), replacement);
1095 EXPECT_EQ(msp.compare(str), 0);
1101 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1104 TEST(ReplaceAll, basicTest) {
1105 char input[] = "this is nice and long input";
1106 auto orig = std::string(input);
1107 auto msp = MutableStringPiece(input);
1109 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1110 EXPECT_EQ("thsi si nice and long input", msp);
1111 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1112 EXPECT_EQ(msp, orig);
1114 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1115 EXPECT_EQ(msp, orig);
1117 // at the very beginning
1118 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1119 EXPECT_EQ("siht is nice and long input", msp);
1120 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1121 EXPECT_EQ(msp, orig);
1124 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1125 EXPECT_EQ("this is nice and long soput", msp);
1126 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1127 EXPECT_EQ(msp, orig);
1130 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1131 EXPECT_EQ("this@is@nice@and@long@input", msp);
1132 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1133 EXPECT_EQ(msp, orig);
1136 TEST(ReplaceAll, randomTest) {
1137 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1138 auto orig = std::string(input);
1139 auto msp = MutableStringPiece(input);
1141 std::random_device rd;
1142 std::mt19937 gen(rd());
1143 std::uniform_int_distribution<> dist('A', 'Z');
1145 for (int i=0; i < 100; ++i) {
1146 for (size_t j = 1; j <= orig.size(); ++j) {
1147 auto replacement = get_rand_str(j, dist, gen);
1148 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1149 auto piece = orig.substr(pos, j);
1150 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1151 EXPECT_EQ(msp.find(replacement), pos);
1152 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1153 EXPECT_EQ(msp, orig);
1159 TEST(ReplaceAll, BadArg) {
1161 auto fst = "longer";
1163 char input[] = "meh meh meh";
1164 auto all = MutableStringPiece(input);
1167 all.replaceAll(fst, snd);
1168 } catch (std::invalid_argument&) {
1173 all.replaceAll(snd, fst);
1174 } catch (std::invalid_argument&) {
1178 EXPECT_EQ(count, 2);