sequence_test_data.hpp 9.7 KB

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