at_key.collisions.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/at_key.hpp>
  6. #include <boost/hana/core/to.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/hash.hpp>
  9. #include <boost/hana/integral_constant.hpp>
  10. #include <boost/hana/map.hpp>
  11. #include <boost/hana/not.hpp>
  12. #include <boost/hana/pair.hpp>
  13. #include <boost/hana/tuple.hpp>
  14. namespace hana = boost::hana;
  15. struct A { };
  16. struct B { };
  17. struct the_hash;
  18. namespace boost { namespace hana {
  19. template <>
  20. struct hash_impl<A> {
  21. static constexpr auto apply(A const&) {
  22. return hana::type_c<the_hash>;
  23. }
  24. };
  25. template <>
  26. struct hash_impl<B> {
  27. static constexpr auto apply(B const&) {
  28. return hana::type_c<the_hash>;
  29. }
  30. };
  31. template <>
  32. struct equal_impl<A, A> {
  33. static constexpr auto apply(A const&, A const&) {
  34. return hana::true_c;
  35. }
  36. };
  37. template <>
  38. struct equal_impl<B, B> {
  39. static constexpr auto apply(B const&, B const&) {
  40. return hana::true_c;
  41. }
  42. };
  43. }}
  44. int main() {
  45. constexpr auto key1 = A{};
  46. constexpr auto key2 = B{};
  47. BOOST_HANA_CONSTANT_CHECK(hana::equal(key1, key1));
  48. BOOST_HANA_CONSTANT_CHECK(hana::equal(key2, key2));
  49. BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::equal(key1, key2)));
  50. // ensure the hashes actually collide
  51. BOOST_HANA_CONSTANT_CHECK(hana::equal(hana::hash(key1), hana::hash(key2)));
  52. {
  53. auto map = hana::to_map(hana::make_tuple(
  54. hana::make_pair(key1, key1),
  55. hana::make_pair(key2, key2)
  56. ));
  57. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  58. hana::at_key(map, key1),
  59. key1
  60. ));
  61. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  62. hana::at_key(map, key2),
  63. key2
  64. ));
  65. }
  66. {
  67. auto map = hana::to_map(hana::make_tuple(
  68. hana::make_pair(key2, key2),
  69. hana::make_pair(key1, key1)
  70. ));
  71. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  72. hana::at_key(map, key1),
  73. key1
  74. ));
  75. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  76. hana::at_key(map, key2),
  77. key2
  78. ));
  79. }
  80. {
  81. auto map = hana::to_map(hana::make_tuple(
  82. hana::make_pair(key1, key1),
  83. hana::make_pair(hana::int_c<56>, hana::int_c<56>),
  84. hana::make_pair(key2, key2)
  85. ));
  86. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  87. hana::at_key(map, key1),
  88. key1
  89. ));
  90. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  91. hana::at_key(map, hana::int_c<56>),
  92. hana::int_c<56>
  93. ));
  94. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  95. hana::at_key(map, key2),
  96. key2
  97. ));
  98. }
  99. {
  100. auto map = hana::to_map(hana::make_tuple(
  101. hana::make_pair(key1, key1),
  102. hana::make_pair(hana::int_c<56>, hana::int_c<56>),
  103. hana::make_pair(key2, key2),
  104. hana::make_pair(hana::int_c<42>, hana::int_c<42>)
  105. ));
  106. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  107. hana::at_key(map, key1),
  108. key1
  109. ));
  110. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  111. hana::at_key(map, hana::int_c<56>),
  112. hana::int_c<56>
  113. ));
  114. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  115. hana::at_key(map, key2),
  116. key2
  117. ));
  118. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  119. hana::at_key(map, hana::int_c<42>),
  120. hana::int_c<42>
  121. ));
  122. }
  123. }