all_of.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef BOOST_HANA_TEST_AUTO_ALL_OF_HPP
  5. #define BOOST_HANA_TEST_AUTO_ALL_OF_HPP
  6. #include <boost/hana/all_of.hpp>
  7. #include <boost/hana/assert.hpp>
  8. #include <boost/hana/bool.hpp>
  9. #include <boost/hana/equal.hpp>
  10. #include <boost/hana/not.hpp>
  11. #include "test_case.hpp"
  12. #include <laws/base.hpp>
  13. TestCase test_all_of{[]{
  14. namespace hana = boost::hana;
  15. using hana::test::ct_eq;
  16. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  17. MAKE_TUPLE(),
  18. [](auto) { return hana::false_c; }
  19. ));
  20. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  21. MAKE_TUPLE(ct_eq<0>{}),
  22. [](auto) { return hana::true_c; }
  23. ));
  24. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  25. MAKE_TUPLE(ct_eq<0>{}),
  26. [](auto) { return hana::false_c; }
  27. )));
  28. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  29. MAKE_TUPLE(ct_eq<0>{}),
  30. hana::equal.to(ct_eq<0>{})
  31. ));
  32. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  33. MAKE_TUPLE(ct_eq<0>{}),
  34. hana::equal.to(ct_eq<999>{})
  35. )));
  36. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  37. MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
  38. [](auto) { return hana::true_c; }
  39. ));
  40. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  41. MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
  42. [](auto) { return hana::false_c; }
  43. )));
  44. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  45. MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}),
  46. hana::equal.to(ct_eq<0>{})
  47. ));
  48. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  49. MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
  50. hana::equal.to(ct_eq<0>{})
  51. )));
  52. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  53. MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}),
  54. [](auto) { return hana::true_c; }
  55. ));
  56. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  57. MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}),
  58. [](auto) { return hana::false_c; }
  59. )));
  60. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  61. MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}),
  62. hana::equal.to(ct_eq<0>{})
  63. ));
  64. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  65. MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<0>{}),
  66. hana::equal.to(ct_eq<0>{})
  67. )));
  68. BOOST_HANA_CONSTANT_CHECK(hana::all_of(
  69. MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}),
  70. hana::equal.to(ct_eq<0>{})
  71. ));
  72. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  73. MAKE_TUPLE(ct_eq<999>{}, ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}),
  74. hana::equal.to(ct_eq<0>{})
  75. )));
  76. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  77. MAKE_TUPLE(ct_eq<0>{}, ct_eq<999>{}, ct_eq<0>{}, ct_eq<0>{}),
  78. hana::equal.to(ct_eq<0>{})
  79. )));
  80. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  81. MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<999>{}, ct_eq<0>{}),
  82. hana::equal.to(ct_eq<0>{})
  83. )));
  84. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
  85. MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}, ct_eq<999>{}),
  86. hana::equal.to(ct_eq<0>{})
  87. )));
  88. // Make sure `all_of` short-circuits with runtime predicates
  89. // See http://stackoverflow.com/q/42012512/627587
  90. {
  91. {
  92. int counter = 0;
  93. auto tuple = MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{});
  94. hana::all_of(tuple, [&](auto) { ++counter; return false; });
  95. BOOST_HANA_RUNTIME_CHECK(counter == 1);
  96. }
  97. {
  98. int counter = 0;
  99. auto tuple = MAKE_TUPLE(ct_eq<0>{}, ct_eq<999>{}, ct_eq<0>{});
  100. hana::all_of(tuple, [&](auto x) -> bool {
  101. ++counter;
  102. return hana::equal(x, ct_eq<0>{});
  103. });
  104. BOOST_HANA_RUNTIME_CHECK(counter == 2);
  105. }
  106. }
  107. }};
  108. #endif // !BOOST_HANA_TEST_AUTO_ALL_OF_HPP