2 // Boost.Pointer Container
4 // Copyright Thorsten Ottosen 2003-2005. 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)
9 // For more information, see http://www.boost.org/libs/ptr_container/
12 #include <boost/test/unit_test.hpp>
13 #include "test_data.hpp"
14 #include <boost/ptr_container/ptr_array.hpp>
15 #include <boost/utility.hpp>
16 #include <boost/array.hpp>
23 using namespace boost
;
25 template< class Node
, size_t N
>
26 class n_ary_tree
: boost::noncopyable
28 typedef n_ary_tree
<Node
,N
> this_type
;
29 typedef ptr_array
<this_type
,N
> tree_t
;
36 n_ary_tree( const Node
& r
) : data(r
) { }
39 void set_data( const Node
& r
) { data
= r
; }
40 template< size_t idx
>
41 void set_child( this_type
* r
) { tree
. BOOST_NESTED_TEMPLATE replace
<idx
>(r
); }
44 void print( std::ostream
&, std::string indent
= " " );
45 template< size_t idx
>
46 this_type
& child() { return tree
. BOOST_NESTED_TEMPLATE at
<idx
>(); }
47 template< size_t idx
>
48 const this_type
& child() const { return tree
. BOOST_NESTED_TEMPLATE at
<idx
>(); }
54 template< class Node
, size_t N
>
55 void n_ary_tree
<Node
,N
>::print( std::ostream
& out
, std::string indent
)
57 out
<< indent
<< data
<< "\n";
59 for( size_t i
= 0; i
!= N
; ++i
)
60 if( !tree
.is_null(i
) )
61 tree
[i
].print( out
, indent
+ " " );
65 template< class C
, class B
, class T
>
66 void test_array_interface();
70 typedef n_ary_tree
<std::string
,2> binary_tree
;
72 tree
.set_data( "root" );
73 tree
.set_child
<0>( new binary_tree( "left subtree" ) );
74 tree
.set_child
<1>( new binary_tree( "right subtree" ) );
75 binary_tree
& left
= tree
.child
<0>();
76 left
.set_child
<0>( new binary_tree( "left left subtree" ) );
77 left
.set_child
<1>( new binary_tree( "left right subtree" ) );
78 binary_tree
& right
= tree
.child
<1>();
79 right
.set_child
<0>( new binary_tree( "right left subtree" ) );
80 right
.set_child
<1>( new binary_tree( "right right subtree" ) );
82 tree
.print( std::cout
);
84 test_array_interface
<ptr_array
<Base
,10>,Base
,Derived_class
>();
85 test_array_interface
<ptr_array
<nullable
<Base
>,10>,Base
,Derived_class
>();
86 test_array_interface
<ptr_array
<Value
,10>,Value
,Value
>();
87 test_array_interface
<ptr_array
<nullable
<Value
>,10>,Value
,Value
>();
89 ptr_array
<int,10> vec
;
90 BOOST_CHECK_THROW( vec
.at(10), bad_ptr_container_operation
);
91 BOOST_CHECK_THROW( (vec
.replace(10u, new int(0))), bad_ptr_container_operation
);
92 BOOST_CHECK_THROW( (vec
.replace(10u, std::auto_ptr
<int>(new int(0)))), bad_ptr_container_operation
);
93 BOOST_CHECK_THROW( (vec
.replace(0u, 0)), bad_ptr_container_operation
);
95 ptr_array
<Derived_class
,2> derived
;
96 derived
.replace( 0, new Derived_class
);
97 derived
.replace( 1, new Derived_class
);
98 ptr_array
<Base
,2> base( derived
);
100 BOOST_TEST_MESSAGE( "finished derived to base test" );
103 ptr_array
<Base
,2> base2( base
);
108 template< class C
, class B
, class T
>
109 void test_array_interface()
112 c
.replace( 0, new T
);
113 c
.replace( 1, new B
);
114 c
.replace( 9, new T
);
115 c
.replace( 0, std::auto_ptr
<T
>( new T
) );
116 const C
c2( c
.clone() );
118 BOOST_DEDUCED_TYPENAME
C::iterator i
= c
.begin();
119 BOOST_DEDUCED_TYPENAME
C::const_iterator ci
= c2
.begin();
120 BOOST_DEDUCED_TYPENAME
C::iterator i2
= c
.end();
121 BOOST_DEDUCED_TYPENAME
C::const_iterator ci2
= c2
.begin();
122 BOOST_DEDUCED_TYPENAME
C::reverse_iterator ri
= c
.rbegin();
123 BOOST_DEDUCED_TYPENAME
C::const_reverse_iterator cri
= c2
.rbegin();
124 BOOST_DEDUCED_TYPENAME
C::reverse_iterator rv2
= c
.rend();
125 BOOST_DEDUCED_TYPENAME
C::const_reverse_iterator cvr2
= c2
.rend();
127 BOOST_TEST_MESSAGE( "finished iterator test" );
129 BOOST_CHECK_EQUAL( c
.empty(), false );
130 BOOST_CHECK_EQUAL( c
.size(), c
.max_size() );
132 BOOST_TEST_MESSAGE( "finished capacity test" );
134 BOOST_CHECK_EQUAL( c
.is_null(0), false );
135 BOOST_CHECK_EQUAL( c
.is_null(1), false );
136 BOOST_CHECK_EQUAL( c
.is_null(2), true );
148 BOOST_CHECK_EQUAL( c
.is_null(0), true );
149 BOOST_CHECK_EQUAL( c3
.is_null(0), false );
151 c
.replace( 5, new T
);
152 BOOST_CHECK_EQUAL( c
.is_null(5), false );
154 for( size_t i
= 0; i
< c3
.size(); ++i
)
155 BOOST_CHECK_EQUAL( c3
.is_null(i
), true );
157 BOOST_TEST_MESSAGE( "finished element access test" );
160 using boost::unit_test::test_suite
;
162 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
164 test_suite
* test
= BOOST_TEST_SUITE( "Pointer Container Test Suite" );
166 test
->add( BOOST_TEST_CASE( &test_array
) );