stream_fluent.t.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // stream_fluent.t.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 1/28/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include <vector>
  10. #include "streams.hpp"
  11. #include "streams/fluent.hpp"
  12. class stream_fluent_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. using int_t = int const &;
  15. using vec_t = std::vector<int>;
  16. public:
  17. void test_collect_arg_preserves() {
  18. vec_t v{1, 2, 3, 4, 5};
  19. auto s = stream::make_stream(v);
  20. std::set<int> o{};
  21. s > o;
  22. for ( int i : v ) {
  23. TS_ASSERT_EQUALS(o.count( i ), 1);
  24. }
  25. }
  26. void test_map_identity() {
  27. vec_t v{1, 2, 3, 4, 5};
  28. auto identity = [](int_t i) { return i; };
  29. auto s = stream::make_stream(v) | identity;
  30. vec_t o{s.begin(), s.end()};
  31. TS_ASSERT_EQUALS(v, o);
  32. }
  33. void test_map_change() {
  34. vec_t v{1, 2, 3, 4, 5};
  35. vec_t expected{3, 5, 7, 9, 11};
  36. auto fmap = [](int_t i) { return 2*i+1; };
  37. auto s = stream::make_stream(v) | fmap;
  38. vec_t o{s.begin(), s.end()};
  39. TS_ASSERT_EQUALS(expected, o);
  40. }
  41. void test_filter_noop() {
  42. vec_t v{1, 2, 3, 4, 5};
  43. auto pass = [](int_t) { return true; };
  44. auto s = stream::make_stream(v) | pass;
  45. vec_t o{s.begin(), s.end()};
  46. TS_ASSERT_EQUALS(v, o);
  47. }
  48. void test_filter_value() {
  49. vec_t v{1, 2, 3, 4, 5};
  50. vec_t expected{2, 4};
  51. auto even = [](int_t i) { return i%2 == 0; };
  52. auto s = stream::make_stream(v) | even;
  53. vec_t o{s.begin(), s.end()};
  54. TS_ASSERT_EQUALS(expected, o);
  55. }
  56. void test_accumulate() {
  57. vec_t v{1, 2, 3, 4, 5};
  58. auto even = [](int_t i) { return i%2 == 0; };
  59. auto sum =[](int_t lhs, int_t rhs) { return lhs + rhs; };
  60. auto s = stream::make_stream(v) | even;
  61. TS_ASSERT_EQUALS( 6 , s > sum );
  62. }
  63. void test_flatmap_joins_lists() {
  64. vec_t vv{1, 2, 3, 4, 5};
  65. auto next3 = [](int_t i) { return vec_t{i, i+1, i+2}; };
  66. vec_t expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7};
  67. auto s = stream::make_stream(vv) || next3;
  68. vec_t o{s.begin(), s.end()};
  69. TS_ASSERT_EQUALS(expected, o);
  70. }
  71. };