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)
7 #include "../helpers/prefix.hpp"
8 #include <boost/unordered_set.hpp>
9 #include <boost/unordered_map.hpp>
10 #include "../helpers/postfix.hpp"
13 #include "../helpers/test.hpp"
14 #include "../objects/test.hpp"
15 #include "../objects/cxx11_allocator.hpp"
16 #include "../helpers/random_values.hpp"
17 #include "../helpers/tracker.hpp"
18 #include "../helpers/equivalent.hpp"
20 #if defined(BOOST_MSVC)
21 #pragma warning(disable : 4127) // conditional expression is constant
24 namespace assign_tests
{
26 test::seed_t
initialize_seed(96785);
28 template <class T
> void assign_tests1(T
*, test::random_generator generator
)
30 BOOST_DEDUCED_TYPENAME
T::hasher hf
;
31 BOOST_DEDUCED_TYPENAME
T::key_equal eq
;
33 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests1.1\n";
35 test::check_instances check_
;
39 BOOST_TEST(x
.empty());
40 BOOST_TEST(test::equivalent(x
.hash_function(), hf
));
41 BOOST_TEST(test::equivalent(x
.key_eq(), eq
));
44 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests1.2\n";
46 test::check_instances check_
;
48 test::random_values
<T
> v(1000, generator
);
49 T
x(v
.begin(), v
.end());
51 test::ordered
<T
> tracker
= test::create_ordered(x
);
52 tracker
.insert_range(v
.begin(), v
.end());
58 y
.max_load_factor(x
.max_load_factor() / 20);
59 float mlf
= x
.max_load_factor();
63 BOOST_TEST(x
.max_load_factor() == mlf
);
64 BOOST_TEST(y
.max_load_factor() == mlf
);
65 BOOST_TEST(y
.load_factor() <= y
.max_load_factor());
69 template <class T
> void assign_tests2(T
*, test::random_generator generator
)
71 BOOST_DEDUCED_TYPENAME
T::hasher
hf1(1);
72 BOOST_DEDUCED_TYPENAME
T::hasher
hf2(2);
73 BOOST_DEDUCED_TYPENAME
T::key_equal
eq1(1);
74 BOOST_DEDUCED_TYPENAME
T::key_equal
eq2(2);
75 BOOST_DEDUCED_TYPENAME
T::allocator_type
al1(1);
76 BOOST_DEDUCED_TYPENAME
T::allocator_type
al2(2);
78 typedef BOOST_DEDUCED_TYPENAME
T::allocator_type allocator_type
;
80 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests2.0 - empty container\n";
82 test::check_instances check_
;
87 BOOST_TEST(test::equivalent(x1
.hash_function(), hf1
));
88 BOOST_TEST(test::equivalent(x1
.key_eq(), eq1
));
89 BOOST_TEST(test::equivalent(x2
.hash_function(), hf1
));
90 BOOST_TEST(test::equivalent(x2
.key_eq(), eq1
));
91 test::check_container(x1
, x2
);
94 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests2.1\n";
96 test::check_instances check_
;
98 test::random_values
<T
> v(1000, generator
);
99 T
x1(v
.begin(), v
.end(), 0, hf1
, eq1
);
102 BOOST_TEST(test::equivalent(x1
.hash_function(), hf1
));
103 BOOST_TEST(test::equivalent(x1
.key_eq(), eq1
));
104 BOOST_TEST(test::equivalent(x2
.hash_function(), hf1
));
105 BOOST_TEST(test::equivalent(x2
.key_eq(), eq1
));
106 test::check_container(x1
, v
);
107 test::check_container(x2
, v
);
108 BOOST_TEST(x2
.load_factor() <= x2
.max_load_factor());
111 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests2.1a\n";
113 test::check_instances check_
;
115 test::random_values
<T
> v1(0, generator
);
116 test::random_values
<T
> v2(1000, generator
);
118 T
x2(v2
.begin(), v2
.end(), 0, hf1
, eq1
);
120 BOOST_TEST(test::equivalent(x1
.hash_function(), hf2
));
121 BOOST_TEST(test::equivalent(x1
.key_eq(), eq2
));
122 BOOST_TEST(test::equivalent(x2
.hash_function(), hf2
));
123 BOOST_TEST(test::equivalent(x2
.key_eq(), eq2
));
124 test::check_container(x1
, v1
);
125 test::check_container(x2
, v1
);
126 BOOST_TEST(x2
.load_factor() <= x2
.max_load_factor());
129 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests2.2\n";
131 test::check_instances check_
;
133 test::random_values
<T
> v1(100, generator
), v2(100, generator
);
134 T
x1(v1
.begin(), v1
.end(), 0, hf1
, eq1
, al1
);
135 T
x2(v2
.begin(), v2
.end(), 0, hf2
, eq2
, al2
);
137 BOOST_TEST(test::equivalent(x2
.hash_function(), hf1
));
138 BOOST_TEST(test::equivalent(x2
.key_eq(), eq1
));
139 if (allocator_type::is_propagate_on_assign
) {
140 BOOST_TEST(test::equivalent(x2
.get_allocator(), al1
));
141 BOOST_TEST(!test::equivalent(x2
.get_allocator(), al2
));
143 BOOST_TEST(test::equivalent(x2
.get_allocator(), al2
));
144 BOOST_TEST(!test::equivalent(x2
.get_allocator(), al1
));
146 test::check_container(x1
, v1
);
147 test::check_container(x2
, v1
);
148 BOOST_TEST(x2
.load_factor() <= x2
.max_load_factor());
151 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests2.3\n";
153 test::check_instances check_
;
155 test::random_values
<T
> v1(100, generator
), v2(1000, generator
);
156 T
x1(v1
.begin(), v1
.end(), 0, hf1
, eq1
, al1
);
157 T
x2(v2
.begin(), v2
.end(), 0, hf2
, eq2
, al2
);
159 BOOST_TEST(test::equivalent(x2
.hash_function(), hf1
));
160 BOOST_TEST(test::equivalent(x2
.key_eq(), eq1
));
161 if (allocator_type::is_propagate_on_assign
) {
162 BOOST_TEST(test::equivalent(x2
.get_allocator(), al1
));
163 BOOST_TEST(!test::equivalent(x2
.get_allocator(), al2
));
165 BOOST_TEST(test::equivalent(x2
.get_allocator(), al2
));
166 BOOST_TEST(!test::equivalent(x2
.get_allocator(), al1
));
168 test::check_container(x1
, v1
);
169 test::check_container(x2
, v1
);
170 BOOST_TEST(x2
.load_factor() <= x2
.max_load_factor());
173 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "assign_tests2.4\n";
175 test::check_instances check_
;
177 test::random_values
<T
> v1(1000, generator
), v2(100, generator
);
178 T
x1(v1
.begin(), v1
.end(), 0, hf1
, eq1
, al1
);
179 T
x2(v2
.begin(), v2
.end(), 0, hf2
, eq2
, al2
);
181 BOOST_TEST(test::equivalent(x2
.hash_function(), hf1
));
182 BOOST_TEST(test::equivalent(x2
.key_eq(), eq1
));
183 if (allocator_type::is_propagate_on_assign
) {
184 BOOST_TEST(test::equivalent(x2
.get_allocator(), al1
));
185 BOOST_TEST(!test::equivalent(x2
.get_allocator(), al2
));
187 BOOST_TEST(test::equivalent(x2
.get_allocator(), al2
));
188 BOOST_TEST(!test::equivalent(x2
.get_allocator(), al1
));
190 test::check_container(x1
, v1
);
191 test::check_container(x2
, v1
);
192 BOOST_TEST(x2
.load_factor() <= x2
.max_load_factor());
196 boost::unordered_map
<test::object
, test::object
, test::hash
, test::equal_to
,
197 std::allocator
<test::object
> >* test_map_std_alloc
;
199 boost::unordered_set
<test::object
, test::hash
, test::equal_to
,
200 test::allocator1
<test::object
> >* test_set
;
201 boost::unordered_multiset
<test::object
, test::hash
, test::equal_to
,
202 test::allocator2
<test::object
> >* test_multiset
;
203 boost::unordered_map
<test::object
, test::object
, test::hash
, test::equal_to
,
204 test::allocator2
<test::object
> >* test_map
;
205 boost::unordered_multimap
<test::object
, test::object
, test::hash
,
206 test::equal_to
, test::allocator1
<test::object
> >* test_multimap
;
208 boost::unordered_set
<test::object
, test::hash
, test::equal_to
,
209 test::cxx11_allocator
<test::object
, test::propagate_assign
> >*
210 test_set_prop_assign
;
211 boost::unordered_multiset
<test::object
, test::hash
, test::equal_to
,
212 test::cxx11_allocator
<test::object
, test::propagate_assign
> >*
213 test_multiset_prop_assign
;
214 boost::unordered_map
<test::object
, test::object
, test::hash
, test::equal_to
,
215 test::cxx11_allocator
<test::object
, test::propagate_assign
> >*
216 test_map_prop_assign
;
217 boost::unordered_multimap
<test::object
, test::object
, test::hash
,
219 test::cxx11_allocator
<test::object
, test::propagate_assign
> >*
220 test_multimap_prop_assign
;
222 boost::unordered_set
<test::object
, test::hash
, test::equal_to
,
223 test::cxx11_allocator
<test::object
, test::no_propagate_assign
> >*
224 test_set_no_prop_assign
;
225 boost::unordered_multiset
<test::object
, test::hash
, test::equal_to
,
226 test::cxx11_allocator
<test::object
, test::no_propagate_assign
> >*
227 test_multiset_no_prop_assign
;
228 boost::unordered_map
<test::object
, test::object
, test::hash
, test::equal_to
,
229 test::cxx11_allocator
<test::object
, test::no_propagate_assign
> >*
230 test_map_no_prop_assign
;
231 boost::unordered_multimap
<test::object
, test::object
, test::hash
,
233 test::cxx11_allocator
<test::object
, test::no_propagate_assign
> >*
234 test_multimap_no_prop_assign
;
236 using test::default_generator
;
237 using test::generate_collisions
;
238 using test::limited_range
;
240 template <typename T
> bool is_propagate(T
*)
242 return T::allocator_type::is_propagate_on_assign
;
245 UNORDERED_AUTO_TEST (check_traits
) {
246 BOOST_TEST(!is_propagate(test_set
));
247 BOOST_TEST(is_propagate(test_set_prop_assign
));
248 BOOST_TEST(!is_propagate(test_set_no_prop_assign
));
251 UNORDERED_TEST(assign_tests1
,
252 ((test_map_std_alloc
)(test_set
)(test_multiset
)(test_map
)(test_multimap
)(
253 test_set_prop_assign
)(test_multiset_prop_assign
)(test_map_prop_assign
)(
254 test_multimap_prop_assign
)(test_set_no_prop_assign
)(
255 test_multiset_no_prop_assign
)(test_map_no_prop_assign
)(
256 test_multimap_no_prop_assign
))(
257 (default_generator
)(generate_collisions
)(limited_range
)))
260 assign_tests2
, ((test_set
)(test_multiset
)(test_map
)(test_multimap
)(
261 test_set_prop_assign
)(test_multiset_prop_assign
)(
262 test_map_prop_assign
)(test_multimap_prop_assign
)(
263 test_set_no_prop_assign
)(test_multiset_no_prop_assign
)(
264 test_map_no_prop_assign
)(test_multimap_no_prop_assign
))(
265 (default_generator
)(generate_collisions
)(limited_range
)))
267 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
269 UNORDERED_AUTO_TEST (assign_default_initializer_list
) {
270 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "Initializer List Tests\n";
271 std::initializer_list
<std::pair
<int const, int> > init
;
272 boost::unordered_map
<int, int> x1
;
275 BOOST_TEST(!x1
.empty());
277 BOOST_TEST(x1
.empty());
282 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
283 UNORDERED_AUTO_TEST (assign_initializer_list
) {
284 BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "Initializer List Tests\n";
286 boost::unordered_set
<int> x
;
290 BOOST_TEST(x
.find(10) == x
.end());
291 BOOST_TEST(x
.find(-10) != x
.end());