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