• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
4 
5 #include <boost/hana/assert.hpp>
6 #include <boost/hana/contains.hpp>
7 #include <boost/hana/core/to.hpp>
8 #include <boost/hana/equal.hpp>
9 #include <boost/hana/map.hpp>
10 #include <boost/hana/permutations.hpp>
11 
12 #include <laws/base.hpp>
13 #include <support/minimal_product.hpp>
14 #include <support/seq.hpp>
15 namespace hana = boost::hana;
16 
17 
18 template <int i>
key()19 auto key() { return hana::test::ct_eq<i>{}; }
20 
21 template <int i>
val()22 auto val() { return hana::test::ct_eq<-i>{}; }
23 
24 template <int i, int j>
p()25 auto p() { return ::minimal_product(key<i>(), val<j>()); }
26 
main()27 int main() {
28     constexpr auto foldable = ::seq;
29     auto sequence = ::seq;
30 
31     // Foldable -> Map
32     {
33         BOOST_HANA_CONSTANT_CHECK(hana::equal(
34             hana::to_map(foldable()),
35             hana::make_map()
36         ));
37         BOOST_HANA_CONSTANT_CHECK(hana::equal(
38             hana::to_map(foldable(p<1, 1>())),
39             hana::make_map(p<1, 1>())
40         ));
41         BOOST_HANA_CONSTANT_CHECK(hana::equal(
42             hana::to_map(foldable(p<1, 1>(), p<2, 2>())),
43             hana::make_map(p<1, 1>(), p<2, 2>())
44         ));
45         BOOST_HANA_CONSTANT_CHECK(hana::equal(
46             hana::to_map(foldable(p<1, 1>(), p<2, 2>(), p<3, 3>())),
47             hana::make_map(p<1, 1>(), p<2, 2>(), p<3, 3>())
48         ));
49 
50         // with duplicates
51         BOOST_HANA_CONSTANT_CHECK(hana::equal(
52             hana::to_map(foldable(p<1, 1>(), p<1, 99>())),
53             hana::make_map(p<1, 1>())
54         ));
55         BOOST_HANA_CONSTANT_CHECK(hana::equal(
56             hana::to_map(foldable(p<1, 1>(), p<2, 2>(), p<1, 99>())),
57             hana::make_map(p<1, 1>(), p<2, 2>())
58         ));
59         BOOST_HANA_CONSTANT_CHECK(hana::equal(
60             hana::to_map(foldable(p<1, 1>(), p<2, 2>(), p<1, 99>(), p<2, 99>())),
61             hana::make_map(p<1, 1>(), p<2, 2>())
62         ));
63         BOOST_HANA_CONSTANT_CHECK(hana::equal(
64             hana::to_map(foldable(p<1, 1>(), p<2, 2>(), p<1, 99>(), p<2, 99>(), p<3, 3>())),
65             hana::make_map(p<1, 1>(), p<2, 2>(), p<3, 3>())
66         ));
67     }
68 
69     // Map -> Sequence
70     {
71         auto check = [=](auto ...xs) {
72             BOOST_HANA_CONSTANT_CHECK(hana::contains(
73                 hana::permutations(sequence(xs...)),
74                 hana::to<::Seq>(hana::make_map(xs...))
75             ));
76         };
77 
78         check();
79         check(p<1, 1>());
80         check(p<1, 1>(), p<2, 2>());
81         check(p<1, 1>(), p<2, 2>(), p<3, 3>());
82         check(p<1, 1>(), p<2, 2>(), p<3, 3>(), p<4, 4>());
83     }
84 
85     // to_map == to<map_tag>
86     {
87         BOOST_HANA_CONSTANT_CHECK(hana::equal(
88             hana::to_map(foldable(p<1, 1>(), p<2, 2>(), p<1, 99>(), p<2, 99>(), p<3, 3>())),
89             hana::to<hana::map_tag>(foldable(p<1, 1>(), p<2, 2>(), p<1, 99>(), p<2, 99>(), p<3, 3>()))
90         ));
91     }
92 }
93