]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/unordered_set/hash_policy/rehash.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / hash_policy / rehash.cc
CommitLineData
a5544970 1// Copyright (C) 2011-2019 Free Software Foundation, Inc.
da29608a
FD
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8//
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17//
52066eae 18// { dg-do run { target c++11 } }
da29608a
FD
19
20#include <unordered_set>
732eb076 21
da29608a
FD
22#include <testsuite_hooks.h>
23
732eb076 24template<typename _USet>
29dbb034 25 void test()
da29608a 26 {
29dbb034
FD
27 _USet us;
28 typedef typename _USet::size_type size_type;
29 bool rehashed = false;
30 for (int i = 0; i != 100000; ++i)
da29608a 31 {
29dbb034
FD
32 size_type bkt_count = us.bucket_count();
33 us.insert(i);
34 if (bkt_count != us.bucket_count())
da29608a 35 {
29dbb034
FD
36 // Container has been rehashed, lets check that it won't be rehash
37 // again if we remove and restore the last 2 inserted elements:
38 rehashed = true;
39 bkt_count = us.bucket_count();
40 VERIFY( us.erase(i) == 1 );
da29608a 41 VERIFY( bkt_count == us.bucket_count() );
29dbb034
FD
42 if (i > 0)
43 {
44 VERIFY( us.erase(i - 1) == 1 );
45 VERIFY( bkt_count == us.bucket_count() );
da29608a 46
29dbb034
FD
47 VERIFY( us.insert(i - 1).second );
48 VERIFY( bkt_count == us.bucket_count() );
49 }
50 VERIFY( us.insert(i).second );
da29608a
FD
51 VERIFY( bkt_count == us.bucket_count() );
52 }
da29608a 53 }
da29608a 54
29dbb034
FD
55 // At lest we check a rehash once:
56 VERIFY( rehashed );
57 }
da29608a 58
732eb076
FD
59template<typename _Value>
60 using unordered_set_power2_rehash =
61 std::_Hashtable<_Value, _Value, std::allocator<_Value>,
62 std::__detail::_Identity,
63 std::equal_to<_Value>,
64 std::hash<_Value>,
65 std::__detail::_Mask_range_hashing,
66 std::__detail::_Default_ranged_hash,
67 std::__detail::_Power2_rehash_policy,
68 std::__detail::_Hashtable_traits<false, true, true>>;
69
da29608a
FD
70int main()
71{
732eb076
FD
72 test<std::unordered_set<int>>();
73 test<unordered_set_power2_rehash<int>>();
da29608a
FD
74 return 0;
75}