set.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright Aleksey Gurtovoy 2003-2007
  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/set.hpp>
  13. #include <boost/mpl/contains.hpp>
  14. #include <boost/mpl/find.hpp>
  15. #include <boost/mpl/deref.hpp>
  16. #include <boost/mpl/next.hpp>
  17. #include <boost/mpl/insert.hpp>
  18. #include <boost/mpl/erase.hpp>
  19. #include <boost/mpl/erase_key.hpp>
  20. #include <boost/mpl/at.hpp>
  21. #include <boost/mpl/clear.hpp>
  22. #include <boost/mpl/has_key.hpp>
  23. #include <boost/mpl/order.hpp>
  24. #include <boost/mpl/size.hpp>
  25. #include <boost/mpl/distance.hpp>
  26. #include <boost/mpl/empty.hpp>
  27. #include <boost/mpl/begin_end.hpp>
  28. #include <boost/mpl/aux_/test.hpp>
  29. // Use templates for testing so that GCC will show us the actual types involved
  30. template< typename s >
  31. void empty_set_test()
  32. {
  33. MPL_ASSERT_RELATION( size<s>::value, ==, 0 );
  34. MPL_ASSERT(( empty<s> ));
  35. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
  36. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, void_ > ));
  37. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, void_ > ));
  38. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,long>::type, void_ > ));
  39. MPL_ASSERT_NOT(( has_key<s,int> ));
  40. MPL_ASSERT_NOT(( has_key<s,char> ));
  41. MPL_ASSERT_NOT(( has_key<s,long> ));
  42. typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
  43. typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
  44. typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
  45. MPL_ASSERT(( is_same< o1, void_ > ));
  46. MPL_ASSERT(( is_same< o2, void_ > ));
  47. MPL_ASSERT(( is_same< o3, void_ > ));
  48. typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
  49. typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
  50. MPL_ASSERT(( is_same<first, last> ));
  51. MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 0 );
  52. }
  53. template< typename s >
  54. void int_set_test()
  55. {
  56. MPL_ASSERT_RELATION( size<s>::value, ==, 1 );
  57. MPL_ASSERT_NOT(( empty<s> ));
  58. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
  59. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, int > ));
  60. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, void_ > ));
  61. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,long>::type, void_ > ));
  62. MPL_ASSERT(( has_key<s,int> ));
  63. MPL_ASSERT_NOT(( has_key<s,char> ));
  64. MPL_ASSERT_NOT(( has_key<s,long> ));
  65. typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
  66. typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
  67. typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
  68. MPL_ASSERT_NOT(( is_same< o1, void_ > ));
  69. MPL_ASSERT(( is_same< o2, void_ > ));
  70. MPL_ASSERT(( is_same< o3, void_ > ));
  71. typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
  72. typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
  73. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME deref<first>::type, int > ));
  74. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME next<first>::type, last > ));
  75. MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 1 );
  76. MPL_ASSERT(( contains< s, int > ));
  77. }
  78. template< typename s >
  79. void int_char_set_test()
  80. {
  81. MPL_ASSERT_RELATION( size<s>::value, ==, 2 );
  82. MPL_ASSERT_NOT(( empty<s> ));
  83. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
  84. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, int > ));
  85. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, char > ));
  86. MPL_ASSERT(( has_key<s,char> ));
  87. MPL_ASSERT_NOT(( has_key<s,long> ));
  88. typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
  89. typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
  90. typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
  91. MPL_ASSERT_NOT(( is_same< o1, void_ > ));
  92. MPL_ASSERT_NOT(( is_same< o2, void_ > ));
  93. MPL_ASSERT(( is_same< o3, void_ > ));
  94. MPL_ASSERT_NOT(( is_same< o1, o2 > ));
  95. typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
  96. typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
  97. MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 2 );
  98. MPL_ASSERT(( contains< s, int > ));
  99. MPL_ASSERT(( contains< s, char > ));
  100. }
  101. template< typename s >
  102. void int_char_long_set_test()
  103. {
  104. MPL_ASSERT_RELATION( size<s>::value, ==, 3 );
  105. MPL_ASSERT_NOT(( empty<s> ));
  106. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
  107. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, int > ));
  108. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, char > ));
  109. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,long>::type, long > ));
  110. MPL_ASSERT(( has_key<s,long> ));
  111. MPL_ASSERT(( has_key<s,int> ));
  112. MPL_ASSERT(( has_key<s,char> ));
  113. typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
  114. typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
  115. typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
  116. MPL_ASSERT_NOT(( is_same< o1, void_ > ));
  117. MPL_ASSERT_NOT(( is_same< o2, void_ > ));
  118. MPL_ASSERT_NOT(( is_same< o3, void_ > ));
  119. MPL_ASSERT_NOT(( is_same< o1, o2 > ));
  120. MPL_ASSERT_NOT(( is_same< o1, o3 > ));
  121. MPL_ASSERT_NOT(( is_same< o2, o3 > ));
  122. typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
  123. typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
  124. MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 3 );
  125. MPL_ASSERT(( contains< s, int > ));
  126. MPL_ASSERT(( contains< s, char > ));
  127. MPL_ASSERT(( contains< s, long > ));
  128. }
  129. template< typename S0, typename S1, typename S2, typename S3 >
  130. void basic_set_test()
  131. {
  132. empty_set_test<S0>();
  133. empty_set_test< BOOST_DEDUCED_TYPENAME erase_key<S1,int>::type >();
  134. empty_set_test< BOOST_DEDUCED_TYPENAME erase_key<
  135. BOOST_DEDUCED_TYPENAME erase_key<S2,char>::type
  136. , int
  137. >::type >();
  138. empty_set_test< BOOST_DEDUCED_TYPENAME erase_key<
  139. BOOST_DEDUCED_TYPENAME erase_key<
  140. BOOST_DEDUCED_TYPENAME erase_key<S3,char>::type
  141. , long
  142. >::type
  143. , int
  144. >::type >();
  145. int_set_test<S1>();
  146. int_set_test< BOOST_DEDUCED_TYPENAME insert<S0,int>::type >();
  147. int_set_test< BOOST_DEDUCED_TYPENAME erase_key<S2,char>::type >();
  148. int_set_test< BOOST_DEDUCED_TYPENAME erase_key<
  149. BOOST_DEDUCED_TYPENAME erase_key<S3,char>::type
  150. , long
  151. >::type >();
  152. int_char_set_test<S2>();
  153. int_char_set_test< BOOST_DEDUCED_TYPENAME insert<
  154. BOOST_DEDUCED_TYPENAME insert<S0,char>::type
  155. , int
  156. >::type >();
  157. int_char_set_test< BOOST_DEDUCED_TYPENAME insert<S1,char>::type >();
  158. int_char_set_test< BOOST_DEDUCED_TYPENAME erase_key<S3,long>::type >();
  159. int_char_long_set_test<S3>();
  160. int_char_long_set_test< BOOST_DEDUCED_TYPENAME insert<
  161. BOOST_DEDUCED_TYPENAME insert<
  162. BOOST_DEDUCED_TYPENAME insert<S0,char>::type
  163. , long
  164. >::type
  165. , int
  166. >::type >();
  167. int_char_long_set_test< BOOST_DEDUCED_TYPENAME insert<
  168. BOOST_DEDUCED_TYPENAME insert<S1,long>::type
  169. , char
  170. >::type >();
  171. int_char_long_set_test< BOOST_DEDUCED_TYPENAME insert<S2,long>::type >();
  172. }
  173. template< typename S1, typename S2 >
  174. void numbered_vs_variadic_set_test()
  175. {
  176. MPL_ASSERT(( is_same< S1, BOOST_DEDUCED_TYPENAME S1::type > ));
  177. MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME S2::type, S1 > ));
  178. }
  179. MPL_TEST_CASE()
  180. {
  181. typedef mpl::set0<> s01;
  182. typedef mpl::set<> s02;
  183. typedef mpl::set1<int> s11;
  184. typedef mpl::set<int> s12;
  185. typedef mpl::set2<int,char> s21;
  186. typedef mpl::set<int,char> s22;
  187. typedef mpl::set<char,int> s23;
  188. typedef mpl::set3<int,char,long> s31;
  189. typedef mpl::set<int,char,long> s32;
  190. typedef mpl::set<int,long,char> s33;
  191. typedef mpl::set<long,char,int> s34;
  192. numbered_vs_variadic_set_test<s01,s02>();
  193. numbered_vs_variadic_set_test<s11,s12>();
  194. numbered_vs_variadic_set_test<s21,s22>();
  195. numbered_vs_variadic_set_test<s31,s32>();
  196. basic_set_test<s01,s11,s21,s31>();
  197. basic_set_test<s02,s12,s22,s32>();
  198. basic_set_test<s01,s11,s23,s31>();
  199. basic_set_test<s01,s11,s23,s33>();
  200. basic_set_test<s01,s11,s23,s34>();
  201. }
  202. template< typename s >
  203. void empty_set_types_variety_test()
  204. {
  205. MPL_ASSERT_NOT(( has_key<s,char> ));
  206. MPL_ASSERT_NOT(( has_key<s,int> ));
  207. MPL_ASSERT_NOT(( has_key<s,UDT> ));
  208. MPL_ASSERT_NOT(( has_key<s,incomplete> ));
  209. MPL_ASSERT_NOT(( has_key<s,char const> ));
  210. MPL_ASSERT_NOT(( has_key<s,int const> ));
  211. MPL_ASSERT_NOT(( has_key<s,UDT const> ));
  212. MPL_ASSERT_NOT(( has_key<s,incomplete const> ));
  213. MPL_ASSERT_NOT(( has_key<s,int*> ));
  214. MPL_ASSERT_NOT(( has_key<s,UDT*> ));
  215. MPL_ASSERT_NOT(( has_key<s,incomplete*> ));
  216. MPL_ASSERT_NOT(( has_key<s,int&> ));
  217. MPL_ASSERT_NOT(( has_key<s,UDT&> ));
  218. MPL_ASSERT_NOT(( has_key<s,incomplete&> ));
  219. }
  220. template< typename s >
  221. void set_types_variety_test()
  222. {
  223. MPL_ASSERT_RELATION( size<s>::value, ==, 8 );
  224. MPL_ASSERT(( has_key<s,char> ));
  225. MPL_ASSERT(( has_key<s,int const> ));
  226. MPL_ASSERT(( has_key<s,long*> ));
  227. MPL_ASSERT(( has_key<s,UDT* const> ));
  228. MPL_ASSERT(( has_key<s,incomplete> ));
  229. MPL_ASSERT(( has_key<s,abstract> ));
  230. MPL_ASSERT(( has_key<s,incomplete volatile&> ));
  231. MPL_ASSERT(( has_key<s,abstract const&> ));
  232. MPL_ASSERT_NOT(( has_key<s,char const> ));
  233. MPL_ASSERT_NOT(( has_key<s,int> ));
  234. MPL_ASSERT_NOT(( has_key<s,long* const> ));
  235. MPL_ASSERT_NOT(( has_key<s,UDT*> ));
  236. MPL_ASSERT_NOT(( has_key<s,incomplete const> ));
  237. MPL_ASSERT_NOT(( has_key<s,abstract volatile> ));
  238. MPL_ASSERT_NOT(( has_key<s,incomplete&> ));
  239. MPL_ASSERT_NOT(( has_key<s,abstract&> ));
  240. }
  241. MPL_TEST_CASE()
  242. {
  243. empty_set_types_variety_test< set<> >();
  244. empty_set_types_variety_test< set<>::type >();
  245. typedef set<
  246. char,int const,long*,UDT* const,incomplete,abstract
  247. , incomplete volatile&,abstract const&
  248. > s;
  249. set_types_variety_test<s>();
  250. set_types_variety_test<s::type>();
  251. }
  252. template <class S>
  253. void find_test()
  254. {
  255. MPL_ASSERT_RELATION( size<S>::value, ==, 3 );
  256. typedef typename end<S>::type not_found;
  257. BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<S,int>::type,not_found> ));
  258. BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<S,long>::type,not_found> ));
  259. BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<S,char>::type,not_found> ));
  260. BOOST_MPL_ASSERT(( is_same<BOOST_DEDUCED_TYPENAME find<S,char*>::type,not_found> ));
  261. }
  262. MPL_TEST_CASE()
  263. {
  264. typedef mpl::set<int,long,char> s;
  265. find_test<s>();
  266. find_test<s::type>();
  267. }
  268. MPL_TEST_CASE()
  269. {
  270. typedef insert< set<>, int >::type little_set;
  271. MPL_ASSERT_RELATION(size<little_set>::value, ==, 1);
  272. MPL_ASSERT_RELATION(size<little_set::type>::value, ==, 1);
  273. }
  274. MPL_TEST_CASE()
  275. {
  276. typedef erase_key< set< float, int >, float >::type little_set;
  277. MPL_ASSERT_RELATION(size<little_set>::value, ==, 1);
  278. MPL_ASSERT_RELATION(size<little_set::type>::value, ==, 1);
  279. }