stream.t.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <vector>
  10. #include "streams.hpp"
  11. class stream_TestSuite : public CxxTest::TestSuite {
  12. public:
  13. using int_t = int const &;
  14. using vec_t = std::vector<int>;
  15. public:
  16. void test_preserved() {
  17. vec_t v{1, 2, 3, 4, 5};
  18. auto s = stream::make_stream(v);
  19. vec_t o{s.begin(), s.end()};
  20. TS_ASSERT_EQUALS(v, o);
  21. }
  22. void test_collect_preserves() {
  23. vec_t v{1, 2, 3, 4, 5};
  24. auto s = stream::make_stream(v);
  25. vec_t o{s.begin(), s.end()};
  26. vec_t o2{s.collect()};
  27. TS_ASSERT_EQUALS(v, o);
  28. TS_ASSERT_EQUALS(v, o2);
  29. }
  30. void test_collect_arg_preserves() {
  31. vec_t v{1, 2, 3, 4, 5};
  32. auto s = stream::make_stream(v);
  33. std::set<int> o{};
  34. s.collect(o);
  35. for ( int i : v ) {
  36. TS_ASSERT_EQUALS(o.count( i ), 1);
  37. }
  38. }
  39. void test_map_identity() {
  40. vec_t v{1, 2, 3, 4, 5};
  41. auto identity = [](int_t i) { return i; };
  42. auto s = stream::make_stream(v).map( identity );
  43. vec_t o{s.begin(), s.end()};
  44. TS_ASSERT_EQUALS(v, o);
  45. }
  46. void test_map_change() {
  47. vec_t v{1, 2, 3, 4, 5};
  48. vec_t expected{3, 5, 7, 9, 11};
  49. auto fmap = [](int_t i) { return 2*i+1; };
  50. auto s = stream::make_stream(v).map( fmap );
  51. vec_t o{s.begin(), s.end()};
  52. TS_ASSERT_EQUALS(expected, o);
  53. }
  54. void test_filter_noop() {
  55. vec_t v{1, 2, 3, 4, 5};
  56. auto pass = [](int_t i) { return true; };
  57. auto s = stream::make_stream(v).filter( pass );
  58. vec_t o{s.begin(), s.end()};
  59. TS_ASSERT_EQUALS(v, o);
  60. }
  61. void test_filter_value() {
  62. vec_t v{1, 2, 3, 4, 5};
  63. vec_t expected{2, 4};
  64. auto even = [](int_t i) { return i%2 == 0; };
  65. auto s = stream::make_stream(v).filter( even );
  66. vec_t o{s.begin(), s.end()};
  67. TS_ASSERT_EQUALS(expected, o);
  68. }
  69. void test_accumulate() {
  70. vec_t v{1, 2, 3, 4, 5};
  71. auto even = [](int_t i) { return i%2 == 0; };
  72. auto sum =[](int_t lhs, int_t rhs) { return lhs + rhs; };
  73. auto s = stream::make_stream(v).filter( even );
  74. TS_ASSERT_EQUALS( 6 , s.accumulate(sum, 0) );
  75. }
  76. void test_flatmap_joins_lists() {
  77. vec_t vv{1, 2, 3, 4, 5};
  78. auto next3 = [](int_t i) { return vec_t{i, i+1, i+2}; };
  79. vec_t expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7};
  80. auto s = stream::make_stream(vv).flatmap( next3 );
  81. vec_t o{s.begin(), s.end()};
  82. TS_ASSERT_EQUALS(expected, o);
  83. }
  84. void test_dereference() {
  85. int val = 5;
  86. std::vector<int*> v{&val};
  87. auto data = stream::make_stream(v).deref().collect();
  88. TS_ASSERT_EQUALS(data.front(), val);
  89. }
  90. };