• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Jason Rice 2016
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/at_key.hpp>
7 #include <boost/hana/core/to.hpp>
8 #include <boost/hana/equal.hpp>
9 #include <boost/hana/hash.hpp>
10 #include <boost/hana/integral_constant.hpp>
11 #include <boost/hana/map.hpp>
12 #include <boost/hana/not.hpp>
13 #include <boost/hana/pair.hpp>
14 #include <boost/hana/tuple.hpp>
15 namespace hana = boost::hana;
16 
17 
18 struct A { };
19 struct B { };
20 
21 struct the_hash;
22 
23 namespace boost { namespace hana {
24     template <>
25     struct hash_impl<A> {
applyboost::hana::hash_impl26         static constexpr auto apply(A const&) {
27             return hana::type_c<the_hash>;
28         }
29     };
30 
31     template <>
32     struct hash_impl<B> {
applyboost::hana::hash_impl33         static constexpr auto apply(B const&) {
34             return hana::type_c<the_hash>;
35         }
36     };
37 
38     template <>
39     struct equal_impl<A, A> {
applyboost::hana::equal_impl40         static constexpr auto apply(A const&, A const&) {
41             return hana::true_c;
42         }
43     };
44 
45     template <>
46     struct equal_impl<B, B> {
applyboost::hana::equal_impl47         static constexpr auto apply(B const&, B const&) {
48             return hana::true_c;
49         }
50     };
51 }}
52 
main()53 int main() {
54     constexpr auto key1 = A{};
55     constexpr auto key2 = B{};
56 
57     BOOST_HANA_CONSTANT_CHECK(hana::equal(key1, key1));
58     BOOST_HANA_CONSTANT_CHECK(hana::equal(key2, key2));
59     BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::equal(key1, key2)));
60 
61     // ensure the hashes actually collide
62     BOOST_HANA_CONSTANT_CHECK(hana::equal(hana::hash(key1), hana::hash(key2)));
63 
64     {
65         auto map = hana::to_map(hana::make_tuple(
66             hana::make_pair(key1, key1),
67             hana::make_pair(key2, key2)
68         ));
69 
70         BOOST_HANA_CONSTANT_CHECK(hana::equal(
71             hana::at_key(map, key1),
72             key1
73         ));
74 
75         BOOST_HANA_CONSTANT_CHECK(hana::equal(
76             hana::at_key(map, key2),
77             key2
78         ));
79     }
80 
81     {
82         auto map = hana::to_map(hana::make_tuple(
83             hana::make_pair(key2, key2),
84             hana::make_pair(key1, key1)
85         ));
86 
87         BOOST_HANA_CONSTANT_CHECK(hana::equal(
88             hana::at_key(map, key1),
89             key1
90         ));
91 
92         BOOST_HANA_CONSTANT_CHECK(hana::equal(
93             hana::at_key(map, key2),
94             key2
95         ));
96     }
97 
98     {
99         auto map = hana::to_map(hana::make_tuple(
100             hana::make_pair(key1, key1),
101             hana::make_pair(hana::int_c<56>, hana::int_c<56>),
102             hana::make_pair(key2, key2)
103         ));
104 
105         BOOST_HANA_CONSTANT_CHECK(hana::equal(
106             hana::at_key(map, key1),
107             key1
108         ));
109 
110         BOOST_HANA_CONSTANT_CHECK(hana::equal(
111             hana::at_key(map, hana::int_c<56>),
112             hana::int_c<56>
113         ));
114 
115         BOOST_HANA_CONSTANT_CHECK(hana::equal(
116             hana::at_key(map, key2),
117             key2
118         ));
119     }
120 
121     {
122         auto map = hana::to_map(hana::make_tuple(
123             hana::make_pair(key1, key1),
124             hana::make_pair(hana::int_c<56>, hana::int_c<56>),
125             hana::make_pair(key2, key2),
126             hana::make_pair(hana::int_c<42>, hana::int_c<42>)
127         ));
128 
129         BOOST_HANA_CONSTANT_CHECK(hana::equal(
130             hana::at_key(map, key1),
131             key1
132         ));
133 
134         BOOST_HANA_CONSTANT_CHECK(hana::equal(
135             hana::at_key(map, hana::int_c<56>),
136             hana::int_c<56>
137         ));
138 
139         BOOST_HANA_CONSTANT_CHECK(hana::equal(
140             hana::at_key(map, key2),
141             key2
142         ));
143 
144         BOOST_HANA_CONSTANT_CHECK(hana::equal(
145             hana::at_key(map, hana::int_c<42>),
146             hana::int_c<42>
147         ));
148     }
149 }
150