2 * Copyright 2013 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)
22 #include <boost/range/concepts.hpp>
23 #include <gtest/gtest.h>
24 #include "folly/Range.h"
26 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);
32 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
33 const StringPiece& needles);
37 using namespace folly;
40 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
42 TEST(StringPiece, All) {
43 const char* foo = "foo";
44 const char* foo2 = "foo";
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
53 // the string object creates copies though, so the c_str of these should be
55 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
57 // test the basic StringPiece functionality
59 EXPECT_EQ(s.size(), 3);
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
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
69 EXPECT_EQ(s, foo); // lexical comparison
70 EXPECT_EQ(s, fooStr); // lexical comparison
71 EXPECT_EQ(s, foo2Str); // lexical comparison
74 // check using StringPiece to reference substrings
75 const char* foobarbaz = "foobarbaz";
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");
84 s.assign(foobarbaz, foobarbaz + 3);
85 EXPECT_EQ(s.size(), 3);
86 EXPECT_EQ(s.start(), foobarbaz);
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);
102 EXPECT_EQ(s.find(""), std::string().find(""));
103 EXPECT_EQ(s.find(""), 0);
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);
118 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
119 EXPECT_EQ(s.find('\0'), StringPiece::npos);
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);
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);
152 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
153 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
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");
162 s.reset(foobarbaz + 3, 3);
163 EXPECT_EQ(s.size(), 3);
168 EXPECT_EQ(s.toString(), "");
170 // test an empty StringPiece
172 EXPECT_EQ(s2.size(), 0);
174 // Test comparison operators
207 TEST(StringPiece, ToByteRange) {
208 StringPiece a("hello");
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()));
215 // and convert back again
217 EXPECT_EQ(a.begin(), c.begin());
218 EXPECT_EQ(a.end(), c.end());
221 template <typename NeedleFinder>
222 class NeedleFinderTest : public ::testing::Test {
224 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
225 return NeedleFinder::find_first_byte_of(haystack, needles);
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);
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);
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);
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);
255 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
256 ByteSetNeedleFinder> NeedleFinders;
257 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
259 TYPED_TEST(NeedleFinderTest, Null) {
260 { // null characters in the string
261 string s(10, char(0));
263 string delims("abc");
264 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
266 { // null characters in delim
268 string delims(10, char(0));
271 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
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));
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));
287 TYPED_TEST(NeedleFinderTest, Empty) {
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));
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));
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) {
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);
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));
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));
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));