ptr_array.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <boost/test/unit_test.hpp>
  12. #include "test_data.hpp"
  13. #include <boost/ptr_container/ptr_array.hpp>
  14. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  15. #include <boost/utility.hpp>
  16. #include <boost/array.hpp>
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <cstddef>
  20. #include <string>
  21. using namespace std;
  22. using namespace boost;
  23. template< class Node, size_t N >
  24. class n_ary_tree : boost::noncopyable
  25. {
  26. typedef n_ary_tree<Node,N> this_type;
  27. typedef ptr_array<this_type,N> tree_t;
  28. tree_t tree;
  29. Node data;
  30. public:
  31. n_ary_tree() { }
  32. n_ary_tree( const Node& r ) : data(r) { }
  33. public: // modifers
  34. void set_data( const Node& r ) { data = r; }
  35. template< size_t idx >
  36. void set_child( this_type* r ) { tree. BOOST_NESTED_TEMPLATE replace<idx>(r); }
  37. public: // accessors
  38. void print( std::ostream&, std::string indent = " " );
  39. template< size_t idx >
  40. this_type& child() { return tree. BOOST_NESTED_TEMPLATE at<idx>(); }
  41. template< size_t idx >
  42. const this_type& child() const { return tree. BOOST_NESTED_TEMPLATE at<idx>(); }
  43. };
  44. template< class Node, size_t N >
  45. void n_ary_tree<Node,N>::print( std::ostream& out, std::string indent )
  46. {
  47. out << indent << data << "\n";
  48. indent += " ";
  49. for( size_t i = 0; i != N; ++i )
  50. if( !tree.is_null(i) )
  51. tree[i].print( out, indent + " " );
  52. }
  53. template< class C, class B, class T >
  54. void test_array_interface();
  55. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  56. #pragma GCC diagnostic push
  57. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  58. #endif
  59. void test_array()
  60. {
  61. typedef n_ary_tree<std::string,2> binary_tree;
  62. binary_tree tree;
  63. tree.set_data( "root" );
  64. tree.set_child<0>( new binary_tree( "left subtree" ) );
  65. tree.set_child<1>( new binary_tree( "right subtree" ) );
  66. binary_tree& left = tree.child<0>();
  67. left.set_child<0>( new binary_tree( "left left subtree" ) );
  68. left.set_child<1>( new binary_tree( "left right subtree" ) );
  69. binary_tree& right = tree.child<1>();
  70. right.set_child<0>( new binary_tree( "right left subtree" ) );
  71. right.set_child<1>( new binary_tree( "right right subtree" ) );
  72. tree.print( std::cout );
  73. test_array_interface<ptr_array<Base,10>,Base,Derived_class>();
  74. test_array_interface<ptr_array<nullable<Base>,10>,Base,Derived_class>();
  75. test_array_interface<ptr_array<Value,10>,Value,Value>();
  76. test_array_interface<ptr_array<nullable<Value>,10>,Value,Value>();
  77. ptr_array<int,10> vec;
  78. BOOST_CHECK_THROW( vec.at(10), bad_ptr_container_operation );
  79. BOOST_CHECK_THROW( (vec.replace(10u, new int(0))), bad_ptr_container_operation );
  80. #ifndef BOOST_NO_AUTO_PTR
  81. BOOST_CHECK_THROW( (vec.replace(10u, std::auto_ptr<int>(new int(0)))), bad_ptr_container_operation );
  82. #endif
  83. #ifndef BOOST_NO_CXX11_SMART_PTR
  84. BOOST_CHECK_THROW( (vec.replace(10u, std::unique_ptr<int>(new int(0)))), bad_ptr_container_operation );
  85. #endif
  86. BOOST_CHECK_THROW( (vec.replace(0u, 0)), bad_ptr_container_operation );
  87. ptr_array<Derived_class,2> derived;
  88. derived.replace( 0, new Derived_class );
  89. derived.replace( 1, new Derived_class );
  90. ptr_array<Base,2> base( derived );
  91. BOOST_TEST_MESSAGE( "finished derived to base test" );
  92. base = derived;
  93. ptr_array<Base,2> base2( base );
  94. base2 = base;
  95. base = base;
  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. #ifndef BOOST_NO_AUTO_PTR
  105. c.replace( 0, std::auto_ptr<T>( new T ) );
  106. #endif
  107. #ifndef BOOST_NO_CXX11_SMART_PTR
  108. c.replace( 0, std::unique_ptr<T>( new T ) );
  109. #endif
  110. const C c2( c.clone() );
  111. BOOST_DEDUCED_TYPENAME C::iterator i = c.begin();
  112. hide_warning(i);
  113. BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
  114. hide_warning(ci);
  115. BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
  116. hide_warning(i2);
  117. BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin();
  118. hide_warning(ci2);
  119. BOOST_DEDUCED_TYPENAME C::reverse_iterator ri = c.rbegin();
  120. hide_warning(ri);
  121. BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri = c2.rbegin();
  122. hide_warning(cri);
  123. BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2 = c.rend();
  124. hide_warning(rv2);
  125. BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend();
  126. hide_warning(cvr2);
  127. BOOST_TEST_MESSAGE( "finished iterator test" );
  128. BOOST_CHECK_EQUAL( c.empty(), false );
  129. BOOST_CHECK_EQUAL( c.size(), c.max_size() );
  130. BOOST_TEST_MESSAGE( "finished capacity test" );
  131. BOOST_CHECK_EQUAL( c.is_null(0), false );
  132. BOOST_CHECK_EQUAL( c.is_null(1), false );
  133. BOOST_CHECK_EQUAL( c.is_null(2), true );
  134. c.front();
  135. c.back();
  136. c2.front();
  137. c2.back();
  138. C c3;
  139. c.swap( c3 );
  140. C c4;
  141. swap(c4,c3);
  142. c3.swap(c4);
  143. BOOST_CHECK_EQUAL( c.is_null(0), true );
  144. BOOST_CHECK_EQUAL( c3.is_null(0), false );
  145. c.replace( 5, new T );
  146. BOOST_CHECK_EQUAL( c.is_null(5), false );
  147. c = c3.release();
  148. for( size_t i = 0; i < c3.size(); ++i )
  149. BOOST_CHECK_EQUAL( c3.is_null(i), true );
  150. BOOST_TEST_MESSAGE( "finished element access test" );
  151. }
  152. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  153. #pragma GCC diagnostic pop
  154. #endif
  155. using boost::unit_test::test_suite;
  156. test_suite* init_unit_test_suite( int argc, char* argv[] )
  157. {
  158. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  159. test->add( BOOST_TEST_CASE( &test_array ) );
  160. return test;
  161. }