summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/_include/laws/hashable.hpp
blob: 0fbd8f33337eaaaafe47e122cec79d5cb0d3fd0d (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
// 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_HASHABLE_HPP
#define BOOST_HANA_TEST_LAWS_HASHABLE_HPP

#include <boost/hana/assert.hpp>
#include <boost/hana/bool.hpp>
#include <boost/hana/concept/hashable.hpp>
#include <boost/hana/core/default.hpp>
#include <boost/hana/core/tag_of.hpp>
#include <boost/hana/core/when.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/hash.hpp>
#include <boost/hana/if.hpp>

#include <laws/base.hpp>


namespace boost { namespace hana { namespace test {
    template <typename G, typename = when<true>>
    struct TestHashable : TestHashable<G, laws> {
        using TestHashable<G, laws>::TestHashable;
    };

    template <typename H>
    struct TestHashable<H, laws> {
        template <typename Xs>
        TestHashable(Xs xs) {
            hana::for_each(xs, [](auto x) {
                static_assert(Hashable<decltype(x)>{}, "");
            });

            hana::for_each(xs, [&](auto const& x) {
                hana::for_each(xs, [&](auto const& y) {
                    using X = hana::tag_of_t<decltype(x)>;
                    using Y = hana::tag_of_t<decltype(y)>;
                    constexpr bool comparable = !hana::is_default<
                        hana::equal_impl<X, Y>
                    >::value;

                    hana::if_(hana::bool_c<comparable>,
                        [](auto const& x, auto const& y) {
                            BOOST_HANA_CHECK(
                                hana::equal(x, y)
                                    ^implies^
                                hana::equal(hana::hash(x), hana::hash(y))
                            );
                        },
                        [](auto...) { }
                    )(x, y);
                });
            });
        }
    };
}}} // end namespace boost::hana::test

#endif // !BOOST_HANA_TEST_LAWS_HASHABLE_HPP