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