]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/unordered_set/insert/hash_policy.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / insert / hash_policy.cc
CommitLineData
be58e01d 1// { dg-do run { target c++11 } }
c41cafd5 2
fbd26352 3// Copyright (C) 2011-2019 Free Software Foundation, Inc.
c41cafd5 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 <vector>
22#include <limits>
47609c12 23
c41cafd5 24#include <ext/throw_allocator.h>
47609c12 25
c41cafd5 26#include <testsuite_hooks.h>
27
47609c12 28template<template<typename _Value, typename _Hash,
29 typename _Pred, typename _Alloc>
30 typename _USet>
31 void test01()
32 {
47609c12 33 // Make sure whatever happen we restore throw allocator limit at exit.
34 __gnu_cxx::limit_condition::adjustor_base adj;
c41cafd5 35
47609c12 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;
39 const int nb = 100;
40 int scheduled_throw_counter = 0;
41 std::size_t thrown_exceptions = 0;
42 for (int i = 0; i != nb; ++i)
43 {
44 if ((float)(us.size() + 1)
45 / (float)us.bucket_count() >= us.max_load_factor())
46 {
47 // We are going to need a rehash, lets introduce allocation issues:
48 __gnu_cxx::limit_condition::set_limit(scheduled_throw_counter++);
49 }
50 try
51 {
52 VERIFY(us.insert(i).second);
53 scheduled_throw_counter = 0;
54 }
55 catch (const __gnu_cxx::forced_error&)
56 {
57 ++thrown_exceptions;
58 --i;
59 }
60 VERIFY( us.load_factor() <= us.max_load_factor() );
61 __gnu_cxx::limit_condition::set_limit(nl_size_t::max());
62 }
c41cafd5 63
47609c12 64 VERIFY( thrown_exceptions != 0 );
65 // Check that all values have been inserted:
66 for (int i = 0; i != nb; ++i)
67 {
68 VERIFY( us.count(i) == 1 );
69 }
70 }
c41cafd5 71
47609c12 72template<template<typename _Value, typename _Hash,
73 typename _Pred, typename _Alloc>
74 typename _USet>
75 void test02()
76 {
47609c12 77 // Make sure whatever happen we restore throw allocator limit at exit.
78 __gnu_cxx::limit_condition::adjustor_base adj;
79
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;
83 const int nb = 100;
84 int scheduled_throw_counter = 0;
85 std::size_t thrown_exceptions = 0;
86 for (int i = 0; i != nb; ++i)
87 {
88 if ((float)(us.size() + 2)
89 / (float)us.bucket_count() >= us.max_load_factor())
90 {
91 // We are going to need a rehash, lets introduce allocation issues:
92 __gnu_cxx::limit_condition::set_limit(scheduled_throw_counter++);
93 }
94 try
95 {
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;
100 }
101 catch (const __gnu_cxx::forced_error&)
102 {
103 ++thrown_exceptions;
104 --i;
105 }
106 VERIFY( us.load_factor() <= us.max_load_factor() );
107 __gnu_cxx::limit_condition::set_limit(nl_size_t::max());
108 }
109
110 VERIFY( thrown_exceptions != 0 );
111 // Check that all values have been inserted:
112 for (int i = 0; i != nb; ++i)
113 {
114 VERIFY( us.count(i) == 1 );
115 }
116 }
117
118template<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,
123 _Pred,
124 _Hash,
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>>;
c41cafd5 129
130int main()
131{
47609c12 132 test01<std::unordered_set>();
133 test01<unordered_set_power2_rehash>();
134 test02<std::unordered_set>();
135 test02<unordered_set_power2_rehash>();
c41cafd5 136 return 0;
137}