map_misc.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*=============================================================================
  2. Copyright (c) 1999-2003 Jaakko Jarvi
  3. Copyright (c) 2001-2013 Joel de Guzman
  4. Copyright (c) 2006 Dan Marsden
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/fusion/container/map/map.hpp>
  9. #include <boost/fusion/container/map/convert.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/fusion/sequence/intrinsic.hpp>
  12. #include <boost/fusion/support/is_sequence.hpp>
  13. #include <boost/fusion/mpl.hpp>
  14. #include <boost/mpl/find.hpp>
  15. #include <boost/mpl/equal.hpp>
  16. #include <boost/mpl/int.hpp>
  17. #include <boost/mpl/integral_c.hpp>
  18. #include <boost/mpl/is_sequence.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <string>
  21. struct k1 {};
  22. struct k2 {};
  23. struct k3 {};
  24. struct k4 {};
  25. template <typename S1, typename S2>
  26. struct is_same
  27. {
  28. };
  29. namespace fn = boost::fusion;
  30. struct test_intrinsics1
  31. {
  32. // test at, begin, end, next, prior, advance, size, deref, etc.
  33. typedef fn::map<
  34. fn::pair<k1, int>, fn::pair<k2, float>,
  35. fn::pair<k3, bool>, fn::pair<k3, char> >
  36. sequence;
  37. typedef boost::mpl::begin<sequence>::type first;
  38. typedef boost::mpl::next<first>::type second;
  39. typedef boost::mpl::next<second>::type third;
  40. typedef boost::mpl::next<third>::type fourth;
  41. typedef boost::mpl::end<sequence>::type last;
  42. BOOST_STATIC_ASSERT((boost::is_same<
  43. boost::mpl::deref<first>::type, fn::pair<k1, int> >::value));
  44. BOOST_STATIC_ASSERT((boost::is_same<
  45. boost::mpl::deref<second>::type, fn::pair<k2, float> >::value));
  46. BOOST_STATIC_ASSERT((boost::is_same<
  47. boost::mpl::deref<third>::type, fn::pair<k3, bool> >::value));
  48. BOOST_STATIC_ASSERT((boost::is_same<
  49. boost::mpl::deref<fourth>::type, fn::pair<k3, char> >::value));
  50. BOOST_STATIC_ASSERT((boost::is_same<
  51. boost::mpl::at_c<sequence, 2>::type, fn::pair<k3, bool> >::value));
  52. BOOST_STATIC_ASSERT((boost::is_same<
  53. boost::mpl::front<sequence>::type, fn::pair<k1, int> >::value));
  54. BOOST_STATIC_ASSERT((boost::is_same<
  55. boost::mpl::deref<
  56. boost::mpl::advance_c<second, 2>::type>::type, fn::pair<k3, char> >::value));
  57. BOOST_STATIC_ASSERT((boost::mpl::size<sequence>::value == 4));
  58. BOOST_STATIC_ASSERT(!(boost::mpl::empty<sequence>::value));
  59. BOOST_STATIC_ASSERT((boost::mpl::distance<second, fourth>::value == 2));
  60. typedef boost::mpl::prior<last>::type fourth_;
  61. typedef boost::mpl::prior<fourth_>::type third_;
  62. typedef boost::mpl::prior<third_>::type second_;
  63. typedef boost::mpl::prior<second_>::type first_;
  64. BOOST_STATIC_ASSERT((boost::is_same<
  65. boost::mpl::deref<first_>::type, fn::pair<k1, int> >::value));
  66. BOOST_STATIC_ASSERT((boost::is_same<
  67. boost::mpl::deref<second_>::type, fn::pair<k2, float> >::value));
  68. BOOST_STATIC_ASSERT((boost::is_same<
  69. boost::mpl::deref<third_>::type, fn::pair<k3, bool> >::value));
  70. BOOST_STATIC_ASSERT((boost::is_same<
  71. boost::mpl::deref<fourth_>::type, fn::pair<k3, char> >::value));
  72. BOOST_STATIC_ASSERT((boost::is_same<
  73. boost::mpl::back<sequence>::type, fn::pair<k3, char> >::value));
  74. };
  75. void
  76. test()
  77. {
  78. using namespace boost::fusion;
  79. { // testing const sequences
  80. const map<pair<k1, int>, pair<k2, float> > t1(5, 3.3f);
  81. BOOST_TEST(at_c<0>(t1).second == 5);
  82. BOOST_TEST(at_c<1>(t1).second == 3.3f);
  83. }
  84. { // testing at<N> works with MPL integral constants
  85. const map<pair<k1, int>, pair<k2, char> > t1(101, 'z');
  86. BOOST_TEST(boost::fusion::at<boost::mpl::int_<0> >(t1).second == 101);
  87. BOOST_TEST(boost::fusion::at<boost::mpl::int_<1> >(t1).second == 'z');
  88. // explicitly try something other than mpl::int_
  89. BOOST_TEST((boost::fusion::at<boost::mpl::integral_c<long, 0> >(t1).second == 101));
  90. BOOST_TEST((boost::fusion::at<boost::mpl::integral_c<long, 1> >(t1).second == 'z'));
  91. }
  92. { // testing size & empty
  93. typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
  94. typedef map<> t2;
  95. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<t1>::value == 3);
  96. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<t2>::value == 0);
  97. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<t1>::value);
  98. BOOST_STATIC_ASSERT(boost::fusion::result_of::empty<t2>::value);
  99. }
  100. { // testing front & back
  101. typedef map<pair<k1, int>, pair<k2, float>, pair<k3, std::string> > tup;
  102. tup t(1, 2.2f, std::string("Kimpo"));
  103. BOOST_TEST(front(t).second == 1);
  104. BOOST_TEST(back(t).second == "Kimpo");
  105. }
  106. { // testing is_sequence
  107. typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
  108. typedef map<> t2;
  109. typedef map<pair<k1, char> > t3;
  110. BOOST_STATIC_ASSERT(traits::is_sequence<t1>::value);
  111. BOOST_STATIC_ASSERT(traits::is_sequence<t2>::value);
  112. BOOST_STATIC_ASSERT(traits::is_sequence<t3>::value);
  113. BOOST_STATIC_ASSERT(!traits::is_sequence<int>::value);
  114. BOOST_STATIC_ASSERT(!traits::is_sequence<char>::value);
  115. }
  116. { // testing mpl::is_sequence
  117. typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
  118. typedef map<> t2;
  119. typedef map<pair<k1, char> > t3;
  120. BOOST_STATIC_ASSERT(boost::mpl::is_sequence<t1>::value);
  121. BOOST_STATIC_ASSERT(boost::mpl::is_sequence<t2>::value);
  122. BOOST_STATIC_ASSERT(boost::mpl::is_sequence<t3>::value);
  123. }
  124. { // testing mpl compatibility
  125. // test an algorithm
  126. typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
  127. typedef boost::mpl::find<t1, pair<k2, float> >::type iter;
  128. typedef boost::mpl::deref<iter>::type type;
  129. BOOST_STATIC_ASSERT((boost::is_same<type, pair<k2, float> >::value));
  130. }
  131. }
  132. int
  133. main()
  134. {
  135. test();
  136. return boost::report_errors();
  137. }