match.pd: Fix indefinite recursion during exp-log transformations [PR118490]
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multimap / insert / hint.cc
blob721a4d4fcea7dfdf7298ba51654829e80b2f5164
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.
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/>.
18 // { dg-do run { target c++11 } }
20 // Insert with hint, specific to this library implementation.
22 #include <iterator>
23 #include <unordered_map>
24 #include <testsuite_hooks.h>
26 void test01()
28 typedef std::unordered_multimap<int, int> Map;
29 typedef typename Map::value_type Pair;
31 Map m;
32 m.reserve(3);
34 auto it1 = m.insert(Pair(0, 0));
35 VERIFY( it1 != m.end() );
36 VERIFY( it1->first == 0 );
37 VERIFY( it1->second == 0 );
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) );
45 Pair p(0, 1);
46 it2 = m.insert(it1, p);
47 VERIFY( it2 == std::next(it1) );
50 struct hasher
52 std::size_t operator()(int val) const
53 { return val / 2; }
56 void test02()
58 typedef std::unordered_multimap<int, int, hasher> Map;
59 typedef typename Map::value_type Pair;
61 Map m;
62 m.reserve(5);
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 );
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 );
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 );
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 );
88 void test03()
90 typedef std::unordered_multimap<int, int> Map;
91 typedef typename Map::value_type Pair;
93 Map m;
94 m.reserve(3);
96 auto it1 = m.insert(Pair(0, 0));
97 VERIFY( it1 != m.end() );
98 VERIFY( it1->first == 0 );
99 VERIFY( it1->second == 0 );
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) );
109 Pair p(0, 1);
110 it2 = m.emplace_hint(it1, p);
111 VERIFY( it2 == std::next(it1) );
114 int main()
116 test01();
117 test02();
118 test03();
119 return 0;