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