From f5d5299b88f30c43c2abd8a64195205ea93ee844 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Wed, 24 May 2023 11:06:45 -0700 Subject: [PATCH] Flesh out constructor exception tests --- test/cfoa/exception_constructor_tests.cpp | 195 +++++++++++++++++++--- 1 file changed, 174 insertions(+), 21 deletions(-) diff --git a/test/cfoa/exception_constructor_tests.cpp b/test/cfoa/exception_constructor_tests.cpp index 1a9bc653..e6c2f993 100644 --- a/test/cfoa/exception_constructor_tests.cpp +++ b/test/cfoa/exception_constructor_tests.cpp @@ -8,8 +8,11 @@ using allocator_type = stateful_allocator >; -using map_type = boost::unordered::concurrent_flat_map; +using hasher = stateful_hash; +using key_equal = stateful_key_equal; + +using map_type = boost::unordered::concurrent_flat_map; namespace { test::seed_t initialize_seed(795610904); @@ -32,26 +35,80 @@ namespace { BOOST_TEST(was_thrown); } - template - void iterator_bucket_count_constructor(G gen, test::random_generator rg) + template void iterator_range(G gen, test::random_generator rg) { auto values = make_random_values(1024 * 16, [&] { return gen(rg); }); - raii::reset_counts(); + { + raii::reset_counts(); - bool was_thrown = false; + bool was_thrown = false; - enable_exceptions(); - try { - map_type x(values.begin(), values.end(), 0, stateful_hash(1), - stateful_key_equal(2), allocator_type(3)); - } catch (...) { - was_thrown = true; + enable_exceptions(); + try { + map_type x(values.begin(), values.end(), 0, hasher(1), key_equal(2), + allocator_type(3)); + } catch (...) { + was_thrown = true; + } + disable_exceptions(); + + BOOST_TEST(was_thrown); + check_raii_counts(); } - disable_exceptions(); - BOOST_TEST(was_thrown); - check_raii_counts(); + { + raii::reset_counts(); + + bool was_thrown = false; + + enable_exceptions(); + try { + map_type x(values.begin(), values.end(), allocator_type(3)); + } catch (...) { + was_thrown = true; + } + disable_exceptions(); + + BOOST_TEST(was_thrown); + check_raii_counts(); + } + + { + raii::reset_counts(); + + bool was_thrown = false; + + enable_exceptions(); + try { + map_type x( + values.begin(), values.end(), values.size(), allocator_type(3)); + } catch (...) { + was_thrown = true; + } + disable_exceptions(); + + BOOST_TEST(was_thrown); + check_raii_counts(); + } + + { + raii::reset_counts(); + + bool was_thrown = false; + + enable_exceptions(); + try { + map_type x(values.begin(), values.end(), values.size(), hasher(1), + allocator_type(3)); + } catch (...) { + was_thrown = true; + } + disable_exceptions(); + + BOOST_TEST(was_thrown); + check_raii_counts(); + } } template void copy_constructor(G gen, test::random_generator rg) @@ -141,6 +198,107 @@ namespace { check_raii_counts(); } } + + UNORDERED_AUTO_TEST (initializer_list_bucket_count) { + using value_type = typename map_type::value_type; + + std::initializer_list values{ + value_type{raii{0}, raii{0}}, + value_type{raii{1}, raii{1}}, + value_type{raii{2}, raii{2}}, + value_type{raii{3}, raii{3}}, + value_type{raii{4}, raii{4}}, + value_type{raii{5}, raii{5}}, + value_type{raii{6}, raii{6}}, + value_type{raii{6}, raii{6}}, + value_type{raii{7}, raii{7}}, + value_type{raii{8}, raii{8}}, + value_type{raii{9}, raii{9}}, + value_type{raii{10}, raii{10}}, + value_type{raii{9}, raii{9}}, + value_type{raii{8}, raii{8}}, + value_type{raii{7}, raii{7}}, + value_type{raii{6}, raii{6}}, + value_type{raii{5}, raii{5}}, + value_type{raii{4}, raii{4}}, + value_type{raii{3}, raii{3}}, + value_type{raii{2}, raii{2}}, + value_type{raii{1}, raii{1}}, + value_type{raii{0}, raii{0}}, + }; + + { + raii::reset_counts(); + unsigned num_throws = 0; + + enable_exceptions(); + for (std::size_t i = 0; i < throw_threshold; ++i) { + try { + map_type x(values, 0, hasher(1), key_equal(2), allocator_type(3)); + } catch (...) { + ++num_throws; + } + } + disable_exceptions(); + + BOOST_TEST_GT(num_throws, 0u); + check_raii_counts(); + } + + { + raii::reset_counts(); + unsigned num_throws = 0; + + enable_exceptions(); + for (std::size_t i = 0; i < alloc_throw_threshold * 2; ++i) { + try { + map_type x(values, allocator_type(3)); + } catch (...) { + ++num_throws; + } + } + disable_exceptions(); + + BOOST_TEST_GT(num_throws, 0u); + check_raii_counts(); + } + + { + raii::reset_counts(); + unsigned num_throws = 0; + + enable_exceptions(); + for (std::size_t i = 0; i < alloc_throw_threshold * 2; ++i) { + try { + map_type x(values, values.size() * 2, allocator_type(3)); + } catch (...) { + ++num_throws; + } + } + disable_exceptions(); + + BOOST_TEST_GT(num_throws, 0u); + check_raii_counts(); + } + + { + raii::reset_counts(); + unsigned num_throws = 0; + + enable_exceptions(); + for (std::size_t i = 0; i < throw_threshold; ++i) { + try { + map_type x(values, values.size() * 2, hasher(1), allocator_type(3)); + } catch (...) { + ++num_throws; + } + } + disable_exceptions(); + + BOOST_TEST_GT(num_throws, 0u); + check_raii_counts(); + } + } } // namespace using test::default_generator; @@ -149,7 +307,7 @@ using test::sequential; // clang-format off UNORDERED_TEST( - iterator_bucket_count_constructor, + iterator_range, ((exception_value_type_generator)) ((default_generator)(sequential)(limited_range))) @@ -158,11 +316,6 @@ UNORDERED_TEST( ((exception_value_type_generator)) ((default_generator)(sequential))) -UNORDERED_TEST( - iterator_range_allocator_constructor, - ((exception_value_type_generator)) - ((default_generator)(sequential)(limited_range))) - UNORDERED_TEST( move_constructor, ((exception_value_type_generator))