stable_vector_test.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #define STABLE_VECTOR_ENABLE_INVARIANT_CHECKING
  11. #include <boost/container/detail/config_begin.hpp>
  12. #include <memory>
  13. #include <boost/container/stable_vector.hpp>
  14. #include <boost/container/node_allocator.hpp>
  15. #include "check_equal_containers.hpp"
  16. #include "movable_int.hpp"
  17. #include "expand_bwd_test_allocator.hpp"
  18. #include "expand_bwd_test_template.hpp"
  19. #include "dummy_test_allocator.hpp"
  20. #include "propagate_allocator_test.hpp"
  21. #include "vector_test.hpp"
  22. #include "default_init_test.hpp"
  23. #include "../../intrusive/test/iterator_test.hpp"
  24. using namespace boost::container;
  25. class recursive_vector
  26. {
  27. public:
  28. int id_;
  29. stable_vector<recursive_vector> vector_;
  30. stable_vector<recursive_vector>::iterator it_;
  31. stable_vector<recursive_vector>::const_iterator cit_;
  32. stable_vector<recursive_vector>::reverse_iterator rit_;
  33. stable_vector<recursive_vector>::const_reverse_iterator crit_;
  34. recursive_vector &operator=(const recursive_vector &o)
  35. { vector_ = o.vector_; return *this; }
  36. };
  37. void recursive_vector_test()//Test for recursive types
  38. {
  39. stable_vector<recursive_vector> recursive, copy;
  40. //Test to test both move emulations
  41. if(!copy.size()){
  42. copy = recursive;
  43. }
  44. }
  45. template<class VoidAllocator>
  46. struct GetAllocatorCont
  47. {
  48. template<class ValueType>
  49. struct apply
  50. {
  51. typedef stable_vector< ValueType
  52. , typename allocator_traits<VoidAllocator>
  53. ::template portable_rebind_alloc<ValueType>::type
  54. > type;
  55. };
  56. };
  57. template<class VoidAllocator>
  58. int test_cont_variants()
  59. {
  60. typedef typename GetAllocatorCont<VoidAllocator>::template apply<int>::type MyCont;
  61. typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::movable_int>::type MyMoveCont;
  62. typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::movable_and_copyable_int>::type MyCopyMoveCont;
  63. typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::copyable_int>::type MyCopyCont;
  64. if(test::vector_test<MyCont>())
  65. return 1;
  66. if(test::vector_test<MyMoveCont>())
  67. return 1;
  68. if(test::vector_test<MyCopyMoveCont>())
  69. return 1;
  70. if(test::vector_test<MyCopyCont>())
  71. return 1;
  72. return 0;
  73. }
  74. struct boost_container_stable_vector;
  75. namespace boost { namespace container { namespace test {
  76. template<>
  77. struct alloc_propagate_base<boost_container_stable_vector>
  78. {
  79. template <class T, class Allocator>
  80. struct apply
  81. {
  82. typedef boost::container::stable_vector<T, Allocator> type;
  83. };
  84. };
  85. }}} //namespace boost::container::test
  86. int main()
  87. {
  88. recursive_vector_test();
  89. {
  90. //Now test move semantics
  91. stable_vector<recursive_vector> original;
  92. stable_vector<recursive_vector> move_ctor(boost::move(original));
  93. stable_vector<recursive_vector> move_assign;
  94. move_assign = boost::move(move_ctor);
  95. move_assign.swap(original);
  96. }
  97. //Test non-copy-move operations
  98. {
  99. stable_vector<test::non_copymovable_int> sv;
  100. sv.emplace_back();
  101. sv.resize(10);
  102. sv.resize(1);
  103. }
  104. ////////////////////////////////////
  105. // Testing allocator implementations
  106. ////////////////////////////////////
  107. // std:allocator
  108. if(test_cont_variants< std::allocator<void> >()){
  109. std::cerr << "test_cont_variants< std::allocator<void> > failed" << std::endl;
  110. return 1;
  111. }
  112. // boost::container::node_allocator
  113. if(test_cont_variants< node_allocator<void> >()){
  114. std::cerr << "test_cont_variants< node_allocator<void> > failed" << std::endl;
  115. return 1;
  116. }
  117. ////////////////////////////////////
  118. // Default init test
  119. ////////////////////////////////////
  120. if(!test::default_init_test< stable_vector<int, test::default_init_allocator<int> > >()){
  121. std::cerr << "Default init test failed" << std::endl;
  122. return 1;
  123. }
  124. ////////////////////////////////////
  125. // Emplace testing
  126. ////////////////////////////////////
  127. const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_BEFORE);
  128. if(!boost::container::test::test_emplace
  129. < stable_vector<test::EmplaceInt>, Options>())
  130. return 1;
  131. ////////////////////////////////////
  132. // Allocator propagation testing
  133. ////////////////////////////////////
  134. if(!boost::container::test::test_propagate_allocator<boost_container_stable_vector>())
  135. return 1;
  136. ////////////////////////////////////
  137. // Initializer lists testing
  138. ////////////////////////////////////
  139. if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for
  140. < boost::container::stable_vector<int> >())
  141. {
  142. std::cerr << "test_methods_with_initializer_list_as_argument failed" << std::endl;
  143. return 1;
  144. }
  145. ////////////////////////////////////
  146. // Iterator testing
  147. ////////////////////////////////////
  148. {
  149. typedef boost::container::stable_vector<int> cont_int;
  150. cont_int a; a.push_back(0); a.push_back(1); a.push_back(2);
  151. boost::intrusive::test::test_iterator_random< cont_int >(a);
  152. if(boost::report_errors() != 0) {
  153. return 1;
  154. }
  155. }
  156. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  157. ////////////////////////////////////
  158. // Constructor Template Auto Deduction testing
  159. ////////////////////////////////////
  160. {
  161. auto gold = std::vector{ 1, 2, 3 };
  162. auto test = boost::container::stable_vector(gold.begin(), gold.end());
  163. if (test.size() != 3) {
  164. return 1;
  165. }
  166. if (!(test[0] == 1 && test[1] == 2 && test[2] == 3)) {
  167. return 1;
  168. }
  169. }
  170. #endif
  171. ////////////////////////////////////
  172. // has_trivial_destructor_after_move testing
  173. ////////////////////////////////////
  174. // default allocator
  175. {
  176. typedef boost::container::stable_vector<int> cont;
  177. typedef cont::allocator_type allocator_type;
  178. typedef boost::container::allocator_traits<allocator_type>::pointer pointer;
  179. if (boost::has_trivial_destructor_after_move<cont>::value !=
  180. boost::has_trivial_destructor_after_move<allocator_type>::value &&
  181. boost::has_trivial_destructor_after_move<pointer>::value) {
  182. std::cerr << "has_trivial_destructor_after_move(default allocator) test failed" << std::endl;
  183. return 1;
  184. }
  185. }
  186. // std::allocator
  187. {
  188. typedef boost::container::stable_vector<int, std::allocator<int> > cont;
  189. typedef cont::allocator_type allocator_type;
  190. typedef boost::container::allocator_traits<allocator_type>::pointer pointer;
  191. if (boost::has_trivial_destructor_after_move<cont>::value !=
  192. boost::has_trivial_destructor_after_move<allocator_type>::value &&
  193. boost::has_trivial_destructor_after_move<pointer>::value) {
  194. std::cerr << "has_trivial_destructor_after_move(std::allocator) test failed" << std::endl;
  195. return 1;
  196. }
  197. }
  198. return 0;
  199. }
  200. #include <boost/container/detail/config_end.hpp>