// Copyright (C) 2023 Christian Mazakas // 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 "helpers.hpp" #include test::seed_t initialize_seed{674140082}; using test::default_generator; using test::limited_range; using test::sequential; using hasher = stateful_hash; using key_equal = stateful_key_equal; using allocator_type = stateful_allocator >; using map_type = boost::unordered::concurrent_flat_map; using map_value_type = typename map_type::value_type; namespace { template void clear_tests(G gen, test::random_generator rg) { auto values = make_random_values(1024 * 16, [&] { return gen(rg); }); raii::reset_counts(); map_type x(values.begin(), values.end(), values.size(), hasher(1), key_equal(2), allocator_type(3)); BOOST_TEST_EQ(raii::copy_constructor, 2 * x.size()); BOOST_TEST_EQ(raii::destructor, 0u); thread_runner(values, [&x](boost::span s) { (void)s; x.clear(); }); BOOST_TEST(x.empty()); check_raii_counts(); } template void insert_and_clear(G gen, test::random_generator rg) { auto values = make_random_values(1024 * 16, [&] { return gen(rg); }); auto reference_map = boost::unordered_flat_map(values.begin(), values.end()); raii::reset_counts(); std::thread t1, t2; { map_type x(0, hasher(1), key_equal(2), allocator_type(3)); std::mutex m; std::condition_variable cv; std::atomic done{false}; std::atomic num_clears{0}; bool ready = false; t1 = std::thread([&x, &values, &cv, &done, &m, &ready] { for (auto i = 0u; i < values.size(); ++i) { x.insert(values[i]); if (i % (values.size() / 128) == 0) { { std::unique_lock lk(m); ready = true; } cv.notify_all(); } } done = true; { std::unique_lock lk(m); ready = true; } cv.notify_all(); }); t2 = std::thread([&x, &m, &cv, &done, &ready, &num_clears] { do { { std::unique_lock lk(m); cv.wait(lk, [&ready] { return ready; }); ready = false; } x.clear(); ++num_clears; } while (!done); }); t1.join(); t2.join(); BOOST_TEST_GE(num_clears, 1u); if (!x.empty()) { test_fuzzy_matches_reference(x, reference_map, rg); } } check_raii_counts(); } } // namespace // clang-format off UNORDERED_TEST( clear_tests, ((value_type_generator)) ((default_generator)(sequential)(limited_range))) UNORDERED_TEST(insert_and_clear, ((value_type_generator)) ((default_generator)(sequential)(limited_range))) // clang-format on RUN_TESTS()