| 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( \ |
| 14 | disable : 4512) // move_assignment operator could not be generated |
| 15 | #endif |
| 16 | |
| 17 | test::seed_t initialize_seed(12847); |
| 18 | |
| 19 | template <class T> struct move_assign_base : public test::exception_base |
| 20 | { |
| 21 | test::random_values<T> x_values, y_values; |
| 22 | T x, y; |
| 23 | int t1, t2; |
| 24 | |
| 25 | typedef typename T::hasher hasher; |
| 26 | typedef typename T::key_equal key_equal; |
| 27 | typedef typename T::allocator_type allocator_type; |
| 28 | |
| 29 | move_assign_base(int tag1, int tag2, float mlf1 = 1.0, float mlf2 = 1.0) |
| 30 | : x_values(), y_values(), |
| 31 | x(0, hasher(tag1), key_equal(tag1), allocator_type(tag1)), |
| 32 | y(0, hasher(tag2), key_equal(tag2), allocator_type(tag2)), |
| 33 | t1(tag1), |
| 34 | t2(tag2) |
| 35 | { |
| 36 | x.max_load_factor(mlf1); |
| 37 | y.max_load_factor(mlf2); |
| 38 | } |
| 39 | |
| 40 | typedef T data_type; |
| 41 | T init() const { return T(x); } |
| 42 | void run(T& x1) const |
| 43 | { |
| 44 | test::exceptions_enable disable_exceptions(false); |
| 45 | T y1 = y; |
| 46 | disable_exceptions.release(); |
| 47 | x1 = std::move(y1); |
| 48 | |
| 49 | DISABLE_EXCEPTIONS; |
| 50 | test::check_container(x1, y_values); |
| 51 | test::check_equivalent_keys(x1); |
| 52 | } |
| 53 | |
| 54 | void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x1) const |
| 55 | { |
| 56 | test::check_equivalent_keys(x1); |
| 57 | |
| 58 | if (x1.hash_function() == hasher(t1)) { |
| 59 | BOOST_TEST(x1.key_eq() == key_equal(t1)); |
| 60 | } |
| 61 | |
| 62 | if (x1.hash_function() == hasher(t2)) { |
| 63 | BOOST_TEST(x1.key_eq() == key_equal(t2)); |
| 64 | } |
| 65 | |
| 66 | if (x1.key_eq() == key_equal(t1)) { |
| 67 | BOOST_TEST(x1.hash_function() == hasher(t1)); |
| 68 | } |
| 69 | |
| 70 | if (x1.key_eq() == key_equal(t2)) { |
| 71 | BOOST_TEST(x1.hash_function() == hasher(t2)); |
| 72 | } |
| 73 | |
| 74 | // If the container is empty at the point of the exception, the |
| 75 | // internal structure is hidden, this exposes it, at the cost of |
| 76 | // messing up the data. |
| 77 | if (x_values.size()) { |
| 78 | T& x2 = const_cast<T&>(x1); |
| 79 | x2.emplace(*x_values.begin()); |
| 80 | test::check_equivalent_keys(x2); |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | template <class T> struct move_assign_values : move_assign_base<T> |
| 86 | { |
| 87 | move_assign_values(unsigned int count1, unsigned int count2, int tag1, |
| 88 | int tag2, float mlf1 = 1.0, float mlf2 = 1.0) |
| 89 | : move_assign_base<T>(tag1, tag2, mlf1, mlf2) |
| 90 | { |
| 91 | this->x_values.fill(count1, test::limited_range); |
| 92 | this->y_values.fill(count2, test::limited_range); |
| 93 | this->x.insert(this->x_values.begin(), this->x_values.end()); |
| 94 | this->y.insert(this->y_values.begin(), this->y_values.end()); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | template <class T> struct move_assign_test1 : move_assign_values<T> |
| 99 | { |
| 100 | move_assign_test1() : move_assign_values<T>(0, 0, 0, 0) {} |
| 101 | }; |
| 102 | |
| 103 | template <class T> struct move_assign_test2 : move_assign_values<T> |
| 104 | { |
| 105 | move_assign_test2() : move_assign_values<T>(60, 0, 0, 0) {} |
| 106 | }; |
| 107 | |
| 108 | template <class T> struct move_assign_test3 : move_assign_values<T> |
| 109 | { |
| 110 | move_assign_test3() : move_assign_values<T>(0, 60, 0, 0) {} |
| 111 | }; |
| 112 | |
| 113 | template <class T> struct move_assign_test4 : move_assign_values<T> |
| 114 | { |
| 115 | move_assign_test4() : move_assign_values<T>(10, 10, 1, 2) {} |
| 116 | }; |
| 117 | |
| 118 | template <class T> struct move_assign_test4a : move_assign_values<T> |
| 119 | { |
| 120 | move_assign_test4a() : move_assign_values<T>(10, 100, 1, 2) {} |
| 121 | }; |
| 122 | |
| 123 | template <class T> struct move_assign_test5 : move_assign_values<T> |
| 124 | { |
| 125 | move_assign_test5() : move_assign_values<T>(5, 60, 0, 0, 1.0f, 0.1f) {} |
| 126 | }; |
| 127 | |
| 128 | template <class T> struct equivalent_test1 : move_assign_base<T> |
| 129 | { |
| 130 | equivalent_test1() : move_assign_base<T>(0, 0) |
| 131 | { |
| 132 | test::random_values<T> x_values2(10, test::limited_range); |
| 133 | this->x_values.insert(x_values2.begin(), x_values2.end()); |
| 134 | this->x_values.insert(x_values2.begin(), x_values2.end()); |
| 135 | test::random_values<T> y_values2(10, test::limited_range); |
| 136 | this->y_values.insert(y_values2.begin(), y_values2.end()); |
| 137 | this->y_values.insert(y_values2.begin(), y_values2.end()); |
| 138 | this->x.insert(this->x_values.begin(), this->x_values.end()); |
| 139 | this->y.insert(this->y_values.begin(), this->y_values.end()); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | // clang-format off |
| 144 | EXCEPTION_TESTS( |
| 145 | (move_assign_test1)(move_assign_test2)(move_assign_test3) |
| 146 | (move_assign_test4)(move_assign_test4a)(move_assign_test5) |
| 147 | (equivalent_test1), |
| 148 | CONTAINER_SEQ) |
| 149 | // clang-format on |
| 150 | |
| 151 | RUN_TESTS() |
| 152 | |