]>
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_AUTO_SORT_HPP | |
6 | #define BOOST_HANA_TEST_AUTO_SORT_HPP | |
7 | ||
8 | #include <boost/hana/all_of.hpp> | |
9 | #include <boost/hana/assert.hpp> | |
10 | #include <boost/hana/equal.hpp> | |
11 | #include <boost/hana/less.hpp> | |
12 | #include <boost/hana/permutations.hpp> | |
13 | #include <boost/hana/sort.hpp> | |
14 | #include <boost/hana/transform.hpp> | |
15 | ||
16 | #include "test_case.hpp" | |
17 | #include <laws/base.hpp> | |
18 | #include <support/equivalence_class.hpp> | |
19 | ||
20 | ||
21 | TestCase test_sort{[]{ | |
22 | namespace hana = boost::hana; | |
23 | using hana::test::ct_eq; | |
24 | using hana::test::ct_ord; | |
25 | ||
26 | // Test without a custom predicate | |
27 | { | |
28 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
29 | hana::sort(MAKE_TUPLE()), | |
30 | MAKE_TUPLE() | |
31 | )); | |
32 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
33 | hana::sort(MAKE_TUPLE(ct_ord<0>{})), | |
34 | MAKE_TUPLE(ct_ord<0>{}) | |
35 | )); | |
36 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
37 | hana::sort(MAKE_TUPLE(ct_ord<0>{}, ct_ord<1>{})), | |
38 | MAKE_TUPLE(ct_ord<0>{}, ct_ord<1>{}) | |
39 | )); | |
40 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
41 | hana::sort(MAKE_TUPLE(ct_ord<1>{}, ct_ord<0>{})), | |
42 | MAKE_TUPLE(ct_ord<0>{}, ct_ord<1>{}) | |
43 | )); | |
44 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
45 | hana::sort(MAKE_TUPLE(ct_ord<1>{}, ct_ord<0>{}, ct_ord<4>{}, ct_ord<2>{})), | |
46 | MAKE_TUPLE(ct_ord<0>{}, ct_ord<1>{}, ct_ord<2>{}, ct_ord<4>{}) | |
47 | )); | |
48 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
49 | hana::sort(MAKE_TUPLE(ct_ord<1>{}, ct_ord<0>{}, ct_ord<-4>{}, ct_ord<2>{})), | |
50 | MAKE_TUPLE(ct_ord<-4>{}, ct_ord<0>{}, ct_ord<1>{}, ct_ord<2>{}) | |
51 | )); | |
52 | } | |
53 | ||
54 | // Test with a custom predicate | |
55 | { | |
56 | auto pred = [](auto x, auto y) { | |
57 | return hana::less(x.unwrap, y.unwrap); | |
58 | }; | |
59 | auto a = [](auto z) { return ::equivalence_class(ct_eq<999>{}, z); }; | |
60 | auto b = [](auto z) { return ::equivalence_class(ct_eq<888>{}, z); }; | |
61 | ||
62 | auto check = [=](auto ...sorted) { | |
63 | auto perms = hana::transform( | |
64 | hana::permutations(MAKE_TUPLE(a(sorted)...)), | |
65 | hana::sort.by(pred) | |
66 | ); | |
67 | BOOST_HANA_CONSTANT_CHECK(hana::all_of(perms, [=](auto xs) { | |
68 | return hana::equal(xs, MAKE_TUPLE(a(sorted)...)); | |
69 | })); | |
70 | }; | |
71 | ||
72 | check(); | |
73 | check(ct_ord<1>{}); | |
74 | check(ct_ord<1>{}, ct_ord<2>{}); | |
75 | check(ct_ord<1>{}, ct_ord<2>{}, ct_ord<3>{}); | |
76 | ||
77 | // check stability | |
78 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
79 | hana::sort(MAKE_TUPLE(a(ct_ord<1>{}), b(ct_ord<1>{})), pred), | |
80 | MAKE_TUPLE(a(ct_ord<1>{}), b(ct_ord<1>{})) | |
81 | )); | |
82 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
83 | hana::sort(MAKE_TUPLE(b(ct_ord<1>{}), a(ct_ord<1>{})), pred), | |
84 | MAKE_TUPLE(b(ct_ord<1>{}), a(ct_ord<1>{})) | |
85 | )); | |
86 | ||
87 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
88 | hana::sort(MAKE_TUPLE(a(ct_ord<1>{}), b(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<2>{})), pred), | |
89 | MAKE_TUPLE(a(ct_ord<1>{}), b(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<2>{})) | |
90 | )); | |
91 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
92 | hana::sort(MAKE_TUPLE(a(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<1>{}), b(ct_ord<2>{})), pred), | |
93 | MAKE_TUPLE(a(ct_ord<1>{}), b(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<2>{})) | |
94 | )); | |
95 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
96 | hana::sort(MAKE_TUPLE(b(ct_ord<1>{}), a(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<2>{})), pred), | |
97 | MAKE_TUPLE(b(ct_ord<1>{}), a(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<2>{})) | |
98 | )); | |
99 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
100 | hana::sort(MAKE_TUPLE(a(ct_ord<2>{}), b(ct_ord<1>{}), b(ct_ord<2>{}), a(ct_ord<1>{})), pred), | |
101 | MAKE_TUPLE(b(ct_ord<1>{}), a(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<2>{})) | |
102 | )); | |
103 | ||
104 | BOOST_HANA_CONSTANT_CHECK(hana::equal( | |
105 | hana::sort(MAKE_TUPLE(a(ct_ord<1>{}), a(ct_ord<3>{}), b(ct_ord<1>{}), a(ct_ord<2>{}), b(ct_ord<3>{})), pred), | |
106 | MAKE_TUPLE(a(ct_ord<1>{}), b(ct_ord<1>{}), a(ct_ord<2>{}), a(ct_ord<3>{}), b(ct_ord<3>{})) | |
107 | )); | |
108 | } | |
109 | }}; | |
110 | ||
111 | #endif // !BOOST_HANA_TEST_AUTO_SORT_HPP |