list_test.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/intrusive/list.hpp>
  14. #include <boost/intrusive/pointer_traits.hpp>
  15. #include "itestvalue.hpp"
  16. #include "bptr_value.hpp"
  17. #include "smart_ptr.hpp"
  18. #include "common_functors.hpp"
  19. #include <vector>
  20. #include <boost/detail/lightweight_test.hpp>
  21. #include "test_macros.hpp"
  22. #include "test_container.hpp"
  23. #include <typeinfo>
  24. using namespace boost::intrusive;
  25. template<class VoidPointer>
  26. struct hooks
  27. {
  28. typedef list_base_hook<void_pointer<VoidPointer> > base_hook_type;
  29. typedef list_base_hook< link_mode<auto_unlink>
  30. , void_pointer<VoidPointer>, tag<void> > auto_base_hook_type;
  31. typedef list_member_hook<void_pointer<VoidPointer>, tag<void> > member_hook_type;
  32. typedef list_member_hook< link_mode<auto_unlink>
  33. , void_pointer<VoidPointer> > auto_member_hook_type;
  34. typedef nonhook_node_member< list_node_traits< VoidPointer >,
  35. circular_list_algorithms > nonhook_node_member_type;
  36. };
  37. template < typename ListType, typename ValueContainer >
  38. struct test_list
  39. {
  40. typedef ListType list_type;
  41. typedef typename list_type::value_traits value_traits;
  42. typedef typename value_traits::value_type value_type;
  43. typedef typename list_type::node_algorithms node_algorithms;
  44. static void test_all(ValueContainer&);
  45. static void test_front_back(ValueContainer&);
  46. static void test_sort(ValueContainer&);
  47. static void test_merge(ValueContainer&);
  48. static void test_remove_unique(ValueContainer&);
  49. static void test_insert(ValueContainer&);
  50. static void test_shift(ValueContainer&);
  51. static void test_swap(ValueContainer&);
  52. static void test_clone(ValueContainer&);
  53. static void test_container_from_end(ValueContainer&, detail::true_type);
  54. static void test_container_from_end(ValueContainer&, detail::false_type) {}
  55. };
  56. template < typename ListType, typename ValueContainer >
  57. void test_list< ListType, ValueContainer >::test_all(ValueContainer& values)
  58. {
  59. {
  60. list_type list(values.begin(), values.end());
  61. test::test_container(list);
  62. list.clear();
  63. list.insert(list.end(), values.begin(), values.end());
  64. test::test_sequence_container(list, values);
  65. }
  66. {
  67. list_type list(values.begin(), values.end());
  68. test::test_iterator_bidirectional(list);
  69. }
  70. test_front_back(values);
  71. test_sort(values);
  72. test_merge(values);
  73. test_remove_unique(values);
  74. test_insert(values);
  75. test_shift(values);
  76. test_swap(values);
  77. test_clone(values);
  78. test_container_from_end(values, detail::bool_< ListType::has_container_from_iterator >());
  79. }
  80. //test: push_front, pop_front, push_back, pop_back, front, back, size, empty:
  81. template < class ListType, typename ValueContainer >
  82. void test_list< ListType, ValueContainer >
  83. ::test_front_back(ValueContainer& values)
  84. {
  85. list_type testlist;
  86. BOOST_TEST (testlist.empty());
  87. testlist.push_back (values[0]);
  88. BOOST_TEST (testlist.size() == 1);
  89. BOOST_TEST (&testlist.front() == &values[0]);
  90. BOOST_TEST (&testlist.back() == &values[0]);
  91. testlist.push_front (values[1]);
  92. BOOST_TEST (testlist.size() == 2);
  93. BOOST_TEST (&testlist.front() == &values[1]);
  94. BOOST_TEST (&testlist.back() == &values[0]);
  95. testlist.pop_back();
  96. BOOST_TEST (testlist.size() == 1);
  97. const list_type &const_testlist = testlist;
  98. BOOST_TEST (&const_testlist.front() == &values[1]);
  99. BOOST_TEST (&const_testlist.back() == &values[1]);
  100. testlist.pop_front();
  101. BOOST_TEST (testlist.empty());
  102. }
  103. //test: constructor, iterator, reverse_iterator, sort, reverse:
  104. template < class ListType, typename ValueContainer >
  105. void test_list< ListType, ValueContainer >
  106. ::test_sort(ValueContainer& values)
  107. {
  108. list_type testlist(values.begin(), values.end());
  109. { int init_values [] = { 1, 2, 3, 4, 5 };
  110. TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); }
  111. testlist.sort (even_odd());
  112. { int init_values [] = { 5, 3, 1, 4, 2 };
  113. TEST_INTRUSIVE_SEQUENCE( init_values, testlist.rbegin() ); }
  114. testlist.reverse();
  115. { int init_values [] = { 5, 3, 1, 4, 2 };
  116. TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); }
  117. }
  118. //test: merge due to error in merge implementation:
  119. template < class ListType, typename ValueContainer >
  120. void test_list< ListType, ValueContainer >
  121. ::test_remove_unique (ValueContainer& values)
  122. {
  123. {
  124. list_type list(values.begin(), values.end());
  125. list.remove_if(is_even());
  126. int init_values [] = { 1, 3, 5 };
  127. TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
  128. }
  129. {
  130. list_type list(values.begin(), values.end());
  131. list.remove_if(is_odd());
  132. int init_values [] = { 2, 4 };
  133. TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
  134. }
  135. {
  136. list_type list(values.begin(), values.end());
  137. list.remove_and_dispose_if(is_even(), test::empty_disposer());
  138. int init_values [] = { 1, 3, 5 };
  139. TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
  140. }
  141. {
  142. list_type list(values.begin(), values.end());
  143. list.remove_and_dispose_if(is_odd(), test::empty_disposer());
  144. int init_values [] = { 2, 4 };
  145. TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
  146. }
  147. {
  148. ValueContainer values2(values);
  149. list_type list(values.begin(), values.end());
  150. list.insert(list.end(), values2.begin(), values2.end());
  151. list.sort();
  152. int init_values [] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
  153. TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
  154. list.unique();
  155. int init_values2 [] = { 1, 2, 3, 4, 5 };
  156. TEST_INTRUSIVE_SEQUENCE( init_values2, list.begin() );
  157. }
  158. }
  159. //test: merge due to error in merge implementation:
  160. template < class ListType, typename ValueContainer >
  161. void test_list< ListType, ValueContainer >
  162. ::test_merge (ValueContainer& values)
  163. {
  164. list_type testlist1, testlist2;
  165. testlist1.push_front (values[0]);
  166. testlist2.push_front (values[4]);
  167. testlist2.push_front (values[3]);
  168. testlist2.push_front (values[2]);
  169. testlist1.merge (testlist2);
  170. int init_values [] = { 1, 3, 4, 5 };
  171. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() );
  172. }
  173. //test: assign, insert, const_iterator, const_reverse_iterator, erase, s_iterator_to:
  174. template < class ListType, typename ValueContainer >
  175. void test_list< ListType, ValueContainer >
  176. ::test_insert(ValueContainer& values)
  177. {
  178. list_type testlist;
  179. testlist.assign (values.begin() + 2, values.begin() + 5);
  180. const list_type& const_testlist = testlist;
  181. { int init_values [] = { 3, 4, 5 };
  182. TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); }
  183. typename list_type::iterator i = ++testlist.begin();
  184. BOOST_TEST (i->value_ == 4);
  185. {
  186. typename list_type::const_iterator ci = typename list_type::iterator();
  187. (void)ci;
  188. }
  189. testlist.insert (i, values[0]);
  190. { int init_values [] = { 5, 4, 1, 3 };
  191. TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.rbegin() ); }
  192. i = testlist.iterator_to (values[4]);
  193. BOOST_TEST (&*i == &values[4]);
  194. i = list_type::s_iterator_to (values[4]);
  195. BOOST_TEST (&*i == &values[4]);
  196. typename list_type::const_iterator ic;
  197. ic = testlist.iterator_to (static_cast< typename list_type::const_reference >(values[4]));
  198. BOOST_TEST (&*ic == &values[4]);
  199. ic = list_type::s_iterator_to (static_cast< typename list_type::const_reference >(values[4]));
  200. BOOST_TEST (&*ic == &values[4]);
  201. i = testlist.erase (i);
  202. BOOST_TEST (i == testlist.end());
  203. { int init_values [] = { 3, 1, 4 };
  204. TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); }
  205. }
  206. template < class ListType, typename ValueContainer >
  207. void test_list< ListType, ValueContainer >
  208. ::test_shift(ValueContainer& values)
  209. {
  210. list_type testlist;
  211. const int num_values = (int)values.size();
  212. std::vector<int> expected_values(num_values);
  213. for(int s = 1; s <= num_values; ++s){
  214. expected_values.resize(s);
  215. //Shift forward all possible positions 3 times
  216. for(int i = 0; i < s*3; ++i){
  217. testlist.insert(testlist.begin(), values.begin(), values.begin() + s);
  218. testlist.shift_forward(i);
  219. for(int j = 0; j < s; ++j){
  220. expected_values[(j + s - i%s) % s] = (j + 1);
  221. }
  222. TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin());
  223. testlist.clear();
  224. }
  225. //Shift backwards all possible positions
  226. for(int i = 0; i < s*3; ++i){
  227. testlist.insert(testlist.begin(), values.begin(), values.begin() + s);
  228. testlist.shift_backwards(i);
  229. for(int j = 0; j < s; ++j){
  230. expected_values[(j + i) % s] = (j + 1);
  231. }
  232. TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin());
  233. testlist.clear();
  234. }
  235. }
  236. }
  237. //test: insert (seq-version), swap, splice, erase (seq-version):
  238. template < class ListType, typename ValueContainer >
  239. void test_list< ListType, ValueContainer >
  240. ::test_swap(ValueContainer& values)
  241. {
  242. {
  243. list_type testlist1 (values.begin(), values.begin() + 2);
  244. list_type testlist2;
  245. testlist2.insert (testlist2.end(), values.begin() + 2, values.begin() + 5);
  246. testlist1.swap (testlist2);
  247. { int init_values [] = { 3, 4, 5 };
  248. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  249. { int init_values [] = { 1, 2 };
  250. TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
  251. testlist2.splice (++testlist2.begin(), testlist1);
  252. { int init_values [] = { 1, 3, 4, 5, 2 };
  253. TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
  254. BOOST_TEST (testlist1.empty());
  255. testlist1.splice (testlist1.end(), testlist2, ++(++testlist2.begin()));
  256. { int init_values [] = { 4 };
  257. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  258. { int init_values [] = { 1, 3, 5, 2 };
  259. TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
  260. testlist1.splice (testlist1.end(), testlist2,
  261. testlist2.begin(), ----testlist2.end());
  262. { int init_values [] = { 4, 1, 3 };
  263. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  264. { int init_values [] = { 5, 2 };
  265. TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
  266. testlist1.erase (testlist1.iterator_to(values[0]), testlist1.end());
  267. BOOST_TEST (testlist1.size() == 1);
  268. BOOST_TEST (&testlist1.front() == &values[3]);
  269. }
  270. {
  271. list_type testlist1 (values.begin(), values.begin() + 2);
  272. list_type testlist2 (values.begin() + 3, values.begin() + 5);
  273. swap_nodes< node_algorithms >(values[0], values[2]);
  274. { int init_values [] = { 3, 2 };
  275. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  276. swap_nodes< node_algorithms >(values[2], values[4]);
  277. { int init_values [] = { 5, 2 };
  278. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  279. { int init_values [] = { 4, 3 };
  280. TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
  281. }
  282. {
  283. list_type testlist1 (values.begin(), values.begin() + 1);
  284. { int init_values [] = { 1 };
  285. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  286. swap_nodes< node_algorithms >(values[1], values[2]);
  287. { int init_values [] = { 1 };
  288. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  289. swap_nodes< node_algorithms >(values[0], values[2]);
  290. { int init_values [] = { 3 };
  291. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  292. swap_nodes< node_algorithms >(values[0], values[2]);
  293. { int init_values [] = { 1 };
  294. TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
  295. }
  296. }
  297. template < class ListType, typename ValueContainer >
  298. void test_list< ListType, ValueContainer >
  299. ::test_container_from_end(ValueContainer& values, detail::true_type)
  300. {
  301. list_type testlist1 (values.begin(), values.begin() + values.size());
  302. BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.end()));
  303. BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.cend()));
  304. }
  305. template < class ListType, typename ValueContainer >
  306. void test_list< ListType, ValueContainer >
  307. ::test_clone(ValueContainer& values)
  308. {
  309. list_type testlist1 (values.begin(), values.begin() + values.size());
  310. list_type testlist2;
  311. testlist2.clone_from(testlist1, test::new_cloner<value_type>(), test::delete_disposer<value_type>());
  312. BOOST_TEST (testlist2 == testlist1);
  313. testlist2.clear_and_dispose(test::delete_disposer<value_type>());
  314. BOOST_TEST (testlist2.empty());
  315. }
  316. template < typename ValueTraits, bool ConstantTimeSize, bool Default_Holder, typename ValueContainer >
  317. struct make_and_test_list
  318. : test_list< list< typename ValueTraits::value_type,
  319. value_traits< ValueTraits >,
  320. size_type< std::size_t >,
  321. constant_time_size< ConstantTimeSize >
  322. >,
  323. ValueContainer
  324. >
  325. {};
  326. template < typename ValueTraits, bool ConstantTimeSize, typename ValueContainer >
  327. struct make_and_test_list< ValueTraits, ConstantTimeSize, false, ValueContainer >
  328. : test_list< list< typename ValueTraits::value_type,
  329. value_traits< ValueTraits >,
  330. size_type< std::size_t >,
  331. constant_time_size< ConstantTimeSize >,
  332. header_holder_type< heap_node_holder< typename ValueTraits::pointer > >
  333. >,
  334. ValueContainer
  335. >
  336. {};
  337. template < class VoidPointer, bool ConstantTimeSize, bool Default_Holder >
  338. class test_main_template
  339. {
  340. public:
  341. int operator()()
  342. {
  343. typedef testvalue< hooks<VoidPointer> > value_type;
  344. std::vector<value_type> data (5);
  345. for (int i = 0; i < 5; ++i)
  346. data[i].value_ = i + 1;
  347. make_and_test_list < typename detail::get_base_value_traits <
  348. value_type,
  349. typename hooks<VoidPointer>::base_hook_type
  350. >::type,
  351. ConstantTimeSize,
  352. Default_Holder,
  353. std::vector< value_type >
  354. >::test_all(data);
  355. make_and_test_list < typename detail::get_member_value_traits <
  356. member_hook< value_type, typename hooks<VoidPointer>::member_hook_type, &value_type::node_>
  357. >::type,
  358. ConstantTimeSize,
  359. Default_Holder,
  360. std::vector< value_type >
  361. >::test_all(data);
  362. make_and_test_list< nonhook_node_member_value_traits <
  363. value_type,
  364. typename hooks<VoidPointer>::nonhook_node_member_type,
  365. &value_type::nhn_member_,
  366. safe_link
  367. >,
  368. ConstantTimeSize,
  369. Default_Holder,
  370. std::vector< value_type >
  371. >::test_all(data);
  372. return 0;
  373. }
  374. };
  375. template < class VoidPointer, bool Default_Holder >
  376. class test_main_template< VoidPointer, false, Default_Holder >
  377. {
  378. public:
  379. int operator()()
  380. {
  381. typedef testvalue< hooks<VoidPointer> > value_type;
  382. std::vector<value_type> data (5);
  383. for (int i = 0; i < 5; ++i)
  384. data[i].value_ = i + 1;
  385. make_and_test_list < typename detail::get_base_value_traits <
  386. value_type,
  387. typename hooks<VoidPointer>::auto_base_hook_type
  388. >::type,
  389. false,
  390. Default_Holder,
  391. std::vector< value_type >
  392. >::test_all(data);
  393. make_and_test_list < typename detail::get_member_value_traits <
  394. member_hook< value_type, typename hooks<VoidPointer>::auto_member_hook_type, &value_type::auto_node_>
  395. >::type,
  396. false,
  397. Default_Holder,
  398. std::vector< value_type >
  399. >::test_all(data);
  400. return 0;
  401. }
  402. };
  403. template < bool ConstantTimeSize >
  404. struct test_main_template_bptr
  405. {
  406. int operator()()
  407. {
  408. typedef BPtr_Value value_type;
  409. typedef BPtr_Value_Traits< List_BPtr_Node_Traits > list_value_traits;
  410. typedef typename list_value_traits::node_ptr node_ptr;
  411. typedef bounded_allocator< value_type > allocator_type;
  412. bounded_allocator_scope<allocator_type> bounded_scope; (void)bounded_scope;
  413. allocator_type allocator;
  414. {
  415. bounded_reference_cont< value_type > ref_cont;
  416. for (int i = 0; i < 5; ++i)
  417. {
  418. node_ptr tmp = allocator.allocate(1);
  419. new (tmp.raw()) value_type(i + 1);
  420. ref_cont.push_back(*tmp);
  421. }
  422. test_list < list < value_type,
  423. value_traits< list_value_traits >,
  424. size_type< std::size_t >,
  425. constant_time_size< ConstantTimeSize >,
  426. header_holder_type< bounded_pointer_holder< value_type > >
  427. >,
  428. bounded_reference_cont< value_type >
  429. >::test_all(ref_cont);
  430. }
  431. return 0;
  432. }
  433. };
  434. int main()
  435. {
  436. // test (plain/smart pointers) x (nonconst/const size) x (void node allocator)
  437. test_main_template<void*, false, true>()();
  438. test_main_template<boost::intrusive::smart_ptr<void>, false, true>()();
  439. test_main_template<void*, true, true>()();
  440. test_main_template<boost::intrusive::smart_ptr<void>, true, true>()();
  441. // test (bounded pointers) x ((nonconst/const size) x (special node allocator)
  442. test_main_template_bptr< true >()();
  443. test_main_template_bptr< false >()();
  444. return boost::report_errors();
  445. }