Files
unordered/test/cfoa/exception_erase_tests.cpp

313 lines
9.3 KiB
C++
Raw Normal View History

2023-05-22 10:11:37 -07:00
// Copyright (C) 2023 Christian Mazakas
// Copyright (C) 2023-2024 Joaquin M Lopez Munoz
2023-05-22 10:11:37 -07:00
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "exception_helpers.hpp"
#include <boost/unordered/concurrent_flat_map.hpp>
2023-09-10 18:35:51 +02:00
#include <boost/unordered/concurrent_flat_set.hpp>
#include <boost/unordered/concurrent_node_map.hpp>
#include <boost/unordered/concurrent_node_set.hpp>
2023-05-22 10:11:37 -07:00
#include <boost/core/ignore_unused.hpp>
namespace {
test::seed_t initialize_seed(3202923);
struct lvalue_eraser_type
{
template <class T, class X> void operator()(std::vector<T>& values, X& x)
{
2023-09-10 18:35:51 +02:00
static constexpr auto value_type_cardinality =
value_cardinality<typename X::value_type>::value;
2023-05-22 10:11:37 -07:00
std::atomic<std::uint64_t> num_erased{0};
2023-05-23 08:43:10 -07:00
2023-05-22 10:11:37 -07:00
auto const old_size = x.size();
auto const old_dc = +raii::default_constructor;
auto const old_cc = +raii::copy_constructor;
auto const old_mc = +raii::move_constructor;
auto const old_d = +raii::destructor;
enable_exceptions();
thread_runner(values, [&values, &num_erased, &x](boost::span<T>) {
2023-09-10 18:35:51 +02:00
for (auto const& v : values) {
2023-05-22 10:11:37 -07:00
try {
2023-09-10 18:35:51 +02:00
auto count = x.erase(get_key(v));
2023-05-22 10:11:37 -07:00
BOOST_TEST_LE(count, 1u);
BOOST_TEST_GE(count, 0u);
2023-05-23 08:43:10 -07:00
num_erased += count;
2023-05-22 10:11:37 -07:00
} catch (...) {
}
}
});
disable_exceptions();
2023-05-23 08:43:10 -07:00
BOOST_TEST_EQ(x.size(), old_size - num_erased);
2023-05-22 10:11:37 -07:00
BOOST_TEST_EQ(raii::default_constructor, old_dc);
BOOST_TEST_EQ(raii::copy_constructor, old_cc);
BOOST_TEST_EQ(raii::move_constructor, old_mc);
2023-09-10 18:35:51 +02:00
BOOST_TEST_EQ(
raii::destructor, old_d + value_type_cardinality * num_erased);
2023-05-22 10:11:37 -07:00
}
} lvalue_eraser;
struct lvalue_eraser_if_type
{
template <class T, class X> void operator()(std::vector<T>& values, X& x)
{
using value_type = typename X::value_type;
2023-09-10 18:35:51 +02:00
static constexpr auto value_type_cardinality =
value_cardinality<value_type>::value;
// concurrent_flat_set visit is always const access
using arg_type = typename std::conditional<
std::is_same<typename X::key_type, typename X::value_type>::value,
typename X::value_type const,
typename X::value_type
>::type;
2023-05-22 10:11:37 -07:00
std::atomic<std::uint64_t> num_erased{0};
auto const old_size = x.size();
auto const old_dc = +raii::default_constructor;
auto const old_cc = +raii::copy_constructor;
auto const old_mc = +raii::move_constructor;
auto const old_d = +raii::destructor;
auto max = 0;
x.visit_all([&max](value_type const& v) {
2023-09-10 18:35:51 +02:00
if (get_value(v).x_ > max) {
max = get_value(v).x_;
2023-05-22 10:11:37 -07:00
}
});
auto threshold = max / 2;
auto expected_erasures = 0u;
x.visit_all([&expected_erasures, threshold](value_type const& v) {
2023-09-10 18:35:51 +02:00
if (get_value(v).x_ > threshold) {
2023-05-22 10:11:37 -07:00
++expected_erasures;
}
});
enable_exceptions();
thread_runner(values, [&num_erased, &x, threshold](boost::span<T> s) {
2023-09-10 18:35:51 +02:00
for (auto const& v : s) {
2023-05-22 10:11:37 -07:00
try {
2023-09-10 18:35:51 +02:00
auto count = x.erase_if(get_key(v),
[threshold](arg_type& w) { return get_value(w).x_ > threshold; });
2023-05-22 10:11:37 -07:00
num_erased += count;
BOOST_TEST_LE(count, 1u);
BOOST_TEST_GE(count, 0u);
} catch (...) {
}
}
});
disable_exceptions();
BOOST_TEST_LE(num_erased, expected_erasures);
BOOST_TEST_EQ(x.size(), old_size - num_erased);
BOOST_TEST_EQ(raii::default_constructor, old_dc);
BOOST_TEST_EQ(raii::copy_constructor, old_cc);
BOOST_TEST_EQ(raii::move_constructor, old_mc);
2023-09-10 18:35:51 +02:00
BOOST_TEST_EQ(
raii::destructor, old_d + value_type_cardinality * num_erased);
2023-05-22 10:11:37 -07:00
}
} lvalue_eraser_if;
struct erase_if_type
{
template <class T, class X> void operator()(std::vector<T>& values, X& x)
{
using value_type = typename X::value_type;
2023-09-10 18:35:51 +02:00
static constexpr auto value_type_cardinality =
value_cardinality<value_type>::value;
// concurrent_flat_set visit is always const access
using arg_type = typename std::conditional<
std::is_same<typename X::key_type, typename X::value_type>::value,
typename X::value_type const,
typename X::value_type
>::type;
2023-05-22 10:11:37 -07:00
auto const old_size = x.size();
auto const old_dc = +raii::default_constructor;
auto const old_cc = +raii::copy_constructor;
auto const old_mc = +raii::move_constructor;
auto const old_d = +raii::destructor;
auto max = 0;
x.visit_all([&max](value_type const& v) {
2023-09-10 18:35:51 +02:00
if (get_value(v).x_ > max) {
max = get_value(v).x_;
2023-05-22 10:11:37 -07:00
}
});
auto threshold = max / 2;
auto expected_erasures = 0u;
x.visit_all([&expected_erasures, threshold](value_type const& v) {
2023-09-10 18:35:51 +02:00
if (get_value(v).x_ > threshold) {
2023-05-22 10:11:37 -07:00
++expected_erasures;
}
});
enable_exceptions();
thread_runner(values, [&x, threshold](boost::span<T> /* s */) {
for (std::size_t i = 0; i < 256; ++i) {
try {
2023-09-10 18:35:51 +02:00
x.erase_if([threshold](arg_type& v) {
static std::atomic<std::uint32_t> c{0};
auto t = ++c;
if (should_throw && (t % throw_threshold == 0)) {
throw exception_tag{};
}
2023-09-10 18:35:51 +02:00
return get_value(v).x_ > threshold;
});
} catch (...) {
2023-05-22 10:11:37 -07:00
}
}
});
2023-05-22 10:11:37 -07:00
disable_exceptions();
BOOST_TEST_EQ(raii::default_constructor, old_dc);
BOOST_TEST_EQ(raii::copy_constructor, old_cc);
BOOST_TEST_EQ(raii::move_constructor, old_mc);
2023-09-10 18:35:51 +02:00
BOOST_TEST_EQ(
raii::destructor,
old_d + value_type_cardinality * (old_size - x.size()));
2023-05-22 10:11:37 -07:00
}
} erase_if;
struct free_fn_erase_if_type
{
template <class T, class X> void operator()(std::vector<T>& values, X& x)
{
using value_type = typename X::value_type;
2023-09-10 18:35:51 +02:00
static constexpr auto value_type_cardinality =
value_cardinality<value_type>::value;
// concurrent_flat_set visit is always const access
using arg_type = typename std::conditional<
std::is_same<typename X::key_type, typename X::value_type>::value,
typename X::value_type const,
typename X::value_type
>::type;
2023-05-22 10:11:37 -07:00
auto const old_size = x.size();
auto const old_dc = +raii::default_constructor;
auto const old_cc = +raii::copy_constructor;
auto const old_mc = +raii::move_constructor;
auto const old_d = +raii::destructor;
auto max = 0;
x.visit_all([&max](value_type const& v) {
2023-09-10 18:35:51 +02:00
if (get_value(v).x_ > max) {
max = get_value(v).x_;
2023-05-22 10:11:37 -07:00
}
});
auto threshold = max / 2;
enable_exceptions();
thread_runner(values, [&x, threshold](boost::span<T> /* s */) {
for (std::size_t i = 0; i < 256; ++i) {
try {
2023-09-10 18:35:51 +02:00
boost::unordered::erase_if(x, [threshold](arg_type& v) {
static std::atomic<std::uint32_t> c{0};
auto t = ++c;
if (should_throw && (t % throw_threshold == 0)) {
throw exception_tag{};
}
2023-09-10 18:35:51 +02:00
return get_value(v).x_ > threshold;
});
} catch (...) {
2023-05-22 10:11:37 -07:00
}
}
});
2023-05-22 10:11:37 -07:00
disable_exceptions();
BOOST_TEST_EQ(raii::default_constructor, old_dc);
BOOST_TEST_EQ(raii::copy_constructor, old_cc);
BOOST_TEST_EQ(raii::move_constructor, old_mc);
2023-09-10 18:35:51 +02:00
BOOST_TEST_EQ(
raii::destructor,
old_d + value_type_cardinality * (old_size - x.size()));
2023-05-22 10:11:37 -07:00
}
} free_fn_erase_if;
2023-09-10 18:35:51 +02:00
template <class X, class GF, class F>
void erase(X*, GF gen_factory, F eraser, test::random_generator rg)
2023-05-22 10:11:37 -07:00
{
2023-09-10 18:35:51 +02:00
auto gen = gen_factory.template get<X>();
2023-05-22 10:11:37 -07:00
auto values = make_random_values(1024 * 16, [&] { return gen(rg); });
2023-09-10 18:35:51 +02:00
auto reference_cont = reference_container<X>(values.begin(), values.end());
2023-05-23 08:43:10 -07:00
2023-05-22 10:11:37 -07:00
raii::reset_counts();
{
2023-05-23 08:43:10 -07:00
X x(values.size());
for (auto const& v : values) {
x.insert(v);
}
2023-05-22 10:11:37 -07:00
2023-09-10 18:35:51 +02:00
BOOST_TEST_EQ(x.size(), reference_cont.size());
2023-05-23 08:43:10 -07:00
BOOST_TEST_EQ(raii::destructor, 0u);
2023-05-22 10:11:37 -07:00
2023-09-10 18:35:51 +02:00
test_fuzzy_matches_reference(x, reference_cont, rg);
2023-05-22 10:11:37 -07:00
eraser(values, x);
2023-09-10 18:35:51 +02:00
test_fuzzy_matches_reference(x, reference_cont, rg);
2023-05-22 10:11:37 -07:00
}
check_raii_counts();
}
boost::unordered::concurrent_flat_map<raii, raii, stateful_hash,
stateful_key_equal, stateful_allocator<std::pair<raii const, raii> > >* map;
boost::unordered::concurrent_node_map<raii, raii, stateful_hash,
stateful_key_equal, stateful_allocator<std::pair<raii const, raii> > >* node_map;
2023-09-10 18:35:51 +02:00
boost::unordered::concurrent_flat_set<raii, stateful_hash,
stateful_key_equal, stateful_allocator<raii> >* set;
boost::unordered::concurrent_node_set<raii, stateful_hash,
stateful_key_equal, stateful_allocator<raii> >* node_set;
2023-05-22 10:11:37 -07:00
} // namespace
using test::default_generator;
using test::limited_range;
using test::sequential;
// clang-format off
UNORDERED_TEST(
erase,
((map)(node_map)(set)(node_set))
2023-09-10 18:35:51 +02:00
((exception_value_type_generator_factory)
(exception_init_type_generator_factory))
2023-05-22 10:11:37 -07:00
((lvalue_eraser)(lvalue_eraser_if)(erase_if)(free_fn_erase_if))
((default_generator)(sequential)(limited_range)))
// clang-format on
RUN_TESTS()