// // stream_fluent.t.h // stream // // Created by Sam Jaffe on 1/28/17. // #pragma once #include #include #include "streams.hpp" #include "streams/fluent.hpp" class stream_fluent_TestSuite : public CxxTest::TestSuite { public: using int_t = int const &; using vec_t = std::vector; public: void test_collect_arg_preserves() { vec_t v{1, 2, 3, 4, 5}; auto s = stream::make_stream(v); std::set o{}; s > o; for ( int i : v ) { TS_ASSERT_EQUALS(o.count( i ), 1); } } void test_map_identity() { vec_t v{1, 2, 3, 4, 5}; auto identity = [](int_t i) { return i; }; auto s = stream::make_stream(v) | identity; vec_t o{s.begin(), s.end()}; TS_ASSERT_EQUALS(v, o); } void test_map_change() { vec_t v{1, 2, 3, 4, 5}; vec_t expected{3, 5, 7, 9, 11}; auto fmap = [](int_t i) { return 2*i+1; }; auto s = stream::make_stream(v) | fmap; vec_t o{s.begin(), s.end()}; TS_ASSERT_EQUALS(expected, o); } void test_filter_noop() { vec_t v{1, 2, 3, 4, 5}; auto pass = [](int_t) { return true; }; auto s = stream::make_stream(v) | pass; vec_t o{s.begin(), s.end()}; TS_ASSERT_EQUALS(v, o); } void test_filter_value() { vec_t v{1, 2, 3, 4, 5}; vec_t expected{2, 4}; auto even = [](int_t i) { return i%2 == 0; }; auto s = stream::make_stream(v) | even; vec_t o{s.begin(), s.end()}; TS_ASSERT_EQUALS(expected, o); } void test_accumulate() { vec_t 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; TS_ASSERT_EQUALS( 6 , s > sum ); } void test_flatmap_joins_lists() { vec_t vv{1, 2, 3, 4, 5}; auto next3 = [](int_t i) { return vec_t{i, i+1, i+2}; }; vec_t expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7}; auto s = stream::make_stream(vv) || next3; vec_t o{s.begin(), s.end()}; TS_ASSERT_EQUALS(expected, o); } };