stream_test.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // stream_td.hpp
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 1/28/17.
  6. //
  7. #include "xcode_gtest_helper.h"
  8. #include <map>
  9. #include <string>
  10. #include <vector>
  11. #include "stream/streams.hpp"
  12. using ::testing::ElementsAreArray;
  13. using ::testing::Eq;
  14. // Workaround for OSX and pointer-to-member-functions
  15. template class std::basic_string<char>;
  16. TEST(StreamTest, IteratorPreservesElements) {
  17. std::vector<int> input{1, 2, 3, 4, 5};
  18. auto s = stream::of(input);
  19. std::vector<int> out{s.begin(), s.end()};
  20. EXPECT_THAT(out, Eq(input));
  21. }
  22. TEST(MapStreamTest, IteratorPreservesElements) {
  23. std::map<int, int> input{{1, 1}, {2, 2}};
  24. auto s = stream::of(input);
  25. std::map<int, int> out{s.begin(), s.end()};
  26. EXPECT_THAT(out, Eq(input));
  27. }
  28. TEST(StreamTest, CollectPreservesElements) {
  29. std::vector<int> input{1, 2, 3, 4, 5};
  30. auto s = stream::of(input);
  31. std::vector<int> out{s.collect()};
  32. EXPECT_THAT(out, Eq(input));
  33. }
  34. TEST(StreamTest, CollectToObjectPreservesElements) {
  35. std::vector<int> input{1, 2, 3, 4, 5};
  36. auto s = stream::of(input);
  37. std::set<int> out{};
  38. s.collect(out);
  39. EXPECT_THAT(out, ElementsAreArray(input));
  40. }
  41. TEST(StreamTest, MapToSelfIsSelfs) {
  42. std::vector<int> input{1, 2, 3, 4, 5};
  43. auto identity = [](int i) { return i; };
  44. auto s = stream::of(input).map(identity);
  45. EXPECT_THAT(s.collect(), Eq(input));
  46. }
  47. TEST(StreamTest, MapCanAlterValues) {
  48. std::vector<int> input{1, 2, 3, 4, 5};
  49. std::vector<int> expected{3, 5, 7, 9, 11};
  50. auto fmap = [](int i) { return 2 * i + 1; };
  51. auto s = stream::of(input).map(fmap);
  52. EXPECT_THAT(s.collect(), Eq(expected));
  53. }
  54. template <typename T> struct nocopy {
  55. T value;
  56. nocopy(T const & val) : value(val) {}
  57. nocopy(nocopy const &) = delete;
  58. nocopy & operator=(nocopy const &) = delete;
  59. nocopy(nocopy &&) = default;
  60. nocopy & operator=(nocopy &&) = default;
  61. operator T() const { return value; }
  62. };
  63. TEST(MapStreamTest, MapToValue) {
  64. auto const input = []() {
  65. std::map<int, nocopy<int>> tmp;
  66. tmp.emplace(0, 1);
  67. tmp.emplace(2, 2);
  68. return tmp;
  69. }();
  70. auto fmap = [](auto & pair) -> auto & { return pair.second; };
  71. auto s = stream::of(input).map(fmap);
  72. std::vector<int> out(s.begin(), s.end());
  73. std::vector<int> const expected{1, 2};
  74. EXPECT_THAT(out, Eq(expected));
  75. }
  76. TEST(StreamTest, CanBuildFromSingleElement) {
  77. int value = 11;
  78. auto even = [](int i) { return i % 2 == 0; };
  79. auto s = stream::of(&value).filter(even);
  80. EXPECT_TRUE(s.empty());
  81. }
  82. TEST(StreamTest, CanBuildFromIterators) {
  83. std::vector<int> input{1, 2, 3, 4, 5};
  84. std::vector<int> expected{5, 7};
  85. auto fmap = [](int i) { return 2 * i + 1; };
  86. auto s = stream::of(input.begin() + 1, input.begin() + 3).map(fmap);
  87. EXPECT_THAT(s.collect(), Eq(expected));
  88. }
  89. TEST(StreamTest, NoOpFilterReturnOriginal) {
  90. std::vector<int> input{1, 2, 3, 4, 5};
  91. auto pass = [](int) { return true; };
  92. auto s = stream::of(input).filter(pass);
  93. EXPECT_THAT(s.collect(), Eq(input));
  94. }
  95. TEST(StreamTest, CanFilterOutElements) {
  96. std::vector<int> input{1, 2, 3, 4, 5};
  97. std::vector<int> expected{2, 4};
  98. auto even = [](int i) { return i % 2 == 0; };
  99. auto s = stream::of(input).filter(even);
  100. EXPECT_THAT(s.collect(), Eq(expected));
  101. }
  102. TEST(StreamTest, AccumulateDefaultsToAdd) {
  103. std::vector<int> input{1, 2, 3, 4, 5};
  104. auto even = [](int i) { return i % 2 == 0; };
  105. auto s = stream::of(input).filter(even);
  106. EXPECT_THAT(s.accumulate(0), Eq(6));
  107. }
  108. TEST(StreamTest, AccumulateCanTakeCustomAccumulator) {
  109. std::vector<int> input{1, 2, 3, 4, 5};
  110. auto even = [](int i) { return i % 2 == 0; };
  111. auto prod = [](int lhs, int rhs) { return lhs * rhs; };
  112. auto s = stream::of(input).filter(even);
  113. EXPECT_THAT(s.accumulate(prod, 0), Eq(0));
  114. EXPECT_THAT(s.accumulate(prod, 1), Eq(8));
  115. }
  116. TEST(StreamTest, FlatmapJoinsIterableOutputs) {
  117. std::vector<int> vv{1, 2, 3, 4, 5};
  118. auto next3 = [](int i) { return std::vector<int>{i, i + 1, i + 2}; };
  119. std::vector<int> expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7};
  120. auto s = stream::of(vv).flatmap(next3);
  121. EXPECT_THAT(s.collect(), Eq(expected));
  122. }
  123. TEST(StreamTest, CanDereferenceElements) {
  124. int val = 5;
  125. std::vector<int *> input{&val};
  126. auto data = stream::of(input).deref().collect();
  127. EXPECT_THAT(data.front(), Eq(val));
  128. }
  129. TEST(StreamTest, CanForEachConsume) {
  130. int hits = 0;
  131. std::vector<int> input{1, 2, 3, 4, 5};
  132. stream::of(input).each([&hits](int) { ++hits; });
  133. EXPECT_THAT(hits, Eq(5));
  134. }
  135. TEST(StreamTest, CanFetchMemPtr) {
  136. struct test {
  137. int val;
  138. };
  139. std::vector<test> input{{1}, {3}, {2}};
  140. std::vector<int> expected{1, 3, 2};
  141. auto out = stream::of(input).map(&test::val).collect();
  142. EXPECT_THAT(out, Eq(expected));
  143. }
  144. TEST(StreamTest, CanMapToMemFn) {
  145. std::vector<std::string> input{"hello", "goodbye"};
  146. std::vector<std::string::size_type> expected{5, 7};
  147. auto out = stream::of(input).map(&std::string::size).collect();
  148. EXPECT_THAT(out, Eq(expected));
  149. }
  150. TEST(StreamTest, CastStreamToParentType) {
  151. struct base {
  152. char cat[4] = "cat";
  153. };
  154. struct test : base {
  155. test(int v) : val(v) {}
  156. int val;
  157. };
  158. std::vector<test> input{{1}, {3}, {2}};
  159. auto addressof_void = [](auto const & p) { return (void *)&p; };
  160. auto strm = stream::of(input).cast<base>();
  161. auto first = stream::of(input).map(addressof_void).collect();
  162. auto second = strm.map(addressof_void).collect();
  163. EXPECT_THAT(first, second);
  164. }