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