17 #include <glog/logging.h> 35 using namespace folly;
42 using std::unique_ptr;
45 #define EXPECT_SAME(A, B) \ 46 static_assert(std::is_same<A, B>::value, "Mismatched: " #A ", " #B) 53 ostream& operator<<(ostream& os, const set<T>&
values) {
58 ostream& operator<<(ostream& os, const vector<T>&
values) {
61 if (&
value != &values.front()) {
70 auto add = [](
int a,
int b) {
return a +
b; };
75 template <
typename A,
typename B>
76 ostream& operator<<(ostream& os, const pair<A, B>& pair) {
77 return os <<
"(" << pair.first <<
", " << pair.second <<
")";
81 auto gen =
seq(1, 10);
87 auto gen =
seq(1, 10);
95 gen | [&](
int x) { accum +=
x; };
98 gen |
take(3) | [&](
int x) { accum2 +=
x; };
103 auto expected = vector<int>{4, 9, 16};
105 EXPECT_EQ((vector<int>{4, 9, 16}), gen | as<vector>());
106 EXPECT_EQ((vector<int>{4, 9}), gen |
take(2) | as<vector>());
123 const int& ref()
const {
130 auto counters =
seq(1, 10) | eachAs<Counter>() | as<vector>();
142 auto m = empty<Counter&>();
143 auto c = empty<const Counter&>();
147 m | member<Const>(&Counter::ref) | assert_type<const int&>();
148 m | member<Mutable>(&Counter::ref) | assert_type<int&>();
149 c | member<Const>(&Counter::ref) | assert_type<const int&>();
154 X() :
a(2),
b(3),
c(4), d(
b) {}
162 std::vector<X> xs(1);
168 empty<X&>() |
field(&
X::a) | assert_type<const int&>();
169 empty<X*>() |
field(&
X::a) | assert_type<const int&>();
170 empty<X&>() |
field(&
X::b) | assert_type<int&>();
171 empty<X*>() |
field(&
X::b) | assert_type<int&>();
172 empty<X&>() |
field(&
X::c) | assert_type<int&>();
173 empty<X*>() |
field(&
X::c) | assert_type<int&>();
175 empty<X&&>() |
field(&
X::a) | assert_type<const int&&>();
176 empty<X&&>() |
field(&
X::b) | assert_type<int&&>();
177 empty<X&&>() |
field(&
X::c) | assert_type<int&&>();
180 empty<const X&>() |
field(&
X::a) | assert_type<const int&>();
181 empty<const X*>() |
field(&
X::a) | assert_type<const int&>();
182 empty<const X&>() |
field(&
X::b) | assert_type<const int&>();
183 empty<const X*>() |
field(&
X::b) | assert_type<const int&>();
185 empty<const X&>() |
field(&
X::c) | assert_type<const int&>();
186 empty<const X*>() |
field(&
X::c) | assert_type<const int&>();
193 for (
int n = 1; n < 100; ++n) {
204 const std::array<int, 6> arr{{1, 2, 3, 4, 5, 6}};
206 9,
seq(&arr[0], &arr[5], 2) |
map([](
const int*
i) {
return *
i; }) |
sum);
211 for (
int n = 1; n < 100; ++n) {
221 vector<int> source{2, 3, 5, 7, 11};
230 |
map([](
int i) {
return std::make_pair(i, i * i); })
231 |
as<std::map<int, int>>();
234 |
map([&](
const std::pair<const int, int>& p) {
235 return p.second - p.first;
242 const auto expected = vector<int>{1, 2, 4, 5, 7, 8};
244 seq(1, 9) |
filter([](
int x) {
return x % 3; }) |
as<vector<int>>();
251 const auto expected = vector<int>{1, 1, 2, 3};
252 auto actual =
from({0, 1, 1, 0, 2, 3, 0}) |
filter() | as<vector>();
260 const auto expected = vector<int*>{&
a, &
b, &c};
262 auto actual =
from({(
int*)
nullptr, &a, &b, &c, (
int*)
nullptr})
270 const auto expected =
273 const auto actual =
from(
284 auto actual =
seq(1, 2)
285 |
map([](
int x) {
return vector<int>{x}; })
286 |
filter([](vector<int>
v) {
return !v.empty(); })
300 auto gen =
seq(1) |
map(
square) | eachTo<std::string>();
309 auto expected = vector<int>{1, 4, 9, 16};
313 |
mapped([](
int x) {
return x *
x; })
320 auto expected = vector<int>{0, 1, 4, 5, 8};
332 auto expected = vector<int>{0, 1, 4, 5, 8};
355 auto expected = vector<int>{1, 2, 3, 4};
356 auto actual =
seq(1, 4) |
stride(1) | as<vector<int>>();
360 auto expected = vector<int>{1, 3, 5, 7};
361 auto actual =
seq(1, 8) |
stride(2) | as<vector<int>>();
365 auto expected = vector<int>{1, 4, 7, 10};
366 auto actual =
seq(1, 12) |
stride(3) | as<vector<int>>();
370 auto expected = vector<int>{1, 3, 5, 7, 9, 1, 4, 7, 10};
384 std::mt19937 rnd(42);
386 auto sampler =
seq(1, 100) |
sample(50, rnd);
387 std::unordered_map<int, int> hits;
388 const int kNumIters = 80;
389 for (
int i = 0;
i < kNumIters;
i++) {
390 auto vec = sampler | as<vector<int>>();
403 for (
auto hit : hits) {
416 EXPECT_EQ((vector<int>{25, 36, 49, 64}), gen | as<vector>());
421 auto expected = vector<int>{1, 4, 9, 16};
425 |
mapped([](
int x) {
return x *
x; })
426 |
until([](
int x) {
return x > 20; })
432 auto expected = vector<int>{0, 1, 4, 5, 8};
435 = ((
seq(0) |
until([](
int i) {
return i > 1; })) +
436 (
seq(4) |
until([](
int i) {
return i > 5; })) +
437 (
seq(8) |
until([](
int i) {
return i > 9; })))
438 |
until([](
int i) {
return i > 8; })
462 auto increment = [](
int&
i) { ++
i; };
463 auto clone =
map([](
int i) {
return i; });
471 auto actual =
seq(0, 3) | clone |
visit(increment) |
sum;
475 std::vector<int> x2, x4;
476 std::vector<int> expected2{0, 1, 4, 9};
477 std::vector<int> expected4{0, 1, 16, 81};
479 auto tee = [](std::vector<int>& container) {
480 return visit([&](
int value) { container.push_back(value); });
496 std::vector<Optional<int>> opts{
none, 4,
none, 6, none};
499 auto sumOpt = valuesOf |
sum;
504 std::vector<int> nums{2, 3, 5, 7};
505 std::map<int, int> mappings{{3, 9}, {5, 25}};
506 auto gen =
from(nums) + (
from(mappings) | get<1>());
513 std::vector<std::vector<int>> nums{{2, 3}, {5, 7}};
526 std::vector<std::vector<int>> nums{{2, 3}, {5, 7}};
530 |
map([](std::vector<int>&
v) {
return from(v); })
539 auto expected = vector<int>{0, 3, 5, 6, 7, 8, 9};
540 auto actual =
from({8, 6, 7, 5, 3, 0, 9}) |
order | as<vector>();
545 auto expected = vector<int>{0, 9, 25, 36, 49, 64, 81};
548 =
from({8, 6, 7, 5, 3, 0, 9})
558 auto expected = vector<int>{9, 8, 7};
561 =
from({8, 6, 7, 5, 3, 0, 9})
570 auto expected = vector<int>{3, 1, 2};
571 auto actual =
from({3, 1, 3, 2, 1, 2, 3}) |
distinct | as<vector>();
576 auto expected = vector<int>{0, 1, 2, 3, 4, 5};
578 seq(0, 100) |
distinctBy([](
int i) {
return i * i % 10; }) | as<vector>();
583 auto expected = vector<int>{0, 1, 2, 3, 4, 5};
584 auto actual =
seq(0, 100) |
585 mapped([](
int i) {
return std::make_unique<int>(
i); })
587 |
distinctBy([](
const std::unique_ptr<int>& pi) {
588 return *pi * *pi % 10;
590 mapped([](std::unique_ptr<int> pi) {
return *pi; }) | as<vector>();
604 auto expected =
seq(0) |
take(5) | as<vector>();
607 |
mapped([](
int i) {
return i / 2; })
615 TEST(Gen, DistinctByInfinite) {
620 auto expected = vector<int>{1, 2};
635 |
minBy([](
int i) ->
double {
644 auto gen =
from({
"three",
"eleven",
"four"});
650 auto odds =
seq(2, 10) |
filter([](
int i) {
return i % 2; });
656 auto odds =
seq(2, 10) |
filter([](
int i) {
return i % 2; });
662 string expected =
"facebook";
663 string actual =
"face";
677 auto expected = 1 * 2 * 3 * 4;
686 auto q =
from([] {
return vector<int>({3, 7, 5}); }());
690 for (
auto size : {5, 1024, 16384, 1 << 20}) {
691 auto q1 =
from(vector<int>(
size, 2));
692 auto q2 =
from(vector<int>(
size, 3));
702 auto q =
from(set<int>{1, 2, 3, 2, 1});
708 auto expected = vector<int>{5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
712 |
orderBy([](
int x) {
return (5.1 - x) * (5.1 - x); })
717 expected =
seq(1, 10) | as<vector>();
721 |
map([] (
int x) {
return 11 -
x; })
729 int expected = 2 * 3 * 4 * 5;
735 int expected = 2 + 3 + 4 + 5;
747 std::vector<unique_ptr<int>> ptrs;
748 ptrs.emplace_back(
new int(1));
750 auto ptrs2 =
from(ptrs) |
move | as<vector>();
756 auto gen =
seq(0) |
filter([](
int x) {
return x > 3; });
772 std::map<int, int> pairs{
778 auto pairSrc =
from(pairs);
779 auto keys = pairSrc | get<0>();
780 auto values = pairSrc | get<1>();
788 vector<tuple<int, int, int>> tuples{
827 for (
int i = 1;
i <= 5; ++
i) {
831 for (
int i = 3;; ++
i) {
835 vector<int> expected{1, 2, 3, 4, 5, 7, 9, 16, 25};
841 for (
int i = 1;; ++
i) {
859 for (i = 1; i < n; ++
i) {
862 for (; i >= 1; --
i) {
868 vector<int> expected {
873 1, 2, 3, 4, 5, 4, 3, 2, 1,
893 auto gen =
from({Foo{2}, Foo{3}}) |
map([](
const Foo&
f) {
return f.y; });
898 auto gen =
seq(1, 5) |
map([](
int x) {
return std::make_unique<int>(
x); }) |
899 map([](unique_ptr<int> p) {
return p; }) |
901 map([](
const unique_ptr<int>& p) {
return *p; });
908 class TestIntSeq :
public GenImpl<int, TestIntSeq> {
912 template <
class Body>
913 bool apply(Body&& body)
const {
914 for (
int i = 1;
i < 6; ++
i) {
922 TestIntSeq(TestIntSeq&&)
noexcept =
default;
923 TestIntSeq& operator=(TestIntSeq&&)
noexcept =
default;
924 TestIntSeq(
const TestIntSeq&) =
delete;
925 TestIntSeq& operator=(
const TestIntSeq&) =
delete;
932 auto x = TestIntSeq() |
take(3);
937 int source[] = {2, 3, 5, 7};
938 auto gen =
from(source);
943 std::array<int, 4> source{{2, 3, 5, 7}};
944 auto gen =
from(source);
949 auto gen =
seq(1, 10) | eachTo<string>() |
rconcat;
950 EXPECT_EQ(
"12345678910", gen | as<string>());
977 this->copies = source.
copies + 1;
978 this->moves = source.
moves;
983 this->copies = source.
copies;
984 this->moves = source.moves + 1;
992 vector<CopyCounter> originals;
993 originals.emplace_back();
997 vector<CopyCounter> copies =
from(originals) | as<vector>();
1001 vector<CopyCounter> moves =
from(originals) |
move | as<vector>();
1029 auto s =
from({7, 6, 5, 4, 3}) |
as<set<int>>();
1035 auto s =
from({1, 2});
1039 auto s =
from({1, 2});
1043 auto s =
from({1, 2, 3});
1045 (vector<int>{1, 2, 1, 2, 1}),
1049 auto s = empty<int>();
1057 <<
"Cycle should have stopped when it didnt' get values!";
1058 for (
int i = 1;
i <= *pcount; ++
i) {
1065 (vector<int>{1, 2, 3, 1, 2, 1}),
s |
cycle |
take(7) | as<vector>());
1072 const int x = 4,
y = 2;
1073 auto s =
from(std::initializer_list<const int*>({&
x,
nullptr, &
y}));
1077 vector<int>
a{1, 2};
1078 vector<int>
b{3, 4};
1079 vector<vector<int>*> pv{&
a,
nullptr, &
b};
1087 vector<std::map<int, int>> maps{
1105 |
map([](std::map<int, int>&
m) {
1113 vector<unique_ptr<int>> ups;
1114 ups.emplace_back(
new int(3));
1116 ups.emplace_back(
new int(7));
1123 struct DereferenceWrapper {
1125 string& operator*() & {
1128 string&& operator*() && {
1131 explicit operator bool() {
1135 bool operator==(
const DereferenceWrapper&
a,
const DereferenceWrapper&
b) {
1136 return a.data == b.data;
1138 void PrintTo(
const DereferenceWrapper& a, std::ostream* o) {
1139 *o <<
"Wrapper{\"" << cEscape<string>(a.data) <<
"\"}";
1143 TEST(Gen, DereferenceWithLValueRef) {
1144 auto original = vector<DereferenceWrapper>{{
"foo"}, {
"bar"}};
1145 auto copy = original;
1146 auto expected = vector<string>{
"foo",
"bar"};
1152 TEST(Gen, DereferenceWithRValueRef) {
1153 auto original = vector<DereferenceWrapper>{{
"foo"}, {
"bar"}};
1154 auto empty = vector<DereferenceWrapper>{{}, {}};
1155 auto expected = vector<string>{
"foo",
"bar"};
1167 using std::runtime_error;
1170 from({
"1",
"a",
"3"})
1176 from({
"1",
"a",
"3"})
1177 | guard<runtime_error>([](runtime_error&,
const char*) {
1184 from({
"1",
"a",
"3"})
1185 | guard<runtime_error>([](runtime_error&,
const char*) {
1191 from({
"1",
"a",
"3"})
1192 | guard<runtime_error>([](runtime_error&,
const char*
v) {
1205 using std::runtime_error;
1209 from({
"1",
"a",
"3"})
1215 from({
"1",
"a",
"3"})
1226 (vector<vector<int>>{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11}}),
1227 seq(1, 11) |
batch(3) | as<vector>());
1232 auto expected = vector<vector<int>>{{0, 1}, {2, 3}, {4}};
1233 auto actual =
seq(0, 4) |
1234 mapped([](
int i) {
return std::make_unique<int>(
i); }) |
batch(2) |
1235 mapped([](std::vector<std::unique_ptr<int>>& pVector) {
1236 std::vector<int> iVector;
1237 for (
const auto& p : pVector) {
1238 iVector.push_back(*p);
1247 auto expected =
seq(0, 10) | as<std::vector>();
1248 for (
size_t windowSize = 1; windowSize <= 20; ++windowSize) {
1250 auto actual =
seq(0, 10) |
1251 mapped([](
int i) {
return std::make_unique<int>(
i); }) |
window(4) |
1253 EXPECT_EQ(expected, actual) << windowSize;
1255 for (
size_t windowSize = 1; windowSize <= 20; ++windowSize) {
1257 auto actual =
seq(0) |
1258 mapped([](
int i) {
return std::make_unique<int>(
i); }) |
take(11) |
1260 EXPECT_EQ(expected, actual) << windowSize;
1262 for (
size_t windowSize = 1; windowSize <= 20; ++windowSize) {
1264 auto actual =
seq(0) |
1265 mapped([](
int i) {
return std::make_unique<int>(
i); }) |
window(4) |
1267 EXPECT_EQ(expected, actual) << windowSize;
1297 vector<string> strs{
1310 auto gb =
from(strs) |
groupBy([](
const string& str) {
return str.size(); });
1315 vector<string>
mode{
"zero",
"four",
"five",
"nine"};
1325 vector<string> largest{
"three",
"seven",
"eight"};
1337 vector<string> finite{
"a",
"b",
"cc",
"dd",
"ee",
"fff",
"g",
"hhh"};
1338 vector<vector<string>> finiteGroups{
1339 {
"a",
"b"}, {
"cc",
"dd",
"ee"}, {
"fff"}, {
"g"}, {
"hhh"}};
1344 mapOp(as<vector>()) | as<vector>());
1346 auto infinite =
seq(0);
1347 vector<vector<int>> infiniteGroups{
1348 {0, 1, 2, 3, 4}, {5, 6, 7, 8, 9}, {10, 11, 12, 13, 14}};
1352 take(3) |
mapOp(as<vector>()) | as<vector>());
1389 decltype(full)
empty;
1404 decltype(full)
empty;
1419 auto fallback =
unwrapOr(std::make_unique<int>(9));
1440 vector<int>
v{1, 2};
1456 gflags::ParseCommandLineFlags(&argc, &argv,
true);
const Map::mapped_type * get_ptr(const Map &map, const Key &key)
IterableProxy< const_value_iterator > values() const
#define ASSERT_GE(val1, val2)
static ObjectMaker object()
CopyOf fromCopy(Container &&source)
CopyCounter(const CopyCounter &source)
CopyCounter(CopyCounter &&source) noexcept
#define EXPECT_THROW(statement, expected_exception)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
constexpr detail::Count count
Map field(FieldType Class::*field)
constexpr detail::IsEmpty< false > notEmpty
From from(Container &source)
#define EXPECT_EQ(val1, val2)
constexpr detail::Order< Identity > order
constexpr detail::Map< Move > move
constexpr detail::Distinct< Identity > distinct
Reduce reduce(Reducer reducer=Reducer())
Gen seq(Value first, Value last)
detail::Batch batch(size_t batchSize)
Visit visit(Visitor visitor=Visitor())
void PrintTo(const dynamic &dyn, std::ostream *os)
—— Concurrent Priority Queue Implementation ——
detail::Skip skip(size_t count)
requires E e noexcept(noexcept(s.error(std::move(e))))
detail::Sample< Random > sample(size_t count, Random rng=Random())
Min minBy(Selector selector=Selector())
constexpr detail::Min< Identity, Less > min
std::unordered_map< int64_t, VecT > Map
constexpr Optional< _t< std::decay< T > > > make_optional(T &&v)
ThreadCachedInt< int64_t > Counter
folly::Optional< PskKeyExchangeMode > mode
constexpr detail::Sum sum
Composed any(Predicate pred=Predicate())
Gen range(Value begin, Value end)
CopyCounter & operator=(CopyCounter &&source)
FoldLeft foldl(Seed seed=Seed(), Fold fold=Fold())
GroupByAdjacent groupByAdjacent(Selector selector=Selector())
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
ConcatImplActionP3< std::string, T1, T2 > Concat(const std::string &a, T1 b, T2 c)
constexpr auto size(C const &c) -> decltype(c.size())
detail::Window window(size_t windowSize)
constexpr detail::IsEmpty< true > isEmpty
Order orderByDescending(Selector selector=Selector())
UnwrapOr unwrapOr(Fallback &&fallback)
Order orderBy(Selector selector=Selector(), Comparer comparer=Comparer())
Contains contains(Needle &&needle)
From fromConst(const Container &source)
constexpr detail::RangeConcat rconcat
constexpr auto data(C &c) -> decltype(c.data())
constexpr detail::Indirect indirect
Map map(Predicate pred=Predicate())
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
constexpr Range< Iter > range(Iter first, Iter last)
static map< string, int > m
TakeWhile takeWhile(Predicate pred=Predicate())
constexpr detail::Cycle< true > cycle
Append appendTo(Collection &collection)
CopyCounter & operator=(const CopyCounter &source)
Filter filter(Predicate pred=Predicate())
std::enable_if< ExprIsConst< Constness >::value, Map >::type member(Return(Class::*member)() const)
Distinct distinctBy(Selector selector=Selector())
#define EXPECT_SAME(A, B)
#define EXPECT_TRUE(condition)
constexpr detail::Concat concat
IterableProxy< const_key_iterator > keys() const
MaxBy maxBy(Selector selector=Selector())
Until until(Predicate pred=Predicate())
bool operator==(const Unexpected< Error > &lhs, const Unexpected< Error > &rhs)
IterableProxy< const_item_iterator > items() const
constexpr detail::Min< Identity, Greater > max
detail::Empty< Value > empty()
static void array(EmptyArrayTag)
#define EXPECT_NE(val1, val2)
constexpr detail::Unwrap unwrap
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
detail::Take take(Number count)
Map mapped(Predicate pred=Predicate())
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
FOLLY_CPP14_CONSTEXPR const Value & value() const &
#define EXPECT_FALSE(condition)
Range< const char * > StringPiece
#define EXPECT_LT(val1, val2)
int main(int argc, char *argv[])
internal::ContainsMatcher< M > Contains(M matcher)
decltype(auto) constexpr apply(F &&func, Tuple &&tuple)
detail::Stride stride(size_t s)
GroupBy groupBy(Selector selector=Selector())
std::vector< int > values(1'000)
constexpr detail::First first
Composed all(Predicate pred=Predicate())
#define EXPECT_GT(val1, val2)
constexpr detail::Dereference dereference