slist_test.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <boost/container/slist.hpp>
  12. #include <boost/container/node_allocator.hpp>
  13. #include <memory>
  14. #include "dummy_test_allocator.hpp"
  15. #include "movable_int.hpp"
  16. #include "list_test.hpp"
  17. #include "propagate_allocator_test.hpp"
  18. #include "emplace_test.hpp"
  19. #include "../../intrusive/test/iterator_test.hpp"
  20. using namespace boost::container;
  21. class recursive_slist
  22. {
  23. public:
  24. int id_;
  25. slist<recursive_slist> slist_;
  26. slist<recursive_slist>::iterator it_;
  27. slist<recursive_slist>::const_iterator cit_;
  28. recursive_slist &operator=(const recursive_slist &o)
  29. { slist_ = o.slist_; return *this; }
  30. };
  31. void recursive_slist_test()//Test for recursive types
  32. {
  33. slist<recursive_slist> recursive_list_list;
  34. }
  35. template<class VoidAllocator>
  36. struct GetAllocatorCont
  37. {
  38. template<class ValueType>
  39. struct apply
  40. {
  41. typedef slist< ValueType
  42. , typename allocator_traits<VoidAllocator>
  43. ::template portable_rebind_alloc<ValueType>::type
  44. > type;
  45. };
  46. };
  47. bool test_support_for_initializer_list()
  48. {
  49. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  50. const std::initializer_list<int> il = {5, 10, 15};
  51. const slist<int> expected_list(il.begin(), il.end());
  52. {
  53. slist<int> sl = il;
  54. if(sl != expected_list)
  55. return false;
  56. }
  57. {
  58. slist<int> sl = {1, 2};
  59. sl = il;
  60. if(sl != expected_list)
  61. return false;
  62. }
  63. {
  64. slist<int> sl({ 1, 2 }, slist<int>::allocator_type());
  65. sl = il;
  66. if (sl != expected_list)
  67. return false;
  68. }
  69. {
  70. slist<int> sl = {4, 5};
  71. sl.assign(il);
  72. if(sl != expected_list)
  73. return false;
  74. }
  75. {
  76. slist<int> sl = {15};
  77. sl.insert(sl.cbegin(), {5, 10});
  78. if(sl != expected_list)
  79. return false;
  80. }
  81. {
  82. slist<int> sl = {5};
  83. sl.insert_after(sl.cbegin(), {10, 15});
  84. if(sl != expected_list)
  85. return false;
  86. }
  87. return true;
  88. #endif
  89. return true;
  90. }
  91. bool test_for_splice()
  92. {
  93. {
  94. slist<int> list1; list1.push_front(3); list1.push_front(2); list1.push_front(1); list1.push_front(0);
  95. slist<int> list2;
  96. slist<int> expected1; expected1.push_front(3); expected1.push_front(2); expected1.push_front(0);
  97. slist<int> expected2; expected2.push_front(1);
  98. list2.splice(list2.begin(), list1, ++list1.begin());
  99. if (!(expected1 == list1 && expected2 == list2))
  100. return false;
  101. }
  102. {
  103. slist<int> list1; list1.push_front(3); list1.push_front(2); list1.push_front(1); list1.push_front(0);
  104. slist<int> list2;
  105. slist<int> expected1;
  106. slist<int> expected2; expected2.push_front(3); expected2.push_front(2); expected2.push_front(1); expected2.push_front(0);
  107. list2.splice(list2.begin(), list1, list1.begin(), list1.end());
  108. if (!(expected1 == list1 && expected2 == list2))
  109. return false;
  110. }
  111. return true;
  112. }
  113. struct boost_container_slist;
  114. namespace boost {
  115. namespace container {
  116. namespace test {
  117. template<>
  118. struct alloc_propagate_base<boost_container_slist>
  119. {
  120. template <class T, class Allocator>
  121. struct apply
  122. {
  123. typedef boost::container::slist<T, Allocator> type;
  124. };
  125. };
  126. }}}
  127. int main ()
  128. {
  129. recursive_slist_test();
  130. {
  131. //Now test move semantics
  132. slist<recursive_slist> original;
  133. slist<recursive_slist> move_ctor(boost::move(original));
  134. slist<recursive_slist> move_assign;
  135. move_assign = boost::move(move_ctor);
  136. move_assign.swap(original);
  137. {
  138. slist<recursive_slist> recursive, copy;
  139. //Test to test both move emulations
  140. if(!copy.size()){
  141. copy = recursive;
  142. }
  143. }
  144. }
  145. ////////////////////////////////////
  146. // Testing allocator implementations
  147. ////////////////////////////////////
  148. if (test::list_test<slist<int, std::allocator<int> >, false>())
  149. return 1;
  150. if (test::list_test<slist<int>, false>())
  151. return 1;
  152. if (test::list_test<slist<int, node_allocator<int> >, false>())
  153. return 1;
  154. if (test::list_test<slist<test::movable_int>, false>())
  155. return 1;
  156. if (test::list_test<slist<test::movable_and_copyable_int>, false>())
  157. return 1;
  158. if (test::list_test<slist<test::copyable_int>, false>())
  159. return 1;
  160. ////////////////////////////////////
  161. // Emplace testing
  162. ////////////////////////////////////
  163. const test::EmplaceOptions Options = (test::EmplaceOptions)
  164. (test::EMPLACE_FRONT | test::EMPLACE_AFTER | test::EMPLACE_BEFORE | test::EMPLACE_AFTER);
  165. if(!boost::container::test::test_emplace
  166. < slist<test::EmplaceInt>, Options>())
  167. return 1;
  168. ////////////////////////////////////
  169. // Allocator propagation testing
  170. ////////////////////////////////////
  171. if(!boost::container::test::test_propagate_allocator<boost_container_slist>())
  172. return 1;
  173. ////////////////////////////////////
  174. // Initializer lists
  175. ////////////////////////////////////
  176. if(!test_support_for_initializer_list())
  177. return 1;
  178. ////////////////////////////////////
  179. // Splice testing
  180. ////////////////////////////////////
  181. if(!test_for_splice())
  182. return 1;
  183. ////////////////////////////////////
  184. // Iterator testing
  185. ////////////////////////////////////
  186. {
  187. typedef boost::container::slist<int> vector_int;
  188. vector_int a; a.push_front(2); a.push_front(1); a.push_front(0);
  189. boost::intrusive::test::test_iterator_forward< boost::container::slist<int> >(a);
  190. if(boost::report_errors() != 0) {
  191. return 1;
  192. }
  193. }
  194. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  195. ////////////////////////////////////
  196. // Constructor Template Auto Deduction Tests
  197. ////////////////////////////////////
  198. {
  199. auto gold = std::list{ 1, 2, 3 };
  200. auto test = boost::container::slist(gold.begin(), gold.end());
  201. if (test.size() != 3) {
  202. return 1;
  203. }
  204. if (test.front() != 1)
  205. return 1;
  206. test.pop_front();
  207. if (test.front() != 2)
  208. return 1;
  209. test.pop_front();
  210. if (test.front() != 3)
  211. return 1;
  212. test.pop_front();
  213. }
  214. {
  215. auto gold = std::list{ 1, 2, 3 };
  216. auto test = boost::container::slist(gold.begin(), gold.end(), new_allocator<int>());
  217. if (test.size() != 3) {
  218. return 1;
  219. }
  220. if (test.front() != 1)
  221. return 1;
  222. test.pop_front();
  223. if (test.front() != 2)
  224. return 1;
  225. test.pop_front();
  226. if (test.front() != 3)
  227. return 1;
  228. test.pop_front();
  229. }
  230. #endif
  231. ////////////////////////////////////
  232. // has_trivial_destructor_after_move testing
  233. ////////////////////////////////////
  234. // default allocator
  235. {
  236. typedef boost::container::slist<int> cont;
  237. typedef cont::allocator_type allocator_type;
  238. typedef boost::container::allocator_traits<allocator_type>::pointer pointer;
  239. if (boost::has_trivial_destructor_after_move<cont>::value !=
  240. boost::has_trivial_destructor_after_move<allocator_type>::value &&
  241. boost::has_trivial_destructor_after_move<pointer>::value) {
  242. std::cerr << "has_trivial_destructor_after_move(default allocator) test failed" << std::endl;
  243. return 1;
  244. }
  245. }
  246. // std::allocator
  247. {
  248. typedef boost::container::slist<int, std::allocator<int> > cont;
  249. typedef cont::allocator_type allocator_type;
  250. typedef boost::container::allocator_traits<allocator_type>::pointer pointer;
  251. if (boost::has_trivial_destructor_after_move<cont>::value !=
  252. boost::has_trivial_destructor_after_move<allocator_type>::value &&
  253. boost::has_trivial_destructor_after_move<pointer>::value) {
  254. std::cerr << "has_trivial_destructor_after_move(std::allocator) test failed" << std::endl;
  255. return 1;
  256. }
  257. }
  258. return 0;
  259. }
  260. #include <boost/container/detail/config_end.hpp>