]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/container/test/static_vector_options_test.cpp
80e88436ea8a874a0abda5b800a465b8adcab32b
[ceph.git] / ceph / src / boost / libs / container / test / static_vector_options_test.cpp
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 <boost/core/no_exceptions_support.hpp>
14 #include <new> //for bad_alloc
15 #include <boost/assert.hpp>
16 #include <cstdlib>
17 using namespace boost::container;
18
19 //User-defined assertion to test throw_on_overflow
20 struct throw_on_overflow_off
21 {};
22
23 namespace boost {
24 void assertion_failed(char const *, char const *, char const *, long)
25 {
26 #ifdef BOOST_NO_EXCEPTIONS
27 std::abort();
28 #else
29 throw throw_on_overflow_off();
30 #endif
31 }
32
33 void assertion_failed_msg(char const *, char const *, char const *, char const *, long )
34 {
35 #ifdef BOOST_NO_EXCEPTIONS
36 std::abort();
37 #else
38 throw throw_on_overflow_off();
39 #endif
40 }
41 }
42
43 void test_alignment()
44 {
45 const std::size_t Capacity = 10u;
46 { //extended alignment
47 const std::size_t extended_alignment = sizeof(int)*4u;
48 BOOST_STATIC_ASSERT(extended_alignment > dtl::alignment_of<int>::value);
49 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
50 using options_t = static_vector_options_t< inplace_alignment<extended_alignment> >;
51 #else
52 typedef static_vector_options
53 < inplace_alignment<extended_alignment> >::type options_t;
54 #endif
55
56 static_vector<int, Capacity, options_t> v;
57 v.resize(v.capacity());
58 BOOST_ASSERT((reinterpret_cast<std::size_t>(&v[0]) % extended_alignment) == 0);
59 }
60 { //default alignment
61 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
62 using options_t = static_vector_options_t< inplace_alignment<0> >;
63 #else
64 typedef static_vector_options< inplace_alignment<0> >::type options_t;
65 #endif
66
67 static_vector<int, Capacity, options_t> v;
68 v.resize(v.capacity());
69 BOOST_ASSERT((reinterpret_cast<std::size_t>(&v[0]) % dtl::alignment_of<int>::value) == 0);
70 }
71 }
72
73 void test_throw_on_overflow()
74 {
75 #if !defined(BOOST_NO_EXCEPTIONS)
76 const std::size_t Capacity = 10u;
77 { //throw_on_overflow == true, expect bad_alloc
78 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
79 using options_t = static_vector_options_t< throw_on_overflow<true> >;
80 #else
81 typedef static_vector_options
82 < throw_on_overflow<true> >::type options_t;
83 #endif
84
85 static_vector<int, Capacity, options_t> v;
86
87 v.resize(Capacity);
88 bool expected_type_thrown = false;
89
90 BOOST_TRY{
91 v.push_back(0);
92 }
93 BOOST_CATCH(std::bad_alloc&)
94 {
95 expected_type_thrown = true;
96 }
97 BOOST_CATCH(...)
98 {}
99 BOOST_CATCH_END
100
101 BOOST_TEST(expected_type_thrown == true);
102 BOOST_TEST(v.capacity() == Capacity);
103 }
104 { //throw_on_overflow == false, test it through BOOST_ASSERT
105 //even in release mode (BOOST_ENABLE_ASSERT_HANDLER), and throwing
106 //a special type in that assertion.
107 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
108 using options_t = static_vector_options_t< throw_on_overflow<false> >;
109 #else
110 typedef static_vector_options< throw_on_overflow<false> >::type options_t;
111 #endif
112
113 static_vector<int, Capacity, options_t> v;
114
115 v.resize(Capacity);
116 bool expected_type_thrown = false;
117
118 BOOST_TRY{
119 v.push_back(0);
120 }
121 BOOST_CATCH(throw_on_overflow_off)
122 {
123 expected_type_thrown = true;
124 }
125 BOOST_CATCH(...)
126 {}
127 BOOST_CATCH_END
128
129 BOOST_TEST(expected_type_thrown == true);
130 BOOST_TEST(v.capacity() == Capacity);
131 }
132 #endif
133 }
134
135 int main()
136 {
137 test_alignment();
138 test_throw_on_overflow();
139 return ::boost::report_errors();
140 }