partial.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. partial.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/partial.hpp>
  8. #include <boost/hof/limit.hpp>
  9. #include "test.hpp"
  10. static constexpr boost::hof::static_<boost::hof::partial_adaptor<binary_class> > binary_partial = {};
  11. static constexpr boost::hof::static_<boost::hof::partial_adaptor<unary_class> > unary_partial = {};
  12. static constexpr boost::hof::static_<boost::hof::partial_adaptor<mutable_class> > mutable_partial = {};
  13. static constexpr boost::hof::static_<boost::hof::partial_adaptor<void_class> > void_partial = {};
  14. static constexpr boost::hof::static_<boost::hof::partial_adaptor<mono_class> > mono_partial = {};
  15. static constexpr boost::hof::static_<boost::hof::partial_adaptor<move_class> > move_partial = {};
  16. constexpr boost::hof::partial_adaptor<binary_class> binary_partial_constexpr = {};
  17. constexpr boost::hof::partial_adaptor<unary_class> unary_partial_constexpr = {};
  18. constexpr boost::hof::partial_adaptor<void_class> void_partial_constexpr = {};
  19. constexpr boost::hof::partial_adaptor<mono_class> mono_partial_constexpr = {};
  20. BOOST_HOF_TEST_CASE()
  21. {
  22. boost::hof::partial_adaptor<void_class>()(1);
  23. void_partial(1);
  24. void_partial()(1);
  25. BOOST_HOF_TEST_CHECK(3 == binary_partial(1)(2));
  26. BOOST_HOF_TEST_CHECK(3 == binary_partial(1, 2));
  27. BOOST_HOF_TEST_CHECK(3 == unary_partial()(3));
  28. BOOST_HOF_TEST_CHECK(3 == unary_partial(3));
  29. BOOST_HOF_TEST_CHECK(3 == mono_partial(2));
  30. BOOST_HOF_TEST_CHECK(3 == mono_partial()(2));
  31. int i1 = 1;
  32. BOOST_HOF_TEST_CHECK(3 == binary_partial(2)(i1));
  33. BOOST_HOF_TEST_CHECK(3 == mutable_partial(std::ref(i1))(2));
  34. BOOST_HOF_TEST_CHECK(3 == i1);
  35. int i2 = 1;
  36. BOOST_HOF_TEST_CHECK(3 == mutable_partial(i2, 2));
  37. BOOST_HOF_TEST_CHECK(3 == i2);
  38. }
  39. BOOST_HOF_TEST_CASE()
  40. {
  41. BOOST_HOF_TEST_CHECK(3 == (move_class()(1, 2)));
  42. BOOST_HOF_TEST_CHECK(3 == (move_partial(1, 2)));
  43. BOOST_HOF_TEST_CHECK(3 == (move_partial(1)(2)));
  44. BOOST_HOF_TEST_CHECK(3 == (boost::hof::partial(move_class())(1)(2)));
  45. BOOST_HOF_TEST_CHECK(3 == (boost::hof::partial(move_class())(1, 2)));
  46. }
  47. BOOST_HOF_TEST_CASE()
  48. {
  49. void_partial_constexpr(1);
  50. void_partial_constexpr()(1);
  51. BOOST_HOF_STATIC_TEST_CHECK(3 == binary_partial_constexpr(1)(2));
  52. BOOST_HOF_STATIC_TEST_CHECK(3 == binary_partial_constexpr(1, 2));
  53. BOOST_HOF_STATIC_TEST_CHECK(3 == unary_partial_constexpr()(3));
  54. BOOST_HOF_STATIC_TEST_CHECK(3 == unary_partial_constexpr(3));
  55. BOOST_HOF_STATIC_TEST_CHECK(3 == mono_partial_constexpr(2));
  56. BOOST_HOF_STATIC_TEST_CHECK(3 == mono_partial_constexpr()(2));
  57. }
  58. #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
  59. BOOST_HOF_TEST_CASE()
  60. {
  61. static_assert(noexcept(boost::hof::partial(binary_class{})(1)(2)), "noexcept partial");
  62. }
  63. #endif
  64. BOOST_HOF_TEST_CASE()
  65. {
  66. auto f = boost::hof::partial(boost::hof::limit_c<2>(binary_class()));
  67. static_assert(boost::hof::is_invocable<decltype(f), int>::value, "Passing the limit is not callable");
  68. static_assert(boost::hof::is_invocable<decltype(f), int, int>::value, "Passing the limit is not callable");
  69. static_assert(!boost::hof::is_invocable<decltype(f), int, int, int>::value, "Passing the limit is not callable");
  70. static_assert(!boost::hof::is_invocable<decltype(f), int, int, int, int>::value, "Passing the limit is not callable");
  71. auto g = f(0);
  72. static_assert(boost::hof::is_invocable<decltype(g), int>::value, "Passing the limit is not callable");
  73. static_assert(!boost::hof::is_invocable<decltype(g), int, int>::value, "Passing the limit is not callable");
  74. static_assert(!boost::hof::is_invocable<decltype(g), int, int, int>::value, "Passing the limit is not callable");
  75. static_assert(!boost::hof::is_invocable<decltype(g), int, int, int, int>::value, "Passing the limit is not callable");
  76. }
  77. BOOST_HOF_TEST_CASE()
  78. {
  79. auto f = boost::hof::partial(binary_class());
  80. static_assert(boost::hof::is_invocable<decltype(f), int>::value, "Passing the limit is not callable");
  81. static_assert(boost::hof::is_invocable<decltype(f), int, int>::value, "Passing the limit is not callable");
  82. static_assert(boost::hof::is_invocable<decltype(f), int, int, int>::value, "Passing the limit is not callable");
  83. static_assert(boost::hof::is_invocable<decltype(f), int, int, int, int>::value, "Passing the limit is not callable");
  84. auto g = f(0);
  85. static_assert(boost::hof::is_invocable<decltype(g), int>::value, "Passing the limit is not callable");
  86. static_assert(boost::hof::is_invocable<decltype(g), int, int>::value, "Passing the limit is not callable");
  87. static_assert(boost::hof::is_invocable<decltype(g), int, int, int>::value, "Passing the limit is not callable");
  88. static_assert(boost::hof::is_invocable<decltype(g), int, int, int, int>::value, "Passing the limit is not callable");
  89. }