1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2011-2023 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <unordered_set>
24 #include <ext/throw_allocator.h>
26 #include <testsuite_hooks.h>
28 template<template<typename _Value
, typename _Hash
,
29 typename _Pred
, typename _Alloc
>
33 // Make sure whatever happen we restore throw allocator limit at exit.
34 __gnu_cxx::limit_condition::adjustor_base adj
;
36 typedef std::numeric_limits
<std::size_t> nl_size_t
;
37 _USet
<int, std::hash
<int>, std::equal_to
<int>,
38 __gnu_cxx::throw_allocator_limit
<int> > us
;
40 int scheduled_throw_counter
= 0;
41 std::size_t thrown_exceptions
= 0;
42 for (int i
= 0; i
!= nb
; ++i
)
44 if ((float)(us
.size() + 1)
45 / (float)us
.bucket_count() >= us
.max_load_factor())
47 // We are going to need a rehash, lets introduce allocation issues:
48 __gnu_cxx::limit_condition::set_limit(scheduled_throw_counter
++);
52 VERIFY(us
.insert(i
).second
);
53 scheduled_throw_counter
= 0;
55 catch (const __gnu_cxx::forced_error
&)
60 VERIFY( us
.load_factor() <= us
.max_load_factor() );
61 __gnu_cxx::limit_condition::set_limit(nl_size_t::max());
64 VERIFY( thrown_exceptions
!= 0 );
65 // Check that all values have been inserted:
66 for (int i
= 0; i
!= nb
; ++i
)
68 VERIFY( us
.count(i
) == 1 );
72 template<template<typename _Value
, typename _Hash
,
73 typename _Pred
, typename _Alloc
>
77 // Make sure whatever happen we restore throw allocator limit at exit.
78 __gnu_cxx::limit_condition::adjustor_base adj
;
80 typedef std::numeric_limits
<std::size_t> nl_size_t
;
81 _USet
<int, std::hash
<int>, std::equal_to
<int>,
82 __gnu_cxx::throw_allocator_limit
<int> > us
;
84 int scheduled_throw_counter
= 0;
85 std::size_t thrown_exceptions
= 0;
86 for (int i
= 0; i
!= nb
; ++i
)
88 if ((float)(us
.size() + 2)
89 / (float)us
.bucket_count() >= us
.max_load_factor())
91 // We are going to need a rehash, lets introduce allocation issues:
92 __gnu_cxx::limit_condition::set_limit(scheduled_throw_counter
++);
96 std::vector
<int> v
= { i
, i
};
97 // Check the insert range robustness
98 us
.insert(v
.begin(), v
.end());
99 scheduled_throw_counter
= 0;
101 catch (const __gnu_cxx::forced_error
&)
106 VERIFY( us
.load_factor() <= us
.max_load_factor() );
107 __gnu_cxx::limit_condition::set_limit(nl_size_t::max());
110 VERIFY( thrown_exceptions
!= 0 );
111 // Check that all values have been inserted:
112 for (int i
= 0; i
!= nb
; ++i
)
114 VERIFY( us
.count(i
) == 1 );
118 template<typename _Value
, typename _Hash
,
119 typename _Pred
, typename _Alloc
>
120 using unordered_set_power2_rehash
=
121 std::_Hashtable
<_Value
, _Value
, _Alloc
,
122 std::__detail::_Identity
,
125 std::__detail::_Mask_range_hashing
,
126 std::__detail::_Default_ranged_hash
,
127 std::__detail::_Power2_rehash_policy
,
128 std::__detail::_Hashtable_traits
<false, true, true>>;
132 test01
<std::unordered_set
>();
133 test01
<unordered_set_power2_rehash
>();
134 test02
<std::unordered_set
>();
135 test02
<unordered_set_power2_rehash
>();