| 1 | // Copyright (C) 2023 Christian Mazakas |
| 2 | // Copyright (C) 2023 Joaquin M Lopez Munoz |
| 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 "exception_helpers.hpp" |
| 7 | |
| 8 | #include <boost/unordered/concurrent_flat_map.hpp> |
| 9 | #include <boost/unordered/concurrent_flat_set.hpp> |
| 10 | |
| 11 | #include <boost/core/ignore_unused.hpp> |
| 12 | |
| 13 | using hasher = stateful_hash; |
| 14 | using key_equal = stateful_key_equal; |
| 15 | |
| 16 | using map_type = boost::unordered::concurrent_flat_map<raii, raii, hasher, |
| 17 | key_equal, stateful_allocator<std::pair<raii const, raii> > >; |
| 18 | |
| 19 | using set_type = boost::unordered::concurrent_flat_set<raii, hasher, |
| 20 | key_equal, stateful_allocator<raii> >; |
| 21 | |
| 22 | map_type* test_map; |
| 23 | set_type* test_set; |
| 24 | |
| 25 | namespace { |
| 26 | test::seed_t initialize_seed(223333016); |
| 27 | |
| 28 | template <class X, class GF> |
| 29 | void merge(X*, GF gen_factory, test::random_generator rg) |
| 30 | { |
| 31 | using allocator_type = typename X::allocator_type; |
| 32 | |
| 33 | auto gen = gen_factory.template get<X>(); |
| 34 | auto values = make_random_values(1024 * 16, [&] { return gen(rg); }); |
| 35 | auto reference_cont = reference_container<X>(values.begin(), values.end()); |
| 36 | |
| 37 | raii::reset_counts(); |
| 38 | |
| 39 | auto begin = values.begin(); |
| 40 | auto mid = begin + static_cast<std::ptrdiff_t>(values.size() / 2); |
| 41 | auto end = values.end(); |
| 42 | |
| 43 | { |
| 44 | unsigned num_throws = 0; |
| 45 | |
| 46 | for (unsigned i = 0; i < 5 * alloc_throw_threshold; ++i) { |
| 47 | disable_exceptions(); |
| 48 | |
| 49 | X x1(0, hasher(1), key_equal(2), allocator_type(3)); |
| 50 | x1.insert(begin, mid); |
| 51 | |
| 52 | X x2(0, hasher(2), key_equal(1), allocator_type(3)); |
| 53 | x2.insert(mid, end); |
| 54 | |
| 55 | enable_exceptions(); |
| 56 | try { |
| 57 | x1.merge(x2); |
| 58 | } catch (...) { |
| 59 | ++num_throws; |
| 60 | } |
| 61 | |
| 62 | disable_exceptions(); |
| 63 | test_fuzzy_matches_reference(x1, reference_cont, rg); |
| 64 | test_fuzzy_matches_reference(x2, reference_cont, rg); |
| 65 | } |
| 66 | |
| 67 | BOOST_TEST_GT(num_throws, 0u); |
| 68 | } |
| 69 | |
| 70 | check_raii_counts(); |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | using test::default_generator; |
| 76 | using test::limited_range; |
| 77 | using test::sequential; |
| 78 | |
| 79 | // clang-format off |
| 80 | UNORDERED_TEST( |
| 81 | merge, |
| 82 | ((test_map)(test_set)) |
| 83 | ((exception_value_type_generator_factory)) |
| 84 | ((default_generator)(sequential)(limited_range))) |
| 85 | |
| 86 | // clang-format on |
| 87 | |
| 88 | RUN_TESTS() |
| 89 | |