]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/unordered/test/exception/insert_exception_tests.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / unordered / test / exception / insert_exception_tests.cpp
1
2 // Copyright 2006-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include "./containers.hpp"
7 #include <string>
8 #include "../helpers/random_values.hpp"
9 #include "../helpers/invariants.hpp"
10 #include "../helpers/strong.hpp"
11 #include "../helpers/helpers.hpp"
12 #include <cmath>
13
14 test::seed_t initialize_seed(747373);
15
16 template <class T>
17 struct insert_test_base : public test::exception_base
18 {
19 test::random_values<T> values;
20 insert_test_base(unsigned int count = 5) : values(count, test::limited_range) {}
21
22 typedef T data_type;
23 typedef test::strong<T> strong_type;
24
25 data_type init() const {
26 return T();
27 }
28
29 void check BOOST_PREVENT_MACRO_SUBSTITUTION(
30 T const& x, strong_type const& strong) const
31 {
32 std::string scope(test::scope);
33
34 if(scope.find("hash::operator()") == std::string::npos)
35 strong.test(x, test::detail::tracker.count_allocations);
36 test::check_equivalent_keys(x);
37 }
38 };
39
40 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
41
42 template <class T>
43 struct emplace_test1 : public insert_test_base<T>
44 {
45 typedef BOOST_DEDUCED_TYPENAME insert_test_base<T>::strong_type strong_type;
46
47 void run(T& x, strong_type& strong) const {
48 for(BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
49 it = this->values.begin(), end = this->values.end();
50 it != end; ++it)
51 {
52 strong.store(x, test::detail::tracker.count_allocations);
53 x.emplace(*it);
54 }
55 }
56 };
57
58 #endif
59
60 template <class T>
61 struct insert_test1 : public insert_test_base<T>
62 {
63 typedef BOOST_DEDUCED_TYPENAME insert_test_base<T>::strong_type strong_type;
64
65 void run(T& x, strong_type& strong) const {
66 for(BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
67 it = this->values.begin(), end = this->values.end();
68 it != end; ++it)
69 {
70 strong.store(x, test::detail::tracker.count_allocations);
71 x.insert(*it);
72 }
73 }
74 };
75
76 template <class T>
77 struct insert_test2 : public insert_test_base<T>
78 {
79 typedef BOOST_DEDUCED_TYPENAME insert_test_base<T>::strong_type strong_type;
80
81 void run(T& x, strong_type& strong) const {
82 for(BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
83 it = this->values.begin(), end = this->values.end();
84 it != end; ++it)
85 {
86 strong.store(x, test::detail::tracker.count_allocations);
87 x.insert(x.begin(), *it);
88 }
89 }
90 };
91
92 template <class T>
93 struct insert_test3 : public insert_test_base<T>
94 {
95 void run(T& x) const {
96 x.insert(this->values.begin(), this->values.end());
97 }
98
99 void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const {
100 test::check_equivalent_keys(x);
101 }
102 };
103
104 template <class T>
105 struct insert_test4 : public insert_test_base<T>
106 {
107 typedef BOOST_DEDUCED_TYPENAME insert_test_base<T>::strong_type strong_type;
108
109 void run(T& x, strong_type& strong) const {
110 for(BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
111 it = this->values.begin(), end = this->values.end();
112 it != end; ++it)
113 {
114 strong.store(x, test::detail::tracker.count_allocations);
115 x.insert(it, test::next(it));
116 }
117 }
118 };
119
120 template <class T>
121 struct insert_test_rehash1 : public insert_test_base<T>
122 {
123 typedef BOOST_DEDUCED_TYPENAME insert_test_base<T>::strong_type strong_type;
124
125 insert_test_rehash1() : insert_test_base<T>(1000) {}
126
127 T init() const {
128 using namespace std;
129 typedef BOOST_DEDUCED_TYPENAME T::size_type size_type;
130
131 T x;
132 x.max_load_factor(0.25);
133 // TODO: This doesn't really work is bucket_count is 0
134 size_type bucket_count = x.bucket_count();
135 size_type initial_elements = static_cast<size_type>(
136 ceil((double) bucket_count * (double) x.max_load_factor()) - 1);
137 BOOST_TEST(initial_elements < this->values.size());
138 x.insert(this->values.begin(),
139 test::next(this->values.begin(), initial_elements));
140 BOOST_TEST(bucket_count == x.bucket_count());
141 return x;
142 }
143
144 void run(T& x, strong_type& strong) const {
145 BOOST_DEDUCED_TYPENAME T::size_type bucket_count = x.bucket_count();
146 int count = 0;
147 BOOST_DEDUCED_TYPENAME T::const_iterator pos = x.cbegin();
148
149 for(BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
150 it = test::next(this->values.begin(), x.size()),
151 end = this->values.end();
152 it != end && count < 10; ++it, ++count)
153 {
154 strong.store(x, test::detail::tracker.count_allocations);
155 pos = x.insert(pos, *it);
156 }
157
158 // This isn't actually a failure, but it means the test isn't doing its
159 // job.
160 BOOST_TEST(x.bucket_count() != bucket_count);
161 }
162 };
163
164 template <class T>
165 struct insert_test_rehash2 : public insert_test_rehash1<T>
166 {
167 typedef BOOST_DEDUCED_TYPENAME insert_test_base<T>::strong_type strong_type;
168
169 void run(T& x, strong_type& strong) const {
170 BOOST_DEDUCED_TYPENAME T::size_type bucket_count = x.bucket_count();
171 int count = 0;
172
173 for(BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
174 it = test::next(this->values.begin(), x.size()),
175 end = this->values.end();
176 it != end && count < 10; ++it, ++count)
177 {
178 strong.store(x, test::detail::tracker.count_allocations);
179 x.insert(*it);
180 }
181
182 // This isn't actually a failure, but it means the test isn't doing its
183 // job.
184 BOOST_TEST(x.bucket_count() != bucket_count);
185 }
186 };
187
188 template <class T>
189 struct insert_test_rehash3 : public insert_test_base<T>
190 {
191 BOOST_DEDUCED_TYPENAME T::size_type mutable
192 rehash_bucket_count, original_bucket_count;
193
194 insert_test_rehash3() : insert_test_base<T>(1000) {}
195
196 T init() const {
197 using namespace std;
198 typedef BOOST_DEDUCED_TYPENAME T::size_type size_type;
199
200 T x;
201 x.max_load_factor(0.25);
202
203 original_bucket_count = x.bucket_count();
204 rehash_bucket_count = static_cast<size_type>(
205 ceil((double) original_bucket_count * (double) x.max_load_factor())) - 1;
206
207 size_type initial_elements =
208 rehash_bucket_count > 5 ? rehash_bucket_count - 5 : 1;
209
210 BOOST_TEST(initial_elements < this->values.size());
211 x.insert(this->values.begin(),
212 test::next(this->values.begin(), initial_elements));
213 BOOST_TEST(original_bucket_count == x.bucket_count());
214 return x;
215 }
216
217 void run(T& x) const {
218 BOOST_DEDUCED_TYPENAME T::size_type bucket_count = x.bucket_count();
219
220 x.insert(test::next(this->values.begin(), x.size()),
221 test::next(this->values.begin(), x.size() + 20));
222
223 // This isn't actually a failure, but it means the test isn't doing its
224 // job.
225 BOOST_TEST(x.bucket_count() != bucket_count);
226 }
227
228 void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const {
229 if(x.size() < rehash_bucket_count) {
230 //BOOST_TEST(x.bucket_count() == original_bucket_count);
231 }
232 test::check_equivalent_keys(x);
233 }
234 };
235
236 #define BASIC_TESTS \
237 (insert_test1)(insert_test2)(insert_test3)(insert_test4) \
238 (insert_test_rehash1)(insert_test_rehash2)(insert_test_rehash3)
239
240 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
241 #define ALL_TESTS (emplace_test1)BASIC_TESTS
242 #else
243 #define ALL_TESTS BASIC_TESTS
244 #endif
245
246
247 EXCEPTION_TESTS(ALL_TESTS, CONTAINER_SEQ)
248 RUN_TESTS()