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