to.cpp 2.9 KB

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