static_vector_options_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 BOOST_ENABLE_ASSERT_HANDLER
  11. #include <boost/container/static_vector.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include <new> //for bad_alloc
  14. #include <boost/assert.hpp>
  15. using namespace boost::container;
  16. //User-defined assertion to test throw_on_overflow
  17. struct throw_on_overflow_off
  18. {};
  19. namespace boost {
  20. void assertion_failed(char const *, char const *, char const *, long)
  21. {
  22. throw throw_on_overflow_off();
  23. }
  24. void assertion_failed_msg(char const *, char const *, char const *, char const *, long )
  25. {
  26. throw throw_on_overflow_off();
  27. }
  28. }
  29. void test_alignment()
  30. {
  31. const std::size_t Capacity = 10u;
  32. { //extended alignment
  33. const std::size_t extended_alignment = sizeof(int)*4u;
  34. BOOST_STATIC_ASSERT(extended_alignment > dtl::alignment_of<int>::value);
  35. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  36. using options_t = static_vector_options_t< inplace_alignment<extended_alignment> >;
  37. #else
  38. typedef static_vector_options
  39. < inplace_alignment<extended_alignment> >::type options_t;
  40. #endif
  41. static_vector<int, Capacity, options_t> v;
  42. v.resize(v.capacity());
  43. BOOST_ASSERT((reinterpret_cast<std::size_t>(&v[0]) % extended_alignment) == 0);
  44. }
  45. { //default alignment
  46. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  47. using options_t = static_vector_options_t< inplace_alignment<0> >;
  48. #else
  49. typedef static_vector_options< inplace_alignment<0> >::type options_t;
  50. #endif
  51. static_vector<int, Capacity, options_t> v;
  52. v.resize(v.capacity());
  53. BOOST_ASSERT((reinterpret_cast<std::size_t>(&v[0]) % dtl::alignment_of<int>::value) == 0);
  54. }
  55. }
  56. void test_throw_on_overflow()
  57. {
  58. #if !defined(BOOST_NO_EXCEPTIONS)
  59. const std::size_t Capacity = 10u;
  60. { //throw_on_overflow == true, expect bad_alloc
  61. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  62. using options_t = static_vector_options_t< throw_on_overflow<true> >;
  63. #else
  64. typedef static_vector_options
  65. < throw_on_overflow<true> >::type options_t;
  66. #endif
  67. static_vector<int, Capacity, options_t> v;
  68. v.resize(Capacity);
  69. bool expected_type_thrown = false;
  70. try{
  71. v.push_back(0);
  72. }
  73. catch(std::bad_alloc&)
  74. {
  75. expected_type_thrown = true;
  76. }
  77. catch(...)
  78. {}
  79. BOOST_TEST(expected_type_thrown == true);
  80. BOOST_TEST(v.capacity() == Capacity);
  81. }
  82. { //throw_on_overflow == false, test it through BOOST_ASSERT
  83. //even in release mode (BOOST_ENABLE_ASSERT_HANDLER), and throwing
  84. //a special type in that assertion.
  85. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  86. using options_t = static_vector_options_t< throw_on_overflow<false> >;
  87. #else
  88. typedef static_vector_options< throw_on_overflow<false> >::type options_t;
  89. #endif
  90. static_vector<int, Capacity, options_t> v;
  91. v.resize(Capacity);
  92. bool expected_type_thrown = false;
  93. try{
  94. v.push_back(0);
  95. }
  96. catch(throw_on_overflow_off)
  97. {
  98. expected_type_thrown = true;
  99. }
  100. catch(...)
  101. {}
  102. BOOST_TEST(expected_type_thrown == true);
  103. BOOST_TEST(v.capacity() == Capacity);
  104. }
  105. #endif
  106. }
  107. int main()
  108. {
  109. test_alignment();
  110. test_throw_on_overflow();
  111. return ::boost::report_errors();
  112. }