| 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/detail/ptr_container_disable_deprecated.hpp> |
| 14 | #include <boost/range/iterator_range.hpp> |
| 15 | #include <boost/bind/bind.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 > |
| 23 | void reversible_container_test(); |
| 24 | |
| 25 | template< class IntContainer > |
| 26 | void algorithms_test(); |
| 27 | |
| 28 | template< typename C, typename B, typename T > |
| 29 | void reversible_container_test() |
| 30 | { |
| 31 | using namespace boost; |
| 32 | |
| 33 | BOOST_TEST_MESSAGE( "starting reversible container test" ); |
| 34 | enum { max_cnt = 10 }; |
| 35 | C c; |
| 36 | set_capacity<C>()( c ); |
| 37 | BOOST_CHECK( c.size() == 0 ); |
| 38 | c.push_back( new T ); |
| 39 | BOOST_CHECK( c.size() == 1 ); |
| 40 | |
| 41 | const C c2_dummy( c.begin(), c.end() ); |
| 42 | BOOST_CHECK_EQUAL( c2_dummy.size(), c.size() ); |
| 43 | const C c2( c.clone() ); |
| 44 | BOOST_CHECK_EQUAL( c2.size(), c.size() ); |
| 45 | |
| 46 | C c3( c.begin(), c.end() ); |
| 47 | set_capacity<C>()( c3 ); |
| 48 | BOOST_CHECK_EQUAL( c.size(), c3.size() ); |
| 49 | |
| 50 | c.assign( c3.begin(), c3.end() ); |
| 51 | BOOST_CHECK_EQUAL( c.size(), c3.size() ); |
| 52 | |
| 53 | c.assign( c3 ); |
| 54 | set_capacity<C>()( c ); |
| 55 | BOOST_TEST_MESSAGE( "finished construction test" ); |
| 56 | |
| 57 | C a_copy( c ); |
| 58 | BOOST_CHECK_EQUAL( a_copy.size(), c.size() ); |
| 59 | a_copy = a_copy; |
| 60 | BOOST_CHECK_EQUAL( a_copy.size(), c.size() ); |
| 61 | a_copy.clear(); |
| 62 | a_copy = a_copy; |
| 63 | BOOST_CHECK( a_copy.empty() ); |
| 64 | BOOST_CHECK( !c.empty() ); |
| 65 | BOOST_TEST_MESSAGE( "finished copying test" ); |
| 66 | |
| 67 | BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator(); |
| 68 | hide_warning(alloc); |
| 69 | BOOST_DEDUCED_TYPENAME C::iterator i = c.begin(); |
| 70 | BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin(); |
| 71 | BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end(); |
| 72 | hide_warning(i2); |
| 73 | BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin(); |
| 74 | hide_warning(ci2); |
| 75 | BOOST_DEDUCED_TYPENAME C::reverse_iterator ri = c.rbegin(); |
| 76 | hide_warning(ri); |
| 77 | BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri = c2.rbegin(); |
| 78 | hide_warning(cri); |
| 79 | BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2 = c.rend(); |
| 80 | hide_warning(rv2); |
| 81 | BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend(); |
| 82 | hide_warning(cvr2); |
| 83 | i = c.rbegin().base(); |
| 84 | ci = c2.rbegin().base(); |
| 85 | i = c.rend().base(); |
| 86 | ci = c2.rend().base(); |
| 87 | BOOST_CHECK_EQUAL( std::distance( c.rbegin(), c.rend() ), |
| 88 | std::distance( c.begin(), c.end() ) ); |
| 89 | |
| 90 | BOOST_TEST_MESSAGE( "finished iterator test" ); |
| 91 | |
| 92 | BOOST_DEDUCED_TYPENAME C::size_type s = c.size(); |
| 93 | hide_warning(s); |
| 94 | BOOST_DEDUCED_TYPENAME C::size_type s2 = c.max_size(); |
| 95 | hide_warning(s2); |
| 96 | c.push_back( new T ); |
| 97 | std::size_t size = 2u; |
| 98 | #ifndef BOOST_NO_AUTO_PTR |
| 99 | c.push_back( std::auto_ptr<T>( new T ) ); |
| 100 | ++size; |
| 101 | #endif |
| 102 | #ifndef BOOST_NO_CXX11_SMART_PTR |
| 103 | c.push_back( std::unique_ptr<T>( new T ) ); |
| 104 | ++size; |
| 105 | #endif |
| 106 | bool b = c.empty(); |
| 107 | BOOST_CHECK( !c.empty() ); |
| 108 | b = is_null( c.begin() ); |
| 109 | BOOST_CHECK( b == false ); |
| 110 | BOOST_DEDUCED_TYPENAME C::reference r = c.front(); |
| 111 | hide_warning(r); |
| 112 | BOOST_DEDUCED_TYPENAME C::const_reference cr = c2.front(); |
| 113 | hide_warning(cr); |
| 114 | BOOST_DEDUCED_TYPENAME C::reference r2 = c.back(); |
| 115 | hide_warning(r2); |
| 116 | BOOST_DEDUCED_TYPENAME C::const_reference cr2 = c2.back(); |
| 117 | hide_warning(cr2); |
| 118 | BOOST_TEST_MESSAGE( "finished accessors test" ); |
| 119 | |
| 120 | c.push_back( new T ); |
| 121 | ++size; |
| 122 | BOOST_CHECK_EQUAL( c.size(), size ); |
| 123 | |
| 124 | c.pop_back(); |
| 125 | BOOST_CHECK( !c.empty() ); |
| 126 | c.insert( c.end(), new T ); |
| 127 | #ifndef BOOST_NO_AUTO_PTR |
| 128 | std::auto_ptr<T> ap(new T); |
| 129 | c.insert( c.end(), ap ); |
| 130 | ++size; |
| 131 | #endif |
| 132 | #ifndef BOOST_NO_CXX11_SMART_PTR |
| 133 | std::unique_ptr<T> up( new T ); |
| 134 | c.insert( c.end(), std::move( up ) ); |
| 135 | ++size; |
| 136 | #endif |
| 137 | BOOST_CHECK_EQUAL( c.size(), size ); |
| 138 | |
| 139 | #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
| 140 | #else |
| 141 | c.insert( c.end(), c3 ); |
| 142 | #endif |
| 143 | c3.insert( c3.end(), c.begin(), c.end() ); |
| 144 | c.erase( c.begin() ); |
| 145 | c3.erase( c3.begin(), c3.end() ); |
| 146 | c3.erase( boost::make_iterator_range(c3) ); |
| 147 | BOOST_CHECK( c3.empty() ); |
| 148 | BOOST_CHECK( !c.empty() ); |
| 149 | c.swap( c3 ); |
| 150 | BOOST_CHECK( !c3.empty() ); |
| 151 | c3.clear(); |
| 152 | BOOST_CHECK( c3.empty() ); |
| 153 | C c4; |
| 154 | c4.swap(c3); |
| 155 | #ifdef BOOST_NO_SFINAE |
| 156 | #else |
| 157 | swap(c4,c3); |
| 158 | #endif |
| 159 | BOOST_TEST_MESSAGE( "finished modifiers test" ); |
| 160 | |
| 161 | c.push_back( new T ); c.push_back( new T ); c.push_back( new T ); |
| 162 | typedef BOOST_DEDUCED_TYPENAME C::auto_type auto_type; |
| 163 | |
| 164 | #ifdef BOOST_NO_SFINAE |
| 165 | #else |
| 166 | auto_type ptr = c.release( c.begin() ); |
| 167 | #endif |
| 168 | #ifndef BOOST_NO_AUTO_PTR |
| 169 | std::auto_ptr<C> ap2 = c.release(); |
| 170 | #else |
| 171 | std::unique_ptr<C> up2 = c.release(); |
| 172 | #endif |
| 173 | c = c2.clone(); |
| 174 | BOOST_CHECK( !c.empty() ); |
| 175 | auto_type ptr2 = c.replace( c.begin(), new T ); |
| 176 | #ifndef BOOST_NO_AUTO_PTR |
| 177 | ptr2 = c.replace( c.begin(), std::auto_ptr<T>( new T ) ); |
| 178 | #endif |
| 179 | #ifndef BOOST_NO_CXX11_SMART_PTR |
| 180 | ptr2 = c.replace( c.begin(), std::unique_ptr<T>( new T ) ); |
| 181 | #endif |
| 182 | BOOST_TEST_MESSAGE( "finished release/clone/replace test" ); |
| 183 | |
| 184 | c3.push_back( new T ); |
| 185 | c3.push_back( new T ); |
| 186 | c3.push_back( new T ); |
| 187 | c. BOOST_NESTED_TEMPLATE transfer<C>( c.begin(), c3.begin(), c3 ); |
| 188 | c. BOOST_NESTED_TEMPLATE transfer<C>( c.end(), c3.begin(), c3.end(), c3 ); |
| 189 | #ifdef BOOST_NO_SFINAE |
| 190 | #else |
| 191 | c. BOOST_NESTED_TEMPLATE transfer<C>( c.end(), boost::make_iterator_range( c3 ), c3 ); |
| 192 | BOOST_CHECK( c3.empty() ); |
| 193 | BOOST_CHECK( !c.empty() ); |
| 194 | #endif |
| 195 | c3. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), c ); |
| 196 | BOOST_CHECK( !c3.empty() ); |
| 197 | BOOST_CHECK( c.empty() ); |
| 198 | BOOST_TEST_MESSAGE( "finished transfer test" ); |
| 199 | |
| 200 | c3.resize( 0u ); |
| 201 | BOOST_CHECK( c3.empty() ); |
| 202 | c3.resize( 10u ); |
| 203 | BOOST_CHECK_EQUAL( c3.size(), 10u ); |
| 204 | c3.resize( 12u, &*c3.begin() ); |
| 205 | BOOST_CHECK_EQUAL( c3.size(), 12u ); |
| 206 | BOOST_TEST_MESSAGE( "finished resize test" ); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | template< class CDerived, class CBase, class T > |
| 213 | void test_transfer() |
| 214 | { |
| 215 | BOOST_TEST_MESSAGE( "starting transfer test" ); |
| 216 | CDerived from; |
| 217 | CBase to; |
| 218 | |
| 219 | set_capacity<CDerived>()( from ); |
| 220 | set_capacity<CBase>()( to ); |
| 221 | |
| 222 | from.push_back( new T ); |
| 223 | from.push_back( new T ); |
| 224 | to. BOOST_NESTED_TEMPLATE transfer<CDerived>( to.end(), from ); |
| 225 | BOOST_TEST_MESSAGE( "finished transfer test" ); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | #include <boost/assign/list_inserter.hpp> |
| 231 | #include <iostream> |
| 232 | |
| 233 | template< class Compare, class C > |
| 234 | bool is_sorted( const C& c ) |
| 235 | { |
| 236 | if( c.size() < 2 ) |
| 237 | return true; |
| 238 | |
| 239 | typename C::const_iterator prev = c.begin(), |
| 240 | e = c.end(), |
| 241 | next = prev; |
| 242 | Compare pred; |
| 243 | for( ; next != e ; ) |
| 244 | { |
| 245 | prev = next; |
| 246 | ++next; |
| 247 | |
| 248 | if( next == c.end() ) |
| 249 | return true; |
| 250 | |
| 251 | if( !pred(*prev,*next) ) |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | struct equal_to_int |
| 261 | { |
| 262 | int i_; |
| 263 | |
| 264 | equal_to_int( int i ) : i_(i) |
| 265 | { } |
| 266 | |
| 267 | bool operator()( int i ) const |
| 268 | { |
| 269 | return i_ == i; |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | |
| 274 | template< class IntContainer > |
| 275 | void random_access_algorithms_test() |
| 276 | { |
| 277 | BOOST_TEST_MESSAGE( "starting random accessors algorithms test" ); |
| 278 | |
| 279 | IntContainer c; |
| 280 | set_capacity<IntContainer>()( c ); |
| 281 | assign::push_back( c ) |
| 282 | ( new int(1) ) |
| 283 | ( new int(3) ) |
| 284 | ( new int(6) ) |
| 285 | ( new int(7) ) |
| 286 | ( new int(2) ) |
| 287 | ( new int(2) ) |
| 288 | ( new int(0) ) |
| 289 | ( new int(6) ) |
| 290 | ( new int(3) ); |
| 291 | c.sort(); |
| 292 | BOOST_CHECK( is_sorted< std::less_equal<int> >( c ) ); |
| 293 | BOOST_CHECK_EQUAL( c.size(), 9u ); |
| 294 | |
| 295 | c.unique(); |
| 296 | BOOST_CHECK_EQUAL( c.size(), 6u ); |
| 297 | #ifdef PTR_LIST_TEST |
| 298 | BOOST_CHECK( c.front() == 0 ); |
| 299 | BOOST_CHECK( c.back() == 7 ); |
| 300 | #else |
| 301 | BOOST_CHECK( c[0] == 0 ); |
| 302 | BOOST_CHECK( c[1] == 1 ); |
| 303 | BOOST_CHECK( c[2] == 2 ); |
| 304 | BOOST_CHECK( c[3] == 3 ); |
| 305 | BOOST_CHECK( c[4] == 6 ); |
| 306 | BOOST_CHECK( c[5] == 7 ); |
| 307 | #endif |
| 308 | |
| 309 | c.erase_if( equal_to_int(1) ); |
| 310 | BOOST_CHECK_EQUAL( c.size(), 5u ); |
| 311 | #ifdef PTR_LIST_TEST |
| 312 | BOOST_CHECK_EQUAL( c.front(), 0 ); |
| 313 | #else |
| 314 | BOOST_CHECK_EQUAL( c[0], 0 ); |
| 315 | BOOST_CHECK_EQUAL( c[1], 2 ); |
| 316 | #endif |
| 317 | |
| 318 | c.erase_if( equal_to_int(7) ); |
| 319 | BOOST_CHECK_EQUAL( c.size(), 4u ); |
| 320 | BOOST_CHECK_EQUAL( c.back(), 6 ); |
| 321 | |
| 322 | // C = [0,2,3,6] |
| 323 | |
| 324 | IntContainer c2; |
| 325 | set_capacity<IntContainer>()( c2 ); |
| 326 | assign::push_back( c2 ) |
| 327 | ( new int(-1) ) |
| 328 | ( new int(1) ) |
| 329 | ( new int(4) ) |
| 330 | ( new int(5) ) |
| 331 | ( new int(7) ); |
| 332 | BOOST_CHECK( c2.size() == 5u ); |
| 333 | c.merge( c2 ); |
| 334 | BOOST_CHECK( c2.empty() ); |
| 335 | BOOST_CHECK( c.size() == 9u ); |
| 336 | BOOST_CHECK( is_sorted< std::less_equal<int> >( c ) ); |
| 337 | BOOST_TEST_MESSAGE( "finished random accessors algorithms test" ); |
| 338 | } |
| 339 | |
| 340 | #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED) |
| 341 | #pragma GCC diagnostic pop |
| 342 | #endif |
| 343 | |