1 | // |
---|
2 | // Boost.Pointer Container |
---|
3 | // |
---|
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) |
---|
8 | // |
---|
9 | // For more information, see http://www.boost.org/libs/ptr_container/ |
---|
10 | // |
---|
11 | |
---|
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> |
---|
17 | #include <algorithm> |
---|
18 | #include <iostream> |
---|
19 | #include <cstddef> |
---|
20 | #include <string> |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | using namespace boost; |
---|
24 | |
---|
25 | template< class Node, size_t N > |
---|
26 | class n_ary_tree : boost::noncopyable |
---|
27 | { |
---|
28 | typedef n_ary_tree<Node,N> this_type; |
---|
29 | typedef ptr_array<this_type,N> tree_t; |
---|
30 | |
---|
31 | tree_t tree; |
---|
32 | Node data; |
---|
33 | |
---|
34 | public: |
---|
35 | n_ary_tree() { } |
---|
36 | n_ary_tree( const Node& r ) : data(r) { } |
---|
37 | |
---|
38 | public: // modifers |
---|
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); } |
---|
42 | |
---|
43 | public: // accessors |
---|
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>(); } |
---|
49 | |
---|
50 | }; |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | template< class Node, size_t N > |
---|
55 | void n_ary_tree<Node,N>::print( std::ostream& out, std::string indent ) |
---|
56 | { |
---|
57 | out << indent << data << "\n"; |
---|
58 | indent += " "; |
---|
59 | for( size_t i = 0; i != N; ++i ) |
---|
60 | if( !tree.is_null(i) ) |
---|
61 | tree[i].print( out, indent + " " ); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | template< class C, class B, class T > |
---|
66 | void test_array_interface(); |
---|
67 | |
---|
68 | void test_array() |
---|
69 | { |
---|
70 | typedef n_ary_tree<std::string,2> binary_tree; |
---|
71 | binary_tree 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" ) ); |
---|
81 | |
---|
82 | tree.print( std::cout ); |
---|
83 | |
---|
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>(); |
---|
88 | |
---|
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 ); |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | template< class C, class B, class T > |
---|
98 | void test_array_interface() |
---|
99 | { |
---|
100 | C c; |
---|
101 | c.replace( 0, new T ); |
---|
102 | c.replace( 1, new B ); |
---|
103 | c.replace( 9, new T ); |
---|
104 | c.replace( 0, std::auto_ptr<T>( new T ) ); |
---|
105 | const C c2( c.clone() ); |
---|
106 | |
---|
107 | BOOST_DEDUCED_TYPENAME C::iterator i = c.begin(); |
---|
108 | BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin(); |
---|
109 | BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end(); |
---|
110 | BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin(); |
---|
111 | BOOST_DEDUCED_TYPENAME C::reverse_iterator ri = c.rbegin(); |
---|
112 | BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri = c2.rbegin(); |
---|
113 | BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2 = c.rend(); |
---|
114 | BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend(); |
---|
115 | |
---|
116 | BOOST_MESSAGE( "finished iterator test" ); |
---|
117 | |
---|
118 | BOOST_CHECK_EQUAL( c.empty(), false ); |
---|
119 | BOOST_CHECK_EQUAL( c.size(), c.max_size() ); |
---|
120 | |
---|
121 | BOOST_MESSAGE( "finished capacity test" ); |
---|
122 | |
---|
123 | BOOST_CHECK_EQUAL( c.is_null(0), false ); |
---|
124 | BOOST_CHECK_EQUAL( c.is_null(1), false ); |
---|
125 | BOOST_CHECK_EQUAL( c.is_null(2), true ); |
---|
126 | |
---|
127 | c.front(); |
---|
128 | c.back(); |
---|
129 | c2.front(); |
---|
130 | c2.back(); |
---|
131 | C c3; |
---|
132 | c.swap( c3 ); |
---|
133 | C c4; |
---|
134 | swap(c4,c3); |
---|
135 | c3.swap(c4); |
---|
136 | |
---|
137 | BOOST_CHECK_EQUAL( c.is_null(0), true ); |
---|
138 | BOOST_CHECK_EQUAL( c3.is_null(0), false ); |
---|
139 | |
---|
140 | c.replace( 5, new T ); |
---|
141 | BOOST_CHECK_EQUAL( c.is_null(5), false ); |
---|
142 | c = c3.release(); |
---|
143 | for( size_t i = 0; i < c3.size(); ++i ) |
---|
144 | BOOST_CHECK_EQUAL( c3.is_null(i), true ); |
---|
145 | |
---|
146 | BOOST_MESSAGE( "finished element access test" ); |
---|
147 | |
---|
148 | } |
---|
149 | |
---|
150 | using boost::unit_test::test_suite; |
---|
151 | |
---|
152 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
153 | { |
---|
154 | test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" ); |
---|
155 | |
---|
156 | test->add( BOOST_TEST_CASE( &test_array ) ); |
---|
157 | |
---|
158 | return test; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|