| 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 "test_data.hpp" |
| 13 | #include <boost/ptr_container/exception.hpp> |
| 14 | #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp> |
| 15 | #include <boost/range/sub_range.hpp> |
| 16 | |
| 17 | #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED) |
| 18 | #pragma GCC diagnostic push |
| 19 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 20 | #endif |
| 21 | |
| 22 | template< typename C, typename B, typename T, bool Ordered > |
| 23 | void ptr_set_test(); |
| 24 | |
| 25 | template< class T, bool Ordered > |
| 26 | struct test_algorithms |
| 27 | { |
| 28 | template< class Cont > |
| 29 | void operator()( Cont& c, const Cont& c2 ) const |
| 30 | { |
| 31 | typename Cont::iterator i; |
| 32 | typename Cont::const_iterator ci; |
| 33 | |
| 34 | T* t = new T; |
| 35 | i = c.lower_bound( *t ); |
| 36 | ci = c2.lower_bound( *t ); |
| 37 | i = c.upper_bound( *t ); |
| 38 | ci = c2.upper_bound( *t ); |
| 39 | delete t; |
| 40 | |
| 41 | BOOST_DEDUCED_TYPENAME Cont::reverse_iterator ri = c.rbegin(); |
| 42 | hide_warning(ri); |
| 43 | BOOST_DEDUCED_TYPENAME Cont::const_reverse_iterator cri = c2.rbegin(); |
| 44 | BOOST_DEDUCED_TYPENAME Cont::reverse_iterator rv2 = c.rend(); |
| 45 | hide_warning(rv2); |
| 46 | BOOST_DEDUCED_TYPENAME Cont::const_reverse_iterator cvr2 = c2.rend(); |
| 47 | hide_warning(cvr2); |
| 48 | cri = c.crbegin(); |
| 49 | cri = c.crend(); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | template< class T > |
| 54 | struct test_algorithms<T,false> |
| 55 | { |
| 56 | template< class Cont> |
| 57 | void operator()( Cont& c, const Cont& c2 ) const |
| 58 | { |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | template< typename C, typename B, typename T, bool Ordered > |
| 63 | void ptr_set_test() |
| 64 | { |
| 65 | using namespace boost; |
| 66 | |
| 67 | BOOST_TEST_MESSAGE( "starting associative container test" ); |
| 68 | enum { max_cnt = 10, size = 100 }; |
| 69 | C c; |
| 70 | BOOST_CHECK( c.size() == 0 ); |
| 71 | c.insert( c.end(), new T ); |
| 72 | c.insert( c.end(), new T ); |
| 73 | |
| 74 | const C c2( c.begin(), c.end() ); |
| 75 | BOOST_CHECK( c.size() == c2.size() ); |
| 76 | |
| 77 | C c3; |
| 78 | |
| 79 | BOOST_TEST_MESSAGE( "finished construction test" ); |
| 80 | |
| 81 | C a_copy( c ); |
| 82 | BOOST_CHECK_EQUAL( a_copy.size(), c.size() ); |
| 83 | a_copy = a_copy; |
| 84 | BOOST_CHECK_EQUAL( a_copy.size(), c.size() ); |
| 85 | c.clear(); |
| 86 | a_copy = c; |
| 87 | a_copy = a_copy; |
| 88 | BOOST_CHECK( a_copy.empty() ); |
| 89 | |
| 90 | BOOST_TEST_MESSAGE( "finished copying test" ); |
| 91 | |
| 92 | BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator(); |
| 93 | BOOST_DEDUCED_TYPENAME C::iterator i = c.begin(); |
| 94 | BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin(); |
| 95 | ci = c.cbegin(); |
| 96 | ci = c.cend(); |
| 97 | BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end(); |
| 98 | hide_warning(i2); |
| 99 | BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin(); |
| 100 | hide_warning(ci2); |
| 101 | |
| 102 | BOOST_TEST_MESSAGE( "finished iterator test" ); |
| 103 | |
| 104 | BOOST_DEDUCED_TYPENAME C::size_type s = c.size(); |
| 105 | BOOST_DEDUCED_TYPENAME C::size_type s2 = c.max_size(); |
| 106 | hide_warning(s2); |
| 107 | BOOST_CHECK_EQUAL( c.size(), s ); |
| 108 | bool b = c.empty(); |
| 109 | hide_warning(b); |
| 110 | BOOST_TEST_MESSAGE( "finished accessors test" ); |
| 111 | |
| 112 | T* t = new T; |
| 113 | c.insert( c.end(), t ); |
| 114 | c.insert( new T ); |
| 115 | #ifndef BOOST_NO_AUTO_PTR |
| 116 | c.insert( c.end(), std::auto_ptr<T>( new T ) ); |
| 117 | std::auto_ptr<T> ap( new T ); |
| 118 | c.insert( ap ); |
| 119 | #endif |
| 120 | #ifndef BOOST_NO_CXX11_SMART_PTR |
| 121 | c.insert( c.end(), std::unique_ptr<T>( new T ) ); |
| 122 | std::unique_ptr<T> up( new T ); |
| 123 | c.insert( std::move( up ) ); |
| 124 | #endif |
| 125 | c3.insert( c.begin(), c.end() ); |
| 126 | c.erase( c.begin() ); |
| 127 | c3.erase( c3.begin(), c3.end() ); |
| 128 | t = new T; |
| 129 | c.insert( new T ); |
| 130 | c.erase( *t ); |
| 131 | delete t; |
| 132 | |
| 133 | BOOST_CHECK( c3.empty() ); |
| 134 | c.swap( c3 ); |
| 135 | BOOST_CHECK( !c3.empty() ); |
| 136 | BOOST_CHECK( c.empty() ); |
| 137 | c3.clear(); |
| 138 | |
| 139 | // |
| 140 | // remark: we cannot pass c3 directly as it would |
| 141 | // extract const iterators ... and the |
| 142 | // current standard does not allow erase() |
| 143 | // to be given const iterators |
| 144 | // |
| 145 | c3.erase( boost::make_iterator_range(c3) ); |
| 146 | BOOST_CHECK( c3.empty() ); |
| 147 | BOOST_TEST_MESSAGE( "finished modifiers test" ); |
| 148 | |
| 149 | c.insert( c.end(), new T ); |
| 150 | typename C::auto_type ptr2 = c.release( c.begin() ); |
| 151 | #ifndef BOOST_NO_AUTO_PTR |
| 152 | std::auto_ptr<C> ap2 = c.release(); |
| 153 | #else |
| 154 | std::unique_ptr<C> up2 = c.release(); |
| 155 | #endif |
| 156 | c = c2.clone(); |
| 157 | BOOST_TEST_MESSAGE( "finished release/clone test" ); |
| 158 | |
| 159 | c3.insert( new T ); |
| 160 | c3.insert( new T ); |
| 161 | BOOST_CHECK_EQUAL( c3.size(), 2u ); |
| 162 | #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
| 163 | #else |
| 164 | c3.insert( make_iterator_range( c ) ); |
| 165 | // BOOST_CHECK_EQUAL( c3.size(), 4u ); |
| 166 | #endif |
| 167 | c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), c3 ); |
| 168 | BOOST_CHECK( c3.empty() == false ); |
| 169 | c.clear(); |
| 170 | unsigned long c3size = c3.size(); |
| 171 | hide_warning( c3size ); |
| 172 | unsigned long num = c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), |
| 173 | c3.end(), |
| 174 | c3 ); |
| 175 | |
| 176 | BOOST_CHECK( num > 0 ); |
| 177 | BOOST_CHECK_EQUAL( num, c.size() ); |
| 178 | BOOST_CHECK( c3.empty() ); |
| 179 | BOOST_CHECK( !c.empty() ); |
| 180 | c3. BOOST_NESTED_TEMPLATE transfer<C>( c ); |
| 181 | BOOST_CHECK( !c3.empty() ); |
| 182 | BOOST_CHECK( c.empty() ); |
| 183 | #ifdef BOOST_NO_SFINAE |
| 184 | #else |
| 185 | c. BOOST_NESTED_TEMPLATE transfer<C>( make_iterator_range( c3 ), c3 ); |
| 186 | BOOST_CHECK( !c.empty() ); |
| 187 | BOOST_CHECK( c3.empty() ); |
| 188 | #endif |
| 189 | |
| 190 | BOOST_TEST_MESSAGE( "finished transfer test" ); |
| 191 | |
| 192 | C c4; |
| 193 | c4.swap(c3); |
| 194 | swap(c4,c3); |
| 195 | BOOST_TEST_MESSAGE( "finished set/map interface test" ); |
| 196 | |
| 197 | sub_range<C> sub; |
| 198 | sub_range<const C> csub; |
| 199 | |
| 200 | t = new T; |
| 201 | i = c.find( *t ); |
| 202 | ci = c2.find( *t ); |
| 203 | c2.count( *t ); |
| 204 | |
| 205 | test_algorithms<T,Ordered>()( c, c2 ); |
| 206 | sub = c.equal_range( *t ); |
| 207 | csub = c2.equal_range( *t ); |
| 208 | delete t; |
| 209 | |
| 210 | BOOST_TEST_MESSAGE( "finished algorithms interface test" ); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED) |
| 215 | #pragma GCC diagnostic pop |
| 216 | #endif |
| 217 | |