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)
5 #ifndef BOOST_HANA_TEST_LAWS_COMPARABLE_HPP
6 #define BOOST_HANA_TEST_LAWS_COMPARABLE_HPP
8 #include <boost/hana/and.hpp>
9 #include <boost/hana/assert.hpp>
10 #include <boost/hana/bool.hpp>
11 #include <boost/hana/comparing.hpp>
12 #include <boost/hana/concept/comparable.hpp>
13 #include <boost/hana/concept/constant.hpp>
14 #include <boost/hana/concept/product.hpp>
15 #include <boost/hana/concept/sequence.hpp>
16 #include <boost/hana/core/make.hpp>
17 #include <boost/hana/core/when.hpp>
18 #include <boost/hana/equal.hpp>
19 #include <boost/hana/first.hpp>
20 #include <boost/hana/for_each.hpp>
21 #include <boost/hana/lazy.hpp>
22 #include <boost/hana/not_equal.hpp>
23 #include <boost/hana/second.hpp>
24 #include <boost/hana/value.hpp>
27 #include <laws/base.hpp>
30 namespace boost { namespace hana { namespace test {
31 template <typename T, typename = hana::when<true>>
32 struct TestComparable : TestComparable<T, laws> {
33 using TestComparable<T, laws>::TestComparable;
37 struct TestComparable<T, laws> {
38 template <typename Xs>
39 TestComparable(Xs xs) {
40 hana::for_each(xs, [](auto x) {
41 static_assert(hana::Comparable<decltype(x)>{}, "");
44 foreach2(xs, [](auto a, auto b) {
53 hana::equal(a, b) ^implies^ hana::equal(b, a)
56 // `not_equal` is the negation of `equal`
58 hana::not_equal(a, b) ^iff^ hana::not_(hana::equal(a, b))
61 // equal.to and not_equal.to
63 hana::equal.to(a)(b) ^iff^ hana::equal(a, b)
67 hana::not_equal.to(a)(b) ^iff^ hana::not_equal(a, b)
73 hana::comparing(f)(a, b) ^iff^ hana::equal(f(a), f(b))
78 foreach3(xs, [](auto a, auto b, auto c) {
80 hana::and_(hana::equal(a, b), hana::equal(b, c))
81 ^implies^ hana::equal(a, c)
88 struct TestComparable<C, when<Constant<C>::value>>
89 : TestComparable<C, laws>
91 template <typename Xs>
92 TestComparable(Xs xs) : TestComparable<C, laws>{xs} {
93 foreach2(xs, [](auto a, auto b) {
95 hana::value(hana::equal(a, b)) ^iff^
96 hana::equal(hana::value(a), hana::value(b))
102 template <typename P>
103 struct TestComparable<P, when<Product<P>::value>>
104 : TestComparable<P, laws>
106 template <typename Products>
107 TestComparable(Products products) : TestComparable<P, laws>{products} {
108 foreach2(products, [](auto x, auto y) {
110 hana::equal(x, y) ^iff^
112 hana::equal(hana::first(x), hana::first(y)),
113 hana::equal(hana::second(x), hana::second(y))
120 template <typename S>
121 struct TestComparable<S, when<Sequence<S>::value>>
122 : TestComparable<S, laws>
125 using x = _constant<i>;
127 template <typename Xs>
128 TestComparable(Xs xs) : TestComparable<S, laws>{xs} {
129 constexpr auto list = make<S>;
131 //////////////////////////////////////////////////////////////////
133 //////////////////////////////////////////////////////////////////
134 BOOST_HANA_CONSTANT_CHECK(hana::equal(
138 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::equal(
142 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::equal(
146 BOOST_HANA_CONSTANT_CHECK(hana::equal(
151 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::equal(
152 list(x<0>{}, x<1>{}),
155 BOOST_HANA_CONSTANT_CHECK(hana::equal(
156 list(x<0>{}, x<1>{}),
159 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::equal(
160 list(x<0>{}, x<1>{}, x<2>{}, x<3>{}),
161 list(x<0>{}, x<1>{}, x<2>{}, x<4>{})
165 }}} // end namespace boost::hana::test
167 #endif // !BOOST_HANA_TEST_LAWS_COMPARABLE_HPP