stream.t.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // stream_td.hpp
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 1/28/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include <string>
  10. #include <vector>
  11. #include "streams.hpp"
  12. // Workaround for OSX and pointer-to-member-functions
  13. template class std::basic_string<char>;
  14. class stream_TestSuite : public CxxTest::TestSuite {
  15. public:
  16. using int_t = int const &;
  17. using vec_t = std::vector<int>;
  18. public:
  19. void test_preserved() {
  20. vec_t v{1, 2, 3, 4, 5};
  21. auto s = stream::make_stream(v);
  22. vec_t o{s.begin(), s.end()};
  23. TS_ASSERT_EQUALS(v, o);
  24. }
  25. void test_collect_preserves() {
  26. vec_t v{1, 2, 3, 4, 5};
  27. auto s = stream::make_stream(v);
  28. vec_t o{s.begin(), s.end()};
  29. vec_t o2{s.collect()};
  30. TS_ASSERT_EQUALS(v, o);
  31. TS_ASSERT_EQUALS(v, o2);
  32. }
  33. void test_collect_arg_preserves() {
  34. vec_t v{1, 2, 3, 4, 5};
  35. auto s = stream::make_stream(v);
  36. std::set<int> o{};
  37. s.collect(o);
  38. for ( int i : v ) {
  39. TS_ASSERT_EQUALS(o.count( i ), 1);
  40. }
  41. }
  42. void test_map_identity() {
  43. vec_t v{1, 2, 3, 4, 5};
  44. auto identity = [](int_t i) { return i; };
  45. auto s = stream::make_stream(v).map( identity );
  46. vec_t o{s.begin(), s.end()};
  47. TS_ASSERT_EQUALS(v, o);
  48. }
  49. void test_map_change() {
  50. vec_t v{1, 2, 3, 4, 5};
  51. vec_t expected{3, 5, 7, 9, 11};
  52. auto fmap = [](int_t i) { return 2*i+1; };
  53. auto s = stream::make_stream(v).map( fmap );
  54. vec_t o{s.begin(), s.end()};
  55. TS_ASSERT_EQUALS(expected, o);
  56. }
  57. void test_filter_noop() {
  58. vec_t v{1, 2, 3, 4, 5};
  59. auto pass = [](int_t i) { return true; };
  60. auto s = stream::make_stream(v).filter( pass );
  61. vec_t o{s.begin(), s.end()};
  62. TS_ASSERT_EQUALS(v, o);
  63. }
  64. void test_filter_value() {
  65. vec_t v{1, 2, 3, 4, 5};
  66. vec_t expected{2, 4};
  67. auto even = [](int_t i) { return i%2 == 0; };
  68. auto s = stream::make_stream(v).filter( even );
  69. vec_t o{s.begin(), s.end()};
  70. TS_ASSERT_EQUALS(expected, o);
  71. }
  72. void test_accumulate() {
  73. vec_t v{1, 2, 3, 4, 5};
  74. auto even = [](int_t i) { return i%2 == 0; };
  75. auto s = stream::make_stream(v).filter( even );
  76. TS_ASSERT_EQUALS( 6 , s.accumulate(0) );
  77. }
  78. void test_accumulate_custom_function() {
  79. vec_t v{1, 2, 3, 4, 5};
  80. auto even = [](int_t i) { return i%2 == 0; };
  81. auto prod =[](int_t lhs, int_t rhs) { return lhs * rhs; };
  82. auto s = stream::make_stream(v).filter( even );
  83. TS_ASSERT_EQUALS( 8 , s.accumulate(prod, 1) );
  84. }
  85. void test_flatmap_joins_lists() {
  86. vec_t vv{1, 2, 3, 4, 5};
  87. auto next3 = [](int_t i) { return vec_t{i, i+1, i+2}; };
  88. vec_t expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7};
  89. auto s = stream::make_stream(vv).flatmap( next3 );
  90. vec_t o{s.begin(), s.end()};
  91. TS_ASSERT_EQUALS(expected, o);
  92. }
  93. void test_dereference() {
  94. int val = 5;
  95. std::vector<int*> v{&val};
  96. auto data = stream::make_stream(v).deref().collect();
  97. TS_ASSERT_EQUALS(data.front(), val);
  98. }
  99. void test_foreach() {
  100. int hits = 0;
  101. vec_t v{1, 2, 3, 4, 5};
  102. stream::make_stream(v).each([&hits](int_t val){ ++hits; });
  103. TS_ASSERT_EQUALS(hits, 5);
  104. }
  105. void test_getmember() {
  106. struct test { int val; };
  107. std::vector<test> v{{1}, {3}, {2}};
  108. vec_t expected{1, 3, 2};
  109. auto out = stream::make_stream(v).map(&test::val).collect();
  110. TS_ASSERT_EQUALS(out, expected);
  111. }
  112. void test_memberfunction() {
  113. std::vector<std::string> v{"hello", "goodbye"};
  114. std::vector<std::string::size_type> expected{5, 7};
  115. auto out = stream::make_stream(v).map(&std::string::size).collect();
  116. TS_ASSERT_EQUALS(out, expected);
  117. }
  118. void test_cast() {
  119. struct base { char cat[4] = "cat"; };
  120. struct test : base { test(int v) : val(v) {} int val; };
  121. std::vector<test> v{{1}, {3}, {2}};
  122. auto strm = stream::make_stream(v).cast<base>();
  123. auto first = stream::make_stream(v).map([](test const & t) { return (void*)&t; }).collect();
  124. auto second = strm.map([](base const & t) { return (void*)&t; }).collect();
  125. TS_ASSERT_EQUALS(first, second);
  126. }
  127. };