ptr_set.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "associative_test_data.hpp"
  13. #include <boost/ptr_container/ptr_set.hpp>
  14. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  15. template< class SetDerived, class SetBase, class T >
  16. void test_transfer()
  17. {
  18. SetBase to;
  19. SetDerived from;
  20. from.insert( new T );
  21. from.insert( new T );
  22. transfer_test( from, to );
  23. }
  24. template< class BaseContainer, class DerivedContainer, class Derived >
  25. void test_copy()
  26. {
  27. DerivedContainer derived;
  28. derived.insert( new Derived );
  29. derived.insert( new Derived );
  30. BaseContainer base( derived );
  31. BOOST_CHECK_EQUAL( derived.size(), base.size() );
  32. base.clear();
  33. base = derived;
  34. BOOST_CHECK_EQUAL( derived.size(), base.size() );
  35. base = base;
  36. }
  37. template< class PtrSet >
  38. void test_erase()
  39. {
  40. PtrSet s;
  41. typedef typename PtrSet::key_type T;
  42. T t;
  43. T* t2 = t.clone();
  44. s.insert ( new T );
  45. s.insert ( t2 );
  46. s.insert ( new T );
  47. BOOST_CHECK_EQUAL( s.size(), 3u );
  48. BOOST_CHECK_EQUAL( t, *t2 );
  49. BOOST_CHECK( ! (t < *t2) );
  50. BOOST_CHECK( ! (*t2 < t) );
  51. BOOST_CHECK_EQUAL( t, *t2 );
  52. unsigned n = s.erase( t );
  53. BOOST_CHECK( n > 0 );
  54. }
  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_set()
  60. {
  61. srand( 0 );
  62. ptr_set_test< ptr_set<Base>, Base, Derived_class, true >();
  63. ptr_set_test< ptr_set<Value>, Value, Value, true >();
  64. ptr_set_test< ptr_multiset<Base>, Base, Derived_class, true >();
  65. ptr_set_test< ptr_multiset<Value>, Value, Value, true >();
  66. test_copy< ptr_set<Base>, ptr_set<Derived_class>,
  67. Derived_class>();
  68. test_copy< ptr_multiset<Base>, ptr_multiset<Derived_class>,
  69. Derived_class>();
  70. test_transfer< ptr_set<Derived_class>, ptr_set<Base>, Derived_class>();
  71. test_transfer< ptr_multiset<Derived_class>, ptr_multiset<Base>, Derived_class>();
  72. ptr_set<int> set;
  73. BOOST_CHECK_THROW( set.insert( 0 ), bad_ptr_container_operation );
  74. set.insert( new int(0) );
  75. #ifndef BOOST_NO_AUTO_PTR
  76. std::auto_ptr<int> ap( new int(1) );
  77. set.insert( ap );
  78. #endif
  79. #ifndef BOOST_NO_CXX11_SMART_PTR
  80. std::unique_ptr<int> up( new int(2) );
  81. set.insert( std::move( up ) );
  82. #endif
  83. BOOST_CHECK_THROW( (set.replace(set.begin(), 0 )), bad_ptr_container_operation );
  84. #ifndef BOOST_NO_AUTO_PTR
  85. BOOST_CHECK_THROW( (set.replace(set.begin(), std::auto_ptr<int>(0) )), bad_ptr_container_operation );
  86. #endif
  87. #if !defined(BOOST_NO_CXX11_SMART_PTR) && !defined(BOOST_NO_CXX11_NULLPTR)
  88. BOOST_CHECK_THROW( (set.replace(set.begin(), std::unique_ptr<int>(nullptr) )), bad_ptr_container_operation );
  89. #endif
  90. test_erase< ptr_set<Base> >();
  91. test_erase< ptr_multiset<Base> >();
  92. }
  93. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  94. #pragma GCC diagnostic pop
  95. #endif
  96. using boost::unit_test::test_suite;
  97. test_suite* init_unit_test_suite( int argc, char* argv[] )
  98. {
  99. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  100. test->add( BOOST_TEST_CASE( &test_set ) );
  101. return test;
  102. }