proj.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. proj.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/proj.hpp>
  8. #include <boost/hof/placeholders.hpp>
  9. #include <boost/hof/mutable.hpp>
  10. #include "test.hpp"
  11. #include <memory>
  12. struct foo
  13. {
  14. constexpr foo(int xp) : x(xp)
  15. {}
  16. int x;
  17. };
  18. struct select_x
  19. {
  20. template<class T>
  21. constexpr auto operator()(T&& x) const BOOST_HOF_RETURNS(x.x);
  22. };
  23. BOOST_HOF_TEST_CASE()
  24. {
  25. #ifndef _MSC_VER
  26. constexpr
  27. #endif
  28. auto add = boost::hof::_ + boost::hof::_;
  29. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::proj(select_x(), add)(foo(1), foo(2)) == 3);
  30. // Using mutable_ as a workaround on libc++, since mem_fn does not meet the
  31. // requirements of a FunctionObject
  32. BOOST_HOF_TEST_CHECK(boost::hof::proj(boost::hof::mutable_(std::mem_fn(&foo::x)), add)(foo(1), foo(2)) == 3);
  33. static_assert(boost::hof::detail::is_default_constructible<decltype(boost::hof::proj(select_x(), add))>::value, "Not default constructible");
  34. }
  35. BOOST_HOF_TEST_CASE()
  36. {
  37. #ifndef _MSC_VER
  38. constexpr
  39. #endif
  40. auto add = boost::hof::_ + boost::hof::_;
  41. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::proj(select_x(), add)(foo(1), foo(2)) == 3);
  42. BOOST_HOF_TEST_CHECK(boost::hof::proj(&foo::x, add)(foo(1), foo(2)) == 3);
  43. static_assert(boost::hof::detail::is_default_constructible<decltype(boost::hof::proj(select_x(), add))>::value, "Not default constructible");
  44. }
  45. BOOST_HOF_TEST_CASE()
  46. {
  47. auto indirect_add = boost::hof::proj(*boost::hof::_, boost::hof::_ + boost::hof::_);
  48. BOOST_HOF_TEST_CHECK(indirect_add(std::unique_ptr<int>(new int(1)), std::unique_ptr<int>(new int(2))) == 3);
  49. static_assert(boost::hof::detail::is_default_constructible<decltype(indirect_add)>::value, "Not default constructible");
  50. }
  51. struct select_x_1
  52. {
  53. std::unique_ptr<int> i;
  54. select_x_1() : i(new int(2))
  55. {}
  56. template<class T>
  57. auto operator()(T&& x) const BOOST_HOF_RETURNS(x.x * (*i));
  58. };
  59. struct sum_1
  60. {
  61. std::unique_ptr<int> i;
  62. sum_1() : i(new int(1))
  63. {}
  64. template<class T, class U>
  65. auto operator()(T&& x, U&& y) const BOOST_HOF_RETURNS(x + y + *i);
  66. };
  67. BOOST_HOF_TEST_CASE()
  68. {
  69. BOOST_HOF_TEST_CHECK(boost::hof::proj(select_x_1(), sum_1())(foo(1), foo(2)) == 7);
  70. }
  71. BOOST_HOF_TEST_CASE()
  72. {
  73. std::string s;
  74. auto f = [&](const std::string& x)
  75. {
  76. s += x;
  77. };
  78. boost::hof::proj(f)("hello", "-", "world");
  79. BOOST_HOF_TEST_CHECK(s == "hello-world");
  80. boost::hof::proj(f)();
  81. BOOST_HOF_TEST_CHECK(s == "hello-world");
  82. }
  83. BOOST_HOF_TEST_CASE()
  84. {
  85. std::string s;
  86. auto f = [&](const std::string& x)
  87. {
  88. s += x;
  89. return s;
  90. };
  91. auto last = [](const std::string& x, const std::string& y, const std::string& z)
  92. {
  93. BOOST_HOF_TEST_CHECK(x == "hello");
  94. BOOST_HOF_TEST_CHECK(y == "hello-");
  95. BOOST_HOF_TEST_CHECK(z == "hello-world");
  96. return z;
  97. };
  98. BOOST_HOF_TEST_CHECK(boost::hof::proj(f, last)("hello", "-", "world") == "hello-world");
  99. }
  100. template<bool B>
  101. struct bool_
  102. {
  103. static const bool value = B;
  104. };
  105. struct constexpr_check
  106. {
  107. template<class T>
  108. constexpr int operator()(T) const
  109. {
  110. static_assert(T::value, "Failed");
  111. return 0;
  112. }
  113. };
  114. struct constexpr_check_each
  115. {
  116. template<class T>
  117. constexpr bool operator()(T x) const
  118. {
  119. return boost::hof::proj(constexpr_check())(x, x), true;
  120. }
  121. };
  122. BOOST_HOF_TEST_CASE()
  123. {
  124. BOOST_HOF_STATIC_TEST_CHECK(constexpr_check_each()(bool_<true>()));
  125. }
  126. BOOST_HOF_TEST_CASE()
  127. {
  128. boost::hof::proj(boost::hof::identity, boost::hof::identity)(0);
  129. }
  130. struct bar {};
  131. BOOST_HOF_TEST_CASE()
  132. {
  133. auto f = boost::hof::proj(bar{});
  134. static_assert(!boost::hof::is_invocable<decltype(f), int>::value, "Not sfinae friendly");
  135. static_assert(!boost::hof::is_invocable<decltype(f), int, int>::value, "Not sfinae friendly");
  136. static_assert(!boost::hof::is_invocable<decltype(f), int, int, int>::value, "Not sfinae friendly");
  137. }
  138. BOOST_HOF_TEST_CASE()
  139. {
  140. auto f = boost::hof::proj(bar{}, bar{});
  141. static_assert(!boost::hof::is_invocable<decltype(f), int>::value, "Not sfinae friendly");
  142. static_assert(!boost::hof::is_invocable<decltype(f), int, int>::value, "Not sfinae friendly");
  143. static_assert(!boost::hof::is_invocable<decltype(f), int, int, int>::value, "Not sfinae friendly");
  144. static_assert(!boost::hof::is_invocable<decltype(f)>::value, "Not sfinae friendly");
  145. }