28 #include <type_traits> 31 #include <boost/algorithm/string/trim.hpp> 32 #include <boost/range/concepts.hpp> 39 using namespace folly;
48 const char*
foo =
"foo";
49 const char*
foo2 =
"foo";
60 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
80 const char* foobarbaz =
"foobarbaz";
83 s.reset(foobarbaz, strlen(foobarbaz));
89 s.assign(foobarbaz, foobarbaz + 3);
95 s.reset(foobarbaz, strlen(foobarbaz));
135 EXPECT_EQ(s.rfind(
'\0'), s.str().rfind(
'\0'));
139 s.reset(foobarbaz, strlen(foobarbaz));
175 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
181 s.reset(foobarbaz + 3, 3);
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) {
259 for (
size_t j = i + 1; j <
count; ++j) {
271 static_cast<const void*>(a.
begin()), static_cast<const void*>(b.
begin()));
273 static_cast<const void*>(a.
end()), static_cast<const void*>(b.
end()));
300 constexpr
const char* helloArray =
"hello";
304 static_assert(hello1.size() == 5,
"hello size should be 5 at compile time");
308 static_assert(hello2.size() == 5,
"hello size should be 5 at compile time");
490 boost::algorithm::trim(a);
497 auto const s =
"this is just a test string";
555 auto const s =
"this is just a test string";
620 auto const s =
"this is just a test string";
630 EXPECT_EQ(std::next(s, 5), p.begin());
631 EXPECT_EQ(e, p.end());
632 EXPECT_EQ(
"this", x);
637 EXPECT_EQ(std::next(s, 8), p.begin());
638 EXPECT_EQ(e, p.end());
644 EXPECT_EQ(std::next(s, 10), p.begin());
645 EXPECT_EQ(e, p.end());
651 EXPECT_EQ(std::next(s, 13), p.begin());
652 EXPECT_EQ(e, p.end());
658 EXPECT_EQ(std::next(s, 14), p.begin());
659 EXPECT_EQ(e, p.end());
665 EXPECT_EQ(std::next(s, 16), p.begin());
666 EXPECT_EQ(e, p.end());
672 EXPECT_EQ(std::next(s, 21), p.begin());
673 EXPECT_EQ(e, p.end());
674 EXPECT_EQ(
"test", x);
679 EXPECT_EQ(e, p.begin());
680 EXPECT_EQ(e, p.end());
681 EXPECT_EQ(
"string", x);
686 EXPECT_EQ(e, p.begin());
687 EXPECT_EQ(e, p.end());
703 auto const s =
"this is just a test string";
713 EXPECT_EQ(std::next(s, 6), p.begin());
714 EXPECT_EQ(e, p.end());
715 EXPECT_EQ(
"this", x);
720 EXPECT_EQ(std::next(s, 10), p.begin());
721 EXPECT_EQ(e, p.end());
727 EXPECT_EQ(std::next(s, 12), p.begin());
728 EXPECT_EQ(e, p.end());
734 EXPECT_EQ(std::next(s, 16), p.begin());
735 EXPECT_EQ(e, p.end());
741 EXPECT_EQ(std::next(s, 18), p.begin());
742 EXPECT_EQ(e, p.end());
748 EXPECT_EQ(std::next(s, 21), p.begin());
749 EXPECT_EQ(e, p.end());
755 EXPECT_EQ(std::next(s, 28), p.begin());
756 EXPECT_EQ(e, p.end());
757 EXPECT_EQ(
" test", x);
762 EXPECT_EQ(e, p.begin());
763 EXPECT_EQ(e, p.end());
764 EXPECT_EQ(
"string", x);
769 EXPECT_EQ(e, p.begin());
770 EXPECT_EQ(e, p.end());
776 EXPECT_EQ(e, p.begin());
777 EXPECT_EQ(e, p.end());
793 auto const s =
"this is just a test string";
795 auto const delimiter =
' ';
828 auto const s =
"this is just a test string";
830 auto const delimiter =
" ";
875 vector<uint32_t>
a({1, 2, 3, 260, 5});
876 vector<uint32_t>
b({2, 3, 4});
887 template <
typename NeedleFinder>
891 return NeedleFinder::find_first_byte_of(haystack, needles);
916 ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, ByteSetNeedleFinder>;
921 string s(10,
char(0));
923 string delims(
"abc");
924 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
928 string delims(10,
char(0));
931 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
934 string buf =
"abcdefghijklmnopqrstuvwxyz";
937 EXPECT_EQ(string::npos, this->find_first_byte_of(
s, delims));
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));
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));
957 string s =
"0123456789ABCDEFGH";
958 for (
size_t i = 0;
i < s.size(); ++
i) {
960 for (
size_t j = 0; j < s.size(); ++j) {
962 EXPECT_EQ((
i > j) ? 0 : j -
i, this->find_first_byte_of(
a,
b));
974 const int haystackSize = 50;
975 for (
int i = minValue;
i <= maxValue;
i++) {
976 needles.push_back(
i);
979 for (
int i = minValue;
i <= maxValue;
i++) {
980 EXPECT_EQ(0, this->find_first_byte_of(
string(haystackSize,
i), needles));
983 needles.append(
"these are redundant characters");
985 for (
int i = minValue;
i <= maxValue;
i++) {
986 EXPECT_EQ(0, this->find_first_byte_of(
string(haystackSize,
i), needles));
991 for (
size_t i = 0;
i < 32; ++
i) {
992 for (
int j = 0; j < 32; ++j) {
995 EXPECT_EQ(
i, this->find_first_byte_of(s, delims));
1008 char* pageAlignedBuf = (
char*)
aligned_malloc(2 * kPageSize, kPageSize);
1009 if (pageAlignedBuf ==
nullptr) {
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;
1022 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
1029 for (
int i = 0;
i <= 32;
i++) {
1030 for (
int j = 0; j <= 33; j++) {
1031 for (
int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1047 auto r1 = this->find_first_byte_of(s1, s2);
1052 auto r2 = this->find_first_byte_of(s2, s1);
1081 template <
class Container>
1082 typename Container::value_type*
dataPtr(Container& cont) {
1087 template <
class T,
size_t N>
1119 std::array<int, 3>
x;
1124 int x[]{1, 2, 3, 4};
1129 static constexpr
const int numArray[4] = {3, 17, 1, 9};
1130 constexpr
const auto numArrayRange =
range(numArray);
1132 constexpr
const auto numArrayRangeSize = numArrayRange.size();
1136 TEST(RangeFunc, ConstexprStdArray) {
1137 static constexpr
const std::array<int, 4> numArray = {{3, 17, 1, 9}};
1138 constexpr
const auto numArrayRange =
range(numArray);
1140 constexpr
const auto numArrayRangeSize = numArrayRange.size();
1144 TEST(RangeFunc, ConstexprStdArrayZero) {
1145 static constexpr
const std::array<int, 0> numArray = {};
1146 constexpr
const auto numArrayRange =
range(numArray);
1147 constexpr
const auto numArrayRangeSize = numArrayRange.size();
1151 TEST(RangeFunc, ConstexprIteratorPair) {
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);
1156 constexpr
const auto numIterRangeSize = numIterRange.size();
1160 TEST(RangeFunc, ConstexprCollection) {
1161 class IntCollection {
1163 constexpr IntCollection(
const int* d,
size_t s) :
data_(d), size_(s) {}
1164 constexpr
const int*
data()
const {
1167 constexpr
size_t size()
const {
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);
1180 constexpr
const auto numCollRangeSize = numCollRange.size();
1185 int numArray[4] = {3, 17, 1, 9};
1186 auto const numArrayRange =
crange(numArray);
1188 (std::is_same<
int const*, decltype(numArrayRange)::iterator>::
value));
1193 std::array<int, 4> numArray = {{3, 17, 1, 9}};
1194 auto const numArrayRange =
crange(numArray);
1196 (std::is_same<
int const*, decltype(numArrayRange)::iterator>::
value));
1201 std::array<int, 0> numArray = {};
1202 auto const numArrayRange =
crange(numArray);
1204 (std::is_same<
int const*, decltype(numArrayRange)::iterator>::
value));
1209 class IntCollection {
1211 constexpr IntCollection(
int* d,
size_t s) :
data_(d), size_(s) {}
1212 constexpr
int const*
data()
const {
1215 constexpr
size_t size()
const {
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);
1228 (std::is_same<
int const*, decltype(numCollRange)::iterator>::
value));
1234 std::uniform_int_distribution<>&
dist,
1235 std::mt19937& gen) {
1237 for (
size_t i = 0;
i <
size; ++
i) {
1238 ret[
i] =
static_cast<char>(
dist(gen));
1255 char input[] =
"this is nice and long input";
1258 std::random_device
rd;
1259 std::mt19937 gen(
rd());
1260 std::uniform_int_distribution<>
dist(
'a',
'z');
1262 for (
int i = 0;
i < 100; ++
i) {
1263 for (
size_t j = 1; j <= msp.size(); ++j) {
1265 for (
size_t pos = 0; pos < msp.size() - j; ++pos) {
1266 msp.replaceAt(pos, replacement);
1267 str.replace(pos, replacement.size(), replacement);
1278 char input[] =
"this is nice and long input";
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);
1287 EXPECT_EQ(msp.replaceAll(
"abcd",
"efgh"), 0);
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);
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);
1304 EXPECT_EQ(
"this@is@nice@and@long@input", msp);
1310 char input[] =
"abcdefghijklmnoprstuwqz";
1314 std::random_device
rd;
1315 std::mt19937 gen(
rd());
1316 std::uniform_int_distribution<>
dist(
'A',
'Z');
1318 for (
int i = 0;
i < 100; ++
i) {
1319 for (
size_t j = 1; j <= orig.size(); ++j) {
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);
1325 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1334 auto fst =
"longer";
1336 char input[] =
"meh meh meh";
1340 all.replaceAll(fst, snd);
1341 }
catch (std::invalid_argument&) {
1346 all.replaceAll(snd, fst);
1347 }
catch (std::invalid_argument&) {
1355 vector<int>
c = {1, 2, 3};
1358 RangeType cr(c.begin(), c.end());
1359 auto subpiece1 = ConstRangeType(cr, 1, 5);
1360 auto subpiece2 = ConstRangeType(cr, 1);
1362 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1363 EXPECT_EQ(subpiece1.end(), subpiece2.end());
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>{};
1373 EXPECT_EQ(charArray.data(), sp1.data());
1377 EXPECT_EQ(constCharArray.data(), sp2.data());
1381 EXPECT_EQ(charArray.data(), msp.data());
1391 static constexpr std::array<int, 4> numArray = {{3, 17, 1, 9}};
1395 static constexpr std::array<int, 0> emptyNumArray{};
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',
"");
1413 constexpr
auto literalPiece =
"hello"_sp;
1416 constexpr
auto literalPiece8 = u8
"hello"_sp;
1419 constexpr
auto literalPiece16 = u
"hello"_sp;
1422 constexpr
auto literalPiece32 = U
"hello"_sp;
1425 constexpr
auto literalPieceW = L
"hello"_sp;
1431 constexpr
auto literalPiece =
"\0foo\0"_sp;
1448 return rhs == lhs.
piece_;
1475 std::array<char, 5> array = {{
'h',
'e',
'l',
'l',
'o'}};
1476 PieceM piecem{array};
1477 PieceC piecec{array};
1478 std::allocator<char> alloc;
1531 std::array<char, 5> array = {{
'h',
'e',
'l',
'l',
'o'}};
1532 PieceM piecem{array};
1533 PieceC piecec{array};
1534 std::allocator<char> alloc;
1564 #if FOLLY_HAS_STRING_VIEW 1566 std::size_t stringViewSize(std::string_view
s) {
1574 struct TrickyTarget {
1575 TrickyTarget(
char const*,
char const*) : which{1} {}
1576 TrickyTarget(
char const*, std::size_t) : which{2} {}
1577 TrickyTarget(std::string_view) : which{3} {}
1582 struct TrickierTarget {
1583 TrickierTarget(std::deque<char>::const_iterator, std::size_t) : which{1} {}
1584 TrickierTarget(std::string_view) : which{2} {}
1594 std::string_view view(
"baz");
1610 TrickyTarget tt1(piece);
1612 TrickyTarget tt2(view);
1615 std::deque<char> deq;
1620 TrickierTarget tt3(deqRange);
#define EXPECT_LE(val1, val2)
void createProtectedBuf(StringPiece &contents, char **buf)
#define EXPECT_NO_THROW(statement)
#define EXPECT_THROW(statement, expected_exception)
void expectLT(const T &a, const T &b)
#define EXPECT_EQ(val1, val2)
std::string get_rand_str(size_t size, std::uniform_int_distribution<> &dist, std::mt19937 &gen)
void aligned_free(void *aligned_ptr)
constexpr detail::Map< Move > move
size_t qfind_first_byte_of_byteset(const StringPieceLite haystack, const StringPieceLite needles)
void advance(size_type n)
static size_t find_first_byte_of(StringPiece haystack, StringPiece needles)
#define ASSERT_LE(val1, val2)
constexpr size_type size() const
FOLLY_NOINLINE void foo2()
fake_string_view(char const *s, size_type c, tag={})
void freeProtectedBuf(char *buf)
Range< char * > MutableStringPiece
Range split_step(value_type delimiter)
TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes)
—— Concurrent Priority Queue Implementation ——
requires E e noexcept(noexcept(s.error(std::move(e))))
#define EXPECT_GE(val1, val2)
bool equals(const const_range_type &other, Comp &&eq) const
BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept< StringPiece >))
FOLLY_PUSH_WARNING RHS rhs
bool removeSuffix(const const_range_type &suffix)
::testing::Types< SseNeedleFinder, NoSseNeedleFinder, ByteSetNeedleFinder > NeedleFinders
constexpr auto size(C const &c) -> decltype(c.size())
constexpr bool empty() const
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
void * aligned_malloc(size_t size, size_t align)
size_t qfind_first_byte_of(const StringPiece haystack, const StringPiece needles)
size_t qfind_first_byte_of_nosse(const StringPieceLite haystack, const StringPieceLite needles)
static size_t find_first_byte_of(StringPiece haystack, StringPiece needles)
TYPED_TEST(SynchronizedTest, Basic)
constexpr Iter data() const
constexpr auto data(C &c) -> decltype(c.data())
size_t rfind(const Range< Iter > &haystack, const typename Range< Iter >::value_type &needle)
Range subpiece(size_type first, size_type length=npos) const
constexpr Range< Iter > range(Iter first, Iter last)
int compare(const const_range_type &o) const
AtomicCounter< T, DeterministicAtomic > Base
std::uniform_int_distribution< milliseconds::rep > dist
static const char *const value
size_t qfind(const Range< Iter > &haystack, const Range< Iter > &needle, Comp eq=Comp())
bool removePrefix(const const_range_type &prefix)
constexpr Iter end() const
constexpr Iter begin() const
#define EXPECT_TRUE(condition)
#define EXPECT_THAT(value, matcher)
bool startsWith(const const_range_type &other) const
bool operator==(const Unexpected< Error > &lhs, const Unexpected< Error > &rhs)
static const size_type npos
Range< const unsigned char * > ByteRange
void testRangeFunc(C &&x, size_t n)
#define EXPECT_NE(val1, val2)
Container::value_type * dataPtr(Container &cont)
friend bool operator==(char const *rhs, fake_string_view lhs)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
void split_step_with_process_noop(folly::StringPiece)
#define EXPECT_FALSE(condition)
bool endsWith(const const_range_type &other) const
Range< const char * > StringPiece
#define EXPECT_LT(val1, val2)
void expectEQ(const T &a, const T &b)
static size_t find_first_byte_of(StringPiece haystack, StringPiece needles)
constexpr auto crange(Collection const &v) -> Range< decltype(v.data())>
#define ASSERT_TRUE(condition)
void erase(Iter b, Iter e)
TEST(SequencedExecutor, CPUThreadPoolExecutor)
void reset(Iter start, size_type size)
std::vector< int > values(1'000)
Composed all(Predicate pred=Predicate())
static size_t find_first_byte_of(StringPiece haystack, StringPiece needles)
#define EXPECT_GT(val1, val2)