/Src/Dependencies/Boost/libs/unordered/test/exception/swap_exception_tests.cpp

http://hadesmem.googlecode.com/ · C++ · 125 lines · 98 code · 24 blank · 3 comment · 10 complexity · fb62df6282ef16db06b9b98f7f24a0af MD5 · raw file

  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 "../helpers/prefix.hpp"
  5. #include "./containers.hpp"
  6. #include "../helpers/random_values.hpp"
  7. #include "../helpers/invariants.hpp"
  8. #if defined(BOOST_MSVC)
  9. #pragma warning(disable:4512) // assignment operator could not be generated
  10. #endif
  11. test::seed_t seed(9387);
  12. template <class T>
  13. struct self_swap_base : public test::exception_base
  14. {
  15. test::random_values<T> values;
  16. self_swap_base(int count = 0) : values(count) {}
  17. typedef T data_type;
  18. T init() const { return T(values.begin(), values.end()); }
  19. void run(T& x) const { x.swap(x); }
  20. void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const {
  21. std::string scope(test::scope);
  22. #if BOOST_UNORDERED_SWAP_METHOD != 2
  23. BOOST_TEST(
  24. scope == "hash::operator(hash)" ||
  25. scope == "hash::operator=(hash)" ||
  26. scope == "equal_to::operator(equal_to)" ||
  27. scope == "equal_to::operator=(equal_to)");
  28. #endif
  29. test::check_equivalent_keys(x);
  30. }
  31. };
  32. template <class T>
  33. struct self_swap_test1 : self_swap_base<T> {};
  34. template <class T>
  35. struct self_swap_test2 : self_swap_base<T>
  36. {
  37. self_swap_test2() : self_swap_base<T>(100) {}
  38. };
  39. template <class T>
  40. struct swap_base : public test::exception_base
  41. {
  42. const test::random_values<T> x_values, y_values;
  43. const T initial_x, initial_y;
  44. typedef BOOST_DEDUCED_TYPENAME T::hasher hasher;
  45. typedef BOOST_DEDUCED_TYPENAME T::key_equal key_equal;
  46. typedef BOOST_DEDUCED_TYPENAME T::allocator_type allocator_type;
  47. swap_base(unsigned int count1, unsigned int count2, int tag1, int tag2)
  48. : x_values(count1), y_values(count2),
  49. initial_x(x_values.begin(), x_values.end(), 0, hasher(tag1),
  50. key_equal(tag1), allocator_type(tag1)),
  51. initial_y(y_values.begin(), y_values.end(), 0, hasher(tag2),
  52. key_equal(tag2), allocator_type(tag2))
  53. {}
  54. struct data_type {
  55. data_type(T const& x, T const& y)
  56. : x(x), y(y) {}
  57. T x, y;
  58. };
  59. data_type init() const { return data_type(initial_x, initial_y); }
  60. void run(data_type& d) const {
  61. try {
  62. d.x.swap(d.y);
  63. } catch (std::runtime_error) {}
  64. }
  65. void check BOOST_PREVENT_MACRO_SUBSTITUTION(data_type const& d) const {
  66. std::string scope(test::scope);
  67. #if BOOST_UNORDERED_SWAP_METHOD != 2
  68. BOOST_TEST(
  69. scope == "hash::operator(hash)" ||
  70. scope == "hash::operator=(hash)" ||
  71. scope == "equal_to::operator(equal_to)" ||
  72. scope == "equal_to::operator=(equal_to)");
  73. #endif
  74. test::check_equivalent_keys(d.x);
  75. test::check_equivalent_keys(d.y);
  76. }
  77. };
  78. template <class T>
  79. struct swap_test1 : swap_base<T>
  80. {
  81. swap_test1() : swap_base<T>(0, 0, 0, 0) {}
  82. };
  83. template <class T>
  84. struct swap_test2 : swap_base<T>
  85. {
  86. swap_test2() : swap_base<T>(60, 0, 0, 0) {}
  87. };
  88. template <class T>
  89. struct swap_test3 : swap_base<T>
  90. {
  91. swap_test3() : swap_base<T>(0, 60, 0, 0) {}
  92. };
  93. template <class T>
  94. struct swap_test4 : swap_base<T>
  95. {
  96. swap_test4() : swap_base<T>(10, 10, 1, 2) {}
  97. };
  98. RUN_EXCEPTION_TESTS(
  99. (self_swap_test1)(self_swap_test2)
  100. (swap_test1)(swap_test2)(swap_test3)(swap_test4),
  101. CONTAINER_SEQ)