for_each.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_FOR_EACH_HPP
  5. #define BOOST_HANA_TEST_AUTO_FOR_EACH_HPP
  6. #include <boost/hana/assert.hpp>
  7. #include <boost/hana/for_each.hpp>
  8. #include "test_case.hpp"
  9. #include <laws/base.hpp>
  10. #include <vector>
  11. TestCase test_for_each{[]{
  12. namespace hana = boost::hana;
  13. using hana::test::ct_eq;
  14. // Make sure the function is applied in left-to-right order.
  15. {
  16. auto check = [](auto ...xs) {
  17. std::vector<int> seen{};
  18. hana::for_each(MAKE_TUPLE(xs...), [&](int x) {
  19. seen.push_back(x);
  20. });
  21. BOOST_HANA_RUNTIME_CHECK(seen == std::vector<int>{xs...});
  22. };
  23. check();
  24. check(0);
  25. check(0, 1);
  26. check(0, 1, 2);
  27. check(0, 1, 2, 3);
  28. check(0, 1, 2, 3, 4);
  29. }
  30. // Make sure the function is never called when the sequence is empty.
  31. {
  32. struct undefined { };
  33. hana::for_each(MAKE_TUPLE(), undefined{});
  34. }
  35. // Make sure it works with heterogeneous sequences.
  36. {
  37. hana::for_each(MAKE_TUPLE(ct_eq<0>{}), [](auto) { });
  38. hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}), [](auto) { });
  39. hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}), [](auto) { });
  40. hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}, ct_eq<3>{}), [](auto) { });
  41. }
  42. // Make sure for_each is constexpr when used with a constexpr function
  43. // and constexpr arguments. This used not to be the case.
  44. #ifndef MAKE_TUPLE_NO_CONSTEXPR
  45. {
  46. struct f { constexpr void operator()(int) const { } };
  47. constexpr int i = (hana::for_each(MAKE_TUPLE(1, 2, 3), f{}), 0);
  48. (void)i;
  49. }
  50. #endif
  51. }};
  52. #endif // !BOOST_HANA_TEST_AUTO_FOR_EACH_HPP