/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
TEST(Gen, MinBy) {
EXPECT_EQ(7, seq(1, 10)
- | minBy([](int i) {
- auto d = i - 6.8;
+ | minBy([](int i) -> double {
+ double d = i - 6.8;
return d * d;
}));
}
}
TEST(Gen, Yielders) {
- auto gen = GENERATOR(int, {
+ auto gen = GENERATOR(int) {
for (int i = 1; i <= 5; ++i) {
yield(i);
}
for (int i = 3; ; ++i) {
yield(i * i);
}
- });
+ };
vector<int> expected {
1, 2, 3, 4, 5, 7, 9, 16, 25
};
}
TEST(Gen, NestedYield) {
- auto nums = GENERATOR(int, {
+ auto nums = GENERATOR(int) {
for (int i = 1; ; ++i) {
yield(i);
}
- });
- auto gen = GENERATOR(int, {
+ };
+ auto gen = GENERATOR(int) {
nums | take(10) | yield;
seq(1, 5) | [&](int i) {
yield(i);
};
- });
+ };
EXPECT_EQ(70, gen | sum);
}
TEST(Gen, MapYielders) {
auto gen = seq(1, 5)
| map([](int n) {
- return GENERATOR(int, {
+ return GENERATOR(int) {
int i;
for (i = 1; i < n; ++i)
yield(i);
for (; i >= 1; --i)
yield(i);
- });
+ };
})
| concat;
vector<int> expected {
}
TEST(StringGen, EmptySplit) {
+ auto collect = eachTo<std::string>() | as<vector>();
+ {
+ auto pieces = split("", ',') | collect;
+ EXPECT_EQ(0, pieces.size());
+ }
+
+ // The last delimiter is eaten, just like std::getline
+ {
+ auto pieces = split(",", ',') | collect;
+ EXPECT_EQ(1, pieces.size());
+ EXPECT_EQ("", pieces[0]);
+ }
+
+ {
+ auto pieces = split(",,", ',') | collect;
+ EXPECT_EQ(2, pieces.size());
+ EXPECT_EQ("", pieces[0]);
+ EXPECT_EQ("", pieces[1]);
+ }
+
+ {
+ auto pieces = split(",,", ',') | take(1) | collect;
+ EXPECT_EQ(1, pieces.size());
+ EXPECT_EQ("", pieces[0]);
+ }
+}
+
+TEST(StringGen, Split) {
+ auto collect = eachTo<std::string>() | as<vector>();
+ {
+ auto pieces = split("hello,, world, goodbye, meow", ',') | collect;
+ EXPECT_EQ(5, pieces.size());
+ EXPECT_EQ("hello", pieces[0]);
+ EXPECT_EQ("", pieces[1]);
+ EXPECT_EQ(" world", pieces[2]);
+ EXPECT_EQ(" goodbye", pieces[3]);
+ EXPECT_EQ(" meow", pieces[4]);
+ }
+
+ {
+ auto pieces = split("hello,, world, goodbye, meow", ',')
+ | take(3) | collect;
+ EXPECT_EQ(3, pieces.size());
+ EXPECT_EQ("hello", pieces[0]);
+ EXPECT_EQ("", pieces[1]);
+ EXPECT_EQ(" world", pieces[2]);
+ }
+
+ {
+ auto pieces = split("hello,, world, goodbye, meow", ',')
+ | take(5) | collect;
+ EXPECT_EQ(5, pieces.size());
+ EXPECT_EQ("hello", pieces[0]);
+ EXPECT_EQ("", pieces[1]);
+ EXPECT_EQ(" world", pieces[2]);
+ }
+}
+
+TEST(StringGen, EmptyResplit) {
auto collect = eachTo<std::string>() | as<vector>();
{
auto pieces = from({""}) | resplit(',') | collect;
}
}
-TEST(StringGen, Split) {
+TEST(StringGen, Resplit) {
auto collect = eachTo<std::string>() | as<vector>();
{
auto pieces = from({"hello,, world, goodbye, meow"}) |