qfind_first_byte_of may suffer from global initialization order
[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 // @author Kristina Holst (kholst@fb.com)
18 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
19
20 #include <limits>
21 #include <string>
22 #include <boost/range/concepts.hpp>
23 #include <gtest/gtest.h>
24 #include "folly/Range.h"
25
26 namespace folly { namespace detail {
27
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 }}  // namespaces
36
37 using namespace folly;
38 using namespace std;
39
40 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
41
42 TEST(StringPiece, All) {
43   const char* foo = "foo";
44   const char* foo2 = "foo";
45   string fooStr(foo);
46   string foo2Str(foo2);
47
48   // we expect the compiler to optimize things so that there's only one copy
49   // of the string literal "foo", even though we've got it in multiple places
50   EXPECT_EQ(foo, foo2);  // remember, this uses ==, not strcmp, so it's a ptr
51                          // comparison rather than lexical
52
53   // the string object creates copies though, so the c_str of these should be
54   // distinct
55   EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
56
57   // test the basic StringPiece functionality
58   StringPiece s(foo);
59   EXPECT_EQ(s.size(), 3);
60
61   EXPECT_EQ(s.start(), foo);              // ptr comparison
62   EXPECT_NE(s.start(), fooStr.c_str());   // ptr comparison
63   EXPECT_NE(s.start(), foo2Str.c_str());  // ptr comparison
64
65   EXPECT_EQ(s.toString(), foo);              // lexical comparison
66   EXPECT_EQ(s.toString(), fooStr.c_str());   // lexical comparison
67   EXPECT_EQ(s.toString(), foo2Str.c_str());  // lexical comparison
68
69   EXPECT_EQ(s, foo);                      // lexical comparison
70   EXPECT_EQ(s, fooStr);                   // lexical comparison
71   EXPECT_EQ(s, foo2Str);                  // lexical comparison
72   EXPECT_EQ(foo, s);
73
74   // check using StringPiece to reference substrings
75   const char* foobarbaz = "foobarbaz";
76
77   // the full "foobarbaz"
78   s.reset(foobarbaz, strlen(foobarbaz));
79   EXPECT_EQ(s.size(), 9);
80   EXPECT_EQ(s.start(), foobarbaz);
81   EXPECT_EQ(s, "foobarbaz");
82
83   // only the 'foo'
84   s.assign(foobarbaz, foobarbaz + 3);
85   EXPECT_EQ(s.size(), 3);
86   EXPECT_EQ(s.start(), foobarbaz);
87   EXPECT_EQ(s, "foo");
88
89   // find
90   s.reset(foobarbaz, strlen(foobarbaz));
91   EXPECT_EQ(s.find("bar"), 3);
92   EXPECT_EQ(s.find("ba", 3), 3);
93   EXPECT_EQ(s.find("ba", 4), 6);
94   EXPECT_EQ(s.find("notfound"), StringPiece::npos);
95   EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
96   EXPECT_EQ(s.find("bar", 4), StringPiece::npos);  // starting position too far
97   // starting pos that is obviously past the end -- This works for std::string
98   EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
99   EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
100   EXPECT_EQ(s.find("z", 55), StringPiece::npos);
101   // empty needle
102   EXPECT_EQ(s.find(""), std::string().find(""));
103   EXPECT_EQ(s.find(""), 0);
104
105   // single char finds
106   EXPECT_EQ(s.find('b'), 3);
107   EXPECT_EQ(s.find('b', 3), 3);
108   EXPECT_EQ(s.find('b', 4), 6);
109   EXPECT_EQ(s.find('o', 2), 2);
110   EXPECT_EQ(s.find('y'), StringPiece::npos);
111   EXPECT_EQ(s.find('y', 1), StringPiece::npos);
112   EXPECT_EQ(s.find('o', 4), StringPiece::npos);  // starting position too far
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);
117   // null char
118   EXPECT_EQ(s.find('\0'), std::string().find('\0'));
119   EXPECT_EQ(s.find('\0'), StringPiece::npos);
120
121   // find_first_of
122   s.reset(foobarbaz, strlen(foobarbaz));
123   EXPECT_EQ(s.find_first_of("bar"), 3);
124   EXPECT_EQ(s.find_first_of("ba", 3), 3);
125   EXPECT_EQ(s.find_first_of("ba", 4), 4);
126   EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
127   EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
128   // starting position too far
129   EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
130   // starting pos that is obviously past the end -- This works for std::string
131   EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
132   EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
133   EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
134   // empty needle. Note that this returns npos, while find() returns 0!
135   EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
136   EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
137
138   // single char find_first_ofs
139   EXPECT_EQ(s.find_first_of('b'), 3);
140   EXPECT_EQ(s.find_first_of('b', 3), 3);
141   EXPECT_EQ(s.find_first_of('b', 4), 6);
142   EXPECT_EQ(s.find_first_of('o', 2), 2);
143   EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
144   EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
145   // starting position too far
146   EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
147   // starting pos that is obviously past the end -- This works for std::string
148   EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
149   EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
150   EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
151   // null char
152   EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
153   EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
154
155   // just "barbaz"
156   s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
157   EXPECT_EQ(s.size(), 6);
158   EXPECT_EQ(s.start(), foobarbaz + 3);
159   EXPECT_EQ(s, "barbaz");
160
161   // just "bar"
162   s.reset(foobarbaz + 3, 3);
163   EXPECT_EQ(s.size(), 3);
164   EXPECT_EQ(s, "bar");
165
166   // clear
167   s.clear();
168   EXPECT_EQ(s.toString(), "");
169
170   // test an empty StringPiece
171   StringPiece s2;
172   EXPECT_EQ(s2.size(), 0);
173
174   // Test comparison operators
175   foo = "";
176   EXPECT_LE(s, foo);
177   EXPECT_LE(foo, s);
178   EXPECT_GE(s, foo);
179   EXPECT_GE(foo, s);
180   EXPECT_EQ(s, foo);
181   EXPECT_EQ(foo, s);
182
183   foo = "abc";
184   EXPECT_LE(s, foo);
185   EXPECT_LT(s, foo);
186   EXPECT_GE(foo, s);
187   EXPECT_GT(foo, s);
188   EXPECT_NE(s, foo);
189
190   EXPECT_LE(s, s);
191   EXPECT_LE(s, s);
192   EXPECT_GE(s, s);
193   EXPECT_GE(s, s);
194   EXPECT_EQ(s, s);
195   EXPECT_EQ(s, s);
196
197   s = "abc";
198   s2 = "abc";
199   EXPECT_LE(s, s2);
200   EXPECT_LE(s2, s);
201   EXPECT_GE(s, s2);
202   EXPECT_GE(s2, s);
203   EXPECT_EQ(s, s2);
204   EXPECT_EQ(s2, s);
205 }
206
207 TEST(StringPiece, ToByteRange) {
208   StringPiece a("hello");
209   ByteRange b(a);
210   EXPECT_EQ(static_cast<const void*>(a.begin()),
211             static_cast<const void*>(b.begin()));
212   EXPECT_EQ(static_cast<const void*>(a.end()),
213             static_cast<const void*>(b.end()));
214
215   // and convert back again
216   StringPiece c(b);
217   EXPECT_EQ(a.begin(), c.begin());
218   EXPECT_EQ(a.end(), c.end());
219 }
220
221 template <typename NeedleFinder>
222 class NeedleFinderTest : public ::testing::Test {
223  public:
224   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
225     return NeedleFinder::find_first_byte_of(haystack, needles);
226   }
227 };
228
229 struct SseNeedleFinder {
230   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
231     // This will only use the SSE version if it is supported on this CPU
232     // (selected using ifunc).
233     return detail::qfind_first_byte_of(haystack, needles);
234   }
235 };
236
237 struct NoSseNeedleFinder {
238   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
239     return detail::qfind_first_byte_of_nosse(haystack, needles);
240   }
241 };
242
243 struct MemchrNeedleFinder {
244   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
245     return detail::qfind_first_byte_of_memchr(haystack, needles);
246   }
247 };
248
249 struct ByteSetNeedleFinder {
250   static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
251     return detail::qfind_first_byte_of_byteset(haystack, needles);
252   }
253 };
254
255 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
256                          ByteSetNeedleFinder> NeedleFinders;
257 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
258
259 TYPED_TEST(NeedleFinderTest, Null) {
260   { // null characters in the string
261     string s(10, char(0));
262     s[5] = 'b';
263     string delims("abc");
264     EXPECT_EQ(5, this->find_first_byte_of(s, delims));
265   }
266   { // null characters in delim
267     string s("abc");
268     string delims(10, char(0));
269     delims[3] = 'c';
270     delims[7] = 'b';
271     EXPECT_EQ(1, this->find_first_byte_of(s, delims));
272   }
273   { // range not terminated by null character
274     string buf = "abcdefghijklmnopqrstuvwxyz";
275     StringPiece s(buf.data() + 5, 3);
276     StringPiece delims("z");
277     EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
278   }
279 }
280
281 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
282   string delims(1000, 'b');
283   EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
284   EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
285 }
286
287 TYPED_TEST(NeedleFinderTest, Empty) {
288   string a = "abc";
289   string b = "";
290   EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
291   EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
292   EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
293 }
294
295 TYPED_TEST(NeedleFinderTest, Unaligned) {
296   // works correctly even if input buffers are not 16-byte aligned
297   string s = "0123456789ABCDEFGH";
298   for (int i = 0; i < s.size(); ++i) {
299     StringPiece a(s.c_str() + i);
300     for (int j = 0; j < s.size(); ++j) {
301       StringPiece b(s.c_str() + j);
302       EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
303     }
304   }
305 }
306
307 // for some algorithms (specifically those that create a set of needles),
308 // we check for the edge-case of _all_ possible needles being sought.
309 TYPED_TEST(NeedleFinderTest, Needles256) {
310   string needles;
311   const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
312   const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
313   // make the size ~big to avoid any edge-case branches for tiny haystacks
314   const int haystackSize = 50;
315   for (int i = minValue; i <= maxValue; i++) {  // <=
316     needles.push_back(i);
317   }
318   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
319   for (int i = minValue; i <= maxValue; i++) {
320     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
321   }
322
323   needles.append("these are redundant characters");
324   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
325   for (int i = minValue; i <= maxValue; i++) {
326     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
327   }
328 }
329
330 TYPED_TEST(NeedleFinderTest, Base) {
331   for (int i = 0; i < 32; ++i) {
332     for (int j = 0; j < 32; ++j) {
333       string s = string(i, 'X') + "abca" + string(i, 'X');
334       string delims = string(j, 'Y') + "a" + string(j, 'Y');
335       EXPECT_EQ(i, this->find_first_byte_of(s, delims));
336     }
337   }
338 }