]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_set/max_load_factor/robustness.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / max_load_factor / robustness.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10 //
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.
15 //
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/>.
19
20 #include <unordered_set>
21 #include <limits>
22 #include <ext/throw_allocator.h>
23 #include <testsuite_hooks.h>
24
25 template<template<typename _Value, typename _Hash,
26 typename _Pred, typename _Alloc>
27 typename _USet>
28 void test()
29 {
30 typedef std::numeric_limits<std::size_t> nl_size_t;
31 _USet<int, std::hash<int>, std::equal_to<int>,
32 __gnu_cxx::throw_allocator_limit<int> > us;
33 int val = 0;
34 for (; val != 100; ++val)
35 {
36 VERIFY( us.insert(val).second );
37 VERIFY( us.load_factor() <= us.max_load_factor() );
38 }
39
40 float cur_max_load_factor = us.max_load_factor();
41 int counter = 0;
42 std::size_t thrown_exceptions = 0;
43
44 // Reduce max load factor.
45 us.max_load_factor(us.max_load_factor() / 4);
46
47 // At this point load factor is higher than max_load_factor because we can't
48 // rehash in max_load_factor call.
49 VERIFY( us.load_factor() > us.max_load_factor() );
50
51 while (true)
52 {
53 __gnu_cxx::limit_condition::limit_adjustor adjustor(counter++);
54 bool do_break = false;
55 try
56 {
57 size_t nbkts = us.bucket_count();
58 // Check that unordered_set will still be correctly resized when
59 // needed.
60 VERIFY( us.insert(val++).second );
61 VERIFY( us.bucket_count() != nbkts );
62 VERIFY( us.load_factor() <= us.max_load_factor() );
63 do_break = true;
64 }
65 catch (const __gnu_cxx::forced_error&)
66 {
67 // max load factor doesn't change.
68 VERIFY( us.max_load_factor() == .25f );
69 ++thrown_exceptions;
70 }
71
72 if (do_break)
73 break;
74 }
75
76 VERIFY( thrown_exceptions > 0 );
77 }
78
79
80 template<typename _Value, typename _Hash,
81 typename _Pred, typename _Alloc>
82 using unordered_set_power2_rehash =
83 std::_Hashtable<_Value, _Value, _Alloc,
84 std::__detail::_Identity,
85 _Pred,
86 _Hash,
87 std::__detail::_Mask_range_hashing,
88 std::__detail::_Default_ranged_hash,
89 std::__detail::_Power2_rehash_policy,
90 std::__detail::_Hashtable_traits<false, true, true>>;
91
92 int main()
93 {
94 test<std::unordered_set>();
95 test<unordered_set_power2_rehash>();
96 return 0;
97 }