| 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 | |
| 8 | #include "../helpers/invariants.hpp" |
| 9 | #include "../helpers/random_values.hpp" |
| 10 | #include "../helpers/tracker.hpp" |
| 11 | |
| 12 | #if defined(BOOST_MSVC) |
| 13 | #pragma warning(disable : 4512) // assignment operator could not be generated |
| 14 | #endif |
| 15 | |
| 16 | #if defined(__clang__) && defined(__has_warning) |
| 17 | #if __has_warning("-Wself-assign-overloaded") |
| 18 | #pragma clang diagnostic ignored "-Wself-assign-overloaded" |
| 19 | #endif |
| 20 | #endif |
| 21 | |
| 22 | test::seed_t initialize_seed(12847); |
| 23 | |
| 24 | template <class T> struct self_assign_base : public test::exception_base |
| 25 | { |
| 26 | test::random_values<T> values; |
| 27 | self_assign_base(std::size_t count = 0) : values(count, test::limited_range) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | typedef T data_type; |
| 32 | T init() const { return T(values.begin(), values.end()); } |
| 33 | |
| 34 | void run(T& x) const |
| 35 | { |
| 36 | x = x; |
| 37 | |
| 38 | DISABLE_EXCEPTIONS; |
| 39 | test::check_container(x, values); |
| 40 | test::check_equivalent_keys(x); |
| 41 | } |
| 42 | |
| 43 | void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const |
| 44 | { |
| 45 | test::check_equivalent_keys(x); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | template <class T> struct self_assign_test1 : self_assign_base<T> |
| 50 | { |
| 51 | }; |
| 52 | |
| 53 | template <class T> struct self_assign_test2 : self_assign_base<T> |
| 54 | { |
| 55 | self_assign_test2() : self_assign_base<T>(100) {} |
| 56 | }; |
| 57 | |
| 58 | template <class T> struct assign_base : public test::exception_base |
| 59 | { |
| 60 | test::random_values<T> x_values, y_values; |
| 61 | T x, y; |
| 62 | |
| 63 | int t1; |
| 64 | int t2; |
| 65 | |
| 66 | typedef typename T::hasher hasher; |
| 67 | typedef typename T::key_equal key_equal; |
| 68 | typedef typename T::allocator_type allocator_type; |
| 69 | |
| 70 | assign_base(int tag1, int tag2, float mlf1 = 1.0, float mlf2 = 1.0) |
| 71 | : x_values(), y_values(), |
| 72 | x(0, hasher(tag1), key_equal(tag1), allocator_type(tag1)), |
| 73 | y(0, hasher(tag2), key_equal(tag2), allocator_type(tag2)), |
| 74 | t1(tag1), |
| 75 | t2(tag2) |
| 76 | |
| 77 | { |
| 78 | x.max_load_factor(mlf1); |
| 79 | y.max_load_factor(mlf2); |
| 80 | } |
| 81 | |
| 82 | typedef T data_type; |
| 83 | T init() const { return T(x); } |
| 84 | |
| 85 | void run(T& x1) const |
| 86 | { |
| 87 | x1 = y; |
| 88 | |
| 89 | DISABLE_EXCEPTIONS; |
| 90 | test::check_container(x1, y_values); |
| 91 | test::check_equivalent_keys(x1); |
| 92 | } |
| 93 | |
| 94 | void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x1) const |
| 95 | { |
| 96 | test::check_equivalent_keys(x1); |
| 97 | |
| 98 | if (x1.hash_function() == hasher(t1)) { |
| 99 | BOOST_TEST(x1.key_eq() == key_equal(t1)); |
| 100 | } |
| 101 | |
| 102 | if (x1.hash_function() == hasher(t2)) { |
| 103 | BOOST_TEST(x1.key_eq() == key_equal(t2)); |
| 104 | } |
| 105 | |
| 106 | if (x1.key_eq() == key_equal(t1)) { |
| 107 | BOOST_TEST(x1.hash_function() == hasher(t1)); |
| 108 | } |
| 109 | |
| 110 | if (x1.key_eq() == key_equal(t2)) { |
| 111 | BOOST_TEST(x1.hash_function() == hasher(t2)); |
| 112 | } |
| 113 | |
| 114 | // If the container is empty at the point of the exception, the |
| 115 | // internal structure is hidden, this exposes it, at the cost of |
| 116 | // messing up the data. |
| 117 | if (x_values.size()) { |
| 118 | T& x2 = const_cast<T&>(x1); |
| 119 | x2.emplace(*x_values.begin()); |
| 120 | test::check_equivalent_keys(x2); |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | template <class T> struct assign_values : assign_base<T> |
| 126 | { |
| 127 | assign_values(unsigned int count1, unsigned int count2, int tag1, int tag2, |
| 128 | test::random_generator gen = test::default_generator, float mlf1 = 1.0, |
| 129 | float mlf2 = 1.0) |
| 130 | : assign_base<T>(tag1, tag2, mlf1, mlf2) |
| 131 | { |
| 132 | this->x_values.fill(count1, gen); |
| 133 | this->y_values.fill(count2, gen); |
| 134 | this->x.insert(this->x_values.begin(), this->x_values.end()); |
| 135 | this->y.insert(this->y_values.begin(), this->y_values.end()); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | template <class T> struct assign_test1 : assign_values<T> |
| 140 | { |
| 141 | assign_test1() : assign_values<T>(0, 0, 0, 0) {} |
| 142 | }; |
| 143 | |
| 144 | template <class T> struct assign_test2 : assign_values<T> |
| 145 | { |
| 146 | assign_test2() : assign_values<T>(60, 0, 0, 0) {} |
| 147 | }; |
| 148 | |
| 149 | template <class T> struct assign_test2a : assign_values<T> |
| 150 | { |
| 151 | assign_test2a() : assign_values<T>(60, 0, 0, 0, test::limited_range) {} |
| 152 | }; |
| 153 | |
| 154 | template <class T> struct assign_test3 : assign_values<T> |
| 155 | { |
| 156 | assign_test3() : assign_values<T>(0, 60, 0, 0) {} |
| 157 | }; |
| 158 | |
| 159 | template <class T> struct assign_test3a : assign_values<T> |
| 160 | { |
| 161 | assign_test3a() : assign_values<T>(0, 60, 0, 0, test::limited_range) {} |
| 162 | }; |
| 163 | |
| 164 | template <class T> struct assign_test4 : assign_values<T> |
| 165 | { |
| 166 | assign_test4() : assign_values<T>(10, 10, 1, 2) {} |
| 167 | }; |
| 168 | |
| 169 | template <class T> struct assign_test4a : assign_values<T> |
| 170 | { |
| 171 | assign_test4a() : assign_values<T>(10, 100, 1, 2) {} |
| 172 | }; |
| 173 | |
| 174 | template <class T> struct assign_test4b : assign_values<T> |
| 175 | { |
| 176 | assign_test4b() : assign_values<T>(10, 100, 1, 2, test::limited_range) {} |
| 177 | }; |
| 178 | |
| 179 | template <class T> struct assign_test5 : assign_values<T> |
| 180 | { |
| 181 | assign_test5() |
| 182 | : assign_values<T>(5, 60, 0, 0, test::default_generator, 1.0f, 0.1f) |
| 183 | { |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | template <class T> struct equivalent_test1 : assign_base<T> |
| 188 | { |
| 189 | equivalent_test1() : assign_base<T>(0, 0) |
| 190 | { |
| 191 | test::random_values<T> x_values2(10); |
| 192 | this->x_values.insert(x_values2.begin(), x_values2.end()); |
| 193 | this->x_values.insert(x_values2.begin(), x_values2.end()); |
| 194 | test::random_values<T> y_values2(10); |
| 195 | this->y_values.insert(y_values2.begin(), y_values2.end()); |
| 196 | this->y_values.insert(y_values2.begin(), y_values2.end()); |
| 197 | this->x.insert(this->x_values.begin(), this->x_values.end()); |
| 198 | this->y.insert(this->y_values.begin(), this->y_values.end()); |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | // clang-format off |
| 203 | EXCEPTION_TESTS_REPEAT(5, |
| 204 | (self_assign_test1)(self_assign_test2) |
| 205 | (assign_test1)(assign_test2)(assign_test2a) |
| 206 | (assign_test3)(assign_test3a) |
| 207 | (assign_test4)(assign_test4a)(assign_test4b) |
| 208 | (assign_test5) |
| 209 | (equivalent_test1), |
| 210 | CONTAINER_SEQ) |
| 211 | // clang-format on |
| 212 | |
| 213 | RUN_TESTS() |
| 214 | |