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