merge_exception_tests.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017-2018 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/exception_test.hpp"
  5. #include "../helpers/invariants.hpp"
  6. #include "../helpers/metafunctions.hpp"
  7. #include "../helpers/random_values.hpp"
  8. #include "./containers.hpp"
  9. template <typename T1, typename T2> void merge_exception_test(T1 x, T2 y)
  10. {
  11. std::size_t size = x.size() + y.size();
  12. try {
  13. ENABLE_EXCEPTIONS;
  14. x.merge(y);
  15. } catch (...) {
  16. test::check_equivalent_keys(x);
  17. test::check_equivalent_keys(y);
  18. throw;
  19. }
  20. // Not a full check, just want to make sure the merge completed.
  21. BOOST_TEST(size == x.size() + y.size());
  22. if (y.size()) {
  23. BOOST_TEST(test::has_unique_keys<T1>::value);
  24. for (typename T2::iterator it = y.begin(); it != y.end(); ++it) {
  25. BOOST_TEST(x.find(test::get_key<T2>(*it)) != x.end());
  26. }
  27. }
  28. test::check_equivalent_keys(x);
  29. test::check_equivalent_keys(y);
  30. }
  31. template <typename T1, typename T2>
  32. void merge_exception_test(T1 const*, T2 const*, std::size_t count12, int tag12,
  33. test::random_generator gen1, test::random_generator gen2)
  34. {
  35. std::size_t count1 = count12 / 256;
  36. std::size_t count2 = count12 % 256;
  37. int tag1 = tag12 / 256;
  38. int tag2 = tag12 % 256;
  39. test::random_values<T1> v1(count1, gen1);
  40. test::random_values<T2> v2(count2, gen2);
  41. T1 x(v1.begin(), v1.end(), 0, test::exception::hash(tag1),
  42. test::exception::equal_to(tag1));
  43. T2 y(v2.begin(), v2.end(), 0, test::exception::hash(tag2),
  44. test::exception::equal_to(tag2));
  45. EXCEPTION_LOOP(merge_exception_test(x, y))
  46. }
  47. boost::unordered_set<test::exception::object, test::exception::hash,
  48. test::exception::equal_to,
  49. test::exception::allocator<test::exception::object> >* test_set_;
  50. boost::unordered_multiset<test::exception::object, test::exception::hash,
  51. test::exception::equal_to,
  52. test::exception::allocator<test::exception::object> >* test_multiset_;
  53. boost::unordered_map<test::exception::object, test::exception::object,
  54. test::exception::hash, test::exception::equal_to,
  55. test::exception::allocator2<test::exception::object> >* test_map_;
  56. boost::unordered_multimap<test::exception::object, test::exception::object,
  57. test::exception::hash, test::exception::equal_to,
  58. test::exception::allocator2<test::exception::object> >* test_multimap_;
  59. using test::default_generator;
  60. using test::generate_collisions;
  61. using test::limited_range;
  62. // clang-format off
  63. UNORDERED_MULTI_TEST(set_merge, merge_exception_test,
  64. ((test_set_)(test_multiset_))
  65. ((test_set_)(test_multiset_))
  66. ((0x0000)(0x6400)(0x0064)(0x0a64)(0x3232))
  67. ((0x0000)(0x0001)(0x0102))
  68. ((default_generator)(limited_range))
  69. ((default_generator)(limited_range))
  70. )
  71. UNORDERED_MULTI_TEST(map_merge, merge_exception_test,
  72. ((test_map_)(test_multimap_))
  73. ((test_map_)(test_multimap_))
  74. ((0x0000)(0x6400)(0x0064)(0x0a64)(0x3232))
  75. ((0x0101)(0x0200)(0x0201))
  76. ((default_generator)(limited_range))
  77. ((default_generator)(limited_range))
  78. )
  79. // Run fewer generate_collisions tests, as they're slow.
  80. UNORDERED_MULTI_TEST(set_merge_collisions, merge_exception_test,
  81. ((test_set_)(test_multiset_))
  82. ((test_set_)(test_multiset_))
  83. ((0x0a0a))
  84. ((0x0202)(0x0100)(0x0201))
  85. ((generate_collisions))
  86. ((generate_collisions))
  87. )
  88. UNORDERED_MULTI_TEST(map_merge_collisions, merge_exception_test,
  89. ((test_map_)(test_multimap_))
  90. ((test_map_)(test_multimap_))
  91. ((0x0a0a))
  92. ((0x0000)(0x0002)(0x0102))
  93. ((generate_collisions))
  94. ((generate_collisions))
  95. )
  96. // clang-format on
  97. RUN_TESTS_QUIET()