move_assign_exception_tests.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2006-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "./containers.hpp"
  5. #include "../helpers/invariants.hpp"
  6. #include "../helpers/random_values.hpp"
  7. #include "../helpers/tracker.hpp"
  8. #if defined(BOOST_MSVC)
  9. #pragma warning( \
  10. disable : 4512) // move_assignment operator could not be generated
  11. #endif
  12. test::seed_t initialize_seed(12847);
  13. template <class T> struct move_assign_base : public test::exception_base
  14. {
  15. test::random_values<T> x_values, y_values;
  16. T x, y;
  17. typedef typename T::hasher hasher;
  18. typedef typename T::key_equal key_equal;
  19. typedef typename T::allocator_type allocator_type;
  20. move_assign_base(int tag1, int tag2, float mlf1 = 1.0, float mlf2 = 1.0)
  21. : x_values(), y_values(),
  22. x(0, hasher(tag1), key_equal(tag1), allocator_type(tag1)),
  23. y(0, hasher(tag2), key_equal(tag2), allocator_type(tag2))
  24. {
  25. x.max_load_factor(mlf1);
  26. y.max_load_factor(mlf2);
  27. }
  28. typedef T data_type;
  29. T init() const { return T(x); }
  30. void run(T& x1) const
  31. {
  32. test::exceptions_enable disable_exceptions(false);
  33. T y1 = y;
  34. disable_exceptions.release();
  35. x1 = boost::move(y1);
  36. DISABLE_EXCEPTIONS;
  37. test::check_container(x1, y_values);
  38. test::check_equivalent_keys(x1);
  39. }
  40. void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x1) const
  41. {
  42. test::check_equivalent_keys(x1);
  43. // If the container is empty at the point of the exception, the
  44. // internal structure is hidden, this exposes it, at the cost of
  45. // messing up the data.
  46. if (x_values.size()) {
  47. T& x2 = const_cast<T&>(x1);
  48. x2.emplace(*x_values.begin());
  49. test::check_equivalent_keys(x2);
  50. }
  51. }
  52. };
  53. template <class T> struct move_assign_values : move_assign_base<T>
  54. {
  55. move_assign_values(unsigned int count1, unsigned int count2, int tag1,
  56. int tag2, float mlf1 = 1.0, float mlf2 = 1.0)
  57. : move_assign_base<T>(tag1, tag2, mlf1, mlf2)
  58. {
  59. this->x_values.fill(count1, test::limited_range);
  60. this->y_values.fill(count2, test::limited_range);
  61. this->x.insert(this->x_values.begin(), this->x_values.end());
  62. this->y.insert(this->y_values.begin(), this->y_values.end());
  63. }
  64. };
  65. template <class T> struct move_assign_test1 : move_assign_values<T>
  66. {
  67. move_assign_test1() : move_assign_values<T>(0, 0, 0, 0) {}
  68. };
  69. template <class T> struct move_assign_test2 : move_assign_values<T>
  70. {
  71. move_assign_test2() : move_assign_values<T>(60, 0, 0, 0) {}
  72. };
  73. template <class T> struct move_assign_test3 : move_assign_values<T>
  74. {
  75. move_assign_test3() : move_assign_values<T>(0, 60, 0, 0) {}
  76. };
  77. template <class T> struct move_assign_test4 : move_assign_values<T>
  78. {
  79. move_assign_test4() : move_assign_values<T>(10, 10, 1, 2) {}
  80. };
  81. template <class T> struct move_assign_test4a : move_assign_values<T>
  82. {
  83. move_assign_test4a() : move_assign_values<T>(10, 100, 1, 2) {}
  84. };
  85. template <class T> struct move_assign_test5 : move_assign_values<T>
  86. {
  87. move_assign_test5() : move_assign_values<T>(5, 60, 0, 0, 1.0f, 0.1f) {}
  88. };
  89. template <class T> struct equivalent_test1 : move_assign_base<T>
  90. {
  91. equivalent_test1() : move_assign_base<T>(0, 0)
  92. {
  93. test::random_values<T> x_values2(10, test::limited_range);
  94. this->x_values.insert(x_values2.begin(), x_values2.end());
  95. this->x_values.insert(x_values2.begin(), x_values2.end());
  96. test::random_values<T> y_values2(10, test::limited_range);
  97. this->y_values.insert(y_values2.begin(), y_values2.end());
  98. this->y_values.insert(y_values2.begin(), y_values2.end());
  99. this->x.insert(this->x_values.begin(), this->x_values.end());
  100. this->y.insert(this->y_values.begin(), this->y_values.end());
  101. }
  102. };
  103. // clang-format off
  104. EXCEPTION_TESTS(
  105. (move_assign_test1)(move_assign_test2)(move_assign_test3)
  106. (move_assign_test4)(move_assign_test4a)(move_assign_test5)
  107. (equivalent_test1),
  108. CONTAINER_SEQ)
  109. // clang-format on
  110. RUN_TESTS()