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 "vector_test.hpp"
12 #include "movable_int.hpp"
13 #include "propagate_allocator_test.hpp"
14 #include "default_init_test.hpp"
15 #include "../../intrusive/test/iterator_test.hpp"
16
17 #include <boost/container/allocator.hpp>
18 #include <boost/container/node_allocator.hpp>
19 #include <boost/container/adaptive_pool.hpp>
20
21 #include <iostream>
22
23 namespace boost {
24 namespace container {
25
26 template class small_vector<char, 0>;
27 template class small_vector<char, 1>;
28 template class small_vector<char, 2>;
29 template class small_vector<char, 10>;
30
31 template class small_vector<int, 0>;
32 template class small_vector<int, 1>;
33 template class small_vector<int, 2>;
34 template class small_vector<int, 10>;
35
36 //Explicit instantiation to detect compilation errors
37 template class boost::container::small_vector
38 < test::movable_and_copyable_int
39 , 10
40 , test::simple_allocator<test::movable_and_copyable_int> >;
41
42 template class boost::container::small_vector
43 < test::movable_and_copyable_int
44 , 10
45 , test::dummy_test_allocator<test::movable_and_copyable_int> >;
46
47 template class boost::container::small_vector
48 < test::movable_and_copyable_int
49 , 10
50 , std::allocator<test::movable_and_copyable_int> >;
51
52 template class boost::container::small_vector
53 < test::movable_and_copyable_int
54 , 10
55 , allocator<test::movable_and_copyable_int> >;
56
57 template class boost::container::small_vector
58 < test::movable_and_copyable_int
59 , 10
60 , adaptive_pool<test::movable_and_copyable_int> >;
61
62 template class boost::container::small_vector
63 < test::movable_and_copyable_int
64 , 10
65 , node_allocator<test::movable_and_copyable_int> >;
66
67 }}
68
69 struct boost_container_small_vector;
70
71 namespace boost { namespace container { namespace test {
72
73 template<>
74 struct alloc_propagate_base<boost_container_small_vector>
75 {
76 template <class T, class Allocator>
77 struct apply
78 {
79 typedef boost::container::small_vector<T, 10, Allocator> type;
80 };
81 };
82
83 }}} //namespace boost::container::test
84
test_small_vector_base_test()85 bool test_small_vector_base_test()
86 {
87 typedef boost::container::small_vector_base<int> smb_t;
88 {
89 typedef boost::container::small_vector<int, 5> sm5_t;
90 sm5_t sm5;
91 smb_t &smb = sm5;
92 smb.push_back(1);
93 sm5_t sm5_copy(sm5);
94 sm5_copy.push_back(1);
95 if (!boost::container::test::CheckEqualContainers(sm5, smb))
96 return false;
97 }
98 {
99 typedef boost::container::small_vector<int, 5> sm7_t;
100 sm7_t sm7;
101 smb_t &smb = sm7;
102 smb.push_back(2);
103 sm7_t sm7_copy(sm7);
104 sm7_copy.push_back(2);
105 if (!boost::container::test::CheckEqualContainers(sm7, smb))
106 return false;
107 }
108 return true;
109 }
110
main()111 int main()
112 {
113 using namespace boost::container;
114 /*
115 typedef small_vector<char, 0>::storage_test storage_test;
116 std::cout << "needed_extra_storages: " << storage_test::needed_extra_storages << '\n';
117 std::cout << "needed_bytes: " << storage_test::needed_bytes << '\n';
118 std::cout << "header_bytes: " << storage_test::header_bytes << '\n';
119 std::cout << "s_start: " << storage_test::s_start << '\n';
120
121 //char
122 std::cout << "sizeof(small_vector<char, 0>): " << sizeof(small_vector<char, 0>) << " extra: " << small_vector<char, 0>::needed_extra_storages << " internal storage: " << small_vector<char, 0>::internal_capacity() << '\n';
123 std::cout << "sizeof(small_vector<char, 1>): " << sizeof(small_vector<char, 1>) << " extra: " << small_vector<char, 1>::needed_extra_storages << " internal storage: " << small_vector<char, 1>::internal_capacity() << '\n';
124 std::cout << "sizeof(small_vector<char, 2>): " << sizeof(small_vector<char, 2>) << " extra: " << small_vector<char, 2>::needed_extra_storages << " internal storage: " << small_vector<char, 2>::internal_capacity() << '\n';
125 std::cout << "sizeof(small_vector<char, 3>): " << sizeof(small_vector<char, 3>) << " extra: " << small_vector<char, 3>::needed_extra_storages << " internal storage: " << small_vector<char, 3>::internal_capacity() << '\n';
126 std::cout << "sizeof(small_vector<char, 4>): " << sizeof(small_vector<char, 4>) << " extra: " << small_vector<char, 4>::needed_extra_storages << " internal storage: " << small_vector<char, 4>::internal_capacity() << '\n';
127 std::cout << "sizeof(small_vector<char, 5>): " << sizeof(small_vector<char, 5>) << " extra: " << small_vector<char, 5>::needed_extra_storages << " internal storage: " << small_vector<char, 5>::internal_capacity() << '\n';
128 std::cout << "\n";
129
130 //short
131 std::cout << "sizeof(small_vector<short, 0>): " << sizeof(small_vector<short, 0>) << " extra: " << small_vector<short, 0>::needed_extra_storages << " internal storage: " << small_vector<short, 0>::internal_capacity() << '\n';
132 std::cout << "sizeof(small_vector<short, 1>): " << sizeof(small_vector<short, 1>) << " extra: " << small_vector<short, 1>::needed_extra_storages << " internal storage: " << small_vector<short, 1>::internal_capacity() << '\n';
133 std::cout << "sizeof(small_vector<short, 2>): " << sizeof(small_vector<short, 2>) << " extra: " << small_vector<short, 2>::needed_extra_storages << " internal storage: " << small_vector<short, 2>::internal_capacity() << '\n';
134 std::cout << "sizeof(small_vector<short, 3>): " << sizeof(small_vector<short, 3>) << " extra: " << small_vector<short, 3>::needed_extra_storages << " internal storage: " << small_vector<short, 3>::internal_capacity() << '\n';
135 std::cout << "sizeof(small_vector<short, 4>): " << sizeof(small_vector<short, 4>) << " extra: " << small_vector<short, 4>::needed_extra_storages << " internal storage: " << small_vector<short, 4>::internal_capacity() << '\n';
136 std::cout << "sizeof(small_vector<short, 5>): " << sizeof(small_vector<short, 5>) << " extra: " << small_vector<short, 5>::needed_extra_storages << " internal storage: " << small_vector<short, 5>::internal_capacity() << '\n';
137 */
138 if(test::vector_test< small_vector<int, 0> >())
139 return 1;
140
141 if(test::vector_test< small_vector<int, 2000> >())
142 return 1;
143
144
145 ////////////////////////////////////
146 // Default init test
147 ////////////////////////////////////
148 if(!test::default_init_test< vector<int, test::default_init_allocator<int> > >()){
149 std::cerr << "Default init test failed" << std::endl;
150 return 1;
151 }
152
153 ////////////////////////////////////
154 // Emplace testing
155 ////////////////////////////////////
156 const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_BEFORE);
157 if(!boost::container::test::test_emplace< vector<test::EmplaceInt>, Options>()){
158 return 1;
159 }
160
161 ////////////////////////////////////
162 // Allocator propagation testing
163 ////////////////////////////////////
164 if(!boost::container::test::test_propagate_allocator<boost_container_small_vector>()){
165 return 1;
166 }
167
168 ////////////////////////////////////
169 // Initializer lists testing
170 ////////////////////////////////////
171 if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for
172 < boost::container::small_vector<int, 5> >()) {
173 return 1;
174 }
175
176 ////////////////////////////////////
177 // Small vector base
178 ////////////////////////////////////
179 if (!test_small_vector_base_test()){
180 return 1;
181 }
182
183 ////////////////////////////////////
184 // Iterator testing
185 ////////////////////////////////////
186 {
187 typedef boost::container::small_vector<int, 0> cont_int;
188 cont_int a; a.push_back(0); a.push_back(1); a.push_back(2);
189 boost::intrusive::test::test_iterator_random< cont_int >(a);
190 if(boost::report_errors() != 0) {
191 return 1;
192 }
193 }
194
195 return 0;
196 }
197