adaptors.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen 2006. 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/range/
  9. //
  10. #include <boost/range/adaptors.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. #include <boost/foreach.hpp>
  14. #include <boost/assign/list_of.hpp>
  15. #include <vector>
  16. #include <list>
  17. #include <string>
  18. #include <map>
  19. template< class T >
  20. struct less_than
  21. {
  22. T val;
  23. less_than() : val(0)
  24. {}
  25. less_than( T t ) : val(t)
  26. {}
  27. bool operator()( const T& r ) const
  28. {
  29. return r < val;
  30. }
  31. };
  32. template< class T >
  33. struct multiply
  34. {
  35. T val;
  36. typedef T& result_type;
  37. multiply( T t ) : val(t)
  38. { }
  39. T& operator()( T& r ) const
  40. {
  41. return r *= 2;
  42. }
  43. };
  44. template< class Rng >
  45. void check_copy( Rng r )
  46. {
  47. //
  48. // Make sure the generated iterators
  49. // can actually be copied
  50. //
  51. Rng r2 = r;
  52. r2 = r;
  53. }
  54. template< class Rng >
  55. void check_direct()
  56. {
  57. using namespace boost::adaptors;
  58. Rng rng = boost::assign::list_of(1)(2)(3)(4)(5).to_container( rng );
  59. Rng out;
  60. //
  61. // test each alphabetically
  62. //
  63. BOOST_FOREACH( int i, rng | filtered( less_than<int>(4) )
  64. /*| reversed*/
  65. | transformed( multiply<int>(2) ) )
  66. {
  67. out.push_back( i );
  68. }
  69. BOOST_CHECK_EQUAL( out.size(), 3u );
  70. BOOST_CHECK_EQUAL( *out.begin(), 2 );
  71. BOOST_CHECK_EQUAL( *boost::next(out.begin()), 4 );
  72. BOOST_CHECK_EQUAL( *boost::next(out.begin(),2), 6 );
  73. rng = boost::assign::list_of(1)(1)(2)(2)(3)(3)(4)(5).to_container( rng );
  74. out.clear();
  75. /*
  76. BOOST_FOREACH( int i, rng | adjacent_filtered( std::equal_to<int>() )
  77. | uniqued )
  78. {
  79. out.push_back( i );
  80. }*/
  81. }
  82. template< class IndirectRng >
  83. void check_indirect()
  84. {
  85. using namespace boost::adaptors;
  86. IndirectRng rng;
  87. std::vector< boost::shared_ptr< int > > holder;
  88. for( unsigned i = 0u; i != 20u; ++i )
  89. {
  90. boost::shared_ptr<int> v(new int(i));
  91. rng.push_back( v.get() );
  92. }
  93. BOOST_FOREACH( int& i, rng | indirected | reversed
  94. | transformed( multiply<int>(2) ) )
  95. {
  96. i += 1;
  97. }
  98. }
  99. template< class RandomAccessRng >
  100. void check_random_access()
  101. {
  102. using namespace boost::adaptors;
  103. RandomAccessRng rng(1, 20u);
  104. RandomAccessRng out;
  105. BOOST_FOREACH( int i, rng | reversed
  106. | transformed( multiply<int>(2) )
  107. /* | sliced(0,15) */ )
  108. {
  109. out.push_back( i );
  110. }
  111. BOOST_FOREACH( int i, rng | copied(3u,13u) )
  112. {
  113. out.push_back( i );
  114. }
  115. }
  116. template< class Map >
  117. void check_map()
  118. {
  119. using namespace boost::adaptors;
  120. Map m;
  121. m.insert( std::make_pair(1,2) );
  122. m.insert( std::make_pair(2,2) );
  123. m.insert( std::make_pair(3,2) );
  124. m.insert( std::make_pair(4,2) );
  125. m.insert( std::make_pair(5,2) );
  126. m.insert( std::make_pair(6,2) );
  127. m.insert( std::make_pair(7,2) );
  128. std::vector<int> keys
  129. = boost::copy_range< std::vector<int> >( m | map_keys );
  130. std::vector<int> values
  131. = boost::copy_range< std::vector<int> >( m | map_values );
  132. }
  133. void check_regex()
  134. {
  135. using namespace boost::adaptors;
  136. std::string s("This is a string of tokens");
  137. std::vector<std::string> tokens =
  138. boost::copy_range< std::vector<std::string> >( s | tokenized( "\\s+", -1 ) );
  139. }
  140. void check_adaptors()
  141. {
  142. check_direct< std::vector<int> >();
  143. check_direct< std::list<int> >();
  144. check_indirect< std::vector<int*> >();
  145. check_indirect< std::list<int*> >();
  146. check_map< std::map<int,int> >();
  147. // check_random_access< std::vector<int> >();
  148. check_regex();
  149. using namespace boost::adaptors;
  150. std::vector<int> vec(10u,20);
  151. std::vector<int*> pvec;
  152. std::map<int,int> map;
  153. check_copy( vec | adjacent_filtered( std::equal_to<int>() ) );
  154. // check_copy( vec | indexed );
  155. check_copy( vec | reversed );
  156. check_copy( vec | uniqued );
  157. check_copy( pvec | indirected );
  158. // check_copy( vec | sliced(1,5) );
  159. //
  160. // This does not return an iterator_range<>, so
  161. // won't pass this test of implicit conversion
  162. // check_copy( vec | copied(1,5) );
  163. //
  164. check_copy( map | map_values );
  165. check_copy( map | map_keys );
  166. check_copy( std::string( "a string" ) | tokenized( "\\s+", -1 ) );
  167. check_copy( vec | filtered( less_than<int>(2) ) );
  168. check_copy( vec | transformed( multiply<int>(2) ) );
  169. }
  170. using boost::unit_test::test_suite;
  171. test_suite* init_unit_test_suite( int argc, char* argv[] )
  172. {
  173. using namespace boost;
  174. test_suite* test = BOOST_TEST_SUITE( "Range Test Suite - Adaptors" );
  175. test->add( BOOST_TEST_CASE( &check_adaptors ) );
  176. return test;
  177. }