generic_set_test.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <boost/intrusive/detail/mpl.hpp>
  20. #include "test_macros.hpp"
  21. #include "test_container.hpp"
  22. #include "generic_assoc_test.hpp"
  23. #include <typeinfo>
  24. #include <boost/intrusive/priority_compare.hpp>
  25. namespace boost{
  26. namespace intrusive{
  27. namespace test{
  28. template<class ContainerDefiner>
  29. struct test_generic_set
  30. {
  31. static void test_all();
  32. private:
  33. typedef typename ContainerDefiner::value_cont_type value_cont_type;
  34. static void test_sort(value_cont_type&);
  35. static void test_insert(value_cont_type&);
  36. static void test_insert_advanced(value_cont_type&, detail::true_type);
  37. static void test_insert_advanced(value_cont_type&, detail::false_type);
  38. static void test_swap(value_cont_type&);
  39. static void test_merge(value_cont_type&);
  40. static void test_find(value_cont_type&);
  41. static void test_impl();
  42. };
  43. template<class ContainerDefiner>
  44. void test_generic_set<ContainerDefiner>::test_all()
  45. {
  46. typedef typename ContainerDefiner::template container
  47. <>::type set_type;
  48. {
  49. static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };
  50. value_cont_type values(6);
  51. for (int i = 0; i < 6; ++i)
  52. (&values[i])->value_ = random_init[i];
  53. {
  54. set_type testset(values.begin(), values.end());
  55. test::test_container(testset);
  56. testset.clear();
  57. testset.insert(values.begin(), values.end());
  58. test::test_common_unordered_and_associative_container(testset, values);
  59. testset.clear();
  60. testset.insert(values.begin(), values.end());
  61. test::test_associative_container(testset, values);
  62. testset.clear();
  63. testset.insert(values.begin(), values.end());
  64. test::test_unique_container(testset, values);
  65. }
  66. test_sort(values);
  67. test_insert(values);
  68. test_insert_advanced(values, detail::bool_< is_treap< set_type >::value >());
  69. test_swap(values);
  70. test_merge(values);
  71. test_find(values);
  72. test_impl();
  73. test_generic_assoc<ContainerDefiner>::test_all(values);
  74. }
  75. {
  76. value_cont_type values(6);
  77. for (int i = 0; i < 6; ++i)
  78. (&values[i])->value_ = i+1;
  79. set_type testset(values.begin(), values.end());
  80. test::test_iterator_bidirectional(testset);
  81. }
  82. }
  83. //test case due to an error in tree implementation:
  84. template<class ContainerDefiner>
  85. void test_generic_set<ContainerDefiner>::test_impl()
  86. {
  87. value_cont_type values (5);
  88. for (int i = 0; i < 5; ++i)
  89. (&values[i])->value_ = i;
  90. typedef typename ContainerDefiner::template container
  91. <>::type set_type;
  92. set_type testset;
  93. for (int i = 0; i < 5; ++i)
  94. testset.insert (values[i]);
  95. testset.erase (testset.iterator_to (values[0]));
  96. testset.erase (testset.iterator_to (values[1]));
  97. testset.insert (values[1]);
  98. testset.erase (testset.iterator_to (values[2]));
  99. testset.erase (testset.iterator_to (values[3]));
  100. }
  101. //test: constructor, iterator, clear, reverse_iterator, front, back, size:
  102. template<class ContainerDefiner>
  103. void test_generic_set<ContainerDefiner>::test_sort(value_cont_type& values)
  104. {
  105. typedef typename ContainerDefiner::template container
  106. <>::type set_type;
  107. set_type testset1 (values.begin(), values.end());
  108. { int init_values [] = { 1, 2, 3, 4, 5 };
  109. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  110. testset1.clear();
  111. BOOST_TEST (testset1.empty());
  112. typedef typename ContainerDefiner::template container
  113. < compare<even_odd> >::type set_type2;
  114. set_type2 testset2 (values.begin(), values.begin() + 6);
  115. { int init_values [] = { 5, 3, 1, 4, 2 };
  116. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() ); }
  117. BOOST_TEST (testset2.begin()->value_ == 2);
  118. BOOST_TEST (testset2.rbegin()->value_ == 5);
  119. }
  120. //test: insert, const_iterator, const_reverse_iterator, erase, s_iterator_to:
  121. template<class ContainerDefiner>
  122. void test_generic_set<ContainerDefiner>::test_insert(value_cont_type& values)
  123. {
  124. typedef typename ContainerDefiner::template container
  125. <>::type set_type;
  126. {
  127. set_type testset;
  128. testset.insert(values.begin() + 2, values.begin() + 5);
  129. testset.check();
  130. const set_type& const_testset = testset;
  131. { int init_values [] = { 1, 4, 5 };
  132. TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); }
  133. typename set_type::iterator i = testset.begin();
  134. BOOST_TEST (i->value_ == 1);
  135. i = testset.insert (i, values[0]);
  136. testset.check();
  137. BOOST_TEST (&*i == &values[0]);
  138. { int init_values [] = { 5, 4, 3, 1 };
  139. TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() ); }
  140. i = testset.iterator_to (values[2]);
  141. BOOST_TEST (&*i == &values[2]);
  142. i = set_type::s_iterator_to(values[2]);
  143. BOOST_TEST (&*i == &values[2]);
  144. typedef typename value_cont_type::const_reference const_reference;
  145. typename set_type::const_iterator ic;
  146. ic = testset.iterator_to (static_cast< const_reference >(values[2]));
  147. BOOST_TEST (&*ic == &values[2]);
  148. ic = set_type::s_iterator_to (static_cast< const_reference >(values[2]));
  149. BOOST_TEST (&*ic == &values[2]);
  150. testset.erase (i);
  151. testset.check();
  152. { int init_values [] = { 1, 3, 5 };
  153. TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
  154. }
  155. }
  156. // treap version
  157. template<class ValueType, class KeyType>
  158. struct prio_comp
  159. : priority_compare<int>
  160. {
  161. bool operator()(const ValueType &v, const KeyType &k) const
  162. { return this->priority_compare<int>::operator()(v.int_value(), k.int_value()); }
  163. bool operator()(const KeyType &k, const ValueType &v) const
  164. { return this->priority_compare<int>::operator()(k.int_value(), v.int_value()); }
  165. };
  166. template<class ContainerDefiner>
  167. void test_generic_set<ContainerDefiner>::test_insert_advanced
  168. (value_cont_type& values, detail::true_type)
  169. {
  170. typedef typename ContainerDefiner::template container
  171. <>::type set_type;
  172. typedef typename set_type::key_of_value key_of_value;
  173. typedef typename set_type::priority_of_value priority_of_value;
  174. typedef typename set_type::value_type value_type;
  175. typedef priority_compare<> prio_comp_t;
  176. {
  177. set_type testset;
  178. testset.insert(values.begin(), values.begin() + values.size());
  179. testset.check();
  180. value_type v(1);
  181. typename set_type::insert_commit_data data;
  182. BOOST_TEST ((!testset.insert_check(1, any_less(), 1, prio_comp_t(), data).second));
  183. BOOST_TEST ((!testset.insert_check(testset.begin(), 1, any_less(), 1, prio_comp_t(), data).second));
  184. BOOST_TEST ((!testset.insert_check(key_of_value()(v), priority_of_value()(v), data).second));
  185. BOOST_TEST ((!testset.insert_check(testset.begin(), key_of_value()(v), priority_of_value()(v), data).second));
  186. }
  187. }
  188. //test: insert, const_iterator, const_reverse_iterator, erase, s_iterator_to:
  189. template<class ContainerDefiner>
  190. void test_generic_set<ContainerDefiner>::test_insert_advanced
  191. (value_cont_type& values, detail::false_type)
  192. {
  193. typedef typename ContainerDefiner::template container
  194. <>::type set_type;
  195. typedef typename set_type::key_of_value key_of_value;
  196. typedef typename set_type::value_type value_type;
  197. {
  198. set_type testset;
  199. testset.insert(values.begin(), values.begin() + values.size());
  200. testset.check();
  201. value_type v(1);
  202. typename set_type::insert_commit_data data;
  203. BOOST_TEST ((!testset.insert_check(1, any_less(), data).second));
  204. BOOST_TEST ((!testset.insert_check(key_of_value()(v), data).second));
  205. BOOST_TEST ((!testset.insert_check(testset.begin(), 1, any_less(), data).second));
  206. BOOST_TEST ((!testset.insert_check(testset.begin(), key_of_value()(v), data).second));
  207. }
  208. }
  209. //test: insert (seq-version), swap, erase (seq-version), size:
  210. template<class ContainerDefiner>
  211. void test_generic_set<ContainerDefiner>::test_swap(value_cont_type& values)
  212. {
  213. typedef typename ContainerDefiner::template container
  214. <>::type set_type;
  215. set_type testset1 (values.begin(), values.begin() + 2);
  216. set_type testset2;
  217. testset2.insert (values.begin() + 2, values.begin() + 6);
  218. testset1.swap (testset2);
  219. { int init_values [] = { 1, 2, 4, 5 };
  220. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  221. { int init_values [] = { 2, 3 };
  222. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
  223. testset1.erase (testset1.iterator_to(values[5]), testset1.end());
  224. BOOST_TEST (testset1.size() == 1);
  225. // BOOST_TEST (&testset1.front() == &values[3]);
  226. BOOST_TEST (&*testset1.begin() == &values[3]);
  227. }
  228. template<class ContainerDefiner>
  229. void test_generic_set<ContainerDefiner>::test_merge(value_cont_type& values)
  230. {
  231. typedef typename ContainerDefiner::template container
  232. <>::type set_type;
  233. typedef typename set_type::key_type key_type;
  234. typedef typename ContainerDefiner::template container
  235. < compare< std::greater<key_type> > >::type set_greater_type;
  236. //2,3
  237. set_type testset1 (values.begin(), values.begin() + 2);
  238. //5, 4, 2, 1
  239. set_greater_type testset2;
  240. testset2.insert (values.begin() + 2, values.begin() + 6);
  241. testset2.merge(testset1);
  242. testset1.check();
  243. testset2.check();
  244. BOOST_TEST (testset1.size() == 1);
  245. { int init_values [] = { 2 };
  246. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  247. BOOST_TEST (&*testset1.begin() == &values[1]);
  248. BOOST_TEST (testset2.size() == 5);
  249. { int init_values [] = { 5, 4, 3, 2, 1 };
  250. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
  251. testset1.merge(testset2);
  252. testset1.check();
  253. testset2.check();
  254. BOOST_TEST (testset1.size() == 5);
  255. { int init_values [] = { 1, 2, 3, 4, 5 };
  256. TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
  257. BOOST_TEST (testset2.size() == 1);
  258. { int init_values [] = { 2 };
  259. TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
  260. BOOST_TEST (&*testset2.begin() == &values[5]);
  261. }
  262. //test: find, equal_range (lower_bound, upper_bound), bounded_range:
  263. template<class ContainerDefiner>
  264. void test_generic_set<ContainerDefiner>::test_find(value_cont_type& values)
  265. {
  266. typedef typename ContainerDefiner::template container
  267. <>::type set_type;
  268. set_type testset (values.begin(), values.end());
  269. typedef typename set_type::iterator iterator;
  270. typedef typename set_type::const_iterator const_iterator;
  271. typedef typename set_type::key_of_value key_of_value;
  272. typedef typename value_cont_type::reference reference;
  273. {
  274. //value_type cmp_val;
  275. value_cont_type cmp_val_cont(1);
  276. reference cmp_val = cmp_val_cont.front();
  277. (&cmp_val)->value_ = 2;
  278. iterator i = testset.find(key_of_value()(cmp_val));
  279. BOOST_TEST (i == testset.find(2, any_less()));
  280. BOOST_TEST (i->value_ == 2);
  281. BOOST_TEST ((++i)->value_ != 2);
  282. std::pair<iterator,iterator> range = testset.equal_range (key_of_value()(cmp_val));
  283. BOOST_TEST(range == testset.equal_range (2, any_less()));
  284. BOOST_TEST (range.first->value_ == 2);
  285. BOOST_TEST (range.second->value_ == 3);
  286. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 1);
  287. (&cmp_val)->value_ = 7;
  288. BOOST_TEST (testset.find (key_of_value()(cmp_val)) == testset.end());
  289. BOOST_TEST (testset.find (7, any_less()) == testset.end());
  290. }
  291. {
  292. const set_type &const_testset = testset;
  293. std::pair<iterator,iterator> range;
  294. std::pair<const_iterator, const_iterator> const_range;
  295. //value_type cmp_val_lower, cmp_val_upper;
  296. value_cont_type cmp_val_cont(2);
  297. reference cmp_val_lower = cmp_val_cont.front();
  298. reference cmp_val_upper = cmp_val_cont.back();
  299. {
  300. (&cmp_val_lower)->value_ = 1;
  301. (&cmp_val_upper)->value_ = 2;
  302. //left-closed, right-closed
  303. range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, true);
  304. BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), true, true));
  305. BOOST_TEST (range.first->value_ == 1);
  306. BOOST_TEST (range.second->value_ == 3);
  307. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
  308. }
  309. {
  310. (&cmp_val_lower)->value_ = 1;
  311. (&cmp_val_upper)->value_ = 2;
  312. const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
  313. BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), true, false));
  314. BOOST_TEST (const_range.first->value_ == 1);
  315. BOOST_TEST (const_range.second->value_ == 2);
  316. BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
  317. (&cmp_val_lower)->value_ = 1;
  318. (&cmp_val_upper)->value_ = 3;
  319. range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
  320. BOOST_TEST (range == testset.bounded_range (1, 3, any_less(), true, false));
  321. BOOST_TEST (range.first->value_ == 1);
  322. BOOST_TEST (range.second->value_ == 3);
  323. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
  324. }
  325. {
  326. (&cmp_val_lower)->value_ = 1;
  327. (&cmp_val_upper)->value_ = 2;
  328. const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, true);
  329. BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), false, true));
  330. BOOST_TEST (const_range.first->value_ == 2);
  331. BOOST_TEST (const_range.second->value_ == 3);
  332. BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
  333. }
  334. {
  335. (&cmp_val_lower)->value_ = 1;
  336. (&cmp_val_upper)->value_ = 2;
  337. range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, false);
  338. BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), false, false));
  339. BOOST_TEST (range.first->value_ == 2);
  340. BOOST_TEST (range.second->value_ == 2);
  341. BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 0);
  342. }
  343. {
  344. (&cmp_val_lower)->value_ = 5;
  345. (&cmp_val_upper)->value_ = 6;
  346. const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
  347. BOOST_TEST (const_range == const_testset.bounded_range (5, 6, any_less(), true, false));
  348. BOOST_TEST (const_range.first->value_ == 5);
  349. BOOST_TEST (const_range.second == const_testset.end());
  350. BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
  351. }
  352. }
  353. }
  354. }}} //namespace boost::intrusive::test
  355. #include <boost/intrusive/detail/config_end.hpp>