| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2011-2025 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 <string> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | std::size_t |
| 27 | get_nb_bucket_elems(const std::unordered_multiset<std::string>& us) |
| 28 | { |
| 29 | std::size_t nb = 0; |
| 30 | for (std::size_t b = 0; b != us.bucket_count(); ++b) |
| 31 | nb += us.bucket_size(b); |
| 32 | return nb; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void test01() |
| 37 | { |
| 38 | typedef std::unordered_multiset<std::string> Mset; |
| 39 | typedef Mset::iterator iterator; |
| 40 | typedef Mset::const_iterator const_iterator; |
| 41 | |
| 42 | Mset ms1; |
| 43 | |
| 44 | ms1.insert("foo"); |
| 45 | ms1.insert("foo"); |
| 46 | ms1.insert("foo"); |
| 47 | ms1.insert("foo"); |
| 48 | ms1.insert("foo"); |
| 49 | ms1.insert("foo"); |
| 50 | ms1.insert("bar"); |
| 51 | ms1.insert("bar"); |
| 52 | ms1.insert("bar"); |
| 53 | ms1.insert("bar"); |
| 54 | ms1.insert("bar"); |
| 55 | ms1.insert("bar"); |
| 56 | VERIFY( ms1.size() == 12 ); |
| 57 | VERIFY( get_nb_bucket_elems(ms1) == ms1.size() ); |
| 58 | VERIFY( distance(ms1.begin(), ms1.end()) - ms1.size() == 0 ); |
| 59 | |
| 60 | VERIFY( ms1.erase(ms1.begin()) != ms1.end() ); |
| 61 | VERIFY( ms1.size() == 11 ); |
| 62 | VERIFY( get_nb_bucket_elems(ms1) == ms1.size() ); |
| 63 | VERIFY( distance(ms1.begin(), ms1.end()) - ms1.size() == 0 ); |
| 64 | |
| 65 | auto it = ms1.begin(); |
| 66 | advance(it, 2); |
| 67 | VERIFY( ms1.erase(ms1.begin(), it) != ms1.end() ); |
| 68 | VERIFY( ms1.size() == 9 ); |
| 69 | VERIFY( get_nb_bucket_elems(ms1) == ms1.size() ); |
| 70 | VERIFY( distance(ms1.begin(), ms1.end()) - ms1.size() == 0 ); |
| 71 | |
| 72 | VERIFY( ms1.erase(*(ms1.begin())) == 3 ); |
| 73 | VERIFY( ms1.size() == 6 ); |
| 74 | VERIFY( get_nb_bucket_elems(ms1) == ms1.size() ); |
| 75 | VERIFY( distance(ms1.begin(), ms1.end()) - ms1.size() == 0 ); |
| 76 | |
| 77 | VERIFY( ms1.erase(ms1.begin(), ms1.end()) == ms1.end() ); |
| 78 | VERIFY( ms1.empty() ); |
| 79 | VERIFY( get_nb_bucket_elems(ms1) == ms1.size() ); |
| 80 | VERIFY( distance(ms1.begin(), ms1.end()) - ms1.size() == 0 ); |
| 81 | } |
| 82 | |
| 83 | int main() |
| 84 | { |
| 85 | test01(); |
| 86 | return 0; |
| 87 | } |