| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- //
- // stream_fluent.t.h
- // stream
- //
- // Created by Sam Jaffe on 1/28/17.
- //
- #include "xcode_gtest_helper.h"
- #include <vector>
- #include "stream/streams.hpp"
- #include "stream/streams/fluent.hpp"
- using ::testing::ElementsAreArray;
- using ::testing::Eq;
- TEST(FluentStreamTest, CollectToObjectPreservesElements) {
- std::vector<int> input{1, 2, 3, 4, 5};
- auto s = stream::of(input);
- std::set<int> out{};
- EXPECT_THAT(s > out, ElementsAreArray(input));
- }
- TEST(FluentStreamTest, MapToSelfIsSelfs) {
- std::vector<int> 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<int> input{1, 2, 3, 4, 5};
- std::vector<int> 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<int> input{1, 2, 3, 4, 5};
- std::vector<bool> 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<int> input{1, 2, 3, 4, 5};
- std::vector<int> 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<int> 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<int> 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<int> vv{1, 2, 3, 4, 5};
- auto next3 = [](int 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::of(vv) || next3;
- EXPECT_THAT(s.collect(), Eq(expected));
- }
|