compose.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. compose.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/compose.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 compose_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::compose(increment(), decrement(), increment())(3)), "noexcept compose");
  66. }
  67. #endif
  68. BOOST_HOF_TEST_CASE()
  69. {
  70. BOOST_HOF_TEST_CHECK(boost::hof::compose(boost::hof::identity)(3) == 3);
  71. BOOST_HOF_TEST_CHECK(boost::hof::compose(boost::hof::identity, boost::hof::identity)(3) == 3);
  72. BOOST_HOF_TEST_CHECK(boost::hof::compose(boost::hof::identity, boost::hof::identity, boost::hof::identity)(3) == 3);
  73. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::compose(boost::hof::identity)(3) == 3);
  74. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::compose(boost::hof::identity, boost::hof::identity)(3) == 3);
  75. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::compose(boost::hof::identity, boost::hof::identity, boost::hof::identity)(3) == 3);
  76. }
  77. BOOST_HOF_TEST_CASE()
  78. {
  79. int r = boost::hof::compose(increment(), decrement(), increment())(3);
  80. BOOST_HOF_TEST_CHECK(r == 4);
  81. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::compose(increment(), decrement(), increment())(3) == 4);
  82. }
  83. BOOST_HOF_TEST_CASE()
  84. {
  85. int r = boost::hof::compose(increment(), negate(), decrement(), decrement())(3);
  86. BOOST_HOF_TEST_CHECK(r == 0);
  87. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::compose(increment(), negate(), decrement(), decrement())(3) == 0);
  88. }
  89. BOOST_HOF_TEST_CASE()
  90. {
  91. constexpr auto f = boost::hof::compose(increment(), decrement());
  92. #ifndef _MSC_VER
  93. static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
  94. #endif
  95. static_assert(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(decltype(f)), "Compose function not default constructible");
  96. int r = f(3);
  97. BOOST_HOF_TEST_CHECK(r == 3);
  98. BOOST_HOF_STATIC_TEST_CHECK(f(3) == 3);
  99. }
  100. #ifndef _MSC_VER
  101. BOOST_HOF_TEST_CASE()
  102. {
  103. constexpr auto f = boost::hof::compose(increment(), negate(), decrement(), decrement());
  104. static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
  105. static_assert(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(decltype(f)), "Compose function not default constructible");
  106. int r = f(3);
  107. BOOST_HOF_TEST_CHECK(r == 0);
  108. BOOST_HOF_STATIC_TEST_CHECK(f(3) == 0);
  109. }
  110. #endif
  111. BOOST_HOF_TEST_CASE()
  112. {
  113. STATIC_ASSERT_MOVE_ONLY(increment_movable);
  114. STATIC_ASSERT_MOVE_ONLY(decrement_movable);
  115. int r = boost::hof::compose(increment_movable(), decrement_movable(), increment_movable())(3);
  116. BOOST_HOF_TEST_CHECK(r == 4);
  117. }
  118. template<class T>
  119. struct print;
  120. BOOST_HOF_TEST_CASE()
  121. {
  122. const auto f = boost::hof::compose([](int i) { return i+1; }, [](int i) { return i-1; }, [](int i) { return i+1; });
  123. #ifndef _MSC_VER
  124. static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
  125. #endif
  126. int r = f(3);
  127. BOOST_HOF_TEST_CHECK(r == 4);
  128. }
  129. BOOST_HOF_STATIC_FUNCTION(f_compose_single_function) = boost::hof::compose(increment());
  130. BOOST_HOF_TEST_CASE()
  131. {
  132. BOOST_HOF_TEST_CHECK(f_compose_single_function(3) == 4);
  133. BOOST_HOF_STATIC_TEST_CHECK(f_compose_single_function(3) == 4);
  134. }
  135. BOOST_HOF_STATIC_FUNCTION(f_compose_function) = boost::hof::compose(increment(), decrement(), increment());
  136. BOOST_HOF_TEST_CASE()
  137. {
  138. BOOST_HOF_TEST_CHECK(f_compose_function(3) == 4);
  139. BOOST_HOF_STATIC_TEST_CHECK(f_compose_function(3) == 4);
  140. }
  141. BOOST_HOF_STATIC_FUNCTION(f_compose_function_4) = boost::hof::compose(increment(), negate(), decrement(), decrement());
  142. BOOST_HOF_TEST_CASE()
  143. {
  144. BOOST_HOF_TEST_CHECK(f_compose_function_4(3) == 0);
  145. BOOST_HOF_STATIC_TEST_CHECK(f_compose_function_4(3) == 0);
  146. }
  147. BOOST_HOF_STATIC_LAMBDA_FUNCTION(f_compose_lambda) = boost::hof::compose(
  148. [](int i) { return i+1; },
  149. [](int i) { return i-1; },
  150. [](int i) { return i+1; }
  151. );
  152. BOOST_HOF_TEST_CASE()
  153. {
  154. int r = f_compose_lambda(3);
  155. BOOST_HOF_TEST_CHECK(r == 4);
  156. }
  157. BOOST_HOF_TEST_CASE()
  158. {
  159. BOOST_HOF_TEST_CHECK(boost::hof::compose(boost::hof::_1 * boost::hof::_1, boost::hof::_1 + boost::hof::_1)(3) == 36);
  160. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::compose(boost::hof::_1 * boost::hof::_1, boost::hof::_1 + boost::hof::_1)(3) == 36);
  161. }
  162. }