| 1 | // Copyright (C) 2013-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 | // Insert with hint, specific to this library implementation. |
| 21 | |
| 22 | #include <iterator> |
| 23 | #include <unordered_map> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void test01() |
| 27 | { |
| 28 | typedef std::unordered_multimap<int, int> Map; |
| 29 | typedef typename Map::value_type Pair; |
| 30 | |
| 31 | Map m; |
| 32 | m.reserve(3); |
| 33 | |
| 34 | auto it1 = m.insert(Pair(0, 0)); |
| 35 | VERIFY( it1 != m.end() ); |
| 36 | VERIFY( it1->first == 0 ); |
| 37 | VERIFY( it1->second == 0 ); |
| 38 | |
| 39 | auto it2 = m.insert(it1, Pair(0, 2)); |
| 40 | VERIFY( it2 != m.end() ); |
| 41 | VERIFY( it2 != it1 ); |
| 42 | VERIFY( it2->second == 2 ); |
| 43 | VERIFY( it2 == std::next(it1) ); |
| 44 | |
| 45 | Pair p(0, 1); |
| 46 | it2 = m.insert(it1, p); |
| 47 | VERIFY( it2 == std::next(it1) ); |
| 48 | } |
| 49 | |
| 50 | struct hasher |
| 51 | { |
| 52 | std::size_t operator()(int val) const |
| 53 | { return val / 2; } |
| 54 | }; |
| 55 | |
| 56 | void test02() |
| 57 | { |
| 58 | typedef std::unordered_multimap<int, int, hasher> Map; |
| 59 | typedef typename Map::value_type Pair; |
| 60 | |
| 61 | Map m; |
| 62 | m.reserve(5); |
| 63 | |
| 64 | auto it1 = m.insert(Pair(0, 0)); |
| 65 | auto it2 = m.insert(it1, Pair(1, 0)); |
| 66 | VERIFY( m.bucket(it1->first) == m.bucket(it2->first) ); |
| 67 | VERIFY( m.bucket_size(m.bucket(it1->first)) == 2 ); |
| 68 | |
| 69 | auto it3 = m.insert(it2, Pair(2, 0)); |
| 70 | VERIFY( m.bucket(it3->first) != m.bucket(it2->first) ); |
| 71 | VERIFY( m.bucket_size(m.bucket(it3->first)) == 1 ); |
| 72 | |
| 73 | auto it4 = m.insert(it1, Pair(0, 1)); |
| 74 | VERIFY( it4 == std::next(it1) ); |
| 75 | VERIFY( m.bucket_size(m.bucket(it1->first)) == 3 ); |
| 76 | VERIFY( m.bucket_size(m.bucket(it3->first)) == 1 ); |
| 77 | |
| 78 | Pair p(1, 1); |
| 79 | it4 = m.insert(it2, p); |
| 80 | VERIFY( it4 == std::next(it2) ); |
| 81 | VERIFY( m.bucket_size(m.bucket(it1->first)) == 4 ); |
| 82 | auto range = m.equal_range(0); |
| 83 | VERIFY( std::distance(range.first, range.second) == 2 ); |
| 84 | range = m.equal_range(1); |
| 85 | VERIFY( std::distance(range.first, range.second) == 2 ); |
| 86 | } |
| 87 | |
| 88 | void test03() |
| 89 | { |
| 90 | typedef std::unordered_multimap<int, int> Map; |
| 91 | typedef typename Map::value_type Pair; |
| 92 | |
| 93 | Map m; |
| 94 | m.reserve(3); |
| 95 | |
| 96 | auto it1 = m.insert(Pair(0, 0)); |
| 97 | VERIFY( it1 != m.end() ); |
| 98 | VERIFY( it1->first == 0 ); |
| 99 | VERIFY( it1->second == 0 ); |
| 100 | |
| 101 | auto it2 = m.emplace_hint(it1, std::piecewise_construct, |
| 102 | std::make_tuple(0), |
| 103 | std::make_tuple(2)); |
| 104 | VERIFY( it2 != m.end() ); |
| 105 | VERIFY( it2 != it1 ); |
| 106 | VERIFY( it2->second == 2 ); |
| 107 | VERIFY( it2 == std::next(it1) ); |
| 108 | |
| 109 | Pair p(0, 1); |
| 110 | it2 = m.emplace_hint(it1, p); |
| 111 | VERIFY( it2 == std::next(it1) ); |
| 112 | } |
| 113 | |
| 114 | int main() |
| 115 | { |
| 116 | test01(); |
| 117 | test02(); |
| 118 | test03(); |
| 119 | return 0; |
| 120 | } |