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 // clang-format off 7 #include "../helpers/prefix.hpp" 8 #include <boost/unordered_set.hpp> 9 #include <boost/unordered_map.hpp> 10 #include "../helpers/postfix.hpp" 11 // clang-format on 12 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" 19 20 #if defined(BOOST_MSVC) 21 #pragma warning(disable : 4127) // conditional expression is constant 22 #endif 23 24 namespace assign_tests { 25 26 test::seed_t initialize_seed(96785); 27 assign_tests1(T *,test::random_generator generator)28 template <class T> void assign_tests1(T*, test::random_generator generator) 29 { 30 typename T::hasher hf; 31 typename T::key_equal eq; 32 33 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests1.1\n"; 34 { 35 test::check_instances check_; 36 37 T x; 38 x = x; 39 BOOST_TEST(x.empty()); 40 BOOST_TEST(test::equivalent(x.hash_function(), hf)); 41 BOOST_TEST(test::equivalent(x.key_eq(), eq)); 42 } 43 44 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests1.2\n"; 45 { 46 test::check_instances check_; 47 48 test::random_values<T> v(1000, generator); 49 T x(v.begin(), v.end()); 50 51 test::ordered<T> tracker = test::create_ordered(x); 52 tracker.insert_range(v.begin(), v.end()); 53 54 x = x; 55 tracker.compare(x); 56 57 T y; 58 y.max_load_factor(x.max_load_factor() / 20); 59 float mlf = x.max_load_factor(); 60 y = x; 61 tracker.compare(x); 62 tracker.compare(y); 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()); 66 } 67 } 68 assign_tests2(T *,test::random_generator generator)69 template <class T> void assign_tests2(T*, test::random_generator generator) 70 { 71 typename T::hasher hf1(1); 72 typename T::hasher hf2(2); 73 typename T::key_equal eq1(1); 74 typename T::key_equal eq2(2); 75 typename T::allocator_type al1(1); 76 typename T::allocator_type al2(2); 77 78 typedef typename T::allocator_type allocator_type; 79 80 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests2.0 - empty container\n"; 81 { 82 test::check_instances check_; 83 84 T x1(0, hf1, eq1); 85 T x2(0, hf2, eq2); 86 x2 = x1; 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); 92 } 93 94 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests2.1\n"; 95 { 96 test::check_instances check_; 97 98 test::random_values<T> v(1000, generator); 99 T x1(v.begin(), v.end(), 0, hf1, eq1); 100 T x2(0, hf2, eq2); 101 x2 = x1; 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()); 109 } 110 111 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests2.1a\n"; 112 { 113 test::check_instances check_; 114 115 test::random_values<T> v1(0, generator); 116 test::random_values<T> v2(1000, generator); 117 T x1(0, hf2, eq2); 118 T x2(v2.begin(), v2.end(), 0, hf1, eq1); 119 x2 = x1; 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()); 127 } 128 129 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests2.2\n"; 130 { 131 test::check_instances check_; 132 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); 136 x2 = x1; 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)); 142 } else { 143 BOOST_TEST(test::equivalent(x2.get_allocator(), al2)); 144 BOOST_TEST(!test::equivalent(x2.get_allocator(), al1)); 145 } 146 test::check_container(x1, v1); 147 test::check_container(x2, v1); 148 BOOST_TEST(x2.load_factor() <= x2.max_load_factor()); 149 } 150 151 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests2.3\n"; 152 { 153 test::check_instances check_; 154 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); 158 x2 = x1; 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)); 164 } else { 165 BOOST_TEST(test::equivalent(x2.get_allocator(), al2)); 166 BOOST_TEST(!test::equivalent(x2.get_allocator(), al1)); 167 } 168 test::check_container(x1, v1); 169 test::check_container(x2, v1); 170 BOOST_TEST(x2.load_factor() <= x2.max_load_factor()); 171 } 172 173 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "assign_tests2.4\n"; 174 { 175 test::check_instances check_; 176 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); 180 x2 = x1; 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)); 186 } else { 187 BOOST_TEST(test::equivalent(x2.get_allocator(), al2)); 188 BOOST_TEST(!test::equivalent(x2.get_allocator(), al1)); 189 } 190 test::check_container(x1, v1); 191 test::check_container(x2, v1); 192 BOOST_TEST(x2.load_factor() <= x2.max_load_factor()); 193 } 194 } 195 196 boost::unordered_map<test::object, test::object, test::hash, test::equal_to, 197 std::allocator<test::object> >* test_map_std_alloc; 198 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; 207 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, 218 test::equal_to, 219 test::cxx11_allocator<test::object, test::propagate_assign> >* 220 test_multimap_prop_assign; 221 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, 232 test::equal_to, 233 test::cxx11_allocator<test::object, test::no_propagate_assign> >* 234 test_multimap_no_prop_assign; 235 236 using test::default_generator; 237 using test::generate_collisions; 238 using test::limited_range; 239 is_propagate(T *)240 template <typename T> bool is_propagate(T*) 241 { 242 return T::allocator_type::is_propagate_on_assign; 243 } 244 UNORDERED_AUTO_TEST(check_traits)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)); 249 } 250 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))) 258 259 UNORDERED_TEST( 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))) 266 267 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) 268 UNORDERED_AUTO_TEST(assign_default_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; 273 x1[25] = 3; 274 x1[16] = 10; 275 BOOST_TEST(!x1.empty()); 276 x1 = init; 277 BOOST_TEST(x1.empty()); 278 } 279 280 #endif 281 282 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) UNORDERED_AUTO_TEST(assign_initializer_list)283 UNORDERED_AUTO_TEST (assign_initializer_list) { 284 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Initializer List Tests\n"; 285 286 boost::unordered_set<int> x; 287 x.insert(10); 288 x.insert(20); 289 x = {1, 2, -10}; 290 BOOST_TEST(x.find(10) == x.end()); 291 BOOST_TEST(x.find(-10) != x.end()); 292 } 293 294 #endif 295 } 296 297 RUN_TESTS() 298