indirected.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. 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. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/adaptor/indirected.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/range/algorithm_ext.hpp>
  17. #include <algorithm>
  18. #include <list>
  19. #include <set>
  20. #include <vector>
  21. namespace boost
  22. {
  23. namespace
  24. {
  25. template< class Container >
  26. void indirected_test_impl( Container& c )
  27. {
  28. using namespace boost::adaptors;
  29. // This is my preferred syntax using the | operator.
  30. std::vector< int > test_result1;
  31. boost::push_back(test_result1, c | indirected);
  32. // This is an alternative syntax preferred by some.
  33. std::vector< int > test_result2;
  34. boost::push_back(test_result2, adaptors::indirect(c));
  35. // Calculate the reference result.
  36. std::vector< int > reference_result;
  37. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
  38. for (iter_t it = c.begin(); it != c.end(); ++it)
  39. {
  40. reference_result.push_back(**it);
  41. }
  42. BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
  43. reference_result.end(),
  44. test_result1.begin(),
  45. test_result1.end() );
  46. BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
  47. reference_result.end(),
  48. test_result2.begin(),
  49. test_result2.end() );
  50. }
  51. template< class Container >
  52. void indirected_test_impl()
  53. {
  54. using namespace boost::assign;
  55. Container c;
  56. indirected_test_impl(c);
  57. c += boost::shared_ptr<int>(new int(1));
  58. indirected_test_impl(c);
  59. std::vector<int> v;
  60. v += 1,1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
  61. for (std::vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
  62. {
  63. c += boost::shared_ptr<int>(new int(*it));
  64. }
  65. indirected_test_impl(c);
  66. }
  67. void indirected_test()
  68. {
  69. indirected_test_impl< std::vector< boost::shared_ptr< int > > >();
  70. }
  71. }
  72. }
  73. boost::unit_test::test_suite*
  74. init_unit_test_suite(int argc, char* argv[])
  75. {
  76. boost::unit_test::test_suite* test
  77. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indirected" );
  78. test->add( BOOST_TEST_CASE( &boost::indirected_test ) );
  79. return test;
  80. }