if.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. if.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/if.hpp>
  8. #include "test.hpp"
  9. #include <boost/hof/first_of.hpp>
  10. #include <boost/hof/placeholders.hpp>
  11. struct is_5
  12. {
  13. template<class T>
  14. constexpr bool operator()(T i) const
  15. {
  16. return i == 5;
  17. }
  18. };
  19. struct is_not_5
  20. {
  21. template<class T>
  22. constexpr bool operator()(T i) const
  23. {
  24. return i != 5;
  25. }
  26. };
  27. template<class F>
  28. struct test_int
  29. {
  30. template<class T>
  31. constexpr bool operator()(T x) const
  32. {
  33. return boost::hof::first_of(
  34. boost::hof::if_(std::is_integral<T>())(F()),
  35. boost::hof::always(true)
  36. )(x);
  37. }
  38. };
  39. BOOST_HOF_TEST_CASE()
  40. {
  41. BOOST_HOF_TEST_CHECK(test_int<is_5>()(5));
  42. BOOST_HOF_TEST_CHECK(test_int<is_5>()(5L));
  43. BOOST_HOF_TEST_CHECK(test_int<is_5>()(5.0));
  44. BOOST_HOF_TEST_CHECK(test_int<is_5>()(6.0));
  45. BOOST_HOF_TEST_CHECK(test_int<is_not_5>()(6));
  46. BOOST_HOF_TEST_CHECK(test_int<is_not_5>()(6L));
  47. BOOST_HOF_TEST_CHECK(test_int<is_not_5>()(5.0));
  48. BOOST_HOF_TEST_CHECK(test_int<is_not_5>()(6.0));
  49. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_5>()(5));
  50. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_5>()(5L));
  51. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_5>()(5.0));
  52. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_5>()(6.0));
  53. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_not_5>()(6));
  54. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_not_5>()(6L));
  55. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_not_5>()(5.0));
  56. BOOST_HOF_STATIC_TEST_CHECK(test_int<is_not_5>()(6.0));
  57. }
  58. template<class F>
  59. struct test_int_c
  60. {
  61. template<class T>
  62. constexpr bool operator()(T x) const
  63. {
  64. return boost::hof::first_of(
  65. boost::hof::if_c<std::is_integral<T>::value>(F()),
  66. boost::hof::always(true)
  67. )(x);
  68. }
  69. };
  70. BOOST_HOF_TEST_CASE()
  71. {
  72. BOOST_HOF_TEST_CHECK(test_int_c<is_5>()(5));
  73. BOOST_HOF_TEST_CHECK(test_int_c<is_5>()(5L));
  74. BOOST_HOF_TEST_CHECK(test_int_c<is_5>()(5.0));
  75. BOOST_HOF_TEST_CHECK(test_int_c<is_5>()(6.0));
  76. BOOST_HOF_TEST_CHECK(test_int_c<is_not_5>()(6));
  77. BOOST_HOF_TEST_CHECK(test_int_c<is_not_5>()(6L));
  78. BOOST_HOF_TEST_CHECK(test_int_c<is_not_5>()(5.0));
  79. BOOST_HOF_TEST_CHECK(test_int_c<is_not_5>()(6.0));
  80. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_5>()(5));
  81. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_5>()(5L));
  82. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_5>()(5.0));
  83. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_5>()(6.0));
  84. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_not_5>()(6));
  85. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_not_5>()(6L));
  86. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_not_5>()(5.0));
  87. BOOST_HOF_STATIC_TEST_CHECK(test_int_c<is_not_5>()(6.0));
  88. }
  89. struct sum_f
  90. {
  91. template<class T>
  92. constexpr int operator()(T x, T y) const
  93. {
  94. return boost::hof::first_of(
  95. boost::hof::if_(std::is_integral<T>())(boost::hof::_ + boost::hof::_),
  96. boost::hof::always(0)
  97. )(x, y);
  98. }
  99. };
  100. BOOST_HOF_TEST_CASE()
  101. {
  102. BOOST_HOF_TEST_CHECK(sum_f()(1, 2) == 3);
  103. BOOST_HOF_TEST_CHECK(sum_f()(1.0, 2.0) == 0);
  104. BOOST_HOF_TEST_CHECK(sum_f()("", "") == 0);
  105. BOOST_HOF_STATIC_TEST_CHECK(sum_f()(1, 2) == 3);
  106. BOOST_HOF_STATIC_TEST_CHECK(sum_f()("", "") == 0);
  107. }
  108. struct sum_f_c
  109. {
  110. template<class T>
  111. constexpr int operator()(T x, T y) const
  112. {
  113. return boost::hof::first_of(
  114. boost::hof::if_c<std::is_integral<T>::value>(boost::hof::_ + boost::hof::_),
  115. boost::hof::always(0)
  116. )(x, y);
  117. }
  118. };
  119. BOOST_HOF_TEST_CASE()
  120. {
  121. BOOST_HOF_TEST_CHECK(sum_f_c()(1, 2) == 3);
  122. BOOST_HOF_TEST_CHECK(sum_f_c()(1.0, 2.0) == 0);
  123. BOOST_HOF_TEST_CHECK(sum_f_c()("", "") == 0);
  124. BOOST_HOF_STATIC_TEST_CHECK(sum_f_c()(1, 2) == 3);
  125. BOOST_HOF_STATIC_TEST_CHECK(sum_f_c()("", "") == 0);
  126. }
  127. #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
  128. BOOST_HOF_TEST_CASE()
  129. {
  130. static_assert(noexcept(boost::hof::if_(std::is_integral<int>())(boost::hof::identity)(1)), "noexcept if");
  131. }
  132. #endif