• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "associative_test_data.hpp"
14 #include <boost/ptr_container/ptr_unordered_set.hpp>
15 #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
16 
17 template< class SetDerived, class SetBase, class T >
test_transfer()18 void test_transfer()
19 {
20     SetBase to;
21     SetDerived from;
22     from.insert( new T );
23     from.insert( new T );
24     transfer_test( from, to );
25 }
26 
27 template< class BaseContainer, class DerivedContainer, class Derived >
test_copy()28 void test_copy()
29 {
30     DerivedContainer derived;
31     derived.insert( new Derived );
32     derived.insert( new Derived );
33 
34     BaseContainer base( derived );
35     BOOST_CHECK_EQUAL( derived.size(), base.size() );
36     base.clear();
37     base = derived;
38     BOOST_CHECK_EQUAL( derived.size(), base.size() );
39     base = base;
40 }
41 
42 template< class Cont, class T >
test_unordered_interface()43 void test_unordered_interface()
44 {
45     Cont c;
46     T* t = new T;
47     c.insert( t );
48     typename Cont::local_iterator i = c.begin( 0 );
49     typename Cont::const_local_iterator ci = i;
50     ci = c.cbegin( 0 );
51     i = c.end( 0 );
52     ci = c.cend( 0 );
53     typename Cont::size_type s = c.bucket_count();
54     hide_warning(s);
55     s = c.max_bucket_count();
56     s = c.bucket_size( 0 );
57     s = c.bucket( *t );
58     float f = c.load_factor();
59     f       = c.max_load_factor();
60     c.max_load_factor(f);
61     c.rehash(1000);
62 }
63 
64 
65 
66 template< class PtrSet >
test_erase()67 void test_erase()
68 {
69     PtrSet s;
70     typedef typename PtrSet::key_type T;
71 
72     T t;
73     s.insert ( new T );
74     T* t2 = t.clone();
75     s.insert ( t2 );
76     s.insert ( new T );
77     BOOST_CHECK_EQUAL( s.size(), 3u );
78     BOOST_CHECK_EQUAL( hash_value(t), hash_value(*t2) );
79     BOOST_CHECK_EQUAL( t, *t2 );
80 
81     typename PtrSet::iterator i = s.find( t );
82 
83     BOOST_CHECK( i != s.end() );
84     unsigned n = s.erase( t );
85     BOOST_CHECK( n > 0 );
86 }
87 
88 
89 #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
90 #pragma GCC diagnostic push
91 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
92 #endif
93 
test_set()94 void test_set()
95 {
96     srand( 0 );
97     ptr_set_test< ptr_unordered_set<Base>, Base, Derived_class, false >();
98     ptr_set_test< ptr_unordered_set<Value>, Value, Value, false >();
99 
100     ptr_set_test< ptr_unordered_multiset<Base>, Base, Derived_class, false >();
101     ptr_set_test< ptr_unordered_multiset<Value>, Value, Value, false >();
102 
103     test_copy< ptr_unordered_set<Base>, ptr_unordered_set<Derived_class>,
104                Derived_class>();
105     test_copy< ptr_unordered_multiset<Base>, ptr_unordered_multiset<Derived_class>,
106                Derived_class>();
107 
108     test_transfer< ptr_unordered_set<Derived_class>, ptr_unordered_set<Base>, Derived_class>();
109     test_transfer< ptr_unordered_multiset<Derived_class>, ptr_unordered_multiset<Base>, Derived_class>();
110 
111     ptr_unordered_set<int> set;
112 
113     BOOST_CHECK_THROW( set.insert( 0 ), bad_ptr_container_operation );
114     set.insert( new int(0) );
115 #ifndef BOOST_NO_AUTO_PTR
116     std::auto_ptr<int> ap( new int(1) );
117     set.insert( ap );
118 #endif
119 #ifndef BOOST_NO_CXX11_SMART_PTR
120     std::unique_ptr<int> up( new int(2) );
121     set.insert( std::move( up ) );
122 #endif
123     BOOST_CHECK_THROW( (set.replace(set.begin(), 0 )), bad_ptr_container_operation );
124 #ifndef BOOST_NO_AUTO_PTR
125     BOOST_CHECK_THROW( (set.replace(set.begin(), std::auto_ptr<int>(0) )), bad_ptr_container_operation );
126 #endif
127 #if !defined(BOOST_NO_CXX11_SMART_PTR) && !defined(BOOST_NO_CXX11_NULLPTR)
128     BOOST_CHECK_THROW( (set.replace(set.begin(), std::unique_ptr<int>(nullptr) )), bad_ptr_container_operation );
129 #endif
130 
131     test_unordered_interface< ptr_unordered_set<Base>, Derived_class >();
132     test_unordered_interface< ptr_unordered_multiset<Base>, Derived_class >();
133 
134     test_erase< ptr_unordered_set<Base> >();
135     test_erase< ptr_unordered_multiset<Base> >();
136 }
137 
138 #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
139 #pragma GCC diagnostic pop
140 #endif
141 
142 using boost::unit_test::test_suite;
143 
init_unit_test_suite(int argc,char * argv[])144 test_suite* init_unit_test_suite( int argc, char* argv[] )
145 {
146     test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
147 
148     test->add( BOOST_TEST_CASE( &test_set ) );
149 
150     return test;
151 }
152