// { dg-do run { target c++11 } } // Copyright (C) 2011-2017 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING3. If not see // . #include #include #include #include #include template typename _USet> void test01() { // Make sure whatever happen we restore throw allocator limit at exit. __gnu_cxx::limit_condition::adjustor_base adj; typedef std::numeric_limits nl_size_t; _USet, std::equal_to, __gnu_cxx::throw_allocator_limit > us; const int nb = 100; int scheduled_throw_counter = 0; std::size_t thrown_exceptions = 0; for (int i = 0; i != nb; ++i) { if ((float)(us.size() + 1) / (float)us.bucket_count() >= us.max_load_factor()) { // We are going to need a rehash, lets introduce allocation issues: __gnu_cxx::limit_condition::set_limit(scheduled_throw_counter++); } try { VERIFY(us.insert(i).second); scheduled_throw_counter = 0; } catch (const __gnu_cxx::forced_error&) { ++thrown_exceptions; --i; } VERIFY( us.load_factor() <= us.max_load_factor() ); __gnu_cxx::limit_condition::set_limit(nl_size_t::max()); } VERIFY( thrown_exceptions != 0 ); // Check that all values have been inserted: for (int i = 0; i != nb; ++i) { VERIFY( us.count(i) == 1 ); } } template typename _USet> void test02() { // Make sure whatever happen we restore throw allocator limit at exit. __gnu_cxx::limit_condition::adjustor_base adj; typedef std::numeric_limits nl_size_t; _USet, std::equal_to, __gnu_cxx::throw_allocator_limit > us; const int nb = 100; int scheduled_throw_counter = 0; std::size_t thrown_exceptions = 0; for (int i = 0; i != nb; ++i) { if ((float)(us.size() + 2) / (float)us.bucket_count() >= us.max_load_factor()) { // We are going to need a rehash, lets introduce allocation issues: __gnu_cxx::limit_condition::set_limit(scheduled_throw_counter++); } try { std::vector v = { i, i }; // Check the insert range robustness us.insert(v.begin(), v.end()); scheduled_throw_counter = 0; } catch (const __gnu_cxx::forced_error&) { ++thrown_exceptions; --i; } VERIFY( us.load_factor() <= us.max_load_factor() ); __gnu_cxx::limit_condition::set_limit(nl_size_t::max()); } VERIFY( thrown_exceptions != 0 ); // Check that all values have been inserted: for (int i = 0; i != nb; ++i) { VERIFY( us.count(i) == 1 ); } } template using unordered_set_power2_rehash = std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mask_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Power2_rehash_policy, std::__detail::_Hashtable_traits>; int main() { test01(); test01(); test02(); test02(); return 0; }