test_bimap_sequenced.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. #include <boost/config.hpp>
  17. #define BOOST_BIMAP_DISABLE_SERIALIZATION
  18. // Boost.Test
  19. #include <boost/test/minimal.hpp>
  20. // std
  21. #include <set>
  22. #include <map>
  23. #include <algorithm>
  24. #include <string>
  25. #include <functional>
  26. // Set type specifications
  27. #include <boost/bimap/list_of.hpp>
  28. #include <boost/bimap/vector_of.hpp>
  29. // bimap container
  30. #include <boost/bimap/bimap.hpp>
  31. #include <boost/bimap/support/lambda.hpp>
  32. #include <libs/bimap/test/test_bimap.hpp>
  33. struct left_tag {};
  34. struct right_tag {};
  35. template< class Container, class Data >
  36. void test_list_operations(Container & b, Container& c, const Data & d)
  37. {
  38. c.clear() ;
  39. c.assign(d.begin(),d.end());
  40. BOOST_CHECK( std::equal( c.begin(), c.end(), d.begin() ) );
  41. c.reverse();
  42. BOOST_CHECK( std::equal( c.begin(), c.end(), d.rbegin() ) );
  43. c.sort();
  44. BOOST_CHECK( std::equal( c.begin(), c.end(), d.begin() ) );
  45. c.push_front( *d.begin() );
  46. BOOST_CHECK( c.size() == d.size()+1 );
  47. c.unique();
  48. BOOST_CHECK( c.size() == d.size() );
  49. c.relocate( c.begin(), ++c.begin() );
  50. c.relocate( c.end(), c.begin(), ++c.begin() );
  51. b.clear();
  52. c.clear();
  53. c.assign(d.begin(),d.end());
  54. b.splice(b.begin(),c);
  55. BOOST_CHECK( c.size() == 0 );
  56. BOOST_CHECK( b.size() == d.size() );
  57. c.splice(c.begin(),b,++b.begin());
  58. BOOST_CHECK( c.size() == 1 );
  59. c.splice(c.begin(),b,b.begin(),b.end());
  60. BOOST_CHECK( b.size() == 0 );
  61. b.assign(d.begin(),d.end());
  62. c.assign(d.begin(),d.end());
  63. b.sort();
  64. c.sort();
  65. b.merge(c);
  66. BOOST_CHECK( b.size() == 2*d.size() );
  67. b.unique();
  68. }
  69. void test_bimap()
  70. {
  71. using namespace boost::bimaps;
  72. typedef std::map<std::string,long> left_data_type;
  73. left_data_type left_data;
  74. left_data.insert( left_data_type::value_type("1",1) );
  75. left_data.insert( left_data_type::value_type("2",2) );
  76. left_data.insert( left_data_type::value_type("3",3) );
  77. left_data.insert( left_data_type::value_type("4",4) );
  78. typedef std::map<long,std::string> right_data_type;
  79. right_data_type right_data;
  80. right_data.insert( right_data_type::value_type(1,"1") );
  81. right_data.insert( right_data_type::value_type(2,"2") );
  82. right_data.insert( right_data_type::value_type(3,"3") );
  83. right_data.insert( right_data_type::value_type(4,"4") );
  84. //--------------------------------------------------------------------
  85. {
  86. typedef bimap<
  87. list_of< std::string >, vector_of< long >
  88. > bm_type;
  89. std::set< bm_type::value_type > data;
  90. data.insert( bm_type::value_type("1",1) );
  91. data.insert( bm_type::value_type("2",2) );
  92. data.insert( bm_type::value_type("3",3) );
  93. data.insert( bm_type::value_type("4",4) );
  94. bm_type b;
  95. test_bimap_init_copy_swap<bm_type>(data) ;
  96. test_sequence_container(b,data);
  97. test_sequence_container(b.left , left_data);
  98. test_vector_container(b.right,right_data);
  99. test_mapped_container(b.left );
  100. test_mapped_container(b.right);
  101. bm_type c;
  102. test_list_operations(b,c,data) ;
  103. test_list_operations(b.left,c.left,left_data) ;
  104. test_list_operations(b.right,c.right,right_data) ;
  105. c.assign(data.begin(),data.end());
  106. b.assign(data.begin(),data.end());
  107. c.remove_if(_key<=bm_type::value_type("1",1));
  108. c.sort(std::less<bm_type::value_type>());
  109. b.sort(std::less<bm_type::value_type>());
  110. c.merge(b,std::less<bm_type::value_type>());
  111. c.unique(std::equal_to<bm_type::value_type>());
  112. c.assign(data.begin(),data.end());
  113. b.assign(data.begin(),data.end());
  114. c.left.remove_if(_key<="1");
  115. c.left.sort(std::less<std::string>());
  116. b.left.sort(std::less<std::string>());
  117. c.left.merge(b.left,std::less<std::string>());
  118. c.left.unique(std::equal_to<std::string>());
  119. c.assign(data.begin(),data.end());
  120. b.assign(data.begin(),data.end());
  121. c.right.remove_if(_key<=1);
  122. c.right.sort(std::less<long>());
  123. b.right.sort(std::less<long>());
  124. c.right.merge(b.right,std::less<long>());
  125. c.right.unique(std::equal_to<long>());
  126. c.assign(data.begin(),data.end());
  127. c.right[0].first = -1;
  128. c.right.at(0).second = "[1]";
  129. }
  130. //--------------------------------------------------------------------
  131. //--------------------------------------------------------------------
  132. {
  133. typedef bimap
  134. <
  135. vector_of<std::string>, list_of<long>,
  136. vector_of_relation
  137. > bm_type;
  138. std::set< bm_type::value_type > data;
  139. data.insert( bm_type::value_type("1",1) );
  140. data.insert( bm_type::value_type("2",2) );
  141. data.insert( bm_type::value_type("3",3) );
  142. data.insert( bm_type::value_type("4",4) );
  143. bm_type b;
  144. test_bimap_init_copy_swap<bm_type>(data) ;
  145. test_vector_container(b,data) ;
  146. bm_type c;
  147. test_list_operations(b,c,data) ;
  148. test_list_operations(b.left,c.left,left_data) ;
  149. test_list_operations(b.right,c.right,right_data) ;
  150. c.assign(data.begin(),data.end());
  151. b.assign(data.begin(),data.end());
  152. c.remove_if(_key<=bm_type::value_type("1",1));
  153. c.sort(std::less<bm_type::value_type>());
  154. b.sort(std::less<bm_type::value_type>());
  155. c.merge(b,std::less<bm_type::value_type>());
  156. c.unique(std::equal_to<bm_type::value_type>());
  157. c.assign(data.begin(),data.end());
  158. b.assign(data.begin(),data.end());
  159. c.left.remove_if(_key<="1");
  160. c.left.sort(std::less<std::string>());
  161. b.left.sort(std::less<std::string>());
  162. c.left.merge(b.left,std::less<std::string>());
  163. c.left.unique(std::equal_to<std::string>());
  164. c.assign(data.begin(),data.end());
  165. b.assign(data.begin(),data.end());
  166. c.right.remove_if(_key<=1);
  167. c.right.sort(std::less<long>());
  168. b.right.sort(std::less<long>());
  169. c.right.merge(b.right,std::less<long>());
  170. c.right.unique(std::equal_to<long>());
  171. c.assign(data.begin(),data.end());
  172. c[0].left = "(1)";
  173. c.at(0).right = -1;
  174. c.left[0].first = "[1]";
  175. c.left.at(0).second = -1;
  176. }
  177. //--------------------------------------------------------------------
  178. //--------------------------------------------------------------------
  179. {
  180. typedef bimap
  181. <
  182. vector_of<std::string>, list_of<long>,
  183. list_of_relation
  184. > bm_type;
  185. std::set< bm_type::value_type > data;
  186. data.insert( bm_type::value_type("1",1) );
  187. data.insert( bm_type::value_type("2",2) );
  188. data.insert( bm_type::value_type("3",3) );
  189. data.insert( bm_type::value_type("4",4) );
  190. bm_type b;
  191. test_bimap_init_copy_swap<bm_type>(data) ;
  192. test_sequence_container(b,data) ;
  193. bm_type c;
  194. test_list_operations(b,c,data) ;
  195. test_list_operations(b.left,c.left,left_data) ;
  196. test_list_operations(b.right,c.right,right_data) ;
  197. c.assign(data.begin(),data.end());
  198. b.assign(data.begin(),data.end());
  199. c.remove_if(_key<=bm_type::value_type("1",1));
  200. c.sort(std::less<bm_type::value_type>());
  201. b.sort(std::less<bm_type::value_type>());
  202. c.merge(b,std::less<bm_type::value_type>());
  203. c.unique(std::equal_to<bm_type::value_type>());
  204. c.assign(data.begin(),data.end());
  205. c.left[0].first = "[1]";
  206. c.left.at(0).second = -1;
  207. }
  208. //--------------------------------------------------------------------
  209. }
  210. int test_main( int, char* [] )
  211. {
  212. test_bimap();
  213. return 0;
  214. }