// // stream_fluent.t.h // stream // // Created by Sam Jaffe on 1/28/17. // #include "xcode_gtest_helper.h" #include #include "stream/streams.hpp" #include "stream/streams/fluent.hpp" using ::testing::ElementsAreArray; using ::testing::Eq; TEST(FluentStreamTest, CollectToObjectPreservesElements) { std::vector input{1, 2, 3, 4, 5}; auto s = stream::of(input); std::set out{}; EXPECT_THAT(s > out, ElementsAreArray(input)); } TEST(FluentStreamTest, MapToSelfIsSelfs) { std::vector input{1, 2, 3, 4, 5}; auto identity = [](int i) { return i; }; auto s = stream::of(input) | identity; EXPECT_THAT(s.collect(), Eq(input)); } TEST(FluentStreamTest, MapCanAlterValues) { std::vector input{1, 2, 3, 4, 5}; std::vector expected{3, 5, 7, 9, 11}; auto fmap = [](int i) { return 2 * i + 1; }; auto s = stream::of(input) | fmap; EXPECT_THAT(s.collect(), Eq(expected)); } TEST(FluentStreamTest, CanMapElementToBool) { std::vector input{1, 2, 3, 4, 5}; std::vector expected{false, true, false, true, false}; auto even = [](int i) { return i % 2 == 0; }; auto s = stream::of(input) | even; EXPECT_THAT(s.collect(), Eq(expected)); } TEST(FluentStreamTest, CanFilterOutElements) { std::vector input{1, 2, 3, 4, 5}; std::vector expected{2, 4}; auto even = [](int i) { return i % 2 == 0; }; auto s = stream::of(input) % even; EXPECT_THAT(s.collect(), Eq(expected)); } TEST(FluentStreamTest, NoOpFilterReturnOriginal) { std::vector input{1, 2, 3, 4, 5}; auto pass = [](int) { return true; }; auto s = stream::of(input) % pass; EXPECT_THAT(s.collect(), Eq(input)); } TEST(FluentStreamTest, AccumulateDefaultsToAdd) { std::vector input{1, 2, 3, 4, 5}; auto even = [](int i) { return i % 2 == 0; }; auto sum = [](int lhs, int rhs) { return lhs + rhs; }; auto s = stream::of(input) % even; EXPECT_THAT(s > sum, Eq(6)); } TEST(FluentStreamTest, FlatmapJoinsIterableOutputs) { std::vector vv{1, 2, 3, 4, 5}; auto next3 = [](int 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::of(vv) || next3; EXPECT_THAT(s.collect(), Eq(expected)); }