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