ptr_inserter.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2008. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #include <boost/ptr_container/ptr_inserter.hpp>
  12. #include <boost/ptr_container/indirect_fun.hpp>
  13. #include <boost/ptr_container/ptr_deque.hpp>
  14. #include <boost/ptr_container/ptr_list.hpp>
  15. #include <boost/assign/list_inserter.hpp>
  16. #include <boost/iterator/transform_iterator.hpp>
  17. #include <boost/test/test_tools.hpp>
  18. #include <algorithm>
  19. #include <functional>
  20. #include <string>
  21. template< class T >
  22. struct caster_to
  23. {
  24. typedef T result_type;
  25. T operator()( void* obj ) const
  26. {
  27. return static_cast<T>( obj );
  28. }
  29. };
  30. template< class PtrSequence >
  31. void test_ptr_inserter_helper()
  32. {
  33. using namespace boost;
  34. PtrSequence seq;
  35. const int size = 1000;
  36. for( int i = 0; i != size; ++i )
  37. seq.push_back( i % 3 == 0 ? 0 : new int(i) );
  38. PtrSequence seq2;
  39. //
  40. // @remark: we call .base() to avoid null pointer indirection.
  41. // The clone_inserter will handle the nulls correctly.
  42. //
  43. std::copy( boost::make_transform_iterator( seq.begin().base(), caster_to<int*>() ),
  44. boost::make_transform_iterator( seq.end().base(), caster_to<int*>() ),
  45. ptr_container::ptr_back_inserter( seq2 ) );
  46. std::copy( boost::make_transform_iterator( seq.begin().base(), caster_to<int*>() ),
  47. boost::make_transform_iterator( seq.end().base(), caster_to<int*>() ),
  48. ptr_container::ptr_front_inserter( seq2 ) );
  49. BOOST_CHECK_EQUAL( seq.size()*2, seq2.size() );
  50. PtrSequence seq3;
  51. for( int i = 0; i != size; ++i )
  52. seq3.push_back( new int(i%3) );
  53. //
  54. // @remark: since there are no nulls in this container, it
  55. // is easier to handle.
  56. //
  57. std::copy( seq3.begin(), seq3.end(),
  58. ptr_container::ptr_inserter( seq, seq.end() ) );
  59. BOOST_CHECK_EQUAL( seq.size(), seq2.size() );
  60. }
  61. void test_ptr_inserter()
  62. {
  63. test_ptr_inserter_helper< boost::ptr_list< boost::nullable<int> > >();
  64. test_ptr_inserter_helper< boost::ptr_deque< boost::nullable<int> > >();
  65. }
  66. #include <boost/test/unit_test.hpp>
  67. using boost::unit_test::test_suite;
  68. test_suite* init_unit_test_suite( int argc, char* argv[] )
  69. {
  70. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  71. test->add( BOOST_TEST_CASE( &test_ptr_inserter ) );
  72. return test;
  73. }