Fix a typo in portability/Memory.cpp
[folly.git] / folly / test / RangeTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author Kristina Holst (kholst@fb.com)
18 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
19
20 #include <folly/Range.h>
21
22 #include <folly/portability/Memory.h>
23
24 #include <sys/mman.h>
25 #include <array>
26 #include <iterator>
27 #include <limits>
28 #include <random>
29 #include <string>
30 #include <type_traits>
31 #include <vector>
32 #include <boost/range/concepts.hpp>
33 #include <boost/algorithm/string/trim.hpp>
34 #include <gtest/gtest.h>
35
36 using namespace folly;
37 using namespace folly::detail;
38 using namespace std;
39
40 static_assert(std::is_literal_type<StringPiece>::value, "");
41
42 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
43
44 TEST(StringPiece, All) {
45   const char* foo = "foo";
46   const char* foo2 = "foo";
47   string fooStr(foo);
48   string foo2Str(foo2);
49
50   // we expect the compiler to optimize things so that there's only one copy
51   // of the string literal "foo", even though we've got it in multiple places
52   EXPECT_EQ(foo, foo2);  // remember, this uses ==, not strcmp, so it's a ptr
53                          // comparison rather than lexical
54
55   // the string object creates copies though, so the c_str of these should be
56   // distinct
57   EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
58
59   // test the basic StringPiece functionality
60   StringPiece s(foo);
61   EXPECT_EQ(s.size(), 3);
62
63   EXPECT_EQ(s.start(), foo);              // ptr comparison
64   EXPECT_NE(s.start(), fooStr.c_str());   // ptr comparison
65   EXPECT_NE(s.start(), foo2Str.c_str());  // ptr comparison
66
67   EXPECT_EQ(s.toString(), foo);              // lexical comparison
68   EXPECT_EQ(s.toString(), fooStr.c_str());   // lexical comparison
69   EXPECT_EQ(s.toString(), foo2Str.c_str());  // lexical comparison
70
71   EXPECT_EQ(s, foo);                      // lexical comparison
72   EXPECT_EQ(s, fooStr);                   // lexical comparison
73   EXPECT_EQ(s, foo2Str);                  // lexical comparison
74   EXPECT_EQ(foo, s);
75
76   // check using StringPiece to reference substrings
77   const char* foobarbaz = "foobarbaz";
78
79   // the full "foobarbaz"
80   s.reset(foobarbaz, strlen(foobarbaz));
81   EXPECT_EQ(s.size(), 9);
82   EXPECT_EQ(s.start(), foobarbaz);
83   EXPECT_EQ(s, "foobarbaz");
84
85   // only the 'foo'
86   s.assign(foobarbaz, foobarbaz + 3);
87   EXPECT_EQ(s.size(), 3);
88   EXPECT_EQ(s.start(), foobarbaz);
89   EXPECT_EQ(s, "foo");
90
91   // find
92   s.reset(foobarbaz, strlen(foobarbaz));
93   EXPECT_EQ(s.find("bar"), 3);
94   EXPECT_EQ(s.find("ba", 3), 3);
95   EXPECT_EQ(s.find("ba", 4), 6);
96   EXPECT_EQ(s.find("notfound"), StringPiece::npos);
97   EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
98   EXPECT_EQ(s.find("bar", 4), StringPiece::npos);  // starting position too far
99   // starting pos that is obviously past the end -- This works for std::string
100   EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
101   EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
102   EXPECT_EQ(s.find("z", 55), StringPiece::npos);
103   // empty needle
104   EXPECT_EQ(s.find(""), std::string().find(""));
105   EXPECT_EQ(s.find(""), 0);
106
107   // single char finds
108   EXPECT_EQ(s.find('b'), 3);
109   EXPECT_EQ(s.find('b', 3), 3);
110   EXPECT_EQ(s.find('b', 4), 6);
111   EXPECT_EQ(s.find('o', 2), 2);
112   EXPECT_EQ(s.find('y'), StringPiece::npos);
113   EXPECT_EQ(s.find('y', 1), StringPiece::npos);
114   EXPECT_EQ(s.find('o', 4), StringPiece::npos);  // starting position too far
115   EXPECT_TRUE(s.contains('z'));
116   // starting pos that is obviously past the end -- This works for std::string
117   EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
118   EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
119   EXPECT_EQ(s.find('z', 55), StringPiece::npos);
120   // null char
121   EXPECT_EQ(s.find('\0'), std::string().find('\0'));
122   EXPECT_EQ(s.find('\0'), StringPiece::npos);
123   EXPECT_FALSE(s.contains('\0'));
124
125   // single char rfinds
126   EXPECT_EQ(s.rfind('b'), 6);
127   EXPECT_EQ(s.rfind('y'), StringPiece::npos);
128   EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
129   EXPECT_EQ(ByteRange(s).rfind('b'), 6);
130   EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
131   // null char
132   EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
133   EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
134
135   // find_first_of
136   s.reset(foobarbaz, strlen(foobarbaz));
137   EXPECT_EQ(s.find_first_of("bar"), 3);
138   EXPECT_EQ(s.find_first_of("ba", 3), 3);
139   EXPECT_EQ(s.find_first_of("ba", 4), 4);
140   EXPECT_TRUE(s.contains("bar"));
141   EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
142   EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
143   EXPECT_FALSE(s.contains("xyxy"));
144   // starting position too far
145   EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
146   // starting pos that is obviously past the end -- This works for std::string
147   EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
148   EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
149   EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
150   // empty needle. Note that this returns npos, while find() returns 0!
151   EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
152   EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
153
154   // single char find_first_ofs
155   EXPECT_EQ(s.find_first_of('b'), 3);
156   EXPECT_EQ(s.find_first_of('b', 3), 3);
157   EXPECT_EQ(s.find_first_of('b', 4), 6);
158   EXPECT_EQ(s.find_first_of('o', 2), 2);
159   EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
160   EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
161   // starting position too far
162   EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
163   // starting pos that is obviously past the end -- This works for std::string
164   EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
165   EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
166   EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
167   // null char
168   EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
169   EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
170
171   // just "barbaz"
172   s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
173   EXPECT_EQ(s.size(), 6);
174   EXPECT_EQ(s.start(), foobarbaz + 3);
175   EXPECT_EQ(s, "barbaz");
176
177   // just "bar"
178   s.reset(foobarbaz + 3, 3);
179   EXPECT_EQ(s.size(), 3);
180   EXPECT_EQ(s, "bar");
181
182   // clear
183   s.clear();
184   EXPECT_EQ(s.toString(), "");
185
186   // test an empty StringPiece
187   StringPiece s2;
188   EXPECT_EQ(s2.size(), 0);
189
190   // Test comparison operators
191   foo = "";
192   EXPECT_LE(s, foo);
193   EXPECT_LE(foo, s);
194   EXPECT_GE(s, foo);
195   EXPECT_GE(foo, s);
196   EXPECT_EQ(s, foo);
197   EXPECT_EQ(foo, s);
198
199   foo = "abc";
200   EXPECT_LE(s, foo);
201   EXPECT_LT(s, foo);
202   EXPECT_GE(foo, s);
203   EXPECT_GT(foo, s);
204   EXPECT_NE(s, foo);
205
206   EXPECT_LE(s, s);
207   EXPECT_LE(s, s);
208   EXPECT_GE(s, s);
209   EXPECT_GE(s, s);
210   EXPECT_EQ(s, s);
211   EXPECT_EQ(s, s);
212
213   s = "abc";
214   s2 = "abc";
215   EXPECT_LE(s, s2);
216   EXPECT_LE(s2, s);
217   EXPECT_GE(s, s2);
218   EXPECT_GE(s2, s);
219   EXPECT_EQ(s, s2);
220   EXPECT_EQ(s2, s);
221 }
222
223 template <class T>
224 void expectLT(const T& a, const T& b) {
225   EXPECT_TRUE(a < b);
226   EXPECT_TRUE(a <= b);
227   EXPECT_FALSE(a == b);
228   EXPECT_FALSE(a >= b);
229   EXPECT_FALSE(a > b);
230
231   EXPECT_FALSE(b < a);
232   EXPECT_FALSE(b <= a);
233   EXPECT_TRUE(b >= a);
234   EXPECT_TRUE(b > a);
235 }
236
237 template <class T>
238 void expectEQ(const T& a, const T& b) {
239   EXPECT_FALSE(a < b);
240   EXPECT_TRUE(a <= b);
241   EXPECT_TRUE(a == b);
242   EXPECT_TRUE(a >= b);
243   EXPECT_FALSE(a > b);
244 }
245
246 TEST(StringPiece, EightBitComparisons) {
247   char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
248   constexpr size_t count = sizeof(values) / sizeof(values[0]);
249   for (size_t i = 0; i < count; ++i) {
250     std::string a(1, values[i]);
251     // Defeat copy-on-write
252     std::string aCopy(a.data(), a.size());
253     expectEQ(a, aCopy);
254     expectEQ(StringPiece(a), StringPiece(aCopy));
255
256     for (size_t j = i + 1; j < count; ++j) {
257       std::string b(1, values[j]);
258       expectLT(a, b);
259       expectLT(StringPiece(a), StringPiece(b));
260     }
261   }
262 }
263
264 TEST(StringPiece, ToByteRange) {
265   StringPiece a("hello");
266   ByteRange b(a);
267   EXPECT_EQ(static_cast<const void*>(a.begin()),
268             static_cast<const void*>(b.begin()));
269   EXPECT_EQ(static_cast<const void*>(a.end()),
270             static_cast<const void*>(b.end()));
271
272   // and convert back again
273   StringPiece c(b);
274   EXPECT_EQ(a.begin(), c.begin());
275   EXPECT_EQ(a.end(), c.end());
276 }
277
278 TEST(StringPiece, InvalidRange) {
279   StringPiece a("hello");
280   EXPECT_EQ(a, a.subpiece(0, 10));
281   EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
282   EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
283   EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
284   EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
285   EXPECT_THROW(a.subpiece(6), std::out_of_range);
286
287   std::string b("hello");
288   EXPECT_EQ(a, StringPiece(b, 0, 10));
289   EXPECT_EQ("ello", a.subpiece(1));
290   EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
291   EXPECT_EQ("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);
294 }
295
296 constexpr char helloArray[] = "hello";
297
298 TEST(StringPiece, Constexpr) {
299   constexpr StringPiece hello1("hello");
300   EXPECT_EQ("hello", hello1);
301
302   constexpr StringPiece hello2(helloArray);
303   EXPECT_EQ("hello", hello2);
304 }
305
306 TEST(StringPiece, Prefix) {
307   StringPiece a("hello");
308   EXPECT_TRUE(a.startsWith(""));
309   EXPECT_TRUE(a.startsWith("h"));
310   EXPECT_TRUE(a.startsWith('h'));
311   EXPECT_TRUE(a.startsWith("hello"));
312   EXPECT_FALSE(a.startsWith("hellox"));
313   EXPECT_FALSE(a.startsWith('x'));
314   EXPECT_FALSE(a.startsWith("x"));
315
316   {
317     auto b = a;
318     EXPECT_TRUE(b.removePrefix(""));
319     EXPECT_EQ("hello", b);
320   }
321   {
322     auto b = a;
323     EXPECT_TRUE(b.removePrefix("h"));
324     EXPECT_EQ("ello", b);
325   }
326   {
327     auto b = a;
328     EXPECT_TRUE(b.removePrefix('h'));
329     EXPECT_EQ("ello", b);
330   }
331   {
332     auto b = a;
333     EXPECT_TRUE(b.removePrefix("hello"));
334     EXPECT_EQ("", b);
335   }
336   {
337     auto b = a;
338     EXPECT_FALSE(b.removePrefix("hellox"));
339     EXPECT_EQ("hello", b);
340   }
341   {
342     auto b = a;
343     EXPECT_FALSE(b.removePrefix("x"));
344     EXPECT_EQ("hello", b);
345   }
346   {
347     auto b = a;
348     EXPECT_FALSE(b.removePrefix('x'));
349     EXPECT_EQ("hello", b);
350   }
351 }
352
353 TEST(StringPiece, Suffix) {
354   StringPiece a("hello");
355   EXPECT_TRUE(a.endsWith(""));
356   EXPECT_TRUE(a.endsWith("o"));
357   EXPECT_TRUE(a.endsWith('o'));
358   EXPECT_TRUE(a.endsWith("hello"));
359   EXPECT_FALSE(a.endsWith("xhello"));
360   EXPECT_FALSE(a.endsWith("x"));
361   EXPECT_FALSE(a.endsWith('x'));
362
363   {
364     auto b = a;
365     EXPECT_TRUE(b.removeSuffix(""));
366     EXPECT_EQ("hello", b);
367   }
368   {
369     auto b = a;
370     EXPECT_TRUE(b.removeSuffix("o"));
371     EXPECT_EQ("hell", b);
372   }
373   {
374     auto b = a;
375     EXPECT_TRUE(b.removeSuffix('o'));
376     EXPECT_EQ("hell", b);
377   }
378   {
379     auto b = a;
380     EXPECT_TRUE(b.removeSuffix("hello"));
381     EXPECT_EQ("", b);
382   }
383   {
384     auto b = a;
385     EXPECT_FALSE(b.removeSuffix("xhello"));
386     EXPECT_EQ("hello", b);
387   }
388   {
389     auto b = a;
390     EXPECT_FALSE(b.removeSuffix("x"));
391     EXPECT_EQ("hello", b);
392   }
393   {
394     auto b = a;
395     EXPECT_FALSE(b.removeSuffix('x'));
396     EXPECT_EQ("hello", b);
397   }
398 }
399
400 TEST(StringPiece, PrefixEmpty) {
401   StringPiece a;
402   EXPECT_TRUE(a.startsWith(""));
403   EXPECT_FALSE(a.startsWith("a"));
404   EXPECT_FALSE(a.startsWith('a'));
405   EXPECT_TRUE(a.removePrefix(""));
406   EXPECT_EQ("", a);
407   EXPECT_FALSE(a.removePrefix("a"));
408   EXPECT_EQ("", a);
409   EXPECT_FALSE(a.removePrefix('a'));
410   EXPECT_EQ("", a);
411 }
412
413 TEST(StringPiece, SuffixEmpty) {
414   StringPiece a;
415   EXPECT_TRUE(a.endsWith(""));
416   EXPECT_FALSE(a.endsWith("a"));
417   EXPECT_FALSE(a.endsWith('a'));
418   EXPECT_TRUE(a.removeSuffix(""));
419   EXPECT_EQ("", a);
420   EXPECT_FALSE(a.removeSuffix("a"));
421   EXPECT_EQ("", a);
422   EXPECT_FALSE(a.removeSuffix('a'));
423   EXPECT_EQ("", a);
424 }
425
426 TEST(StringPiece, erase) {
427   StringPiece a("hello");
428   auto b = a.begin();
429   auto e = b + 1;
430   a.erase(b, e);
431   EXPECT_EQ("ello", a);
432
433   e = a.end();
434   b = e - 1;
435   a.erase(b, e);
436   EXPECT_EQ("ell", a);
437
438   b = a.end() - 1;
439   e = a.end() - 1;
440   EXPECT_THROW(a.erase(b, e), std::out_of_range);
441
442   b = a.begin();
443   e = a.end();
444   a.erase(b, e);
445   EXPECT_EQ("", a);
446
447   a = "hello";
448   b = a.begin();
449   e = b + 2;
450   a.erase(b, e);
451   EXPECT_EQ("llo", a);
452
453   b = a.end() - 2;
454   e = a.end();
455   a.erase(b, e);
456   EXPECT_EQ("l", a);
457
458   a = "      hello  ";
459   boost::algorithm::trim(a);
460   EXPECT_EQ(a, "hello");
461 }
462
463 TEST(StringPiece, split_step_char_delimiter) {
464   //              0         1         2
465   //              012345678901234567890123456
466   auto const s = "this is just  a test string";
467   auto const e = std::next(s, std::strlen(s));
468   EXPECT_EQ('\0', *e);
469
470   folly::StringPiece p(s);
471   EXPECT_EQ(s, p.begin());
472   EXPECT_EQ(e, p.end());
473   EXPECT_EQ(s, p);
474
475   auto x = p.split_step(' ');
476   EXPECT_EQ(std::next(s, 5), p.begin());
477   EXPECT_EQ(e, p.end());
478   EXPECT_EQ("this", x);
479
480   x = p.split_step(' ');
481   EXPECT_EQ(std::next(s, 8), p.begin());
482   EXPECT_EQ(e, p.end());
483   EXPECT_EQ("is", x);
484
485   x = p.split_step('u');
486   EXPECT_EQ(std::next(s, 10), p.begin());
487   EXPECT_EQ(e, p.end());
488   EXPECT_EQ("j", x);
489
490   x = p.split_step(' ');
491   EXPECT_EQ(std::next(s, 13), p.begin());
492   EXPECT_EQ(e, p.end());
493   EXPECT_EQ("st", x);
494
495   x = p.split_step(' ');
496   EXPECT_EQ(std::next(s, 14), p.begin());
497   EXPECT_EQ(e, p.end());
498   EXPECT_EQ("", x);
499
500   x = p.split_step(' ');
501   EXPECT_EQ(std::next(s, 16), p.begin());
502   EXPECT_EQ(e, p.end());
503   EXPECT_EQ("a", x);
504
505   x = p.split_step(' ');
506   EXPECT_EQ(std::next(s, 21), p.begin());
507   EXPECT_EQ(e, p.end());
508   EXPECT_EQ("test", x);
509
510   x = p.split_step(' ');
511   EXPECT_EQ(e, p.begin());
512   EXPECT_EQ(e, p.end());
513   EXPECT_EQ("string", x);
514
515   x = p.split_step(' ');
516   EXPECT_EQ(e, p.begin());
517   EXPECT_EQ(e, p.end());
518   EXPECT_EQ("", x);
519 }
520
521 TEST(StringPiece, split_step_range_delimiter) {
522   //              0         1         2         3
523   //              0123456789012345678901234567890123
524   auto const s = "this  is  just    a   test  string";
525   auto const e = std::next(s, std::strlen(s));
526   EXPECT_EQ('\0', *e);
527
528   folly::StringPiece p(s);
529   EXPECT_EQ(s, p.begin());
530   EXPECT_EQ(e, p.end());
531   EXPECT_EQ(s, p);
532
533   auto x = p.split_step("  ");
534   EXPECT_EQ(std::next(s, 6), p.begin());
535   EXPECT_EQ(e, p.end());
536   EXPECT_EQ("this", x);
537
538   x = p.split_step("  ");
539   EXPECT_EQ(std::next(s, 10), p.begin());
540   EXPECT_EQ(e, p.end());
541   EXPECT_EQ("is", x);
542
543   x = p.split_step("u");
544   EXPECT_EQ(std::next(s, 12), p.begin());
545   EXPECT_EQ(e, p.end());
546   EXPECT_EQ("j", x);
547
548   x = p.split_step("  ");
549   EXPECT_EQ(std::next(s, 16), p.begin());
550   EXPECT_EQ(e, p.end());
551   EXPECT_EQ("st", x);
552
553   x = p.split_step("  ");
554   EXPECT_EQ(std::next(s, 18), p.begin());
555   EXPECT_EQ(e, p.end());
556   EXPECT_EQ("", x);
557
558   x = p.split_step("  ");
559   EXPECT_EQ(std::next(s, 21), p.begin());
560   EXPECT_EQ(e, p.end());
561   EXPECT_EQ("a", x);
562
563   x = p.split_step("  ");
564   EXPECT_EQ(std::next(s, 28), p.begin());
565   EXPECT_EQ(e, p.end());
566   EXPECT_EQ(" test", x);
567
568   x = p.split_step("  ");
569   EXPECT_EQ(e, p.begin());
570   EXPECT_EQ(e, p.end());
571   EXPECT_EQ("string", x);
572
573   x = p.split_step("  ");
574   EXPECT_EQ(e, p.begin());
575   EXPECT_EQ(e, p.end());
576   EXPECT_EQ("", x);
577
578   x = p.split_step(" ");
579   EXPECT_EQ(e, p.begin());
580   EXPECT_EQ(e, p.end());
581   EXPECT_EQ("", x);
582 }
583
584 void split_step_with_process_noop(folly::StringPiece) {}
585
586 TEST(StringPiece, split_step_with_process_char_delimiter) {
587   //              0         1         2
588   //              012345678901234567890123456
589   auto const s = "this is just  a test string";
590   auto const e = std::next(s, std::strlen(s));
591   EXPECT_EQ('\0', *e);
592
593   folly::StringPiece p(s);
594   EXPECT_EQ(s, p.begin());
595   EXPECT_EQ(e, p.end());
596   EXPECT_EQ(s, p);
597
598   EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
599     EXPECT_EQ(std::next(s, 5), p.begin());
600     EXPECT_EQ(e, p.end());
601     EXPECT_EQ("this", x);
602     return 1;
603   })));
604
605   EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
606     EXPECT_EQ(std::next(s, 8), p.begin());
607     EXPECT_EQ(e, p.end());
608     EXPECT_EQ("is", x);
609     return 2;
610   })));
611
612   EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
613     EXPECT_EQ(std::next(s, 10), p.begin());
614     EXPECT_EQ(e, p.end());
615     EXPECT_EQ("j", x);
616     return 3;
617   })));
618
619   EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
620     EXPECT_EQ(std::next(s, 13), p.begin());
621     EXPECT_EQ(e, p.end());
622     EXPECT_EQ("st", x);
623     return 4;
624   })));
625
626   EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
627     EXPECT_EQ(std::next(s, 14), p.begin());
628     EXPECT_EQ(e, p.end());
629     EXPECT_EQ("", x);
630     return 5;
631   })));
632
633   EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
634     EXPECT_EQ(std::next(s, 16), p.begin());
635     EXPECT_EQ(e, p.end());
636     EXPECT_EQ("a", x);
637     return 6;
638   })));
639
640   EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
641     EXPECT_EQ(std::next(s, 21), p.begin());
642     EXPECT_EQ(e, p.end());
643     EXPECT_EQ("test", x);
644     return 7;
645   })));
646
647   EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
648     EXPECT_EQ(e, p.begin());
649     EXPECT_EQ(e, p.end());
650     EXPECT_EQ("string", x);
651     return 8;
652   })));
653
654   EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
655     EXPECT_EQ(e, p.begin());
656     EXPECT_EQ(e, p.end());
657     EXPECT_EQ("", x);
658     return 9;
659   })));
660
661   EXPECT_TRUE((std::is_same<
662     void,
663     decltype(p.split_step(' ', split_step_with_process_noop))
664   >::value));
665
666   EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
667 }
668
669 TEST(StringPiece, split_step_with_process_range_delimiter) {
670   //              0         1         2         3
671   //              0123456789012345678901234567890123
672   auto const s = "this  is  just    a   test  string";
673   auto const e = std::next(s, std::strlen(s));
674   EXPECT_EQ('\0', *e);
675
676   folly::StringPiece p(s);
677   EXPECT_EQ(s, p.begin());
678   EXPECT_EQ(e, p.end());
679   EXPECT_EQ(s, p);
680
681   EXPECT_EQ(1, (p.split_step("  ", [&](folly::StringPiece x) {
682     EXPECT_EQ(std::next(s, 6), p.begin());
683     EXPECT_EQ(e, p.end());
684     EXPECT_EQ("this", x);
685     return 1;
686   })));
687
688   EXPECT_EQ(2, (p.split_step("  ", [&](folly::StringPiece x) {
689     EXPECT_EQ(std::next(s, 10), p.begin());
690     EXPECT_EQ(e, p.end());
691     EXPECT_EQ("is", x);
692     return 2;
693   })));
694
695   EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
696     EXPECT_EQ(std::next(s, 12), p.begin());
697     EXPECT_EQ(e, p.end());
698     EXPECT_EQ("j", x);
699     return 3;
700   })));
701
702   EXPECT_EQ(4, (p.split_step("  ", [&](folly::StringPiece x) {
703     EXPECT_EQ(std::next(s, 16), p.begin());
704     EXPECT_EQ(e, p.end());
705     EXPECT_EQ("st", x);
706     return 4;
707   })));
708
709   EXPECT_EQ(5, (p.split_step("  ", [&](folly::StringPiece x) {
710     EXPECT_EQ(std::next(s, 18), p.begin());
711     EXPECT_EQ(e, p.end());
712     EXPECT_EQ("", x);
713     return 5;
714   })));
715
716   EXPECT_EQ(6, (p.split_step("  ", [&](folly::StringPiece x) {
717     EXPECT_EQ(std::next(s, 21), p.begin());
718     EXPECT_EQ(e, p.end());
719     EXPECT_EQ("a", x);
720     return 6;
721   })));
722
723   EXPECT_EQ(7, (p.split_step("  ", [&](folly::StringPiece x) {
724     EXPECT_EQ(std::next(s, 28), p.begin());
725     EXPECT_EQ(e, p.end());
726     EXPECT_EQ(" test", x);
727     return 7;
728   })));
729
730   EXPECT_EQ(8, (p.split_step("  ", [&](folly::StringPiece x) {
731     EXPECT_EQ(e, p.begin());
732     EXPECT_EQ(e, p.end());
733     EXPECT_EQ("string", x);
734     return 8;
735   })));
736
737   EXPECT_EQ(9, (p.split_step("  ", [&](folly::StringPiece x) {
738     EXPECT_EQ(e, p.begin());
739     EXPECT_EQ(e, p.end());
740     EXPECT_EQ("", x);
741     return 9;
742   })));
743
744   EXPECT_EQ(10, (p.split_step("  ", [&](folly::StringPiece x) {
745     EXPECT_EQ(e, p.begin());
746     EXPECT_EQ(e, p.end());
747     EXPECT_EQ("", x);
748     return 10;
749   })));
750
751   EXPECT_TRUE((std::is_same<
752     void,
753     decltype(p.split_step(' ', split_step_with_process_noop))
754   >::value));
755
756   EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
757 }
758
759 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
760   //              0         1         2
761   //              012345678901234567890123456
762   auto const s = "this is just  a test string";
763   auto const e = std::next(s, std::strlen(s));
764   auto const delimiter = ' ';
765   EXPECT_EQ('\0', *e);
766
767   folly::StringPiece p(s);
768   EXPECT_EQ(s, p.begin());
769   EXPECT_EQ(e, p.end());
770   EXPECT_EQ(s, p);
771
772   auto const functor = [](
773     folly::StringPiece s,
774     folly::StringPiece expected
775   ) {
776     EXPECT_EQ(expected, s);
777     return expected;
778   };
779
780   auto const checker = [&](folly::StringPiece expected) {
781     EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
782   };
783
784   checker("this");
785   checker("is");
786   checker("just");
787   checker("");
788   checker("a");
789   checker("test");
790   checker("string");
791   checker("");
792   checker("");
793
794   EXPECT_TRUE(p.empty());
795 }
796
797 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
798   //              0         1         2         3
799   //              0123456789012345678901234567890123
800   auto const s = "this  is  just    a   test  string";
801   auto const e = std::next(s, std::strlen(s));
802   auto const delimiter = "  ";
803   EXPECT_EQ('\0', *e);
804
805   folly::StringPiece p(s);
806   EXPECT_EQ(s, p.begin());
807   EXPECT_EQ(e, p.end());
808   EXPECT_EQ(s, p);
809
810   auto const functor = [](
811     folly::StringPiece s,
812     folly::StringPiece expected
813   ) {
814     EXPECT_EQ(expected, s);
815     return expected;
816   };
817
818   auto const checker = [&](folly::StringPiece expected) {
819     EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
820   };
821
822   checker("this");
823   checker("is");
824   checker("just");
825   checker("");
826   checker("a");
827   checker(" test");
828   checker("string");
829   checker("");
830   checker("");
831
832   EXPECT_TRUE(p.empty());
833 }
834
835 TEST(StringPiece, NoInvalidImplicitConversions) {
836   struct IsString {
837     bool operator()(folly::Range<int*>) { return false; }
838     bool operator()(folly::StringPiece) { return true; }
839   };
840
841   std::string s = "hello";
842   EXPECT_TRUE(IsString()(s));
843 }
844
845 TEST(qfind, UInt32_Ranges) {
846   vector<uint32_t> a({1, 2, 3, 260, 5});
847   vector<uint32_t> b({2, 3, 4});
848
849   auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
850   auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
851
852   EXPECT_EQ(qfind(a_range, b_range), string::npos);
853
854   a[3] = 4;
855   EXPECT_EQ(qfind(a_range, b_range), 1);
856 }
857
858 template <typename NeedleFinder>
859 class NeedleFinderTest : public ::testing::Test {
860  public:
861   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
862     return NeedleFinder::find_first_byte_of(haystack, needles);
863   }
864 };
865
866 struct SseNeedleFinder {
867   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
868     // This will only use the SSE version if it is supported on this CPU
869     // (selected using ifunc).
870     return detail::qfind_first_byte_of(haystack, needles);
871   }
872 };
873
874 struct NoSseNeedleFinder {
875   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
876     return detail::qfind_first_byte_of_nosse(haystack, needles);
877   }
878 };
879
880 struct ByteSetNeedleFinder {
881   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
882     return detail::qfind_first_byte_of_byteset(haystack, needles);
883   }
884 };
885
886 typedef ::testing::Types<SseNeedleFinder,
887                          NoSseNeedleFinder,
888                          ByteSetNeedleFinder> NeedleFinders;
889 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
890
891 TYPED_TEST(NeedleFinderTest, Null) {
892   { // null characters in the string
893     string s(10, char(0));
894     s[5] = 'b';
895     string delims("abc");
896     EXPECT_EQ(5, this->find_first_byte_of(s, delims));
897   }
898   { // null characters in delim
899     string s("abc");
900     string delims(10, char(0));
901     delims[3] = 'c';
902     delims[7] = 'b';
903     EXPECT_EQ(1, this->find_first_byte_of(s, delims));
904   }
905   { // range not terminated by null character
906     string buf = "abcdefghijklmnopqrstuvwxyz";
907     StringPiece s(buf.data() + 5, 3);
908     StringPiece delims("z");
909     EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
910   }
911 }
912
913 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
914   string delims(1000, 'b');
915   EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
916   EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
917 }
918
919 TYPED_TEST(NeedleFinderTest, Empty) {
920   string a = "abc";
921   string b = "";
922   EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
923   EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
924   EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
925 }
926
927 TYPED_TEST(NeedleFinderTest, Unaligned) {
928   // works correctly even if input buffers are not 16-byte aligned
929   string s = "0123456789ABCDEFGH";
930   for (size_t i = 0; i < s.size(); ++i) {
931     StringPiece a(s.c_str() + i);
932     for (size_t j = 0; j < s.size(); ++j) {
933       StringPiece b(s.c_str() + j);
934       EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
935     }
936   }
937 }
938
939 // for some algorithms (specifically those that create a set of needles),
940 // we check for the edge-case of _all_ possible needles being sought.
941 TYPED_TEST(NeedleFinderTest, Needles256) {
942   string needles;
943   const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
944   const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
945   // make the size ~big to avoid any edge-case branches for tiny haystacks
946   const int haystackSize = 50;
947   for (size_t i = minValue; i <= maxValue; i++) {  // <=
948     needles.push_back(i);
949   }
950   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
951   for (size_t i = minValue; i <= maxValue; i++) {
952     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
953   }
954
955   needles.append("these are redundant characters");
956   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
957   for (size_t i = minValue; i <= maxValue; i++) {
958     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
959   }
960 }
961
962 TYPED_TEST(NeedleFinderTest, Base) {
963   for (size_t i = 0; i < 32; ++i) {
964     for (int j = 0; j < 32; ++j) {
965       string s = string(i, 'X') + "abca" + string(i, 'X');
966       string delims = string(j, 'Y') + "a" + string(j, 'Y');
967       EXPECT_EQ(i, this->find_first_byte_of(s, delims));
968     }
969   }
970 }
971
972 const size_t kPageSize = 4096;
973 // Updates contents so that any read accesses past the last byte will
974 // cause a SIGSEGV.  It accomplishes this by changing access to the page that
975 // begins immediately after the end of the contents (as allocators and mmap()
976 // all operate on page boundaries, this is a reasonable assumption).
977 // This function will also initialize buf, which caller must free().
978 void createProtectedBuf(StringPiece& contents, char** buf) {
979   ASSERT_LE(contents.size(), kPageSize);
980   const size_t kSuccess = 0;
981   char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
982   if (pageAlignedBuf == nullptr) {
983     ASSERT_FALSE(true);
984   }
985   // Protect the page after the first full page-aligned region of the
986   // malloc'ed buffer
987   mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
988   size_t newBegin = kPageSize - contents.size();
989   memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
990   contents.reset(pageAlignedBuf + newBegin, contents.size());
991   *buf = pageAlignedBuf;
992 }
993
994 void freeProtectedBuf(char* buf) {
995   mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
996   aligned_free(buf);
997 }
998
999 TYPED_TEST(NeedleFinderTest, NoSegFault) {
1000   const string base = string(32, 'a') + string("b");
1001   const string delims = string(32, 'c') + string("b");
1002   for (int i = 0; i <= 32; i++) {
1003     for (int j = 0; j <= 33; j++) {
1004       for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1005         StringPiece s1(base);
1006         s1.advance(i);
1007         ASSERT_TRUE(!s1.empty());
1008         if (!shouldFind) {
1009           s1.pop_back();
1010         }
1011         StringPiece s2(delims);
1012         s2.advance(j);
1013         char* buf1;
1014         char* buf2;
1015         createProtectedBuf(s1, &buf1);
1016         createProtectedBuf(s2, &buf2);
1017         // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
1018         //        string(s1.data(), s1.size()).c_str(), s1.size(),
1019         //        string(s2.data(), s2.size()).c_str(), s2.size());
1020         auto r1 = this->find_first_byte_of(s1, s2);
1021         auto f1 = std::find_first_of(s1.begin(), s1.end(),
1022                                      s2.begin(), s2.end());
1023         auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
1024         EXPECT_EQ(r1, e1);
1025         auto r2 = this->find_first_byte_of(s2, s1);
1026         auto f2 = std::find_first_of(s2.begin(), s2.end(),
1027                                      s1.begin(), s1.end());
1028         auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1029         EXPECT_EQ(r2, e2);
1030         freeProtectedBuf(buf1);
1031         freeProtectedBuf(buf2);
1032       }
1033     }
1034   }
1035 }
1036
1037 TEST(NonConstTest, StringPiece) {
1038   std::string hello("hello");
1039   MutableStringPiece sp(&hello.front(), hello.size());
1040   sp[0] = 'x';
1041   EXPECT_EQ("xello", hello);
1042   {
1043     StringPiece s(sp);
1044     EXPECT_EQ("xello", s);
1045   }
1046   {
1047     ByteRange r1(sp);
1048     MutableByteRange r2(sp);
1049   }
1050 }
1051
1052 template<class C>
1053 void testRangeFunc(C&& x, size_t n) {
1054   const auto& cx = x;
1055   // type, conversion checks
1056   Range<int*> r1 = range(std::forward<C>(x));
1057   Range<const int*> r2 = range(std::forward<C>(x));
1058   Range<const int*> r3 = range(cx);
1059   Range<const int*> r5 = range(std::move(cx));
1060   EXPECT_EQ(r1.begin(), &x[0]);
1061   EXPECT_EQ(r1.end(), &x[n]);
1062   EXPECT_EQ(n, r1.size());
1063   EXPECT_EQ(n, r2.size());
1064   EXPECT_EQ(n, r3.size());
1065   EXPECT_EQ(n, r5.size());
1066 }
1067
1068 TEST(RangeFunc, Vector) {
1069   std::vector<int> x;
1070   testRangeFunc(x, 0);
1071   x.push_back(2);
1072   testRangeFunc(x, 1);
1073   testRangeFunc(std::vector<int>{1, 2}, 2);
1074 }
1075
1076 TEST(RangeFunc, Array) {
1077   std::array<int, 3> x;
1078   testRangeFunc(x, 3);
1079 }
1080
1081 TEST(RangeFunc, CArray) {
1082   int x[] {1, 2, 3, 4};
1083   testRangeFunc(x, 4);
1084 }
1085
1086 std::string get_rand_str(size_t size,
1087                          std::uniform_int_distribution<>& dist,
1088                          std::mt19937& gen) {
1089   std::string ret(size, '\0');
1090   for (size_t i = 0; i < size; ++i) {
1091     ret[i] = static_cast<char>(dist(gen));
1092   }
1093
1094   return ret;
1095 }
1096
1097 namespace folly {
1098 bool operator==(MutableStringPiece mp, StringPiece sp) {
1099   return mp.compare(sp) == 0;
1100 }
1101
1102 bool operator==(StringPiece sp, MutableStringPiece mp) {
1103   return mp.compare(sp) == 0;
1104 }
1105 }
1106
1107 TEST(ReplaceAt, exhaustiveTest) {
1108   char input[] = "this is nice and long input";
1109   auto msp = MutableStringPiece(input);
1110   auto str = std::string(input);
1111   std::random_device rd;
1112   std::mt19937 gen(rd());
1113   std::uniform_int_distribution<> dist('a', 'z');
1114
1115   for (int i=0; i < 100; ++i) {
1116     for (size_t j = 1; j <= msp.size(); ++j) {
1117       auto replacement = get_rand_str(j, dist, gen);
1118       for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1119         msp.replaceAt(pos, replacement);
1120         str.replace(pos, replacement.size(), replacement);
1121         EXPECT_EQ(msp.compare(str), 0);
1122       }
1123     }
1124   }
1125
1126   // too far
1127   EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1128 }
1129
1130 TEST(ReplaceAll, basicTest) {
1131   char input[] = "this is nice and long input";
1132   auto orig = std::string(input);
1133   auto msp = MutableStringPiece(input);
1134
1135   EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1136   EXPECT_EQ("thsi si nice and long input", msp);
1137   EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1138   EXPECT_EQ(msp, orig);
1139
1140   EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1141   EXPECT_EQ(msp, orig);
1142
1143   // at the very beginning
1144   EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1145   EXPECT_EQ("siht is nice and long input", msp);
1146   EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1147   EXPECT_EQ(msp, orig);
1148
1149   // at the very end
1150   EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1151   EXPECT_EQ("this is nice and long soput", msp);
1152   EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1153   EXPECT_EQ(msp, orig);
1154
1155   // all spaces
1156   EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1157   EXPECT_EQ("this@is@nice@and@long@input", msp);
1158   EXPECT_EQ(msp.replaceAll("@", " "), 5);
1159   EXPECT_EQ(msp, orig);
1160 }
1161
1162 TEST(ReplaceAll, randomTest) {
1163   char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1164   auto orig = std::string(input);
1165   auto msp = MutableStringPiece(input);
1166
1167   std::random_device rd;
1168   std::mt19937 gen(rd());
1169   std::uniform_int_distribution<> dist('A', 'Z');
1170
1171   for (int i=0; i < 100; ++i) {
1172     for (size_t j = 1; j <= orig.size(); ++j) {
1173       auto replacement = get_rand_str(j, dist, gen);
1174       for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1175         auto piece = orig.substr(pos, j);
1176         EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1177         EXPECT_EQ(msp.find(replacement), pos);
1178         EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1179         EXPECT_EQ(msp, orig);
1180       }
1181     }
1182   }
1183 }
1184
1185 TEST(ReplaceAll, BadArg) {
1186   int count = 0;
1187   auto fst = "longer";
1188   auto snd = "small";
1189   char input[] = "meh meh meh";
1190   auto all =  MutableStringPiece(input);
1191
1192   try {
1193     all.replaceAll(fst, snd);
1194   } catch (std::invalid_argument&) {
1195     ++count;
1196   }
1197
1198   try {
1199     all.replaceAll(snd, fst);
1200   } catch (std::invalid_argument&) {
1201     ++count;
1202   }
1203
1204   EXPECT_EQ(count, 2);
1205 }
1206
1207 TEST(Range, Constructors) {
1208   vector<int> c = {1, 2, 3};
1209   typedef Range<vector<int>::iterator> RangeType;
1210   typedef Range<vector<int>::const_iterator> ConstRangeType;
1211   RangeType cr(c.begin(), c.end());
1212   auto subpiece1 = ConstRangeType(cr, 1, 5);
1213   auto subpiece2 = ConstRangeType(cr, 1);
1214   EXPECT_EQ(subpiece1.size(), 2);
1215   EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1216   EXPECT_EQ(subpiece1.end(), subpiece2.end());
1217 }