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>
33 namespace folly { namespace detail {
35 // declaration of functions in Range.cpp
36 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
37 const StringPiece& needles);
39 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
40 const StringPiece& needles);
44 using namespace folly;
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(qfind, UInt32_Ranges) {
806 vector<uint32_t> a({1, 2, 3, 260, 5});
807 vector<uint32_t> b({2, 3, 4});
809 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
810 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
812 EXPECT_EQ(qfind(a_range, b_range), string::npos);
815 EXPECT_EQ(qfind(a_range, b_range), 1);
818 template <typename NeedleFinder>
819 class NeedleFinderTest : public ::testing::Test {
821 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
822 return NeedleFinder::find_first_byte_of(haystack, needles);
826 struct SseNeedleFinder {
827 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
828 // This will only use the SSE version if it is supported on this CPU
829 // (selected using ifunc).
830 return detail::qfind_first_byte_of(haystack, needles);
834 struct NoSseNeedleFinder {
835 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
836 return detail::qfind_first_byte_of_nosse(haystack, needles);
840 struct MemchrNeedleFinder {
841 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
842 return detail::qfind_first_byte_of_memchr(haystack, needles);
846 struct ByteSetNeedleFinder {
847 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
848 return detail::qfind_first_byte_of_byteset(haystack, needles);
852 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
853 ByteSetNeedleFinder> NeedleFinders;
854 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
856 TYPED_TEST(NeedleFinderTest, Null) {
857 { // null characters in the string
858 string s(10, char(0));
860 string delims("abc");
861 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
863 { // null characters in delim
865 string delims(10, char(0));
868 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
870 { // range not terminated by null character
871 string buf = "abcdefghijklmnopqrstuvwxyz";
872 StringPiece s(buf.data() + 5, 3);
873 StringPiece delims("z");
874 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
878 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
879 string delims(1000, 'b');
880 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
881 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
884 TYPED_TEST(NeedleFinderTest, Empty) {
887 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
888 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
889 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
892 TYPED_TEST(NeedleFinderTest, Unaligned) {
893 // works correctly even if input buffers are not 16-byte aligned
894 string s = "0123456789ABCDEFGH";
895 for (int i = 0; i < s.size(); ++i) {
896 StringPiece a(s.c_str() + i);
897 for (int j = 0; j < s.size(); ++j) {
898 StringPiece b(s.c_str() + j);
899 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
904 // for some algorithms (specifically those that create a set of needles),
905 // we check for the edge-case of _all_ possible needles being sought.
906 TYPED_TEST(NeedleFinderTest, Needles256) {
908 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
909 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
910 // make the size ~big to avoid any edge-case branches for tiny haystacks
911 const int haystackSize = 50;
912 for (int i = minValue; i <= maxValue; i++) { // <=
913 needles.push_back(i);
915 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
916 for (int i = minValue; i <= maxValue; i++) {
917 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
920 needles.append("these are redundant characters");
921 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
922 for (int i = minValue; i <= maxValue; i++) {
923 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
927 TYPED_TEST(NeedleFinderTest, Base) {
928 for (int i = 0; i < 32; ++i) {
929 for (int j = 0; j < 32; ++j) {
930 string s = string(i, 'X') + "abca" + string(i, 'X');
931 string delims = string(j, 'Y') + "a" + string(j, 'Y');
932 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
937 const size_t kPageSize = 4096;
938 // Updates contents so that any read accesses past the last byte will
939 // cause a SIGSEGV. It accomplishes this by changing access to the page that
940 // begins immediately after the end of the contents (as allocators and mmap()
941 // all operate on page boundaries, this is a reasonable assumption).
942 // This function will also initialize buf, which caller must free().
943 void createProtectedBuf(StringPiece& contents, char** buf) {
944 ASSERT_LE(contents.size(), kPageSize);
945 const size_t kSuccess = 0;
946 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
949 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
950 size_t newBegin = kPageSize - contents.size();
951 memcpy(*buf + newBegin, contents.data(), contents.size());
952 contents.reset(*buf + newBegin, contents.size());
955 void freeProtectedBuf(char* buf) {
956 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
960 TYPED_TEST(NeedleFinderTest, NoSegFault) {
961 const string base = string(32, 'a') + string("b");
962 const string delims = string(32, 'c') + string("b");
963 for (int i = 0; i <= 32; i++) {
964 for (int j = 0; j <= 33; j++) {
965 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
966 StringPiece s1(base);
968 ASSERT_TRUE(!s1.empty());
972 StringPiece s2(delims);
976 createProtectedBuf(s1, &buf1);
977 createProtectedBuf(s2, &buf2);
978 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
979 // string(s1.data(), s1.size()).c_str(), s1.size(),
980 // string(s2.data(), s2.size()).c_str(), s2.size());
981 auto r1 = this->find_first_byte_of(s1, s2);
982 auto f1 = std::find_first_of(s1.begin(), s1.end(),
983 s2.begin(), s2.end());
984 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
986 auto r2 = this->find_first_byte_of(s2, s1);
987 auto f2 = std::find_first_of(s2.begin(), s2.end(),
988 s1.begin(), s1.end());
989 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
991 freeProtectedBuf(buf1);
992 freeProtectedBuf(buf2);
998 TEST(NonConstTest, StringPiece) {
999 std::string hello("hello");
1000 MutableStringPiece sp(&hello.front(), hello.size());
1002 EXPECT_EQ("xello", hello);
1005 EXPECT_EQ("xello", s);
1009 MutableByteRange r2(sp);
1014 void testRangeFunc(C&& x, size_t n) {
1016 // type, conversion checks
1017 Range<int*> r1 = range(std::forward<C>(x));
1018 Range<const int*> r2 = range(std::forward<C>(x));
1019 Range<const int*> r3 = range(cx);
1020 Range<const int*> r5 = range(std::move(cx));
1021 EXPECT_EQ(r1.begin(), &x[0]);
1022 EXPECT_EQ(r1.end(), &x[n]);
1023 EXPECT_EQ(n, r1.size());
1024 EXPECT_EQ(n, r2.size());
1025 EXPECT_EQ(n, r3.size());
1026 EXPECT_EQ(n, r5.size());
1029 TEST(RangeFunc, Vector) {
1031 testRangeFunc(x, 0);
1033 testRangeFunc(x, 1);
1034 testRangeFunc(std::vector<int>{1, 2}, 2);
1037 TEST(RangeFunc, Array) {
1038 std::array<int, 3> x;
1039 testRangeFunc(x, 3);
1042 TEST(RangeFunc, CArray) {
1043 int x[] {1, 2, 3, 4};
1044 testRangeFunc(x, 4);
1047 std::string get_rand_str(
1048 int size, std::uniform_int_distribution<>& dist, std::mt19937& gen) {
1049 std::string ret(size, '\0');
1050 for (int i=0; i<size; ++i) {
1051 ret[i] = static_cast<char>(dist(gen));
1058 bool operator==(MutableStringPiece mp, StringPiece sp) {
1059 return mp.compare(sp) == 0;
1062 bool operator==(StringPiece sp, MutableStringPiece mp) {
1063 return mp.compare(sp) == 0;
1067 TEST(ReplaceAt, exhaustiveTest) {
1068 char input[] = "this is nice and long input";
1069 auto msp = MutableStringPiece(input);
1070 auto str = std::string(input);
1071 std::random_device rd;
1072 std::mt19937 gen(rd());
1073 std::uniform_int_distribution<> dist('a', 'z');
1075 for (int i=0; i < 100; ++i) {
1076 for (int j = 1; j <= msp.size(); ++j) {
1077 auto replacement = get_rand_str(j, dist, gen);
1078 for (int pos=0; pos < msp.size() - j; ++pos) {
1079 msp.replaceAt(pos, replacement);
1080 str.replace(pos, replacement.size(), replacement);
1081 EXPECT_EQ(msp.compare(str), 0);
1087 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1090 TEST(ReplaceAll, basicTest) {
1091 char input[] = "this is nice and long input";
1092 auto orig = std::string(input);
1093 auto msp = MutableStringPiece(input);
1095 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1096 EXPECT_EQ("thsi si nice and long input", msp);
1097 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1098 EXPECT_EQ(msp, orig);
1100 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1101 EXPECT_EQ(msp, orig);
1103 // at the very beginning
1104 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1105 EXPECT_EQ("siht is nice and long input", msp);
1106 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1107 EXPECT_EQ(msp, orig);
1110 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1111 EXPECT_EQ("this is nice and long soput", msp);
1112 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1113 EXPECT_EQ(msp, orig);
1116 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1117 EXPECT_EQ("this@is@nice@and@long@input", msp);
1118 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1119 EXPECT_EQ(msp, orig);
1122 TEST(ReplaceAll, randomTest) {
1123 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1124 auto orig = std::string(input);
1125 auto msp = MutableStringPiece(input);
1127 std::random_device rd;
1128 std::mt19937 gen(rd());
1129 std::uniform_int_distribution<> dist('A', 'Z');
1131 for (int i=0; i < 100; ++i) {
1132 for (int j = 1; j <= orig.size(); ++j) {
1133 auto replacement = get_rand_str(j, dist, gen);
1134 for (int pos=0; pos < msp.size() - j; ++pos) {
1135 auto piece = orig.substr(pos, j);
1136 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1137 EXPECT_EQ(msp.find(replacement), pos);
1138 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1139 EXPECT_EQ(msp, orig);
1145 TEST(ReplaceAll, BadArg) {
1147 auto fst = "longer";
1149 char input[] = "meh meh meh";
1150 auto all = MutableStringPiece(input);
1153 all.replaceAll(fst, snd);
1154 } catch (std::invalid_argument&) {
1159 all.replaceAll(snd, fst);
1160 } catch (std::invalid_argument&) {
1164 EXPECT_EQ(count, 2);