small_vector_test.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/small_vector.hpp>
  11. #include "vector_test.hpp"
  12. #include "movable_int.hpp"
  13. #include "propagate_allocator_test.hpp"
  14. #include "default_init_test.hpp"
  15. #include "../../intrusive/test/iterator_test.hpp"
  16. #include <boost/container/allocator.hpp>
  17. #include <iostream>
  18. struct boost_container_small_vector;
  19. namespace boost { namespace container { namespace test {
  20. template<>
  21. struct alloc_propagate_base<boost_container_small_vector>
  22. {
  23. template <class T, class Allocator>
  24. struct apply
  25. {
  26. typedef boost::container::small_vector<T, 10, Allocator> type;
  27. };
  28. };
  29. }}} //namespace boost::container::test
  30. bool test_small_vector_base_test()
  31. {
  32. typedef boost::container::small_vector_base<int> smb_t;
  33. {
  34. typedef boost::container::small_vector<int, 5> sm5_t;
  35. BOOST_STATIC_ASSERT(sm5_t::static_capacity == 5);
  36. sm5_t sm5;
  37. smb_t &smb = sm5;
  38. smb.push_back(1);
  39. sm5_t sm5_copy(sm5);
  40. sm5_copy.push_back(1);
  41. if (!boost::container::test::CheckEqualContainers(sm5, smb))
  42. return false;
  43. }
  44. {
  45. typedef boost::container::small_vector<int, 7> sm7_t;
  46. BOOST_STATIC_ASSERT(sm7_t::static_capacity == 7);
  47. sm7_t sm7;
  48. smb_t &smb = sm7;
  49. smb.push_back(2);
  50. sm7_t sm7_copy(sm7);
  51. sm7_copy.push_back(2);
  52. if (!boost::container::test::CheckEqualContainers(sm7, smb))
  53. return false;
  54. }
  55. {
  56. typedef boost::container::small_vector<int, 5> sm5_t;
  57. sm5_t sm5;
  58. smb_t &smb = sm5;
  59. smb.push_back(1);
  60. sm5_t sm5_copy(smb);
  61. if (!boost::container::test::CheckEqualContainers(sm5, sm5_copy))
  62. return false;
  63. smb.push_back(2);
  64. if(smb.size() != 2){
  65. return false;
  66. }
  67. sm5_copy = smb;
  68. if (!boost::container::test::CheckEqualContainers(sm5, sm5_copy))
  69. return false;
  70. sm5_t sm5_move(boost::move(smb));
  71. smb.clear();
  72. if (!boost::container::test::CheckEqualContainers(sm5_move, sm5_copy))
  73. return false;
  74. smb = sm5_copy;
  75. sm5_move = boost::move(smb);
  76. smb.clear();
  77. if (!boost::container::test::CheckEqualContainers(sm5_move, sm5_copy))
  78. return false;
  79. }
  80. return true;
  81. }
  82. //small vector has internal storage so some special swap cases must be tested
  83. bool test_swap()
  84. {
  85. typedef boost::container::small_vector<int, 10> vec;
  86. { //v bigger than static capacity, w empty
  87. vec v;
  88. for(std::size_t i = 0, max = v.capacity()+1; i != max; ++i){
  89. v.push_back(int(i));
  90. }
  91. vec w;
  92. const std::size_t v_size = v.size();
  93. const std::size_t w_size = w.size();
  94. v.swap(w);
  95. if(v.size() != w_size || w.size() != v_size)
  96. return false;
  97. }
  98. { //v smaller than static capacity, w empty
  99. vec v;
  100. for(std::size_t i = 0, max = v.capacity()-1; i != max; ++i){
  101. v.push_back(int(i));
  102. }
  103. vec w;
  104. const std::size_t v_size = v.size();
  105. const std::size_t w_size = w.size();
  106. v.swap(w);
  107. if(v.size() != w_size || w.size() != v_size)
  108. return false;
  109. }
  110. { //v & w smaller than static capacity
  111. vec v;
  112. for(std::size_t i = 0, max = v.capacity()-1; i != max; ++i){
  113. v.push_back(int(i));
  114. }
  115. vec w;
  116. for(std::size_t i = 0, max = v.capacity()/2; i != max; ++i){
  117. w.push_back(int(i));
  118. }
  119. const std::size_t v_size = v.size();
  120. const std::size_t w_size = w.size();
  121. v.swap(w);
  122. if(v.size() != w_size || w.size() != v_size)
  123. return false;
  124. }
  125. { //v & w bigger than static capacity
  126. vec v;
  127. for(std::size_t i = 0, max = v.capacity()+1; i != max; ++i){
  128. v.push_back(int(i));
  129. }
  130. vec w;
  131. for(std::size_t i = 0, max = v.capacity()*2; i != max; ++i){
  132. w.push_back(int(i));
  133. }
  134. const std::size_t v_size = v.size();
  135. const std::size_t w_size = w.size();
  136. v.swap(w);
  137. if(v.size() != w_size || w.size() != v_size)
  138. return false;
  139. }
  140. return true;
  141. }
  142. int main()
  143. {
  144. using namespace boost::container;
  145. if(!test_swap())
  146. return 1;
  147. if(test::vector_test< small_vector<int, 0> >())
  148. return 1;
  149. if(test::vector_test< small_vector<int, 2000> >())
  150. return 1;
  151. ////////////////////////////////////
  152. // Default init test
  153. ////////////////////////////////////
  154. if(!test::default_init_test< small_vector<int, 5, test::default_init_allocator<int> > >()){
  155. std::cerr << "Default init test failed" << std::endl;
  156. return 1;
  157. }
  158. ////////////////////////////////////
  159. // Emplace testing
  160. ////////////////////////////////////
  161. const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_BEFORE);
  162. if(!boost::container::test::test_emplace< small_vector<test::EmplaceInt, 5>, Options>()){
  163. return 1;
  164. }
  165. ////////////////////////////////////
  166. // Allocator propagation testing
  167. ////////////////////////////////////
  168. if(!boost::container::test::test_propagate_allocator<boost_container_small_vector>()){
  169. return 1;
  170. }
  171. ////////////////////////////////////
  172. // Initializer lists testing
  173. ////////////////////////////////////
  174. if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for
  175. < boost::container::small_vector<int, 5> >()) {
  176. return 1;
  177. }
  178. ////////////////////////////////////
  179. // Small vector base
  180. ////////////////////////////////////
  181. if (!test_small_vector_base_test()){
  182. return 1;
  183. }
  184. ////////////////////////////////////
  185. // Iterator testing
  186. ////////////////////////////////////
  187. {
  188. typedef boost::container::small_vector<int, 0> cont_int;
  189. cont_int a; a.push_back(0); a.push_back(1); a.push_back(2);
  190. boost::intrusive::test::test_iterator_random< cont_int >(a);
  191. if(boost::report_errors() != 0) {
  192. return 1;
  193. }
  194. }
  195. ////////////////////////////////////
  196. // has_trivial_destructor_after_move testing
  197. ////////////////////////////////////
  198. // default allocator
  199. {
  200. typedef boost::container::small_vector<int, 0> cont;
  201. if (boost::has_trivial_destructor_after_move<cont>::value) {
  202. std::cerr << "has_trivial_destructor_after_move(default allocator) test failed" << std::endl;
  203. return 1;
  204. }
  205. }
  206. // std::allocator
  207. {
  208. typedef boost::container::small_vector<int, 0, std::allocator<int> > cont;
  209. if (boost::has_trivial_destructor_after_move<cont>::value) {
  210. std::cerr << "has_trivial_destructor_after_move(std::allocator) test failed" << std::endl;
  211. return 1;
  212. }
  213. }
  214. return 0;
  215. }