summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/_include/auto/for_each.hpp
blob: 4e36e985334777ea682b37b8084bf9f157157d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#ifndef BOOST_HANA_TEST_AUTO_FOR_EACH_HPP
#define BOOST_HANA_TEST_AUTO_FOR_EACH_HPP

#include <boost/hana/assert.hpp>
#include <boost/hana/for_each.hpp>

#include "test_case.hpp"
#include <laws/base.hpp>

#include <vector>


TestCase test_for_each{[]{
    namespace hana = boost::hana;
    using hana::test::ct_eq;

    // Make sure the function is applied in left-to-right order.
    {
        auto check = [](auto ...xs) {
            std::vector<int> seen{};
            hana::for_each(MAKE_TUPLE(xs...), [&](int x) {
                seen.push_back(x);
            });
            BOOST_HANA_RUNTIME_CHECK(seen == std::vector<int>{xs...});
        };

        check();
        check(0);
        check(0, 1);
        check(0, 1, 2);
        check(0, 1, 2, 3);
        check(0, 1, 2, 3, 4);
    }

    // Make sure the function is never called when the sequence is empty.
    {
        struct undefined { };
        hana::for_each(MAKE_TUPLE(), undefined{});
    }

    // Make sure it works with heterogeneous sequences.
    {
        hana::for_each(MAKE_TUPLE(ct_eq<0>{}), [](auto) { });
        hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}), [](auto) { });
        hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}), [](auto) { });
        hana::for_each(MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}, ct_eq<3>{}), [](auto) { });
    }

    // Make sure for_each is constexpr when used with a constexpr function
    // and constexpr arguments. This used not to be the case.
#ifndef MAKE_TUPLE_NO_CONSTEXPR
    {
        struct f { constexpr void operator()(int) const { } };
        constexpr int i = (hana::for_each(MAKE_TUPLE(1, 2, 3), f{}), 0);
        (void)i;
    }
#endif
}};

#endif // !BOOST_HANA_TEST_AUTO_FOR_EACH_HPP