summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/_include/laws/constant.hpp
blob: 1acd335e457833d26c75e27a84e1d1449a857542 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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_LAWS_CONSTANT_HPP
#define BOOST_HANA_TEST_LAWS_CONSTANT_HPP

#include <boost/hana/assert.hpp>
#include <boost/hana/bool.hpp>
#include <boost/hana/concept/comparable.hpp>
#include <boost/hana/core/when.hpp>
#include <boost/hana/functional/capture.hpp>
#include <boost/hana/concept/logical.hpp>

#include <laws/base.hpp>
#include <laws/comparable.hpp>
#include <laws/euclidean_ring.hpp>
#include <laws/group.hpp>
#include <laws/logical.hpp>
#include <laws/monoid.hpp>
#include <laws/orderable.hpp>
#include <laws/ring.hpp>

#include <type_traits>


namespace boost { namespace hana { namespace test {
    template <typename C, typename = when<true>>
    struct TestConstant {
        using T = typename C::value_type;

        template <typename X>
        struct wrap_arbitrary_constant {
            static constexpr bool value = boost::hana::value<X>();
            using hana_tag = detail::CanonicalConstant<T>;
        };

        template <typename Xs, typename Convertibles>
        TestConstant(Xs xs, Convertibles types) {
            hana::for_each(xs, [](auto x) {
                static_assert(Constant<decltype(x)>{}, "");
            });

            hana::for_each(xs, hana::capture(types)([](auto types, auto c) {

                // constexpr-ness of hana::value(c)
                constexpr auto must_be_constexpr1 = hana::value(c);
                constexpr auto must_be_constexpr2 = hana::value<decltype(c)>();
                (void)must_be_constexpr1;
                (void)must_be_constexpr2;

                // consistency of C::value_type
                static_assert(std::is_same<
                    T,
                    tag_of_t<decltype(hana::value(c))>
                >{}, "");

                // equivalence of value_of(c) and value<decltype(c)>
                BOOST_HANA_CHECK(hana::equal(
                    hana::value_of(c),
                    hana::value<decltype(c)>()
                ));

                // equivalence of value<decltype(c)>() and value(c)
                BOOST_HANA_CHECK(hana::equal(
                    hana::value<decltype(c)>(),
                    hana::value(c)
                ));

                // conversion from an arbitrary Constant
                (void)to<C>(wrap_arbitrary_constant<decltype(c)>{});
                static_assert(is_embedded<detail::CanonicalConstant<T>, C>{}, "");

                hana::for_each(types, hana::capture(c)([](auto c, auto u) {
                    using U = typename decltype(u)::type;

                    // conversion to something to which the underlying data
                    // type can be converted.
                    BOOST_HANA_CHECK(equal(
                        to<U>(c),
                        make<U>(hana::value(c))
                    ));
                    static_assert(is_embedded<C, U>::value ^iff^ is_embedded<T, U>::value, "");

                    // common data type
                    static_assert(std::is_same<
                        common_t<C, detail::CanonicalConstant<U>>,
                        detail::CanonicalConstant<common_t<T, U>>
                    >{}, "");

                    static_assert(std::is_same<
                        common_t<detail::CanonicalConstant<U>, C>,
                        detail::CanonicalConstant<common_t<T, U>>
                    >{}, "");

                    static_assert(std::is_same<
                        common_t<C, U>,
                        common_t<typename C::value_type, U>
                    >{}, "");
                }));
            }));
        }
    };
}}} // end namespace boost::hana::test

#endif // !BOOST_HANA_TEST_LAWS_CONSTANT_HPP