ptr_vector.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "sequence_test_data.hpp"
  13. #include <boost/ptr_container/ptr_vector.hpp>
  14. #include <boost/ptr_container/ptr_list.hpp>
  15. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  16. #include <boost/assign/list_inserter.hpp>
  17. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  18. #pragma GCC diagnostic push
  19. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  20. #endif
  21. void test_ptr_vector()
  22. {
  23. reversible_container_test< ptr_vector<Base>, Base, Derived_class >();
  24. reversible_container_test< ptr_vector<Value>, Value, Value >();
  25. #ifdef BOOST_NO_SFINAE
  26. #else
  27. reversible_container_test< ptr_vector< nullable<Base> >, Base, Derived_class >();
  28. reversible_container_test< ptr_vector< nullable<Value> >, Value, Value >();
  29. #endif
  30. container_assignment_test< ptr_vector<Base>, ptr_vector<Derived_class>,
  31. Derived_class>();
  32. container_assignment_test< ptr_vector< nullable<Base> >,
  33. ptr_vector< nullable<Derived_class> >,
  34. Derived_class>();
  35. container_assignment_test< ptr_vector< nullable<Base> >,
  36. ptr_vector<Derived_class>,
  37. Derived_class>();
  38. container_assignment_test< ptr_vector<Base>,
  39. ptr_vector< nullable<Derived_class> >,
  40. Derived_class>();
  41. test_transfer< ptr_vector<Derived_class>, ptr_vector<Base>, Derived_class>();
  42. test_transfer< ptr_vector<Derived_class>, ptr_list<Base>, Derived_class>();
  43. random_access_algorithms_test< ptr_vector<int> >();
  44. ptr_vector<int> vec( 100u );
  45. BOOST_CHECK( vec.capacity() >= 100u );
  46. #ifdef BOOST_PTR_CONTAINER_NO_EXCEPTIONS
  47. #else
  48. BOOST_CHECK_THROW( vec.push_back(0), bad_ptr_container_operation );
  49. BOOST_CHECK_THROW( (vec.insert( vec.begin(), 0 )), bad_ptr_container_operation );
  50. BOOST_CHECK_THROW( vec.at( 42 ), bad_ptr_container_operation );
  51. vec.push_back( new int(0) );
  52. BOOST_CHECK_THROW( (vec.replace(10u, new int(0))), bad_ptr_container_operation );
  53. BOOST_CHECK_THROW( (vec.replace(0u, 0)), bad_ptr_container_operation );
  54. BOOST_CHECK_THROW( (vec.replace(vec.begin(), 0 )), bad_ptr_container_operation );
  55. #endif
  56. vec.clear();
  57. assign::push_back( vec )( new int(2) )
  58. ( new int(4) )
  59. ( new int(6) )
  60. ( new int(8) );
  61. ptr_vector<int> vec2;
  62. assign::push_back( vec2 )
  63. ( new int(1) )
  64. ( new int(3) )
  65. ( new int(5) )
  66. ( new int(7) );
  67. BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
  68. BOOST_CHECK( vec > vec2 );
  69. BOOST_CHECK( vec != vec2 );
  70. BOOST_CHECK( !(vec == vec2) );
  71. BOOST_CHECK( vec2 < vec );
  72. BOOST_CHECK( vec2 <= vec );
  73. BOOST_CHECK( vec >= vec2 );
  74. const int data_size = 10;
  75. int** array = new int*[data_size];
  76. for( int i = 0; i != data_size; ++i )
  77. array[i] = new int(i);
  78. vec.transfer( vec.begin(), array, data_size );
  79. int** array2 = vec.c_array();
  80. BOOST_CHECK( array2 != array );
  81. }
  82. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  83. #pragma GCC diagnostic pop
  84. #endif
  85. using boost::unit_test::test_suite;
  86. test_suite* init_unit_test_suite( int argc, char* argv[] )
  87. {
  88. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  89. test->add( BOOST_TEST_CASE( &test_ptr_vector ) );
  90. return test;
  91. }