small_vector_options_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <boost/core/lightweight_test.hpp>
  12. #include <boost/assert.hpp>
  13. using namespace boost::container;
  14. const std::size_t Capacity = 10u;
  15. void test_alignment()
  16. {
  17. { //extended alignment
  18. const std::size_t extended_alignment = sizeof(int)*4u;
  19. BOOST_STATIC_ASSERT(extended_alignment > dtl::alignment_of<int>::value);
  20. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  21. using options_t = small_vector_options_t< inplace_alignment<extended_alignment> >;
  22. #else
  23. typedef small_vector_options
  24. < inplace_alignment<extended_alignment> >::type options_t;
  25. #endif
  26. small_vector<int, Capacity, void, options_t> v;
  27. v.resize(v.capacity());
  28. BOOST_ASSERT((reinterpret_cast<std::size_t>(&v[0]) % extended_alignment) == 0);
  29. }
  30. { //default alignment
  31. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  32. using options_t = small_vector_options_t< inplace_alignment<0> >;
  33. #else
  34. typedef small_vector_options< inplace_alignment<0> >::type options_t;
  35. #endif
  36. small_vector<int, Capacity, void, options_t> v;
  37. v.resize(v.capacity());
  38. BOOST_ASSERT((reinterpret_cast<std::size_t>(&v[0]) % dtl::alignment_of<int>::value) == 0);
  39. }
  40. }
  41. void test_growth_factor_50()
  42. {
  43. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  44. using options_t = small_vector_options_t< growth_factor<growth_factor_50> >;
  45. #else
  46. typedef small_vector_options
  47. < growth_factor<growth_factor_50> >::type options_t;
  48. #endif
  49. small_vector<int, Capacity, new_allocator<int>, options_t> v;
  50. v.resize(5);
  51. v.resize(v.capacity());
  52. std::size_t old_capacity = v.capacity();
  53. v.push_back(0);
  54. std::size_t new_capacity = v.capacity();
  55. BOOST_TEST(new_capacity == old_capacity + old_capacity/2);
  56. }
  57. void test_growth_factor_60()
  58. {
  59. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  60. using options_t = small_vector_options_t< growth_factor<growth_factor_60> >;
  61. #else
  62. typedef small_vector_options
  63. < growth_factor<growth_factor_60> >::type options_t;
  64. #endif
  65. small_vector<int, Capacity, new_allocator<int>, options_t> v;
  66. v.resize(5);
  67. v.resize(v.capacity());
  68. std::size_t old_capacity = v.capacity();
  69. v.push_back(0);
  70. std::size_t new_capacity = v.capacity();
  71. BOOST_TEST(new_capacity == old_capacity + 3*old_capacity/5);
  72. }
  73. void test_growth_factor_100()
  74. {
  75. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  76. using options_t = small_vector_options_t< growth_factor<growth_factor_100> >;
  77. #else
  78. typedef small_vector_options
  79. < growth_factor<growth_factor_100> >::type options_t;
  80. #endif
  81. small_vector<int, Capacity, new_allocator<int>, options_t> v;
  82. v.resize(5);
  83. v.resize(v.capacity());
  84. std::size_t old_capacity = v.capacity();
  85. v.push_back(0);
  86. std::size_t new_capacity = v.capacity();
  87. BOOST_TEST(new_capacity == 2*old_capacity);
  88. }
  89. int main()
  90. {
  91. test_alignment();
  92. test_growth_factor_50();
  93. test_growth_factor_60();
  94. test_growth_factor_100();
  95. return ::boost::report_errors();
  96. }