ptr_unordered_set.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_unordered_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 Cont, class T >
  38. void test_unordered_interface()
  39. {
  40. Cont c;
  41. T* t = new T;
  42. c.insert( t );
  43. typename Cont::local_iterator i = c.begin( 0 );
  44. typename Cont::const_local_iterator ci = i;
  45. ci = c.cbegin( 0 );
  46. i = c.end( 0 );
  47. ci = c.cend( 0 );
  48. typename Cont::size_type s = c.bucket_count();
  49. hide_warning(s);
  50. s = c.max_bucket_count();
  51. s = c.bucket_size( 0 );
  52. s = c.bucket( *t );
  53. float f = c.load_factor();
  54. f = c.max_load_factor();
  55. c.max_load_factor(f);
  56. c.rehash(1000);
  57. }
  58. template< class PtrSet >
  59. void test_erase()
  60. {
  61. PtrSet s;
  62. typedef typename PtrSet::key_type T;
  63. T t;
  64. s.insert ( new T );
  65. T* t2 = t.clone();
  66. s.insert ( t2 );
  67. s.insert ( new T );
  68. BOOST_CHECK_EQUAL( s.size(), 3u );
  69. BOOST_CHECK_EQUAL( hash_value(t), hash_value(*t2) );
  70. BOOST_CHECK_EQUAL( t, *t2 );
  71. typename PtrSet::iterator i = s.find( t );
  72. BOOST_CHECK( i != s.end() );
  73. unsigned n = s.erase( t );
  74. BOOST_CHECK( n > 0 );
  75. }
  76. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  77. #pragma GCC diagnostic push
  78. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  79. #endif
  80. void test_set()
  81. {
  82. srand( 0 );
  83. ptr_set_test< ptr_unordered_set<Base>, Base, Derived_class, false >();
  84. ptr_set_test< ptr_unordered_set<Value>, Value, Value, false >();
  85. ptr_set_test< ptr_unordered_multiset<Base>, Base, Derived_class, false >();
  86. ptr_set_test< ptr_unordered_multiset<Value>, Value, Value, false >();
  87. test_copy< ptr_unordered_set<Base>, ptr_unordered_set<Derived_class>,
  88. Derived_class>();
  89. test_copy< ptr_unordered_multiset<Base>, ptr_unordered_multiset<Derived_class>,
  90. Derived_class>();
  91. test_transfer< ptr_unordered_set<Derived_class>, ptr_unordered_set<Base>, Derived_class>();
  92. test_transfer< ptr_unordered_multiset<Derived_class>, ptr_unordered_multiset<Base>, Derived_class>();
  93. ptr_unordered_set<int> set;
  94. BOOST_CHECK_THROW( set.insert( 0 ), bad_ptr_container_operation );
  95. set.insert( new int(0) );
  96. #ifndef BOOST_NO_AUTO_PTR
  97. std::auto_ptr<int> ap( new int(1) );
  98. set.insert( ap );
  99. #endif
  100. #ifndef BOOST_NO_CXX11_SMART_PTR
  101. std::unique_ptr<int> up( new int(2) );
  102. set.insert( std::move( up ) );
  103. #endif
  104. BOOST_CHECK_THROW( (set.replace(set.begin(), 0 )), bad_ptr_container_operation );
  105. #ifndef BOOST_NO_AUTO_PTR
  106. BOOST_CHECK_THROW( (set.replace(set.begin(), std::auto_ptr<int>(0) )), bad_ptr_container_operation );
  107. #endif
  108. #if !defined(BOOST_NO_CXX11_SMART_PTR) && !defined(BOOST_NO_CXX11_NULLPTR)
  109. BOOST_CHECK_THROW( (set.replace(set.begin(), std::unique_ptr<int>(nullptr) )), bad_ptr_container_operation );
  110. #endif
  111. test_unordered_interface< ptr_unordered_set<Base>, Derived_class >();
  112. test_unordered_interface< ptr_unordered_multiset<Base>, Derived_class >();
  113. test_erase< ptr_unordered_set<Base> >();
  114. test_erase< ptr_unordered_multiset<Base> >();
  115. }
  116. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  117. #pragma GCC diagnostic pop
  118. #endif
  119. using boost::unit_test::test_suite;
  120. test_suite* init_unit_test_suite( int argc, char* argv[] )
  121. {
  122. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  123. test->add( BOOST_TEST_CASE( &test_set ) );
  124. return test;
  125. }