list_test.cpp 8.4 KB

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