list_of.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/list_of.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <boost/array.hpp>
  18. #include <algorithm>
  19. #include <vector>
  20. #include <list>
  21. #include <deque>
  22. #include <set>
  23. #include <map>
  24. #include <stack>
  25. #include <string>
  26. #include <cstdlib>
  27. #include <complex>
  28. struct nothing
  29. {
  30. template< class T >
  31. void operator()( T )
  32. { }
  33. };
  34. template< class Range >
  35. void for_each( const Range& r )
  36. {
  37. std::for_each( r.begin(), r.end(), nothing() );
  38. }
  39. namespace ba = boost::assign;
  40. template< class C >
  41. void test_sequence_list_of_string()
  42. {
  43. #if BOOST_WORKAROUND(BOOST_MSVC, <=1300)
  44. const C c = ba::list_of( "foo" )( "bar" ).to_container( c );
  45. #else
  46. const C c = ba::list_of( "foo" )( "bar" );
  47. #endif
  48. BOOST_CHECK_EQUAL( c.size(), 2u );
  49. }
  50. struct parameter_list
  51. {
  52. int val;
  53. template< class T >
  54. parameter_list( T )
  55. : val(0)
  56. { }
  57. template< class T >
  58. parameter_list( const T&, int )
  59. : val(1)
  60. { }
  61. };
  62. template< class C >
  63. void test_sequence_list_of_int()
  64. {
  65. using namespace std;
  66. #if BOOST_WORKAROUND(BOOST_MSVC, <=1300)
  67. const C c = ba::list_of<int>(1)(2)(3)(4).to_container( c );
  68. const C c2 = ba::list_of(1)(2)(3)(4).to_container( c2 );
  69. BOOST_CHECK_EQUAL( c.size(), 4u );
  70. BOOST_CHECK_EQUAL( c2.size(), 4u );
  71. C c3 = ba::list_of(1).repeat( 1, 2 )(3).to_container( c3 );
  72. BOOST_CHECK_EQUAL( c3.size(), 3u );
  73. c3 = ba::list_of(1).repeat_fun( 10, &rand )(2)(3).to_container( c3 );
  74. BOOST_CHECK_EQUAL( c3.size(), 13u );
  75. #else
  76. const C c = ba::list_of<int>(1)(2)(3)(4);
  77. const C c2 = ba::list_of(1)(2)(3)(4);
  78. BOOST_CHECK_EQUAL( c.size(), 4u );
  79. BOOST_CHECK_EQUAL( c2.size(), 4u );
  80. C c3 = ba::list_of(1).repeat( 1, 2 )(3);
  81. BOOST_CHECK_EQUAL( c3.size(), 3u );
  82. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  83. // BCB fails to use operator=() directly,
  84. // it must be worked around using e.g. auxiliary variable
  85. C aux = ba::list_of(1).repeat_fun( 10, &rand )(2)(3);
  86. BOOST_CHECK_EQUAL( aux.size(), 13u );
  87. c3 = aux;
  88. BOOST_CHECK_EQUAL( c3.size(), 13u );
  89. #elif defined( BOOST_NO_CXX11_HDR_INITIALIZER_LIST )
  90. c3 = ba::list_of(1).repeat_fun( 10, &rand )(2)(3);
  91. BOOST_CHECK_EQUAL( c3.size(), 13u );
  92. #endif
  93. #if !defined( BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS )
  94. C c4;
  95. c4 = ba::list_of(1)(2)(3)(4);
  96. BOOST_CHECK_EQUAL( c4.size(), 4u );
  97. C c5(ba::list_of(1)(2)(3)(4)(5));
  98. BOOST_CHECK_EQUAL( c5.size(), 5u );
  99. #endif
  100. #endif
  101. parameter_list p( ba::list_of(1)(2), 3u );
  102. BOOST_CHECK_EQUAL( p.val, 1 );
  103. }
  104. template< class C >
  105. void test_map_list_of()
  106. {
  107. const C c = ba::list_of< std::pair<std::string,int> >( "foo", 1 )( "bar", 2 )( "buh", 3 )( "bah", 4 );
  108. BOOST_CHECK_EQUAL( c.size(), 4u );
  109. const C c2 = ba::map_list_of( "foo", 1 )( "bar", 2 )( "buh", 3 )( "bah", 4 );
  110. BOOST_CHECK_EQUAL( c2.size(), 4u );
  111. }
  112. void test_vector_matrix()
  113. {
  114. using namespace boost;
  115. using namespace boost::assign;
  116. using namespace std;
  117. using boost::array;
  118. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(BOOST_MSVC, <=1300)
  119. #else
  120. const int sz = 3;
  121. typedef array<int,sz> row3;
  122. typedef array<row3,sz> matrix3x3;
  123. matrix3x3 m = list_of( list_of(1)(2)(3) )
  124. ( list_of(4)(5)(6) )
  125. ( list_of(7)(8)(9) );
  126. for( int i = 0; i != sz; ++i )
  127. for( int j = 0; j != sz; ++j )
  128. BOOST_CHECK_EQUAL( m[i][j], i*sz + j + 1 );
  129. typedef vector<int> row;
  130. typedef vector<row> matrix;
  131. //
  132. // note: some libraries need a little help
  133. // with the conversion, hence the 'row' template parameter.
  134. //
  135. matrix m2 = list_of< row >( list_of(1)(2)(3) )
  136. ( list_of(4)(5) )
  137. ( list_of(6) );
  138. for( int i = 0; i != sz; ++i )
  139. for( int j = 0; j != sz - i; ++j )
  140. BOOST_CHECK_EQUAL( m[i][j], i*sz + j + 1 );
  141. #endif
  142. }
  143. void test_map_list_of()
  144. {
  145. /*
  146. maybe in the future...
  147. using namespace std;
  148. using namespace boost::assign;
  149. typedef vector<int> score_type;
  150. typedef map<string,score_type> team_score_map;
  151. team_score_map team_score = map_list_of
  152. ( "Team Foo", list_of(1)(1)(0) )
  153. ( "Team Bar", list_of(0)(0)(0) )
  154. ( "Team FooBar", list_of(0)(0)(1) );
  155. BOOST_CHECK_EQUAL( team_score.size(), 3 );
  156. BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 );
  157. BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 );
  158. */
  159. }
  160. /*
  161. void test_complex_list_of()
  162. {
  163. typedef std::complex<float> complex_t;
  164. std::vector<complex_t> v;
  165. v = ba::list_of<complex_t>(1,2)(2,3)(4,5)(0).
  166. repeat_from_to( complex_t(0,0), complex_t(10,10), complex_t(1,1) );
  167. }
  168. */
  169. struct five
  170. {
  171. five( int, int, int, int, int )
  172. {
  173. }
  174. };
  175. void test_list_of()
  176. {
  177. ba::list_of< five >(1,2,3,4,5)(6,7,8,9,10);
  178. /* Maybe this could be useful in a later version?
  179. // an anonymous lists, fulfills Range concept
  180. for_each( ba::list_of( T() )( T() )( T() ) );
  181. // non-anonymous lists
  182. ba::generic_list<T> list_1 = ba::list_of( T() );
  183. BOOST_CHECK_EQUAL( list_1.size(), 1 );
  184. ba::generic_list<T> list_2 = list_1 + ba::list_of( T() )( T() ) + list_1;
  185. BOOST_CHECK_EQUAL( list_2.size(), 4 );
  186. list_1 += list_2;
  187. BOOST_CHECK_EQUAL( list_1.size(), 5 );
  188. */
  189. }
  190. //
  191. // @remark: ADL is required here, but it is a bit weird to
  192. // open up namespace std. Perhaps Boost.Test needs a
  193. // better configuration option.
  194. //
  195. namespace std
  196. {
  197. template< class T, class Elem, class Traits >
  198. inline std::basic_ostream<Elem,Traits>&
  199. operator<<( std::basic_ostream<Elem, Traits>& Os,
  200. const std::vector<T>& r )
  201. {
  202. return Os << ::boost::make_iterator_range( r.begin(), r.end() );
  203. }
  204. }
  205. template <class Seq>
  206. inline std::vector<int> as_seq( const Seq& s )
  207. {
  208. std::vector<int> c;
  209. return s.to_container( c );
  210. }
  211. void test_comparison_operations()
  212. {
  213. BOOST_CHECK_EQUAL( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(2)) );
  214. BOOST_CHECK_NE( ba::list_of(0)(1)(2), as_seq(ba::list_of(-1)(1)(2)) );
  215. BOOST_CHECK_LT( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(3)) );
  216. BOOST_CHECK_LE( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(2)) );
  217. BOOST_CHECK_GT( ba::list_of(0)(1)(3), as_seq(ba::list_of(0)(1)(2)) );
  218. BOOST_CHECK_GE( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(2)) );
  219. BOOST_CHECK_EQUAL( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(2) );
  220. BOOST_CHECK_NE( as_seq(ba::list_of(0)(1)(2)), ba::list_of(-1)(1)(2) );
  221. BOOST_CHECK_LT( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(3) );
  222. BOOST_CHECK_LE( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(2) );
  223. BOOST_CHECK_GT( as_seq(ba::list_of(0)(1)(3)), ba::list_of(0)(1)(2) );
  224. BOOST_CHECK_GE( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(2) );
  225. }
  226. void check_list_of()
  227. {
  228. test_sequence_list_of_int< std::vector<int> >();
  229. test_sequence_list_of_int< std::list<int> >();
  230. test_sequence_list_of_int< std::deque<int> >();
  231. test_sequence_list_of_int< std::set<int> >();
  232. test_sequence_list_of_int< std::multiset<int> >();
  233. test_sequence_list_of_int< std::vector<float> >();
  234. test_sequence_list_of_string< std::vector<std::string> >();
  235. test_map_list_of< std::map<std::string,int> >();
  236. test_map_list_of< std::multimap<std::string,int> >();
  237. std::stack<std::string> s = ba::list_of( "Foo" )( "Bar" )( "FooBar" ).to_adapter( s );
  238. test_list_of();
  239. test_vector_matrix();
  240. test_comparison_operations();
  241. }
  242. #include <boost/test/unit_test.hpp>
  243. using boost::unit_test::test_suite;
  244. test_suite* init_unit_test_suite( int argc, char* argv[] )
  245. {
  246. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  247. test->add( BOOST_TEST_CASE( &check_list_of ) );
  248. return test;
  249. }