generic_multiset_test.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2013.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #include <boost/container/vector.hpp>
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include "common_functors.hpp"
  16. #include <boost/detail/lightweight_test.hpp>
  17. #include <boost/intrusive/options.hpp>
  18. #include <boost/intrusive/detail/iterator.hpp>
  19. #include "test_macros.hpp"
  20. #include "test_container.hpp"
  21. #include "generic_assoc_test.hpp"
  22. #include <typeinfo>
  23. namespace boost{
  24. namespace intrusive{
  25. namespace test{
  26. template<class ContainerDefiner>
  27. struct test_generic_multiset
  28. {
  29. typedef typename ContainerDefiner::value_cont_type value_cont_type;
  30. static void test_all();
  31. static void test_sort(value_cont_type&);
  32. static void test_insert(value_cont_type&);
  33. static void test_swap(value_cont_type&);
  34. static void test_merge(value_cont_type&);
  35. static void test_find(value_cont_type&);
  36. static void test_impl();
  37. };
  38. template<class ContainerDefiner>
  39. void test_generic_multiset<ContainerDefiner>::test_all ()
  40. {
  41. static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };
  42. value_cont_type values (6);
  43. for (int i = 0; i < 6; ++i)
  44. (&values[i])->value_ = random_init[i];
  45. typedef typename ContainerDefiner::template container
  46. <>::type multiset_type;
  47. {
  48. multiset_type testset(values.begin(), values.end());
  49. test::test_container(testset);
  50. testset.clear();
  51. testset.insert(values.begin(), values.end());
  52. test::test_common_unordered_and_associative_container(testset, values);
  53. testset.clear();
  54. testset.insert(values.begin(), values.end());
  55. test::test_associative_container(testset, values);
  56. testset.clear();
  57. testset.insert(values.begin(), values.end());
  58. test::test_non_unique_container(testset, values);
  59. }
  60. test_sort(values);
  61. test_insert(values);
  62. test_swap(values);
  63. test_merge(values);
  64. test_find(values);
  65. test_impl();
  66. test_generic_assoc<ContainerDefiner>::test_all(values);
  67. }
  68. //test case due to an error in tree implementation:
  69. template<class ContainerDefiner>
  70. void test_generic_multiset<ContainerDefiner>::test_impl()
  71. {
  72. value_cont_type values (5);
  73. for (int i = 0; i < 5; ++i)
  74. (&values[i])->value_ = i;
  75. typedef typename ContainerDefiner::template container
  76. <>::type multiset_type;
  77. multiset_type testset;
  78. for (int i = 0; i < 5; ++i)
  79. testset.insert (values[i]);
  80. testset.erase (testset.iterator_to (values[0]));
  81. testset.erase (testset.iterator_to (values[1]));
  82. testset.insert (values[1]);
  83. testset.erase (testset.iterator_to (values[2]));
  84. testset.erase (testset.iterator_to (values[3]));
  85. }
  86. //test: constructor, iterator, clear, reverse_iterator, front, back, size:
  87. template<class ContainerDefiner>
  88. void test_generic_multiset<ContainerDefiner>::test_sort(value_cont_type& values)
  89. {
  90. typedef typename ContainerDefiner::template container
  91. <>::type multiset_type;
  92. multiset_type testset1 (values.begin(), values.end());
  93. { int init_values [] = { 1, 2, 2, 3, 4, 5 };
  94. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  95. testset1.clear();
  96. BOOST_TEST (testset1.empty());
  97. typedef typename ContainerDefiner::template container
  98. <compare<even_odd>
  99. >::type multiset_type2;
  100. multiset_type2 testset2 (values.begin(), values.begin() + 6);
  101. { int init_values [] = { 5, 3, 1, 4, 2, 2 };
  102. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() ); }
  103. BOOST_TEST (testset2.begin()->value_ == 2);
  104. BOOST_TEST (testset2.rbegin()->value_ == 5);
  105. }
  106. //test: insert, const_iterator, const_reverse_iterator, erase, iterator_to:
  107. template<class ContainerDefiner>
  108. void test_generic_multiset<ContainerDefiner>::test_insert(value_cont_type& values)
  109. {
  110. typedef typename ContainerDefiner::template container
  111. <>::type multiset_type;
  112. multiset_type testset;
  113. testset.insert(values.begin() + 2, values.begin() + 5);
  114. testset.check();
  115. { int init_values [] = { 1, 4, 5 };
  116. TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
  117. typename multiset_type::iterator i = testset.begin();
  118. BOOST_TEST (i->value_ == 1);
  119. i = testset.insert (i, values[0]);
  120. testset.check();
  121. BOOST_TEST (&*i == &values[0]);
  122. { int init_values [] = { 5, 4, 3, 1 };
  123. TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() ); }
  124. i = testset.iterator_to (values[2]);
  125. BOOST_TEST (&*i == &values[2]);
  126. i = multiset_type::s_iterator_to (values[2]);
  127. BOOST_TEST (&*i == &values[2]);
  128. testset.erase(i);
  129. testset.check();
  130. { int init_values [] = { 1, 3, 5 };
  131. TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
  132. }
  133. //test: insert (seq-version), swap, erase (seq-version), size:
  134. template<class ContainerDefiner>
  135. void test_generic_multiset<ContainerDefiner>::test_swap(value_cont_type& values)
  136. {
  137. typedef typename ContainerDefiner::template container
  138. <>::type multiset_type;
  139. multiset_type testset1 (values.begin(), values.begin() + 2);
  140. multiset_type testset2;
  141. testset2.insert (values.begin() + 2, values.begin() + 6);
  142. testset1.swap (testset2);
  143. { int init_values [] = { 1, 2, 4, 5 };
  144. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  145. { int init_values [] = { 2, 3 };
  146. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
  147. testset1.erase (testset1.iterator_to(values[5]), testset1.end());
  148. BOOST_TEST (testset1.size() == 1);
  149. BOOST_TEST (&*testset1.begin() == &values[3]);
  150. }
  151. template<class ContainerDefiner>
  152. void test_generic_multiset<ContainerDefiner>::test_merge(value_cont_type& values)
  153. {
  154. typedef typename ContainerDefiner::template container
  155. <>::type multiset_type;
  156. typedef typename multiset_type::key_of_value key_of_value;
  157. typedef typename multiset_type::key_type key_type;
  158. typedef typename ContainerDefiner::template container
  159. < compare< std::greater<key_type> > >::type multiset_greater_type;
  160. //original vector: 3, 2, 4, 1, 5, 2
  161. //2,3
  162. multiset_type testset1 (values.begin(), values.begin() + 2);
  163. //5, 4, 2, 1
  164. multiset_greater_type testset2;
  165. testset2.insert (values.begin() + 2, values.begin() + 6);
  166. testset2.merge(testset1);
  167. testset1.check();
  168. testset2.check();
  169. BOOST_TEST (testset1.empty());
  170. BOOST_TEST (testset2.size() == 6);
  171. { int init_values [] = { 5, 4, 3, 2, 2, 1 };
  172. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
  173. value_cont_type cmp_val_cont(1);
  174. typename value_cont_type::reference cmp_val = cmp_val_cont.front();
  175. (&cmp_val)->value_ = 2;
  176. BOOST_TEST (*testset2.find(key_of_value()(cmp_val)) == values[5]);
  177. BOOST_TEST (*testset2.find(2, any_greater()) == values[5]);
  178. BOOST_TEST (&*(++testset2.find(key_of_value()(cmp_val))) == &values[1]);
  179. testset1.merge(testset2);
  180. testset1.check();
  181. testset2.check();
  182. BOOST_TEST (testset1.size() == 6);
  183. { int init_values [] = { 1, 2, 2, 3, 4, 5 };
  184. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  185. BOOST_TEST (*testset1.find(key_of_value()(cmp_val)) == values[5]);
  186. BOOST_TEST (*testset1.find(2, any_less()) == values[5]);
  187. BOOST_TEST (&*(++testset1.find(key_of_value()(cmp_val))) == &values[1]);
  188. BOOST_TEST (testset2.empty());
  189. }
  190. //test: find, equal_range (lower_bound, upper_bound):
  191. template<class ContainerDefiner>
  192. void test_generic_multiset<ContainerDefiner>::test_find(value_cont_type& values)
  193. {
  194. typedef typename ContainerDefiner::template container
  195. <>::type multiset_type;
  196. typedef typename multiset_type::key_of_value key_of_value;
  197. multiset_type testset (values.begin(), values.end());
  198. typedef typename multiset_type::iterator iterator;
  199. typedef typename multiset_type::const_iterator const_iterator;
  200. {
  201. value_cont_type cmp_val_cont(1);
  202. typename value_cont_type::reference cmp_val = cmp_val_cont.front();
  203. (&cmp_val)->value_ = 2;
  204. iterator i = testset.find (key_of_value()(cmp_val));
  205. BOOST_TEST (i == testset.find (2, any_less()));
  206. BOOST_TEST (i->value_ == 2);
  207. BOOST_TEST ((++i)->value_ == 2);
  208. std::pair<iterator,iterator> range = testset.equal_range (key_of_value()(cmp_val));
  209. BOOST_TEST(range == testset.equal_range (2, any_less()));
  210. BOOST_TEST (range.first->value_ == 2);
  211. BOOST_TEST (range.second->value_ == 3);
  212. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
  213. (&cmp_val)->value_ = 7;
  214. BOOST_TEST (testset.find(key_of_value()(cmp_val)) == testset.end());
  215. BOOST_TEST (testset.find (7, any_less()) == testset.end());
  216. }
  217. { //1, 2, 2, 3, 4, 5
  218. const multiset_type &const_testset = testset;
  219. std::pair<iterator,iterator> range;
  220. std::pair<const_iterator, const_iterator> const_range;
  221. value_cont_type cmp_val_cont(2);
  222. typename value_cont_type::reference cmp_val_lower = cmp_val_cont.front();
  223. typename value_cont_type::reference cmp_val_upper = cmp_val_cont.back();
  224. {
  225. (&cmp_val_lower)->value_ = 1;
  226. (&cmp_val_upper)->value_ = 2;
  227. //left-closed, right-closed
  228. range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, true);
  229. BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), true, true));
  230. BOOST_TEST (range.first->value_ == 1);
  231. BOOST_TEST (range.second->value_ == 3);
  232. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 3);
  233. }
  234. {
  235. (&cmp_val_lower)->value_ = 1;
  236. (&cmp_val_upper)->value_ = 2;
  237. const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
  238. BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), true, false));
  239. BOOST_TEST (const_range.first->value_ == 1);
  240. BOOST_TEST (const_range.second->value_ == 2);
  241. BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
  242. (&cmp_val_lower)->value_ = 1;
  243. (&cmp_val_upper)->value_ = 3;
  244. range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
  245. BOOST_TEST (range == testset.bounded_range (1, 3, any_less(), true, false));
  246. BOOST_TEST (range.first->value_ == 1);
  247. BOOST_TEST (range.second->value_ == 3);
  248. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 3);
  249. }
  250. {
  251. (&cmp_val_lower)->value_ = 1;
  252. (&cmp_val_upper)->value_ = 2;
  253. const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, true);
  254. BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), false, true));
  255. BOOST_TEST (const_range.first->value_ == 2);
  256. BOOST_TEST (const_range.second->value_ == 3);
  257. BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 2);
  258. }
  259. {
  260. (&cmp_val_lower)->value_ = 1;
  261. (&cmp_val_upper)->value_ = 2;
  262. range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, false);
  263. BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), false, false));
  264. BOOST_TEST (range.first->value_ == 2);
  265. BOOST_TEST (range.second->value_ == 2);
  266. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 0);
  267. }
  268. {
  269. (&cmp_val_lower)->value_ = 5;
  270. (&cmp_val_upper)->value_ = 6;
  271. const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
  272. BOOST_TEST (const_range == const_testset.bounded_range (5, 6, any_less(), true, false));
  273. BOOST_TEST (const_range.first->value_ == 5);
  274. BOOST_TEST (const_range.second == const_testset.end());
  275. BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
  276. }
  277. }
  278. }
  279. }}} //namespace boost::intrusive::test
  280. #include <boost/intrusive/detail/config_end.hpp>