2 * Copyright 2015 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_byteset(const StringPiece haystack,
38 const StringPiece needles);
42 using namespace folly;
45 static_assert(std::is_literal_type<StringPiece>::value, "");
47 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
49 TEST(StringPiece, All) {
50 const char* foo = "foo";
51 const char* foo2 = "foo";
55 // we expect the compiler to optimize things so that there's only one copy
56 // of the string literal "foo", even though we've got it in multiple places
57 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
58 // comparison rather than lexical
60 // the string object creates copies though, so the c_str of these should be
62 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
64 // test the basic StringPiece functionality
66 EXPECT_EQ(s.size(), 3);
68 EXPECT_EQ(s.start(), foo); // ptr comparison
69 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
70 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
72 EXPECT_EQ(s.toString(), foo); // lexical comparison
73 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
74 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
76 EXPECT_EQ(s, foo); // lexical comparison
77 EXPECT_EQ(s, fooStr); // lexical comparison
78 EXPECT_EQ(s, foo2Str); // lexical comparison
81 // check using StringPiece to reference substrings
82 const char* foobarbaz = "foobarbaz";
84 // the full "foobarbaz"
85 s.reset(foobarbaz, strlen(foobarbaz));
86 EXPECT_EQ(s.size(), 9);
87 EXPECT_EQ(s.start(), foobarbaz);
88 EXPECT_EQ(s, "foobarbaz");
91 s.assign(foobarbaz, foobarbaz + 3);
92 EXPECT_EQ(s.size(), 3);
93 EXPECT_EQ(s.start(), foobarbaz);
97 s.reset(foobarbaz, strlen(foobarbaz));
98 EXPECT_EQ(s.find("bar"), 3);
99 EXPECT_EQ(s.find("ba", 3), 3);
100 EXPECT_EQ(s.find("ba", 4), 6);
101 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
102 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
103 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
104 // starting pos that is obviously past the end -- This works for std::string
105 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
106 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
107 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
109 EXPECT_EQ(s.find(""), std::string().find(""));
110 EXPECT_EQ(s.find(""), 0);
113 EXPECT_EQ(s.find('b'), 3);
114 EXPECT_EQ(s.find('b', 3), 3);
115 EXPECT_EQ(s.find('b', 4), 6);
116 EXPECT_EQ(s.find('o', 2), 2);
117 EXPECT_EQ(s.find('y'), StringPiece::npos);
118 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
119 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
120 EXPECT_TRUE(s.contains('z'));
121 // starting pos that is obviously past the end -- This works for std::string
122 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
123 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
124 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
126 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
127 EXPECT_EQ(s.find('\0'), StringPiece::npos);
128 EXPECT_FALSE(s.contains('\0'));
130 // single char rfinds
131 EXPECT_EQ(s.rfind('b'), 6);
132 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
133 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
134 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
135 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
137 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
138 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
141 s.reset(foobarbaz, strlen(foobarbaz));
142 EXPECT_EQ(s.find_first_of("bar"), 3);
143 EXPECT_EQ(s.find_first_of("ba", 3), 3);
144 EXPECT_EQ(s.find_first_of("ba", 4), 4);
145 EXPECT_TRUE(s.contains("bar"));
146 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
147 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
148 EXPECT_FALSE(s.contains("xyxy"));
149 // starting position too far
150 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
151 // starting pos that is obviously past the end -- This works for std::string
152 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
153 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
154 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
155 // empty needle. Note that this returns npos, while find() returns 0!
156 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
157 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
159 // single char find_first_ofs
160 EXPECT_EQ(s.find_first_of('b'), 3);
161 EXPECT_EQ(s.find_first_of('b', 3), 3);
162 EXPECT_EQ(s.find_first_of('b', 4), 6);
163 EXPECT_EQ(s.find_first_of('o', 2), 2);
164 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
165 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
166 // starting position too far
167 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
168 // starting pos that is obviously past the end -- This works for std::string
169 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
170 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
171 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
173 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
174 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
177 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
178 EXPECT_EQ(s.size(), 6);
179 EXPECT_EQ(s.start(), foobarbaz + 3);
180 EXPECT_EQ(s, "barbaz");
183 s.reset(foobarbaz + 3, 3);
184 EXPECT_EQ(s.size(), 3);
189 EXPECT_EQ(s.toString(), "");
191 // test an empty StringPiece
193 EXPECT_EQ(s2.size(), 0);
195 // Test comparison operators
229 void expectLT(const T& a, const T& b) {
232 EXPECT_FALSE(a == b);
233 EXPECT_FALSE(a >= b);
237 EXPECT_FALSE(b <= a);
243 void expectEQ(const T& a, const T& b) {
251 TEST(StringPiece, EightBitComparisons) {
252 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
253 constexpr size_t count = sizeof(values) / sizeof(values[0]);
254 for (size_t i = 0; i < count; ++i) {
255 std::string a(1, values[i]);
256 // Defeat copy-on-write
257 std::string aCopy(a.data(), a.size());
259 expectEQ(StringPiece(a), StringPiece(aCopy));
261 for (size_t j = i + 1; j < count; ++j) {
262 std::string b(1, values[j]);
264 expectLT(StringPiece(a), StringPiece(b));
269 TEST(StringPiece, ToByteRange) {
270 StringPiece a("hello");
272 EXPECT_EQ(static_cast<const void*>(a.begin()),
273 static_cast<const void*>(b.begin()));
274 EXPECT_EQ(static_cast<const void*>(a.end()),
275 static_cast<const void*>(b.end()));
277 // and convert back again
279 EXPECT_EQ(a.begin(), c.begin());
280 EXPECT_EQ(a.end(), c.end());
283 TEST(StringPiece, InvalidRange) {
284 StringPiece a("hello");
285 EXPECT_EQ(a, a.subpiece(0, 10));
286 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
287 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
288 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
289 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
290 EXPECT_THROW(a.subpiece(6), std::out_of_range);
292 std::string b("hello");
293 EXPECT_EQ(a, StringPiece(b, 0, 10));
294 EXPECT_EQ("ello", a.subpiece(1));
295 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
296 EXPECT_EQ("ell", a.subpiece(1, 3));
297 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
298 EXPECT_THROW(a.subpiece(6), std::out_of_range);
301 #if FOLLY_HAVE_CONSTEXPR_STRLEN
302 constexpr char helloArray[] = "hello";
304 TEST(StringPiece, Constexpr) {
305 constexpr StringPiece hello1("hello");
306 EXPECT_EQ("hello", hello1);
308 constexpr StringPiece hello2(helloArray);
309 EXPECT_EQ("hello", hello2);
313 TEST(StringPiece, Prefix) {
314 StringPiece a("hello");
315 EXPECT_TRUE(a.startsWith(""));
316 EXPECT_TRUE(a.startsWith("h"));
317 EXPECT_TRUE(a.startsWith('h'));
318 EXPECT_TRUE(a.startsWith("hello"));
319 EXPECT_FALSE(a.startsWith("hellox"));
320 EXPECT_FALSE(a.startsWith('x'));
321 EXPECT_FALSE(a.startsWith("x"));
325 EXPECT_TRUE(b.removePrefix(""));
326 EXPECT_EQ("hello", b);
330 EXPECT_TRUE(b.removePrefix("h"));
331 EXPECT_EQ("ello", b);
335 EXPECT_TRUE(b.removePrefix('h'));
336 EXPECT_EQ("ello", b);
340 EXPECT_TRUE(b.removePrefix("hello"));
345 EXPECT_FALSE(b.removePrefix("hellox"));
346 EXPECT_EQ("hello", b);
350 EXPECT_FALSE(b.removePrefix("x"));
351 EXPECT_EQ("hello", b);
355 EXPECT_FALSE(b.removePrefix('x'));
356 EXPECT_EQ("hello", b);
360 TEST(StringPiece, Suffix) {
361 StringPiece a("hello");
362 EXPECT_TRUE(a.endsWith(""));
363 EXPECT_TRUE(a.endsWith("o"));
364 EXPECT_TRUE(a.endsWith('o'));
365 EXPECT_TRUE(a.endsWith("hello"));
366 EXPECT_FALSE(a.endsWith("xhello"));
367 EXPECT_FALSE(a.endsWith("x"));
368 EXPECT_FALSE(a.endsWith('x'));
372 EXPECT_TRUE(b.removeSuffix(""));
373 EXPECT_EQ("hello", b);
377 EXPECT_TRUE(b.removeSuffix("o"));
378 EXPECT_EQ("hell", b);
382 EXPECT_TRUE(b.removeSuffix('o'));
383 EXPECT_EQ("hell", b);
387 EXPECT_TRUE(b.removeSuffix("hello"));
392 EXPECT_FALSE(b.removeSuffix("xhello"));
393 EXPECT_EQ("hello", b);
397 EXPECT_FALSE(b.removeSuffix("x"));
398 EXPECT_EQ("hello", b);
402 EXPECT_FALSE(b.removeSuffix('x'));
403 EXPECT_EQ("hello", b);
407 TEST(StringPiece, PrefixEmpty) {
409 EXPECT_TRUE(a.startsWith(""));
410 EXPECT_FALSE(a.startsWith("a"));
411 EXPECT_FALSE(a.startsWith('a'));
412 EXPECT_TRUE(a.removePrefix(""));
414 EXPECT_FALSE(a.removePrefix("a"));
416 EXPECT_FALSE(a.removePrefix('a'));
420 TEST(StringPiece, SuffixEmpty) {
422 EXPECT_TRUE(a.endsWith(""));
423 EXPECT_FALSE(a.endsWith("a"));
424 EXPECT_FALSE(a.endsWith('a'));
425 EXPECT_TRUE(a.removeSuffix(""));
427 EXPECT_FALSE(a.removeSuffix("a"));
429 EXPECT_FALSE(a.removeSuffix('a'));
433 TEST(StringPiece, split_step_char_delimiter) {
435 // 012345678901234567890123456
436 auto const s = "this is just a test string";
437 auto const e = std::next(s, std::strlen(s));
440 folly::StringPiece p(s);
441 EXPECT_EQ(s, p.begin());
442 EXPECT_EQ(e, p.end());
445 auto x = p.split_step(' ');
446 EXPECT_EQ(std::next(s, 5), p.begin());
447 EXPECT_EQ(e, p.end());
448 EXPECT_EQ("this", x);
450 x = p.split_step(' ');
451 EXPECT_EQ(std::next(s, 8), p.begin());
452 EXPECT_EQ(e, p.end());
455 x = p.split_step('u');
456 EXPECT_EQ(std::next(s, 10), p.begin());
457 EXPECT_EQ(e, p.end());
460 x = p.split_step(' ');
461 EXPECT_EQ(std::next(s, 13), p.begin());
462 EXPECT_EQ(e, p.end());
465 x = p.split_step(' ');
466 EXPECT_EQ(std::next(s, 14), p.begin());
467 EXPECT_EQ(e, p.end());
470 x = p.split_step(' ');
471 EXPECT_EQ(std::next(s, 16), p.begin());
472 EXPECT_EQ(e, p.end());
475 x = p.split_step(' ');
476 EXPECT_EQ(std::next(s, 21), p.begin());
477 EXPECT_EQ(e, p.end());
478 EXPECT_EQ("test", x);
480 x = p.split_step(' ');
481 EXPECT_EQ(e, p.begin());
482 EXPECT_EQ(e, p.end());
483 EXPECT_EQ("string", x);
485 x = p.split_step(' ');
486 EXPECT_EQ(e, p.begin());
487 EXPECT_EQ(e, p.end());
491 TEST(StringPiece, split_step_range_delimiter) {
493 // 0123456789012345678901234567890123
494 auto const s = "this is just a test string";
495 auto const e = std::next(s, std::strlen(s));
498 folly::StringPiece p(s);
499 EXPECT_EQ(s, p.begin());
500 EXPECT_EQ(e, p.end());
503 auto x = p.split_step(" ");
504 EXPECT_EQ(std::next(s, 6), p.begin());
505 EXPECT_EQ(e, p.end());
506 EXPECT_EQ("this", x);
508 x = p.split_step(" ");
509 EXPECT_EQ(std::next(s, 10), p.begin());
510 EXPECT_EQ(e, p.end());
513 x = p.split_step("u");
514 EXPECT_EQ(std::next(s, 12), p.begin());
515 EXPECT_EQ(e, p.end());
518 x = p.split_step(" ");
519 EXPECT_EQ(std::next(s, 16), p.begin());
520 EXPECT_EQ(e, p.end());
523 x = p.split_step(" ");
524 EXPECT_EQ(std::next(s, 18), p.begin());
525 EXPECT_EQ(e, p.end());
528 x = p.split_step(" ");
529 EXPECT_EQ(std::next(s, 21), p.begin());
530 EXPECT_EQ(e, p.end());
533 x = p.split_step(" ");
534 EXPECT_EQ(std::next(s, 28), p.begin());
535 EXPECT_EQ(e, p.end());
536 EXPECT_EQ(" test", x);
538 x = p.split_step(" ");
539 EXPECT_EQ(e, p.begin());
540 EXPECT_EQ(e, p.end());
541 EXPECT_EQ("string", x);
543 x = p.split_step(" ");
544 EXPECT_EQ(e, p.begin());
545 EXPECT_EQ(e, p.end());
548 x = p.split_step(" ");
549 EXPECT_EQ(e, p.begin());
550 EXPECT_EQ(e, p.end());
554 void split_step_with_process_noop(folly::StringPiece) {}
556 TEST(StringPiece, split_step_with_process_char_delimiter) {
558 // 012345678901234567890123456
559 auto const s = "this is just a test string";
560 auto const e = std::next(s, std::strlen(s));
563 folly::StringPiece p(s);
564 EXPECT_EQ(s, p.begin());
565 EXPECT_EQ(e, p.end());
568 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
569 EXPECT_EQ(std::next(s, 5), p.begin());
570 EXPECT_EQ(e, p.end());
571 EXPECT_EQ("this", x);
575 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
576 EXPECT_EQ(std::next(s, 8), p.begin());
577 EXPECT_EQ(e, p.end());
582 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
583 EXPECT_EQ(std::next(s, 10), p.begin());
584 EXPECT_EQ(e, p.end());
589 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
590 EXPECT_EQ(std::next(s, 13), p.begin());
591 EXPECT_EQ(e, p.end());
596 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
597 EXPECT_EQ(std::next(s, 14), p.begin());
598 EXPECT_EQ(e, p.end());
603 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
604 EXPECT_EQ(std::next(s, 16), p.begin());
605 EXPECT_EQ(e, p.end());
610 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
611 EXPECT_EQ(std::next(s, 21), p.begin());
612 EXPECT_EQ(e, p.end());
613 EXPECT_EQ("test", x);
617 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
618 EXPECT_EQ(e, p.begin());
619 EXPECT_EQ(e, p.end());
620 EXPECT_EQ("string", x);
624 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
625 EXPECT_EQ(e, p.begin());
626 EXPECT_EQ(e, p.end());
631 EXPECT_TRUE((std::is_same<
633 decltype(p.split_step(' ', split_step_with_process_noop))
636 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
639 TEST(StringPiece, split_step_with_process_range_delimiter) {
641 // 0123456789012345678901234567890123
642 auto const s = "this is just a test string";
643 auto const e = std::next(s, std::strlen(s));
646 folly::StringPiece p(s);
647 EXPECT_EQ(s, p.begin());
648 EXPECT_EQ(e, p.end());
651 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
652 EXPECT_EQ(std::next(s, 6), p.begin());
653 EXPECT_EQ(e, p.end());
654 EXPECT_EQ("this", x);
658 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
659 EXPECT_EQ(std::next(s, 10), p.begin());
660 EXPECT_EQ(e, p.end());
665 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
666 EXPECT_EQ(std::next(s, 12), p.begin());
667 EXPECT_EQ(e, p.end());
672 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
673 EXPECT_EQ(std::next(s, 16), p.begin());
674 EXPECT_EQ(e, p.end());
679 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
680 EXPECT_EQ(std::next(s, 18), p.begin());
681 EXPECT_EQ(e, p.end());
686 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
687 EXPECT_EQ(std::next(s, 21), p.begin());
688 EXPECT_EQ(e, p.end());
693 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
694 EXPECT_EQ(std::next(s, 28), p.begin());
695 EXPECT_EQ(e, p.end());
696 EXPECT_EQ(" test", x);
700 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
701 EXPECT_EQ(e, p.begin());
702 EXPECT_EQ(e, p.end());
703 EXPECT_EQ("string", x);
707 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
708 EXPECT_EQ(e, p.begin());
709 EXPECT_EQ(e, p.end());
714 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
715 EXPECT_EQ(e, p.begin());
716 EXPECT_EQ(e, p.end());
721 EXPECT_TRUE((std::is_same<
723 decltype(p.split_step(' ', split_step_with_process_noop))
726 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
729 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
731 // 012345678901234567890123456
732 auto const s = "this is just a test string";
733 auto const e = std::next(s, std::strlen(s));
734 auto const delimiter = ' ';
737 folly::StringPiece p(s);
738 EXPECT_EQ(s, p.begin());
739 EXPECT_EQ(e, p.end());
742 auto const functor = [](
743 folly::StringPiece s,
744 folly::StringPiece expected
746 EXPECT_EQ(expected, s);
750 auto const checker = [&](folly::StringPiece expected) {
751 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
764 EXPECT_TRUE(p.empty());
767 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
769 // 0123456789012345678901234567890123
770 auto const s = "this is just a test string";
771 auto const e = std::next(s, std::strlen(s));
772 auto const delimiter = " ";
775 folly::StringPiece p(s);
776 EXPECT_EQ(s, p.begin());
777 EXPECT_EQ(e, p.end());
780 auto const functor = [](
781 folly::StringPiece s,
782 folly::StringPiece expected
784 EXPECT_EQ(expected, s);
788 auto const checker = [&](folly::StringPiece expected) {
789 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
802 EXPECT_TRUE(p.empty());
805 TEST(StringPiece, NoInvalidImplicitConversions) {
807 bool operator()(folly::Range<int*>) { return false; }
808 bool operator()(folly::StringPiece) { return true; }
811 std::string s = "hello";
812 EXPECT_TRUE(IsString()(s));
815 TEST(qfind, UInt32_Ranges) {
816 vector<uint32_t> a({1, 2, 3, 260, 5});
817 vector<uint32_t> b({2, 3, 4});
819 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
820 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
822 EXPECT_EQ(qfind(a_range, b_range), string::npos);
825 EXPECT_EQ(qfind(a_range, b_range), 1);
828 template <typename NeedleFinder>
829 class NeedleFinderTest : public ::testing::Test {
831 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
832 return NeedleFinder::find_first_byte_of(haystack, needles);
836 struct SseNeedleFinder {
837 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
838 // This will only use the SSE version if it is supported on this CPU
839 // (selected using ifunc).
840 return detail::qfind_first_byte_of(haystack, needles);
844 struct NoSseNeedleFinder {
845 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
846 return detail::qfind_first_byte_of_nosse(haystack, needles);
850 struct ByteSetNeedleFinder {
851 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
852 return detail::qfind_first_byte_of_byteset(haystack, needles);
856 typedef ::testing::Types<SseNeedleFinder,
858 ByteSetNeedleFinder> NeedleFinders;
859 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
861 TYPED_TEST(NeedleFinderTest, Null) {
862 { // null characters in the string
863 string s(10, char(0));
865 string delims("abc");
866 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
868 { // null characters in delim
870 string delims(10, char(0));
873 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
875 { // range not terminated by null character
876 string buf = "abcdefghijklmnopqrstuvwxyz";
877 StringPiece s(buf.data() + 5, 3);
878 StringPiece delims("z");
879 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
883 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
884 string delims(1000, 'b');
885 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
886 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
889 TYPED_TEST(NeedleFinderTest, Empty) {
892 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
893 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
894 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
897 TYPED_TEST(NeedleFinderTest, Unaligned) {
898 // works correctly even if input buffers are not 16-byte aligned
899 string s = "0123456789ABCDEFGH";
900 for (size_t i = 0; i < s.size(); ++i) {
901 StringPiece a(s.c_str() + i);
902 for (size_t j = 0; j < s.size(); ++j) {
903 StringPiece b(s.c_str() + j);
904 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
909 // for some algorithms (specifically those that create a set of needles),
910 // we check for the edge-case of _all_ possible needles being sought.
911 TYPED_TEST(NeedleFinderTest, Needles256) {
913 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
914 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
915 // make the size ~big to avoid any edge-case branches for tiny haystacks
916 const int haystackSize = 50;
917 for (size_t i = minValue; i <= maxValue; i++) { // <=
918 needles.push_back(i);
920 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
921 for (size_t i = minValue; i <= maxValue; i++) {
922 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
925 needles.append("these are redundant characters");
926 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
927 for (size_t i = minValue; i <= maxValue; i++) {
928 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
932 TYPED_TEST(NeedleFinderTest, Base) {
933 for (size_t i = 0; i < 32; ++i) {
934 for (int j = 0; j < 32; ++j) {
935 string s = string(i, 'X') + "abca" + string(i, 'X');
936 string delims = string(j, 'Y') + "a" + string(j, 'Y');
937 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
942 const size_t kPageSize = 4096;
943 // Updates contents so that any read accesses past the last byte will
944 // cause a SIGSEGV. It accomplishes this by changing access to the page that
945 // begins immediately after the end of the contents (as allocators and mmap()
946 // all operate on page boundaries, this is a reasonable assumption).
947 // This function will also initialize buf, which caller must free().
948 void createProtectedBuf(StringPiece& contents, char** buf) {
949 ASSERT_LE(contents.size(), kPageSize);
950 const size_t kSuccess = 0;
951 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
954 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
955 size_t newBegin = kPageSize - contents.size();
956 memcpy(*buf + newBegin, contents.data(), contents.size());
957 contents.reset(*buf + newBegin, contents.size());
960 void freeProtectedBuf(char* buf) {
961 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
965 TYPED_TEST(NeedleFinderTest, NoSegFault) {
966 const string base = string(32, 'a') + string("b");
967 const string delims = string(32, 'c') + string("b");
968 for (int i = 0; i <= 32; i++) {
969 for (int j = 0; j <= 33; j++) {
970 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
971 StringPiece s1(base);
973 ASSERT_TRUE(!s1.empty());
977 StringPiece s2(delims);
981 createProtectedBuf(s1, &buf1);
982 createProtectedBuf(s2, &buf2);
983 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
984 // string(s1.data(), s1.size()).c_str(), s1.size(),
985 // string(s2.data(), s2.size()).c_str(), s2.size());
986 auto r1 = this->find_first_byte_of(s1, s2);
987 auto f1 = std::find_first_of(s1.begin(), s1.end(),
988 s2.begin(), s2.end());
989 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
991 auto r2 = this->find_first_byte_of(s2, s1);
992 auto f2 = std::find_first_of(s2.begin(), s2.end(),
993 s1.begin(), s1.end());
994 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
996 freeProtectedBuf(buf1);
997 freeProtectedBuf(buf2);
1003 TEST(NonConstTest, StringPiece) {
1004 std::string hello("hello");
1005 MutableStringPiece sp(&hello.front(), hello.size());
1007 EXPECT_EQ("xello", hello);
1010 EXPECT_EQ("xello", s);
1014 MutableByteRange r2(sp);
1019 void testRangeFunc(C&& x, size_t n) {
1021 // type, conversion checks
1022 Range<int*> r1 = range(std::forward<C>(x));
1023 Range<const int*> r2 = range(std::forward<C>(x));
1024 Range<const int*> r3 = range(cx);
1025 Range<const int*> r5 = range(std::move(cx));
1026 EXPECT_EQ(r1.begin(), &x[0]);
1027 EXPECT_EQ(r1.end(), &x[n]);
1028 EXPECT_EQ(n, r1.size());
1029 EXPECT_EQ(n, r2.size());
1030 EXPECT_EQ(n, r3.size());
1031 EXPECT_EQ(n, r5.size());
1034 TEST(RangeFunc, Vector) {
1036 testRangeFunc(x, 0);
1038 testRangeFunc(x, 1);
1039 testRangeFunc(std::vector<int>{1, 2}, 2);
1042 TEST(RangeFunc, Array) {
1043 std::array<int, 3> x;
1044 testRangeFunc(x, 3);
1047 TEST(RangeFunc, CArray) {
1048 int x[] {1, 2, 3, 4};
1049 testRangeFunc(x, 4);
1052 std::string get_rand_str(size_t size,
1053 std::uniform_int_distribution<>& dist,
1054 std::mt19937& gen) {
1055 std::string ret(size, '\0');
1056 for (size_t i = 0; i < size; ++i) {
1057 ret[i] = static_cast<char>(dist(gen));
1064 bool operator==(MutableStringPiece mp, StringPiece sp) {
1065 return mp.compare(sp) == 0;
1068 bool operator==(StringPiece sp, MutableStringPiece mp) {
1069 return mp.compare(sp) == 0;
1073 TEST(ReplaceAt, exhaustiveTest) {
1074 char input[] = "this is nice and long input";
1075 auto msp = MutableStringPiece(input);
1076 auto str = std::string(input);
1077 std::random_device rd;
1078 std::mt19937 gen(rd());
1079 std::uniform_int_distribution<> dist('a', 'z');
1081 for (int i=0; i < 100; ++i) {
1082 for (size_t j = 1; j <= msp.size(); ++j) {
1083 auto replacement = get_rand_str(j, dist, gen);
1084 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1085 msp.replaceAt(pos, replacement);
1086 str.replace(pos, replacement.size(), replacement);
1087 EXPECT_EQ(msp.compare(str), 0);
1093 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1096 TEST(ReplaceAll, basicTest) {
1097 char input[] = "this is nice and long input";
1098 auto orig = std::string(input);
1099 auto msp = MutableStringPiece(input);
1101 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1102 EXPECT_EQ("thsi si nice and long input", msp);
1103 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1104 EXPECT_EQ(msp, orig);
1106 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1107 EXPECT_EQ(msp, orig);
1109 // at the very beginning
1110 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1111 EXPECT_EQ("siht is nice and long input", msp);
1112 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1113 EXPECT_EQ(msp, orig);
1116 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1117 EXPECT_EQ("this is nice and long soput", msp);
1118 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1119 EXPECT_EQ(msp, orig);
1122 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1123 EXPECT_EQ("this@is@nice@and@long@input", msp);
1124 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1125 EXPECT_EQ(msp, orig);
1128 TEST(ReplaceAll, randomTest) {
1129 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1130 auto orig = std::string(input);
1131 auto msp = MutableStringPiece(input);
1133 std::random_device rd;
1134 std::mt19937 gen(rd());
1135 std::uniform_int_distribution<> dist('A', 'Z');
1137 for (int i=0; i < 100; ++i) {
1138 for (size_t j = 1; j <= orig.size(); ++j) {
1139 auto replacement = get_rand_str(j, dist, gen);
1140 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1141 auto piece = orig.substr(pos, j);
1142 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1143 EXPECT_EQ(msp.find(replacement), pos);
1144 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1145 EXPECT_EQ(msp, orig);
1151 TEST(ReplaceAll, BadArg) {
1153 auto fst = "longer";
1155 char input[] = "meh meh meh";
1156 auto all = MutableStringPiece(input);
1159 all.replaceAll(fst, snd);
1160 } catch (std::invalid_argument&) {
1165 all.replaceAll(snd, fst);
1166 } catch (std::invalid_argument&) {
1170 EXPECT_EQ(count, 2);
1173 TEST(Range, Constructors) {
1174 vector<int> c = {1, 2, 3};
1175 typedef Range<vector<int>::iterator> RangeType;
1176 typedef Range<vector<int>::const_iterator> ConstRangeType;
1177 RangeType cr(c.begin(), c.end());
1178 auto subpiece1 = ConstRangeType(cr, 1, 5);
1179 auto subpiece2 = ConstRangeType(cr, 1);
1180 EXPECT_EQ(subpiece1.size(), 2);
1181 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1182 EXPECT_EQ(subpiece1.end(), subpiece2.end());