| 1 | // Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 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 | // |
| 18 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <unordered_set> |
| 21 | |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | template<typename _USet> |
| 25 | void test() |
| 26 | { |
| 27 | _USet us; |
| 28 | typedef typename _USet::size_type size_type; |
| 29 | bool rehashed = false; |
| 30 | for (int i = 0; i != 100000; ++i) |
| 31 | { |
| 32 | size_type bkt_count = us.bucket_count(); |
| 33 | us.insert(i); |
| 34 | if (bkt_count != us.bucket_count()) |
| 35 | { |
| 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 ); |
| 41 | VERIFY( bkt_count == us.bucket_count() ); |
| 42 | if (i > 0) |
| 43 | { |
| 44 | VERIFY( us.erase(i - 1) == 1 ); |
| 45 | VERIFY( bkt_count == us.bucket_count() ); |
| 46 | |
| 47 | VERIFY( us.insert(i - 1).second ); |
| 48 | VERIFY( bkt_count == us.bucket_count() ); |
| 49 | } |
| 50 | VERIFY( us.insert(i).second ); |
| 51 | VERIFY( bkt_count == us.bucket_count() ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // At lest we check a rehash once: |
| 56 | VERIFY( rehashed ); |
| 57 | } |
| 58 | |
| 59 | template<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 | |
| 70 | int main() |
| 71 | { |
| 72 | test<std::unordered_set<int>>(); |
| 73 | test<unordered_set_power2_rehash<int>>(); |
| 74 | return 0; |
| 75 | } |