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 | |
12 | #include <boost/ptr_container/ptr_inserter.hpp> |
13 | #include <boost/ptr_container/indirect_fun.hpp> |
14 | #include <boost/ptr_container/ptr_deque.hpp> |
15 | #include <boost/ptr_container/ptr_list.hpp> |
16 | #include <boost/assign/list_inserter.hpp> |
17 | #include <boost/iterator/transform_iterator.hpp> |
18 | #include <boost/test/test_tools.hpp> |
19 | #include <algorithm> |
20 | #include <functional> |
21 | #include <string> |
22 | |
23 | template< class T > |
24 | struct caster_to |
25 | { |
26 | typedef T result_type; |
27 | |
28 | T operator()( void* obj ) const |
29 | { |
30 | return static_cast<T>( obj ); |
31 | } |
32 | }; |
33 | |
34 | template< class PtrSequence > |
35 | void test_ptr_inserter_helper() |
36 | { |
37 | using namespace boost; |
38 | PtrSequence seq; |
39 | const int size = 1000; |
40 | for( int i = 0; i != size; ++i ) |
41 | seq.push_back( i % 3 == 0 ? 0 : new int(i) ); |
42 | |
43 | PtrSequence seq2; |
44 | // |
45 | // @remark: we call .base() to avoid null pointer indirection. |
46 | // The clone_inserter will handle the nulls correctly. |
47 | // |
48 | std::copy( boost::make_transform_iterator( seq.begin().base(), caster_to<int*>() ), |
49 | boost::make_transform_iterator( seq.end().base(), caster_to<int*>() ), |
50 | ptr_container::ptr_back_inserter( seq2 ) ); |
51 | |
52 | std::copy( boost::make_transform_iterator( seq.begin().base(), caster_to<int*>() ), |
53 | boost::make_transform_iterator( seq.end().base(), caster_to<int*>() ), |
54 | ptr_container::ptr_front_inserter( seq2 ) ); |
55 | BOOST_CHECK_EQUAL( seq.size()*2, seq2.size() ); |
56 | |
57 | PtrSequence seq3; |
58 | for( int i = 0; i != size; ++i ) |
59 | seq3.push_back( new int(i%3) ); |
60 | |
61 | // |
62 | // @remark: since there are no nulls in this container, it |
63 | // is easier to handle. |
64 | // |
65 | std::copy( seq3.begin(), seq3.end(), |
66 | ptr_container::ptr_inserter( seq, seq.end() ) ); |
67 | BOOST_CHECK_EQUAL( seq.size(), seq2.size() ); |
68 | } |
69 | |
70 | |
71 | void test_ptr_inserter() |
72 | { |
73 | test_ptr_inserter_helper< boost::ptr_list< boost::nullable<int> > >(); |
74 | test_ptr_inserter_helper< boost::ptr_deque< boost::nullable<int> > >(); |
75 | |
76 | |
77 | } |
78 | |
79 | |
80 | |
81 | #include <boost/test/unit_test.hpp> |
82 | using boost::unit_test::test_suite; |
83 | |
84 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
85 | { |
86 | test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" ); |
87 | |
88 | test->add( BOOST_TEST_CASE( &test_ptr_inserter ) ); |
89 | |
90 | return test; |
91 | } |
92 | |
93 | |
94 | |