| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // stream_fluent.t.h
- // stream
- //
- // Created by Sam Jaffe on 1/28/17.
- //
- #include "xcode_gtest_helper.h"
- #include <vector>
- #include "stream/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));
- }
|