main.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/adjust_if.hpp>
  5. #include <boost/hana/ap.hpp>
  6. #include <boost/hana/assert.hpp>
  7. #include <boost/hana/chain.hpp>
  8. #include <boost/hana/equal.hpp>
  9. #include <boost/hana/fill.hpp>
  10. #include <boost/hana/functional/always.hpp>
  11. #include <boost/hana/functional/compose.hpp>
  12. #include <boost/hana/lift.hpp>
  13. #include <boost/hana/replace_if.hpp>
  14. #include <boost/hana/tap.hpp>
  15. #include <boost/hana/then.hpp>
  16. #include <boost/hana/transform.hpp>
  17. #include <boost/hana/tuple.hpp>
  18. #include <laws/applicative.hpp>
  19. #include <laws/base.hpp>
  20. #include <laws/functor.hpp>
  21. #include <laws/monad.hpp>
  22. #include <support/cnumeric.hpp>
  23. #include <support/identity.hpp>
  24. namespace hana = boost::hana;
  25. using hana::test::ct_eq;
  26. int main() {
  27. hana::test::_injection<0> f{};
  28. // Functor
  29. {
  30. auto functor = ::identity;
  31. // adjust_if
  32. {
  33. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  34. hana::adjust_if(functor(ct_eq<0>{}), hana::always(cnumeric<bool, true>), f),
  35. functor(f(ct_eq<0>{}))
  36. ));
  37. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  38. hana::adjust_if(functor(ct_eq<0>{}), hana::always(cnumeric<bool, false>), f),
  39. functor(ct_eq<0>{})
  40. ));
  41. }
  42. // fill
  43. {
  44. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  45. hana::fill(functor(ct_eq<0>{}), ct_eq<1>{}),
  46. functor(ct_eq<1>{})
  47. ));
  48. }
  49. // transform
  50. {
  51. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  52. hana::transform(functor(ct_eq<0>{}), f),
  53. functor(f(ct_eq<0>{}))
  54. ));
  55. }
  56. // replace_if
  57. {
  58. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  59. hana::replace_if(functor(ct_eq<0>{}), hana::always(cnumeric<bool, true>), ct_eq<1>{}),
  60. functor(ct_eq<1>{})
  61. ));
  62. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  63. hana::replace_if(functor(ct_eq<0>{}), hana::always(cnumeric<bool, false>), ct_eq<1>{}),
  64. functor(ct_eq<0>{})
  65. ));
  66. }
  67. }
  68. // Applicative
  69. {
  70. auto a = ::identity;
  71. using A = ::Identity;
  72. // ap
  73. {
  74. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  75. hana::ap(a(f), a(ct_eq<0>{})),
  76. a(f(ct_eq<0>{}))
  77. ));
  78. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  79. hana::ap(a(f), a(ct_eq<0>{}), a(ct_eq<1>{})),
  80. a(f(ct_eq<0>{}, ct_eq<1>{}))
  81. ));
  82. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  83. hana::ap(a(f), a(ct_eq<0>{}), a(ct_eq<1>{}), a(ct_eq<2>{})),
  84. a(f(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}))
  85. ));
  86. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  87. hana::ap(a(f), a(ct_eq<0>{}), a(ct_eq<1>{}), a(ct_eq<2>{}), a(ct_eq<3>{})),
  88. a(f(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}, ct_eq<3>{}))
  89. ));
  90. }
  91. // lift
  92. {
  93. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  94. hana::lift<A>(ct_eq<0>{}),
  95. a(ct_eq<0>{})
  96. ));
  97. }
  98. }
  99. // Monad
  100. {
  101. auto monad = ::identity;
  102. using M = ::Identity;
  103. auto f = hana::compose(monad, hana::test::_injection<0>{});
  104. // chain
  105. {
  106. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  107. hana::chain(monad(ct_eq<1>{}), f),
  108. f(ct_eq<1>{})
  109. ));
  110. }
  111. // tap
  112. {
  113. bool executed = false;
  114. auto exec = [&](auto) { executed = true; };
  115. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  116. hana::chain(monad(ct_eq<0>{}), hana::tap<M>(exec)),
  117. monad(ct_eq<0>{})
  118. ));
  119. BOOST_HANA_RUNTIME_CHECK(executed);
  120. }
  121. // then
  122. {
  123. struct invalid { };
  124. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  125. hana::then(monad(invalid{}), monad(ct_eq<0>{})),
  126. monad(ct_eq<0>{})
  127. ));
  128. }
  129. }
  130. }