std.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Boost.Assign library
  2. //
  3. // Copyright Thorsten Ottosen 2003-2004. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/assign/
  9. //
  10. #include <boost/detail/workaround.hpp>
  11. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  12. # pragma warn -8091 // suppress warning in Boost.Test
  13. # pragma warn -8057 // unused argument argc/argv in Boost.Test
  14. #endif
  15. #include <boost/assign/std.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <utility>
  18. #include <string>
  19. using std::deque;
  20. using std::list;
  21. using std::vector;
  22. using std::set;
  23. using std::multiset;
  24. using std::map;
  25. using std::multimap;
  26. using std::stack;
  27. using std::queue;
  28. using std::priority_queue;
  29. using std::string;
  30. using std::pair;
  31. using std::make_pair;
  32. using namespace boost::assign;
  33. template< typename K, typename V >
  34. inline pair<K,V> P( K k, V v )
  35. {
  36. return make_pair( k, v );
  37. }
  38. struct three
  39. {
  40. three( int, int, int ) { }
  41. three( const string&, const string&, const string& ) { }
  42. };
  43. struct four
  44. {
  45. four( int, int, int, int ) { }
  46. four( const string&, const string&, const string&, const string& ) { }
  47. };
  48. struct five
  49. {
  50. five( int, int, int, int, int ) { }
  51. five( const string&, const string&, const string&,
  52. const string&, const string& ) { }
  53. };
  54. template< class C >
  55. void test_int_sequence()
  56. {
  57. C c;
  58. BOOST_CHECK_EQUAL( c.size(), 0u );
  59. c +=1,2,3,4,5,6,7,8,9,10;
  60. BOOST_CHECK_EQUAL( c.size(), 10u );
  61. }
  62. template< class C >
  63. void test_string_sequence()
  64. {
  65. C c;
  66. BOOST_CHECK_EQUAL( c.size(), 0u );
  67. c += "1","2","3","4","5","6","7","8","9","10";
  68. BOOST_CHECK_EQUAL( c.size(), 10u );
  69. }
  70. typedef pair<string,int> two_tuple;
  71. template< class C >
  72. void test_tuple_sequence()
  73. {
  74. C c;
  75. BOOST_CHECK_EQUAL( c.size(), 0u );
  76. c += P("1",1), P("2",2), P("3",3), P("4",4), P("5",5), P("6",6),
  77. P("7",7), P("8",8), P("9",9), P("10",10);
  78. BOOST_CHECK_EQUAL( c.size(), 10u );
  79. }
  80. template< class M >
  81. void test_map()
  82. {
  83. M m;
  84. m += P( "january", 31 ), P( "february", 28 ),
  85. P( "march", 31 ), P( "april", 30 ),
  86. P( "may", 31 ), P( "june", 30 ),
  87. P( "july", 31 ), P( "august", 31 ),
  88. P( "september", 30 ), P( "october", 31 ),
  89. P( "november", 30 ), P( "december", 31 );
  90. BOOST_CHECK_EQUAL( m.size(), 12u );
  91. m.clear();
  92. insert( m )
  93. ( "january", 31 )( "february", 28 )
  94. ( "march", 31 )( "april", 30 )
  95. ( "may", 31 )( "june", 30 )
  96. ( "july", 31 )( "august", 31 )
  97. ( "september", 30 )( "october", 31 )
  98. ( "november", 30 )( "december", 31 );
  99. BOOST_CHECK_EQUAL( m.size(), 12u );
  100. }
  101. void test_tuple()
  102. {
  103. vector<three> v_three;
  104. vector<four> v_four;
  105. vector<five> v_five;
  106. push_back( v_three ) (1,2,3) ("1","2","3");
  107. push_back( v_four ) (1,2,3,4) ("1","2","3","4");
  108. push_back( v_five ) (1,2,3,4,5) ("1","2","3","4","5");
  109. BOOST_CHECK_EQUAL( v_three.size(), 2u );
  110. BOOST_CHECK_EQUAL( v_four.size(), 2u );
  111. BOOST_CHECK_EQUAL( v_five.size(), 2u );
  112. }
  113. void check_std()
  114. {
  115. test_int_sequence< deque<int> >();
  116. test_int_sequence< list<int> >();
  117. test_int_sequence< vector<int> >();
  118. test_int_sequence< set<int> >();
  119. test_int_sequence< multiset<int> >();
  120. test_int_sequence< stack<int> >();
  121. test_int_sequence< queue<int> >();
  122. test_int_sequence< priority_queue<int> >();
  123. test_string_sequence< deque<string> >();
  124. test_string_sequence< list<string> >();
  125. test_string_sequence< vector<string> >();
  126. test_string_sequence< set<string> >();
  127. test_string_sequence< multiset<string> >();
  128. test_string_sequence< stack<string> >();
  129. test_string_sequence< queue<string> >();
  130. test_string_sequence< priority_queue<string> >();
  131. test_tuple_sequence< deque<two_tuple> >();
  132. test_tuple_sequence< list<two_tuple> >();
  133. test_tuple_sequence< vector<two_tuple> >();
  134. test_tuple_sequence< set<two_tuple> >();
  135. test_tuple_sequence< multiset<two_tuple> >();
  136. test_tuple_sequence< stack<two_tuple> >();
  137. test_tuple_sequence< queue<two_tuple> >();
  138. test_tuple_sequence< priority_queue<two_tuple> >();
  139. test_tuple();
  140. deque<int> di;
  141. push_back( di )( 1 );
  142. push_front( di )( 2 );
  143. BOOST_CHECK_EQUAL( di[0], 2 );
  144. BOOST_CHECK_EQUAL( di[1], 1 );
  145. list<int> li;
  146. push_back( li )( 2 );
  147. push_front( li )( 1 );
  148. BOOST_CHECK_EQUAL( li.front(), 1 );
  149. BOOST_CHECK_EQUAL( li.back(), 2 );
  150. vector<int> vi;
  151. push_back( vi ) = 2,3;
  152. BOOST_CHECK_EQUAL( vi[0], 2 );
  153. BOOST_CHECK_EQUAL( vi[1], 3 );
  154. set<int> si;
  155. insert( si )( 4 );
  156. BOOST_CHECK_EQUAL( *si.find( 4 ), 4 );
  157. multiset<int> msi;
  158. insert( msi )( 5 );
  159. BOOST_CHECK_EQUAL( *msi.find( 5 ), 5 );
  160. stack<int> sti;
  161. push( sti )( 6 );
  162. BOOST_CHECK_EQUAL( sti.top(), 6 );
  163. queue<int> qi;
  164. push( qi )( 7 );
  165. BOOST_CHECK_EQUAL( qi.back(), 7 );
  166. priority_queue<int> pqi;
  167. push( pqi )( 8 );
  168. BOOST_CHECK_EQUAL( pqi.top(), 8 );
  169. test_map< map<string,int> >();
  170. test_map< multimap<string,int> >();
  171. }
  172. #include <boost/test/unit_test.hpp>
  173. using boost::unit_test::test_suite;
  174. test_suite* init_unit_test_suite( int argc, char* argv[] )
  175. {
  176. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  177. test->add( BOOST_TEST_CASE( &check_std ) );
  178. return test;
  179. }