ptr_unordered_map.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2008. 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/test/unit_test.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. #include <boost/cast.hpp>
  17. #include <cstdlib>
  18. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. //
  22. // abstract base class definition
  23. //
  24. struct abstract_base
  25. {
  26. virtual ~abstract_base() {}
  27. virtual void foo() = 0;
  28. virtual abstract_base* clone() const = 0;
  29. };
  30. struct implementation : abstract_base
  31. {
  32. implementation()
  33. { }
  34. implementation( const implementation& )
  35. { }
  36. implementation( int, std::string, int, std::string )
  37. { }
  38. virtual void foo() {}
  39. virtual abstract_base* clone() const
  40. {
  41. return new implementation( *this );
  42. }
  43. };
  44. inline std::ostream& operator<<( std::ostream& out, const abstract_base& r )
  45. {
  46. return out;
  47. }
  48. inline abstract_base* new_clone( const abstract_base& r )
  49. {
  50. return r.clone();
  51. }
  52. inline std::size_t hash_value( const abstract_base& b )
  53. {
  54. return boost::hash_value( &b );
  55. }
  56. //
  57. // ptr_map test
  58. //
  59. template< typename C, typename B, typename T >
  60. void ptr_map_test();
  61. template< class Key >
  62. Key get_next_key( const Key& k );
  63. template<>
  64. int get_next_key<int>( const int& )
  65. {
  66. return rand();
  67. }
  68. template<>
  69. std::string get_next_key<std::string>( const std::string& )
  70. {
  71. return boost::lexical_cast<std::string>( rand() );
  72. }
  73. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  74. #pragma GCC diagnostic push
  75. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  76. #endif
  77. template< typename C, typename B, typename T >
  78. void ptr_map_test()
  79. {
  80. using namespace boost;
  81. BOOST_TEST_MESSAGE( "starting associative container test" );
  82. enum { max_cnt = 10, size = 100 };
  83. C c;
  84. BOOST_CHECK( c.size() == 0 );
  85. const C c2( c.begin(), c.end() );
  86. BOOST_CHECK( c.size() == c2.size() );
  87. C c3;
  88. BOOST_TEST_MESSAGE( "finished construction test" );
  89. BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator();
  90. BOOST_DEDUCED_TYPENAME C::iterator i = c.begin();
  91. BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
  92. BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
  93. hide_warning(i2);
  94. BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin();
  95. hide_warning(ci2);
  96. ci = c.cbegin();
  97. ci = c.cend();
  98. BOOST_DEDUCED_TYPENAME C::key_type a_key;
  99. BOOST_TEST_MESSAGE( "finished iterator test" );
  100. BOOST_DEDUCED_TYPENAME C::size_type s = c.size();
  101. BOOST_DEDUCED_TYPENAME C::size_type s2 = c.max_size();
  102. hide_warning(s2);
  103. BOOST_CHECK_EQUAL( c.size(), s );
  104. bool b = c.empty();
  105. hide_warning(b);
  106. BOOST_TEST_MESSAGE( "finished accessors test" );
  107. a_key = get_next_key( a_key );
  108. c.insert( a_key, new T );
  109. a_key = get_next_key( a_key );
  110. c.insert( a_key, new T );
  111. c3.insert( c.begin(), c.end() );
  112. c.insert( c3 );
  113. c.erase( c.begin() );
  114. BOOST_CHECK( c3.end() == c3.erase( boost::make_iterator_range(c3) ) );
  115. c3.erase( a_key );
  116. BOOST_CHECK( c3.empty() );
  117. c.swap( c3 );
  118. swap(c,c3);
  119. swap(c3,c);
  120. BOOST_CHECK( !c3.empty() );
  121. c3.clear();
  122. BOOST_CHECK( c3.empty() );
  123. BOOST_TEST_MESSAGE( "finished modifiers test" );
  124. a_key = get_next_key( a_key );
  125. c.insert( a_key, new T );
  126. a_key = get_next_key( a_key );
  127. #ifndef BOOST_NO_AUTO_PTR
  128. c.insert( a_key, std::auto_ptr<T>( new T ) );
  129. #endif
  130. #ifndef BOOST_NO_CXX11_SMART_PTR
  131. c.insert( a_key, std::unique_ptr<T>( new T ) );
  132. #endif
  133. typename C::auto_type ptr2 = c.release( c.begin() );
  134. #ifndef BOOST_NO_AUTO_PTR
  135. std::auto_ptr<C> ap = c.release();
  136. #else
  137. std::unique_ptr<C> up = c.release();
  138. #endif
  139. c = c2.clone();
  140. BOOST_TEST_MESSAGE( "finished release/clone test" );
  141. a_key = get_next_key( a_key );
  142. c3.insert( a_key, new T );
  143. a_key = get_next_key( a_key );
  144. c3.insert( a_key, new T );
  145. c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), c3 );
  146. c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), c3.end(), c3 );
  147. BOOST_CHECK( c3.empty() );
  148. BOOST_CHECK( !c.empty() );
  149. c3. BOOST_NESTED_TEMPLATE transfer<C>( c );
  150. BOOST_CHECK( !c3.empty() );
  151. BOOST_CHECK( c.empty() );
  152. #ifdef BOOST_NO_SFINAE
  153. #else
  154. c. BOOST_NESTED_TEMPLATE transfer<C>( make_iterator_range(c3), c3 );
  155. BOOST_CHECK( !c.empty() );
  156. BOOST_CHECK( c3.empty() );
  157. c3. BOOST_NESTED_TEMPLATE transfer<C>(c);
  158. #endif
  159. BOOST_TEST_MESSAGE( "finished transfer test" );
  160. BOOST_CHECK( !c3.empty() );
  161. c3.replace( c3.begin(), new T );
  162. #ifndef BOOST_NO_AUTO_PTR
  163. c3.replace( c3.begin(), std::auto_ptr<T>( new T ) );
  164. #endif
  165. #ifndef BOOST_NO_CXX11_SMART_PTR
  166. c3.replace( c3.begin(), std::unique_ptr<T>( new T ) );
  167. #endif
  168. BOOST_TEST_MESSAGE( "finished set/map interface test" );
  169. // @todo: make macro with algorithms so that the right erase() is called.
  170. // c.unique();
  171. // c.unique( std::not_equal_to<T>() );
  172. // c.remove( T() );
  173. // c.remove_if( std::binder1st< std::equal_to<T> >( T() ) );
  174. sub_range<C> sub;
  175. sub_range<const C> csub;
  176. i = c.find( get_next_key( a_key ) );
  177. ci = c2.find( get_next_key( a_key ) );
  178. c2.count( get_next_key( a_key ) );
  179. sub = c.equal_range( get_next_key( a_key ) );
  180. csub = c2.equal_range( get_next_key( a_key ) );
  181. try
  182. {
  183. c.at( get_next_key( a_key ) );
  184. }
  185. catch( const bad_ptr_container_operation& )
  186. { }
  187. try
  188. {
  189. c2.at( get_next_key( a_key ) );
  190. }
  191. catch( const bad_ptr_container_operation& )
  192. { }
  193. BOOST_TEST_MESSAGE( "finished algorithms interface test" );
  194. typename C::iterator it = c.begin(), e = c.end();
  195. for( ; it != e; ++it )
  196. {
  197. std::cout << "\n mapped value = " << *it->second << " key = " << it->first;
  198. //std::cout << "\n mapped value = " << it.value() << " key = " << it.key();
  199. }
  200. BOOST_TEST_MESSAGE( "finished iterator test" );
  201. a_key = get_next_key( a_key );
  202. c.insert( a_key, new T );
  203. c.erase( a_key );
  204. c.erase( a_key );
  205. }
  206. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  207. #pragma GCC diagnostic pop
  208. #endif
  209. template< class CDerived, class CBase, class T >
  210. void test_transfer()
  211. {
  212. CDerived from;
  213. CBase to;
  214. int key = get_next_key( key );
  215. from.insert( key, new T );
  216. key = get_next_key( key );
  217. from.insert( key, new T );
  218. transfer_test( from, to );
  219. }
  220. template< class BaseContainer, class DerivedContainer, class Derived >
  221. void map_container_assignment_test()
  222. {
  223. DerivedContainer derived;
  224. std::string foo( "foo" );
  225. std::string bar( "foo" );
  226. derived.insert( foo, new Derived );
  227. derived.insert( bar, new Derived );
  228. BaseContainer base_container( derived );
  229. BOOST_CHECK_EQUAL( derived.size(), base_container.size() );
  230. base_container.clear();
  231. base_container = derived;
  232. BOOST_CHECK_EQUAL( derived.size(), base_container.size() );
  233. BaseContainer base2( base_container );
  234. BOOST_CHECK_EQUAL( base2.size(), base_container.size() );
  235. base2 = base_container;
  236. BOOST_CHECK_EQUAL( base2.size(), base_container.size() );
  237. base_container = base_container;
  238. }
  239. template< class Cont, class Key, class T >
  240. void test_unordered_interface()
  241. {
  242. Cont c;
  243. T* t = new T;
  244. Key key = get_next_key( key );
  245. c.insert( key, t );
  246. typename Cont::local_iterator i = c.begin( 0 );
  247. typename Cont::const_local_iterator ci = i;
  248. ci = c.cbegin( 0 );
  249. i = c.end( 0 );
  250. ci = c.cend( 0 );
  251. typename Cont::size_type s = c.bucket_count();
  252. hide_warning(s);
  253. s = c.max_bucket_count();
  254. s = c.bucket_size( 0 );
  255. s = c.bucket( key );
  256. float f = c.load_factor();
  257. f = c.max_load_factor();
  258. c.max_load_factor(f);
  259. c.rehash(1000);
  260. }
  261. #include <boost/ptr_container/ptr_unordered_map.hpp>
  262. using namespace std;
  263. void test_map()
  264. {
  265. ptr_map_test< ptr_unordered_map<int, Base>, Base, Derived_class >();
  266. ptr_map_test< ptr_unordered_map<int, Value>, Value, Value >();
  267. ptr_map_test< ptr_unordered_map<int, nullable<Base> >, Base, Derived_class >();
  268. ptr_map_test< ptr_unordered_map<int, nullable<Value> >, Value, Value >();
  269. ptr_map_test< ptr_unordered_map<int, abstract_base>, abstract_base, implementation >();
  270. ptr_map_test< ptr_unordered_multimap<int,Base>, Base, Derived_class >();
  271. ptr_map_test< ptr_unordered_multimap<int,Value>, Value, Value >();
  272. ptr_map_test< ptr_unordered_multimap<int, nullable<Base> >, Base, Derived_class >();
  273. ptr_map_test< ptr_unordered_multimap<int, nullable<Value> >, Value, Value >();
  274. map_container_assignment_test< ptr_unordered_map<std::string,Base>,
  275. ptr_unordered_map<std::string,Derived_class>,
  276. Derived_class>();
  277. map_container_assignment_test< ptr_unordered_map<std::string, nullable<Base> >,
  278. ptr_unordered_map<std::string,Derived_class>,
  279. Derived_class>();
  280. map_container_assignment_test< ptr_unordered_map<std::string, nullable<Base> >,
  281. ptr_unordered_map<std::string, nullable<Derived_class> >,
  282. Derived_class>();
  283. map_container_assignment_test< ptr_unordered_multimap<std::string,Base>,
  284. ptr_unordered_multimap<std::string,Derived_class>,
  285. Derived_class>();
  286. map_container_assignment_test< ptr_unordered_multimap<std::string, nullable<Base> >,
  287. ptr_unordered_multimap<std::string,Derived_class>,
  288. Derived_class>();
  289. map_container_assignment_test< ptr_unordered_multimap<std::string, nullable<Base> >,
  290. ptr_unordered_multimap<std::string, nullable<Derived_class> >,
  291. Derived_class>();
  292. test_transfer< ptr_unordered_map<int,Derived_class>, ptr_unordered_map<int,Base>, Derived_class >();
  293. test_transfer< ptr_unordered_multimap<int,Derived_class>, ptr_unordered_multimap<int,Base>, Derived_class >();
  294. string joe = "joe";
  295. string brian = "brian";
  296. string kenny = "kenny";
  297. ptr_unordered_map<string,int> m;
  298. m.insert( joe, new int( 4 ) );
  299. m.insert( brian, new int( 6 ) );
  300. BOOST_CHECK( m[ "foo" ] == 0 );
  301. m[ "bar" ] += 5;
  302. BOOST_CHECK( m[ "bar" ] == 5 );
  303. m[ joe ] += 56;
  304. m[ brian ] += 10;
  305. BOOST_CHECK_THROW( (m.insert(kenny, 0 )), bad_ptr_container_operation );
  306. BOOST_CHECK_THROW( (m.replace(m.begin(), 0 )), bad_ptr_container_operation );
  307. BOOST_CHECK_THROW( (m.at("not there")), bad_ptr_container_operation );
  308. for( ptr_unordered_map<string,int>::iterator i = m.begin();
  309. i != m.end(); ++i )
  310. {
  311. if( is_null(i) )
  312. BOOST_CHECK( false );
  313. const string& ref = i->first;
  314. hide_warning(ref);
  315. int& ref2 = *(*i).second;
  316. ref2++;
  317. }
  318. typedef ptr_unordered_map<string,Derived_class> map_type;
  319. map_type m2;
  320. m2.insert( joe, new Derived_class );
  321. //
  322. // This works fine since 'm2' is not const
  323. //
  324. m2.begin()->second->foo();
  325. //
  326. // These all return an implementation-defined proxy
  327. // with two public members: 'first' and 'second'
  328. //
  329. map_type::value_type a_value = *m2.begin();
  330. a_value.second->foo();
  331. map_type::reference a_reference = *m2.begin();
  332. a_reference.second->foo();
  333. map_type::const_reference a_creference = *const_begin(m2);
  334. hide_warning(a_creference);
  335. //
  336. //
  337. // These will fail as iterators propagate constness
  338. //
  339. //a_creference.second->foo();
  340. //a_cpointer->second->foo();
  341. //const_begin(m2)->second->foo();
  342. test_unordered_interface< ptr_unordered_map<string,Base>, string, Derived_class >();
  343. test_unordered_interface< ptr_unordered_map<string,Base>, string, Derived_class >();
  344. }
  345. using boost::unit_test::test_suite;
  346. test_suite* init_unit_test_suite( int argc, char* argv[] )
  347. {
  348. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  349. test->add( BOOST_TEST_CASE( &test_map ) );
  350. return test;
  351. }