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/vector.hpp>
11 #include <boost/container/allocator.hpp>
12 #include <boost/container/detail/next_capacity.hpp>
13 #include <boost/core/lightweight_test.hpp>
14
15 using namespace boost::container;
16
17 template<class Unsigned, class VectorType>
test_stored_size_type_impl()18 void test_stored_size_type_impl()
19 {
20 VectorType v;
21 typedef typename VectorType::size_type size_type;
22 typedef typename VectorType::value_type value_type;
23 size_type const max = Unsigned(-1);
24 v.resize(5);
25 v.resize(max);
26 BOOST_TEST_THROWS(v.resize(max+1), std::exception);
27 BOOST_TEST_THROWS(v.push_back(value_type(1)), std::exception);
28 BOOST_TEST_THROWS(v.insert(v.begin(), value_type(1)), std::exception);
29 BOOST_TEST_THROWS(v.emplace(v.begin(), value_type(1)),std::exception);
30 BOOST_TEST_THROWS(v.reserve(max+1), std::exception);
31 BOOST_TEST_THROWS(VectorType v2(max+1), std::exception);
32 }
33
34 template<class Unsigned>
test_stored_size_type()35 void test_stored_size_type()
36 {
37 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
38 using options_t = vector_options_t< stored_size<Unsigned> >;
39 #else
40 typedef typename vector_options
41 < stored_size<Unsigned> >::type options_t;
42 #endif
43
44 //Test first with a typical allocator
45 {
46 typedef vector<unsigned char, new_allocator<unsigned char>, options_t> vector_t;
47 test_stored_size_type_impl<Unsigned, vector_t>();
48 }
49 //Test with a V2 allocator
50 {
51 typedef vector<unsigned char, allocator<unsigned char>, options_t> vector_t;
52 test_stored_size_type_impl<Unsigned, vector_t>();
53 }
54 }
55
test_growth_factor_50()56 void test_growth_factor_50()
57 {
58 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
59 using options_t = vector_options_t< growth_factor<growth_factor_50> >;
60 #else
61 typedef vector_options
62 < growth_factor<growth_factor_50> >::type options_t;
63 #endif
64
65 vector<int, new_allocator<int>, options_t> v;
66
67 v.resize(5);
68 v.resize(v.capacity());
69 std::size_t old_capacity = v.capacity();
70 v.push_back(0);
71 std::size_t new_capacity = v.capacity();
72 BOOST_TEST(new_capacity == old_capacity + old_capacity/2);
73 }
74
test_growth_factor_60()75 void test_growth_factor_60()
76 {
77 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
78 using options_t = vector_options_t< growth_factor<growth_factor_60> >;
79 #else
80 typedef vector_options
81 < growth_factor<growth_factor_60> >::type options_t;
82 #endif
83
84 vector<int, new_allocator<int>, options_t> v;
85
86 v.resize(5);
87 v.resize(v.capacity());
88 std::size_t old_capacity = v.capacity();
89 v.push_back(0);
90 std::size_t new_capacity = v.capacity();
91 BOOST_TEST(new_capacity == old_capacity + 3*old_capacity/5);
92 }
93
test_growth_factor_100()94 void test_growth_factor_100()
95 {
96 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
97 using options_t = vector_options_t< growth_factor<growth_factor_100> >;
98 #else
99 typedef vector_options
100 < growth_factor<growth_factor_100> >::type options_t;
101 #endif
102
103 vector<int, new_allocator<int>, options_t> v;
104
105 v.resize(5);
106 v.resize(v.capacity());
107 std::size_t old_capacity = v.capacity();
108 v.push_back(0);
109 std::size_t new_capacity = v.capacity();
110 BOOST_TEST(new_capacity == 2*old_capacity);
111 }
112
main()113 int main()
114 {
115 test_growth_factor_50();
116 test_growth_factor_60();
117 test_growth_factor_100();
118 test_stored_size_type<unsigned char>();
119 test_stored_size_type<unsigned short>();
120 return ::boost::report_errors();
121 }
122