| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // stream_td.hpp
- // stream
- //
- // Created by Sam Jaffe on 1/28/17.
- //
- #include <gmock/gmock.h>
- #include <string>
- #include <vector>
- #include "stream/streams.hpp"
- using ::testing::Eq;
- // Workaround for OSX and pointer-to-member-functions
- template class std::basic_string<char>;
- using int_t = int const &;
- TEST(StreamTest, IteratorPreservesElements) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto s = stream::make_stream(v);
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(v, o);
- }
- TEST(StreamTest, CollectPreservesElements) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto s = stream::make_stream(v);
- std::vector<int> o{s.begin(), s.end()};
- std::vector<int> o2{s.collect()};
- EXPECT_THAT(v, o);
- EXPECT_THAT(v, o2);
- }
- TEST(StreamTest, CollectToObjectPreservesElements) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto s = stream::make_stream(v);
- std::set<int> o{};
- s.collect(o);
- for (int i : v) {
- EXPECT_THAT(o.count(i), Eq(1));
- }
- }
- TEST(StreamTest, MapToSelfIsSelfs) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto identity = [](int_t i) { return i; };
- auto s = stream::make_stream(v).map( identity );
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(v, o);
- }
- TEST(StreamTest, 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).map( fmap );
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(expected, o);
- }
- TEST(StreamTest, CanBuildFromSingleElement) {
- int_t value = 11;
- auto even = [](int_t i) { return i%2==0; };
- auto s = stream::make_stream(&value).filter( even );
- EXPECT_TRUE(s.empty());
- }
- TEST(StreamTest, CanBuildFromIterators) {
- std::vector<int> v{1, 2, 3, 4, 5};
- std::vector<int> expected{5, 7};
- auto fmap = [](int_t i) { return 2*i+1; };
- auto s = stream::make_stream(v.begin()+1, v.begin()+3).map(fmap);
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(o, Eq(expected));
- }
- TEST(StreamTest, NoOpFilterReturnOriginal) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto pass = [](int_t) { return true; };
- auto s = stream::make_stream(v).filter( pass );
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(o, Eq(v));
- }
- TEST(StreamTest, 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).filter(even);
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(o, Eq(expected));
- }
- TEST(StreamTest, AccumulateDefaultsToAdd) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto even = [](int_t i) { return i%2 == 0; };
- auto s = stream::make_stream(v).filter( even );
- EXPECT_THAT(s.accumulate(0), Eq(6));
- }
- TEST(StreamTest, AccumulateCanTakeCustomAccumulator) {
- std::vector<int> v{1, 2, 3, 4, 5};
- auto even = [](int_t i) { return i%2 == 0; };
- auto prod = [](int_t lhs, int_t rhs) { return lhs * rhs; };
- auto s = stream::make_stream(v).filter(even);
- EXPECT_THAT(s.accumulate(prod, 0), Eq(0));
- EXPECT_THAT(s.accumulate(prod, 1), Eq(8));
- }
- TEST(StreamTest, 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).flatmap(next3);
- std::vector<int> o{s.begin(), s.end()};
- EXPECT_THAT(o, Eq(expected));
- }
- TEST(StreamTest, CanDereferenceElements) {
- int val = 5;
- std::vector<int*> v{&val};
- auto data = stream::make_stream(v).deref().collect();
- EXPECT_THAT(data.front(), val);
- }
- TEST(StreamTest, CanForEachConsume) {
- int hits = 0;
- std::vector<int> v{1, 2, 3, 4, 5};
- stream::make_stream(v).each([&hits](int_t){ ++hits; });
- EXPECT_THAT(hits, 5);
- }
- TEST(StreamTest, CanFetchMemPtr) {
- struct test { int val; };
- std::vector<test> v{{1}, {3}, {2}};
- std::vector<int> expected{1, 3, 2};
- auto out = stream::make_stream(v).map(&test::val).collect();
- EXPECT_THAT(out, Eq(expected));
- }
- TEST(StreamTest, CanMapToMemFn) {
- std::vector<std::string> v{"hello", "goodbye"};
- std::vector<std::string::size_type> expected{5, 7};
- auto out = stream::make_stream(v).map(&std::string::size).collect();
- EXPECT_THAT(out, Eq(expected));
- }
- TEST(StreamTest, CastStreamToParentType) {
- struct base { char cat[4] = "cat"; };
- struct test : base { test(int v) : val(v) {} int val; };
- std::vector<test> v{{1}, {3}, {2}};
- auto strm = stream::make_stream(v).cast<base>();
- auto first = stream::make_stream(v).map([](test const & t) { return (void*)&t; }).collect();
- auto second = strm.map([](base const & t) { return (void*)&t; }).collect();
- EXPECT_THAT(first, second);
- }
|