map.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright Aleksey Gurtovoy 2003-2004
  2. // Copyright David Abrahams 2003-2004
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/mpl for documentation.
  9. // $Id$
  10. // $Date$
  11. // $Revision$
  12. #include <boost/mpl/map.hpp>
  13. #include <boost/mpl/insert.hpp>
  14. #include <boost/mpl/erase_key.hpp>
  15. #include <boost/mpl/erase_key.hpp>
  16. #include <boost/mpl/contains.hpp>
  17. #include <boost/mpl/at.hpp>
  18. #include <boost/mpl/clear.hpp>
  19. #include <boost/mpl/has_key.hpp>
  20. #include <boost/mpl/order.hpp>
  21. #include <boost/mpl/size.hpp>
  22. #include <boost/mpl/empty.hpp>
  23. #include <boost/mpl/begin_end.hpp>
  24. #include <boost/type_traits/is_same.hpp>
  25. #include <boost/mpl/aux_/test.hpp>
  26. MPL_TEST_CASE()
  27. {
  28. typedef map2<
  29. mpl::pair<int,unsigned>
  30. , mpl::pair<char,unsigned char>
  31. > m_;
  32. typedef erase_key<m_,char>::type m;
  33. MPL_ASSERT_RELATION( size<m>::type::value, ==, 1 );
  34. MPL_ASSERT_NOT(( empty<m> ));
  35. MPL_ASSERT(( is_same< clear<m>::type,map0<> > ));
  36. MPL_ASSERT(( is_same< at<m,int>::type,unsigned > ));
  37. MPL_ASSERT(( is_same< at<m,char>::type,void_ > ));
  38. MPL_ASSERT(( contains< m,mpl::pair<int,unsigned> > ));
  39. MPL_ASSERT_NOT(( contains< m,mpl::pair<int,int> > ));
  40. MPL_ASSERT_NOT(( contains< m,mpl::pair<char,unsigned char> > ));
  41. MPL_ASSERT_NOT(( has_key<m,char>::type ));
  42. MPL_ASSERT(( has_key<m,int>::type ));
  43. MPL_ASSERT_NOT(( is_same< order<m,int>::type, void_ > ));
  44. MPL_ASSERT(( is_same< order<m,char>::type,void_ > ));
  45. typedef begin<m>::type first;
  46. typedef end<m>::type last;
  47. MPL_ASSERT(( is_same< deref<first>::type,mpl::pair<int,unsigned> > ));
  48. MPL_ASSERT(( is_same< next<first>::type,last > ));
  49. typedef insert<m,mpl::pair<char,long> >::type m2;
  50. MPL_ASSERT_RELATION( size<m2>::type::value, ==, 2 );
  51. MPL_ASSERT_NOT(( empty<m2>::type ));
  52. MPL_ASSERT(( is_same< clear<m2>::type,map0<> > ));
  53. MPL_ASSERT(( is_same< at<m2,int>::type,unsigned > ));
  54. MPL_ASSERT(( is_same< at<m2,char>::type,long > ));
  55. MPL_ASSERT(( contains< m2,mpl::pair<int,unsigned> > ));
  56. MPL_ASSERT_NOT(( contains< m2,mpl::pair<int,int> > ));
  57. MPL_ASSERT_NOT(( contains< m2,mpl::pair<char,unsigned char> > ));
  58. MPL_ASSERT(( contains< m2,mpl::pair<char,long> > ));
  59. MPL_ASSERT(( has_key<m2,char>::type ));
  60. MPL_ASSERT_NOT(( has_key<m2,long>::type ));
  61. MPL_ASSERT_NOT(( is_same< order<m2,int>::type, void_ > ));
  62. MPL_ASSERT_NOT(( is_same< order<m2,char>::type, void_ > ));
  63. MPL_ASSERT_NOT(( is_same< order<m2,char>::type, order<m2,int>::type > ));
  64. typedef begin<m2>::type first2;
  65. typedef end<m2>::type last2;
  66. MPL_ASSERT(( is_same<deref<first2>::type,mpl::pair<int,unsigned> > ));
  67. typedef next<first2>::type iter;
  68. MPL_ASSERT(( is_same<deref<iter>::type,mpl::pair<char,long> > ));
  69. MPL_ASSERT(( is_same< next<iter>::type,last2 > ));
  70. typedef insert<m2,mpl::pair<int,unsigned> >::type s2_1;
  71. MPL_ASSERT(( is_same<m2,s2_1> ));
  72. typedef insert<m2,mpl::pair<long,unsigned> >::type m3;
  73. MPL_ASSERT_RELATION( size<m3>::type::value, ==, 3 );
  74. MPL_ASSERT(( has_key<m3,long>::type ));
  75. MPL_ASSERT(( has_key<m3,int>::type ));
  76. MPL_ASSERT(( has_key<m3,char>::type ));
  77. MPL_ASSERT(( contains< m3,mpl::pair<long,unsigned> > ));
  78. MPL_ASSERT(( contains< m3,mpl::pair<int,unsigned> > ));
  79. typedef insert<m,mpl::pair<char,long> >::type m1;
  80. MPL_ASSERT_RELATION( size<m1>::type::value, ==, 2 );
  81. MPL_ASSERT(( is_same< at<m1,int>::type,unsigned > ));
  82. MPL_ASSERT(( is_same< at<m1,char>::type,long > ));
  83. MPL_ASSERT(( contains< m1,mpl::pair<int,unsigned> > ));
  84. MPL_ASSERT_NOT(( contains< m1,mpl::pair<int,int> > ));
  85. MPL_ASSERT_NOT(( contains< m1,mpl::pair<char,unsigned char> > ));
  86. MPL_ASSERT(( contains< m1,mpl::pair<char,long> > ));
  87. MPL_ASSERT(( is_same< m1,m2 > ));
  88. typedef erase_key<m1,char>::type m_1;
  89. MPL_ASSERT(( is_same<m,m_1> ));
  90. MPL_ASSERT_RELATION( size<m_1>::type::value, ==, 1 );
  91. MPL_ASSERT(( is_same< at<m_1,char>::type,void_ > ));
  92. MPL_ASSERT(( is_same< at<m_1,int>::type,unsigned > ));
  93. typedef erase_key<m3,char>::type m2_1;
  94. MPL_ASSERT_RELATION( size<m2_1>::type::value, ==, 2 );
  95. MPL_ASSERT(( is_same< at<m2_1,char>::type,void_ > ));
  96. MPL_ASSERT(( is_same< at<m2_1,int>::type,unsigned > ));
  97. MPL_ASSERT(( is_same< at<m2_1,long>::type,unsigned > ));
  98. }
  99. MPL_TEST_CASE()
  100. {
  101. typedef map0<> m;
  102. MPL_ASSERT_RELATION( size<m>::type::value, ==, 0 );
  103. MPL_ASSERT(( empty<m>::type ));
  104. MPL_ASSERT(( is_same< clear<m>::type,map0<> > ));
  105. MPL_ASSERT(( is_same< at<m,char>::type,void_ > ));
  106. MPL_ASSERT_NOT(( has_key<m,char>::type ));
  107. MPL_ASSERT_NOT(( has_key<m,int>::type ));
  108. MPL_ASSERT_NOT(( has_key<m,UDT>::type ));
  109. MPL_ASSERT_NOT(( has_key<m,incomplete>::type ));
  110. MPL_ASSERT_NOT(( has_key<m,char const>::type ));
  111. MPL_ASSERT_NOT(( has_key<m,int const>::type ));
  112. MPL_ASSERT_NOT(( has_key<m,UDT const>::type ));
  113. MPL_ASSERT_NOT(( has_key<m,incomplete const>::type ));
  114. MPL_ASSERT_NOT(( has_key<m,int*>::type ));
  115. MPL_ASSERT_NOT(( has_key<m,UDT*>::type ));
  116. MPL_ASSERT_NOT(( has_key<m,incomplete*>::type ));
  117. MPL_ASSERT_NOT(( has_key<m,int&>::type ));
  118. MPL_ASSERT_NOT(( has_key<m,UDT&>::type ));
  119. MPL_ASSERT_NOT(( has_key<m,incomplete&>::type ));
  120. typedef insert<m,mpl::pair<char,int> >::type m1;
  121. MPL_ASSERT_RELATION( size<m1>::type::value, ==, 1 );
  122. MPL_ASSERT(( is_same< at<m1,char>::type,int > ));
  123. typedef erase_key<m,char>::type m0_1;
  124. MPL_ASSERT_RELATION( size<m0_1>::type::value, ==, 0 );
  125. MPL_ASSERT(( is_same< at<m0_1,char>::type,void_ > ));
  126. }
  127. // Use a template for testing so that GCC will show us the actual types involved
  128. template <class M>
  129. void test()
  130. {
  131. MPL_ASSERT_RELATION( size<M>::value, ==, 3 );
  132. typedef typename end<M>::type not_found;
  133. BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<M,mpl::pair<int,int*> >::type,not_found> ));
  134. BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<M,mpl::pair<long,long*> >::type,not_found> ));
  135. BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<M,mpl::pair<char,char*> >::type,not_found> ));
  136. BOOST_MPL_ASSERT(( is_same<BOOST_DEDUCED_TYPENAME find<M,int>::type,not_found> ));
  137. };
  138. MPL_TEST_CASE()
  139. {
  140. typedef map< mpl::pair<int,int*> > map_of_1_pair;
  141. typedef begin<map_of_1_pair>::type iter_to_1_pair;
  142. BOOST_MPL_ASSERT((
  143. is_same<
  144. deref<iter_to_1_pair>::type
  145. , mpl::pair<int,int*>
  146. >
  147. ));
  148. typedef map<
  149. mpl::pair<int,int*>
  150. , mpl::pair<long,long*>
  151. , mpl::pair<char,char*>
  152. > mymap;
  153. test<mymap>();
  154. test<mymap::type>();
  155. }
  156. MPL_TEST_CASE()
  157. {
  158. typedef mpl::erase_key<
  159. mpl::map<
  160. mpl::pair<char, double>
  161. , mpl::pair<int, float>
  162. >
  163. , char
  164. >::type int_to_float_map;
  165. typedef mpl::insert<
  166. int_to_float_map
  167. , mpl::pair<char, long>
  168. >::type with_char_too;
  169. BOOST_MPL_ASSERT((
  170. boost::is_same<
  171. mpl::at<with_char_too, char>::type
  172. , long
  173. >
  174. ));
  175. }
  176. MPL_TEST_CASE()
  177. {
  178. typedef insert< map<>, pair<int,int> >::type little_map;
  179. MPL_ASSERT_RELATION(size<little_map>::value, ==, 1);
  180. MPL_ASSERT_RELATION(size<little_map::type>::value, ==, 1);
  181. }
  182. MPL_TEST_CASE()
  183. {
  184. typedef erase_key< map< pair<float,float>, pair<int,int> >, float >::type little_map;
  185. MPL_ASSERT_RELATION(size<little_map>::value, ==, 1);
  186. MPL_ASSERT_RELATION(size<little_map::type>::value, ==, 1);
  187. }