]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // Copyright Louis Dionne 2013-2017 |
7c673cae FG |
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_LAWS_MONOID_HPP | |
6 | #define BOOST_HANA_TEST_LAWS_MONOID_HPP | |
7 | ||
8 | #include <boost/hana/assert.hpp> | |
9 | #include <boost/hana/bool.hpp> | |
10 | #include <boost/hana/concept/comparable.hpp> | |
11 | #include <boost/hana/core/when.hpp> | |
12 | #include <boost/hana/functional/capture.hpp> | |
13 | #include <boost/hana/lazy.hpp> | |
14 | #include <boost/hana/concept/monoid.hpp> | |
15 | ||
16 | #include <laws/base.hpp> | |
17 | ||
18 | ||
19 | namespace boost { namespace hana { namespace test { | |
20 | template <typename M, typename = when<true>> | |
21 | struct TestMonoid : TestMonoid<M, laws> { | |
22 | using TestMonoid<M, laws>::TestMonoid; | |
23 | }; | |
24 | ||
25 | template <typename M> | |
26 | struct TestMonoid<M, laws> { | |
27 | template <typename Xs> | |
28 | TestMonoid(Xs xs) { | |
29 | hana::for_each(xs, hana::capture(xs)([](auto xs, auto a) { | |
b32b8144 | 30 | static_assert(Monoid<decltype(a)>{}, ""); |
7c673cae FG |
31 | |
32 | // left identity | |
33 | BOOST_HANA_CHECK(hana::equal( | |
34 | hana::plus(zero<M>(), a), | |
35 | a | |
36 | )); | |
37 | ||
38 | // right identity | |
39 | BOOST_HANA_CHECK(hana::equal( | |
40 | hana::plus(a, zero<M>()), | |
41 | a | |
42 | )); | |
43 | ||
44 | hana::for_each(xs, | |
45 | hana::capture(xs, a)([](auto xs, auto a, auto b) { | |
46 | hana::for_each(xs, | |
47 | hana::capture(a, b)([](auto a, auto b, auto c) { | |
48 | // associativity | |
49 | BOOST_HANA_CHECK(equal( | |
50 | hana::plus(a, hana::plus(b, c)), | |
51 | hana::plus(hana::plus(a, b), c) | |
52 | )); | |
53 | })); | |
54 | })); | |
55 | ||
56 | })); | |
57 | } | |
58 | }; | |
59 | ||
60 | template <typename C> | |
61 | struct TestMonoid<C, when<Constant<C>::value>> | |
62 | : TestMonoid<C, laws> | |
63 | { | |
64 | template <typename Xs> | |
65 | TestMonoid(Xs xs) : TestMonoid<C, laws>{xs} { | |
66 | ||
67 | BOOST_HANA_CHECK(hana::equal( | |
68 | hana::value(zero<C>()), | |
69 | zero<typename C::value_type>() | |
70 | )); | |
71 | ||
72 | foreach2(xs, [](auto x, auto y) { | |
73 | BOOST_HANA_CHECK(hana::equal( | |
74 | hana::plus(hana::value(x), hana::value(y)), | |
75 | hana::value(hana::plus(x, y)) | |
76 | )); | |
77 | }); | |
78 | } | |
79 | }; | |
80 | }}} // end namespace boost::hana::test | |
81 | ||
82 | #endif // !BOOST_HANA_TEST_LAWS_MONOID_HPP |