proxygen
RangeTest.cpp File Reference
#include <folly/Range.h>
#include <array>
#include <deque>
#include <iterator>
#include <limits>
#include <random>
#include <string>
#include <type_traits>
#include <vector>
#include <boost/algorithm/string/trim.hpp>
#include <boost/range/concepts.hpp>
#include <folly/Memory.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
#include <folly/portability/SysMman.h>

Go to the source code of this file.

Classes

class  NeedleFinderTest< NeedleFinder >
 
struct  SseNeedleFinder
 
struct  NoSseNeedleFinder
 
struct  ByteSetNeedleFinder
 
class  tag
 
class  fake_string_view
 

Namespaces

 folly
 —— Concurrent Priority Queue Implementation ——
 

Typedefs

using NeedleFinders = ::testing::Types< SseNeedleFinder, NoSseNeedleFinder, ByteSetNeedleFinder >
 

Functions

 BOOST_CONCEPT_ASSERT ((boost::RandomAccessRangeConcept< StringPiece >))
 
 TEST (StringPiece, All)
 
template<class T >
void expectLT (const T &a, const T &b)
 
template<class T >
void expectEQ (const T &a, const T &b)
 
 TEST (StringPiece, EightBitComparisons)
 
 TEST (StringPiece, ToByteRange)
 
 TEST (StringPiece, InvalidRange)
 
 TEST (StringPiece, Constexpr)
 
 TEST (StringPiece, Prefix)
 
 TEST (StringPiece, Suffix)
 
 TEST (StringPiece, Equals)
 
 TEST (StringPiece, PrefixEmpty)
 
 TEST (StringPiece, SuffixEmpty)
 
 TEST (StringPiece, erase)
 
 TEST (StringPiece, split_step_char_delimiter)
 
 TEST (StringPiece, split_step_range_delimiter)
 
void split_step_with_process_noop (folly::StringPiece)
 
 TEST (StringPiece, split_step_with_process_char_delimiter)
 
 TEST (StringPiece, split_step_with_process_range_delimiter)
 
 TEST (StringPiece, split_step_with_process_char_delimiter_additional_args)
 
 TEST (StringPiece, split_step_with_process_range_delimiter_additional_args)
 
 TEST (StringPiece, NoInvalidImplicitConversions)
 
 TEST (qfind, UInt32_Ranges)
 
 TYPED_TEST_CASE (NeedleFinderTest, NeedleFinders)
 
 TYPED_TEST (NeedleFinderTest, Null)
 
 TYPED_TEST (NeedleFinderTest, DelimDuplicates)
 
 TYPED_TEST (NeedleFinderTest, Empty)
 
 TYPED_TEST (NeedleFinderTest, Unaligned)
 
 TYPED_TEST (NeedleFinderTest, Needles256)
 
 TYPED_TEST (NeedleFinderTest, Base)
 
void createProtectedBuf (StringPiece &contents, char **buf)
 
void freeProtectedBuf (char *buf)
 
 TYPED_TEST (NeedleFinderTest, NoSegFault)
 
 TEST (NonConstTest, StringPiece)
 
template<class Container >
Container::value_type * dataPtr (Container &cont)
 
template<class T , size_t N>
constexpr TdataPtr (T(&arr)[N]) noexcept
 
template<class C >
void testRangeFunc (C &&x, size_t n)
 
 TEST (RangeFunc, Vector)
 
 TEST (RangeFunc, Array)
 
 TEST (RangeFunc, CArray)
 
 TEST (RangeFunc, ConstexprCArray)
 
 TEST (RangeFunc, ConstexprStdArray)
 
 TEST (RangeFunc, ConstexprStdArrayZero)
 
 TEST (RangeFunc, ConstexprIteratorPair)
 
 TEST (RangeFunc, ConstexprCollection)
 
 TEST (CRangeFunc, CArray)
 
 TEST (CRangeFunc, StdArray)
 
 TEST (CRangeFunc, StdArrayZero)
 
 TEST (CRangeFunc, Collection)
 
std::string get_rand_str (size_t size, std::uniform_int_distribution<> &dist, std::mt19937 &gen)
 
bool folly::operator== (MutableStringPiece mp, StringPiece sp)
 
bool folly::operator== (StringPiece sp, MutableStringPiece mp)
 
 TEST (ReplaceAt, exhaustiveTest)
 
 TEST (ReplaceAll, basicTest)
 
 TEST (ReplaceAll, randomTest)
 
 TEST (ReplaceAll, BadArg)
 
 TEST (Range, Constructors)
 
 TEST (Range, ArrayConstructors)
 
 TEST (Range, ConstexprAccessors)
 
 TEST (Range, LiteralSuffix)
 
 TEST (Range, LiteralSuffixContainsNulBytes)
 
 TEST (Range, StringPieceExplicitConversionOperator)
 
 TEST (Range, MutableStringPieceExplicitConversionOperator)
 

Variables

const size_t kPageSize = 4096
 

Typedef Documentation

Definition at line 916 of file RangeTest.cpp.

Function Documentation

BOOST_CONCEPT_ASSERT ( (boost::RandomAccessRangeConcept< StringPiece >)  )
void createProtectedBuf ( StringPiece contents,
char **  buf 
)

Definition at line 1006 of file RangeTest.cpp.

References folly::aligned_malloc(), ASSERT_LE, folly::Range< Iter >::data(), FAIL, kPageSize, folly::Range< Iter >::reset(), and folly::Range< Iter >::size().

Referenced by TYPED_TEST().

1006  {
1007  ASSERT_LE(contents.size(), kPageSize);
1008  char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
1009  if (pageAlignedBuf == nullptr) {
1010  FAIL();
1011  }
1012  // Protect the page after the first full page-aligned region of the
1013  // malloc'ed buffer
1014  mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
1015  size_t newBegin = kPageSize - contents.size();
1016  memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
1017  contents.reset(pageAlignedBuf + newBegin, contents.size());
1018  *buf = pageAlignedBuf;
1019 }
#define FAIL()
Definition: gtest.h:1822
#define ASSERT_LE(val1, val2)
Definition: gtest.h:1964
constexpr size_type size() const
Definition: Range.h:431
void * aligned_malloc(size_t size, size_t align)
Definition: Memory.h:85
constexpr Iter data() const
Definition: Range.h:446
const size_t kPageSize
Definition: RangeTest.cpp:1000
void reset(Iter start, size_type size)
Definition: Range.h:421
template<class Container >
Container::value_type* dataPtr ( Container &  cont)

Definition at line 1082 of file RangeTest.cpp.

Referenced by folly::ssl::OpenSSLCertUtils::derEncode(), wangle::SSLUtil::exDataStdStringDup(), fizz::CertUtils::getCertMessage(), and testRangeFunc().

1082  {
1083  // NOTE: &cont[0] is undefined if cont is empty (it creates a
1084  // reference to nullptr - which is not dereferenced, but still UBSAN).
1085  return cont.data();
1086 }
template<class T , size_t N>
constexpr T* dataPtr ( T(&)  arr[N])
noexcept

Definition at line 1088 of file RangeTest.cpp.

1088  {
1089  return &arr[0];
1090 }
template<class T >
void expectEQ ( const T a,
const T b 
)

Definition at line 241 of file RangeTest.cpp.

References EXPECT_FALSE, and EXPECT_TRUE.

Referenced by TEST().

241  {
242  EXPECT_FALSE(a < b);
243  EXPECT_TRUE(a <= b);
244  EXPECT_TRUE(a == b);
245  EXPECT_TRUE(a >= b);
246  EXPECT_FALSE(a > b);
247 }
char b
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
template<class T >
void expectLT ( const T a,
const T b 
)

Definition at line 227 of file RangeTest.cpp.

References EXPECT_FALSE, and EXPECT_TRUE.

Referenced by TEST().

227  {
228  EXPECT_TRUE(a < b);
229  EXPECT_TRUE(a <= b);
230  EXPECT_FALSE(a == b);
231  EXPECT_FALSE(a >= b);
232  EXPECT_FALSE(a > b);
233 
234  EXPECT_FALSE(b < a);
235  EXPECT_FALSE(b <= a);
236  EXPECT_TRUE(b >= a);
237  EXPECT_TRUE(b > a);
238 }
char b
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
void freeProtectedBuf ( char *  buf)

Definition at line 1021 of file RangeTest.cpp.

References folly::aligned_free().

Referenced by TYPED_TEST().

1021  {
1022  mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
1023  aligned_free(buf);
1024 }
void aligned_free(void *aligned_ptr)
Definition: Memory.h:89
const size_t kPageSize
Definition: RangeTest.cpp:1000
std::string get_rand_str ( size_t  size,
std::uniform_int_distribution<> &  dist,
std::mt19937 &  gen 
)

Definition at line 1232 of file RangeTest.cpp.

References dist, i, folly::size(), and string.

Referenced by TEST().

1235  {
1236  std::string ret(size, '\0');
1237  for (size_t i = 0; i < size; ++i) {
1238  ret[i] = static_cast<char>(dist(gen));
1239  }
1240 
1241  return ret;
1242 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
std::uniform_int_distribution< milliseconds::rep > dist
const char * string
Definition: Conv.cpp:212
void split_step_with_process_noop ( folly::StringPiece  )

Definition at line 615 of file RangeTest.cpp.

Referenced by TEST().

615 {}
TEST ( StringPiece  ,
All   
)

Definition at line 47 of file RangeTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_GE, EXPECT_GT, EXPECT_LE, EXPECT_LT, EXPECT_NE, EXPECT_TRUE, folly::foo, foo2(), folly::Range< const char * >::npos, folly::rfind(), s, and string.

47  {
48  const char* foo = "foo";
49  const char* foo2 = "foo";
50  string fooStr(foo);
51  string foo2Str(foo2);
52 
53  // we expect the compiler to optimize things so that there's only one copy
54  // of the string literal "foo", even though we've got it in multiple places
55  EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
56  // comparison rather than lexical
57 
58  // the string object creates copies though, so the c_str of these should be
59  // distinct
60  EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
61 
62  // test the basic StringPiece functionality
63  StringPiece s(foo);
64  EXPECT_EQ(s.size(), 3);
65 
66  EXPECT_EQ(s.start(), foo); // ptr comparison
67  EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
68  EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
69 
70  EXPECT_EQ(s.toString(), foo); // lexical comparison
71  EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
72  EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
73 
74  EXPECT_EQ(s, foo); // lexical comparison
75  EXPECT_EQ(s, fooStr); // lexical comparison
76  EXPECT_EQ(s, foo2Str); // lexical comparison
77  EXPECT_EQ(foo, s);
78 
79  // check using StringPiece to reference substrings
80  const char* foobarbaz = "foobarbaz";
81 
82  // the full "foobarbaz"
83  s.reset(foobarbaz, strlen(foobarbaz));
84  EXPECT_EQ(s.size(), 9);
85  EXPECT_EQ(s.start(), foobarbaz);
86  EXPECT_EQ(s, "foobarbaz");
87 
88  // only the 'foo'
89  s.assign(foobarbaz, foobarbaz + 3);
90  EXPECT_EQ(s.size(), 3);
91  EXPECT_EQ(s.start(), foobarbaz);
92  EXPECT_EQ(s, "foo");
93 
94  // find
95  s.reset(foobarbaz, strlen(foobarbaz));
96  EXPECT_EQ(s.find("bar"), 3);
97  EXPECT_EQ(s.find("ba", 3), 3);
98  EXPECT_EQ(s.find("ba", 4), 6);
99  EXPECT_EQ(s.find("notfound"), StringPiece::npos);
100  EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
101  EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
102  // starting pos that is obviously past the end -- This works for std::string
103  EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
104  EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
105  EXPECT_EQ(s.find("z", 55), StringPiece::npos);
106  // empty needle
107  EXPECT_EQ(s.find(""), std::string().find(""));
108  EXPECT_EQ(s.find(""), 0);
109 
110  // single char finds
111  EXPECT_EQ(s.find('b'), 3);
112  EXPECT_EQ(s.find('b', 3), 3);
113  EXPECT_EQ(s.find('b', 4), 6);
114  EXPECT_EQ(s.find('o', 2), 2);
115  EXPECT_EQ(s.find('y'), StringPiece::npos);
116  EXPECT_EQ(s.find('y', 1), StringPiece::npos);
117  EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
118  EXPECT_TRUE(s.contains('z'));
119  // starting pos that is obviously past the end -- This works for std::string
120  EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
121  EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
122  EXPECT_EQ(s.find('z', 55), StringPiece::npos);
123  // null char
124  EXPECT_EQ(s.find('\0'), std::string().find('\0'));
125  EXPECT_EQ(s.find('\0'), StringPiece::npos);
126  EXPECT_FALSE(s.contains('\0'));
127 
128  // single char rfinds
129  EXPECT_EQ(s.rfind('b'), 6);
130  EXPECT_EQ(s.rfind('y'), StringPiece::npos);
131  EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
132  EXPECT_EQ(ByteRange(s).rfind('b'), 6);
133  EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
134  // null char
135  EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
136  EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
137 
138  // find_first_of
139  s.reset(foobarbaz, strlen(foobarbaz));
140  EXPECT_EQ(s.find_first_of("bar"), 3);
141  EXPECT_EQ(s.find_first_of("ba", 3), 3);
142  EXPECT_EQ(s.find_first_of("ba", 4), 4);
143  EXPECT_TRUE(s.contains("bar"));
144  EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
145  EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
146  EXPECT_FALSE(s.contains("xyxy"));
147  // starting position too far
148  EXPECT_EQ(s.find_first_of("foo", 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("xyxy", 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  // empty needle. Note that this returns npos, while find() returns 0!
154  EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
155  EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
156 
157  // single char find_first_ofs
158  EXPECT_EQ(s.find_first_of('b'), 3);
159  EXPECT_EQ(s.find_first_of('b', 3), 3);
160  EXPECT_EQ(s.find_first_of('b', 4), 6);
161  EXPECT_EQ(s.find_first_of('o', 2), 2);
162  EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
163  EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
164  // starting position too far
165  EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
166  // starting pos that is obviously past the end -- This works for std::string
167  EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
168  EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
169  EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
170  // null char
171  EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
172  EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
173 
174  // just "barbaz"
175  s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
176  EXPECT_EQ(s.size(), 6);
177  EXPECT_EQ(s.start(), foobarbaz + 3);
178  EXPECT_EQ(s, "barbaz");
179 
180  // just "bar"
181  s.reset(foobarbaz + 3, 3);
182  EXPECT_EQ(s.size(), 3);
183  EXPECT_EQ(s, "bar");
184 
185  // clear
186  s.clear();
187  EXPECT_EQ(s.toString(), "");
188 
189  // test an empty StringPiece
190  StringPiece s2;
191  EXPECT_EQ(s2.size(), 0);
192 
193  // Test comparison operators
194  foo = "";
195  EXPECT_LE(s, foo);
196  EXPECT_LE(foo, s);
197  EXPECT_GE(s, foo);
198  EXPECT_GE(foo, s);
199  EXPECT_EQ(s, foo);
200  EXPECT_EQ(foo, s);
201 
202  foo = "abc";
203  EXPECT_LE(s, foo);
204  EXPECT_LT(s, foo);
205  EXPECT_GE(foo, s);
206  EXPECT_GT(foo, s);
207  EXPECT_NE(s, foo);
208 
209  EXPECT_LE(s, s);
210  EXPECT_LE(s, s);
211  EXPECT_GE(s, s);
212  EXPECT_GE(s, s);
213  EXPECT_EQ(s, s);
214  EXPECT_EQ(s, s);
215 
216  s = "abc";
217  s2 = "abc";
218  EXPECT_LE(s, s2);
219  EXPECT_LE(s2, s);
220  EXPECT_GE(s, s2);
221  EXPECT_GE(s2, s);
222  EXPECT_EQ(s, s2);
223  EXPECT_EQ(s2, s);
224 }
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
FOLLY_NOINLINE void foo2()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
size_t rfind(const Range< Iter > &haystack, const typename Range< Iter >::value_type &needle)
Definition: Range.h:1404
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
static set< string > s
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
#define EXPECT_LT(val1, val2)
Definition: gtest.h:1930
#define EXPECT_GT(val1, val2)
Definition: gtest.h:1934
TEST ( StringPiece  ,
EightBitComparisons   
)

Definition at line 249 of file RangeTest.cpp.

References a, b, count, expectEQ(), expectLT(), i, string, and values().

249  {
250  char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
251  constexpr size_t count = sizeof(values) / sizeof(values[0]);
252  for (size_t i = 0; i < count; ++i) {
253  std::string a(1, values[i]);
254  // Defeat copy-on-write
255  std::string aCopy(a.data(), a.size());
256  expectEQ(a, aCopy);
257  expectEQ(StringPiece(a), StringPiece(aCopy));
258 
259  for (size_t j = i + 1; j < count; ++j) {
260  std::string b(1, values[j]);
261  expectLT(a, b);
263  }
264  }
265 }
char b
void expectLT(const T &a, const T &b)
Definition: RangeTest.cpp:227
char a
int * count
const char * string
Definition: Conv.cpp:212
Range< const char * > StringPiece
void expectEQ(const T &a, const T &b)
Definition: RangeTest.cpp:241
std::vector< int > values(1'000)
TEST ( StringPiece  ,
ToByteRange   
)

Definition at line 267 of file RangeTest.cpp.

References a, b, folly::Range< Iter >::begin(), c, folly::Range< Iter >::end(), and EXPECT_EQ.

267  {
268  StringPiece a("hello");
269  ByteRange b(a);
270  EXPECT_EQ(
271  static_cast<const void*>(a.begin()), static_cast<const void*>(b.begin()));
272  EXPECT_EQ(
273  static_cast<const void*>(a.end()), static_cast<const void*>(b.end()));
274 
275  // and convert back again
276  StringPiece c(b);
277  EXPECT_EQ(a.begin(), c.begin());
278  EXPECT_EQ(a.end(), c.end());
279 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
char c
TEST ( StringPiece  ,
InvalidRange   
)

Definition at line 281 of file RangeTest.cpp.

References a, b, EXPECT_EQ, EXPECT_THROW, string, and folly::Range< Iter >::subpiece().

281  {
282  StringPiece a("hello");
283  EXPECT_EQ(a, a.subpiece(0, 10));
284  EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
285  EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
286  EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
287  EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
288  EXPECT_THROW(a.subpiece(6), std::out_of_range);
289 
290  std::string b("hello");
291  EXPECT_EQ(a, StringPiece(b, 0, 10));
292  EXPECT_EQ("ello", a.subpiece(1));
293  EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
294  EXPECT_EQ("ell", a.subpiece(1, 3));
295  EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
296  EXPECT_THROW(a.subpiece(6), std::out_of_range);
297 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
const char * string
Definition: Conv.cpp:212
Range< const char * > StringPiece
TEST ( StringPiece  ,
Constexpr   
)

Definition at line 299 of file RangeTest.cpp.

References EXPECT_EQ.

299  {
300  constexpr const char* helloArray = "hello";
301 
302  constexpr StringPiece hello1("hello");
303  EXPECT_EQ("hello", hello1);
304  static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
305 
306  constexpr StringPiece hello2(helloArray);
307  EXPECT_EQ("hello", hello2);
308  static_assert(hello2.size() == 5, "hello size should be 5 at compile time");
309 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( StringPiece  ,
Prefix   
)

Definition at line 311 of file RangeTest.cpp.

References a, b, EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, and folly::Range< Iter >::startsWith().

311  {
312  StringPiece a("hello");
313  EXPECT_TRUE(a.startsWith(""));
314  EXPECT_TRUE(a.startsWith("h"));
315  EXPECT_TRUE(a.startsWith('h'));
316  EXPECT_TRUE(a.startsWith("hello"));
317  EXPECT_FALSE(a.startsWith("hellox"));
318  EXPECT_FALSE(a.startsWith('x'));
319  EXPECT_FALSE(a.startsWith("x"));
320 
321  EXPECT_TRUE(a.startsWith("", folly::AsciiCaseInsensitive()));
322  EXPECT_TRUE(a.startsWith("hello", folly::AsciiCaseInsensitive()));
323  EXPECT_TRUE(a.startsWith("hellO", folly::AsciiCaseInsensitive()));
324  EXPECT_TRUE(a.startsWith("HELL", folly::AsciiCaseInsensitive()));
325  EXPECT_TRUE(a.startsWith("H", folly::AsciiCaseInsensitive()));
326  EXPECT_FALSE(a.startsWith("HELLOX", folly::AsciiCaseInsensitive()));
327  EXPECT_FALSE(a.startsWith("x", folly::AsciiCaseInsensitive()));
328  EXPECT_FALSE(a.startsWith("X", folly::AsciiCaseInsensitive()));
329 
330  {
331  auto b = a;
332  EXPECT_TRUE(b.removePrefix(""));
333  EXPECT_EQ("hello", b);
334  }
335  {
336  auto b = a;
337  EXPECT_TRUE(b.removePrefix("h"));
338  EXPECT_EQ("ello", b);
339  }
340  {
341  auto b = a;
342  EXPECT_TRUE(b.removePrefix('h'));
343  EXPECT_EQ("ello", b);
344  }
345  {
346  auto b = a;
347  EXPECT_TRUE(b.removePrefix("hello"));
348  EXPECT_EQ("", b);
349  }
350  {
351  auto b = a;
352  EXPECT_FALSE(b.removePrefix("hellox"));
353  EXPECT_EQ("hello", b);
354  }
355  {
356  auto b = a;
357  EXPECT_FALSE(b.removePrefix("x"));
358  EXPECT_EQ("hello", b);
359  }
360  {
361  auto b = a;
362  EXPECT_FALSE(b.removePrefix('x'));
363  EXPECT_EQ("hello", b);
364  }
365 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( StringPiece  ,
Suffix   
)

Definition at line 367 of file RangeTest.cpp.

References a, b, folly::Range< Iter >::endsWith(), EXPECT_EQ, EXPECT_FALSE, and EXPECT_TRUE.

367  {
368  StringPiece a("hello");
369  EXPECT_TRUE(a.endsWith(""));
370  EXPECT_TRUE(a.endsWith("o"));
371  EXPECT_TRUE(a.endsWith('o'));
372  EXPECT_TRUE(a.endsWith("hello"));
373  EXPECT_FALSE(a.endsWith("xhello"));
374  EXPECT_FALSE(a.endsWith("x"));
375  EXPECT_FALSE(a.endsWith('x'));
376 
377  EXPECT_TRUE(a.endsWith("", folly::AsciiCaseInsensitive()));
378  EXPECT_TRUE(a.endsWith("o", folly::AsciiCaseInsensitive()));
379  EXPECT_TRUE(a.endsWith("O", folly::AsciiCaseInsensitive()));
380  EXPECT_TRUE(a.endsWith("hello", folly::AsciiCaseInsensitive()));
381  EXPECT_TRUE(a.endsWith("hellO", folly::AsciiCaseInsensitive()));
382  EXPECT_FALSE(a.endsWith("xhello", folly::AsciiCaseInsensitive()));
383  EXPECT_FALSE(a.endsWith("Xhello", folly::AsciiCaseInsensitive()));
384  EXPECT_FALSE(a.endsWith("x", folly::AsciiCaseInsensitive()));
385  EXPECT_FALSE(a.endsWith("X", folly::AsciiCaseInsensitive()));
386 
387  {
388  auto b = a;
389  EXPECT_TRUE(b.removeSuffix(""));
390  EXPECT_EQ("hello", b);
391  }
392  {
393  auto b = a;
394  EXPECT_TRUE(b.removeSuffix("o"));
395  EXPECT_EQ("hell", b);
396  }
397  {
398  auto b = a;
399  EXPECT_TRUE(b.removeSuffix('o'));
400  EXPECT_EQ("hell", b);
401  }
402  {
403  auto b = a;
404  EXPECT_TRUE(b.removeSuffix("hello"));
405  EXPECT_EQ("", b);
406  }
407  {
408  auto b = a;
409  EXPECT_FALSE(b.removeSuffix("xhello"));
410  EXPECT_EQ("hello", b);
411  }
412  {
413  auto b = a;
414  EXPECT_FALSE(b.removeSuffix("x"));
415  EXPECT_EQ("hello", b);
416  }
417  {
418  auto b = a;
419  EXPECT_FALSE(b.removeSuffix('x'));
420  EXPECT_EQ("hello", b);
421  }
422 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( StringPiece  ,
Equals   
)

Definition at line 424 of file RangeTest.cpp.

References a, folly::Range< Iter >::equals(), EXPECT_FALSE, and EXPECT_TRUE.

424  {
425  StringPiece a("hello");
426 
427  EXPECT_TRUE(a.equals("HELLO", AsciiCaseInsensitive()));
428  EXPECT_FALSE(a.equals("HELLOX", AsciiCaseInsensitive()));
429 }
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( StringPiece  ,
PrefixEmpty   
)

Definition at line 431 of file RangeTest.cpp.

References a, EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::Range< Iter >::removePrefix(), and folly::Range< Iter >::startsWith().

431  {
432  StringPiece a;
433  EXPECT_TRUE(a.startsWith(""));
434  EXPECT_FALSE(a.startsWith("a"));
435  EXPECT_FALSE(a.startsWith('a'));
436  EXPECT_TRUE(a.removePrefix(""));
437  EXPECT_EQ("", a);
438  EXPECT_FALSE(a.removePrefix("a"));
439  EXPECT_EQ("", a);
440  EXPECT_FALSE(a.removePrefix('a'));
441  EXPECT_EQ("", a);
442 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
bool removePrefix(const const_range_type &prefix)
Definition: Range.h:893
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool startsWith(const const_range_type &other) const
Definition: Range.h:828
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( StringPiece  ,
SuffixEmpty   
)

Definition at line 444 of file RangeTest.cpp.

References a, folly::Range< Iter >::endsWith(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, and folly::Range< Iter >::removeSuffix().

444  {
445  StringPiece a;
446  EXPECT_TRUE(a.endsWith(""));
447  EXPECT_FALSE(a.endsWith("a"));
448  EXPECT_FALSE(a.endsWith('a'));
449  EXPECT_TRUE(a.removeSuffix(""));
450  EXPECT_EQ("", a);
451  EXPECT_FALSE(a.removeSuffix("a"));
452  EXPECT_EQ("", a);
453  EXPECT_FALSE(a.removeSuffix('a'));
454  EXPECT_EQ("", a);
455 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
bool removeSuffix(const const_range_type &suffix)
Definition: Range.h:904
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool endsWith(const const_range_type &other) const
Definition: Range.h:849
TEST ( StringPiece  ,
erase   
)

Definition at line 457 of file RangeTest.cpp.

References a, b, folly::Range< Iter >::begin(), folly::Range< Iter >::end(), folly::Range< Iter >::erase(), EXPECT_EQ, and EXPECT_THROW.

457  {
458  StringPiece a("hello");
459  auto b = a.begin();
460  auto e = b + 1;
461  a.erase(b, e);
462  EXPECT_EQ("ello", a);
463 
464  e = a.end();
465  b = e - 1;
466  a.erase(b, e);
467  EXPECT_EQ("ell", a);
468 
469  b = a.end() - 1;
470  e = a.end() - 1;
471  EXPECT_THROW(a.erase(b, e), std::out_of_range);
472 
473  b = a.begin();
474  e = a.end();
475  a.erase(b, e);
476  EXPECT_EQ("", a);
477 
478  a = "hello";
479  b = a.begin();
480  e = b + 2;
481  a.erase(b, e);
482  EXPECT_EQ("llo", a);
483 
484  b = a.end() - 2;
485  e = a.end();
486  a.erase(b, e);
487  EXPECT_EQ("l", a);
488 
489  a = " hello ";
490  boost::algorithm::trim(a);
491  EXPECT_EQ(a, "hello");
492 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
TEST ( StringPiece  ,
split_step_char_delimiter   
)

Definition at line 494 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::end(), EXPECT_EQ, cpp.ast::next(), s, and folly::Range< Iter >::split_step().

494  {
495  // 0 1 2
496  // 012345678901234567890123456
497  auto const s = "this is just a test string";
498  auto const e = std::next(s, std::strlen(s));
499  EXPECT_EQ('\0', *e);
500 
502  EXPECT_EQ(s, p.begin());
503  EXPECT_EQ(e, p.end());
504  EXPECT_EQ(s, p);
505 
506  auto x = p.split_step(' ');
507  EXPECT_EQ(std::next(s, 5), p.begin());
508  EXPECT_EQ(e, p.end());
509  EXPECT_EQ("this", x);
510 
511  x = p.split_step(' ');
512  EXPECT_EQ(std::next(s, 8), p.begin());
513  EXPECT_EQ(e, p.end());
514  EXPECT_EQ("is", x);
515 
516  x = p.split_step('u');
517  EXPECT_EQ(std::next(s, 10), p.begin());
518  EXPECT_EQ(e, p.end());
519  EXPECT_EQ("j", x);
520 
521  x = p.split_step(' ');
522  EXPECT_EQ(std::next(s, 13), p.begin());
523  EXPECT_EQ(e, p.end());
524  EXPECT_EQ("st", x);
525 
526  x = p.split_step(' ');
527  EXPECT_EQ(std::next(s, 14), p.begin());
528  EXPECT_EQ(e, p.end());
529  EXPECT_EQ("", x);
530 
531  x = p.split_step(' ');
532  EXPECT_EQ(std::next(s, 16), p.begin());
533  EXPECT_EQ(e, p.end());
534  EXPECT_EQ("a", x);
535 
536  x = p.split_step(' ');
537  EXPECT_EQ(std::next(s, 21), p.begin());
538  EXPECT_EQ(e, p.end());
539  EXPECT_EQ("test", x);
540 
541  x = p.split_step(' ');
542  EXPECT_EQ(e, p.begin());
543  EXPECT_EQ(e, p.end());
544  EXPECT_EQ("string", x);
545 
546  x = p.split_step(' ');
547  EXPECT_EQ(e, p.begin());
548  EXPECT_EQ(e, p.end());
549  EXPECT_EQ("", x);
550 }
Definition: InvokeTest.cpp:58
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s
def next(obj)
Definition: ast.py:58
TEST ( StringPiece  ,
split_step_range_delimiter   
)

Definition at line 552 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::end(), EXPECT_EQ, cpp.ast::next(), s, and folly::Range< Iter >::split_step().

552  {
553  // 0 1 2 3
554  // 0123456789012345678901234567890123
555  auto const s = "this is just a test string";
556  auto const e = std::next(s, std::strlen(s));
557  EXPECT_EQ('\0', *e);
558 
560  EXPECT_EQ(s, p.begin());
561  EXPECT_EQ(e, p.end());
562  EXPECT_EQ(s, p);
563 
564  auto x = p.split_step(" ");
565  EXPECT_EQ(std::next(s, 6), p.begin());
566  EXPECT_EQ(e, p.end());
567  EXPECT_EQ("this", x);
568 
569  x = p.split_step(" ");
570  EXPECT_EQ(std::next(s, 10), p.begin());
571  EXPECT_EQ(e, p.end());
572  EXPECT_EQ("is", x);
573 
574  x = p.split_step("u");
575  EXPECT_EQ(std::next(s, 12), p.begin());
576  EXPECT_EQ(e, p.end());
577  EXPECT_EQ("j", x);
578 
579  x = p.split_step(" ");
580  EXPECT_EQ(std::next(s, 16), p.begin());
581  EXPECT_EQ(e, p.end());
582  EXPECT_EQ("st", x);
583 
584  x = p.split_step(" ");
585  EXPECT_EQ(std::next(s, 18), p.begin());
586  EXPECT_EQ(e, p.end());
587  EXPECT_EQ("", x);
588 
589  x = p.split_step(" ");
590  EXPECT_EQ(std::next(s, 21), p.begin());
591  EXPECT_EQ(e, p.end());
592  EXPECT_EQ("a", x);
593 
594  x = p.split_step(" ");
595  EXPECT_EQ(std::next(s, 28), p.begin());
596  EXPECT_EQ(e, p.end());
597  EXPECT_EQ(" test", x);
598 
599  x = p.split_step(" ");
600  EXPECT_EQ(e, p.begin());
601  EXPECT_EQ(e, p.end());
602  EXPECT_EQ("string", x);
603 
604  x = p.split_step(" ");
605  EXPECT_EQ(e, p.begin());
606  EXPECT_EQ(e, p.end());
607  EXPECT_EQ("", x);
608 
609  x = p.split_step(" ");
610  EXPECT_EQ(e, p.begin());
611  EXPECT_EQ(e, p.end());
612  EXPECT_EQ("", x);
613 }
Definition: InvokeTest.cpp:58
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s
def next(obj)
Definition: ast.py:58
TEST ( StringPiece  ,
split_step_with_process_char_delimiter   
)

Definition at line 617 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::end(), EXPECT_EQ, EXPECT_NO_THROW, EXPECT_TRUE, cpp.ast::next(), s, folly::Range< Iter >::split_step(), split_step_with_process_noop(), and folly::value().

617  {
618  // 0 1 2
619  // 012345678901234567890123456
620  auto const s = "this is just a test string";
621  auto const e = std::next(s, std::strlen(s));
622  EXPECT_EQ('\0', *e);
623 
625  EXPECT_EQ(s, p.begin());
626  EXPECT_EQ(e, p.end());
627  EXPECT_EQ(s, p);
628 
629  EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
630  EXPECT_EQ(std::next(s, 5), p.begin());
631  EXPECT_EQ(e, p.end());
632  EXPECT_EQ("this", x);
633  return 1;
634  })));
635 
636  EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
637  EXPECT_EQ(std::next(s, 8), p.begin());
638  EXPECT_EQ(e, p.end());
639  EXPECT_EQ("is", x);
640  return 2;
641  })));
642 
643  EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
644  EXPECT_EQ(std::next(s, 10), p.begin());
645  EXPECT_EQ(e, p.end());
646  EXPECT_EQ("j", x);
647  return 3;
648  })));
649 
650  EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
651  EXPECT_EQ(std::next(s, 13), p.begin());
652  EXPECT_EQ(e, p.end());
653  EXPECT_EQ("st", x);
654  return 4;
655  })));
656 
657  EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
658  EXPECT_EQ(std::next(s, 14), p.begin());
659  EXPECT_EQ(e, p.end());
660  EXPECT_EQ("", x);
661  return 5;
662  })));
663 
664  EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
665  EXPECT_EQ(std::next(s, 16), p.begin());
666  EXPECT_EQ(e, p.end());
667  EXPECT_EQ("a", x);
668  return 6;
669  })));
670 
671  EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
672  EXPECT_EQ(std::next(s, 21), p.begin());
673  EXPECT_EQ(e, p.end());
674  EXPECT_EQ("test", x);
675  return 7;
676  })));
677 
678  EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
679  EXPECT_EQ(e, p.begin());
680  EXPECT_EQ(e, p.end());
681  EXPECT_EQ("string", x);
682  return 8;
683  })));
684 
685  EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
686  EXPECT_EQ(e, p.begin());
687  EXPECT_EQ(e, p.end());
688  EXPECT_EQ("", x);
689  return 9;
690  })));
691 
692  EXPECT_TRUE(
693  (std::is_same<
694  void,
695  decltype(p.split_step(' ', split_step_with_process_noop))>::value));
696 
697  EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
698 }
Definition: InvokeTest.cpp:58
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
void split_step_with_process_noop(folly::StringPiece)
Definition: RangeTest.cpp:615
def next(obj)
Definition: ast.py:58
TEST ( StringPiece  ,
split_step_with_process_range_delimiter   
)

Definition at line 700 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::end(), EXPECT_EQ, EXPECT_NO_THROW, EXPECT_TRUE, cpp.ast::next(), s, folly::Range< Iter >::split_step(), split_step_with_process_noop(), and folly::value().

700  {
701  // 0 1 2 3
702  // 0123456789012345678901234567890123
703  auto const s = "this is just a test string";
704  auto const e = std::next(s, std::strlen(s));
705  EXPECT_EQ('\0', *e);
706 
708  EXPECT_EQ(s, p.begin());
709  EXPECT_EQ(e, p.end());
710  EXPECT_EQ(s, p);
711 
712  EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
713  EXPECT_EQ(std::next(s, 6), p.begin());
714  EXPECT_EQ(e, p.end());
715  EXPECT_EQ("this", x);
716  return 1;
717  })));
718 
719  EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
720  EXPECT_EQ(std::next(s, 10), p.begin());
721  EXPECT_EQ(e, p.end());
722  EXPECT_EQ("is", x);
723  return 2;
724  })));
725 
726  EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
727  EXPECT_EQ(std::next(s, 12), p.begin());
728  EXPECT_EQ(e, p.end());
729  EXPECT_EQ("j", x);
730  return 3;
731  })));
732 
733  EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
734  EXPECT_EQ(std::next(s, 16), p.begin());
735  EXPECT_EQ(e, p.end());
736  EXPECT_EQ("st", x);
737  return 4;
738  })));
739 
740  EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
741  EXPECT_EQ(std::next(s, 18), p.begin());
742  EXPECT_EQ(e, p.end());
743  EXPECT_EQ("", x);
744  return 5;
745  })));
746 
747  EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
748  EXPECT_EQ(std::next(s, 21), p.begin());
749  EXPECT_EQ(e, p.end());
750  EXPECT_EQ("a", x);
751  return 6;
752  })));
753 
754  EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
755  EXPECT_EQ(std::next(s, 28), p.begin());
756  EXPECT_EQ(e, p.end());
757  EXPECT_EQ(" test", x);
758  return 7;
759  })));
760 
761  EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
762  EXPECT_EQ(e, p.begin());
763  EXPECT_EQ(e, p.end());
764  EXPECT_EQ("string", x);
765  return 8;
766  })));
767 
768  EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
769  EXPECT_EQ(e, p.begin());
770  EXPECT_EQ(e, p.end());
771  EXPECT_EQ("", x);
772  return 9;
773  })));
774 
775  EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
776  EXPECT_EQ(e, p.begin());
777  EXPECT_EQ(e, p.end());
778  EXPECT_EQ("", x);
779  return 10;
780  })));
781 
782  EXPECT_TRUE(
783  (std::is_same<
784  void,
785  decltype(p.split_step(' ', split_step_with_process_noop))>::value));
786 
787  EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
788 }
Definition: InvokeTest.cpp:58
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
void split_step_with_process_noop(folly::StringPiece)
Definition: RangeTest.cpp:615
def next(obj)
Definition: ast.py:58
TEST ( StringPiece  ,
split_step_with_process_char_delimiter_additional_args   
)

Definition at line 790 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::empty(), folly::Range< Iter >::end(), EXPECT_EQ, EXPECT_TRUE, cpp.ast::next(), s, s_, and folly::Range< Iter >::split_step().

790  {
791  // 0 1 2
792  // 012345678901234567890123456
793  auto const s = "this is just a test string";
794  auto const e = std::next(s, std::strlen(s));
795  auto const delimiter = ' ';
796  EXPECT_EQ('\0', *e);
797 
799  EXPECT_EQ(s, p.begin());
800  EXPECT_EQ(e, p.end());
801  EXPECT_EQ(s, p);
802 
803  auto const functor = [](folly::StringPiece s_, folly::StringPiece expected) {
804  EXPECT_EQ(expected, s_);
805  return expected;
806  };
807 
808  auto const checker = [&](folly::StringPiece expected) {
809  EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
810  };
811 
812  checker("this");
813  checker("is");
814  checker("just");
815  checker("");
816  checker("a");
817  checker("test");
818  checker("string");
819  checker("");
820  checker("");
821 
822  EXPECT_TRUE(p.empty());
823 }
std::string s_
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
def next(obj)
Definition: ast.py:58
TEST ( StringPiece  ,
split_step_with_process_range_delimiter_additional_args   
)

Definition at line 825 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::empty(), folly::Range< Iter >::end(), EXPECT_EQ, EXPECT_TRUE, cpp.ast::next(), s, s_, and folly::Range< Iter >::split_step().

825  {
826  // 0 1 2 3
827  // 0123456789012345678901234567890123
828  auto const s = "this is just a test string";
829  auto const e = std::next(s, std::strlen(s));
830  auto const delimiter = " ";
831  EXPECT_EQ('\0', *e);
832 
834  EXPECT_EQ(s, p.begin());
835  EXPECT_EQ(e, p.end());
836  EXPECT_EQ(s, p);
837 
838  auto const functor = [](folly::StringPiece s_, folly::StringPiece expected) {
839  EXPECT_EQ(expected, s_);
840  return expected;
841  };
842 
843  auto const checker = [&](folly::StringPiece expected) {
844  EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
845  };
846 
847  checker("this");
848  checker("is");
849  checker("just");
850  checker("");
851  checker("a");
852  checker(" test");
853  checker("string");
854  checker("");
855  checker("");
856 
857  EXPECT_TRUE(p.empty());
858 }
std::string s_
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
def next(obj)
Definition: ast.py:58
TEST ( StringPiece  ,
NoInvalidImplicitConversions   
)

Definition at line 860 of file RangeTest.cpp.

References EXPECT_TRUE, s, and string.

860  {
861  struct IsString {
862  bool operator()(folly::Range<int*>) {
863  return false;
864  }
865  bool operator()(folly::StringPiece) {
866  return true;
867  }
868  };
869 
870  std::string s = "hello";
871  EXPECT_TRUE(IsString()(s));
872 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
static set< string > s
TEST ( qfind  ,
UInt32_Ranges   
)

Definition at line 874 of file RangeTest.cpp.

References a, b, EXPECT_EQ, folly::qfind(), and folly::Range< Iter >::size().

874  {
875  vector<uint32_t> a({1, 2, 3, 260, 5});
876  vector<uint32_t> b({2, 3, 4});
877 
878  auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
879  auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
880 
881  EXPECT_EQ(qfind(a_range, b_range), string::npos);
882 
883  a[3] = 4;
884  EXPECT_EQ(qfind(a_range, b_range), 1);
885 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr size_type size() const
Definition: Range.h:431
char a
size_t qfind(const Range< Iter > &haystack, const Range< Iter > &needle, Comp eq=Comp())
Definition: Range.h:1292
TEST ( NonConstTest  ,
StringPiece   
)

Definition at line 1064 of file RangeTest.cpp.

References EXPECT_EQ, s, and string.

1064  {
1065  std::string hello("hello");
1066  MutableStringPiece sp(&hello.front(), hello.size());
1067  sp[0] = 'x';
1068  EXPECT_EQ("xello", hello);
1069  {
1070  StringPiece s(sp);
1071  EXPECT_EQ("xello", s);
1072  }
1073  {
1074  ByteRange r1(sp);
1075  MutableByteRange r2(sp);
1076  }
1077 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * string
Definition: Conv.cpp:212
static set< string > s
TEST ( RangeFunc  ,
Vector   
)

Definition at line 1110 of file RangeTest.cpp.

References testRangeFunc(), and x.

1110  {
1111  std::vector<int> x;
1112  testRangeFunc(x, 0);
1113  x.push_back(2);
1114  testRangeFunc(x, 1);
1115  testRangeFunc(std::vector<int>{1, 2}, 2);
1116 }
const int x
void testRangeFunc(C &&x, size_t n)
Definition: RangeTest.cpp:1093
TEST ( RangeFunc  ,
Array   
)

Definition at line 1118 of file RangeTest.cpp.

References testRangeFunc(), and x.

1118  {
1119  std::array<int, 3> x;
1120  testRangeFunc(x, 3);
1121 }
const int x
void testRangeFunc(C &&x, size_t n)
Definition: RangeTest.cpp:1093
TEST ( RangeFunc  ,
CArray   
)

Definition at line 1123 of file RangeTest.cpp.

References testRangeFunc().

1123  {
1124  int x[]{1, 2, 3, 4};
1125  testRangeFunc(x, 4);
1126 }
Definition: InvokeTest.cpp:58
void testRangeFunc(C &&x, size_t n)
Definition: RangeTest.cpp:1093
TEST ( RangeFunc  ,
ConstexprCArray   
)

Definition at line 1128 of file RangeTest.cpp.

References EXPECT_EQ, and folly::range().

1128  {
1129  static constexpr const int numArray[4] = {3, 17, 1, 9};
1130  constexpr const auto numArrayRange = range(numArray);
1131  EXPECT_EQ(17, numArrayRange[1]);
1132  constexpr const auto numArrayRangeSize = numArrayRange.size();
1133  EXPECT_EQ(4, numArrayRangeSize);
1134 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
TEST ( RangeFunc  ,
ConstexprStdArray   
)

Definition at line 1136 of file RangeTest.cpp.

References EXPECT_EQ, and folly::range().

1136  {
1137  static constexpr const std::array<int, 4> numArray = {{3, 17, 1, 9}};
1138  constexpr const auto numArrayRange = range(numArray);
1139  EXPECT_EQ(17, numArrayRange[1]);
1140  constexpr const auto numArrayRangeSize = numArrayRange.size();
1141  EXPECT_EQ(4, numArrayRangeSize);
1142 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
TEST ( RangeFunc  ,
ConstexprStdArrayZero   
)

Definition at line 1144 of file RangeTest.cpp.

References EXPECT_EQ, and folly::range().

1144  {
1145  static constexpr const std::array<int, 0> numArray = {};
1146  constexpr const auto numArrayRange = range(numArray);
1147  constexpr const auto numArrayRangeSize = numArrayRange.size();
1148  EXPECT_EQ(0, numArrayRangeSize);
1149 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
TEST ( RangeFunc  ,
ConstexprIteratorPair   
)

Definition at line 1151 of file RangeTest.cpp.

References EXPECT_EQ, and folly::range().

1151  {
1152  static constexpr const int numArray[4] = {3, 17, 1, 9};
1153  constexpr const auto numPtr = static_cast<const int*>(numArray);
1154  constexpr const auto numIterRange = range(numPtr + 1, numPtr + 3);
1155  EXPECT_EQ(1, numIterRange[1]);
1156  constexpr const auto numIterRangeSize = numIterRange.size();
1157  EXPECT_EQ(2, numIterRangeSize);
1158 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
TEST ( RangeFunc  ,
ConstexprCollection   
)

Definition at line 1160 of file RangeTest.cpp.

References folly::data(), data_, EXPECT_EQ, folly::range(), s, and folly::size().

1160  {
1161  class IntCollection {
1162  public:
1163  constexpr IntCollection(const int* d, size_t s) : data_(d), size_(s) {}
1164  constexpr const int* data() const {
1165  return data_;
1166  }
1167  constexpr size_t size() const {
1168  return size_;
1169  }
1170 
1171  private:
1172  const int* data_;
1173  size_t size_;
1174  };
1175  static constexpr const int numArray[4] = {3, 17, 1, 9};
1176  constexpr const auto numPtr = static_cast<const int*>(numArray);
1177  constexpr const auto numColl = IntCollection(numPtr + 1, 2);
1178  constexpr const auto numCollRange = range(numColl);
1179  EXPECT_EQ(1, numCollRange[1]);
1180  constexpr const auto numCollRangeSize = numCollRange.size();
1181  EXPECT_EQ(2, numCollRangeSize);
1182 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static set< string > s
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
StringPiece data_
TEST ( CRangeFunc  ,
CArray   
)

Definition at line 1184 of file RangeTest.cpp.

References folly::crange(), testing::ElementsAreArray(), EXPECT_THAT, EXPECT_TRUE, and folly::value().

1184  {
1185  int numArray[4] = {3, 17, 1, 9};
1186  auto const numArrayRange = crange(numArray);
1187  EXPECT_TRUE(
1188  (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
1189  EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
1190 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
constexpr auto crange(Collection const &v) -> Range< decltype(v.data())>
Definition: Range.h:1131
TEST ( CRangeFunc  ,
StdArray   
)

Definition at line 1192 of file RangeTest.cpp.

References folly::crange(), testing::ElementsAreArray(), EXPECT_THAT, EXPECT_TRUE, and folly::value().

1192  {
1193  std::array<int, 4> numArray = {{3, 17, 1, 9}};
1194  auto const numArrayRange = crange(numArray);
1195  EXPECT_TRUE(
1196  (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
1197  EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
1198 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
constexpr auto crange(Collection const &v) -> Range< decltype(v.data())>
Definition: Range.h:1131
TEST ( CRangeFunc  ,
StdArrayZero   
)

Definition at line 1200 of file RangeTest.cpp.

References folly::crange(), EXPECT_THAT, EXPECT_TRUE, and folly::value().

1200  {
1201  std::array<int, 0> numArray = {};
1202  auto const numArrayRange = crange(numArray);
1203  EXPECT_TRUE(
1204  (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
1205  EXPECT_THAT(numArrayRange, testing::IsEmpty());
1206 }
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
constexpr auto crange(Collection const &v) -> Range< decltype(v.data())>
Definition: Range.h:1131
TEST ( CRangeFunc  ,
Collection   
)

Definition at line 1208 of file RangeTest.cpp.

References folly::crange(), folly::data(), data_, testing::ElementsAreArray(), EXPECT_THAT, EXPECT_TRUE, s, folly::size(), and folly::value().

1208  {
1209  class IntCollection {
1210  public:
1211  constexpr IntCollection(int* d, size_t s) : data_(d), size_(s) {}
1212  constexpr int const* data() const {
1213  return data_;
1214  }
1215  constexpr size_t size() const {
1216  return size_;
1217  }
1218 
1219  private:
1220  int* data_;
1221  size_t size_;
1222  };
1223  int numArray[4] = {3, 17, 1, 9};
1224  auto numPtr = static_cast<int*>(numArray);
1225  auto numColl = IntCollection(numPtr + 1, 2);
1226  auto const numCollRange = crange(numColl);
1227  EXPECT_TRUE(
1228  (std::is_same<int const*, decltype(numCollRange)::iterator>::value));
1229  EXPECT_THAT(numCollRange, testing::ElementsAreArray({17, 1}));
1230 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
static set< string > s
constexpr auto crange(Collection const &v) -> Range< decltype(v.data())>
Definition: Range.h:1131
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
StringPiece data_
TEST ( ReplaceAt  ,
exhaustiveTest   
)

Definition at line 1254 of file RangeTest.cpp.

References dist, EXPECT_EQ, get_rand_str(), i, rd, and string.

1254  {
1255  char input[] = "this is nice and long input";
1256  auto msp = MutableStringPiece(input);
1257  auto str = std::string(input);
1258  std::random_device rd;
1259  std::mt19937 gen(rd());
1260  std::uniform_int_distribution<> dist('a', 'z');
1261 
1262  for (int i = 0; i < 100; ++i) {
1263  for (size_t j = 1; j <= msp.size(); ++j) {
1264  auto replacement = get_rand_str(j, dist, gen);
1265  for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1266  msp.replaceAt(pos, replacement);
1267  str.replace(pos, replacement.size(), replacement);
1268  EXPECT_EQ(msp.compare(str), 0);
1269  }
1270  }
1271  }
1272 
1273  // too far
1274  EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1275 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string get_rand_str(size_t size, std::uniform_int_distribution<> &dist, std::mt19937 &gen)
Definition: RangeTest.cpp:1232
Range< char * > MutableStringPiece
Definition: Range.h:1162
std::uniform_int_distribution< milliseconds::rep > dist
std::random_device rd
const char * string
Definition: Conv.cpp:212
Range< const char * > StringPiece
TEST ( ReplaceAll  ,
basicTest   
)

Definition at line 1277 of file RangeTest.cpp.

References EXPECT_EQ, and string.

1277  {
1278  char input[] = "this is nice and long input";
1279  auto orig = std::string(input);
1280  auto msp = MutableStringPiece(input);
1281 
1282  EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1283  EXPECT_EQ("thsi si nice and long input", msp);
1284  EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1285  EXPECT_EQ(msp, orig);
1286 
1287  EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1288  EXPECT_EQ(msp, orig);
1289 
1290  // at the very beginning
1291  EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1292  EXPECT_EQ("siht is nice and long input", msp);
1293  EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1294  EXPECT_EQ(msp, orig);
1295 
1296  // at the very end
1297  EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1298  EXPECT_EQ("this is nice and long soput", msp);
1299  EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1300  EXPECT_EQ(msp, orig);
1301 
1302  // all spaces
1303  EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1304  EXPECT_EQ("this@is@nice@and@long@input", msp);
1305  EXPECT_EQ(msp.replaceAll("@", " "), 5);
1306  EXPECT_EQ(msp, orig);
1307 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Range< char * > MutableStringPiece
Definition: Range.h:1162
const char * string
Definition: Conv.cpp:212
TEST ( ReplaceAll  ,
randomTest   
)

Definition at line 1309 of file RangeTest.cpp.

References dist, EXPECT_EQ, get_rand_str(), i, rd, and string.

1309  {
1310  char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1311  auto orig = std::string(input);
1312  auto msp = MutableStringPiece(input);
1313 
1314  std::random_device rd;
1315  std::mt19937 gen(rd());
1316  std::uniform_int_distribution<> dist('A', 'Z');
1317 
1318  for (int i = 0; i < 100; ++i) {
1319  for (size_t j = 1; j <= orig.size(); ++j) {
1320  auto replacement = get_rand_str(j, dist, gen);
1321  for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1322  auto piece = orig.substr(pos, j);
1323  EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1324  EXPECT_EQ(msp.find(replacement), pos);
1325  EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1326  EXPECT_EQ(msp, orig);
1327  }
1328  }
1329  }
1330 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string get_rand_str(size_t size, std::uniform_int_distribution<> &dist, std::mt19937 &gen)
Definition: RangeTest.cpp:1232
Range< char * > MutableStringPiece
Definition: Range.h:1162
std::uniform_int_distribution< milliseconds::rep > dist
std::random_device rd
const char * string
Definition: Conv.cpp:212
TEST ( ReplaceAll  ,
BadArg   
)

Definition at line 1332 of file RangeTest.cpp.

References folly::gen::all(), count, and EXPECT_EQ.

1332  {
1333  int count = 0;
1334  auto fst = "longer";
1335  auto snd = "small";
1336  char input[] = "meh meh meh";
1337  auto all = MutableStringPiece(input);
1338 
1339  try {
1340  all.replaceAll(fst, snd);
1341  } catch (std::invalid_argument&) {
1342  ++count;
1343  }
1344 
1345  try {
1346  all.replaceAll(snd, fst);
1347  } catch (std::invalid_argument&) {
1348  ++count;
1349  }
1350 
1351  EXPECT_EQ(count, 2);
1352 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Range< char * > MutableStringPiece
Definition: Range.h:1162
int * count
Composed all(Predicate pred=Predicate())
Definition: Base.h:786
TEST ( Range  ,
Constructors   
)

Definition at line 1354 of file RangeTest.cpp.

References c, and EXPECT_EQ.

1354  {
1355  vector<int> c = {1, 2, 3};
1356  typedef Range<vector<int>::iterator> RangeType;
1357  typedef Range<vector<int>::const_iterator> ConstRangeType;
1358  RangeType cr(c.begin(), c.end());
1359  auto subpiece1 = ConstRangeType(cr, 1, 5);
1360  auto subpiece2 = ConstRangeType(cr, 1);
1361  EXPECT_EQ(subpiece1.size(), 2);
1362  EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1363  EXPECT_EQ(subpiece1.end(), subpiece2.end());
1364 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char c
TEST ( Range  ,
ArrayConstructors   
)

Definition at line 1366 of file RangeTest.cpp.

References EXPECT_EQ.

1366  {
1367  auto charArray = std::array<char, 4>{{'t', 'e', 's', 't'}};
1368  auto constCharArray = std::array<char, 6>{{'f', 'o', 'o', 'b', 'a', 'r'}};
1369  auto emptyArray = std::array<char, 0>{};
1370 
1371  auto sp1 = StringPiece{charArray};
1372  EXPECT_EQ(4, sp1.size());
1373  EXPECT_EQ(charArray.data(), sp1.data());
1374 
1375  auto sp2 = StringPiece(constCharArray);
1376  EXPECT_EQ(6, sp2.size());
1377  EXPECT_EQ(constCharArray.data(), sp2.data());
1378 
1379  auto msp = MutableStringPiece(charArray);
1380  EXPECT_EQ(4, msp.size());
1381  EXPECT_EQ(charArray.data(), msp.data());
1382 
1383  auto esp = StringPiece(emptyArray);
1384  EXPECT_EQ(0, esp.size());
1385  EXPECT_EQ(nullptr, esp.data());
1386 
1387  auto emsp = MutableStringPiece(emptyArray);
1388  EXPECT_EQ(0, emsp.size());
1389  EXPECT_EQ(nullptr, emsp.data());
1390 
1391  static constexpr std::array<int, 4> numArray = {{3, 17, 1, 9}};
1392  constexpr auto numRange = Range<const int*>{numArray};
1393  EXPECT_EQ(17, numRange[1]);
1394 
1395  static constexpr std::array<int, 0> emptyNumArray{};
1396  constexpr auto emptyNumRange = Range<const int*>{emptyNumArray};
1397  EXPECT_EQ(0, emptyNumRange.size());
1398 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Range< char * > MutableStringPiece
Definition: Range.h:1162
Range< const char * > StringPiece
TEST ( Range  ,
ConstexprAccessors   
)

Definition at line 1400 of file RangeTest.cpp.

References folly::range().

1400  {
1401  constexpr StringPiece piece = range("hello");
1402  static_assert(piece.size() == 6u, "");
1403  static_assert(piece.end() - piece.begin() == 6u, "");
1404  static_assert(piece.data() == piece.begin(), "");
1405  static_assert(piece.start() == piece.begin(), "");
1406  static_assert(piece.cbegin() == piece.begin(), "");
1407  static_assert(piece.cend() == piece.end(), "");
1408  static_assert(*piece.begin() == 'h', "");
1409  static_assert(*(piece.end() - 1) == '\0', "");
1410 }
Gen range(Value begin, Value end)
Definition: Base.h:467
TEST ( Range  ,
LiteralSuffix   
)

Definition at line 1412 of file RangeTest.cpp.

References EXPECT_EQ.

1412  {
1413  constexpr auto literalPiece = "hello"_sp;
1414  constexpr StringPiece piece = "hello";
1415  EXPECT_EQ(literalPiece, piece);
1416  constexpr auto literalPiece8 = u8"hello"_sp;
1417  constexpr Range<char const*> piece8 = u8"hello";
1418  EXPECT_EQ(literalPiece8, piece8);
1419  constexpr auto literalPiece16 = u"hello"_sp;
1420  constexpr Range<char16_t const*> piece16{u"hello", 5};
1421  EXPECT_EQ(literalPiece16, piece16);
1422  constexpr auto literalPiece32 = U"hello"_sp;
1423  constexpr Range<char32_t const*> piece32{U"hello", 5};
1424  EXPECT_EQ(literalPiece32, piece32);
1425  constexpr auto literalPieceW = L"hello"_sp;
1426  constexpr Range<wchar_t const*> pieceW{L"hello", 5};
1427  EXPECT_EQ(literalPieceW, pieceW);
1428 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Range  ,
LiteralSuffixContainsNulBytes   
)

Definition at line 1430 of file RangeTest.cpp.

References EXPECT_EQ.

1430  {
1431  constexpr auto literalPiece = "\0foo\0"_sp;
1432  EXPECT_EQ(5u, literalPiece.size());
1433 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Range  ,
StringPieceExplicitConversionOperator   
)

Definition at line 1452 of file RangeTest.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_FALSE, EXPECT_THAT, EXPECT_TRUE, string, value, and folly::value().

1452  {
1453  using PieceM = StringPiece;
1454  using PieceC = StringPiece const;
1455 
1458  EXPECT_FALSE((std::is_convertible<PieceM, std::vector<char>>::value));
1462  EXPECT_TRUE((std::is_constructible<std::vector<char>, PieceM>::value));
1464 
1467  EXPECT_FALSE((std::is_convertible<PieceC, std::vector<char>>::value));
1471  EXPECT_TRUE((std::is_constructible<std::vector<char>, PieceC>::value));
1473 
1475  std::array<char, 5> array = {{'h', 'e', 'l', 'l', 'o'}};
1476  PieceM piecem{array};
1477  PieceC piecec{array};
1478  std::allocator<char> alloc;
1479 
1480  EXPECT_EQ("hello", std::string(piecem));
1481  EXPECT_EQ("hello", std::string(piecec));
1482  EXPECT_EQ("hello", std::string{piecem});
1483  EXPECT_EQ("hello", std::string{piecec});
1484  EXPECT_EQ("hello", piecem.to<std::string>());
1485  EXPECT_EQ("hello", piecec.to<std::string>());
1486  EXPECT_EQ("hello", piecem.to<std::string>(alloc));
1487  EXPECT_EQ("hello", piecec.to<std::string>(alloc));
1488 
1489  EXPECT_THAT(std::vector<char>(piecem), ElementsAreArray(array));
1490  EXPECT_THAT(std::vector<char>(piecec), ElementsAreArray(array));
1491  EXPECT_THAT(std::vector<char>{piecem}, ElementsAreArray(array));
1492  EXPECT_THAT(std::vector<char>{piecec}, ElementsAreArray(array));
1493  EXPECT_THAT(piecem.to<std::vector<char>>(), ElementsAreArray(array));
1494  EXPECT_THAT(piecec.to<std::vector<char>>(), ElementsAreArray(array));
1495  EXPECT_THAT(piecem.to<std::vector<char>>(alloc), ElementsAreArray(array));
1496  EXPECT_THAT(piecec.to<std::vector<char>>(alloc), ElementsAreArray(array));
1497 
1498  EXPECT_EQ("hello", fake_string_view(piecem));
1499  EXPECT_EQ("hello", fake_string_view(piecec));
1500  EXPECT_EQ("hello", fake_string_view{piecem});
1501  EXPECT_EQ("hello", fake_string_view{piecec});
1502  EXPECT_EQ("hello", piecem.to<fake_string_view>());
1503  EXPECT_EQ("hello", piecec.to<fake_string_view>());
1504  EXPECT_EQ("hello", piecem.to<fake_string_view>(tag{}));
1505  EXPECT_EQ("hello", piecec.to<fake_string_view>(tag{}));
1506 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
const char * string
Definition: Conv.cpp:212
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
Range< const char * > StringPiece
TEST ( Range  ,
MutableStringPieceExplicitConversionOperator   
)

Definition at line 1508 of file RangeTest.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_FALSE, EXPECT_THAT, EXPECT_TRUE, s, folly::Range< Iter >::size(), string, folly::TEST(), value, and folly::value().

1508  {
1509  using PieceM = MutableStringPiece;
1510  using PieceC = MutableStringPiece const;
1511 
1514  EXPECT_FALSE((std::is_convertible<PieceM, std::vector<char>>::value));
1518  EXPECT_TRUE((std::is_constructible<std::vector<char>, PieceM>::value));
1520 
1523  EXPECT_FALSE((std::is_convertible<PieceC, std::vector<char>>::value));
1527  EXPECT_TRUE((std::is_constructible<std::vector<char>, PieceC>::value));
1529 
1531  std::array<char, 5> array = {{'h', 'e', 'l', 'l', 'o'}};
1532  PieceM piecem{array};
1533  PieceC piecec{array};
1534  std::allocator<char> alloc;
1535 
1536  EXPECT_EQ("hello", std::string(piecem));
1537  EXPECT_EQ("hello", std::string(piecec));
1538  EXPECT_EQ("hello", std::string{piecem});
1539  EXPECT_EQ("hello", std::string{piecec});
1540  EXPECT_EQ("hello", piecem.to<std::string>());
1541  EXPECT_EQ("hello", piecec.to<std::string>());
1542  EXPECT_EQ("hello", piecem.to<std::string>(alloc));
1543  EXPECT_EQ("hello", piecec.to<std::string>(alloc));
1544 
1545  EXPECT_THAT(std::vector<char>(piecem), ElementsAreArray(array));
1546  EXPECT_THAT(std::vector<char>(piecec), ElementsAreArray(array));
1547  EXPECT_THAT(std::vector<char>{piecem}, ElementsAreArray(array));
1548  EXPECT_THAT(std::vector<char>{piecec}, ElementsAreArray(array));
1549  EXPECT_THAT(piecem.to<std::vector<char>>(), ElementsAreArray(array));
1550  EXPECT_THAT(piecec.to<std::vector<char>>(), ElementsAreArray(array));
1551  EXPECT_THAT(piecem.to<std::vector<char>>(alloc), ElementsAreArray(array));
1552  EXPECT_THAT(piecec.to<std::vector<char>>(alloc), ElementsAreArray(array));
1553 
1554  EXPECT_EQ("hello", fake_string_view(piecem));
1555  EXPECT_EQ("hello", fake_string_view(piecec));
1556  EXPECT_EQ("hello", fake_string_view{piecem});
1557  EXPECT_EQ("hello", fake_string_view{piecec});
1558  EXPECT_EQ("hello", piecem.to<fake_string_view>());
1559  EXPECT_EQ("hello", piecec.to<fake_string_view>());
1560  EXPECT_EQ("hello", piecem.to<fake_string_view>(tag{}));
1561  EXPECT_EQ("hello", piecec.to<fake_string_view>(tag{}));
1562 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Range< char * > MutableStringPiece
Definition: Range.h:1162
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
const char * string
Definition: Conv.cpp:212
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
template<class C >
void testRangeFunc ( C &&  x,
size_t  n 
)

Definition at line 1093 of file RangeTest.cpp.

References folly::Range< Iter >::begin(), dataPtr(), folly::Range< Iter >::end(), EXPECT_EQ, folly::gen::move, folly::range(), folly::Range< Iter >::size(), folly::value(), and x.

Referenced by TEST().

1093  {
1094  const auto& cx = x;
1095  // type, conversion checks
1096  using R1Iter =
1098  Range<R1Iter> r1 = range(std::forward<C>(x));
1099  Range<const int*> r2 = range(std::forward<C>(x));
1100  Range<const int*> r3 = range(cx);
1101  Range<const int*> r5 = range(std::move(cx));
1102  EXPECT_EQ(r1.begin(), dataPtr(x));
1103  EXPECT_EQ(r1.end(), dataPtr(x) + n);
1104  EXPECT_EQ(n, r1.size());
1105  EXPECT_EQ(n, r2.size());
1106  EXPECT_EQ(n, r3.size());
1107  EXPECT_EQ(n, r5.size());
1108 }
Definition: InvokeTest.cpp:58
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const int x
constexpr size_type size() const
Definition: Range.h:431
Gen range(Value begin, Value end)
Definition: Base.h:467
typename T::type _t
Definition: Traits.h:171
static const char *const value
Definition: Conv.cpp:50
constexpr Iter end() const
Definition: Range.h:455
constexpr Iter begin() const
Definition: Range.h:452
Container::value_type * dataPtr(Container &cont)
Definition: RangeTest.cpp:1082
TYPED_TEST ( NeedleFinderTest  ,
Null   
)

Definition at line 919 of file RangeTest.cpp.

References EXPECT_EQ, and s.

919  {
920  { // null characters in the string
921  string s(10, char(0));
922  s[5] = 'b';
923  string delims("abc");
924  EXPECT_EQ(5, this->find_first_byte_of(s, delims));
925  }
926  { // null characters in delim
927  string s("abc");
928  string delims(10, char(0));
929  delims[3] = 'c';
930  delims[7] = 'b';
931  EXPECT_EQ(1, this->find_first_byte_of(s, delims));
932  }
933  { // range not terminated by null character
934  string buf = "abcdefghijklmnopqrstuvwxyz";
935  StringPiece s(buf.data() + 5, 3);
936  StringPiece delims("z");
937  EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
938  }
939 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s
TYPED_TEST ( NeedleFinderTest  ,
DelimDuplicates   
)

Definition at line 941 of file RangeTest.cpp.

References EXPECT_EQ.

941  {
942  string delims(1000, 'b');
943  EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
944  EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
945 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TYPED_TEST ( NeedleFinderTest  ,
Empty   
)

Definition at line 947 of file RangeTest.cpp.

References a, b, and EXPECT_EQ.

947  {
948  string a = "abc";
949  string b = "";
950  EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
951  EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
952  EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
953 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
TYPED_TEST ( NeedleFinderTest  ,
Unaligned   
)

Definition at line 955 of file RangeTest.cpp.

References a, b, EXPECT_EQ, i, and s.

955  {
956  // works correctly even if input buffers are not 16-byte aligned
957  string s = "0123456789ABCDEFGH";
958  for (size_t i = 0; i < s.size(); ++i) {
959  StringPiece a(s.c_str() + i);
960  for (size_t j = 0; j < s.size(); ++j) {
961  StringPiece b(s.c_str() + j);
962  EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
963  }
964  }
965 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
static set< string > s
TYPED_TEST ( NeedleFinderTest  ,
Needles256   
)

Definition at line 969 of file RangeTest.cpp.

References EXPECT_EQ, i, max, min, and folly::Range< const char * >::npos.

969  {
970  string needles;
973  // make the size ~big to avoid any edge-case branches for tiny haystacks
974  const int haystackSize = 50;
975  for (int i = minValue; i <= maxValue; i++) { // <=
976  needles.push_back(i);
977  }
978  EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
979  for (int i = minValue; i <= maxValue; i++) {
980  EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
981  }
982 
983  needles.append("these are redundant characters");
984  EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
985  for (int i = minValue; i <= maxValue; i++) {
986  EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
987  }
988 }
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogLevel min
Definition: LogLevel.cpp:30
TYPED_TEST ( NeedleFinderTest  ,
Base   
)

Definition at line 990 of file RangeTest.cpp.

References EXPECT_EQ, i, s, and string.

990  {
991  for (size_t i = 0; i < 32; ++i) {
992  for (int j = 0; j < 32; ++j) {
993  string s = string(i, 'X') + "abca" + string(i, 'X');
994  string delims = string(j, 'Y') + "a" + string(j, 'Y');
995  EXPECT_EQ(i, this->find_first_byte_of(s, delims));
996  }
997  }
998 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * string
Definition: Conv.cpp:212
static set< string > s
TYPED_TEST ( NeedleFinderTest  ,
NoSegFault   
)

Definition at line 1026 of file RangeTest.cpp.

References folly::Range< Iter >::advance(), ASSERT_TRUE, folly::Range< Iter >::begin(), createProtectedBuf(), folly::Range< Iter >::empty(), folly::Range< Iter >::end(), EXPECT_EQ, freeProtectedBuf(), i, folly::Range< const char * >::npos, folly::Range< Iter >::pop_back(), and string.

1026  {
1027  const string base = string(32, 'a') + string("b");
1028  const string delims = string(32, 'c') + string("b");
1029  for (int i = 0; i <= 32; i++) {
1030  for (int j = 0; j <= 33; j++) {
1031  for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1032  StringPiece s1(base);
1033  s1.advance(i);
1034  ASSERT_TRUE(!s1.empty());
1035  if (!shouldFind) {
1036  s1.pop_back();
1037  }
1038  StringPiece s2(delims);
1039  s2.advance(j);
1040  char* buf1;
1041  char* buf2;
1042  createProtectedBuf(s1, &buf1);
1043  createProtectedBuf(s2, &buf2);
1044  // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
1045  // string(s1.data(), s1.size()).c_str(), s1.size(),
1046  // string(s2.data(), s2.size()).c_str(), s2.size());
1047  auto r1 = this->find_first_byte_of(s1, s2);
1048  auto f1 =
1049  std::find_first_of(s1.begin(), s1.end(), s2.begin(), s2.end());
1050  auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
1051  EXPECT_EQ(r1, e1);
1052  auto r2 = this->find_first_byte_of(s2, s1);
1053  auto f2 =
1054  std::find_first_of(s2.begin(), s2.end(), s1.begin(), s1.end());
1055  auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1056  EXPECT_EQ(r2, e2);
1057  freeProtectedBuf(buf1);
1058  freeProtectedBuf(buf2);
1059  }
1060  }
1061  }
1062 }
void createProtectedBuf(StringPiece &contents, char **buf)
Definition: RangeTest.cpp:1006
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void freeProtectedBuf(char *buf)
Definition: RangeTest.cpp:1021
const char * string
Definition: Conv.cpp:212
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TYPED_TEST_CASE ( NeedleFinderTest  ,
NeedleFinders   
)

Variable Documentation

const size_t kPageSize = 4096

Definition at line 1000 of file RangeTest.cpp.

Referenced by createProtectedBuf(), and folly::TEST().