// // stream_fluent.t.h // stream // // Created by Sam Jaffe on 1/28/17. // #include #include #include "stream/streams.hpp" #include "stream/streams/fluent.hpp" using ::testing::Eq; using int_t = int const &; TEST(FluentStreamTest, CollectToObjectPreservesElements) { std::vector v{1, 2, 3, 4, 5}; auto s = stream::make_stream(v); std::set o{}; s > o; for (int i : v ) { EXPECT_THAT(o.count(i), Eq(1)); } } TEST(FluentStreamTest, MapToSelfIsSelfs) { std::vector v{1, 2, 3, 4, 5}; auto identity = [](int_t i) { return i; }; auto s = stream::make_stream(v) | identity; std::vector o{s.begin(), s.end()}; EXPECT_THAT(o, Eq(v)); } TEST(FluentStreamTest, MapCanAlterValues) { std::vector v{1, 2, 3, 4, 5}; std::vector expected{3, 5, 7, 9, 11}; auto fmap = [](int_t i) { return 2*i+1; }; auto s = stream::make_stream(v) | fmap; std::vector o{s.begin(), s.end()}; EXPECT_THAT(o, Eq(expected)); } TEST(FluentStreamTest, NoOpFilterReturnOriginal) { std::vector v{1, 2, 3, 4, 5}; auto pass = [](int_t) { return true; }; auto s = stream::make_stream(v) | pass; std::vector o{s.begin(), s.end()}; EXPECT_THAT(o, Eq(v)); } TEST(FluentStreamTest, CanFilterOutElements) { std::vector v{1, 2, 3, 4, 5}; std::vector expected{2, 4}; auto even = [](int_t i) { return i%2 == 0; }; auto s = stream::make_stream(v) | even; std::vector o{s.begin(), s.end()}; EXPECT_THAT(o, Eq(expected)); } TEST(FluentStreamTest, AccumulateDefaultsToAdd) { std::vector v{1, 2, 3, 4, 5}; auto even = [](int_t i) { return i%2 == 0; }; auto sum = [](int_t lhs, int_t rhs) { return lhs + rhs; }; auto s = stream::make_stream(v) | even; EXPECT_THAT(s > sum, Eq(6)); } TEST(FluentStreamTest, FlatmapJoinsIterableOutputs) { std::vector vv{1, 2, 3, 4, 5}; auto next3 = [](int_t i) { return std::vector{i, i+1, i+2}; }; std::vector expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7}; auto s = stream::make_stream(vv) || next3; std::vector o{s.begin(), s.end()}; EXPECT_THAT(o, Eq(expected)); }