]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/ptr_container/test/ptr_array.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / ptr_container / test / ptr_array.cpp
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 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 );
99
100 BOOST_TEST_MESSAGE( "finished derived to base test" );
101
102 base = derived;
103 ptr_array<Base,2> base2( base );
104 base2 = base;
105 base = base;
106 }
107
108 template< class C, class B, class T >
109 void test_array_interface()
110 {
111 C c;
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() );
117
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();
126
127 BOOST_TEST_MESSAGE( "finished iterator test" );
128
129 BOOST_CHECK_EQUAL( c.empty(), false );
130 BOOST_CHECK_EQUAL( c.size(), c.max_size() );
131
132 BOOST_TEST_MESSAGE( "finished capacity test" );
133
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 );
137
138 c.front();
139 c.back();
140 c2.front();
141 c2.back();
142 C c3;
143 c.swap( c3 );
144 C c4;
145 swap(c4,c3);
146 c3.swap(c4);
147
148 BOOST_CHECK_EQUAL( c.is_null(0), true );
149 BOOST_CHECK_EQUAL( c3.is_null(0), false );
150
151 c.replace( 5, new T );
152 BOOST_CHECK_EQUAL( c.is_null(5), false );
153 c = c3.release();
154 for( size_t i = 0; i < c3.size(); ++i )
155 BOOST_CHECK_EQUAL( c3.is_null(i), true );
156
157 BOOST_TEST_MESSAGE( "finished element access test" );
158 }
159
160 using boost::unit_test::test_suite;
161
162 test_suite* init_unit_test_suite( int argc, char* argv[] )
163 {
164 test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
165
166 test->add( BOOST_TEST_CASE( &test_array ) );
167
168 return test;
169 }
170
171