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 using namespace folly;
37 static_assert(std::is_literal_type<StringPiece>::value, "");
39 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
41 TEST(StringPiece, All) {
42 const char* foo = "foo";
43 const char* foo2 = "foo";
47 // we expect the compiler to optimize things so that there's only one copy
48 // of the string literal "foo", even though we've got it in multiple places
49 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
50 // comparison rather than lexical
52 // the string object creates copies though, so the c_str of these should be
54 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
56 // test the basic StringPiece functionality
58 EXPECT_EQ(s.size(), 3);
60 EXPECT_EQ(s.start(), foo); // ptr comparison
61 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
62 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
64 EXPECT_EQ(s.toString(), foo); // lexical comparison
65 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
66 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
68 EXPECT_EQ(s, foo); // lexical comparison
69 EXPECT_EQ(s, fooStr); // lexical comparison
70 EXPECT_EQ(s, foo2Str); // lexical comparison
73 // check using StringPiece to reference substrings
74 const char* foobarbaz = "foobarbaz";
76 // the full "foobarbaz"
77 s.reset(foobarbaz, strlen(foobarbaz));
78 EXPECT_EQ(s.size(), 9);
79 EXPECT_EQ(s.start(), foobarbaz);
80 EXPECT_EQ(s, "foobarbaz");
83 s.assign(foobarbaz, foobarbaz + 3);
84 EXPECT_EQ(s.size(), 3);
85 EXPECT_EQ(s.start(), foobarbaz);
89 s.reset(foobarbaz, strlen(foobarbaz));
90 EXPECT_EQ(s.find("bar"), 3);
91 EXPECT_EQ(s.find("ba", 3), 3);
92 EXPECT_EQ(s.find("ba", 4), 6);
93 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
94 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
95 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
96 // starting pos that is obviously past the end -- This works for std::string
97 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
98 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
99 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
101 EXPECT_EQ(s.find(""), std::string().find(""));
102 EXPECT_EQ(s.find(""), 0);
105 EXPECT_EQ(s.find('b'), 3);
106 EXPECT_EQ(s.find('b', 3), 3);
107 EXPECT_EQ(s.find('b', 4), 6);
108 EXPECT_EQ(s.find('o', 2), 2);
109 EXPECT_EQ(s.find('y'), StringPiece::npos);
110 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
111 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
112 EXPECT_TRUE(s.contains('z'));
113 // starting pos that is obviously past the end -- This works for std::string
114 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
115 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
116 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
118 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
119 EXPECT_EQ(s.find('\0'), StringPiece::npos);
120 EXPECT_FALSE(s.contains('\0'));
122 // single char rfinds
123 EXPECT_EQ(s.rfind('b'), 6);
124 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
125 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
126 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
127 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
129 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
130 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
133 s.reset(foobarbaz, strlen(foobarbaz));
134 EXPECT_EQ(s.find_first_of("bar"), 3);
135 EXPECT_EQ(s.find_first_of("ba", 3), 3);
136 EXPECT_EQ(s.find_first_of("ba", 4), 4);
137 EXPECT_TRUE(s.contains("bar"));
138 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
139 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
140 EXPECT_FALSE(s.contains("xyxy"));
141 // starting position too far
142 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
143 // starting pos that is obviously past the end -- This works for std::string
144 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
145 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
146 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
147 // empty needle. Note that this returns npos, while find() returns 0!
148 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
149 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
151 // single char find_first_ofs
152 EXPECT_EQ(s.find_first_of('b'), 3);
153 EXPECT_EQ(s.find_first_of('b', 3), 3);
154 EXPECT_EQ(s.find_first_of('b', 4), 6);
155 EXPECT_EQ(s.find_first_of('o', 2), 2);
156 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
157 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
158 // starting position too far
159 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
160 // starting pos that is obviously past the end -- This works for std::string
161 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
162 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
163 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
165 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
166 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
169 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
170 EXPECT_EQ(s.size(), 6);
171 EXPECT_EQ(s.start(), foobarbaz + 3);
172 EXPECT_EQ(s, "barbaz");
175 s.reset(foobarbaz + 3, 3);
176 EXPECT_EQ(s.size(), 3);
181 EXPECT_EQ(s.toString(), "");
183 // test an empty StringPiece
185 EXPECT_EQ(s2.size(), 0);
187 // Test comparison operators
221 void expectLT(const T& a, const T& b) {
224 EXPECT_FALSE(a == b);
225 EXPECT_FALSE(a >= b);
229 EXPECT_FALSE(b <= a);
235 void expectEQ(const T& a, const T& b) {
243 TEST(StringPiece, EightBitComparisons) {
244 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
245 constexpr size_t count = sizeof(values) / sizeof(values[0]);
246 for (size_t i = 0; i < count; ++i) {
247 std::string a(1, values[i]);
248 // Defeat copy-on-write
249 std::string aCopy(a.data(), a.size());
251 expectEQ(StringPiece(a), StringPiece(aCopy));
253 for (size_t j = i + 1; j < count; ++j) {
254 std::string b(1, values[j]);
256 expectLT(StringPiece(a), StringPiece(b));
261 TEST(StringPiece, ToByteRange) {
262 StringPiece a("hello");
264 EXPECT_EQ(static_cast<const void*>(a.begin()),
265 static_cast<const void*>(b.begin()));
266 EXPECT_EQ(static_cast<const void*>(a.end()),
267 static_cast<const void*>(b.end()));
269 // and convert back again
271 EXPECT_EQ(a.begin(), c.begin());
272 EXPECT_EQ(a.end(), c.end());
275 TEST(StringPiece, InvalidRange) {
276 StringPiece a("hello");
277 EXPECT_EQ(a, a.subpiece(0, 10));
278 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
279 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
280 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
281 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
282 EXPECT_THROW(a.subpiece(6), std::out_of_range);
284 std::string b("hello");
285 EXPECT_EQ(a, StringPiece(b, 0, 10));
286 EXPECT_EQ("ello", a.subpiece(1));
287 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
288 EXPECT_EQ("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);
293 constexpr char helloArray[] = "hello";
295 TEST(StringPiece, Constexpr) {
296 constexpr StringPiece hello1("hello");
297 EXPECT_EQ("hello", hello1);
299 constexpr StringPiece hello2(helloArray);
300 EXPECT_EQ("hello", hello2);
303 TEST(StringPiece, Prefix) {
304 StringPiece a("hello");
305 EXPECT_TRUE(a.startsWith(""));
306 EXPECT_TRUE(a.startsWith("h"));
307 EXPECT_TRUE(a.startsWith('h'));
308 EXPECT_TRUE(a.startsWith("hello"));
309 EXPECT_FALSE(a.startsWith("hellox"));
310 EXPECT_FALSE(a.startsWith('x'));
311 EXPECT_FALSE(a.startsWith("x"));
315 EXPECT_TRUE(b.removePrefix(""));
316 EXPECT_EQ("hello", b);
320 EXPECT_TRUE(b.removePrefix("h"));
321 EXPECT_EQ("ello", b);
325 EXPECT_TRUE(b.removePrefix('h'));
326 EXPECT_EQ("ello", b);
330 EXPECT_TRUE(b.removePrefix("hello"));
335 EXPECT_FALSE(b.removePrefix("hellox"));
336 EXPECT_EQ("hello", b);
340 EXPECT_FALSE(b.removePrefix("x"));
341 EXPECT_EQ("hello", b);
345 EXPECT_FALSE(b.removePrefix('x'));
346 EXPECT_EQ("hello", b);
350 TEST(StringPiece, Suffix) {
351 StringPiece a("hello");
352 EXPECT_TRUE(a.endsWith(""));
353 EXPECT_TRUE(a.endsWith("o"));
354 EXPECT_TRUE(a.endsWith('o'));
355 EXPECT_TRUE(a.endsWith("hello"));
356 EXPECT_FALSE(a.endsWith("xhello"));
357 EXPECT_FALSE(a.endsWith("x"));
358 EXPECT_FALSE(a.endsWith('x'));
362 EXPECT_TRUE(b.removeSuffix(""));
363 EXPECT_EQ("hello", b);
367 EXPECT_TRUE(b.removeSuffix("o"));
368 EXPECT_EQ("hell", b);
372 EXPECT_TRUE(b.removeSuffix('o'));
373 EXPECT_EQ("hell", b);
377 EXPECT_TRUE(b.removeSuffix("hello"));
382 EXPECT_FALSE(b.removeSuffix("xhello"));
383 EXPECT_EQ("hello", b);
387 EXPECT_FALSE(b.removeSuffix("x"));
388 EXPECT_EQ("hello", b);
392 EXPECT_FALSE(b.removeSuffix('x'));
393 EXPECT_EQ("hello", b);
397 TEST(StringPiece, PrefixEmpty) {
399 EXPECT_TRUE(a.startsWith(""));
400 EXPECT_FALSE(a.startsWith("a"));
401 EXPECT_FALSE(a.startsWith('a'));
402 EXPECT_TRUE(a.removePrefix(""));
404 EXPECT_FALSE(a.removePrefix("a"));
406 EXPECT_FALSE(a.removePrefix('a'));
410 TEST(StringPiece, SuffixEmpty) {
412 EXPECT_TRUE(a.endsWith(""));
413 EXPECT_FALSE(a.endsWith("a"));
414 EXPECT_FALSE(a.endsWith('a'));
415 EXPECT_TRUE(a.removeSuffix(""));
417 EXPECT_FALSE(a.removeSuffix("a"));
419 EXPECT_FALSE(a.removeSuffix('a'));
423 TEST(StringPiece, split_step_char_delimiter) {
425 // 012345678901234567890123456
426 auto const s = "this is just a test string";
427 auto const e = std::next(s, std::strlen(s));
430 folly::StringPiece p(s);
431 EXPECT_EQ(s, p.begin());
432 EXPECT_EQ(e, p.end());
435 auto x = p.split_step(' ');
436 EXPECT_EQ(std::next(s, 5), p.begin());
437 EXPECT_EQ(e, p.end());
438 EXPECT_EQ("this", x);
440 x = p.split_step(' ');
441 EXPECT_EQ(std::next(s, 8), p.begin());
442 EXPECT_EQ(e, p.end());
445 x = p.split_step('u');
446 EXPECT_EQ(std::next(s, 10), p.begin());
447 EXPECT_EQ(e, p.end());
450 x = p.split_step(' ');
451 EXPECT_EQ(std::next(s, 13), p.begin());
452 EXPECT_EQ(e, p.end());
455 x = p.split_step(' ');
456 EXPECT_EQ(std::next(s, 14), p.begin());
457 EXPECT_EQ(e, p.end());
460 x = p.split_step(' ');
461 EXPECT_EQ(std::next(s, 16), p.begin());
462 EXPECT_EQ(e, p.end());
465 x = p.split_step(' ');
466 EXPECT_EQ(std::next(s, 21), p.begin());
467 EXPECT_EQ(e, p.end());
468 EXPECT_EQ("test", x);
470 x = p.split_step(' ');
471 EXPECT_EQ(e, p.begin());
472 EXPECT_EQ(e, p.end());
473 EXPECT_EQ("string", x);
475 x = p.split_step(' ');
476 EXPECT_EQ(e, p.begin());
477 EXPECT_EQ(e, p.end());
481 TEST(StringPiece, split_step_range_delimiter) {
483 // 0123456789012345678901234567890123
484 auto const s = "this is just a test string";
485 auto const e = std::next(s, std::strlen(s));
488 folly::StringPiece p(s);
489 EXPECT_EQ(s, p.begin());
490 EXPECT_EQ(e, p.end());
493 auto x = p.split_step(" ");
494 EXPECT_EQ(std::next(s, 6), p.begin());
495 EXPECT_EQ(e, p.end());
496 EXPECT_EQ("this", x);
498 x = p.split_step(" ");
499 EXPECT_EQ(std::next(s, 10), p.begin());
500 EXPECT_EQ(e, p.end());
503 x = p.split_step("u");
504 EXPECT_EQ(std::next(s, 12), p.begin());
505 EXPECT_EQ(e, p.end());
508 x = p.split_step(" ");
509 EXPECT_EQ(std::next(s, 16), p.begin());
510 EXPECT_EQ(e, p.end());
513 x = p.split_step(" ");
514 EXPECT_EQ(std::next(s, 18), p.begin());
515 EXPECT_EQ(e, p.end());
518 x = p.split_step(" ");
519 EXPECT_EQ(std::next(s, 21), p.begin());
520 EXPECT_EQ(e, p.end());
523 x = p.split_step(" ");
524 EXPECT_EQ(std::next(s, 28), p.begin());
525 EXPECT_EQ(e, p.end());
526 EXPECT_EQ(" test", x);
528 x = p.split_step(" ");
529 EXPECT_EQ(e, p.begin());
530 EXPECT_EQ(e, p.end());
531 EXPECT_EQ("string", x);
533 x = p.split_step(" ");
534 EXPECT_EQ(e, p.begin());
535 EXPECT_EQ(e, p.end());
538 x = p.split_step(" ");
539 EXPECT_EQ(e, p.begin());
540 EXPECT_EQ(e, p.end());
544 void split_step_with_process_noop(folly::StringPiece) {}
546 TEST(StringPiece, split_step_with_process_char_delimiter) {
548 // 012345678901234567890123456
549 auto const s = "this is just a test string";
550 auto const e = std::next(s, std::strlen(s));
553 folly::StringPiece p(s);
554 EXPECT_EQ(s, p.begin());
555 EXPECT_EQ(e, p.end());
558 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
559 EXPECT_EQ(std::next(s, 5), p.begin());
560 EXPECT_EQ(e, p.end());
561 EXPECT_EQ("this", x);
565 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
566 EXPECT_EQ(std::next(s, 8), p.begin());
567 EXPECT_EQ(e, p.end());
572 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
573 EXPECT_EQ(std::next(s, 10), p.begin());
574 EXPECT_EQ(e, p.end());
579 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
580 EXPECT_EQ(std::next(s, 13), p.begin());
581 EXPECT_EQ(e, p.end());
586 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
587 EXPECT_EQ(std::next(s, 14), p.begin());
588 EXPECT_EQ(e, p.end());
593 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
594 EXPECT_EQ(std::next(s, 16), p.begin());
595 EXPECT_EQ(e, p.end());
600 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
601 EXPECT_EQ(std::next(s, 21), p.begin());
602 EXPECT_EQ(e, p.end());
603 EXPECT_EQ("test", x);
607 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
608 EXPECT_EQ(e, p.begin());
609 EXPECT_EQ(e, p.end());
610 EXPECT_EQ("string", x);
614 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
615 EXPECT_EQ(e, p.begin());
616 EXPECT_EQ(e, p.end());
621 EXPECT_TRUE((std::is_same<
623 decltype(p.split_step(' ', split_step_with_process_noop))
626 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
629 TEST(StringPiece, split_step_with_process_range_delimiter) {
631 // 0123456789012345678901234567890123
632 auto const s = "this is just a test string";
633 auto const e = std::next(s, std::strlen(s));
636 folly::StringPiece p(s);
637 EXPECT_EQ(s, p.begin());
638 EXPECT_EQ(e, p.end());
641 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
642 EXPECT_EQ(std::next(s, 6), p.begin());
643 EXPECT_EQ(e, p.end());
644 EXPECT_EQ("this", x);
648 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
649 EXPECT_EQ(std::next(s, 10), p.begin());
650 EXPECT_EQ(e, p.end());
655 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
656 EXPECT_EQ(std::next(s, 12), p.begin());
657 EXPECT_EQ(e, p.end());
662 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
663 EXPECT_EQ(std::next(s, 16), p.begin());
664 EXPECT_EQ(e, p.end());
669 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
670 EXPECT_EQ(std::next(s, 18), p.begin());
671 EXPECT_EQ(e, p.end());
676 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
677 EXPECT_EQ(std::next(s, 21), p.begin());
678 EXPECT_EQ(e, p.end());
683 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
684 EXPECT_EQ(std::next(s, 28), p.begin());
685 EXPECT_EQ(e, p.end());
686 EXPECT_EQ(" test", x);
690 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
691 EXPECT_EQ(e, p.begin());
692 EXPECT_EQ(e, p.end());
693 EXPECT_EQ("string", x);
697 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
698 EXPECT_EQ(e, p.begin());
699 EXPECT_EQ(e, p.end());
704 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
705 EXPECT_EQ(e, p.begin());
706 EXPECT_EQ(e, p.end());
711 EXPECT_TRUE((std::is_same<
713 decltype(p.split_step(' ', split_step_with_process_noop))
716 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
719 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
721 // 012345678901234567890123456
722 auto const s = "this is just a test string";
723 auto const e = std::next(s, std::strlen(s));
724 auto const delimiter = ' ';
727 folly::StringPiece p(s);
728 EXPECT_EQ(s, p.begin());
729 EXPECT_EQ(e, p.end());
732 auto const functor = [](
733 folly::StringPiece s,
734 folly::StringPiece expected
736 EXPECT_EQ(expected, s);
740 auto const checker = [&](folly::StringPiece expected) {
741 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
754 EXPECT_TRUE(p.empty());
757 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
759 // 0123456789012345678901234567890123
760 auto const s = "this is just a test string";
761 auto const e = std::next(s, std::strlen(s));
762 auto const delimiter = " ";
765 folly::StringPiece p(s);
766 EXPECT_EQ(s, p.begin());
767 EXPECT_EQ(e, p.end());
770 auto const functor = [](
771 folly::StringPiece s,
772 folly::StringPiece expected
774 EXPECT_EQ(expected, s);
778 auto const checker = [&](folly::StringPiece expected) {
779 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
792 EXPECT_TRUE(p.empty());
795 TEST(StringPiece, NoInvalidImplicitConversions) {
797 bool operator()(folly::Range<int*>) { return false; }
798 bool operator()(folly::StringPiece) { return true; }
801 std::string s = "hello";
802 EXPECT_TRUE(IsString()(s));
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 ByteSetNeedleFinder {
841 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
842 return detail::qfind_first_byte_of_byteset(haystack, needles);
846 typedef ::testing::Types<SseNeedleFinder,
848 ByteSetNeedleFinder> NeedleFinders;
849 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
851 TYPED_TEST(NeedleFinderTest, Null) {
852 { // null characters in the string
853 string s(10, char(0));
855 string delims("abc");
856 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
858 { // null characters in delim
860 string delims(10, char(0));
863 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
865 { // range not terminated by null character
866 string buf = "abcdefghijklmnopqrstuvwxyz";
867 StringPiece s(buf.data() + 5, 3);
868 StringPiece delims("z");
869 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
873 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
874 string delims(1000, 'b');
875 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
876 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
879 TYPED_TEST(NeedleFinderTest, Empty) {
882 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
883 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
884 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
887 TYPED_TEST(NeedleFinderTest, Unaligned) {
888 // works correctly even if input buffers are not 16-byte aligned
889 string s = "0123456789ABCDEFGH";
890 for (size_t i = 0; i < s.size(); ++i) {
891 StringPiece a(s.c_str() + i);
892 for (size_t j = 0; j < s.size(); ++j) {
893 StringPiece b(s.c_str() + j);
894 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
899 // for some algorithms (specifically those that create a set of needles),
900 // we check for the edge-case of _all_ possible needles being sought.
901 TYPED_TEST(NeedleFinderTest, Needles256) {
903 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
904 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
905 // make the size ~big to avoid any edge-case branches for tiny haystacks
906 const int haystackSize = 50;
907 for (size_t i = minValue; i <= maxValue; i++) { // <=
908 needles.push_back(i);
910 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
911 for (size_t i = minValue; i <= maxValue; i++) {
912 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
915 needles.append("these are redundant characters");
916 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
917 for (size_t i = minValue; i <= maxValue; i++) {
918 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
922 TYPED_TEST(NeedleFinderTest, Base) {
923 for (size_t i = 0; i < 32; ++i) {
924 for (int j = 0; j < 32; ++j) {
925 string s = string(i, 'X') + "abca" + string(i, 'X');
926 string delims = string(j, 'Y') + "a" + string(j, 'Y');
927 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
932 const size_t kPageSize = 4096;
933 // Updates contents so that any read accesses past the last byte will
934 // cause a SIGSEGV. It accomplishes this by changing access to the page that
935 // begins immediately after the end of the contents (as allocators and mmap()
936 // all operate on page boundaries, this is a reasonable assumption).
937 // This function will also initialize buf, which caller must free().
938 void createProtectedBuf(StringPiece& contents, char** buf) {
939 ASSERT_LE(contents.size(), kPageSize);
940 const size_t kSuccess = 0;
941 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
944 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
945 size_t newBegin = kPageSize - contents.size();
946 memcpy(*buf + newBegin, contents.data(), contents.size());
947 contents.reset(*buf + newBegin, contents.size());
950 void freeProtectedBuf(char* buf) {
951 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
955 TYPED_TEST(NeedleFinderTest, NoSegFault) {
956 const string base = string(32, 'a') + string("b");
957 const string delims = string(32, 'c') + string("b");
958 for (int i = 0; i <= 32; i++) {
959 for (int j = 0; j <= 33; j++) {
960 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
961 StringPiece s1(base);
963 ASSERT_TRUE(!s1.empty());
967 StringPiece s2(delims);
971 createProtectedBuf(s1, &buf1);
972 createProtectedBuf(s2, &buf2);
973 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
974 // string(s1.data(), s1.size()).c_str(), s1.size(),
975 // string(s2.data(), s2.size()).c_str(), s2.size());
976 auto r1 = this->find_first_byte_of(s1, s2);
977 auto f1 = std::find_first_of(s1.begin(), s1.end(),
978 s2.begin(), s2.end());
979 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
981 auto r2 = this->find_first_byte_of(s2, s1);
982 auto f2 = std::find_first_of(s2.begin(), s2.end(),
983 s1.begin(), s1.end());
984 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
986 freeProtectedBuf(buf1);
987 freeProtectedBuf(buf2);
993 TEST(NonConstTest, StringPiece) {
994 std::string hello("hello");
995 MutableStringPiece sp(&hello.front(), hello.size());
997 EXPECT_EQ("xello", hello);
1000 EXPECT_EQ("xello", s);
1004 MutableByteRange r2(sp);
1009 void testRangeFunc(C&& x, size_t n) {
1011 // type, conversion checks
1012 Range<int*> r1 = range(std::forward<C>(x));
1013 Range<const int*> r2 = range(std::forward<C>(x));
1014 Range<const int*> r3 = range(cx);
1015 Range<const int*> r5 = range(std::move(cx));
1016 EXPECT_EQ(r1.begin(), &x[0]);
1017 EXPECT_EQ(r1.end(), &x[n]);
1018 EXPECT_EQ(n, r1.size());
1019 EXPECT_EQ(n, r2.size());
1020 EXPECT_EQ(n, r3.size());
1021 EXPECT_EQ(n, r5.size());
1024 TEST(RangeFunc, Vector) {
1026 testRangeFunc(x, 0);
1028 testRangeFunc(x, 1);
1029 testRangeFunc(std::vector<int>{1, 2}, 2);
1032 TEST(RangeFunc, Array) {
1033 std::array<int, 3> x;
1034 testRangeFunc(x, 3);
1037 TEST(RangeFunc, CArray) {
1038 int x[] {1, 2, 3, 4};
1039 testRangeFunc(x, 4);
1042 std::string get_rand_str(size_t size,
1043 std::uniform_int_distribution<>& dist,
1044 std::mt19937& gen) {
1045 std::string ret(size, '\0');
1046 for (size_t i = 0; i < size; ++i) {
1047 ret[i] = static_cast<char>(dist(gen));
1054 bool operator==(MutableStringPiece mp, StringPiece sp) {
1055 return mp.compare(sp) == 0;
1058 bool operator==(StringPiece sp, MutableStringPiece mp) {
1059 return mp.compare(sp) == 0;
1063 TEST(ReplaceAt, exhaustiveTest) {
1064 char input[] = "this is nice and long input";
1065 auto msp = MutableStringPiece(input);
1066 auto str = std::string(input);
1067 std::random_device rd;
1068 std::mt19937 gen(rd());
1069 std::uniform_int_distribution<> dist('a', 'z');
1071 for (int i=0; i < 100; ++i) {
1072 for (size_t j = 1; j <= msp.size(); ++j) {
1073 auto replacement = get_rand_str(j, dist, gen);
1074 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1075 msp.replaceAt(pos, replacement);
1076 str.replace(pos, replacement.size(), replacement);
1077 EXPECT_EQ(msp.compare(str), 0);
1083 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1086 TEST(ReplaceAll, basicTest) {
1087 char input[] = "this is nice and long input";
1088 auto orig = std::string(input);
1089 auto msp = MutableStringPiece(input);
1091 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1092 EXPECT_EQ("thsi si nice and long input", msp);
1093 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1094 EXPECT_EQ(msp, orig);
1096 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1097 EXPECT_EQ(msp, orig);
1099 // at the very beginning
1100 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1101 EXPECT_EQ("siht is nice and long input", msp);
1102 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1103 EXPECT_EQ(msp, orig);
1106 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1107 EXPECT_EQ("this is nice and long soput", msp);
1108 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1109 EXPECT_EQ(msp, orig);
1112 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1113 EXPECT_EQ("this@is@nice@and@long@input", msp);
1114 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1115 EXPECT_EQ(msp, orig);
1118 TEST(ReplaceAll, randomTest) {
1119 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1120 auto orig = std::string(input);
1121 auto msp = MutableStringPiece(input);
1123 std::random_device rd;
1124 std::mt19937 gen(rd());
1125 std::uniform_int_distribution<> dist('A', 'Z');
1127 for (int i=0; i < 100; ++i) {
1128 for (size_t j = 1; j <= orig.size(); ++j) {
1129 auto replacement = get_rand_str(j, dist, gen);
1130 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1131 auto piece = orig.substr(pos, j);
1132 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1133 EXPECT_EQ(msp.find(replacement), pos);
1134 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1135 EXPECT_EQ(msp, orig);
1141 TEST(ReplaceAll, BadArg) {
1143 auto fst = "longer";
1145 char input[] = "meh meh meh";
1146 auto all = MutableStringPiece(input);
1149 all.replaceAll(fst, snd);
1150 } catch (std::invalid_argument&) {
1155 all.replaceAll(snd, fst);
1156 } catch (std::invalid_argument&) {
1160 EXPECT_EQ(count, 2);
1163 TEST(Range, Constructors) {
1164 vector<int> c = {1, 2, 3};
1165 typedef Range<vector<int>::iterator> RangeType;
1166 typedef Range<vector<int>::const_iterator> ConstRangeType;
1167 RangeType cr(c.begin(), c.end());
1168 auto subpiece1 = ConstRangeType(cr, 1, 5);
1169 auto subpiece2 = ConstRangeType(cr, 1);
1170 EXPECT_EQ(subpiece1.size(), 2);
1171 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1172 EXPECT_EQ(subpiece1.end(), subpiece2.end());