]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 "../helpers/prefix.hpp" | |
7 | #include <boost/unordered_set.hpp> | |
8 | #include <boost/unordered_map.hpp> | |
9 | #include "../helpers/postfix.hpp" | |
10 | ||
11 | #include <boost/config.hpp> | |
12 | #include <algorithm> | |
13 | #include <iterator> | |
14 | #include "../helpers/test.hpp" | |
15 | #include "../objects/test.hpp" | |
16 | #include "../objects/cxx11_allocator.hpp" | |
17 | #include "../helpers/random_values.hpp" | |
18 | #include "../helpers/tracker.hpp" | |
19 | #include "../helpers/invariants.hpp" | |
20 | ||
21 | #if defined(BOOST_MSVC) | |
22 | #pragma warning(disable:4127) // conditional expression is constant | |
23 | #endif | |
24 | ||
25 | namespace swap_tests | |
26 | { | |
27 | ||
28 | test::seed_t initialize_seed(783472); | |
29 | ||
30 | template <class X> | |
31 | void swap_test_impl(X& x1, X& x2) | |
32 | { | |
33 | test::ordered<X> tracker1 = test::create_ordered(x1); | |
34 | test::ordered<X> tracker2 = test::create_ordered(x2); | |
35 | tracker1.insert_range(x1.begin(), x1.end()); | |
36 | tracker2.insert_range(x2.begin(), x2.end()); | |
37 | x1.swap(x2); | |
38 | tracker1.compare(x2); | |
39 | tracker2.compare(x1); | |
40 | } | |
41 | ||
42 | template <class X> | |
43 | void swap_tests1(X*, test::random_generator generator) | |
44 | { | |
45 | { | |
46 | test::check_instances check_; | |
47 | ||
48 | X x; | |
49 | swap_test_impl(x, x); | |
50 | } | |
51 | ||
52 | { | |
53 | test::check_instances check_; | |
54 | ||
55 | X x,y; | |
56 | swap_test_impl(x, y); | |
57 | } | |
58 | ||
59 | { | |
60 | test::check_instances check_; | |
61 | ||
62 | test::random_values<X> v(1000, generator); | |
63 | X x, y(v.begin(), v.end()); | |
64 | swap_test_impl(x, y); | |
65 | swap_test_impl(x, y); | |
66 | } | |
67 | ||
68 | { | |
69 | test::check_instances check_; | |
70 | ||
71 | test::random_values<X> vx(1000, generator), vy(1000, generator); | |
72 | X x(vx.begin(), vx.end()), y(vy.begin(), vy.end()); | |
73 | swap_test_impl(x, y); | |
74 | swap_test_impl(x, y); | |
75 | } | |
76 | } | |
77 | ||
78 | template <class X> | |
79 | void swap_tests2(X* ptr, test::random_generator generator) | |
80 | { | |
81 | swap_tests1(ptr, generator); | |
82 | ||
83 | typedef BOOST_DEDUCED_TYPENAME X::hasher hasher; | |
84 | typedef BOOST_DEDUCED_TYPENAME X::key_equal key_equal; | |
85 | typedef BOOST_DEDUCED_TYPENAME X::allocator_type allocator_type; | |
86 | ||
87 | { | |
88 | test::check_instances check_; | |
89 | ||
90 | X x(0, hasher(1), key_equal(1)); | |
91 | X y(0, hasher(2), key_equal(2)); | |
92 | swap_test_impl(x, y); | |
93 | } | |
94 | ||
95 | { | |
96 | test::check_instances check_; | |
97 | ||
98 | test::random_values<X> v(1000, generator); | |
99 | X x(v.begin(), v.end(), 0, hasher(1), key_equal(1)); | |
100 | X y(0, hasher(2), key_equal(2)); | |
101 | swap_test_impl(x, y); | |
102 | } | |
103 | ||
104 | { | |
105 | test::check_instances check_; | |
106 | ||
107 | test::random_values<X> vx(100, generator), vy(50, generator); | |
108 | X x(vx.begin(), vx.end(), 0, hasher(1), key_equal(1)); | |
109 | X y(vy.begin(), vy.end(), 0, hasher(2), key_equal(2)); | |
110 | swap_test_impl(x, y); | |
111 | swap_test_impl(x, y); | |
112 | } | |
113 | ||
114 | { | |
115 | test::force_equal_allocator force_( | |
116 | !allocator_type::is_propagate_on_swap); | |
117 | test::check_instances check_; | |
118 | ||
119 | test::random_values<X> vx(50, generator), vy(100, generator); | |
120 | X x(vx.begin(), vx.end(), 0, hasher(), key_equal(), allocator_type(1)); | |
121 | X y(vy.begin(), vy.end(), 0, hasher(), key_equal(), allocator_type(2)); | |
122 | ||
123 | if (allocator_type::is_propagate_on_swap || | |
124 | x.get_allocator() == y.get_allocator()) | |
125 | { | |
126 | swap_test_impl(x, y); | |
127 | } | |
128 | } | |
129 | ||
130 | { | |
131 | test::force_equal_allocator force_( | |
132 | !allocator_type::is_propagate_on_swap); | |
133 | test::check_instances check_; | |
134 | ||
135 | test::random_values<X> vx(100, generator), vy(100, generator); | |
136 | X x(vx.begin(), vx.end(), 0, hasher(1), key_equal(1), | |
137 | allocator_type(1)); | |
138 | X y(vy.begin(), vy.end(), 0, hasher(2), key_equal(2), | |
139 | allocator_type(2)); | |
140 | ||
141 | if (allocator_type::is_propagate_on_swap || | |
142 | x.get_allocator() == y.get_allocator()) | |
143 | { | |
144 | swap_test_impl(x, y); | |
145 | swap_test_impl(x, y); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | boost::unordered_map<test::object, test::object, | |
151 | test::hash, test::equal_to, | |
152 | std::allocator<test::object> >* test_map_std_alloc; | |
153 | ||
154 | boost::unordered_set<test::object, | |
155 | test::hash, test::equal_to, | |
156 | test::allocator1<test::object> >* test_set; | |
157 | boost::unordered_multiset<test::object, | |
158 | test::hash, test::equal_to, | |
159 | test::allocator2<test::object> >* test_multiset; | |
160 | boost::unordered_map<test::object, test::object, | |
161 | test::hash, test::equal_to, | |
162 | test::allocator1<test::object> >* test_map; | |
163 | boost::unordered_multimap<test::object, test::object, | |
164 | test::hash, test::equal_to, | |
165 | test::allocator2<test::object> >* test_multimap; | |
166 | ||
167 | boost::unordered_set<test::object, | |
168 | test::hash, test::equal_to, | |
169 | test::cxx11_allocator<test::object, test::propagate_swap> >* | |
170 | test_set_prop_swap; | |
171 | boost::unordered_multiset<test::object, | |
172 | test::hash, test::equal_to, | |
173 | test::cxx11_allocator<test::object, test::propagate_swap> >* | |
174 | test_multiset_prop_swap; | |
175 | boost::unordered_map<test::object, test::object, | |
176 | test::hash, test::equal_to, | |
177 | test::cxx11_allocator<test::object, test::propagate_swap> >* | |
178 | test_map_prop_swap; | |
179 | boost::unordered_multimap<test::object, test::object, | |
180 | test::hash, test::equal_to, | |
181 | test::cxx11_allocator<test::object, test::propagate_swap> >* | |
182 | test_multimap_prop_swap; | |
183 | ||
184 | boost::unordered_set<test::object, | |
185 | test::hash, test::equal_to, | |
186 | test::cxx11_allocator<test::object, test::no_propagate_swap> >* | |
187 | test_set_no_prop_swap; | |
188 | boost::unordered_multiset<test::object, | |
189 | test::hash, test::equal_to, | |
190 | test::cxx11_allocator<test::object, test::no_propagate_swap> >* | |
191 | test_multiset_no_prop_swap; | |
192 | boost::unordered_map<test::object, test::object, | |
193 | test::hash, test::equal_to, | |
194 | test::cxx11_allocator<test::object, test::no_propagate_swap> >* | |
195 | test_map_no_prop_swap; | |
196 | boost::unordered_multimap<test::object, test::object, | |
197 | test::hash, test::equal_to, | |
198 | test::cxx11_allocator<test::object, test::no_propagate_swap> >* | |
199 | test_multimap_no_prop_swap; | |
200 | ||
201 | template <typename T> | |
202 | bool is_propagate(T*) | |
203 | { | |
204 | return T::allocator_type::is_propagate_on_swap; | |
205 | } | |
206 | ||
207 | using test::default_generator; | |
208 | using test::generate_collisions; | |
209 | using test::limited_range; | |
210 | ||
211 | UNORDERED_AUTO_TEST(check_traits) | |
212 | { | |
213 | BOOST_TEST(!is_propagate(test_set)); | |
214 | BOOST_TEST(is_propagate(test_set_prop_swap)); | |
215 | BOOST_TEST(!is_propagate(test_set_no_prop_swap)); | |
216 | } | |
217 | ||
218 | UNORDERED_TEST(swap_tests1, ( | |
219 | (test_map_std_alloc) | |
220 | (test_set)(test_multiset)(test_map)(test_multimap) | |
221 | (test_set_prop_swap)(test_multiset_prop_swap)(test_map_prop_swap)(test_multimap_prop_swap) | |
222 | (test_set_no_prop_swap)(test_multiset_no_prop_swap)(test_map_no_prop_swap)(test_multimap_no_prop_swap) | |
223 | ) | |
224 | ((default_generator)(generate_collisions)(limited_range)) | |
225 | ) | |
226 | ||
227 | UNORDERED_TEST(swap_tests2, ( | |
228 | (test_set)(test_multiset)(test_map)(test_multimap) | |
229 | (test_set_prop_swap)(test_multiset_prop_swap)(test_map_prop_swap)(test_multimap_prop_swap) | |
230 | (test_set_no_prop_swap)(test_multiset_no_prop_swap)(test_map_no_prop_swap)(test_multimap_no_prop_swap) | |
231 | ) | |
232 | ((default_generator)(generate_collisions)(limited_range)) | |
233 | ) | |
234 | ||
235 | } | |
236 | RUN_TESTS() |