flat_map_test.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/detail/config_begin.hpp>
  11. #include <vector>
  12. #include <boost/container/flat_map.hpp>
  13. #include <boost/container/allocator.hpp>
  14. #include <boost/container/detail/container_or_allocator_rebind.hpp>
  15. #include "print_container.hpp"
  16. #include "dummy_test_allocator.hpp"
  17. #include "movable_int.hpp"
  18. #include "map_test.hpp"
  19. #include "propagate_allocator_test.hpp"
  20. #include "container_common_tests.hpp"
  21. #include "emplace_test.hpp"
  22. #include "../../intrusive/test/iterator_test.hpp"
  23. #include <map>
  24. #include <utility>
  25. using namespace boost::container;
  26. class recursive_flat_map
  27. {
  28. public:
  29. recursive_flat_map(const recursive_flat_map &c)
  30. : id_(c.id_), map_(c.map_)
  31. {}
  32. recursive_flat_map & operator =(const recursive_flat_map &c)
  33. {
  34. id_ = c.id_;
  35. map_= c.map_;
  36. return *this;
  37. }
  38. int id_;
  39. flat_map<recursive_flat_map, recursive_flat_map> map_;
  40. flat_map<recursive_flat_map, recursive_flat_map>::iterator it_;
  41. flat_map<recursive_flat_map, recursive_flat_map>::const_iterator cit_;
  42. flat_map<recursive_flat_map, recursive_flat_map>::reverse_iterator rit_;
  43. flat_map<recursive_flat_map, recursive_flat_map>::const_reverse_iterator crit_;
  44. friend bool operator< (const recursive_flat_map &a, const recursive_flat_map &b)
  45. { return a.id_ < b.id_; }
  46. };
  47. class recursive_flat_multimap
  48. {
  49. public:
  50. recursive_flat_multimap(const recursive_flat_multimap &c)
  51. : id_(c.id_), map_(c.map_)
  52. {}
  53. recursive_flat_multimap & operator =(const recursive_flat_multimap &c)
  54. {
  55. id_ = c.id_;
  56. map_= c.map_;
  57. return *this;
  58. }
  59. int id_;
  60. flat_multimap<recursive_flat_multimap, recursive_flat_multimap> map_;
  61. flat_multimap<recursive_flat_multimap, recursive_flat_multimap>::iterator it_;
  62. flat_multimap<recursive_flat_multimap, recursive_flat_multimap>::const_iterator cit_;
  63. flat_multimap<recursive_flat_multimap, recursive_flat_multimap>::reverse_iterator rit_;
  64. flat_multimap<recursive_flat_multimap, recursive_flat_multimap>::const_reverse_iterator crit_;
  65. friend bool operator< (const recursive_flat_multimap &a, const recursive_flat_multimap &b)
  66. { return a.id_ < b.id_; }
  67. };
  68. template<class C>
  69. void test_move()
  70. {
  71. //Now test move semantics
  72. C original;
  73. C move_ctor(boost::move(original));
  74. C move_assign;
  75. move_assign = boost::move(move_ctor);
  76. move_assign.swap(original);
  77. }
  78. namespace boost{
  79. namespace container {
  80. namespace test{
  81. bool flat_tree_ordered_insertion_test()
  82. {
  83. using namespace boost::container;
  84. const std::size_t NumElements = 100;
  85. //Ordered insertion multimap
  86. {
  87. std::multimap<int, int> int_mmap;
  88. for(std::size_t i = 0; i != NumElements; ++i){
  89. int_mmap.insert(std::multimap<int, int>::value_type(static_cast<int>(i), static_cast<int>(i)));
  90. }
  91. //Construction insertion
  92. flat_multimap<int, int> fmmap(ordered_range, int_mmap.begin(), int_mmap.end());
  93. if(!CheckEqualContainers(int_mmap, fmmap))
  94. return false;
  95. //Insertion when empty
  96. fmmap.clear();
  97. fmmap.insert(ordered_range, int_mmap.begin(), int_mmap.end());
  98. if(!CheckEqualContainers(int_mmap, fmmap))
  99. return false;
  100. //Re-insertion
  101. fmmap.insert(ordered_range, int_mmap.begin(), int_mmap.end());
  102. std::multimap<int, int> int_mmap2(int_mmap);
  103. int_mmap2.insert(int_mmap.begin(), int_mmap.end());
  104. if(!CheckEqualContainers(int_mmap2, fmmap))
  105. return false;
  106. //Re-re-insertion
  107. fmmap.insert(ordered_range, int_mmap2.begin(), int_mmap2.end());
  108. std::multimap<int, int> int_mmap4(int_mmap2);
  109. int_mmap4.insert(int_mmap2.begin(), int_mmap2.end());
  110. if(!CheckEqualContainers(int_mmap4, fmmap))
  111. return false;
  112. //Re-re-insertion of even
  113. std::multimap<int, int> int_even_mmap;
  114. for(std::size_t i = 0; i < NumElements; i+=2){
  115. int_mmap.insert(std::multimap<int, int>::value_type(static_cast<int>(i), static_cast<int>(i)));
  116. }
  117. fmmap.insert(ordered_range, int_even_mmap.begin(), int_even_mmap.end());
  118. int_mmap4.insert(int_even_mmap.begin(), int_even_mmap.end());
  119. if(!CheckEqualContainers(int_mmap4, fmmap))
  120. return false;
  121. }
  122. //Ordered insertion map
  123. {
  124. std::map<int, int> int_map;
  125. for(std::size_t i = 0; i != NumElements; ++i){
  126. int_map.insert(std::map<int, int>::value_type(static_cast<int>(i), static_cast<int>(i)));
  127. }
  128. //Construction insertion
  129. flat_map<int, int> fmap(ordered_unique_range, int_map.begin(), int_map.end());
  130. if(!CheckEqualContainers(int_map, fmap))
  131. return false;
  132. //Insertion when empty
  133. fmap.clear();
  134. fmap.insert(ordered_unique_range, int_map.begin(), int_map.end());
  135. if(!CheckEqualContainers(int_map, fmap))
  136. return false;
  137. //Re-insertion
  138. fmap.insert(ordered_unique_range, int_map.begin(), int_map.end());
  139. std::map<int, int> int_map2(int_map);
  140. int_map2.insert(int_map.begin(), int_map.end());
  141. if(!CheckEqualContainers(int_map2, fmap))
  142. return false;
  143. //Re-re-insertion
  144. fmap.insert(ordered_unique_range, int_map2.begin(), int_map2.end());
  145. std::map<int, int> int_map4(int_map2);
  146. int_map4.insert(int_map2.begin(), int_map2.end());
  147. if(!CheckEqualContainers(int_map4, fmap))
  148. return false;
  149. //Re-re-insertion of even
  150. std::map<int, int> int_even_map;
  151. for(std::size_t i = 0; i < NumElements; i+=2){
  152. int_map.insert(std::map<int, int>::value_type(static_cast<int>(i), static_cast<int>(i)));
  153. }
  154. fmap.insert(ordered_unique_range, int_even_map.begin(), int_even_map.end());
  155. int_map4.insert(int_even_map.begin(), int_even_map.end());
  156. if(!CheckEqualContainers(int_map4, fmap))
  157. return false;
  158. }
  159. return true;
  160. }
  161. bool constructor_template_auto_deduction_test()
  162. {
  163. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  164. using namespace boost::container;
  165. const std::size_t NumElements = 100;
  166. {
  167. std::map<int, int> int_map;
  168. for(std::size_t i = 0; i != NumElements; ++i){
  169. int_map.insert(std::map<int, int>::value_type(static_cast<int>(i), static_cast<int>(i)));
  170. }
  171. std::multimap<int, int> int_mmap;
  172. for (std::size_t i = 0; i != NumElements; ++i) {
  173. int_mmap.insert(std::multimap<int, int>::value_type(static_cast<int>(i), static_cast<int>(i)));
  174. }
  175. typedef std::less<int> comp_int_t;
  176. typedef std::allocator<std::pair<int, int> > alloc_pair_int_t;
  177. //range
  178. {
  179. auto fmap = flat_map(int_map.begin(), int_map.end());
  180. if (!CheckEqualContainers(int_map, fmap))
  181. return false;
  182. auto fmmap = flat_multimap(int_mmap.begin(), int_mmap.end());
  183. if (!CheckEqualContainers(int_mmap, fmmap))
  184. return false;
  185. }
  186. //range+comp
  187. {
  188. auto fmap = flat_map(int_map.begin(), int_map.end(), comp_int_t());
  189. if (!CheckEqualContainers(int_map, fmap))
  190. return false;
  191. auto fmmap = flat_multimap(int_mmap.begin(), int_mmap.end(), comp_int_t());
  192. if (!CheckEqualContainers(int_mmap, fmmap))
  193. return false;
  194. }
  195. //range+comp+alloc
  196. {
  197. auto fmap = flat_map(int_map.begin(), int_map.end(), comp_int_t(), alloc_pair_int_t());
  198. if (!CheckEqualContainers(int_map, fmap))
  199. return false;
  200. auto fmmap = flat_multimap(int_mmap.begin(), int_mmap.end(), comp_int_t(), alloc_pair_int_t());
  201. if (!CheckEqualContainers(int_mmap, fmmap))
  202. return false;
  203. }
  204. //range+alloc
  205. {
  206. auto fmap = flat_map(int_map.begin(), int_map.end(), alloc_pair_int_t());
  207. if (!CheckEqualContainers(int_map, fmap))
  208. return false;
  209. auto fmmap = flat_multimap(int_mmap.begin(), int_mmap.end(), alloc_pair_int_t());
  210. if (!CheckEqualContainers(int_mmap, fmmap))
  211. return false;
  212. }
  213. //ordered_unique_range / ordered_range
  214. //range
  215. {
  216. auto fmap = flat_map(ordered_unique_range, int_map.begin(), int_map.end());
  217. if(!CheckEqualContainers(int_map, fmap))
  218. return false;
  219. auto fmmap = flat_multimap(ordered_range, int_mmap.begin(), int_mmap.end());
  220. if(!CheckEqualContainers(int_mmap, fmmap))
  221. return false;
  222. }
  223. //range+comp
  224. {
  225. auto fmap = flat_map(ordered_unique_range, int_map.begin(), int_map.end(), comp_int_t());
  226. if (!CheckEqualContainers(int_map, fmap))
  227. return false;
  228. auto fmmap = flat_multimap(ordered_range, int_mmap.begin(), int_mmap.end(), comp_int_t());
  229. if (!CheckEqualContainers(int_mmap, fmmap))
  230. return false;
  231. }
  232. //range+comp+alloc
  233. {
  234. auto fmap = flat_map(ordered_unique_range, int_map.begin(), int_map.end(), comp_int_t(), alloc_pair_int_t());
  235. if (!CheckEqualContainers(int_map, fmap))
  236. return false;
  237. auto fmmap = flat_multimap(ordered_range, int_mmap.begin(), int_mmap.end(), comp_int_t(), alloc_pair_int_t());
  238. if (!CheckEqualContainers(int_mmap, fmmap))
  239. return false;
  240. }
  241. //range+alloc
  242. {
  243. auto fmap = flat_map(ordered_unique_range, int_map.begin(), int_map.end(),alloc_pair_int_t());
  244. if (!CheckEqualContainers(int_map, fmap))
  245. return false;
  246. auto fmmap = flat_multimap(ordered_range, int_mmap.begin(), int_mmap.end(),alloc_pair_int_t());
  247. if (!CheckEqualContainers(int_mmap, fmmap))
  248. return false;
  249. }
  250. }
  251. #endif
  252. return true;
  253. }
  254. template< class RandomIt >
  255. void random_shuffle( RandomIt first, RandomIt last )
  256. {
  257. typedef typename boost::container::iterator_traits<RandomIt>::difference_type difference_type;
  258. difference_type n = last - first;
  259. for (difference_type i = n-1; i > 0; --i) {
  260. difference_type j = std::rand() % (i+1);
  261. if(j != i) {
  262. boost::adl_move_swap(first[i], first[j]);
  263. }
  264. }
  265. }
  266. bool flat_tree_extract_adopt_test()
  267. {
  268. using namespace boost::container;
  269. const std::size_t NumElements = 100;
  270. //extract/adopt map
  271. {
  272. //Construction insertion
  273. flat_map<int, int> fmap;
  274. for(std::size_t i = 0; i != NumElements; ++i){
  275. fmap.emplace(static_cast<int>(i), -static_cast<int>(i));
  276. }
  277. flat_map<int, int> fmap_copy(fmap);
  278. flat_map<int, int>::sequence_type seq(fmap.extract_sequence());
  279. if(!fmap.empty())
  280. return false;
  281. if(!CheckEqualContainers(seq, fmap_copy))
  282. return false;
  283. seq.insert(seq.end(), fmap_copy.begin(), fmap_copy.end());
  284. boost::container::test::random_shuffle(seq.begin(), seq.end());
  285. fmap.adopt_sequence(boost::move(seq));
  286. if(!CheckEqualContainers(fmap, fmap_copy))
  287. return false;
  288. }
  289. //extract/adopt map, ordered_unique_range
  290. {
  291. //Construction insertion
  292. flat_map<int, int> fmap;
  293. for(std::size_t i = 0; i != NumElements; ++i){
  294. fmap.emplace(static_cast<int>(i), -static_cast<int>(i));
  295. }
  296. flat_map<int, int> fmap_copy(fmap);
  297. flat_map<int, int>::sequence_type seq(fmap.extract_sequence());
  298. if(!fmap.empty())
  299. return false;
  300. if(!CheckEqualContainers(seq, fmap_copy))
  301. return false;
  302. fmap.adopt_sequence(ordered_unique_range, boost::move(seq));
  303. if(!CheckEqualContainers(fmap, fmap_copy))
  304. return false;
  305. }
  306. //extract/adopt multimap
  307. {
  308. //Construction insertion
  309. flat_multimap<int, int> fmmap;
  310. for(std::size_t i = 0; i != NumElements; ++i){
  311. fmmap.emplace(static_cast<int>(i), -static_cast<int>(i));
  312. fmmap.emplace(static_cast<int>(i), -static_cast<int>(i));
  313. }
  314. flat_multimap<int, int> fmmap_copy(fmmap);
  315. flat_multimap<int, int>::sequence_type seq(fmmap.extract_sequence());
  316. if(!fmmap.empty())
  317. return false;
  318. if(!CheckEqualContainers(seq, fmmap_copy))
  319. return false;
  320. boost::container::test::random_shuffle(seq.begin(), seq.end());
  321. fmmap.adopt_sequence(boost::move(seq));
  322. if(!CheckEqualContainers(fmmap, fmmap_copy))
  323. return false;
  324. }
  325. //extract/adopt multimap, ordered_range
  326. {
  327. //Construction insertion
  328. flat_multimap<int, int> fmmap;
  329. for(std::size_t i = 0; i != NumElements; ++i){
  330. fmmap.emplace(static_cast<int>(i), -static_cast<int>(i));
  331. fmmap.emplace(static_cast<int>(i), -static_cast<int>(i));
  332. }
  333. flat_multimap<int, int> fmmap_copy(fmmap);
  334. flat_multimap<int, int>::sequence_type seq(fmmap.extract_sequence());
  335. if(!fmmap.empty())
  336. return false;
  337. if(!CheckEqualContainers(seq, fmmap_copy))
  338. return false;
  339. fmmap.adopt_sequence(ordered_range, boost::move(seq));
  340. if(!CheckEqualContainers(fmmap, fmmap_copy))
  341. return false;
  342. }
  343. return true;
  344. }
  345. }}}
  346. template<class VoidAllocatorOrContainer>
  347. struct GetMapContainer
  348. {
  349. template<class ValueType>
  350. struct apply
  351. {
  352. typedef std::pair<ValueType, ValueType> type_t;
  353. typedef flat_map< ValueType
  354. , ValueType
  355. , std::less<ValueType>
  356. , typename boost::container::dtl::container_or_allocator_rebind<VoidAllocatorOrContainer, type_t>::type
  357. > map_type;
  358. typedef flat_multimap< ValueType
  359. , ValueType
  360. , std::less<ValueType>
  361. , typename boost::container::dtl::container_or_allocator_rebind<VoidAllocatorOrContainer, type_t>::type
  362. > multimap_type;
  363. };
  364. };
  365. struct boost_container_flat_map;
  366. struct boost_container_flat_multimap;
  367. namespace boost { namespace container { namespace test {
  368. template<>
  369. struct alloc_propagate_base<boost_container_flat_map>
  370. {
  371. template <class T, class Allocator>
  372. struct apply
  373. {
  374. typedef typename boost::container::allocator_traits<Allocator>::
  375. template portable_rebind_alloc<std::pair<T, T> >::type TypeAllocator;
  376. typedef boost::container::flat_map<T, T, std::less<T>, TypeAllocator> type;
  377. };
  378. };
  379. template<>
  380. struct alloc_propagate_base<boost_container_flat_multimap>
  381. {
  382. template <class T, class Allocator>
  383. struct apply
  384. {
  385. typedef typename boost::container::allocator_traits<Allocator>::
  386. template portable_rebind_alloc<std::pair<T, T> >::type TypeAllocator;
  387. typedef boost::container::flat_multimap<T, T, std::less<T>, TypeAllocator> type;
  388. };
  389. };
  390. template <class Key, class T, class Compare, class Allocator>
  391. struct get_real_stored_allocator<flat_map<Key, T, Compare, Allocator> >
  392. {
  393. typedef typename flat_map<Key, T, Compare, Allocator>::impl_stored_allocator_type type;
  394. };
  395. template <class Key, class T, class Compare, class Allocator>
  396. struct get_real_stored_allocator<flat_multimap<Key, T, Compare, Allocator> >
  397. {
  398. typedef typename flat_multimap<Key, T, Compare, Allocator>::impl_stored_allocator_type type;
  399. };
  400. bool test_heterogeneous_lookups()
  401. {
  402. BOOST_STATIC_ASSERT((dtl::is_transparent<less_transparent>::value));
  403. BOOST_STATIC_ASSERT(!(dtl::is_transparent<std::less<int> >::value));
  404. typedef flat_map<int, char, less_transparent> map_t;
  405. typedef flat_multimap<int, char, less_transparent> mmap_t;
  406. typedef map_t::value_type value_type;
  407. map_t map1;
  408. mmap_t mmap1;
  409. const map_t &cmap1 = map1;
  410. const mmap_t &cmmap1 = mmap1;
  411. if(!map1.insert_or_assign(1, 'a').second)
  412. return false;
  413. if( map1.insert_or_assign(1, 'b').second)
  414. return false;
  415. if(!map1.insert_or_assign(2, 'c').second)
  416. return false;
  417. if( map1.insert_or_assign(2, 'd').second)
  418. return false;
  419. if(!map1.insert_or_assign(3, 'e').second)
  420. return false;
  421. if(map1.insert_or_assign(1, 'a').second)
  422. return false;
  423. if(map1.insert_or_assign(1, 'b').second)
  424. return false;
  425. if(map1.insert_or_assign(2, 'c').second)
  426. return false;
  427. if(map1.insert_or_assign(2, 'd').second)
  428. return false;
  429. if(map1.insert_or_assign(3, 'e').second)
  430. return false;
  431. mmap1.insert(value_type(1, 'a'));
  432. mmap1.insert(value_type(1, 'b'));
  433. mmap1.insert(value_type(2, 'c'));
  434. mmap1.insert(value_type(2, 'd'));
  435. mmap1.insert(value_type(3, 'e'));
  436. const test::non_copymovable_int find_me(2);
  437. //find
  438. if(map1.find(find_me)->second != 'd')
  439. return false;
  440. if(cmap1.find(find_me)->second != 'd')
  441. return false;
  442. if(mmap1.find(find_me)->second != 'c')
  443. return false;
  444. if(cmmap1.find(find_me)->second != 'c')
  445. return false;
  446. //count
  447. if(map1.count(find_me) != 1)
  448. return false;
  449. if(cmap1.count(find_me) != 1)
  450. return false;
  451. if(mmap1.count(find_me) != 2)
  452. return false;
  453. if(cmmap1.count(find_me) != 2)
  454. return false;
  455. //contains
  456. if(!map1.contains(find_me))
  457. return false;
  458. if(!cmap1.contains(find_me))
  459. return false;
  460. if(!mmap1.contains(find_me))
  461. return false;
  462. if(!cmmap1.contains(find_me))
  463. return false;
  464. //lower_bound
  465. if(map1.lower_bound(find_me)->second != 'd')
  466. return false;
  467. if(cmap1.lower_bound(find_me)->second != 'd')
  468. return false;
  469. if(mmap1.lower_bound(find_me)->second != 'c')
  470. return false;
  471. if(cmmap1.lower_bound(find_me)->second != 'c')
  472. return false;
  473. //upper_bound
  474. if(map1.upper_bound(find_me)->second != 'e')
  475. return false;
  476. if(cmap1.upper_bound(find_me)->second != 'e')
  477. return false;
  478. if(mmap1.upper_bound(find_me)->second != 'e')
  479. return false;
  480. if(cmmap1.upper_bound(find_me)->second != 'e')
  481. return false;
  482. //equal_range
  483. if(map1.equal_range(find_me).first->second != 'd')
  484. return false;
  485. if(cmap1.equal_range(find_me).second->second != 'e')
  486. return false;
  487. if(mmap1.equal_range(find_me).first->second != 'c')
  488. return false;
  489. if(cmmap1.equal_range(find_me).second->second != 'e')
  490. return false;
  491. return true;
  492. }
  493. // An ordered sequence of std:pair is also ordered by std::pair::first.
  494. struct with_lookup_by_first
  495. {
  496. typedef void is_transparent;
  497. inline bool operator()(std::pair<int, int> a, std::pair<int, int> b) const
  498. {
  499. return a < b;
  500. }
  501. inline bool operator()(std::pair<int, int> a, int first) const
  502. {
  503. return a.first < first;
  504. }
  505. inline bool operator()(int first, std::pair<int, int> b) const
  506. {
  507. return first < b.first;
  508. }
  509. };
  510. bool test_heterogeneous_lookup_by_partial_key()
  511. {
  512. typedef flat_map<std::pair<int, int>,int, with_lookup_by_first> map_t;
  513. map_t map1;
  514. map1[std::pair<int, int>(0, 1)] = 3;
  515. map1[std::pair<int, int>(0, 2)] = 3;
  516. std::pair<map_t::iterator, map_t::iterator> const first_0_range = map1.equal_range(0);
  517. if(2 != (first_0_range.second - first_0_range.first))
  518. return false;
  519. if(2 != map1.count(0))
  520. return false;
  521. return true;
  522. }
  523. }}} //namespace boost::container::test
  524. int main()
  525. {
  526. using namespace boost::container::test;
  527. //Allocator argument container
  528. {
  529. flat_map<int, int> map_((flat_map<int, int>::allocator_type()));
  530. flat_multimap<int, int> multimap_((flat_multimap<int, int>::allocator_type()));
  531. }
  532. //Now test move semantics
  533. {
  534. test_move<flat_map<recursive_flat_map, recursive_flat_map> >();
  535. test_move<flat_multimap<recursive_flat_multimap, recursive_flat_multimap> >();
  536. }
  537. //Now test nth/index_of
  538. {
  539. flat_map<int, int> map;
  540. flat_multimap<int, int> mmap;
  541. map.insert(std::pair<int, int>(0, 0));
  542. map.insert(std::pair<int, int>(1, 0));
  543. map.insert(std::pair<int, int>(2, 0));
  544. mmap.insert(std::pair<int, int>(0, 0));
  545. mmap.insert(std::pair<int, int>(1, 0));
  546. mmap.insert(std::pair<int, int>(2, 0));
  547. if(!boost::container::test::test_nth_index_of(map))
  548. return 1;
  549. if(!boost::container::test::test_nth_index_of(mmap))
  550. return 1;
  551. }
  552. ////////////////////////////////////
  553. // Ordered insertion test
  554. ////////////////////////////////////
  555. if(!flat_tree_ordered_insertion_test()){
  556. return 1;
  557. }
  558. ////////////////////////////////////
  559. // Constructor Template Auto Deduction test
  560. ////////////////////////////////////
  561. if(!constructor_template_auto_deduction_test()){
  562. return 1;
  563. }
  564. ////////////////////////////////////
  565. // Extract/Adopt test
  566. ////////////////////////////////////
  567. if(!flat_tree_extract_adopt_test()){
  568. return 1;
  569. }
  570. if (!boost::container::test::instantiate_constructors<flat_map<int, int>, flat_multimap<int, int> >())
  571. return 1;
  572. if (!test_heterogeneous_lookups())
  573. return 1;
  574. if (!test_heterogeneous_lookup_by_partial_key())
  575. return 1;
  576. ////////////////////////////////////
  577. // Testing allocator implementations
  578. ////////////////////////////////////
  579. {
  580. typedef std::map<int, int> MyStdMap;
  581. typedef std::multimap<int, int> MyStdMultiMap;
  582. if (0 != test::map_test
  583. < GetMapContainer<std::allocator<void> >::apply<int>::map_type
  584. , MyStdMap
  585. , GetMapContainer<std::allocator<void> >::apply<int>::multimap_type
  586. , MyStdMultiMap>()) {
  587. std::cout << "Error in map_test<std::allocator<void> >" << std::endl;
  588. return 1;
  589. }
  590. if (0 != test::map_test
  591. < GetMapContainer<new_allocator<void> >::apply<int>::map_type
  592. , MyStdMap
  593. , GetMapContainer<new_allocator<void> >::apply<int>::multimap_type
  594. , MyStdMultiMap>()) {
  595. std::cout << "Error in map_test<new_allocator<void> >" << std::endl;
  596. return 1;
  597. }
  598. if (0 != test::map_test
  599. < GetMapContainer<new_allocator<void> >::apply<test::movable_int>::map_type
  600. , MyStdMap
  601. , GetMapContainer<new_allocator<void> >::apply<test::movable_int>::multimap_type
  602. , MyStdMultiMap>()) {
  603. std::cout << "Error in map_test<new_allocator<void> >" << std::endl;
  604. return 1;
  605. }
  606. if (0 != test::map_test
  607. < GetMapContainer<new_allocator<void> >::apply<test::copyable_int>::map_type
  608. , MyStdMap
  609. , GetMapContainer<new_allocator<void> >::apply<test::copyable_int>::multimap_type
  610. , MyStdMultiMap>()) {
  611. std::cout << "Error in map_test<new_allocator<void> >" << std::endl;
  612. return 1;
  613. }
  614. if (0 != test::map_test
  615. < GetMapContainer<new_allocator<void> >::apply<test::movable_and_copyable_int>::map_type
  616. , MyStdMap
  617. , GetMapContainer<new_allocator<void> >::apply<test::movable_and_copyable_int>::multimap_type
  618. , MyStdMultiMap>()) {
  619. std::cout << "Error in map_test<new_allocator<void> >" << std::endl;
  620. return 1;
  621. }
  622. }
  623. if(!boost::container::test::test_map_support_for_initialization_list_for<flat_map<int, int> >())
  624. return 1;
  625. if (!boost::container::test::test_map_support_for_initialization_list_for<flat_multimap<int, int> >())
  626. return 1;
  627. ////////////////////////////////////
  628. // Emplace testing
  629. ////////////////////////////////////
  630. const test::EmplaceOptions MapOptions = (test::EmplaceOptions)(test::EMPLACE_HINT_PAIR | test::EMPLACE_ASSOC_PAIR);
  631. if(!boost::container::test::test_emplace<flat_map<test::EmplaceInt, test::EmplaceInt>, MapOptions>())
  632. return 1;
  633. if(!boost::container::test::test_emplace<flat_multimap<test::EmplaceInt, test::EmplaceInt>, MapOptions>())
  634. return 1;
  635. ////////////////////////////////////
  636. // Allocator propagation testing
  637. ////////////////////////////////////
  638. if(!boost::container::test::test_propagate_allocator<boost_container_flat_map>())
  639. return 1;
  640. if(!boost::container::test::test_propagate_allocator<boost_container_flat_multimap>())
  641. return 1;
  642. ////////////////////////////////////
  643. // Iterator testing
  644. ////////////////////////////////////
  645. {
  646. typedef boost::container::flat_map<int, int> cont_int;
  647. cont_int a; a.insert(cont_int::value_type(0, 9)); a.insert(cont_int::value_type(1, 9)); a.insert(cont_int::value_type(2, 9));
  648. boost::intrusive::test::test_iterator_random< cont_int >(a);
  649. if(boost::report_errors() != 0) {
  650. return 1;
  651. }
  652. }
  653. {
  654. typedef boost::container::flat_multimap<int, int> cont_int;
  655. cont_int a; a.insert(cont_int::value_type(0, 9)); a.insert(cont_int::value_type(1, 9)); a.insert(cont_int::value_type(2, 9));
  656. boost::intrusive::test::test_iterator_random< cont_int >(a);
  657. if(boost::report_errors() != 0) {
  658. return 1;
  659. }
  660. }
  661. ////////////////////////////////////
  662. // has_trivial_destructor_after_move testing
  663. ////////////////////////////////////
  664. {
  665. typedef boost::container::dtl::pair<int, int> value_t;
  666. typedef boost::container::dtl::select1st<int> key_of_value_t;
  667. // flat_map, default
  668. {
  669. typedef boost::container::new_allocator<value_t> alloc_or_cont_t;
  670. typedef boost::container::flat_map<int, int> cont;
  671. typedef boost::container::dtl::flat_tree<value_t, key_of_value_t, std::less<int>, alloc_or_cont_t> tree;
  672. if (boost::has_trivial_destructor_after_move<cont>::value !=
  673. boost::has_trivial_destructor_after_move<tree>::value) {
  674. std::cerr << "has_trivial_destructor_after_move(flat_map, default) test failed" << std::endl;
  675. return 1;
  676. }
  677. }
  678. // flat_map, vector
  679. {
  680. typedef boost::container::vector<value_t> alloc_or_cont_t;
  681. typedef boost::container::flat_map<int, int, std::less<int>, alloc_or_cont_t> cont;
  682. typedef boost::container::dtl::flat_tree<value_t, key_of_value_t, std::less<int>, alloc_or_cont_t> tree;
  683. if (boost::has_trivial_destructor_after_move<cont>::value !=
  684. boost::has_trivial_destructor_after_move<tree>::value) {
  685. std::cerr << "has_trivial_destructor_after_move(flat_map, vector) test failed" << std::endl;
  686. return 1;
  687. }
  688. }
  689. // flat_map, std::vector
  690. {
  691. typedef std::vector<value_t> alloc_or_cont_t;
  692. typedef boost::container::flat_map<int, int, std::less<int>, alloc_or_cont_t> cont;
  693. typedef boost::container::dtl::flat_tree<value_t, key_of_value_t, std::less<int>, alloc_or_cont_t> tree;
  694. if (boost::has_trivial_destructor_after_move<cont>::value !=
  695. boost::has_trivial_destructor_after_move<tree>::value) {
  696. std::cerr << "has_trivial_destructor_after_move(flat_map, std::vector) test failed" << std::endl;
  697. return 1;
  698. }
  699. }
  700. // flat_multimap, default
  701. {
  702. typedef boost::container::new_allocator<value_t> alloc_or_cont_t;
  703. typedef boost::container::flat_multimap<int, int> cont;
  704. typedef boost::container::dtl::flat_tree<value_t, key_of_value_t, std::less<int>, alloc_or_cont_t> tree;
  705. if (boost::has_trivial_destructor_after_move<cont>::value !=
  706. boost::has_trivial_destructor_after_move<tree>::value) {
  707. std::cerr << "has_trivial_destructor_after_move(flat_multimap, default) test failed" << std::endl;
  708. return 1;
  709. }
  710. }
  711. // flat_multimap, vector
  712. {
  713. typedef boost::container::vector<value_t> alloc_or_cont_t;
  714. typedef boost::container::flat_multimap<int, int, std::less<int>, alloc_or_cont_t> cont;
  715. typedef boost::container::dtl::flat_tree<value_t, key_of_value_t, std::less<int>, alloc_or_cont_t> tree;
  716. if (boost::has_trivial_destructor_after_move<cont>::value !=
  717. boost::has_trivial_destructor_after_move<tree>::value) {
  718. std::cerr << "has_trivial_destructor_after_move(flat_multimap, vector) test failed" << std::endl;
  719. return 1;
  720. }
  721. }
  722. // flat_multimap, std::vector
  723. {
  724. typedef std::vector<value_t> alloc_or_cont_t;
  725. typedef boost::container::flat_multimap<int, int, std::less<int>, alloc_or_cont_t> cont;
  726. typedef boost::container::dtl::flat_tree<value_t, key_of_value_t, std::less<int>, alloc_or_cont_t> tree;
  727. if (boost::has_trivial_destructor_after_move<cont>::value !=
  728. boost::has_trivial_destructor_after_move<tree>::value) {
  729. std::cerr << "has_trivial_destructor_after_move(flat_multimap, std::vector) test failed" << std::endl;
  730. return 1;
  731. }
  732. }
  733. }
  734. return 0;
  735. }
  736. #include <boost/container/detail/config_end.hpp>