Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/ptr_container/test/ptr_array.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

  • Property svn:executable set to *
File size: 4.8 KB
Line 
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
22using namespace std;
23using namespace boost;
24
25template< class Node, size_t N >
26class 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
34public:
35    n_ary_tree()                                 { }
36    n_ary_tree( const Node& r ) : data(r)        { }
37
38public: // 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
43public: // 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
54template< class Node, size_t N >
55void 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
65template< class C, class B, class T >
66void test_array_interface();
67
68void 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(0u, 0)), bad_ptr_container_operation ); 
93   
94}
95
96template< class C, class B, class T >
97void test_array_interface()
98{
99    C c;
100    c.replace( 0, new T );
101    c.replace( 1, new B );
102    c.replace( 9, new T );
103    const C c2( c.clone() );
104   
105    BOOST_DEDUCED_TYPENAME C::iterator i                  = c.begin();
106    BOOST_DEDUCED_TYPENAME C::const_iterator ci           = c2.begin();
107    BOOST_DEDUCED_TYPENAME C::iterator i2                 = c.end();
108    BOOST_DEDUCED_TYPENAME C::const_iterator ci2          = c2.begin();
109    BOOST_DEDUCED_TYPENAME C::reverse_iterator ri         = c.rbegin();
110    BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri  = c2.rbegin();
111    BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2        = c.rend();
112    BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend();
113
114    BOOST_MESSAGE( "finished iterator test" ); 
115
116    BOOST_CHECK_EQUAL( c.empty(), false );
117    BOOST_CHECK_EQUAL( c.size(), c.max_size() );
118   
119    BOOST_MESSAGE( "finished capacity test" ); 
120
121    BOOST_CHECK_EQUAL( c.is_null(0), false );
122    BOOST_CHECK_EQUAL( c.is_null(1), false );
123    BOOST_CHECK_EQUAL( c.is_null(2), true );
124
125    c.front();
126    c.back();
127    c2.front();
128    c2.back();
129    C c3;
130    c.swap( c3 );
131    C c4;
132    swap(c4,c3);
133    c3.swap(c4);
134
135    BOOST_CHECK_EQUAL( c.is_null(0), true );
136    BOOST_CHECK_EQUAL( c3.is_null(0), false );
137
138    c.replace( 5, new T );
139    BOOST_CHECK_EQUAL( c.is_null(5), false );
140    c = c3.release();
141    for( size_t i = 0; i < c3.size(); ++i )
142        BOOST_CHECK_EQUAL( c3.is_null(i), true );
143
144    BOOST_MESSAGE( "finished element access test" ); 
145
146}
147
148using boost::unit_test::test_suite;
149
150test_suite* init_unit_test_suite( int argc, char* argv[] )
151{
152    test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
153
154    test->add( BOOST_TEST_CASE( &test_array ) );
155
156    return test;
157}
158
159
Note: See TracBrowser for help on using the repository browser.