logical.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "minimal.hpp"
  5. #include <boost/hana/and.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/eval_if.hpp>
  8. #include <boost/hana/if.hpp>
  9. #include <boost/hana/not.hpp>
  10. #include <boost/hana/not_equal.hpp>
  11. #include <boost/hana/or.hpp>
  12. #include <boost/hana/tuple.hpp>
  13. #include <boost/hana/while.hpp>
  14. #include <laws/base.hpp>
  15. #include <laws/logical.hpp>
  16. namespace hana = boost::hana;
  17. struct invalid { };
  18. int main() {
  19. constexpr auto bools = hana::make_tuple(
  20. minimal_constant<bool, false>{},
  21. minimal_constant<bool, true>{}
  22. );
  23. hana::test::TestLogical<minimal_constant_tag<bool>>{bools};
  24. constexpr auto true_ = minimal_constant<bool, true>{};
  25. constexpr auto false_ = minimal_constant<bool, false>{};
  26. // not_
  27. {
  28. BOOST_HANA_CONSTANT_CHECK(hana::equal(hana::not_(true_), false_));
  29. BOOST_HANA_CONSTANT_CHECK(hana::equal(hana::not_(false_), true_));
  30. }
  31. // and_
  32. {
  33. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  34. hana::and_(true_),
  35. true_
  36. ));
  37. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  38. hana::and_(false_),
  39. false_
  40. ));
  41. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  42. hana::and_(true_, true_),
  43. true_
  44. ));
  45. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  46. hana::and_(true_, false_),
  47. false_
  48. ));
  49. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  50. hana::and_(false_, invalid{}),
  51. false_
  52. ));
  53. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  54. hana::and_(true_, true_, true_),
  55. true_
  56. ));
  57. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  58. hana::and_(true_, true_, false_),
  59. false_
  60. ));
  61. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  62. hana::and_(true_, false_, invalid{}),
  63. false_
  64. ));
  65. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  66. hana::and_(false_, invalid{}, invalid{}),
  67. false_
  68. ));
  69. }
  70. // or_
  71. {
  72. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  73. hana::or_(true_),
  74. true_
  75. ));
  76. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  77. hana::or_(false_),
  78. false_
  79. ));
  80. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  81. hana::or_(false_, false_),
  82. false_
  83. ));
  84. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  85. hana::or_(false_, true_),
  86. true_
  87. ));
  88. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  89. hana::or_(true_, invalid{}),
  90. true_
  91. ));
  92. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  93. hana::or_(false_, false_, false_),
  94. false_
  95. ));
  96. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  97. hana::or_(false_, false_, true_),
  98. true_
  99. ));
  100. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  101. hana::or_(false_, true_, invalid{}),
  102. true_
  103. ));
  104. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  105. hana::or_(true_, invalid{}, invalid{}),
  106. true_
  107. ));
  108. }
  109. // if_
  110. {
  111. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  112. hana::if_(true_, hana::test::ct_eq<3>{}, hana::test::ct_eq<4>{}),
  113. hana::test::ct_eq<3>{}
  114. ));
  115. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  116. hana::if_(false_, hana::test::ct_eq<3>{}, hana::test::ct_eq<4>{}),
  117. hana::test::ct_eq<4>{}
  118. ));
  119. }
  120. // eval_if
  121. {
  122. auto t = [](auto) { return hana::test::ct_eq<2>{}; };
  123. auto e = [](auto) { return hana::test::ct_eq<3>{}; };
  124. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  125. hana::eval_if(true_, t, invalid{}),
  126. hana::test::ct_eq<2>{}
  127. ));
  128. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  129. hana::eval_if(false_, invalid{}, e),
  130. hana::test::ct_eq<3>{}
  131. ));
  132. }
  133. // while_
  134. {
  135. hana::test::_injection<0> f{};
  136. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  137. hana::while_(hana::not_equal.to(hana::test::ct_eq<0>{}), hana::test::ct_eq<0>{}, invalid{}),
  138. hana::test::ct_eq<0>{}
  139. ));
  140. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  141. hana::while_(hana::not_equal.to(f(hana::test::ct_eq<0>{})), hana::test::ct_eq<0>{}, f),
  142. f(hana::test::ct_eq<0>{})
  143. ));
  144. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  145. hana::while_(hana::not_equal.to(f(f(hana::test::ct_eq<0>{}))), hana::test::ct_eq<0>{}, f),
  146. f(f(hana::test::ct_eq<0>{}))
  147. ));
  148. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  149. hana::while_(hana::not_equal.to(f(f(f(hana::test::ct_eq<0>{})))), hana::test::ct_eq<0>{}, f),
  150. f(f(f(hana::test::ct_eq<0>{})))
  151. ));
  152. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  153. hana::while_(hana::not_equal.to(f(f(f(f(hana::test::ct_eq<0>{}))))), hana::test::ct_eq<0>{}, f),
  154. f(f(f(f(hana::test::ct_eq<0>{}))))
  155. ));
  156. // Make sure it can be called with an lvalue state:
  157. auto state = hana::test::ct_eq<0>{};
  158. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  159. hana::while_(hana::not_equal.to(f(f(f(f(hana::test::ct_eq<0>{}))))), state, f),
  160. f(f(f(f(hana::test::ct_eq<0>{}))))
  161. ));
  162. }
  163. }