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 #include "../helpers/invariants.hpp" 20 21 test::seed_t initialize_seed(9063); 22 23 namespace copy_tests { 24 25 template <class T> copy_construct_tests1(T *,test::random_generator const & generator)26 void copy_construct_tests1(T*, test::random_generator const& generator) 27 { 28 typedef typename T::allocator_type allocator_type; 29 30 typename T::hasher hf; 31 typename T::key_equal eq; 32 typename T::allocator_type al; 33 34 { 35 test::check_instances check_; 36 37 T x; 38 T y(x); 39 BOOST_TEST(y.empty()); 40 BOOST_TEST(test::equivalent(y.hash_function(), hf)); 41 BOOST_TEST(test::equivalent(y.key_eq(), eq)); 42 BOOST_TEST(test::equivalent(y.get_allocator(), al)); 43 BOOST_TEST(x.max_load_factor() == y.max_load_factor()); 44 BOOST_TEST(test::selected_count(y.get_allocator()) == 45 (allocator_type::is_select_on_copy)); 46 test::check_equivalent_keys(y); 47 } 48 49 { 50 test::check_instances check_; 51 52 test::random_values<T> v(1000, generator); 53 54 T x(v.begin(), v.end()); 55 T y(x); 56 test::unordered_equivalence_tester<T> equivalent(x); 57 BOOST_TEST(equivalent(y)); 58 BOOST_TEST(test::selected_count(y.get_allocator()) == 59 (allocator_type::is_select_on_copy)); 60 test::check_equivalent_keys(y); 61 } 62 63 { 64 test::check_instances check_; 65 66 // In this test I drop the original containers max load factor, so it 67 // is much lower than the load factor. The hash table is not allowed 68 // to rehash, but the destination container should probably allocate 69 // enough buckets to decrease the load factor appropriately. 70 test::random_values<T> v(1000, generator); 71 T x(v.begin(), v.end()); 72 x.max_load_factor(x.load_factor() / 4); 73 T y(x); 74 test::unordered_equivalence_tester<T> equivalent(x); 75 BOOST_TEST(equivalent(y)); 76 // This isn't guaranteed: 77 BOOST_TEST(y.load_factor() < y.max_load_factor()); 78 BOOST_TEST(test::selected_count(y.get_allocator()) == 79 (allocator_type::is_select_on_copy)); 80 test::check_equivalent_keys(y); 81 } 82 } 83 84 template <class T> copy_construct_tests2(T *,test::random_generator const & generator)85 void copy_construct_tests2(T*, test::random_generator const& generator) 86 { 87 typename T::hasher hf(1); 88 typename T::key_equal eq(1); 89 typename T::allocator_type al(1); 90 typename T::allocator_type al2(2); 91 92 typedef typename T::allocator_type allocator_type; 93 94 { 95 test::check_instances check_; 96 97 T x(10000, hf, eq, al); 98 T y(x); 99 BOOST_TEST(y.empty()); 100 BOOST_TEST(test::equivalent(y.hash_function(), hf)); 101 BOOST_TEST(test::equivalent(y.key_eq(), eq)); 102 BOOST_TEST(test::equivalent(y.get_allocator(), al)); 103 BOOST_TEST(x.max_load_factor() == y.max_load_factor()); 104 BOOST_TEST(test::selected_count(y.get_allocator()) == 105 (allocator_type::is_select_on_copy)); 106 test::check_equivalent_keys(y); 107 } 108 109 { 110 test::check_instances check_; 111 112 T x(1000, hf, eq, al); 113 T y(x, al2); 114 BOOST_TEST(y.empty()); 115 BOOST_TEST(test::equivalent(y.hash_function(), hf)); 116 BOOST_TEST(test::equivalent(y.key_eq(), eq)); 117 BOOST_TEST(test::equivalent(y.get_allocator(), al2)); 118 BOOST_TEST(x.max_load_factor() == y.max_load_factor()); 119 BOOST_TEST(test::selected_count(y.get_allocator()) == 0); 120 test::check_equivalent_keys(y); 121 } 122 123 { 124 test::check_instances check_; 125 126 test::random_values<T> v(1000, generator); 127 128 T x(v.begin(), v.end(), 0, hf, eq, al); 129 T y(x); 130 test::unordered_equivalence_tester<T> equivalent(x); 131 BOOST_TEST(equivalent(y)); 132 test::check_equivalent_keys(y); 133 BOOST_TEST(test::selected_count(y.get_allocator()) == 134 (allocator_type::is_select_on_copy)); 135 BOOST_TEST(test::equivalent(y.get_allocator(), al)); 136 } 137 138 { 139 test::check_instances check_; 140 141 test::random_values<T> v(500, generator); 142 143 T x(v.begin(), v.end(), 0, hf, eq, al); 144 T y(x, al2); 145 test::unordered_equivalence_tester<T> equivalent(x); 146 BOOST_TEST(equivalent(y)); 147 test::check_equivalent_keys(y); 148 BOOST_TEST(test::selected_count(y.get_allocator()) == 0); 149 BOOST_TEST(test::equivalent(y.get_allocator(), al2)); 150 } 151 } 152 153 boost::unordered_set<test::object, test::hash, test::equal_to, 154 test::allocator1<test::object> >* test_set; 155 boost::unordered_multiset<test::object, test::hash, test::equal_to, 156 test::allocator2<test::object> >* test_multiset; 157 boost::unordered_map<test::object, test::object, test::hash, test::equal_to, 158 test::allocator1<test::object> >* test_map; 159 boost::unordered_multimap<test::object, test::object, test::hash, 160 test::equal_to, test::allocator2<test::object> >* test_multimap; 161 162 boost::unordered_set<test::object, test::hash, test::equal_to, 163 test::cxx11_allocator<test::object, test::select_copy> >* 164 test_set_select_copy; 165 boost::unordered_multiset<test::object, test::hash, test::equal_to, 166 test::cxx11_allocator<test::object, test::select_copy> >* 167 test_multiset_select_copy; 168 boost::unordered_map<test::object, test::object, test::hash, test::equal_to, 169 test::cxx11_allocator<test::object, test::select_copy> >* 170 test_map_select_copy; 171 boost::unordered_multimap<test::object, test::object, test::hash, 172 test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* 173 test_multimap_select_copy; 174 175 boost::unordered_set<test::object, test::hash, test::equal_to, 176 test::cxx11_allocator<test::object, test::no_select_copy> >* 177 test_set_no_select_copy; 178 boost::unordered_multiset<test::object, test::hash, test::equal_to, 179 test::cxx11_allocator<test::object, test::no_select_copy> >* 180 test_multiset_no_select_copy; 181 boost::unordered_map<test::object, test::object, test::hash, test::equal_to, 182 test::cxx11_allocator<test::object, test::no_select_copy> >* 183 test_map_no_select_copy; 184 boost::unordered_multimap<test::object, test::object, test::hash, 185 test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* 186 test_multimap_no_select_copy; 187 188 using test::default_generator; 189 using test::generate_collisions; 190 using test::limited_range; 191 192 UNORDERED_TEST(copy_construct_tests1, 193 ((test_set)(test_multiset)(test_map)(test_multimap)(test_set_select_copy)( 194 test_multiset_select_copy)(test_map_select_copy)( 195 test_multimap_select_copy)(test_set_no_select_copy)( 196 test_multiset_no_select_copy)(test_map_no_select_copy)( 197 test_multimap_no_select_copy))( 198 (default_generator)(generate_collisions)(limited_range))) 199 200 UNORDERED_TEST(copy_construct_tests2, 201 ((test_set)(test_multiset)(test_map)(test_multimap)(test_set_select_copy)( 202 test_multiset_select_copy)(test_map_select_copy)( 203 test_multimap_select_copy)(test_set_no_select_copy)( 204 test_multiset_no_select_copy)(test_map_no_select_copy)( 205 test_multimap_no_select_copy))( 206 (default_generator)(generate_collisions)(limited_range))) 207 } 208 209 RUN_TESTS() 210