Speed-up StringPiece::find_first_of()
[folly.git] / folly / test / RangeTest.cpp
1 /*
2  * Copyright 2013 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 //
18 // @author Kristina Holst (kholst@fb.com)
19 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
20
21 #include <limits>
22 #include <string>
23 #include <boost/range/concepts.hpp>
24 #include <gtest/gtest.h>
25 #include "folly/Range.h"
26
27 namespace folly { namespace detail {
28 // declaration of functions in Range.cpp
29 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
30                                   const StringPiece& needles);
31
32 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
33                                    const StringPiece& needles);
34
35 size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
36                                  const StringPiece& needles);
37 }}
38
39 using namespace folly;
40 using namespace std;
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   // starting pos that is obviously past the end -- This works for std::string
116   EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
117   EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
118   EXPECT_EQ(s.find('z', 55), StringPiece::npos);
119   // null char
120   EXPECT_EQ(s.find('\0'), std::string().find('\0'));
121   EXPECT_EQ(s.find('\0'), StringPiece::npos);
122
123   // find_first_of
124   s.reset(foobarbaz, strlen(foobarbaz));
125   EXPECT_EQ(s.find_first_of("bar"), 3);
126   EXPECT_EQ(s.find_first_of("ba", 3), 3);
127   EXPECT_EQ(s.find_first_of("ba", 4), 4);
128   EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
129   EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
130   // starting position too far
131   EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
132   // starting pos that is obviously past the end -- This works for std::string
133   EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
134   EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
135   EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
136   // empty needle. Note that this returns npos, while find() returns 0!
137   EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
138   EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
139
140   // single char find_first_ofs
141   EXPECT_EQ(s.find_first_of('b'), 3);
142   EXPECT_EQ(s.find_first_of('b', 3), 3);
143   EXPECT_EQ(s.find_first_of('b', 4), 6);
144   EXPECT_EQ(s.find_first_of('o', 2), 2);
145   EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
146   EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
147   // starting position too far
148   EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
149   // starting pos that is obviously past the end -- This works for std::string
150   EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
151   EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
152   EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
153   // null char
154   EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
155   EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
156
157   // just "barbaz"
158   s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
159   EXPECT_EQ(s.size(), 6);
160   EXPECT_EQ(s.start(), foobarbaz + 3);
161   EXPECT_EQ(s, "barbaz");
162
163   // just "bar"
164   s.reset(foobarbaz + 3, 3);
165   EXPECT_EQ(s.size(), 3);
166   EXPECT_EQ(s, "bar");
167
168   // clear
169   s.clear();
170   EXPECT_EQ(s.toString(), "");
171
172   // test an empty StringPiece
173   StringPiece s2;
174   EXPECT_EQ(s2.size(), 0);
175
176   // Test comparison operators
177   foo = "";
178   EXPECT_LE(s, foo);
179   EXPECT_LE(foo, s);
180   EXPECT_GE(s, foo);
181   EXPECT_GE(foo, s);
182   EXPECT_EQ(s, foo);
183   EXPECT_EQ(foo, s);
184
185   foo = "abc";
186   EXPECT_LE(s, foo);
187   EXPECT_LT(s, foo);
188   EXPECT_GE(foo, s);
189   EXPECT_GT(foo, s);
190   EXPECT_NE(s, foo);
191
192   EXPECT_LE(s, s);
193   EXPECT_LE(s, s);
194   EXPECT_GE(s, s);
195   EXPECT_GE(s, s);
196   EXPECT_EQ(s, s);
197   EXPECT_EQ(s, s);
198
199   s = "abc";
200   s2 = "abc";
201   EXPECT_LE(s, s2);
202   EXPECT_LE(s2, s);
203   EXPECT_GE(s, s2);
204   EXPECT_GE(s2, s);
205   EXPECT_EQ(s, s2);
206   EXPECT_EQ(s2, s);
207 }
208
209 TEST(StringPiece, ToByteRange) {
210   StringPiece a("hello");
211   ByteRange b(a);
212   EXPECT_EQ(static_cast<const void*>(a.begin()),
213             static_cast<const void*>(b.begin()));
214   EXPECT_EQ(static_cast<const void*>(a.end()),
215             static_cast<const void*>(b.end()));
216
217   // and convert back again
218   StringPiece c(b);
219   EXPECT_EQ(a.begin(), c.begin());
220   EXPECT_EQ(a.end(), c.end());
221 }
222
223 template <typename NeedleFinder>
224 class NeedleFinderTest : public ::testing::Test {
225  public:
226   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
227     return NeedleFinder::find_first_byte_of(haystack, needles);
228   }
229 };
230
231 struct SseNeedleFinder {
232   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
233     // This will only use the SSE version if it is supported on this CPU
234     // (selected using ifunc).
235     return detail::qfind_first_byte_of(haystack, needles);
236   }
237 };
238
239 struct NoSseNeedleFinder {
240   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
241     return detail::qfind_first_byte_of_nosse(haystack, needles);
242   }
243 };
244
245 struct MemchrNeedleFinder {
246   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
247     return detail::qfind_first_byte_of_memchr(haystack, needles);
248   }
249 };
250
251 struct ByteSetNeedleFinder {
252   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
253     return detail::qfind_first_byte_of_byteset(haystack, needles);
254   }
255 };
256
257 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
258                          ByteSetNeedleFinder> NeedleFinders;
259 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
260
261 TYPED_TEST(NeedleFinderTest, Null) {
262   { // null characters in the string
263     string s(10, char(0));
264     s[5] = 'b';
265     string delims("abc");
266     EXPECT_EQ(5, this->find_first_byte_of(s, delims));
267   }
268   { // null characters in delim
269     string s("abc");
270     string delims(10, char(0));
271     delims[3] = 'c';
272     delims[7] = 'b';
273     EXPECT_EQ(1, this->find_first_byte_of(s, delims));
274   }
275   { // range not terminated by null character
276     string buf = "abcdefghijklmnopqrstuvwxyz";
277     StringPiece s(buf.data() + 5, 3);
278     StringPiece delims("z");
279     EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
280   }
281 }
282
283 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
284   string delims(1000, 'b');
285   EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
286   EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
287 }
288
289 TYPED_TEST(NeedleFinderTest, Empty) {
290   string a = "abc";
291   string b = "";
292   EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
293   EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
294   EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
295 }
296
297 TYPED_TEST(NeedleFinderTest, Unaligned) {
298   // works correctly even if input buffers are not 16-byte aligned
299   string s = "0123456789ABCDEFGH";
300   for (int i = 0; i < s.size(); ++i) {
301     StringPiece a(s.c_str() + i);
302     for (int j = 0; j < s.size(); ++j) {
303       StringPiece b(s.c_str() + j);
304       EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
305     }
306   }
307 }
308
309 // for some algorithms (specifically those that create a set of needles),
310 // we check for the edge-case of _all_ possible needles being sought.
311 TYPED_TEST(NeedleFinderTest, Needles256) {
312   string needles;
313   const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
314   const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
315   // make the size ~big to avoid any edge-case branches for tiny haystacks
316   const int haystackSize = 50;
317   for (int i = minValue; i <= maxValue; i++) {  // <=
318     needles.push_back(i);
319   }
320   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
321   for (int i = minValue; i <= maxValue; i++) {
322     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
323   }
324
325   needles.append("these are redundant characters");
326   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
327   for (int i = minValue; i <= maxValue; i++) {
328     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
329   }
330 }
331
332 TYPED_TEST(NeedleFinderTest, Base) {
333   for (int i = 0; i < 32; ++i) {
334     for (int j = 0; j < 32; ++j) {
335       string s = string(i, 'X') + "abca" + string(i, 'X');
336       string delims = string(j, 'Y') + "a" + string(j, 'Y');
337       EXPECT_EQ(i, this->find_first_byte_of(s, delims));
338     }
339   }
340 }