swap_exception_tests.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(disable : 4512) // assignment operator could not be generated
  10. #endif
  11. test::seed_t initialize_seed(9387);
  12. template <class T> struct self_swap_base : public test::exception_base
  13. {
  14. test::random_values<T> values;
  15. self_swap_base(std::size_t count = 0) : values(count, test::limited_range) {}
  16. typedef T data_type;
  17. T init() const { return T(values.begin(), values.end()); }
  18. void run(T& x) const
  19. {
  20. x.swap(x);
  21. DISABLE_EXCEPTIONS;
  22. test::check_container(x, this->values);
  23. test::check_equivalent_keys(x);
  24. }
  25. void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const
  26. {
  27. std::string scope(test::scope);
  28. // TODO: In C++11 exceptions are only allowed in the swap function.
  29. BOOST_TEST(scope == "hash::hash(hash)" ||
  30. scope == "hash::operator=(hash)" ||
  31. scope == "equal_to::equal_to(equal_to)" ||
  32. scope == "equal_to::operator=(equal_to)");
  33. test::check_equivalent_keys(x);
  34. }
  35. };
  36. template <class T> struct self_swap_test1 : self_swap_base<T>
  37. {
  38. };
  39. template <class T> struct self_swap_test2 : self_swap_base<T>
  40. {
  41. self_swap_test2() : self_swap_base<T>(100) {}
  42. };
  43. template <class T> struct swap_base : public test::exception_base
  44. {
  45. const test::random_values<T> x_values, y_values;
  46. const T initial_x, initial_y;
  47. typedef typename T::hasher hasher;
  48. typedef typename T::key_equal key_equal;
  49. typedef typename T::allocator_type allocator_type;
  50. swap_base(unsigned int count1, unsigned int count2, int tag1, int tag2)
  51. : x_values(count1, test::limited_range),
  52. y_values(count2, test::limited_range),
  53. initial_x(x_values.begin(), x_values.end(), 0, hasher(tag1),
  54. key_equal(tag1), allocator_type(tag1)),
  55. initial_y(y_values.begin(), y_values.end(), 0, hasher(tag2),
  56. key_equal(tag2),
  57. allocator_type(T::allocator_type::propagate_on_container_swap::value
  58. ? tag2
  59. : tag1))
  60. {
  61. }
  62. struct data_type
  63. {
  64. data_type(T const& x_, T const& y_) : x(x_), y(y_) {}
  65. T x, y;
  66. };
  67. data_type init() const { return data_type(initial_x, initial_y); }
  68. void run(data_type& d) const
  69. {
  70. try {
  71. d.x.swap(d.y);
  72. } catch (std::runtime_error&) {
  73. }
  74. DISABLE_EXCEPTIONS;
  75. test::check_container(d.x, this->y_values);
  76. test::check_equivalent_keys(d.x);
  77. test::check_container(d.y, this->x_values);
  78. test::check_equivalent_keys(d.y);
  79. }
  80. void check BOOST_PREVENT_MACRO_SUBSTITUTION(data_type const& d) const
  81. {
  82. std::string scope(test::scope);
  83. // TODO: In C++11 exceptions are only allowed in the swap function.
  84. BOOST_TEST(scope == "hash::hash(hash)" ||
  85. scope == "hash::operator=(hash)" ||
  86. scope == "equal_to::equal_to(equal_to)" ||
  87. scope == "equal_to::operator=(equal_to)");
  88. test::check_equivalent_keys(d.x);
  89. test::check_equivalent_keys(d.y);
  90. }
  91. };
  92. template <class T> struct swap_test1 : swap_base<T>
  93. {
  94. swap_test1() : swap_base<T>(0, 0, 0, 0) {}
  95. };
  96. template <class T> struct swap_test2 : swap_base<T>
  97. {
  98. swap_test2() : swap_base<T>(60, 0, 0, 0) {}
  99. };
  100. template <class T> struct swap_test3 : swap_base<T>
  101. {
  102. swap_test3() : swap_base<T>(0, 60, 0, 0) {}
  103. };
  104. template <class T> struct swap_test4 : swap_base<T>
  105. {
  106. swap_test4() : swap_base<T>(10, 10, 1, 2) {}
  107. };
  108. // clang-format off
  109. EXCEPTION_TESTS(
  110. (self_swap_test1)(self_swap_test2)
  111. (swap_test1)(swap_test2)(swap_test3)(swap_test4),
  112. CONTAINER_SEQ)
  113. // clang-format on
  114. RUN_TESTS()