flow.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. flow.cpp
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/hof/flow.hpp>
  8. #include <boost/hof/function.hpp>
  9. #include <boost/hof/lambda.hpp>
  10. #include <boost/hof/placeholders.hpp>
  11. #include <memory>
  12. #include "test.hpp"
  13. namespace flow_test {
  14. struct increment
  15. {
  16. template<class T>
  17. constexpr T operator()(T x) const noexcept
  18. {
  19. return x + 1;
  20. }
  21. };
  22. struct decrement
  23. {
  24. template<class T>
  25. constexpr T operator()(T x) const noexcept
  26. {
  27. return x - 1;
  28. }
  29. };
  30. struct negate
  31. {
  32. template<class T>
  33. constexpr T operator()(T x) const noexcept
  34. {
  35. return -x;
  36. }
  37. };
  38. struct increment_movable
  39. {
  40. std::unique_ptr<int> n;
  41. increment_movable()
  42. : n(new int(1))
  43. {}
  44. template<class T>
  45. T operator()(T x) const
  46. {
  47. return x + *n;
  48. }
  49. };
  50. struct decrement_movable
  51. {
  52. std::unique_ptr<int> n;
  53. decrement_movable()
  54. : n(new int(1))
  55. {}
  56. template<class T>
  57. T operator()(T x) const
  58. {
  59. return x - *n;
  60. }
  61. };
  62. #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
  63. BOOST_HOF_TEST_CASE()
  64. {
  65. static_assert(noexcept(boost::hof::flow(increment(), decrement(), increment())(3)), "noexcept flow");
  66. }
  67. #endif
  68. BOOST_HOF_TEST_CASE()
  69. {
  70. BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::identity)(3) == 3);
  71. BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity)(3) == 3);
  72. BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity, boost::hof::identity)(3) == 3);
  73. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::identity)(3) == 3);
  74. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity)(3) == 3);
  75. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity, boost::hof::identity)(3) == 3);
  76. }
  77. BOOST_HOF_TEST_CASE()
  78. {
  79. int r = boost::hof::flow(increment(), decrement(), increment())(3);
  80. BOOST_HOF_TEST_CHECK(r == 4);
  81. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(increment(), decrement(), increment())(3) == 4);
  82. }
  83. BOOST_HOF_TEST_CASE()
  84. {
  85. int r = boost::hof::flow(increment(), negate(), decrement(), decrement())(3);
  86. BOOST_HOF_TEST_CHECK(r == -6);
  87. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(increment(), negate(), decrement(), decrement())(3) == -6);
  88. }
  89. #ifndef _MSC_VER
  90. BOOST_HOF_TEST_CASE()
  91. {
  92. constexpr auto f = boost::hof::flow(increment(), decrement());
  93. static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
  94. int r = f(3);
  95. BOOST_HOF_TEST_CHECK(r == 3);
  96. BOOST_HOF_STATIC_TEST_CHECK(f(3) == 3);
  97. }
  98. #endif
  99. BOOST_HOF_TEST_CASE()
  100. {
  101. STATIC_ASSERT_MOVE_ONLY(increment_movable);
  102. STATIC_ASSERT_MOVE_ONLY(decrement_movable);
  103. int r = boost::hof::flow(increment_movable(), decrement_movable(), increment_movable())(3);
  104. BOOST_HOF_TEST_CHECK(r == 4);
  105. }
  106. BOOST_HOF_TEST_CASE()
  107. {
  108. const auto f = boost::hof::flow([](int i) { return i+1; }, [](int i) { return i-1; }, [](int i) { return i+1; });
  109. #ifndef _MSC_VER
  110. static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
  111. #endif
  112. int r = f(3);
  113. BOOST_HOF_TEST_CHECK(r == 4);
  114. }
  115. BOOST_HOF_STATIC_FUNCTION(f_flow_single_function) = boost::hof::flow(increment());
  116. BOOST_HOF_TEST_CASE()
  117. {
  118. BOOST_HOF_TEST_CHECK(f_flow_single_function(3) == 4);
  119. BOOST_HOF_STATIC_TEST_CHECK(f_flow_single_function(3) == 4);
  120. }
  121. BOOST_HOF_STATIC_FUNCTION(f_flow_function) = boost::hof::flow(increment(), decrement(), increment());
  122. BOOST_HOF_TEST_CASE()
  123. {
  124. BOOST_HOF_TEST_CHECK(f_flow_function(3) == 4);
  125. BOOST_HOF_STATIC_TEST_CHECK(f_flow_function(3) == 4);
  126. }
  127. BOOST_HOF_STATIC_FUNCTION(f_flow_function_4) = boost::hof::flow(increment(), negate(), decrement(), decrement());
  128. BOOST_HOF_TEST_CASE()
  129. {
  130. BOOST_HOF_TEST_CHECK(f_flow_function_4(3) == -6);
  131. BOOST_HOF_STATIC_TEST_CHECK(f_flow_function_4(3) == -6);
  132. }
  133. BOOST_HOF_STATIC_LAMBDA_FUNCTION(f_flow_lambda) = boost::hof::flow(
  134. [](int i) { return i+1; },
  135. [](int i) { return i-1; },
  136. [](int i) { return i+1; }
  137. );
  138. BOOST_HOF_TEST_CASE()
  139. {
  140. int r = f_flow_lambda(3);
  141. BOOST_HOF_TEST_CHECK(r == 4);
  142. }
  143. BOOST_HOF_TEST_CASE()
  144. {
  145. BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::_1 + boost::hof::_1, boost::hof::_1 * boost::hof::_1)(3) == 36);
  146. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::_1 + boost::hof::_1, boost::hof::_1 * boost::hof::_1)(3) == 36);
  147. }
  148. }