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