vector_options_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/vector.hpp>
  11. #include <boost/container/allocator.hpp>
  12. #include <boost/container/detail/next_capacity.hpp>
  13. #include <boost/core/lightweight_test.hpp>
  14. using namespace boost::container;
  15. template<class Unsigned, class VectorType>
  16. void test_stored_size_type_impl()
  17. {
  18. VectorType v;
  19. typedef typename VectorType::size_type size_type;
  20. typedef typename VectorType::value_type value_type;
  21. size_type const max = Unsigned(-1);
  22. v.resize(5);
  23. v.resize(max);
  24. BOOST_TEST_THROWS(v.resize(max+1), std::exception);
  25. BOOST_TEST_THROWS(v.push_back(value_type(1)), std::exception);
  26. BOOST_TEST_THROWS(v.insert(v.begin(), value_type(1)), std::exception);
  27. BOOST_TEST_THROWS(v.emplace(v.begin(), value_type(1)),std::exception);
  28. BOOST_TEST_THROWS(v.reserve(max+1), std::exception);
  29. BOOST_TEST_THROWS(VectorType v2(max+1), std::exception);
  30. }
  31. template<class Unsigned>
  32. void test_stored_size_type()
  33. {
  34. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  35. using options_t = vector_options_t< stored_size<Unsigned> >;
  36. #else
  37. typedef typename vector_options
  38. < stored_size<Unsigned> >::type options_t;
  39. #endif
  40. //Test first with a typical allocator
  41. {
  42. typedef vector<unsigned char, new_allocator<unsigned char>, options_t> vector_t;
  43. test_stored_size_type_impl<Unsigned, vector_t>();
  44. }
  45. //Test with a V2 allocator
  46. {
  47. typedef vector<unsigned char, allocator<unsigned char>, options_t> vector_t;
  48. test_stored_size_type_impl<Unsigned, vector_t>();
  49. }
  50. }
  51. void test_growth_factor_50()
  52. {
  53. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  54. using options_t = vector_options_t< growth_factor<growth_factor_50> >;
  55. #else
  56. typedef vector_options
  57. < growth_factor<growth_factor_50> >::type options_t;
  58. #endif
  59. vector<int, new_allocator<int>, options_t> v;
  60. v.resize(5);
  61. v.resize(v.capacity());
  62. std::size_t old_capacity = v.capacity();
  63. v.push_back(0);
  64. std::size_t new_capacity = v.capacity();
  65. BOOST_TEST(new_capacity == old_capacity + old_capacity/2);
  66. }
  67. void test_growth_factor_60()
  68. {
  69. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  70. using options_t = vector_options_t< growth_factor<growth_factor_60> >;
  71. #else
  72. typedef vector_options
  73. < growth_factor<growth_factor_60> >::type options_t;
  74. #endif
  75. vector<int, new_allocator<int>, options_t> v;
  76. v.resize(5);
  77. v.resize(v.capacity());
  78. std::size_t old_capacity = v.capacity();
  79. v.push_back(0);
  80. std::size_t new_capacity = v.capacity();
  81. BOOST_TEST(new_capacity == old_capacity + 3*old_capacity/5);
  82. }
  83. void test_growth_factor_100()
  84. {
  85. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  86. using options_t = vector_options_t< growth_factor<growth_factor_100> >;
  87. #else
  88. typedef vector_options
  89. < growth_factor<growth_factor_100> >::type options_t;
  90. #endif
  91. vector<int, new_allocator<int>, options_t> v;
  92. v.resize(5);
  93. v.resize(v.capacity());
  94. std::size_t old_capacity = v.capacity();
  95. v.push_back(0);
  96. std::size_t new_capacity = v.capacity();
  97. BOOST_TEST(new_capacity == 2*old_capacity);
  98. }
  99. int main()
  100. {
  101. test_growth_factor_50();
  102. test_growth_factor_60();
  103. test_growth_factor_100();
  104. test_stored_size_type<unsigned char>();
  105. test_stored_size_type<unsigned short>();
  106. return ::boost::report_errors();
  107. }