From 2eb9867ec55287efa7f1d850ae0f77c9b084e59d Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Wed, 16 Jul 2014 17:12:19 -0400 Subject: [PATCH] Comparable: Check laws widely --- include/boost/hana/comparable/laws.hpp | 21 ++++++---- test/comparable/laws.cpp | 14 +++---- test/ext/boost/mpl/list/comparable/laws.cpp | 31 ++++++++++++++ test/ext/boost/mpl/vector/comparable/laws.cpp | 31 ++++++++++++++ .../std/integer_sequence/comparable/laws.cpp | 41 +++++++++++++++++++ .../{comparable.cpp => comparable/equal.cpp} | 0 test/integral/comparable/laws.cpp | 31 ++++++++++++++ test/list/typeclass/comparable/laws.cpp | 38 +++++++++++++++++ test/maybe/comparable.cpp | 27 ------------ test/maybe/comparable/equal.cpp | 23 +++++++++++ test/maybe/comparable/laws.cpp | 28 +++++++++++++ test/pair/instance/comparable/laws.cpp | 27 ++++++++++++ test/range/comparable.cpp | 26 ------------ test/range/comparable/equal.cpp | 26 ++++++++++++ test/range/comparable/laws.cpp | 29 +++++++++++++ test/type/comparable/equal.cpp | 26 ++++++++++++ .../{comparable.cpp => comparable/laws.cpp} | 12 +++--- test/type_list/comparable/laws.cpp | 30 ++++++++++++++ 18 files changed, 387 insertions(+), 74 deletions(-) create mode 100644 test/ext/boost/mpl/list/comparable/laws.cpp create mode 100644 test/ext/boost/mpl/vector/comparable/laws.cpp create mode 100644 test/ext/std/integer_sequence/comparable/laws.cpp rename test/integral/{comparable.cpp => comparable/equal.cpp} (100%) create mode 100644 test/integral/comparable/laws.cpp create mode 100644 test/list/typeclass/comparable/laws.cpp delete mode 100644 test/maybe/comparable.cpp create mode 100644 test/maybe/comparable/equal.cpp create mode 100644 test/maybe/comparable/laws.cpp create mode 100644 test/pair/instance/comparable/laws.cpp delete mode 100644 test/range/comparable.cpp create mode 100644 test/range/comparable/equal.cpp create mode 100644 test/range/comparable/laws.cpp create mode 100644 test/type/comparable/equal.cpp rename test/type/{comparable.cpp => comparable/laws.cpp} (54%) create mode 100644 test/type_list/comparable/laws.cpp diff --git a/include/boost/hana/comparable/laws.hpp b/include/boost/hana/comparable/laws.hpp index 0f87f86c8..4ecae25dc 100644 --- a/include/boost/hana/comparable/laws.hpp +++ b/include/boost/hana/comparable/laws.hpp @@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0. #include +#include #include @@ -25,14 +26,20 @@ namespace boost { namespace hana { //! if a == b && b == c then a == c // Transitivity //! @endcode struct Comparable::laws { - template - static constexpr auto check(A a, B b, C c) { + template + static constexpr auto check(ComparableObjects objs) { auto implies = [](auto p, auto q) { return or_(not_(p), q); }; - return and_( - equal(a, a), - implies(equal(a, b), equal(b, a)), - implies(and_(equal(a, b), equal(b, c)), equal(a, c)) - ); + return all([=](auto a) { + return all([=](auto b) { + return all([=](auto c) { + return and_( + equal(a, a), + implies(equal(a, b), equal(b, a)), + implies(and_(equal(a, b), equal(b, c)), equal(a, c)) + ); + }, objs); + }, objs); + }, objs); } }; }} // end namespace boost::hana diff --git a/test/comparable/laws.cpp b/test/comparable/laws.cpp index d5fc6cb81..924ee0282 100644 --- a/test/comparable/laws.cpp +++ b/test/comparable/laws.cpp @@ -18,15 +18,11 @@ using namespace boost::hana; template void test() { constexpr auto comparable = detail::minimal::comparable; - - BOOST_HANA_CONSTEXPR_LAMBDA auto xs = list(comparable(0), comparable(1), comparable(2)); - for_each(xs, [=](auto a) { - for_each(xs, [=](auto b) { - for_each(xs, [=](auto c) { - BOOST_HANA_STATIC_ASSERT(Comparable::laws::check(a, b, c)); - }); - }); - }); + BOOST_HANA_STATIC_ASSERT( + Comparable::laws::check( + list(comparable(0), comparable(1), comparable(2)) + ) + ); } int main() { diff --git a/test/ext/boost/mpl/list/comparable/laws.cpp b/test/ext/boost/mpl/list/comparable/laws.cpp new file mode 100644 index 000000000..6145d6c25 --- /dev/null +++ b/test/ext/boost/mpl/list/comparable/laws.cpp @@ -0,0 +1,31 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include + +#include +using namespace boost::hana; + + +template struct x; + +template +constexpr boost::mpl::list...> mpl_list{}; + +int main() { + BOOST_HANA_STATIC_ASSERT( + Comparable::laws::check( + list( + mpl_list<>, mpl_list<0>, mpl_list<0, 1>, + mpl_list<0, 1, 2>, mpl_list<1, 0, 2> + ) + ) + ); +} diff --git a/test/ext/boost/mpl/vector/comparable/laws.cpp b/test/ext/boost/mpl/vector/comparable/laws.cpp new file mode 100644 index 000000000..5414c879a --- /dev/null +++ b/test/ext/boost/mpl/vector/comparable/laws.cpp @@ -0,0 +1,31 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include + +#include +using namespace boost::hana; + + +template struct x; + +template +constexpr boost::mpl::vector...> mpl_vector{}; + +int main() { + BOOST_HANA_STATIC_ASSERT( + Comparable::laws::check( + list( + mpl_vector<>, mpl_vector<0>, mpl_vector<0, 1>, + mpl_vector<0, 1, 2>, mpl_vector<1, 0, 2> + ) + ) + ); +} diff --git a/test/ext/std/integer_sequence/comparable/laws.cpp b/test/ext/std/integer_sequence/comparable/laws.cpp new file mode 100644 index 000000000..29620e048 --- /dev/null +++ b/test/ext/std/integer_sequence/comparable/laws.cpp @@ -0,0 +1,41 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include + +#include +using namespace boost::hana; + + +template +void test() { + BOOST_HANA_STATIC_ASSERT( + Comparable::laws::check( + list( + std::integer_sequence{}, + std::integer_sequence{}, + + std::integer_sequence{}, + std::integer_sequence{}, + + std::integer_sequence{}, + std::integer_sequence{}, + + std::integer_sequence{}, + std::integer_sequence{} + ) + ) + ); +} + +int main() { + test(); + test(); +} diff --git a/test/integral/comparable.cpp b/test/integral/comparable/equal.cpp similarity index 100% rename from test/integral/comparable.cpp rename to test/integral/comparable/equal.cpp diff --git a/test/integral/comparable/laws.cpp b/test/integral/comparable/laws.cpp new file mode 100644 index 000000000..2a9cd071f --- /dev/null +++ b/test/integral/comparable/laws.cpp @@ -0,0 +1,31 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include +using namespace boost::hana; + + +template +void test() { + BOOST_HANA_STATIC_ASSERT( + Comparable::laws::check( + list( + integral, integral, + integral, integral, + integral, integral + ) + ) + ); +} + +int main() { + test(); + test(); +} diff --git a/test/list/typeclass/comparable/laws.cpp b/test/list/typeclass/comparable/laws.cpp new file mode 100644 index 000000000..8cb6fc4ac --- /dev/null +++ b/test/list/typeclass/comparable/laws.cpp @@ -0,0 +1,38 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include +#include +#include +using namespace boost::hana; + + +template +constexpr auto x = detail::minimal::comparable<>(i); + +template +void test() { + BOOST_HANA_CONSTEXPR_LAMBDA auto list = detail::minimal::list; + + BOOST_HANA_STATIC_ASSERT( + Comparable::laws::check( + list( + list(), + list(x<0>), + list(x<0>, x<1>), + list(x<0>, x<1>, x<2>) + ) + ) + ); +} + +int main() { + test>(); +} diff --git a/test/maybe/comparable.cpp b/test/maybe/comparable.cpp deleted file mode 100644 index 14008cefc..000000000 --- a/test/maybe/comparable.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* -@copyright Louis Dionne 2014 -Distributed under the Boost Software License, Version 1.0. -(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) - */ - -#include - -#include -#include -using namespace boost::hana; - - -int main() { - BOOST_HANA_STATIC_ASSERT(nothing == nothing); - BOOST_HANA_STATIC_ASSERT(nothing != just(int_<0>)); - BOOST_HANA_STATIC_ASSERT(just(int_<0>) != nothing); - - BOOST_HANA_STATIC_ASSERT(just(int_<0>) == just(int_<0>)); - BOOST_HANA_STATIC_ASSERT(just(int_<0>) != just(int_<1>)); - - - BOOST_HANA_STATIC_ASSERT(just(0) == just(int_<0>)); - BOOST_HANA_STATIC_ASSERT(just(0) != just(int_<1>)); - BOOST_HANA_STATIC_ASSERT(just(int_<0>) == just(0)); - BOOST_HANA_STATIC_ASSERT(just(int_<1>) != just(0)); -} diff --git a/test/maybe/comparable/equal.cpp b/test/maybe/comparable/equal.cpp new file mode 100644 index 000000000..4102698e8 --- /dev/null +++ b/test/maybe/comparable/equal.cpp @@ -0,0 +1,23 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +using namespace boost::hana; + + +template +constexpr auto x = detail::minimal::comparable<>(i); + +int main() { + BOOST_HANA_STATIC_ASSERT( equal(nothing, nothing)); + BOOST_HANA_STATIC_ASSERT(!equal(nothing, just(x<0>))); + BOOST_HANA_STATIC_ASSERT(!equal(just(x<0>), nothing)); + BOOST_HANA_STATIC_ASSERT( equal(just(x<0>), just(x<0>))); + BOOST_HANA_STATIC_ASSERT(!equal(just(x<0>), just(x<1>))); +} diff --git a/test/maybe/comparable/laws.cpp b/test/maybe/comparable/laws.cpp new file mode 100644 index 000000000..d372cf9c7 --- /dev/null +++ b/test/maybe/comparable/laws.cpp @@ -0,0 +1,28 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include +#include +using namespace boost::hana; + + +template +constexpr auto x = detail::minimal::comparable<>(i); + +int main() { + BOOST_HANA_STATIC_ASSERT(Comparable::laws::check( + list( + nothing, + just(x<0>), + just(x<1>), + just(x<2>) + ) + )); +} diff --git a/test/pair/instance/comparable/laws.cpp b/test/pair/instance/comparable/laws.cpp new file mode 100644 index 000000000..b8737010b --- /dev/null +++ b/test/pair/instance/comparable/laws.cpp @@ -0,0 +1,27 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include +#include +using namespace boost::hana; + + +template +constexpr auto x = detail::minimal::comparable<>(i); + +int main() { + BOOST_HANA_STATIC_ASSERT(Comparable::laws::check( + list( + pair(x<0>, x<1>), + pair(x<1>, x<0>), + pair(x<0>, x<0>) + ) + )); +} diff --git a/test/range/comparable.cpp b/test/range/comparable.cpp deleted file mode 100644 index f15e97e4e..000000000 --- a/test/range/comparable.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* -@copyright Louis Dionne 2014 -Distributed under the Boost Software License, Version 1.0. -(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) - */ - -#include - -#include -#include -using namespace boost::hana; - - -int main() { - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<0>) == range(int_<0>, int_<0>)); - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<0>) != range(int_<0>, int_<1>)); - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<1>) != range(int_<0>, int_<0>)); - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<1>) == range(int_<0>, int_<1>)); - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<2>) != range(int_<0>, int_<1>)); - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<2>) == range(int_<0>, int_<2>)); - BOOST_HANA_STATIC_ASSERT(range(int_<0>, int_<0>) == range(int_<2>, int_<2>)); - - BOOST_HANA_STATIC_ASSERT(range(int_<2>, int_<4>) == range(int_<2>, int_<4>)); - BOOST_HANA_STATIC_ASSERT(range(int_<-4>, int_<-3>) == range(int_<-4>, int_<-3>)); - BOOST_HANA_STATIC_ASSERT(range(int_<-4>, int_<2>) == range(int_<-4>, int_<2>)); -} diff --git a/test/range/comparable/equal.cpp b/test/range/comparable/equal.cpp new file mode 100644 index 000000000..37ca32fb9 --- /dev/null +++ b/test/range/comparable/equal.cpp @@ -0,0 +1,26 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +using namespace boost::hana; + + +int main() { + BOOST_HANA_STATIC_ASSERT( equal(range(int_<0>, int_<0>), range(int_<0>, int_<0>))); + BOOST_HANA_STATIC_ASSERT(!equal(range(int_<0>, int_<0>), range(int_<0>, int_<1>))); + BOOST_HANA_STATIC_ASSERT(!equal(range(int_<0>, int_<1>), range(int_<0>, int_<0>))); + BOOST_HANA_STATIC_ASSERT( equal(range(int_<0>, int_<1>), range(int_<0>, int_<1>))); + BOOST_HANA_STATIC_ASSERT(!equal(range(int_<0>, int_<2>), range(int_<0>, int_<1>))); + BOOST_HANA_STATIC_ASSERT( equal(range(int_<0>, int_<2>), range(int_<0>, int_<2>))); + BOOST_HANA_STATIC_ASSERT( equal(range(int_<0>, int_<0>), range(int_<2>, int_<2>))); + + BOOST_HANA_STATIC_ASSERT(equal(range(int_<2>, int_<4>), range(int_<2>, int_<4>))); + BOOST_HANA_STATIC_ASSERT(equal(range(int_<-4>, int_<-3>), range(int_<-4>, int_<-3>))); + BOOST_HANA_STATIC_ASSERT(equal(range(int_<-4>, int_<2>), range(int_<-4>, int_<2>))); +} diff --git a/test/range/comparable/laws.cpp b/test/range/comparable/laws.cpp new file mode 100644 index 000000000..2a872e0b0 --- /dev/null +++ b/test/range/comparable/laws.cpp @@ -0,0 +1,29 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include +#include +using namespace boost::hana; + + +int main() { + BOOST_HANA_STATIC_ASSERT(Comparable::laws::check( + list( + range(int_<0>, int_<0>), + range(int_<1>, int_<1>), + + range(int_<0>, int_<1>), + range(long_<0>, long_<1>), + range(long_<0>, int_<1>), + + range(int_<3>, int_<6>) + ) + )); +} diff --git a/test/type/comparable/equal.cpp b/test/type/comparable/equal.cpp new file mode 100644 index 000000000..cbf70b410 --- /dev/null +++ b/test/type/comparable/equal.cpp @@ -0,0 +1,26 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +using namespace boost::hana; + + +struct T; struct U; + +int main() { + BOOST_HANA_STATIC_ASSERT( equal(type, type)); + BOOST_HANA_STATIC_ASSERT(!equal(type, type)); + BOOST_HANA_STATIC_ASSERT(!equal(type, type)); + BOOST_HANA_STATIC_ASSERT(!equal(type, type)); + BOOST_HANA_STATIC_ASSERT( equal(type, type)); + + BOOST_HANA_STATIC_ASSERT( equal(type, type)); + BOOST_HANA_STATIC_ASSERT(!equal(type, type)); + BOOST_HANA_STATIC_ASSERT(!equal(type, type)); + BOOST_HANA_STATIC_ASSERT( equal(type, type)); +} diff --git a/test/type/comparable.cpp b/test/type/comparable/laws.cpp similarity index 54% rename from test/type/comparable.cpp rename to test/type/comparable/laws.cpp index 350e950bf..fa31604f3 100644 --- a/test/type/comparable.cpp +++ b/test/type/comparable/laws.cpp @@ -6,14 +6,16 @@ Distributed under the Boost Software License, Version 1.0. #include +#include #include +#include using namespace boost::hana; -int main() { - BOOST_HANA_STATIC_ASSERT(equal(type, type)); - BOOST_HANA_STATIC_ASSERT(!not_equal(type, type)); +struct T; struct U; - BOOST_HANA_STATIC_ASSERT(!equal(type, type)); - BOOST_HANA_STATIC_ASSERT(not_equal(type, type)); +int main() { + BOOST_HANA_STATIC_ASSERT(Comparable::laws::check( + list(type, type, type, type, type) + )); } diff --git a/test/type_list/comparable/laws.cpp b/test/type_list/comparable/laws.cpp new file mode 100644 index 000000000..7a83fe34f --- /dev/null +++ b/test/type_list/comparable/laws.cpp @@ -0,0 +1,30 @@ +/* +@copyright Louis Dionne 2014 +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) + */ + +#include + +#include +#include +#include +using namespace boost::hana; + + +template +struct x; + +template +constexpr auto tlist = type_list...>; + +int main() { + BOOST_HANA_STATIC_ASSERT(Comparable::laws::check( + list( + tlist<>, + tlist<0>, + tlist<0, 1>, + tlist<0, 1, 2> + ) + )); +}